xref: /linux/mm/mmap.c (revision 997071bcb34005f42e0fe5bc7930e895b070f251)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * mm/mmap.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Written by obz.
51da177e4SLinus Torvalds  *
6046c6884SAlan Cox  * Address space accounting code	<alan@lxorguk.ukuu.org.uk>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/slab.h>
104af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/shm.h>
131da177e4SLinus Torvalds #include <linux/mman.h>
141da177e4SLinus Torvalds #include <linux/pagemap.h>
151da177e4SLinus Torvalds #include <linux/swap.h>
161da177e4SLinus Torvalds #include <linux/syscalls.h>
17c59ede7bSRandy.Dunlap #include <linux/capability.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/file.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/personality.h>
221da177e4SLinus Torvalds #include <linux/security.h>
231da177e4SLinus Torvalds #include <linux/hugetlb.h>
241da177e4SLinus Torvalds #include <linux/profile.h>
25b95f1b31SPaul Gortmaker #include <linux/export.h>
261da177e4SLinus Torvalds #include <linux/mount.h>
271da177e4SLinus Torvalds #include <linux/mempolicy.h>
281da177e4SLinus Torvalds #include <linux/rmap.h>
29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
30cdd6c482SIngo Molnar #include <linux/perf_event.h>
31120a795dSAl Viro #include <linux/audit.h>
32b15d00b6SAndrea Arcangeli #include <linux/khugepaged.h>
332b144498SSrikar Dronamraju #include <linux/uprobes.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds #include <asm/cacheflush.h>
371da177e4SLinus Torvalds #include <asm/tlb.h>
38d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h>
391da177e4SLinus Torvalds 
4042b77728SJan Beulich #include "internal.h"
4142b77728SJan Beulich 
423a459756SKirill Korotaev #ifndef arch_mmap_check
433a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags)	(0)
443a459756SKirill Korotaev #endif
453a459756SKirill Korotaev 
4608e7d9b5SMartin Schwidefsky #ifndef arch_rebalance_pgtables
4708e7d9b5SMartin Schwidefsky #define arch_rebalance_pgtables(addr, len)		(addr)
4808e7d9b5SMartin Schwidefsky #endif
4908e7d9b5SMartin Schwidefsky 
50e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm,
51e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
52e0da382cSHugh Dickins 		unsigned long start, unsigned long end);
53e0da382cSHugh Dickins 
541da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation.
551da177e4SLinus Torvalds  * this is due to the limited x86 page protection hardware.  The expected
561da177e4SLinus Torvalds  * behavior is in parens:
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * map_type	prot
591da177e4SLinus Torvalds  *		PROT_NONE	PROT_READ	PROT_WRITE	PROT_EXEC
601da177e4SLinus Torvalds  * MAP_SHARED	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
611da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (yes) yes	w: (no) no
621da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
631da177e4SLinus Torvalds  *
641da177e4SLinus Torvalds  * MAP_PRIVATE	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
651da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (copy) copy	w: (no) no
661da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
671da177e4SLinus Torvalds  *
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds pgprot_t protection_map[16] = {
701da177e4SLinus Torvalds 	__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
711da177e4SLinus Torvalds 	__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
74804af2cfSHugh Dickins pgprot_t vm_get_page_prot(unsigned long vm_flags)
75804af2cfSHugh Dickins {
76b845f313SDave Kleikamp 	return __pgprot(pgprot_val(protection_map[vm_flags &
77b845f313SDave Kleikamp 				(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
78b845f313SDave Kleikamp 			pgprot_val(arch_vm_get_page_prot(vm_flags)));
79804af2cfSHugh Dickins }
80804af2cfSHugh Dickins EXPORT_SYMBOL(vm_get_page_prot);
81804af2cfSHugh Dickins 
8234679d7eSShaohua Li int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;  /* heuristic overcommit */
8334679d7eSShaohua Li int sysctl_overcommit_ratio __read_mostly = 50;	/* default is 50% */
84c3d8c141SChristoph Lameter int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
8534679d7eSShaohua Li /*
8634679d7eSShaohua Li  * Make sure vm_committed_as in one cacheline and not cacheline shared with
8734679d7eSShaohua Li  * other variables. It can be updated by several CPUs frequently.
8834679d7eSShaohua Li  */
8934679d7eSShaohua Li struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /*
92*997071bcSK. Y. Srinivasan  * The global memory commitment made in the system can be a metric
93*997071bcSK. Y. Srinivasan  * that can be used to drive ballooning decisions when Linux is hosted
94*997071bcSK. Y. Srinivasan  * as a guest. On Hyper-V, the host implements a policy engine for dynamically
95*997071bcSK. Y. Srinivasan  * balancing memory across competing virtual machines that are hosted.
96*997071bcSK. Y. Srinivasan  * Several metrics drive this policy engine including the guest reported
97*997071bcSK. Y. Srinivasan  * memory commitment.
98*997071bcSK. Y. Srinivasan  */
99*997071bcSK. Y. Srinivasan unsigned long vm_memory_committed(void)
100*997071bcSK. Y. Srinivasan {
101*997071bcSK. Y. Srinivasan 	return percpu_counter_read_positive(&vm_committed_as);
102*997071bcSK. Y. Srinivasan }
103*997071bcSK. Y. Srinivasan EXPORT_SYMBOL_GPL(vm_memory_committed);
104*997071bcSK. Y. Srinivasan 
105*997071bcSK. Y. Srinivasan /*
1061da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
1071da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
1081da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
1091da177e4SLinus Torvalds  *
1101da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
1111da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
1121da177e4SLinus Torvalds  *
1131da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1141da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
1151da177e4SLinus Torvalds  *
1161da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1171da177e4SLinus Torvalds  *
1181da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
1191da177e4SLinus Torvalds  * wish to use this logic.
1201da177e4SLinus Torvalds  */
12134b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	unsigned long free, allowed;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	vm_acct_memory(pages);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/*
1281da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
1291da177e4SLinus Torvalds 	 */
1301da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1311da177e4SLinus Torvalds 		return 0;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
134c15bef30SDmitry Fink 		free = global_page_state(NR_FREE_PAGES);
135c15bef30SDmitry Fink 		free += global_page_state(NR_FILE_PAGES);
1361da177e4SLinus Torvalds 
137c15bef30SDmitry Fink 		/*
138c15bef30SDmitry Fink 		 * shmem pages shouldn't be counted as free in this
139c15bef30SDmitry Fink 		 * case, they can't be purged, only swapped out, and
140c15bef30SDmitry Fink 		 * that won't affect the overall amount of available
141c15bef30SDmitry Fink 		 * memory in the system.
142c15bef30SDmitry Fink 		 */
143c15bef30SDmitry Fink 		free -= global_page_state(NR_SHMEM);
144c15bef30SDmitry Fink 
1451da177e4SLinus Torvalds 		free += nr_swap_pages;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 		/*
1481da177e4SLinus Torvalds 		 * Any slabs which are created with the
1491da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1501da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
1511da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
1521da177e4SLinus Torvalds 		 */
153972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 		/*
156c15bef30SDmitry Fink 		 * Leave reserved pages. The pages are not for anonymous pages.
157c15bef30SDmitry Fink 		 */
158c15bef30SDmitry Fink 		if (free <= totalreserve_pages)
159c15bef30SDmitry Fink 			goto error;
160c15bef30SDmitry Fink 		else
161c15bef30SDmitry Fink 			free -= totalreserve_pages;
162c15bef30SDmitry Fink 
163c15bef30SDmitry Fink 		/*
1641da177e4SLinus Torvalds 		 * Leave the last 3% for root
1651da177e4SLinus Torvalds 		 */
1661da177e4SLinus Torvalds 		if (!cap_sys_admin)
1671da177e4SLinus Torvalds 			free -= free / 32;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 		if (free > pages)
1701da177e4SLinus Torvalds 			return 0;
1711da177e4SLinus Torvalds 
1726d9f7839SHideo AOKI 		goto error;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	allowed = (totalram_pages - hugetlb_total_pages())
1761da177e4SLinus Torvalds 	       	* sysctl_overcommit_ratio / 100;
1771da177e4SLinus Torvalds 	/*
1781da177e4SLinus Torvalds 	 * Leave the last 3% for root
1791da177e4SLinus Torvalds 	 */
1801da177e4SLinus Torvalds 	if (!cap_sys_admin)
1811da177e4SLinus Torvalds 		allowed -= allowed / 32;
1821da177e4SLinus Torvalds 	allowed += total_swap_pages;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
1851da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
186731572d3SAlan Cox 	if (mm)
18734b4e4aaSAlan Cox 		allowed -= mm->total_vm / 32;
1881da177e4SLinus Torvalds 
18900a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1901da177e4SLinus Torvalds 		return 0;
1916d9f7839SHideo AOKI error:
1921da177e4SLinus Torvalds 	vm_unacct_memory(pages);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	return -ENOMEM;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /*
1983d48ae45SPeter Zijlstra  * Requires inode->i_mapping->i_mmap_mutex
1991da177e4SLinus Torvalds  */
2001da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma,
2011da177e4SLinus Torvalds 		struct file *file, struct address_space *mapping)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	if (vma->vm_flags & VM_DENYWRITE)
204d3ac7f89SJosef "Jeff" Sipek 		atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
2051da177e4SLinus Torvalds 	if (vma->vm_flags & VM_SHARED)
2061da177e4SLinus Torvalds 		mapping->i_mmap_writable--;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	flush_dcache_mmap_lock(mapping);
2091da177e4SLinus Torvalds 	if (unlikely(vma->vm_flags & VM_NONLINEAR))
2106b2dbba8SMichel Lespinasse 		list_del_init(&vma->shared.nonlinear);
2111da177e4SLinus Torvalds 	else
2126b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
2131da177e4SLinus Torvalds 	flush_dcache_mmap_unlock(mapping);
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2176b2dbba8SMichel Lespinasse  * Unlink a file-based vm structure from its interval tree, to hide
218a8fb5618SHugh Dickins  * vma from rmap and vmtruncate before freeing its page tables.
2191da177e4SLinus Torvalds  */
220a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	if (file) {
2251da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
2263d48ae45SPeter Zijlstra 		mutex_lock(&mapping->i_mmap_mutex);
2271da177e4SLinus Torvalds 		__remove_shared_vm_struct(vma, file, mapping);
2283d48ae45SPeter Zijlstra 		mutex_unlock(&mapping->i_mmap_mutex);
2291da177e4SLinus Torvalds 	}
230a8fb5618SHugh Dickins }
231a8fb5618SHugh Dickins 
232a8fb5618SHugh Dickins /*
233a8fb5618SHugh Dickins  * Close a vm structure and free it, returning the next.
234a8fb5618SHugh Dickins  */
235a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
236a8fb5618SHugh Dickins {
237a8fb5618SHugh Dickins 	struct vm_area_struct *next = vma->vm_next;
238a8fb5618SHugh Dickins 
239a8fb5618SHugh Dickins 	might_sleep();
2401da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
2411da177e4SLinus Torvalds 		vma->vm_ops->close(vma);
242e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
243a8fb5618SHugh Dickins 		fput(vma->vm_file);
244f0be3d32SLee Schermerhorn 	mpol_put(vma_policy(vma));
2451da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
246a8fb5618SHugh Dickins 	return next;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249e4eb1ff6SLinus Torvalds static unsigned long do_brk(unsigned long addr, unsigned long len);
250e4eb1ff6SLinus Torvalds 
2516a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	unsigned long rlim, retval;
2541da177e4SLinus Torvalds 	unsigned long newbrk, oldbrk;
2551da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
256a5b4592cSJiri Kosina 	unsigned long min_brk;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
2591da177e4SLinus Torvalds 
260a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK
2615520e894SJiri Kosina 	/*
2625520e894SJiri Kosina 	 * CONFIG_COMPAT_BRK can still be overridden by setting
2635520e894SJiri Kosina 	 * randomize_va_space to 2, which will still cause mm->start_brk
2645520e894SJiri Kosina 	 * to be arbitrarily shifted
2655520e894SJiri Kosina 	 */
2664471a675SJiri Kosina 	if (current->brk_randomized)
2675520e894SJiri Kosina 		min_brk = mm->start_brk;
2685520e894SJiri Kosina 	else
2695520e894SJiri Kosina 		min_brk = mm->end_data;
270a5b4592cSJiri Kosina #else
271a5b4592cSJiri Kosina 	min_brk = mm->start_brk;
272a5b4592cSJiri Kosina #endif
273a5b4592cSJiri Kosina 	if (brk < min_brk)
2741da177e4SLinus Torvalds 		goto out;
2751e624196SRam Gupta 
2761e624196SRam Gupta 	/*
2771e624196SRam Gupta 	 * Check against rlimit here. If this check is done later after the test
2781e624196SRam Gupta 	 * of oldbrk with newbrk then it can escape the test and let the data
2791e624196SRam Gupta 	 * segment grow beyond its set limit the in case where the limit is
2801e624196SRam Gupta 	 * not page aligned -Ram Gupta
2811e624196SRam Gupta 	 */
28259e99e5bSJiri Slaby 	rlim = rlimit(RLIMIT_DATA);
283c1d171a0SJiri Kosina 	if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
284c1d171a0SJiri Kosina 			(mm->end_data - mm->start_data) > rlim)
2851e624196SRam Gupta 		goto out;
2861e624196SRam Gupta 
2871da177e4SLinus Torvalds 	newbrk = PAGE_ALIGN(brk);
2881da177e4SLinus Torvalds 	oldbrk = PAGE_ALIGN(mm->brk);
2891da177e4SLinus Torvalds 	if (oldbrk == newbrk)
2901da177e4SLinus Torvalds 		goto set_brk;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	/* Always allow shrinking brk. */
2931da177e4SLinus Torvalds 	if (brk <= mm->brk) {
2941da177e4SLinus Torvalds 		if (!do_munmap(mm, newbrk, oldbrk-newbrk))
2951da177e4SLinus Torvalds 			goto set_brk;
2961da177e4SLinus Torvalds 		goto out;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	/* Check against existing mmap mappings. */
3001da177e4SLinus Torvalds 	if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
3011da177e4SLinus Torvalds 		goto out;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/* Ok, looks good - let it rip. */
3041da177e4SLinus Torvalds 	if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
3051da177e4SLinus Torvalds 		goto out;
3061da177e4SLinus Torvalds set_brk:
3071da177e4SLinus Torvalds 	mm->brk = brk;
3081da177e4SLinus Torvalds out:
3091da177e4SLinus Torvalds 	retval = mm->brk;
3101da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
3111da177e4SLinus Torvalds 	return retval;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
314ed8ea815SMichel Lespinasse #ifdef CONFIG_DEBUG_VM_RB
3151da177e4SLinus Torvalds static int browse_rb(struct rb_root *root)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	int i = 0, j;
3181da177e4SLinus Torvalds 	struct rb_node *nd, *pn = NULL;
3191da177e4SLinus Torvalds 	unsigned long prev = 0, pend = 0;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
3221da177e4SLinus Torvalds 		struct vm_area_struct *vma;
3231da177e4SLinus Torvalds 		vma = rb_entry(nd, struct vm_area_struct, vm_rb);
3241da177e4SLinus Torvalds 		if (vma->vm_start < prev)
3251da177e4SLinus Torvalds 			printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
3261da177e4SLinus Torvalds 		if (vma->vm_start < pend)
3271da177e4SLinus Torvalds 			printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
3281da177e4SLinus Torvalds 		if (vma->vm_start > vma->vm_end)
3291da177e4SLinus Torvalds 			printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
3301da177e4SLinus Torvalds 		i++;
3311da177e4SLinus Torvalds 		pn = nd;
332d1af65d1SDavid Miller 		prev = vma->vm_start;
333d1af65d1SDavid Miller 		pend = vma->vm_end;
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 	j = 0;
3361da177e4SLinus Torvalds 	for (nd = pn; nd; nd = rb_prev(nd)) {
3371da177e4SLinus Torvalds 		j++;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 	if (i != j)
3401da177e4SLinus Torvalds 		printk("backwards %d, forwards %d\n", j, i), i = 0;
3411da177e4SLinus Torvalds 	return i;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds void validate_mm(struct mm_struct *mm)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	int bug = 0;
3471da177e4SLinus Torvalds 	int i = 0;
348ed8ea815SMichel Lespinasse 	struct vm_area_struct *vma = mm->mmap;
349ed8ea815SMichel Lespinasse 	while (vma) {
350ed8ea815SMichel Lespinasse 		struct anon_vma_chain *avc;
351ed8ea815SMichel Lespinasse 		list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
352ed8ea815SMichel Lespinasse 			anon_vma_interval_tree_verify(avc);
353ed8ea815SMichel Lespinasse 		vma = vma->vm_next;
3541da177e4SLinus Torvalds 		i++;
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds 	if (i != mm->map_count)
3571da177e4SLinus Torvalds 		printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
3581da177e4SLinus Torvalds 	i = browse_rb(&mm->mm_rb);
3591da177e4SLinus Torvalds 	if (i != mm->map_count)
3601da177e4SLinus Torvalds 		printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
36146a350efSEric Sesterhenn 	BUG_ON(bug);
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds #else
3641da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0)
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 
367bf181b9fSMichel Lespinasse /*
368bf181b9fSMichel Lespinasse  * vma has some anon_vma assigned, and is already inserted on that
369bf181b9fSMichel Lespinasse  * anon_vma's interval trees.
370bf181b9fSMichel Lespinasse  *
371bf181b9fSMichel Lespinasse  * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
372bf181b9fSMichel Lespinasse  * vma must be removed from the anon_vma's interval trees using
373bf181b9fSMichel Lespinasse  * anon_vma_interval_tree_pre_update_vma().
374bf181b9fSMichel Lespinasse  *
375bf181b9fSMichel Lespinasse  * After the update, the vma will be reinserted using
376bf181b9fSMichel Lespinasse  * anon_vma_interval_tree_post_update_vma().
377bf181b9fSMichel Lespinasse  *
378bf181b9fSMichel Lespinasse  * The entire update must be protected by exclusive mmap_sem and by
379bf181b9fSMichel Lespinasse  * the root anon_vma's mutex.
380bf181b9fSMichel Lespinasse  */
381bf181b9fSMichel Lespinasse static inline void
382bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
383bf181b9fSMichel Lespinasse {
384bf181b9fSMichel Lespinasse 	struct anon_vma_chain *avc;
385bf181b9fSMichel Lespinasse 
386bf181b9fSMichel Lespinasse 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
387bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
388bf181b9fSMichel Lespinasse }
389bf181b9fSMichel Lespinasse 
390bf181b9fSMichel Lespinasse static inline void
391bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
392bf181b9fSMichel Lespinasse {
393bf181b9fSMichel Lespinasse 	struct anon_vma_chain *avc;
394bf181b9fSMichel Lespinasse 
395bf181b9fSMichel Lespinasse 	list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
396bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
397bf181b9fSMichel Lespinasse }
398bf181b9fSMichel Lespinasse 
3996597d783SHugh Dickins static int find_vma_links(struct mm_struct *mm, unsigned long addr,
4006597d783SHugh Dickins 		unsigned long end, struct vm_area_struct **pprev,
4016597d783SHugh Dickins 		struct rb_node ***rb_link, struct rb_node **rb_parent)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct rb_node **__rb_link, *__rb_parent, *rb_prev;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	__rb_link = &mm->mm_rb.rb_node;
4061da177e4SLinus Torvalds 	rb_prev = __rb_parent = NULL;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	while (*__rb_link) {
4091da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		__rb_parent = *__rb_link;
4121da177e4SLinus Torvalds 		vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 		if (vma_tmp->vm_end > addr) {
4156597d783SHugh Dickins 			/* Fail if an existing vma overlaps the area */
4166597d783SHugh Dickins 			if (vma_tmp->vm_start < end)
4176597d783SHugh Dickins 				return -ENOMEM;
4181da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_left;
4191da177e4SLinus Torvalds 		} else {
4201da177e4SLinus Torvalds 			rb_prev = __rb_parent;
4211da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_right;
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	*pprev = NULL;
4261da177e4SLinus Torvalds 	if (rb_prev)
4271da177e4SLinus Torvalds 		*pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
4281da177e4SLinus Torvalds 	*rb_link = __rb_link;
4291da177e4SLinus Torvalds 	*rb_parent = __rb_parent;
4306597d783SHugh Dickins 	return 0;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
4341da177e4SLinus Torvalds 		struct rb_node **rb_link, struct rb_node *rb_parent)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	rb_link_node(&vma->vm_rb, rb_parent, rb_link);
4371da177e4SLinus Torvalds 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	struct file *file;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	file = vma->vm_file;
4451da177e4SLinus Torvalds 	if (file) {
4461da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 		if (vma->vm_flags & VM_DENYWRITE)
449d3ac7f89SJosef "Jeff" Sipek 			atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
4501da177e4SLinus Torvalds 		if (vma->vm_flags & VM_SHARED)
4511da177e4SLinus Torvalds 			mapping->i_mmap_writable++;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
4541da177e4SLinus Torvalds 		if (unlikely(vma->vm_flags & VM_NONLINEAR))
4551da177e4SLinus Torvalds 			vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
4561da177e4SLinus Torvalds 		else
4576b2dbba8SMichel Lespinasse 			vma_interval_tree_insert(vma, &mapping->i_mmap);
4581da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
4591da177e4SLinus Torvalds 	}
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds static void
4631da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4641da177e4SLinus Torvalds 	struct vm_area_struct *prev, struct rb_node **rb_link,
4651da177e4SLinus Torvalds 	struct rb_node *rb_parent)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	__vma_link_list(mm, vma, prev, rb_parent);
4681da177e4SLinus Torvalds 	__vma_link_rb(mm, vma, rb_link, rb_parent);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4721da177e4SLinus Torvalds 			struct vm_area_struct *prev, struct rb_node **rb_link,
4731da177e4SLinus Torvalds 			struct rb_node *rb_parent)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (vma->vm_file)
4781da177e4SLinus Torvalds 		mapping = vma->vm_file->f_mapping;
4791da177e4SLinus Torvalds 
48097a89413SPeter Zijlstra 	if (mapping)
4813d48ae45SPeter Zijlstra 		mutex_lock(&mapping->i_mmap_mutex);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4841da177e4SLinus Torvalds 	__vma_link_file(vma);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	if (mapping)
4873d48ae45SPeter Zijlstra 		mutex_unlock(&mapping->i_mmap_mutex);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	mm->map_count++;
4901da177e4SLinus Torvalds 	validate_mm(mm);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /*
49488f6b4c3SKautuk Consul  * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
4956b2dbba8SMichel Lespinasse  * mm's list and rbtree.  It has already been inserted into the interval tree.
4961da177e4SLinus Torvalds  */
49748aae425SZhenwenXu static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
4981da177e4SLinus Torvalds {
4996597d783SHugh Dickins 	struct vm_area_struct *prev;
5001da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
5011da177e4SLinus Torvalds 
5026597d783SHugh Dickins 	if (find_vma_links(mm, vma->vm_start, vma->vm_end,
5036597d783SHugh Dickins 			   &prev, &rb_link, &rb_parent))
5046597d783SHugh Dickins 		BUG();
5051da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
5061da177e4SLinus Torvalds 	mm->map_count++;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds static inline void
5101da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
5111da177e4SLinus Torvalds 		struct vm_area_struct *prev)
5121da177e4SLinus Torvalds {
513297c5eeeSLinus Torvalds 	struct vm_area_struct *next = vma->vm_next;
514297c5eeeSLinus Torvalds 
515297c5eeeSLinus Torvalds 	prev->vm_next = next;
516297c5eeeSLinus Torvalds 	if (next)
517297c5eeeSLinus Torvalds 		next->vm_prev = prev;
5181da177e4SLinus Torvalds 	rb_erase(&vma->vm_rb, &mm->mm_rb);
5191da177e4SLinus Torvalds 	if (mm->mmap_cache == vma)
5201da177e4SLinus Torvalds 		mm->mmap_cache = prev;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /*
5241da177e4SLinus Torvalds  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
5251da177e4SLinus Torvalds  * is already present in an i_mmap tree without adjusting the tree.
5261da177e4SLinus Torvalds  * The following helper function should be used when such adjustments
5271da177e4SLinus Torvalds  * are necessary.  The "insert" vma (if any) is to be inserted
5281da177e4SLinus Torvalds  * before we drop the necessary locks.
5291da177e4SLinus Torvalds  */
5305beb4930SRik van Riel int vma_adjust(struct vm_area_struct *vma, unsigned long start,
5311da177e4SLinus Torvalds 	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
5341da177e4SLinus Torvalds 	struct vm_area_struct *next = vma->vm_next;
5351da177e4SLinus Torvalds 	struct vm_area_struct *importer = NULL;
5361da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
5376b2dbba8SMichel Lespinasse 	struct rb_root *root = NULL;
538012f1800SRik van Riel 	struct anon_vma *anon_vma = NULL;
5391da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
5401da177e4SLinus Torvalds 	long adjust_next = 0;
5411da177e4SLinus Torvalds 	int remove_next = 0;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (next && !insert) {
544287d97acSLinus Torvalds 		struct vm_area_struct *exporter = NULL;
545287d97acSLinus Torvalds 
5461da177e4SLinus Torvalds 		if (end >= next->vm_end) {
5471da177e4SLinus Torvalds 			/*
5481da177e4SLinus Torvalds 			 * vma expands, overlapping all the next, and
5491da177e4SLinus Torvalds 			 * perhaps the one after too (mprotect case 6).
5501da177e4SLinus Torvalds 			 */
5511da177e4SLinus Torvalds again:			remove_next = 1 + (end > next->vm_end);
5521da177e4SLinus Torvalds 			end = next->vm_end;
553287d97acSLinus Torvalds 			exporter = next;
5541da177e4SLinus Torvalds 			importer = vma;
5551da177e4SLinus Torvalds 		} else if (end > next->vm_start) {
5561da177e4SLinus Torvalds 			/*
5571da177e4SLinus Torvalds 			 * vma expands, overlapping part of the next:
5581da177e4SLinus Torvalds 			 * mprotect case 5 shifting the boundary up.
5591da177e4SLinus Torvalds 			 */
5601da177e4SLinus Torvalds 			adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
561287d97acSLinus Torvalds 			exporter = next;
5621da177e4SLinus Torvalds 			importer = vma;
5631da177e4SLinus Torvalds 		} else if (end < vma->vm_end) {
5641da177e4SLinus Torvalds 			/*
5651da177e4SLinus Torvalds 			 * vma shrinks, and !insert tells it's not
5661da177e4SLinus Torvalds 			 * split_vma inserting another: so it must be
5671da177e4SLinus Torvalds 			 * mprotect case 4 shifting the boundary down.
5681da177e4SLinus Torvalds 			 */
5691da177e4SLinus Torvalds 			adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
570287d97acSLinus Torvalds 			exporter = vma;
5711da177e4SLinus Torvalds 			importer = next;
5721da177e4SLinus Torvalds 		}
5731da177e4SLinus Torvalds 
5745beb4930SRik van Riel 		/*
5755beb4930SRik van Riel 		 * Easily overlooked: when mprotect shifts the boundary,
5765beb4930SRik van Riel 		 * make sure the expanding vma has anon_vma set if the
5775beb4930SRik van Riel 		 * shrinking vma had, to cover any anon pages imported.
5785beb4930SRik van Riel 		 */
579287d97acSLinus Torvalds 		if (exporter && exporter->anon_vma && !importer->anon_vma) {
580287d97acSLinus Torvalds 			if (anon_vma_clone(importer, exporter))
5815beb4930SRik van Riel 				return -ENOMEM;
582287d97acSLinus Torvalds 			importer->anon_vma = exporter->anon_vma;
5835beb4930SRik van Riel 		}
5845beb4930SRik van Riel 	}
5855beb4930SRik van Riel 
5861da177e4SLinus Torvalds 	if (file) {
5871da177e4SLinus Torvalds 		mapping = file->f_mapping;
588682968e0SSrikar Dronamraju 		if (!(vma->vm_flags & VM_NONLINEAR)) {
5891da177e4SLinus Torvalds 			root = &mapping->i_mmap;
590cbc91f71SSrikar Dronamraju 			uprobe_munmap(vma, vma->vm_start, vma->vm_end);
591682968e0SSrikar Dronamraju 
592682968e0SSrikar Dronamraju 			if (adjust_next)
593cbc91f71SSrikar Dronamraju 				uprobe_munmap(next, next->vm_start,
594cbc91f71SSrikar Dronamraju 							next->vm_end);
595682968e0SSrikar Dronamraju 		}
596682968e0SSrikar Dronamraju 
5973d48ae45SPeter Zijlstra 		mutex_lock(&mapping->i_mmap_mutex);
5981da177e4SLinus Torvalds 		if (insert) {
5991da177e4SLinus Torvalds 			/*
6006b2dbba8SMichel Lespinasse 			 * Put into interval tree now, so instantiated pages
6011da177e4SLinus Torvalds 			 * are visible to arm/parisc __flush_dcache_page
6021da177e4SLinus Torvalds 			 * throughout; but we cannot insert into address
6031da177e4SLinus Torvalds 			 * space until vma start or end is updated.
6041da177e4SLinus Torvalds 			 */
6051da177e4SLinus Torvalds 			__vma_link_file(insert);
6061da177e4SLinus Torvalds 		}
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds 
60994fcc585SAndrea Arcangeli 	vma_adjust_trans_huge(vma, start, end, adjust_next);
61094fcc585SAndrea Arcangeli 
611012f1800SRik van Riel 	anon_vma = vma->anon_vma;
612bf181b9fSMichel Lespinasse 	if (!anon_vma && adjust_next)
613bf181b9fSMichel Lespinasse 		anon_vma = next->anon_vma;
614bf181b9fSMichel Lespinasse 	if (anon_vma) {
615ca42b26aSMichel Lespinasse 		VM_BUG_ON(adjust_next && next->anon_vma &&
616ca42b26aSMichel Lespinasse 			  anon_vma != next->anon_vma);
617012f1800SRik van Riel 		anon_vma_lock(anon_vma);
618bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_pre_update_vma(vma);
619bf181b9fSMichel Lespinasse 		if (adjust_next)
620bf181b9fSMichel Lespinasse 			anon_vma_interval_tree_pre_update_vma(next);
621bf181b9fSMichel Lespinasse 	}
622012f1800SRik van Riel 
6231da177e4SLinus Torvalds 	if (root) {
6241da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
6256b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, root);
6261da177e4SLinus Torvalds 		if (adjust_next)
6276b2dbba8SMichel Lespinasse 			vma_interval_tree_remove(next, root);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	vma->vm_start = start;
6311da177e4SLinus Torvalds 	vma->vm_end = end;
6321da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
6331da177e4SLinus Torvalds 	if (adjust_next) {
6341da177e4SLinus Torvalds 		next->vm_start += adjust_next << PAGE_SHIFT;
6351da177e4SLinus Torvalds 		next->vm_pgoff += adjust_next;
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (root) {
6391da177e4SLinus Torvalds 		if (adjust_next)
6406b2dbba8SMichel Lespinasse 			vma_interval_tree_insert(next, root);
6416b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, root);
6421da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if (remove_next) {
6461da177e4SLinus Torvalds 		/*
6471da177e4SLinus Torvalds 		 * vma_merge has merged next into vma, and needs
6481da177e4SLinus Torvalds 		 * us to remove next before dropping the locks.
6491da177e4SLinus Torvalds 		 */
6501da177e4SLinus Torvalds 		__vma_unlink(mm, next, vma);
6511da177e4SLinus Torvalds 		if (file)
6521da177e4SLinus Torvalds 			__remove_shared_vm_struct(next, file, mapping);
6531da177e4SLinus Torvalds 	} else if (insert) {
6541da177e4SLinus Torvalds 		/*
6551da177e4SLinus Torvalds 		 * split_vma has split insert from vma, and needs
6561da177e4SLinus Torvalds 		 * us to insert it before dropping the locks
6571da177e4SLinus Torvalds 		 * (it may either follow vma or precede it).
6581da177e4SLinus Torvalds 		 */
6591da177e4SLinus Torvalds 		__insert_vm_struct(mm, insert);
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
662bf181b9fSMichel Lespinasse 	if (anon_vma) {
663bf181b9fSMichel Lespinasse 		anon_vma_interval_tree_post_update_vma(vma);
664bf181b9fSMichel Lespinasse 		if (adjust_next)
665bf181b9fSMichel Lespinasse 			anon_vma_interval_tree_post_update_vma(next);
666012f1800SRik van Riel 		anon_vma_unlock(anon_vma);
667bf181b9fSMichel Lespinasse 	}
6681da177e4SLinus Torvalds 	if (mapping)
6693d48ae45SPeter Zijlstra 		mutex_unlock(&mapping->i_mmap_mutex);
6701da177e4SLinus Torvalds 
6712b144498SSrikar Dronamraju 	if (root) {
6727b2d81d4SIngo Molnar 		uprobe_mmap(vma);
6732b144498SSrikar Dronamraju 
6742b144498SSrikar Dronamraju 		if (adjust_next)
6757b2d81d4SIngo Molnar 			uprobe_mmap(next);
6762b144498SSrikar Dronamraju 	}
6772b144498SSrikar Dronamraju 
6781da177e4SLinus Torvalds 	if (remove_next) {
679925d1c40SMatt Helsley 		if (file) {
680cbc91f71SSrikar Dronamraju 			uprobe_munmap(next, next->vm_start, next->vm_end);
6811da177e4SLinus Torvalds 			fput(file);
682925d1c40SMatt Helsley 		}
6835beb4930SRik van Riel 		if (next->anon_vma)
6845beb4930SRik van Riel 			anon_vma_merge(vma, next);
6851da177e4SLinus Torvalds 		mm->map_count--;
686f0be3d32SLee Schermerhorn 		mpol_put(vma_policy(next));
6871da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, next);
6881da177e4SLinus Torvalds 		/*
6891da177e4SLinus Torvalds 		 * In mprotect's case 6 (see comments on vma_merge),
6901da177e4SLinus Torvalds 		 * we must remove another next too. It would clutter
6911da177e4SLinus Torvalds 		 * up the code too much to do both in one go.
6921da177e4SLinus Torvalds 		 */
6931da177e4SLinus Torvalds 		if (remove_next == 2) {
6941da177e4SLinus Torvalds 			next = vma->vm_next;
6951da177e4SLinus Torvalds 			goto again;
6961da177e4SLinus Torvalds 		}
6971da177e4SLinus Torvalds 	}
6982b144498SSrikar Dronamraju 	if (insert && file)
6997b2d81d4SIngo Molnar 		uprobe_mmap(insert);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	validate_mm(mm);
7025beb4930SRik van Riel 
7035beb4930SRik van Riel 	return 0;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds /*
7071da177e4SLinus Torvalds  * If the vma has a ->close operation then the driver probably needs to release
7081da177e4SLinus Torvalds  * per-vma resources, so we don't attempt to merge those.
7091da177e4SLinus Torvalds  */
7101da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma,
7111da177e4SLinus Torvalds 			struct file *file, unsigned long vm_flags)
7121da177e4SLinus Torvalds {
7130b173bc4SKonstantin Khlebnikov 	if (vma->vm_flags ^ vm_flags)
7141da177e4SLinus Torvalds 		return 0;
7151da177e4SLinus Torvalds 	if (vma->vm_file != file)
7161da177e4SLinus Torvalds 		return 0;
7171da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
7181da177e4SLinus Torvalds 		return 0;
7191da177e4SLinus Torvalds 	return 1;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
723965f55deSShaohua Li 					struct anon_vma *anon_vma2,
724965f55deSShaohua Li 					struct vm_area_struct *vma)
7251da177e4SLinus Torvalds {
726965f55deSShaohua Li 	/*
727965f55deSShaohua Li 	 * The list_is_singular() test is to avoid merging VMA cloned from
728965f55deSShaohua Li 	 * parents. This can improve scalability caused by anon_vma lock.
729965f55deSShaohua Li 	 */
730965f55deSShaohua Li 	if ((!anon_vma1 || !anon_vma2) && (!vma ||
731965f55deSShaohua Li 		list_is_singular(&vma->anon_vma_chain)))
732965f55deSShaohua Li 		return 1;
733965f55deSShaohua Li 	return anon_vma1 == anon_vma2;
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds /*
7371da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
7381da177e4SLinus Torvalds  * in front of (at a lower virtual address and file offset than) the vma.
7391da177e4SLinus Torvalds  *
7401da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
7411da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
7421da177e4SLinus Torvalds  *
7431da177e4SLinus Torvalds  * We don't check here for the merged mmap wrapping around the end of pagecache
7441da177e4SLinus Torvalds  * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
7451da177e4SLinus Torvalds  * wrap, nor mmaps which cover the final page at index -1UL.
7461da177e4SLinus Torvalds  */
7471da177e4SLinus Torvalds static int
7481da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
7491da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
752965f55deSShaohua Li 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
7531da177e4SLinus Torvalds 		if (vma->vm_pgoff == vm_pgoff)
7541da177e4SLinus Torvalds 			return 1;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 	return 0;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /*
7601da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
7611da177e4SLinus Torvalds  * beyond (at a higher virtual address and file offset than) the vma.
7621da177e4SLinus Torvalds  *
7631da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
7641da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
7651da177e4SLinus Torvalds  */
7661da177e4SLinus Torvalds static int
7671da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
7681da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
771965f55deSShaohua Li 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
7721da177e4SLinus Torvalds 		pgoff_t vm_pglen;
7731da177e4SLinus Torvalds 		vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
7741da177e4SLinus Torvalds 		if (vma->vm_pgoff + vm_pglen == vm_pgoff)
7751da177e4SLinus Torvalds 			return 1;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 	return 0;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds /*
7811da177e4SLinus Torvalds  * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
7821da177e4SLinus Torvalds  * whether that can be merged with its predecessor or its successor.
7831da177e4SLinus Torvalds  * Or both (it neatly fills a hole).
7841da177e4SLinus Torvalds  *
7851da177e4SLinus Torvalds  * In most cases - when called for mmap, brk or mremap - [addr,end) is
7861da177e4SLinus Torvalds  * certain not to be mapped by the time vma_merge is called; but when
7871da177e4SLinus Torvalds  * called for mprotect, it is certain to be already mapped (either at
7881da177e4SLinus Torvalds  * an offset within prev, or at the start of next), and the flags of
7891da177e4SLinus Torvalds  * this area are about to be changed to vm_flags - and the no-change
7901da177e4SLinus Torvalds  * case has already been eliminated.
7911da177e4SLinus Torvalds  *
7921da177e4SLinus Torvalds  * The following mprotect cases have to be considered, where AAAA is
7931da177e4SLinus Torvalds  * the area passed down from mprotect_fixup, never extending beyond one
7941da177e4SLinus Torvalds  * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
7951da177e4SLinus Torvalds  *
7961da177e4SLinus Torvalds  *     AAAA             AAAA                AAAA          AAAA
7971da177e4SLinus Torvalds  *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
7981da177e4SLinus Torvalds  *    cannot merge    might become    might become    might become
7991da177e4SLinus Torvalds  *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
8001da177e4SLinus Torvalds  *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
8011da177e4SLinus Torvalds  *    mremap move:                                    PPPPNNNNNNNN 8
8021da177e4SLinus Torvalds  *        AAAA
8031da177e4SLinus Torvalds  *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
8041da177e4SLinus Torvalds  *    might become    case 1 below    case 2 below    case 3 below
8051da177e4SLinus Torvalds  *
8061da177e4SLinus Torvalds  * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
8071da177e4SLinus Torvalds  * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
8081da177e4SLinus Torvalds  */
8091da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm,
8101da177e4SLinus Torvalds 			struct vm_area_struct *prev, unsigned long addr,
8111da177e4SLinus Torvalds 			unsigned long end, unsigned long vm_flags,
8121da177e4SLinus Torvalds 		     	struct anon_vma *anon_vma, struct file *file,
8131da177e4SLinus Torvalds 			pgoff_t pgoff, struct mempolicy *policy)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
8161da177e4SLinus Torvalds 	struct vm_area_struct *area, *next;
8175beb4930SRik van Riel 	int err;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	/*
8201da177e4SLinus Torvalds 	 * We later require that vma->vm_flags == vm_flags,
8211da177e4SLinus Torvalds 	 * so this tests vma->vm_flags & VM_SPECIAL, too.
8221da177e4SLinus Torvalds 	 */
8231da177e4SLinus Torvalds 	if (vm_flags & VM_SPECIAL)
8241da177e4SLinus Torvalds 		return NULL;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	if (prev)
8271da177e4SLinus Torvalds 		next = prev->vm_next;
8281da177e4SLinus Torvalds 	else
8291da177e4SLinus Torvalds 		next = mm->mmap;
8301da177e4SLinus Torvalds 	area = next;
8311da177e4SLinus Torvalds 	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
8321da177e4SLinus Torvalds 		next = next->vm_next;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	/*
8351da177e4SLinus Torvalds 	 * Can it merge with the predecessor?
8361da177e4SLinus Torvalds 	 */
8371da177e4SLinus Torvalds 	if (prev && prev->vm_end == addr &&
8381da177e4SLinus Torvalds   			mpol_equal(vma_policy(prev), policy) &&
8391da177e4SLinus Torvalds 			can_vma_merge_after(prev, vm_flags,
8401da177e4SLinus Torvalds 						anon_vma, file, pgoff)) {
8411da177e4SLinus Torvalds 		/*
8421da177e4SLinus Torvalds 		 * OK, it can.  Can we now merge in the successor as well?
8431da177e4SLinus Torvalds 		 */
8441da177e4SLinus Torvalds 		if (next && end == next->vm_start &&
8451da177e4SLinus Torvalds 				mpol_equal(policy, vma_policy(next)) &&
8461da177e4SLinus Torvalds 				can_vma_merge_before(next, vm_flags,
8471da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen) &&
8481da177e4SLinus Torvalds 				is_mergeable_anon_vma(prev->anon_vma,
849965f55deSShaohua Li 						      next->anon_vma, NULL)) {
8501da177e4SLinus Torvalds 							/* cases 1, 6 */
8515beb4930SRik van Riel 			err = vma_adjust(prev, prev->vm_start,
8521da177e4SLinus Torvalds 				next->vm_end, prev->vm_pgoff, NULL);
8531da177e4SLinus Torvalds 		} else					/* cases 2, 5, 7 */
8545beb4930SRik van Riel 			err = vma_adjust(prev, prev->vm_start,
8551da177e4SLinus Torvalds 				end, prev->vm_pgoff, NULL);
8565beb4930SRik van Riel 		if (err)
8575beb4930SRik van Riel 			return NULL;
858b15d00b6SAndrea Arcangeli 		khugepaged_enter_vma_merge(prev);
8591da177e4SLinus Torvalds 		return prev;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	/*
8631da177e4SLinus Torvalds 	 * Can this new request be merged in front of next?
8641da177e4SLinus Torvalds 	 */
8651da177e4SLinus Torvalds 	if (next && end == next->vm_start &&
8661da177e4SLinus Torvalds  			mpol_equal(policy, vma_policy(next)) &&
8671da177e4SLinus Torvalds 			can_vma_merge_before(next, vm_flags,
8681da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen)) {
8691da177e4SLinus Torvalds 		if (prev && addr < prev->vm_end)	/* case 4 */
8705beb4930SRik van Riel 			err = vma_adjust(prev, prev->vm_start,
8711da177e4SLinus Torvalds 				addr, prev->vm_pgoff, NULL);
8721da177e4SLinus Torvalds 		else					/* cases 3, 8 */
8735beb4930SRik van Riel 			err = vma_adjust(area, addr, next->vm_end,
8741da177e4SLinus Torvalds 				next->vm_pgoff - pglen, NULL);
8755beb4930SRik van Riel 		if (err)
8765beb4930SRik van Riel 			return NULL;
877b15d00b6SAndrea Arcangeli 		khugepaged_enter_vma_merge(area);
8781da177e4SLinus Torvalds 		return area;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	return NULL;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds /*
885d0e9fe17SLinus Torvalds  * Rough compatbility check to quickly see if it's even worth looking
886d0e9fe17SLinus Torvalds  * at sharing an anon_vma.
887d0e9fe17SLinus Torvalds  *
888d0e9fe17SLinus Torvalds  * They need to have the same vm_file, and the flags can only differ
889d0e9fe17SLinus Torvalds  * in things that mprotect may change.
890d0e9fe17SLinus Torvalds  *
891d0e9fe17SLinus Torvalds  * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
892d0e9fe17SLinus Torvalds  * we can merge the two vma's. For example, we refuse to merge a vma if
893d0e9fe17SLinus Torvalds  * there is a vm_ops->close() function, because that indicates that the
894d0e9fe17SLinus Torvalds  * driver is doing some kind of reference counting. But that doesn't
895d0e9fe17SLinus Torvalds  * really matter for the anon_vma sharing case.
896d0e9fe17SLinus Torvalds  */
897d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
898d0e9fe17SLinus Torvalds {
899d0e9fe17SLinus Torvalds 	return a->vm_end == b->vm_start &&
900d0e9fe17SLinus Torvalds 		mpol_equal(vma_policy(a), vma_policy(b)) &&
901d0e9fe17SLinus Torvalds 		a->vm_file == b->vm_file &&
902d0e9fe17SLinus Torvalds 		!((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
903d0e9fe17SLinus Torvalds 		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
904d0e9fe17SLinus Torvalds }
905d0e9fe17SLinus Torvalds 
906d0e9fe17SLinus Torvalds /*
907d0e9fe17SLinus Torvalds  * Do some basic sanity checking to see if we can re-use the anon_vma
908d0e9fe17SLinus Torvalds  * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
909d0e9fe17SLinus Torvalds  * the same as 'old', the other will be the new one that is trying
910d0e9fe17SLinus Torvalds  * to share the anon_vma.
911d0e9fe17SLinus Torvalds  *
912d0e9fe17SLinus Torvalds  * NOTE! This runs with mm_sem held for reading, so it is possible that
913d0e9fe17SLinus Torvalds  * the anon_vma of 'old' is concurrently in the process of being set up
914d0e9fe17SLinus Torvalds  * by another page fault trying to merge _that_. But that's ok: if it
915d0e9fe17SLinus Torvalds  * is being set up, that automatically means that it will be a singleton
916d0e9fe17SLinus Torvalds  * acceptable for merging, so we can do all of this optimistically. But
917d0e9fe17SLinus Torvalds  * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
918d0e9fe17SLinus Torvalds  *
919d0e9fe17SLinus Torvalds  * IOW: that the "list_is_singular()" test on the anon_vma_chain only
920d0e9fe17SLinus Torvalds  * matters for the 'stable anon_vma' case (ie the thing we want to avoid
921d0e9fe17SLinus Torvalds  * is to return an anon_vma that is "complex" due to having gone through
922d0e9fe17SLinus Torvalds  * a fork).
923d0e9fe17SLinus Torvalds  *
924d0e9fe17SLinus Torvalds  * We also make sure that the two vma's are compatible (adjacent,
925d0e9fe17SLinus Torvalds  * and with the same memory policies). That's all stable, even with just
926d0e9fe17SLinus Torvalds  * a read lock on the mm_sem.
927d0e9fe17SLinus Torvalds  */
928d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
929d0e9fe17SLinus Torvalds {
930d0e9fe17SLinus Torvalds 	if (anon_vma_compatible(a, b)) {
931d0e9fe17SLinus Torvalds 		struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
932d0e9fe17SLinus Torvalds 
933d0e9fe17SLinus Torvalds 		if (anon_vma && list_is_singular(&old->anon_vma_chain))
934d0e9fe17SLinus Torvalds 			return anon_vma;
935d0e9fe17SLinus Torvalds 	}
936d0e9fe17SLinus Torvalds 	return NULL;
937d0e9fe17SLinus Torvalds }
938d0e9fe17SLinus Torvalds 
939d0e9fe17SLinus Torvalds /*
9401da177e4SLinus Torvalds  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
9411da177e4SLinus Torvalds  * neighbouring vmas for a suitable anon_vma, before it goes off
9421da177e4SLinus Torvalds  * to allocate a new anon_vma.  It checks because a repetitive
9431da177e4SLinus Torvalds  * sequence of mprotects and faults may otherwise lead to distinct
9441da177e4SLinus Torvalds  * anon_vmas being allocated, preventing vma merge in subsequent
9451da177e4SLinus Torvalds  * mprotect.
9461da177e4SLinus Torvalds  */
9471da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
9481da177e4SLinus Torvalds {
949d0e9fe17SLinus Torvalds 	struct anon_vma *anon_vma;
9501da177e4SLinus Torvalds 	struct vm_area_struct *near;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	near = vma->vm_next;
9531da177e4SLinus Torvalds 	if (!near)
9541da177e4SLinus Torvalds 		goto try_prev;
9551da177e4SLinus Torvalds 
956d0e9fe17SLinus Torvalds 	anon_vma = reusable_anon_vma(near, vma, near);
957d0e9fe17SLinus Torvalds 	if (anon_vma)
958d0e9fe17SLinus Torvalds 		return anon_vma;
9591da177e4SLinus Torvalds try_prev:
9609be34c9dSLinus Torvalds 	near = vma->vm_prev;
9611da177e4SLinus Torvalds 	if (!near)
9621da177e4SLinus Torvalds 		goto none;
9631da177e4SLinus Torvalds 
964d0e9fe17SLinus Torvalds 	anon_vma = reusable_anon_vma(near, near, vma);
965d0e9fe17SLinus Torvalds 	if (anon_vma)
966d0e9fe17SLinus Torvalds 		return anon_vma;
9671da177e4SLinus Torvalds none:
9681da177e4SLinus Torvalds 	/*
9691da177e4SLinus Torvalds 	 * There's no absolute need to look only at touching neighbours:
9701da177e4SLinus Torvalds 	 * we could search further afield for "compatible" anon_vmas.
9711da177e4SLinus Torvalds 	 * But it would probably just be a waste of time searching,
9721da177e4SLinus Torvalds 	 * or lead to too many vmas hanging off the same anon_vma.
9731da177e4SLinus Torvalds 	 * We're trying to allow mprotect remerging later on,
9741da177e4SLinus Torvalds 	 * not trying to minimize memory used for anon_vmas.
9751da177e4SLinus Torvalds 	 */
9761da177e4SLinus Torvalds 	return NULL;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
980ab50b8edSHugh Dickins void vm_stat_account(struct mm_struct *mm, unsigned long flags,
9811da177e4SLinus Torvalds 						struct file *file, long pages)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	const unsigned long stack_flags
9841da177e4SLinus Torvalds 		= VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
9851da177e4SLinus Torvalds 
98644de9d0cSHuang Shijie 	mm->total_vm += pages;
98744de9d0cSHuang Shijie 
9881da177e4SLinus Torvalds 	if (file) {
9891da177e4SLinus Torvalds 		mm->shared_vm += pages;
9901da177e4SLinus Torvalds 		if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
9911da177e4SLinus Torvalds 			mm->exec_vm += pages;
9921da177e4SLinus Torvalds 	} else if (flags & stack_flags)
9931da177e4SLinus Torvalds 		mm->stack_vm += pages;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds /*
99840401530SAl Viro  * If a hint addr is less than mmap_min_addr change hint to be as
99940401530SAl Viro  * low as possible but still greater than mmap_min_addr
100040401530SAl Viro  */
100140401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint)
100240401530SAl Viro {
100340401530SAl Viro 	hint &= PAGE_MASK;
100440401530SAl Viro 	if (((void *)hint != NULL) &&
100540401530SAl Viro 	    (hint < mmap_min_addr))
100640401530SAl Viro 		return PAGE_ALIGN(mmap_min_addr);
100740401530SAl Viro 	return hint;
100840401530SAl Viro }
100940401530SAl Viro 
101040401530SAl Viro /*
101127f5de79SJianjun Kong  * The caller must hold down_write(&current->mm->mmap_sem).
10121da177e4SLinus Torvalds  */
10131da177e4SLinus Torvalds 
1014e3fc629dSAl Viro unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
10151da177e4SLinus Torvalds 			unsigned long len, unsigned long prot,
10161da177e4SLinus Torvalds 			unsigned long flags, unsigned long pgoff)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
10191da177e4SLinus Torvalds 	struct inode *inode;
1020ca16d140SKOSAKI Motohiro 	vm_flags_t vm_flags;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	/*
10231da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC?
10241da177e4SLinus Torvalds 	 *
10251da177e4SLinus Torvalds 	 * (the exception is when the underlying filesystem is noexec
10261da177e4SLinus Torvalds 	 *  mounted, in which case we dont add PROT_EXEC.)
10271da177e4SLinus Torvalds 	 */
10281da177e4SLinus Torvalds 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
1029d3ac7f89SJosef "Jeff" Sipek 		if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
10301da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	if (!len)
10331da177e4SLinus Torvalds 		return -EINVAL;
10341da177e4SLinus Torvalds 
10357cd94146SEric Paris 	if (!(flags & MAP_FIXED))
10367cd94146SEric Paris 		addr = round_hint_to_min(addr);
10377cd94146SEric Paris 
10381da177e4SLinus Torvalds 	/* Careful about overflows.. */
10391da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
10409206de95SAl Viro 	if (!len)
10411da177e4SLinus Torvalds 		return -ENOMEM;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/* offset overflow? */
10441da177e4SLinus Torvalds 	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
10451da177e4SLinus Torvalds                return -EOVERFLOW;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	/* Too many mappings? */
10481da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
10491da177e4SLinus Torvalds 		return -ENOMEM;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	/* Obtain the address to map to. we verify (or select) it and ensure
10521da177e4SLinus Torvalds 	 * that it represents a valid section of the address space.
10531da177e4SLinus Torvalds 	 */
10541da177e4SLinus Torvalds 	addr = get_unmapped_area(file, addr, len, pgoff, flags);
10551da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
10561da177e4SLinus Torvalds 		return addr;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	/* Do simple checking here so the lower-level routines won't have
10591da177e4SLinus Torvalds 	 * to. we assume access permissions have been handled by the open
10601da177e4SLinus Torvalds 	 * of the memory object, so we don't do any here.
10611da177e4SLinus Torvalds 	 */
10621da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
10631da177e4SLinus Torvalds 			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10641da177e4SLinus Torvalds 
1065cdf7b341SHuang Shijie 	if (flags & MAP_LOCKED)
10661da177e4SLinus Torvalds 		if (!can_do_mlock())
10671da177e4SLinus Torvalds 			return -EPERM;
1068ba470de4SRik van Riel 
10691da177e4SLinus Torvalds 	/* mlock MCL_FUTURE? */
10701da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
10711da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
107293ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
107393ea1d0aSChris Wright 		locked += mm->locked_vm;
107459e99e5bSJiri Slaby 		lock_limit = rlimit(RLIMIT_MEMLOCK);
107593ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
10761da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
10771da177e4SLinus Torvalds 			return -EAGAIN;
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
1080d3ac7f89SJosef "Jeff" Sipek 	inode = file ? file->f_path.dentry->d_inode : NULL;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	if (file) {
10831da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
10841da177e4SLinus Torvalds 		case MAP_SHARED:
10851da177e4SLinus Torvalds 			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
10861da177e4SLinus Torvalds 				return -EACCES;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 			/*
10891da177e4SLinus Torvalds 			 * Make sure we don't allow writing to an append-only
10901da177e4SLinus Torvalds 			 * file..
10911da177e4SLinus Torvalds 			 */
10921da177e4SLinus Torvalds 			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
10931da177e4SLinus Torvalds 				return -EACCES;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 			/*
10961da177e4SLinus Torvalds 			 * Make sure there are no mandatory locks on the file.
10971da177e4SLinus Torvalds 			 */
10981da177e4SLinus Torvalds 			if (locks_verify_locked(inode))
10991da177e4SLinus Torvalds 				return -EAGAIN;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
11021da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_WRITE))
11031da177e4SLinus Torvalds 				vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 			/* fall through */
11061da177e4SLinus Torvalds 		case MAP_PRIVATE:
11071da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_READ))
11081da177e4SLinus Torvalds 				return -EACCES;
1109d3ac7f89SJosef "Jeff" Sipek 			if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
111080c5606cSLinus Torvalds 				if (vm_flags & VM_EXEC)
111180c5606cSLinus Torvalds 					return -EPERM;
111280c5606cSLinus Torvalds 				vm_flags &= ~VM_MAYEXEC;
111380c5606cSLinus Torvalds 			}
111480c5606cSLinus Torvalds 
111580c5606cSLinus Torvalds 			if (!file->f_op || !file->f_op->mmap)
111680c5606cSLinus Torvalds 				return -ENODEV;
11171da177e4SLinus Torvalds 			break;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		default:
11201da177e4SLinus Torvalds 			return -EINVAL;
11211da177e4SLinus Torvalds 		}
11221da177e4SLinus Torvalds 	} else {
11231da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
11241da177e4SLinus Torvalds 		case MAP_SHARED:
1125ce363942STejun Heo 			/*
1126ce363942STejun Heo 			 * Ignore pgoff.
1127ce363942STejun Heo 			 */
1128ce363942STejun Heo 			pgoff = 0;
11291da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
11301da177e4SLinus Torvalds 			break;
11311da177e4SLinus Torvalds 		case MAP_PRIVATE:
11321da177e4SLinus Torvalds 			/*
11331da177e4SLinus Torvalds 			 * Set pgoff according to addr for anon_vma.
11341da177e4SLinus Torvalds 			 */
11351da177e4SLinus Torvalds 			pgoff = addr >> PAGE_SHIFT;
11361da177e4SLinus Torvalds 			break;
11371da177e4SLinus Torvalds 		default:
11381da177e4SLinus Torvalds 			return -EINVAL;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11425a6fe125SMel Gorman 	return mmap_region(file, addr, len, flags, vm_flags, pgoff);
11430165ab44SMiklos Szeredi }
11446be5ceb0SLinus Torvalds 
114566f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
114666f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
114766f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
114866f0dc48SHugh Dickins {
114966f0dc48SHugh Dickins 	struct file *file = NULL;
115066f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
115166f0dc48SHugh Dickins 
115266f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
1153120a795dSAl Viro 		audit_mmap_fd(fd, flags);
115466f0dc48SHugh Dickins 		if (unlikely(flags & MAP_HUGETLB))
115566f0dc48SHugh Dickins 			return -EINVAL;
115666f0dc48SHugh Dickins 		file = fget(fd);
115766f0dc48SHugh Dickins 		if (!file)
115866f0dc48SHugh Dickins 			goto out;
115966f0dc48SHugh Dickins 	} else if (flags & MAP_HUGETLB) {
116066f0dc48SHugh Dickins 		struct user_struct *user = NULL;
116166f0dc48SHugh Dickins 		/*
116266f0dc48SHugh Dickins 		 * VM_NORESERVE is used because the reservations will be
116366f0dc48SHugh Dickins 		 * taken when vm_ops->mmap() is called
116466f0dc48SHugh Dickins 		 * A dummy user value is used because we are not locking
116566f0dc48SHugh Dickins 		 * memory so no accounting is necessary
116666f0dc48SHugh Dickins 		 */
116740716e29SSteven Truelove 		file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
116840716e29SSteven Truelove 						VM_NORESERVE, &user,
116940716e29SSteven Truelove 						HUGETLB_ANONHUGE_INODE);
117066f0dc48SHugh Dickins 		if (IS_ERR(file))
117166f0dc48SHugh Dickins 			return PTR_ERR(file);
117266f0dc48SHugh Dickins 	}
117366f0dc48SHugh Dickins 
117466f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
117566f0dc48SHugh Dickins 
1176eb36c587SAl Viro 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
117766f0dc48SHugh Dickins 	if (file)
117866f0dc48SHugh Dickins 		fput(file);
117966f0dc48SHugh Dickins out:
118066f0dc48SHugh Dickins 	return retval;
118166f0dc48SHugh Dickins }
118266f0dc48SHugh Dickins 
1183a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1184a4679373SChristoph Hellwig struct mmap_arg_struct {
1185a4679373SChristoph Hellwig 	unsigned long addr;
1186a4679373SChristoph Hellwig 	unsigned long len;
1187a4679373SChristoph Hellwig 	unsigned long prot;
1188a4679373SChristoph Hellwig 	unsigned long flags;
1189a4679373SChristoph Hellwig 	unsigned long fd;
1190a4679373SChristoph Hellwig 	unsigned long offset;
1191a4679373SChristoph Hellwig };
1192a4679373SChristoph Hellwig 
1193a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1194a4679373SChristoph Hellwig {
1195a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1196a4679373SChristoph Hellwig 
1197a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1198a4679373SChristoph Hellwig 		return -EFAULT;
1199a4679373SChristoph Hellwig 	if (a.offset & ~PAGE_MASK)
1200a4679373SChristoph Hellwig 		return -EINVAL;
1201a4679373SChristoph Hellwig 
1202a4679373SChristoph Hellwig 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1203a4679373SChristoph Hellwig 			      a.offset >> PAGE_SHIFT);
1204a4679373SChristoph Hellwig }
1205a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1206a4679373SChristoph Hellwig 
12074e950f6fSAlexey Dobriyan /*
12084e950f6fSAlexey Dobriyan  * Some shared mappigns will want the pages marked read-only
12094e950f6fSAlexey Dobriyan  * to track write events. If so, we'll downgrade vm_page_prot
12104e950f6fSAlexey Dobriyan  * to the private version (using protection_map[] without the
12114e950f6fSAlexey Dobriyan  * VM_SHARED bit).
12124e950f6fSAlexey Dobriyan  */
12134e950f6fSAlexey Dobriyan int vma_wants_writenotify(struct vm_area_struct *vma)
12144e950f6fSAlexey Dobriyan {
1215ca16d140SKOSAKI Motohiro 	vm_flags_t vm_flags = vma->vm_flags;
12164e950f6fSAlexey Dobriyan 
12174e950f6fSAlexey Dobriyan 	/* If it was private or non-writable, the write bit is already clear */
12184e950f6fSAlexey Dobriyan 	if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
12194e950f6fSAlexey Dobriyan 		return 0;
12204e950f6fSAlexey Dobriyan 
12214e950f6fSAlexey Dobriyan 	/* The backer wishes to know when pages are first written to? */
12224e950f6fSAlexey Dobriyan 	if (vma->vm_ops && vma->vm_ops->page_mkwrite)
12234e950f6fSAlexey Dobriyan 		return 1;
12244e950f6fSAlexey Dobriyan 
12254e950f6fSAlexey Dobriyan 	/* The open routine did something to the protections already? */
12264e950f6fSAlexey Dobriyan 	if (pgprot_val(vma->vm_page_prot) !=
12273ed75eb8SColy Li 	    pgprot_val(vm_get_page_prot(vm_flags)))
12284e950f6fSAlexey Dobriyan 		return 0;
12294e950f6fSAlexey Dobriyan 
12304e950f6fSAlexey Dobriyan 	/* Specialty mapping? */
12314b6e1e37SKonstantin Khlebnikov 	if (vm_flags & VM_PFNMAP)
12324e950f6fSAlexey Dobriyan 		return 0;
12334e950f6fSAlexey Dobriyan 
12344e950f6fSAlexey Dobriyan 	/* Can the mapping track the dirty pages? */
12354e950f6fSAlexey Dobriyan 	return vma->vm_file && vma->vm_file->f_mapping &&
12364e950f6fSAlexey Dobriyan 		mapping_cap_account_dirty(vma->vm_file->f_mapping);
12374e950f6fSAlexey Dobriyan }
12384e950f6fSAlexey Dobriyan 
1239fc8744adSLinus Torvalds /*
1240fc8744adSLinus Torvalds  * We account for memory if it's a private writeable mapping,
12415a6fe125SMel Gorman  * not hugepages and VM_NORESERVE wasn't set.
1242fc8744adSLinus Torvalds  */
1243ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
1244fc8744adSLinus Torvalds {
12455a6fe125SMel Gorman 	/*
12465a6fe125SMel Gorman 	 * hugetlb has its own accounting separate from the core VM
12475a6fe125SMel Gorman 	 * VM_HUGETLB may not be set yet so we cannot check for that flag.
12485a6fe125SMel Gorman 	 */
12495a6fe125SMel Gorman 	if (file && is_file_hugepages(file))
12505a6fe125SMel Gorman 		return 0;
12515a6fe125SMel Gorman 
1252fc8744adSLinus Torvalds 	return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1253fc8744adSLinus Torvalds }
1254fc8744adSLinus Torvalds 
12550165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr,
12560165ab44SMiklos Szeredi 			  unsigned long len, unsigned long flags,
1257ca16d140SKOSAKI Motohiro 			  vm_flags_t vm_flags, unsigned long pgoff)
12580165ab44SMiklos Szeredi {
12590165ab44SMiklos Szeredi 	struct mm_struct *mm = current->mm;
12600165ab44SMiklos Szeredi 	struct vm_area_struct *vma, *prev;
12610165ab44SMiklos Szeredi 	int correct_wcount = 0;
12620165ab44SMiklos Szeredi 	int error;
12630165ab44SMiklos Szeredi 	struct rb_node **rb_link, *rb_parent;
12640165ab44SMiklos Szeredi 	unsigned long charged = 0;
12650165ab44SMiklos Szeredi 	struct inode *inode =  file ? file->f_path.dentry->d_inode : NULL;
12660165ab44SMiklos Szeredi 
12671da177e4SLinus Torvalds 	/* Clear old maps */
12681da177e4SLinus Torvalds 	error = -ENOMEM;
12691da177e4SLinus Torvalds munmap_back:
12706597d783SHugh Dickins 	if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
12711da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
12721da177e4SLinus Torvalds 			return -ENOMEM;
12731da177e4SLinus Torvalds 		goto munmap_back;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	/* Check against address space limit. */
1277119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
12781da177e4SLinus Torvalds 		return -ENOMEM;
12791da177e4SLinus Torvalds 
1280fc8744adSLinus Torvalds 	/*
1281fc8744adSLinus Torvalds 	 * Set 'VM_NORESERVE' if we should not account for the
12825a6fe125SMel Gorman 	 * memory use of this mapping.
1283fc8744adSLinus Torvalds 	 */
12845a6fe125SMel Gorman 	if ((flags & MAP_NORESERVE)) {
12855a6fe125SMel Gorman 		/* We honor MAP_NORESERVE if allowed to overcommit */
12865a6fe125SMel Gorman 		if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1287fc8744adSLinus Torvalds 			vm_flags |= VM_NORESERVE;
12885a6fe125SMel Gorman 
12895a6fe125SMel Gorman 		/* hugetlb applies strict overcommit unless MAP_NORESERVE */
12905a6fe125SMel Gorman 		if (file && is_file_hugepages(file))
1291cdfd4325SAndy Whitcroft 			vm_flags |= VM_NORESERVE;
12925a6fe125SMel Gorman 	}
1293cdfd4325SAndy Whitcroft 
12941da177e4SLinus Torvalds 	/*
12951da177e4SLinus Torvalds 	 * Private writable mapping: check memory availability
12961da177e4SLinus Torvalds 	 */
12975a6fe125SMel Gorman 	if (accountable_mapping(file, vm_flags)) {
12981da177e4SLinus Torvalds 		charged = len >> PAGE_SHIFT;
1299191c5424SAl Viro 		if (security_vm_enough_memory_mm(mm, charged))
13001da177e4SLinus Torvalds 			return -ENOMEM;
13011da177e4SLinus Torvalds 		vm_flags |= VM_ACCOUNT;
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	/*
1305de33c8dbSLinus Torvalds 	 * Can we just expand an old mapping?
13061da177e4SLinus Torvalds 	 */
1307de33c8dbSLinus Torvalds 	vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1308ba470de4SRik van Riel 	if (vma)
13091da177e4SLinus Torvalds 		goto out;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/*
13121da177e4SLinus Torvalds 	 * Determine the object being mapped and call the appropriate
13131da177e4SLinus Torvalds 	 * specific mapper. the address has already been validated, but
13141da177e4SLinus Torvalds 	 * not unmapped, but the maps are removed from the list.
13151da177e4SLinus Torvalds 	 */
1316c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
13171da177e4SLinus Torvalds 	if (!vma) {
13181da177e4SLinus Torvalds 		error = -ENOMEM;
13191da177e4SLinus Torvalds 		goto unacct_error;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	vma->vm_mm = mm;
13231da177e4SLinus Torvalds 	vma->vm_start = addr;
13241da177e4SLinus Torvalds 	vma->vm_end = addr + len;
13251da177e4SLinus Torvalds 	vma->vm_flags = vm_flags;
13263ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vm_flags);
13271da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
13285beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
13291da177e4SLinus Torvalds 
1330ce8fea7aSHugh Dickins 	error = -EINVAL;	/* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1331ce8fea7aSHugh Dickins 
13321da177e4SLinus Torvalds 	if (file) {
13331da177e4SLinus Torvalds 		if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
13341da177e4SLinus Torvalds 			goto free_vma;
13351da177e4SLinus Torvalds 		if (vm_flags & VM_DENYWRITE) {
13361da177e4SLinus Torvalds 			error = deny_write_access(file);
13371da177e4SLinus Torvalds 			if (error)
13381da177e4SLinus Torvalds 				goto free_vma;
13391da177e4SLinus Torvalds 			correct_wcount = 1;
13401da177e4SLinus Torvalds 		}
1341cb0942b8SAl Viro 		vma->vm_file = get_file(file);
13421da177e4SLinus Torvalds 		error = file->f_op->mmap(file, vma);
13431da177e4SLinus Torvalds 		if (error)
13441da177e4SLinus Torvalds 			goto unmap_and_free_vma;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		/* Can addr have changed??
13471da177e4SLinus Torvalds 		 *
13481da177e4SLinus Torvalds 		 * Answer: Yes, several device drivers can do it in their
13491da177e4SLinus Torvalds 		 *         f_op->mmap method. -DaveM
13501da177e4SLinus Torvalds 		 */
13511da177e4SLinus Torvalds 		addr = vma->vm_start;
13521da177e4SLinus Torvalds 		pgoff = vma->vm_pgoff;
13531da177e4SLinus Torvalds 		vm_flags = vma->vm_flags;
1354f8dbf0a7SHuang Shijie 	} else if (vm_flags & VM_SHARED) {
1355835ee797SAl Viro 		if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
1356835ee797SAl Viro 			goto free_vma;
1357f8dbf0a7SHuang Shijie 		error = shmem_zero_setup(vma);
1358f8dbf0a7SHuang Shijie 		if (error)
1359f8dbf0a7SHuang Shijie 			goto free_vma;
1360f8dbf0a7SHuang Shijie 	}
13611da177e4SLinus Torvalds 
1362c9d0bf24SMagnus Damm 	if (vma_wants_writenotify(vma)) {
1363c9d0bf24SMagnus Damm 		pgprot_t pprot = vma->vm_page_prot;
1364c9d0bf24SMagnus Damm 
1365c9d0bf24SMagnus Damm 		/* Can vma->vm_page_prot have changed??
1366c9d0bf24SMagnus Damm 		 *
1367c9d0bf24SMagnus Damm 		 * Answer: Yes, drivers may have changed it in their
1368c9d0bf24SMagnus Damm 		 *         f_op->mmap method.
1369c9d0bf24SMagnus Damm 		 *
1370c9d0bf24SMagnus Damm 		 * Ensures that vmas marked as uncached stay that way.
1371c9d0bf24SMagnus Damm 		 */
13721ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
1373c9d0bf24SMagnus Damm 		if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1374c9d0bf24SMagnus Damm 			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1375c9d0bf24SMagnus Damm 	}
1376d08b3851SPeter Zijlstra 
13774d3d5b41SOleg Nesterov 	vma_link(mm, vma, prev, rb_link, rb_parent);
13784d3d5b41SOleg Nesterov 	file = vma->vm_file;
13794d3d5b41SOleg Nesterov 
13804d3d5b41SOleg Nesterov 	/* Once vma denies write, undo our temporary denial count */
13814d3d5b41SOleg Nesterov 	if (correct_wcount)
13824d3d5b41SOleg Nesterov 		atomic_inc(&inode->i_writecount);
13831da177e4SLinus Torvalds out:
1384cdd6c482SIngo Molnar 	perf_event_mmap(vma);
13850a4a9391SPeter Zijlstra 
1386ab50b8edSHugh Dickins 	vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
13871da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
138806f9d8c2SKOSAKI Motohiro 		if (!mlock_vma_pages_range(vma, addr, addr + len))
138906f9d8c2SKOSAKI Motohiro 			mm->locked_vm += (len >> PAGE_SHIFT);
1390ba470de4SRik van Riel 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
139154cb8821SNick Piggin 		make_pages_present(addr, addr + len);
13922b144498SSrikar Dronamraju 
1393c7a3a88cSOleg Nesterov 	if (file)
1394c7a3a88cSOleg Nesterov 		uprobe_mmap(vma);
13952b144498SSrikar Dronamraju 
13961da177e4SLinus Torvalds 	return addr;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds unmap_and_free_vma:
13991da177e4SLinus Torvalds 	if (correct_wcount)
14001da177e4SLinus Torvalds 		atomic_inc(&inode->i_writecount);
14011da177e4SLinus Torvalds 	vma->vm_file = NULL;
14021da177e4SLinus Torvalds 	fput(file);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	/* Undo any partial mapping done by a device driver. */
1405e0da382cSHugh Dickins 	unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1406e0da382cSHugh Dickins 	charged = 0;
14071da177e4SLinus Torvalds free_vma:
14081da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
14091da177e4SLinus Torvalds unacct_error:
14101da177e4SLinus Torvalds 	if (charged)
14111da177e4SLinus Torvalds 		vm_unacct_memory(charged);
14121da177e4SLinus Torvalds 	return error;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds /* Get an address range which is currently unmapped.
14161da177e4SLinus Torvalds  * For shmat() with addr=0.
14171da177e4SLinus Torvalds  *
14181da177e4SLinus Torvalds  * Ugly calling convention alert:
14191da177e4SLinus Torvalds  * Return value with the low bits set means error value,
14201da177e4SLinus Torvalds  * ie
14211da177e4SLinus Torvalds  *	if (ret & ~PAGE_MASK)
14221da177e4SLinus Torvalds  *		error = ret;
14231da177e4SLinus Torvalds  *
14241da177e4SLinus Torvalds  * This function "knows" that -ENOMEM has the bits set.
14251da177e4SLinus Torvalds  */
14261da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA
14271da177e4SLinus Torvalds unsigned long
14281da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr,
14291da177e4SLinus Torvalds 		unsigned long len, unsigned long pgoff, unsigned long flags)
14301da177e4SLinus Torvalds {
14311da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
14321da177e4SLinus Torvalds 	struct vm_area_struct *vma;
14331da177e4SLinus Torvalds 	unsigned long start_addr;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	if (len > TASK_SIZE)
14361da177e4SLinus Torvalds 		return -ENOMEM;
14371da177e4SLinus Torvalds 
143806abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
143906abdfb4SBenjamin Herrenschmidt 		return addr;
144006abdfb4SBenjamin Herrenschmidt 
14411da177e4SLinus Torvalds 	if (addr) {
14421da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
14431da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
14441da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
14451da177e4SLinus Torvalds 		    (!vma || addr + len <= vma->vm_start))
14461da177e4SLinus Torvalds 			return addr;
14471da177e4SLinus Torvalds 	}
14481363c3cdSWolfgang Wander 	if (len > mm->cached_hole_size) {
14491da177e4SLinus Torvalds 	        start_addr = addr = mm->free_area_cache;
14501363c3cdSWolfgang Wander 	} else {
14511363c3cdSWolfgang Wander 	        start_addr = addr = TASK_UNMAPPED_BASE;
14521363c3cdSWolfgang Wander 	        mm->cached_hole_size = 0;
14531363c3cdSWolfgang Wander 	}
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds full_search:
14561da177e4SLinus Torvalds 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
14571da177e4SLinus Torvalds 		/* At this point:  (!vma || addr < vma->vm_end). */
14581da177e4SLinus Torvalds 		if (TASK_SIZE - len < addr) {
14591da177e4SLinus Torvalds 			/*
14601da177e4SLinus Torvalds 			 * Start a new search - just in case we missed
14611da177e4SLinus Torvalds 			 * some holes.
14621da177e4SLinus Torvalds 			 */
14631da177e4SLinus Torvalds 			if (start_addr != TASK_UNMAPPED_BASE) {
14641363c3cdSWolfgang Wander 				addr = TASK_UNMAPPED_BASE;
14651363c3cdSWolfgang Wander 			        start_addr = addr;
14661363c3cdSWolfgang Wander 				mm->cached_hole_size = 0;
14671da177e4SLinus Torvalds 				goto full_search;
14681da177e4SLinus Torvalds 			}
14691da177e4SLinus Torvalds 			return -ENOMEM;
14701da177e4SLinus Torvalds 		}
14711da177e4SLinus Torvalds 		if (!vma || addr + len <= vma->vm_start) {
14721da177e4SLinus Torvalds 			/*
14731da177e4SLinus Torvalds 			 * Remember the place where we stopped the search:
14741da177e4SLinus Torvalds 			 */
14751da177e4SLinus Torvalds 			mm->free_area_cache = addr + len;
14761da177e4SLinus Torvalds 			return addr;
14771da177e4SLinus Torvalds 		}
14781363c3cdSWolfgang Wander 		if (addr + mm->cached_hole_size < vma->vm_start)
14791363c3cdSWolfgang Wander 		        mm->cached_hole_size = vma->vm_start - addr;
14801da177e4SLinus Torvalds 		addr = vma->vm_end;
14811da177e4SLinus Torvalds 	}
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds #endif
14841da177e4SLinus Torvalds 
14851363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
14861da177e4SLinus Torvalds {
14871da177e4SLinus Torvalds 	/*
14881da177e4SLinus Torvalds 	 * Is this a new hole at the lowest possible address?
14891da177e4SLinus Torvalds 	 */
1490f44d2198SXiao Guangrong 	if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
14911363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds /*
14951da177e4SLinus Torvalds  * This mmap-allocator allocates new areas top-down from below the
14961da177e4SLinus Torvalds  * stack's low limit (the base):
14971da177e4SLinus Torvalds  */
14981da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
14991da177e4SLinus Torvalds unsigned long
15001da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
15011da177e4SLinus Torvalds 			  const unsigned long len, const unsigned long pgoff,
15021da177e4SLinus Torvalds 			  const unsigned long flags)
15031da177e4SLinus Torvalds {
15041da177e4SLinus Torvalds 	struct vm_area_struct *vma;
15051da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
1506b716ad95SXiao Guangrong 	unsigned long addr = addr0, start_addr;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	/* requested length too big for entire address space */
15091da177e4SLinus Torvalds 	if (len > TASK_SIZE)
15101da177e4SLinus Torvalds 		return -ENOMEM;
15111da177e4SLinus Torvalds 
151206abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
151306abdfb4SBenjamin Herrenschmidt 		return addr;
151406abdfb4SBenjamin Herrenschmidt 
15151da177e4SLinus Torvalds 	/* requesting a specific address */
15161da177e4SLinus Torvalds 	if (addr) {
15171da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
15181da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
15191da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
15201da177e4SLinus Torvalds 				(!vma || addr + len <= vma->vm_start))
15211da177e4SLinus Torvalds 			return addr;
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds 
15241363c3cdSWolfgang Wander 	/* check if free_area_cache is useful for us */
15251363c3cdSWolfgang Wander 	if (len <= mm->cached_hole_size) {
15261363c3cdSWolfgang Wander  	        mm->cached_hole_size = 0;
15271363c3cdSWolfgang Wander  		mm->free_area_cache = mm->mmap_base;
15281363c3cdSWolfgang Wander  	}
15291363c3cdSWolfgang Wander 
1530b716ad95SXiao Guangrong try_again:
15311da177e4SLinus Torvalds 	/* either no address requested or can't fit in requested address hole */
1532b716ad95SXiao Guangrong 	start_addr = addr = mm->free_area_cache;
15331da177e4SLinus Torvalds 
1534b716ad95SXiao Guangrong 	if (addr < len)
1535b716ad95SXiao Guangrong 		goto fail;
15361da177e4SLinus Torvalds 
1537b716ad95SXiao Guangrong 	addr -= len;
15381da177e4SLinus Torvalds 	do {
15391da177e4SLinus Torvalds 		/*
15401da177e4SLinus Torvalds 		 * Lookup failure means no vma is above this address,
15411da177e4SLinus Torvalds 		 * else if new region fits below vma->vm_start,
15421da177e4SLinus Torvalds 		 * return with success:
15431da177e4SLinus Torvalds 		 */
15441da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
15451da177e4SLinus Torvalds 		if (!vma || addr+len <= vma->vm_start)
15461da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
15471da177e4SLinus Torvalds 			return (mm->free_area_cache = addr);
15481da177e4SLinus Torvalds 
15491363c3cdSWolfgang Wander  		/* remember the largest hole we saw so far */
15501363c3cdSWolfgang Wander  		if (addr + mm->cached_hole_size < vma->vm_start)
15511363c3cdSWolfgang Wander  		        mm->cached_hole_size = vma->vm_start - addr;
15521363c3cdSWolfgang Wander 
15531da177e4SLinus Torvalds 		/* try just below the current vma->vm_start */
15541da177e4SLinus Torvalds 		addr = vma->vm_start-len;
155549a43876SLinus Torvalds 	} while (len < vma->vm_start);
15561da177e4SLinus Torvalds 
1557b716ad95SXiao Guangrong fail:
1558b716ad95SXiao Guangrong 	/*
1559b716ad95SXiao Guangrong 	 * if hint left us with no space for the requested
1560b716ad95SXiao Guangrong 	 * mapping then try again:
1561b716ad95SXiao Guangrong 	 *
1562b716ad95SXiao Guangrong 	 * Note: this is different with the case of bottomup
1563b716ad95SXiao Guangrong 	 * which does the fully line-search, but we use find_vma
1564b716ad95SXiao Guangrong 	 * here that causes some holes skipped.
1565b716ad95SXiao Guangrong 	 */
1566b716ad95SXiao Guangrong 	if (start_addr != mm->mmap_base) {
1567b716ad95SXiao Guangrong 		mm->free_area_cache = mm->mmap_base;
1568b716ad95SXiao Guangrong 		mm->cached_hole_size = 0;
1569b716ad95SXiao Guangrong 		goto try_again;
1570b716ad95SXiao Guangrong 	}
1571b716ad95SXiao Guangrong 
15721da177e4SLinus Torvalds 	/*
15731da177e4SLinus Torvalds 	 * A failed mmap() very likely causes application failure,
15741da177e4SLinus Torvalds 	 * so fall back to the bottom-up function here. This scenario
15751da177e4SLinus Torvalds 	 * can happen with large stack limits and large mmap()
15761da177e4SLinus Torvalds 	 * allocations.
15771da177e4SLinus Torvalds 	 */
15781363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
15791da177e4SLinus Torvalds   	mm->free_area_cache = TASK_UNMAPPED_BASE;
15801da177e4SLinus Torvalds 	addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
15811da177e4SLinus Torvalds 	/*
15821da177e4SLinus Torvalds 	 * Restore the topdown base:
15831da177e4SLinus Torvalds 	 */
15841da177e4SLinus Torvalds 	mm->free_area_cache = mm->mmap_base;
15851363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	return addr;
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds #endif
15901da177e4SLinus Torvalds 
15911363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	/*
15941da177e4SLinus Torvalds 	 * Is this a new hole at the highest possible address?
15951da177e4SLinus Torvalds 	 */
15961363c3cdSWolfgang Wander 	if (addr > mm->free_area_cache)
15971363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	/* dont allow allocations above current base */
16001363c3cdSWolfgang Wander 	if (mm->free_area_cache > mm->mmap_base)
16011363c3cdSWolfgang Wander 		mm->free_area_cache = mm->mmap_base;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds unsigned long
16051da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
16061da177e4SLinus Torvalds 		unsigned long pgoff, unsigned long flags)
16071da177e4SLinus Torvalds {
160806abdfb4SBenjamin Herrenschmidt 	unsigned long (*get_area)(struct file *, unsigned long,
160906abdfb4SBenjamin Herrenschmidt 				  unsigned long, unsigned long, unsigned long);
161007ab67c8SLinus Torvalds 
16119206de95SAl Viro 	unsigned long error = arch_mmap_check(addr, len, flags);
16129206de95SAl Viro 	if (error)
16139206de95SAl Viro 		return error;
16149206de95SAl Viro 
16159206de95SAl Viro 	/* Careful about overflows.. */
16169206de95SAl Viro 	if (len > TASK_SIZE)
16179206de95SAl Viro 		return -ENOMEM;
16189206de95SAl Viro 
161907ab67c8SLinus Torvalds 	get_area = current->mm->get_unmapped_area;
162007ab67c8SLinus Torvalds 	if (file && file->f_op && file->f_op->get_unmapped_area)
162107ab67c8SLinus Torvalds 		get_area = file->f_op->get_unmapped_area;
162207ab67c8SLinus Torvalds 	addr = get_area(file, addr, len, pgoff, flags);
162307ab67c8SLinus Torvalds 	if (IS_ERR_VALUE(addr))
162407ab67c8SLinus Torvalds 		return addr;
162507ab67c8SLinus Torvalds 
16261da177e4SLinus Torvalds 	if (addr > TASK_SIZE - len)
16271da177e4SLinus Torvalds 		return -ENOMEM;
16281da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
16291da177e4SLinus Torvalds 		return -EINVAL;
163006abdfb4SBenjamin Herrenschmidt 
16319ac4ed4bSAl Viro 	addr = arch_rebalance_pgtables(addr, len);
16329ac4ed4bSAl Viro 	error = security_mmap_addr(addr);
16339ac4ed4bSAl Viro 	return error ? error : addr;
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
16391da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
16421da177e4SLinus Torvalds 
1643841e31e5SRajman Mekaco 	if (WARN_ON_ONCE(!mm))		/* Remove this in linux-3.6 */
1644841e31e5SRajman Mekaco 		return NULL;
1645841e31e5SRajman Mekaco 
16461da177e4SLinus Torvalds 	/* Check the cache first. */
16471da177e4SLinus Torvalds 	/* (Cache hit rate is typically around 35%.) */
16481da177e4SLinus Torvalds 	vma = mm->mmap_cache;
16491da177e4SLinus Torvalds 	if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
16501da177e4SLinus Torvalds 		struct rb_node *rb_node;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 		rb_node = mm->mm_rb.rb_node;
16531da177e4SLinus Torvalds 		vma = NULL;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 		while (rb_node) {
16561da177e4SLinus Torvalds 			struct vm_area_struct *vma_tmp;
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 			vma_tmp = rb_entry(rb_node,
16591da177e4SLinus Torvalds 					   struct vm_area_struct, vm_rb);
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 			if (vma_tmp->vm_end > addr) {
16621da177e4SLinus Torvalds 				vma = vma_tmp;
16631da177e4SLinus Torvalds 				if (vma_tmp->vm_start <= addr)
16641da177e4SLinus Torvalds 					break;
16651da177e4SLinus Torvalds 				rb_node = rb_node->rb_left;
16661da177e4SLinus Torvalds 			} else
16671da177e4SLinus Torvalds 				rb_node = rb_node->rb_right;
16681da177e4SLinus Torvalds 		}
16691da177e4SLinus Torvalds 		if (vma)
16701da177e4SLinus Torvalds 			mm->mmap_cache = vma;
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 	return vma;
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma);
16761da177e4SLinus Torvalds 
16776bd4837dSKOSAKI Motohiro /*
16786bd4837dSKOSAKI Motohiro  * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
16796bd4837dSKOSAKI Motohiro  */
16801da177e4SLinus Torvalds struct vm_area_struct *
16811da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr,
16821da177e4SLinus Torvalds 			struct vm_area_struct **pprev)
16831da177e4SLinus Torvalds {
16846bd4837dSKOSAKI Motohiro 	struct vm_area_struct *vma;
16851da177e4SLinus Torvalds 
16866bd4837dSKOSAKI Motohiro 	vma = find_vma(mm, addr);
168783cd904dSMikulas Patocka 	if (vma) {
168883cd904dSMikulas Patocka 		*pprev = vma->vm_prev;
168983cd904dSMikulas Patocka 	} else {
169083cd904dSMikulas Patocka 		struct rb_node *rb_node = mm->mm_rb.rb_node;
169183cd904dSMikulas Patocka 		*pprev = NULL;
169283cd904dSMikulas Patocka 		while (rb_node) {
169383cd904dSMikulas Patocka 			*pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
169483cd904dSMikulas Patocka 			rb_node = rb_node->rb_right;
169583cd904dSMikulas Patocka 		}
169683cd904dSMikulas Patocka 	}
16976bd4837dSKOSAKI Motohiro 	return vma;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds /*
17011da177e4SLinus Torvalds  * Verify that the stack growth is acceptable and
17021da177e4SLinus Torvalds  * update accounting. This is shared with both the
17031da177e4SLinus Torvalds  * grow-up and grow-down cases.
17041da177e4SLinus Torvalds  */
17051da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
17081da177e4SLinus Torvalds 	struct rlimit *rlim = current->signal->rlim;
17090d59a01bSAdam Litke 	unsigned long new_start;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	/* address space limit tests */
1712119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, grow))
17131da177e4SLinus Torvalds 		return -ENOMEM;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	/* Stack limit test */
171659e99e5bSJiri Slaby 	if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
17171da177e4SLinus Torvalds 		return -ENOMEM;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	/* mlock limit tests */
17201da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED) {
17211da177e4SLinus Torvalds 		unsigned long locked;
17221da177e4SLinus Torvalds 		unsigned long limit;
17231da177e4SLinus Torvalds 		locked = mm->locked_vm + grow;
172459e99e5bSJiri Slaby 		limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
172559e99e5bSJiri Slaby 		limit >>= PAGE_SHIFT;
17261da177e4SLinus Torvalds 		if (locked > limit && !capable(CAP_IPC_LOCK))
17271da177e4SLinus Torvalds 			return -ENOMEM;
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 
17300d59a01bSAdam Litke 	/* Check to ensure the stack will not grow into a hugetlb-only region */
17310d59a01bSAdam Litke 	new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
17320d59a01bSAdam Litke 			vma->vm_end - size;
17330d59a01bSAdam Litke 	if (is_hugepage_only_range(vma->vm_mm, new_start, size))
17340d59a01bSAdam Litke 		return -EFAULT;
17350d59a01bSAdam Litke 
17361da177e4SLinus Torvalds 	/*
17371da177e4SLinus Torvalds 	 * Overcommit..  This must be the final test, as it will
17381da177e4SLinus Torvalds 	 * update security statistics.
17391da177e4SLinus Torvalds 	 */
174005fa199dSHugh Dickins 	if (security_vm_enough_memory_mm(mm, grow))
17411da177e4SLinus Torvalds 		return -ENOMEM;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	/* Ok, everything looks good - let it rip */
17441da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED)
17451da177e4SLinus Torvalds 		mm->locked_vm += grow;
1746ab50b8edSHugh Dickins 	vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
17471da177e4SLinus Torvalds 	return 0;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
175046dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
17511da177e4SLinus Torvalds /*
175246dea3d0SHugh Dickins  * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
175346dea3d0SHugh Dickins  * vma is the last one with address > vma->vm_end.  Have to extend vma.
17541da177e4SLinus Torvalds  */
175546dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	int error;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSUP))
17601da177e4SLinus Torvalds 		return -EFAULT;
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	/*
17631da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
17641da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
17651da177e4SLinus Torvalds 	 */
17661da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
17671da177e4SLinus Torvalds 		return -ENOMEM;
1768bb4a340eSRik van Riel 	vma_lock_anon_vma(vma);
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	/*
17711da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
17721da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
17731da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
177406b32f3aSHelge Deller 	 * Also guard against wrapping around to address 0.
17751da177e4SLinus Torvalds 	 */
177606b32f3aSHelge Deller 	if (address < PAGE_ALIGN(address+4))
177706b32f3aSHelge Deller 		address = PAGE_ALIGN(address+4);
177806b32f3aSHelge Deller 	else {
1779bb4a340eSRik van Riel 		vma_unlock_anon_vma(vma);
178006b32f3aSHelge Deller 		return -ENOMEM;
178106b32f3aSHelge Deller 	}
17821da177e4SLinus Torvalds 	error = 0;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
17851da177e4SLinus Torvalds 	if (address > vma->vm_end) {
17861da177e4SLinus Torvalds 		unsigned long size, grow;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 		size = address - vma->vm_start;
17891da177e4SLinus Torvalds 		grow = (address - vma->vm_end) >> PAGE_SHIFT;
17901da177e4SLinus Torvalds 
179142c36f63SHugh Dickins 		error = -ENOMEM;
179242c36f63SHugh Dickins 		if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
17931da177e4SLinus Torvalds 			error = acct_stack_growth(vma, size, grow);
17943af9e859SEric B Munson 			if (!error) {
1795bf181b9fSMichel Lespinasse 				anon_vma_interval_tree_pre_update_vma(vma);
17961da177e4SLinus Torvalds 				vma->vm_end = address;
1797bf181b9fSMichel Lespinasse 				anon_vma_interval_tree_post_update_vma(vma);
17983af9e859SEric B Munson 				perf_event_mmap(vma);
17993af9e859SEric B Munson 			}
18001da177e4SLinus Torvalds 		}
180142c36f63SHugh Dickins 	}
1802bb4a340eSRik van Riel 	vma_unlock_anon_vma(vma);
1803b15d00b6SAndrea Arcangeli 	khugepaged_enter_vma_merge(vma);
1804ed8ea815SMichel Lespinasse 	validate_mm(vma->vm_mm);
18051da177e4SLinus Torvalds 	return error;
18061da177e4SLinus Torvalds }
180746dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
180846dea3d0SHugh Dickins 
18091da177e4SLinus Torvalds /*
18101da177e4SLinus Torvalds  * vma is the first one with address < vma->vm_start.  Have to extend vma.
18111da177e4SLinus Torvalds  */
1812d05f3169SMichal Hocko int expand_downwards(struct vm_area_struct *vma,
1813b6a2fea3SOllie Wild 				   unsigned long address)
18141da177e4SLinus Torvalds {
18151da177e4SLinus Torvalds 	int error;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	/*
18181da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
18191da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
18201da177e4SLinus Torvalds 	 */
18211da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
18221da177e4SLinus Torvalds 		return -ENOMEM;
18238869477aSEric Paris 
18248869477aSEric Paris 	address &= PAGE_MASK;
1825e5467859SAl Viro 	error = security_mmap_addr(address);
18268869477aSEric Paris 	if (error)
18278869477aSEric Paris 		return error;
18288869477aSEric Paris 
1829bb4a340eSRik van Riel 	vma_lock_anon_vma(vma);
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	/*
18321da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
18331da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
18341da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
18351da177e4SLinus Torvalds 	 */
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
18381da177e4SLinus Torvalds 	if (address < vma->vm_start) {
18391da177e4SLinus Torvalds 		unsigned long size, grow;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 		size = vma->vm_end - address;
18421da177e4SLinus Torvalds 		grow = (vma->vm_start - address) >> PAGE_SHIFT;
18431da177e4SLinus Torvalds 
1844a626ca6aSLinus Torvalds 		error = -ENOMEM;
1845a626ca6aSLinus Torvalds 		if (grow <= vma->vm_pgoff) {
18461da177e4SLinus Torvalds 			error = acct_stack_growth(vma, size, grow);
18471da177e4SLinus Torvalds 			if (!error) {
1848bf181b9fSMichel Lespinasse 				anon_vma_interval_tree_pre_update_vma(vma);
18491da177e4SLinus Torvalds 				vma->vm_start = address;
18501da177e4SLinus Torvalds 				vma->vm_pgoff -= grow;
1851bf181b9fSMichel Lespinasse 				anon_vma_interval_tree_post_update_vma(vma);
18523af9e859SEric B Munson 				perf_event_mmap(vma);
18531da177e4SLinus Torvalds 			}
18541da177e4SLinus Torvalds 		}
1855a626ca6aSLinus Torvalds 	}
1856bb4a340eSRik van Riel 	vma_unlock_anon_vma(vma);
1857b15d00b6SAndrea Arcangeli 	khugepaged_enter_vma_merge(vma);
1858ed8ea815SMichel Lespinasse 	validate_mm(vma->vm_mm);
18591da177e4SLinus Torvalds 	return error;
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
1862b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP
1863b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1864b6a2fea3SOllie Wild {
1865b6a2fea3SOllie Wild 	return expand_upwards(vma, address);
1866b6a2fea3SOllie Wild }
1867b6a2fea3SOllie Wild 
1868b6a2fea3SOllie Wild struct vm_area_struct *
1869b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr)
1870b6a2fea3SOllie Wild {
1871b6a2fea3SOllie Wild 	struct vm_area_struct *vma, *prev;
1872b6a2fea3SOllie Wild 
1873b6a2fea3SOllie Wild 	addr &= PAGE_MASK;
1874b6a2fea3SOllie Wild 	vma = find_vma_prev(mm, addr, &prev);
1875b6a2fea3SOllie Wild 	if (vma && (vma->vm_start <= addr))
1876b6a2fea3SOllie Wild 		return vma;
18771c127185SDenys Vlasenko 	if (!prev || expand_stack(prev, addr))
1878b6a2fea3SOllie Wild 		return NULL;
1879ba470de4SRik van Riel 	if (prev->vm_flags & VM_LOCKED) {
1880c58267c3SKOSAKI Motohiro 		mlock_vma_pages_range(prev, addr, prev->vm_end);
1881ba470de4SRik van Riel 	}
1882b6a2fea3SOllie Wild 	return prev;
1883b6a2fea3SOllie Wild }
1884b6a2fea3SOllie Wild #else
1885b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1886b6a2fea3SOllie Wild {
1887b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1888b6a2fea3SOllie Wild }
1889b6a2fea3SOllie Wild 
18901da177e4SLinus Torvalds struct vm_area_struct *
18911da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	struct vm_area_struct * vma;
18941da177e4SLinus Torvalds 	unsigned long start;
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	addr &= PAGE_MASK;
18971da177e4SLinus Torvalds 	vma = find_vma(mm,addr);
18981da177e4SLinus Torvalds 	if (!vma)
18991da177e4SLinus Torvalds 		return NULL;
19001da177e4SLinus Torvalds 	if (vma->vm_start <= addr)
19011da177e4SLinus Torvalds 		return vma;
19021da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSDOWN))
19031da177e4SLinus Torvalds 		return NULL;
19041da177e4SLinus Torvalds 	start = vma->vm_start;
19051da177e4SLinus Torvalds 	if (expand_stack(vma, addr))
19061da177e4SLinus Torvalds 		return NULL;
1907ba470de4SRik van Riel 	if (vma->vm_flags & VM_LOCKED) {
1908c58267c3SKOSAKI Motohiro 		mlock_vma_pages_range(vma, addr, start);
1909ba470de4SRik van Riel 	}
19101da177e4SLinus Torvalds 	return vma;
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds #endif
19131da177e4SLinus Torvalds 
19142c0b3814SHugh Dickins /*
19152c0b3814SHugh Dickins  * Ok - we have the memory areas we should free on the vma list,
19162c0b3814SHugh Dickins  * so release them, and do the vma updates.
19171da177e4SLinus Torvalds  *
19182c0b3814SHugh Dickins  * Called with the mm semaphore held.
19191da177e4SLinus Torvalds  */
19202c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
19211da177e4SLinus Torvalds {
19224f74d2c8SLinus Torvalds 	unsigned long nr_accounted = 0;
19234f74d2c8SLinus Torvalds 
1924365e9c87SHugh Dickins 	/* Update high watermark before we lower total_vm */
1925365e9c87SHugh Dickins 	update_hiwater_vm(mm);
19262c0b3814SHugh Dickins 	do {
1927ab50b8edSHugh Dickins 		long nrpages = vma_pages(vma);
19281da177e4SLinus Torvalds 
19294f74d2c8SLinus Torvalds 		if (vma->vm_flags & VM_ACCOUNT)
19304f74d2c8SLinus Torvalds 			nr_accounted += nrpages;
1931ab50b8edSHugh Dickins 		vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
1932a8fb5618SHugh Dickins 		vma = remove_vma(vma);
1933146425a3SHugh Dickins 	} while (vma);
19344f74d2c8SLinus Torvalds 	vm_unacct_memory(nr_accounted);
19351da177e4SLinus Torvalds 	validate_mm(mm);
19361da177e4SLinus Torvalds }
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds /*
19391da177e4SLinus Torvalds  * Get rid of page table information in the indicated region.
19401da177e4SLinus Torvalds  *
1941f10df686SPaolo 'Blaisorblade' Giarrusso  * Called with the mm semaphore held.
19421da177e4SLinus Torvalds  */
19431da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm,
1944e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
1945e0da382cSHugh Dickins 		unsigned long start, unsigned long end)
19461da177e4SLinus Torvalds {
1947e0da382cSHugh Dickins 	struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
1948d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	lru_add_drain();
1951d16dfc55SPeter Zijlstra 	tlb_gather_mmu(&tlb, mm, 0);
1952365e9c87SHugh Dickins 	update_hiwater_rss(mm);
19534f74d2c8SLinus Torvalds 	unmap_vmas(&tlb, vma, start, end);
1954d16dfc55SPeter Zijlstra 	free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
1955e0da382cSHugh Dickins 				 next ? next->vm_start : 0);
1956d16dfc55SPeter Zijlstra 	tlb_finish_mmu(&tlb, start, end);
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds /*
19601da177e4SLinus Torvalds  * Create a list of vma's touched by the unmap, removing them from the mm's
19611da177e4SLinus Torvalds  * vma list as we go..
19621da177e4SLinus Torvalds  */
19631da177e4SLinus Torvalds static void
19641da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
19651da177e4SLinus Torvalds 	struct vm_area_struct *prev, unsigned long end)
19661da177e4SLinus Torvalds {
19671da177e4SLinus Torvalds 	struct vm_area_struct **insertion_point;
19681da177e4SLinus Torvalds 	struct vm_area_struct *tail_vma = NULL;
19691363c3cdSWolfgang Wander 	unsigned long addr;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1972297c5eeeSLinus Torvalds 	vma->vm_prev = NULL;
19731da177e4SLinus Torvalds 	do {
19741da177e4SLinus Torvalds 		rb_erase(&vma->vm_rb, &mm->mm_rb);
19751da177e4SLinus Torvalds 		mm->map_count--;
19761da177e4SLinus Torvalds 		tail_vma = vma;
19771da177e4SLinus Torvalds 		vma = vma->vm_next;
19781da177e4SLinus Torvalds 	} while (vma && vma->vm_start < end);
19791da177e4SLinus Torvalds 	*insertion_point = vma;
1980297c5eeeSLinus Torvalds 	if (vma)
1981297c5eeeSLinus Torvalds 		vma->vm_prev = prev;
19821da177e4SLinus Torvalds 	tail_vma->vm_next = NULL;
19831363c3cdSWolfgang Wander 	if (mm->unmap_area == arch_unmap_area)
19841363c3cdSWolfgang Wander 		addr = prev ? prev->vm_end : mm->mmap_base;
19851363c3cdSWolfgang Wander 	else
19861363c3cdSWolfgang Wander 		addr = vma ?  vma->vm_start : mm->mmap_base;
19871363c3cdSWolfgang Wander 	mm->unmap_area(mm, addr);
19881da177e4SLinus Torvalds 	mm->mmap_cache = NULL;		/* Kill the cache. */
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds /*
1992659ace58SKOSAKI Motohiro  * __split_vma() bypasses sysctl_max_map_count checking.  We use this on the
1993659ace58SKOSAKI Motohiro  * munmap path where it doesn't make sense to fail.
19941da177e4SLinus Torvalds  */
1995659ace58SKOSAKI Motohiro static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
19961da177e4SLinus Torvalds 	      unsigned long addr, int new_below)
19971da177e4SLinus Torvalds {
19981da177e4SLinus Torvalds 	struct mempolicy *pol;
19991da177e4SLinus Torvalds 	struct vm_area_struct *new;
20005beb4930SRik van Riel 	int err = -ENOMEM;
20011da177e4SLinus Torvalds 
2002a5516438SAndi Kleen 	if (is_vm_hugetlb_page(vma) && (addr &
2003a5516438SAndi Kleen 					~(huge_page_mask(hstate_vma(vma)))))
20041da177e4SLinus Torvalds 		return -EINVAL;
20051da177e4SLinus Torvalds 
2006e94b1766SChristoph Lameter 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
20071da177e4SLinus Torvalds 	if (!new)
20085beb4930SRik van Riel 		goto out_err;
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	/* most fields are the same, copy all, and then fixup */
20111da177e4SLinus Torvalds 	*new = *vma;
20121da177e4SLinus Torvalds 
20135beb4930SRik van Riel 	INIT_LIST_HEAD(&new->anon_vma_chain);
20145beb4930SRik van Riel 
20151da177e4SLinus Torvalds 	if (new_below)
20161da177e4SLinus Torvalds 		new->vm_end = addr;
20171da177e4SLinus Torvalds 	else {
20181da177e4SLinus Torvalds 		new->vm_start = addr;
20191da177e4SLinus Torvalds 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
20201da177e4SLinus Torvalds 	}
20211da177e4SLinus Torvalds 
2022846a16bfSLee Schermerhorn 	pol = mpol_dup(vma_policy(vma));
20231da177e4SLinus Torvalds 	if (IS_ERR(pol)) {
20245beb4930SRik van Riel 		err = PTR_ERR(pol);
20255beb4930SRik van Riel 		goto out_free_vma;
20261da177e4SLinus Torvalds 	}
20271da177e4SLinus Torvalds 	vma_set_policy(new, pol);
20281da177e4SLinus Torvalds 
20295beb4930SRik van Riel 	if (anon_vma_clone(new, vma))
20305beb4930SRik van Riel 		goto out_free_mpol;
20315beb4930SRik van Riel 
2032e9714acfSKonstantin Khlebnikov 	if (new->vm_file)
20331da177e4SLinus Torvalds 		get_file(new->vm_file);
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	if (new->vm_ops && new->vm_ops->open)
20361da177e4SLinus Torvalds 		new->vm_ops->open(new);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	if (new_below)
20395beb4930SRik van Riel 		err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
20401da177e4SLinus Torvalds 			((addr - new->vm_start) >> PAGE_SHIFT), new);
20411da177e4SLinus Torvalds 	else
20425beb4930SRik van Riel 		err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
20431da177e4SLinus Torvalds 
20445beb4930SRik van Riel 	/* Success. */
20455beb4930SRik van Riel 	if (!err)
20461da177e4SLinus Torvalds 		return 0;
20475beb4930SRik van Riel 
20485beb4930SRik van Riel 	/* Clean everything up if vma_adjust failed. */
204958927533SRik van Riel 	if (new->vm_ops && new->vm_ops->close)
20505beb4930SRik van Riel 		new->vm_ops->close(new);
2051e9714acfSKonstantin Khlebnikov 	if (new->vm_file)
20525beb4930SRik van Riel 		fput(new->vm_file);
20532aeadc30SAndrea Arcangeli 	unlink_anon_vmas(new);
20545beb4930SRik van Riel  out_free_mpol:
20555beb4930SRik van Riel 	mpol_put(pol);
20565beb4930SRik van Riel  out_free_vma:
20575beb4930SRik van Riel 	kmem_cache_free(vm_area_cachep, new);
20585beb4930SRik van Riel  out_err:
20595beb4930SRik van Riel 	return err;
20601da177e4SLinus Torvalds }
20611da177e4SLinus Torvalds 
2062659ace58SKOSAKI Motohiro /*
2063659ace58SKOSAKI Motohiro  * Split a vma into two pieces at address 'addr', a new vma is allocated
2064659ace58SKOSAKI Motohiro  * either for the first part or the tail.
2065659ace58SKOSAKI Motohiro  */
2066659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2067659ace58SKOSAKI Motohiro 	      unsigned long addr, int new_below)
2068659ace58SKOSAKI Motohiro {
2069659ace58SKOSAKI Motohiro 	if (mm->map_count >= sysctl_max_map_count)
2070659ace58SKOSAKI Motohiro 		return -ENOMEM;
2071659ace58SKOSAKI Motohiro 
2072659ace58SKOSAKI Motohiro 	return __split_vma(mm, vma, addr, new_below);
2073659ace58SKOSAKI Motohiro }
2074659ace58SKOSAKI Motohiro 
20751da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds
20761da177e4SLinus Torvalds  * what needs doing, and the areas themselves, which do the
20771da177e4SLinus Torvalds  * work.  This now handles partial unmappings.
20781da177e4SLinus Torvalds  * Jeremy Fitzhardinge <jeremy@goop.org>
20791da177e4SLinus Torvalds  */
20801da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
20811da177e4SLinus Torvalds {
20821da177e4SLinus Torvalds 	unsigned long end;
2083146425a3SHugh Dickins 	struct vm_area_struct *vma, *prev, *last;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
20861da177e4SLinus Torvalds 		return -EINVAL;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	if ((len = PAGE_ALIGN(len)) == 0)
20891da177e4SLinus Torvalds 		return -EINVAL;
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	/* Find the first overlapping VMA */
20929be34c9dSLinus Torvalds 	vma = find_vma(mm, start);
2093146425a3SHugh Dickins 	if (!vma)
20941da177e4SLinus Torvalds 		return 0;
20959be34c9dSLinus Torvalds 	prev = vma->vm_prev;
2096146425a3SHugh Dickins 	/* we have  start < vma->vm_end  */
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	/* if it doesn't overlap, we have nothing.. */
20991da177e4SLinus Torvalds 	end = start + len;
2100146425a3SHugh Dickins 	if (vma->vm_start >= end)
21011da177e4SLinus Torvalds 		return 0;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	/*
21041da177e4SLinus Torvalds 	 * If we need to split any vma, do it now to save pain later.
21051da177e4SLinus Torvalds 	 *
21061da177e4SLinus Torvalds 	 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
21071da177e4SLinus Torvalds 	 * unmapped vm_area_struct will remain in use: so lower split_vma
21081da177e4SLinus Torvalds 	 * places tmp vma above, and higher split_vma places tmp vma below.
21091da177e4SLinus Torvalds 	 */
2110146425a3SHugh Dickins 	if (start > vma->vm_start) {
2111659ace58SKOSAKI Motohiro 		int error;
2112659ace58SKOSAKI Motohiro 
2113659ace58SKOSAKI Motohiro 		/*
2114659ace58SKOSAKI Motohiro 		 * Make sure that map_count on return from munmap() will
2115659ace58SKOSAKI Motohiro 		 * not exceed its limit; but let map_count go just above
2116659ace58SKOSAKI Motohiro 		 * its limit temporarily, to help free resources as expected.
2117659ace58SKOSAKI Motohiro 		 */
2118659ace58SKOSAKI Motohiro 		if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2119659ace58SKOSAKI Motohiro 			return -ENOMEM;
2120659ace58SKOSAKI Motohiro 
2121659ace58SKOSAKI Motohiro 		error = __split_vma(mm, vma, start, 0);
21221da177e4SLinus Torvalds 		if (error)
21231da177e4SLinus Torvalds 			return error;
2124146425a3SHugh Dickins 		prev = vma;
21251da177e4SLinus Torvalds 	}
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	/* Does it split the last one? */
21281da177e4SLinus Torvalds 	last = find_vma(mm, end);
21291da177e4SLinus Torvalds 	if (last && end > last->vm_start) {
2130659ace58SKOSAKI Motohiro 		int error = __split_vma(mm, last, end, 1);
21311da177e4SLinus Torvalds 		if (error)
21321da177e4SLinus Torvalds 			return error;
21331da177e4SLinus Torvalds 	}
2134146425a3SHugh Dickins 	vma = prev? prev->vm_next: mm->mmap;
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	/*
2137ba470de4SRik van Riel 	 * unlock any mlock()ed ranges before detaching vmas
2138ba470de4SRik van Riel 	 */
2139ba470de4SRik van Riel 	if (mm->locked_vm) {
2140ba470de4SRik van Riel 		struct vm_area_struct *tmp = vma;
2141ba470de4SRik van Riel 		while (tmp && tmp->vm_start < end) {
2142ba470de4SRik van Riel 			if (tmp->vm_flags & VM_LOCKED) {
2143ba470de4SRik van Riel 				mm->locked_vm -= vma_pages(tmp);
2144ba470de4SRik van Riel 				munlock_vma_pages_all(tmp);
2145ba470de4SRik van Riel 			}
2146ba470de4SRik van Riel 			tmp = tmp->vm_next;
2147ba470de4SRik van Riel 		}
2148ba470de4SRik van Riel 	}
2149ba470de4SRik van Riel 
2150ba470de4SRik van Riel 	/*
21511da177e4SLinus Torvalds 	 * Remove the vma's, and unmap the actual pages
21521da177e4SLinus Torvalds 	 */
2153146425a3SHugh Dickins 	detach_vmas_to_be_unmapped(mm, vma, prev, end);
2154146425a3SHugh Dickins 	unmap_region(mm, vma, prev, start, end);
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	/* Fix up all other VM information */
21572c0b3814SHugh Dickins 	remove_vma_list(mm, vma);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	return 0;
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
2162bfce281cSAl Viro int vm_munmap(unsigned long start, size_t len)
2163a46ef99dSLinus Torvalds {
2164a46ef99dSLinus Torvalds 	int ret;
2165bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
2166a46ef99dSLinus Torvalds 
2167a46ef99dSLinus Torvalds 	down_write(&mm->mmap_sem);
2168a46ef99dSLinus Torvalds 	ret = do_munmap(mm, start, len);
2169a46ef99dSLinus Torvalds 	up_write(&mm->mmap_sem);
2170a46ef99dSLinus Torvalds 	return ret;
2171a46ef99dSLinus Torvalds }
2172a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
2173a46ef99dSLinus Torvalds 
21746a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
21751da177e4SLinus Torvalds {
21761da177e4SLinus Torvalds 	profile_munmap(addr);
2177bfce281cSAl Viro 	return vm_munmap(addr, len);
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm)
21811da177e4SLinus Torvalds {
2182a241ec65SPaul E. McKenney #ifdef CONFIG_DEBUG_VM
21831da177e4SLinus Torvalds 	if (unlikely(down_read_trylock(&mm->mmap_sem))) {
21841da177e4SLinus Torvalds 		WARN_ON(1);
21851da177e4SLinus Torvalds 		up_read(&mm->mmap_sem);
21861da177e4SLinus Torvalds 	}
21871da177e4SLinus Torvalds #endif
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds /*
21911da177e4SLinus Torvalds  *  this is really a simplified "do_mmap".  it only handles
21921da177e4SLinus Torvalds  *  anonymous maps.  eventually we may be able to do some
21931da177e4SLinus Torvalds  *  brk-specific accounting here.
21941da177e4SLinus Torvalds  */
2195e4eb1ff6SLinus Torvalds static unsigned long do_brk(unsigned long addr, unsigned long len)
21961da177e4SLinus Torvalds {
21971da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
21981da177e4SLinus Torvalds 	struct vm_area_struct * vma, * prev;
21991da177e4SLinus Torvalds 	unsigned long flags;
22001da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
22011da177e4SLinus Torvalds 	pgoff_t pgoff = addr >> PAGE_SHIFT;
22023a459756SKirill Korotaev 	int error;
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
22051da177e4SLinus Torvalds 	if (!len)
22061da177e4SLinus Torvalds 		return addr;
22071da177e4SLinus Torvalds 
22083a459756SKirill Korotaev 	flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
22093a459756SKirill Korotaev 
22102c6a1016SAl Viro 	error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
22112c6a1016SAl Viro 	if (error & ~PAGE_MASK)
22123a459756SKirill Korotaev 		return error;
22133a459756SKirill Korotaev 
22141da177e4SLinus Torvalds 	/*
22151da177e4SLinus Torvalds 	 * mlock MCL_FUTURE?
22161da177e4SLinus Torvalds 	 */
22171da177e4SLinus Torvalds 	if (mm->def_flags & VM_LOCKED) {
22181da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
221993ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
222093ea1d0aSChris Wright 		locked += mm->locked_vm;
222159e99e5bSJiri Slaby 		lock_limit = rlimit(RLIMIT_MEMLOCK);
222293ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
22231da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
22241da177e4SLinus Torvalds 			return -EAGAIN;
22251da177e4SLinus Torvalds 	}
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	/*
22281da177e4SLinus Torvalds 	 * mm->mmap_sem is required to protect against another thread
22291da177e4SLinus Torvalds 	 * changing the mappings in case we sleep.
22301da177e4SLinus Torvalds 	 */
22311da177e4SLinus Torvalds 	verify_mm_writelocked(mm);
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	/*
22341da177e4SLinus Torvalds 	 * Clear old maps.  this also does some error checking for us
22351da177e4SLinus Torvalds 	 */
22361da177e4SLinus Torvalds  munmap_back:
22376597d783SHugh Dickins 	if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
22381da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
22391da177e4SLinus Torvalds 			return -ENOMEM;
22401da177e4SLinus Torvalds 		goto munmap_back;
22411da177e4SLinus Torvalds 	}
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	/* Check against address space limits *after* clearing old maps... */
2244119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
22451da177e4SLinus Torvalds 		return -ENOMEM;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
22481da177e4SLinus Torvalds 		return -ENOMEM;
22491da177e4SLinus Torvalds 
2250191c5424SAl Viro 	if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
22511da177e4SLinus Torvalds 		return -ENOMEM;
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	/* Can we just expand an old private anonymous mapping? */
2254ba470de4SRik van Riel 	vma = vma_merge(mm, prev, addr, addr + len, flags,
2255ba470de4SRik van Riel 					NULL, NULL, pgoff, NULL);
2256ba470de4SRik van Riel 	if (vma)
22571da177e4SLinus Torvalds 		goto out;
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	/*
22601da177e4SLinus Torvalds 	 * create a vma struct for an anonymous mapping
22611da177e4SLinus Torvalds 	 */
2262c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
22631da177e4SLinus Torvalds 	if (!vma) {
22641da177e4SLinus Torvalds 		vm_unacct_memory(len >> PAGE_SHIFT);
22651da177e4SLinus Torvalds 		return -ENOMEM;
22661da177e4SLinus Torvalds 	}
22671da177e4SLinus Torvalds 
22685beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
22691da177e4SLinus Torvalds 	vma->vm_mm = mm;
22701da177e4SLinus Torvalds 	vma->vm_start = addr;
22711da177e4SLinus Torvalds 	vma->vm_end = addr + len;
22721da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
22731da177e4SLinus Torvalds 	vma->vm_flags = flags;
22743ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(flags);
22751da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
22761da177e4SLinus Torvalds out:
22773af9e859SEric B Munson 	perf_event_mmap(vma);
22781da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
22791da177e4SLinus Torvalds 	if (flags & VM_LOCKED) {
2280ba470de4SRik van Riel 		if (!mlock_vma_pages_range(vma, addr, addr + len))
2281ba470de4SRik van Riel 			mm->locked_vm += (len >> PAGE_SHIFT);
22821da177e4SLinus Torvalds 	}
22831da177e4SLinus Torvalds 	return addr;
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
2286e4eb1ff6SLinus Torvalds unsigned long vm_brk(unsigned long addr, unsigned long len)
2287e4eb1ff6SLinus Torvalds {
2288e4eb1ff6SLinus Torvalds 	struct mm_struct *mm = current->mm;
2289e4eb1ff6SLinus Torvalds 	unsigned long ret;
2290e4eb1ff6SLinus Torvalds 
2291e4eb1ff6SLinus Torvalds 	down_write(&mm->mmap_sem);
2292e4eb1ff6SLinus Torvalds 	ret = do_brk(addr, len);
2293e4eb1ff6SLinus Torvalds 	up_write(&mm->mmap_sem);
2294e4eb1ff6SLinus Torvalds 	return ret;
2295e4eb1ff6SLinus Torvalds }
2296e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk);
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds /* Release all mmaps. */
22991da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
23001da177e4SLinus Torvalds {
2301d16dfc55SPeter Zijlstra 	struct mmu_gather tlb;
2302ba470de4SRik van Riel 	struct vm_area_struct *vma;
23031da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
23041da177e4SLinus Torvalds 
2305d6dd61c8SJeremy Fitzhardinge 	/* mm's last user has gone, and its about to be pulled down */
2306cddb8a5cSAndrea Arcangeli 	mmu_notifier_release(mm);
2307d6dd61c8SJeremy Fitzhardinge 
2308ba470de4SRik van Riel 	if (mm->locked_vm) {
2309ba470de4SRik van Riel 		vma = mm->mmap;
2310ba470de4SRik van Riel 		while (vma) {
2311ba470de4SRik van Riel 			if (vma->vm_flags & VM_LOCKED)
2312ba470de4SRik van Riel 				munlock_vma_pages_all(vma);
2313ba470de4SRik van Riel 			vma = vma->vm_next;
2314ba470de4SRik van Riel 		}
2315ba470de4SRik van Riel 	}
23169480c53eSJeremy Fitzhardinge 
23179480c53eSJeremy Fitzhardinge 	arch_exit_mmap(mm);
23189480c53eSJeremy Fitzhardinge 
2319ba470de4SRik van Riel 	vma = mm->mmap;
23209480c53eSJeremy Fitzhardinge 	if (!vma)	/* Can happen if dup_mmap() received an OOM */
23219480c53eSJeremy Fitzhardinge 		return;
23229480c53eSJeremy Fitzhardinge 
23231da177e4SLinus Torvalds 	lru_add_drain();
23241da177e4SLinus Torvalds 	flush_cache_mm(mm);
2325d16dfc55SPeter Zijlstra 	tlb_gather_mmu(&tlb, mm, 1);
2326901608d9SOleg Nesterov 	/* update_hiwater_rss(mm) here? but nobody should be looking */
2327e0da382cSHugh Dickins 	/* Use -1 here to ensure all VMAs in the mm are unmapped */
23284f74d2c8SLinus Torvalds 	unmap_vmas(&tlb, vma, 0, -1);
23299ba69294SHugh Dickins 
2330d16dfc55SPeter Zijlstra 	free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
2331853f5e26SAl Viro 	tlb_finish_mmu(&tlb, 0, -1);
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	/*
23348f4f8c16SHugh Dickins 	 * Walk the list again, actually closing and freeing it,
23358f4f8c16SHugh Dickins 	 * with preemption enabled, without holding any MM locks.
23361da177e4SLinus Torvalds 	 */
23374f74d2c8SLinus Torvalds 	while (vma) {
23384f74d2c8SLinus Torvalds 		if (vma->vm_flags & VM_ACCOUNT)
23394f74d2c8SLinus Torvalds 			nr_accounted += vma_pages(vma);
2340a8fb5618SHugh Dickins 		vma = remove_vma(vma);
23414f74d2c8SLinus Torvalds 	}
23424f74d2c8SLinus Torvalds 	vm_unacct_memory(nr_accounted);
2343e0da382cSHugh Dickins 
2344f9aed62aSHugh Dickins 	WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
23451da177e4SLinus Torvalds }
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address
23481da177e4SLinus Torvalds  * and into the inode's i_mmap tree.  If vm_file is non-NULL
23493d48ae45SPeter Zijlstra  * then i_mmap_mutex is taken here.
23501da177e4SLinus Torvalds  */
23511da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
23521da177e4SLinus Torvalds {
23536597d783SHugh Dickins 	struct vm_area_struct *prev;
23541da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 	/*
23571da177e4SLinus Torvalds 	 * The vm_pgoff of a purely anonymous vma should be irrelevant
23581da177e4SLinus Torvalds 	 * until its first write fault, when page's anon_vma and index
23591da177e4SLinus Torvalds 	 * are set.  But now set the vm_pgoff it will almost certainly
23601da177e4SLinus Torvalds 	 * end up with (unless mremap moves it elsewhere before that
23611da177e4SLinus Torvalds 	 * first wfault), so /proc/pid/maps tells a consistent story.
23621da177e4SLinus Torvalds 	 *
23631da177e4SLinus Torvalds 	 * By setting it to reflect the virtual start address of the
23641da177e4SLinus Torvalds 	 * vma, merges and splits can happen in a seamless way, just
23651da177e4SLinus Torvalds 	 * using the existing file pgoff checks and manipulations.
23661da177e4SLinus Torvalds 	 * Similarly in do_mmap_pgoff and in do_brk.
23671da177e4SLinus Torvalds 	 */
23681da177e4SLinus Torvalds 	if (!vma->vm_file) {
23691da177e4SLinus Torvalds 		BUG_ON(vma->anon_vma);
23701da177e4SLinus Torvalds 		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
23711da177e4SLinus Torvalds 	}
23726597d783SHugh Dickins 	if (find_vma_links(mm, vma->vm_start, vma->vm_end,
23736597d783SHugh Dickins 			   &prev, &rb_link, &rb_parent))
23741da177e4SLinus Torvalds 		return -ENOMEM;
23752fd4ef85SHugh Dickins 	if ((vma->vm_flags & VM_ACCOUNT) &&
237634b4e4aaSAlan Cox 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
23772fd4ef85SHugh Dickins 		return -ENOMEM;
23782b144498SSrikar Dronamraju 
23791da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
23801da177e4SLinus Torvalds 	return 0;
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds /*
23841da177e4SLinus Torvalds  * Copy the vma structure to a new location in the same mm,
23851da177e4SLinus Torvalds  * prior to moving page table entries, to effect an mremap move.
23861da177e4SLinus Torvalds  */
23871da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
238838a76013SMichel Lespinasse 	unsigned long addr, unsigned long len, pgoff_t pgoff,
238938a76013SMichel Lespinasse 	bool *need_rmap_locks)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds 	struct vm_area_struct *vma = *vmap;
23921da177e4SLinus Torvalds 	unsigned long vma_start = vma->vm_start;
23931da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
23941da177e4SLinus Torvalds 	struct vm_area_struct *new_vma, *prev;
23951da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
23961da177e4SLinus Torvalds 	struct mempolicy *pol;
2397948f017bSAndrea Arcangeli 	bool faulted_in_anon_vma = true;
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	/*
24001da177e4SLinus Torvalds 	 * If anonymous vma has not yet been faulted, update new pgoff
24011da177e4SLinus Torvalds 	 * to match new location, to increase its chance of merging.
24021da177e4SLinus Torvalds 	 */
2403948f017bSAndrea Arcangeli 	if (unlikely(!vma->vm_file && !vma->anon_vma)) {
24041da177e4SLinus Torvalds 		pgoff = addr >> PAGE_SHIFT;
2405948f017bSAndrea Arcangeli 		faulted_in_anon_vma = false;
2406948f017bSAndrea Arcangeli 	}
24071da177e4SLinus Torvalds 
24086597d783SHugh Dickins 	if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
24096597d783SHugh Dickins 		return NULL;	/* should never get here */
24101da177e4SLinus Torvalds 	new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
24111da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
24121da177e4SLinus Torvalds 	if (new_vma) {
24131da177e4SLinus Torvalds 		/*
24141da177e4SLinus Torvalds 		 * Source vma may have been merged into new_vma
24151da177e4SLinus Torvalds 		 */
2416948f017bSAndrea Arcangeli 		if (unlikely(vma_start >= new_vma->vm_start &&
2417948f017bSAndrea Arcangeli 			     vma_start < new_vma->vm_end)) {
2418948f017bSAndrea Arcangeli 			/*
2419948f017bSAndrea Arcangeli 			 * The only way we can get a vma_merge with
2420948f017bSAndrea Arcangeli 			 * self during an mremap is if the vma hasn't
2421948f017bSAndrea Arcangeli 			 * been faulted in yet and we were allowed to
2422948f017bSAndrea Arcangeli 			 * reset the dst vma->vm_pgoff to the
2423948f017bSAndrea Arcangeli 			 * destination address of the mremap to allow
2424948f017bSAndrea Arcangeli 			 * the merge to happen. mremap must change the
2425948f017bSAndrea Arcangeli 			 * vm_pgoff linearity between src and dst vmas
2426948f017bSAndrea Arcangeli 			 * (in turn preventing a vma_merge) to be
2427948f017bSAndrea Arcangeli 			 * safe. It is only safe to keep the vm_pgoff
2428948f017bSAndrea Arcangeli 			 * linear if there are no pages mapped yet.
2429948f017bSAndrea Arcangeli 			 */
2430948f017bSAndrea Arcangeli 			VM_BUG_ON(faulted_in_anon_vma);
243138a76013SMichel Lespinasse 			*vmap = vma = new_vma;
2432108d6642SMichel Lespinasse 		}
243338a76013SMichel Lespinasse 		*need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
24341da177e4SLinus Torvalds 	} else {
2435e94b1766SChristoph Lameter 		new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
24361da177e4SLinus Torvalds 		if (new_vma) {
24371da177e4SLinus Torvalds 			*new_vma = *vma;
24381da177e4SLinus Torvalds 			new_vma->vm_start = addr;
24391da177e4SLinus Torvalds 			new_vma->vm_end = addr + len;
24401da177e4SLinus Torvalds 			new_vma->vm_pgoff = pgoff;
2441523d4e20SMichel Lespinasse 			pol = mpol_dup(vma_policy(vma));
2442523d4e20SMichel Lespinasse 			if (IS_ERR(pol))
2443523d4e20SMichel Lespinasse 				goto out_free_vma;
2444523d4e20SMichel Lespinasse 			vma_set_policy(new_vma, pol);
2445523d4e20SMichel Lespinasse 			INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2446523d4e20SMichel Lespinasse 			if (anon_vma_clone(new_vma, vma))
2447523d4e20SMichel Lespinasse 				goto out_free_mempol;
2448e9714acfSKonstantin Khlebnikov 			if (new_vma->vm_file)
24491da177e4SLinus Torvalds 				get_file(new_vma->vm_file);
24501da177e4SLinus Torvalds 			if (new_vma->vm_ops && new_vma->vm_ops->open)
24511da177e4SLinus Torvalds 				new_vma->vm_ops->open(new_vma);
24521da177e4SLinus Torvalds 			vma_link(mm, new_vma, prev, rb_link, rb_parent);
245338a76013SMichel Lespinasse 			*need_rmap_locks = false;
24541da177e4SLinus Torvalds 		}
24551da177e4SLinus Torvalds 	}
24561da177e4SLinus Torvalds 	return new_vma;
24575beb4930SRik van Riel 
24585beb4930SRik van Riel  out_free_mempol:
24595beb4930SRik van Riel 	mpol_put(pol);
24605beb4930SRik van Riel  out_free_vma:
24615beb4930SRik van Riel 	kmem_cache_free(vm_area_cachep, new_vma);
24625beb4930SRik van Riel 	return NULL;
24631da177e4SLinus Torvalds }
2464119f657cSakpm@osdl.org 
2465119f657cSakpm@osdl.org /*
2466119f657cSakpm@osdl.org  * Return true if the calling process may expand its vm space by the passed
2467119f657cSakpm@osdl.org  * number of pages
2468119f657cSakpm@osdl.org  */
2469119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2470119f657cSakpm@osdl.org {
2471119f657cSakpm@osdl.org 	unsigned long cur = mm->total_vm;	/* pages */
2472119f657cSakpm@osdl.org 	unsigned long lim;
2473119f657cSakpm@osdl.org 
247459e99e5bSJiri Slaby 	lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
2475119f657cSakpm@osdl.org 
2476119f657cSakpm@osdl.org 	if (cur + npages > lim)
2477119f657cSakpm@osdl.org 		return 0;
2478119f657cSakpm@osdl.org 	return 1;
2479119f657cSakpm@osdl.org }
2480fa5dc22fSRoland McGrath 
2481fa5dc22fSRoland McGrath 
2482b1d0e4f5SNick Piggin static int special_mapping_fault(struct vm_area_struct *vma,
2483b1d0e4f5SNick Piggin 				struct vm_fault *vmf)
2484fa5dc22fSRoland McGrath {
2485b1d0e4f5SNick Piggin 	pgoff_t pgoff;
2486fa5dc22fSRoland McGrath 	struct page **pages;
2487fa5dc22fSRoland McGrath 
2488b1d0e4f5SNick Piggin 	/*
2489b1d0e4f5SNick Piggin 	 * special mappings have no vm_file, and in that case, the mm
2490b1d0e4f5SNick Piggin 	 * uses vm_pgoff internally. So we have to subtract it from here.
2491b1d0e4f5SNick Piggin 	 * We are allowed to do this because we are the mm; do not copy
2492b1d0e4f5SNick Piggin 	 * this code into drivers!
2493b1d0e4f5SNick Piggin 	 */
2494b1d0e4f5SNick Piggin 	pgoff = vmf->pgoff - vma->vm_pgoff;
2495fa5dc22fSRoland McGrath 
2496b1d0e4f5SNick Piggin 	for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2497b1d0e4f5SNick Piggin 		pgoff--;
2498fa5dc22fSRoland McGrath 
2499fa5dc22fSRoland McGrath 	if (*pages) {
2500fa5dc22fSRoland McGrath 		struct page *page = *pages;
2501fa5dc22fSRoland McGrath 		get_page(page);
2502b1d0e4f5SNick Piggin 		vmf->page = page;
2503b1d0e4f5SNick Piggin 		return 0;
2504fa5dc22fSRoland McGrath 	}
2505fa5dc22fSRoland McGrath 
2506b1d0e4f5SNick Piggin 	return VM_FAULT_SIGBUS;
2507fa5dc22fSRoland McGrath }
2508fa5dc22fSRoland McGrath 
2509fa5dc22fSRoland McGrath /*
2510fa5dc22fSRoland McGrath  * Having a close hook prevents vma merging regardless of flags.
2511fa5dc22fSRoland McGrath  */
2512fa5dc22fSRoland McGrath static void special_mapping_close(struct vm_area_struct *vma)
2513fa5dc22fSRoland McGrath {
2514fa5dc22fSRoland McGrath }
2515fa5dc22fSRoland McGrath 
2516f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct special_mapping_vmops = {
2517fa5dc22fSRoland McGrath 	.close = special_mapping_close,
2518b1d0e4f5SNick Piggin 	.fault = special_mapping_fault,
2519fa5dc22fSRoland McGrath };
2520fa5dc22fSRoland McGrath 
2521fa5dc22fSRoland McGrath /*
2522fa5dc22fSRoland McGrath  * Called with mm->mmap_sem held for writing.
2523fa5dc22fSRoland McGrath  * Insert a new vma covering the given region, with the given flags.
2524fa5dc22fSRoland McGrath  * Its pages are supplied by the given array of struct page *.
2525fa5dc22fSRoland McGrath  * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2526fa5dc22fSRoland McGrath  * The region past the last page supplied will always produce SIGBUS.
2527fa5dc22fSRoland McGrath  * The array pointer and the pages it points to are assumed to stay alive
2528fa5dc22fSRoland McGrath  * for as long as this mapping might exist.
2529fa5dc22fSRoland McGrath  */
2530fa5dc22fSRoland McGrath int install_special_mapping(struct mm_struct *mm,
2531fa5dc22fSRoland McGrath 			    unsigned long addr, unsigned long len,
2532fa5dc22fSRoland McGrath 			    unsigned long vm_flags, struct page **pages)
2533fa5dc22fSRoland McGrath {
2534462e635eSTavis Ormandy 	int ret;
2535fa5dc22fSRoland McGrath 	struct vm_area_struct *vma;
2536fa5dc22fSRoland McGrath 
2537fa5dc22fSRoland McGrath 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2538fa5dc22fSRoland McGrath 	if (unlikely(vma == NULL))
2539fa5dc22fSRoland McGrath 		return -ENOMEM;
2540fa5dc22fSRoland McGrath 
25415beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
2542fa5dc22fSRoland McGrath 	vma->vm_mm = mm;
2543fa5dc22fSRoland McGrath 	vma->vm_start = addr;
2544fa5dc22fSRoland McGrath 	vma->vm_end = addr + len;
2545fa5dc22fSRoland McGrath 
25462f98735cSNick Piggin 	vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
25473ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2548fa5dc22fSRoland McGrath 
2549fa5dc22fSRoland McGrath 	vma->vm_ops = &special_mapping_vmops;
2550fa5dc22fSRoland McGrath 	vma->vm_private_data = pages;
2551fa5dc22fSRoland McGrath 
2552462e635eSTavis Ormandy 	ret = insert_vm_struct(mm, vma);
2553462e635eSTavis Ormandy 	if (ret)
2554462e635eSTavis Ormandy 		goto out;
2555fa5dc22fSRoland McGrath 
2556fa5dc22fSRoland McGrath 	mm->total_vm += len >> PAGE_SHIFT;
2557fa5dc22fSRoland McGrath 
2558cdd6c482SIngo Molnar 	perf_event_mmap(vma);
2559089dd79dSPeter Zijlstra 
2560fa5dc22fSRoland McGrath 	return 0;
2561462e635eSTavis Ormandy 
2562462e635eSTavis Ormandy out:
2563462e635eSTavis Ormandy 	kmem_cache_free(vm_area_cachep, vma);
2564462e635eSTavis Ormandy 	return ret;
2565fa5dc22fSRoland McGrath }
25667906d00cSAndrea Arcangeli 
25677906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex);
25687906d00cSAndrea Arcangeli 
2569454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
25707906d00cSAndrea Arcangeli {
2571bf181b9fSMichel Lespinasse 	if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
25727906d00cSAndrea Arcangeli 		/*
25737906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change from under us
25747906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
25757906d00cSAndrea Arcangeli 		 */
25762b575eb6SPeter Zijlstra 		mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
25777906d00cSAndrea Arcangeli 		/*
25787906d00cSAndrea Arcangeli 		 * We can safely modify head.next after taking the
25792b575eb6SPeter Zijlstra 		 * anon_vma->root->mutex. If some other vma in this mm shares
25807906d00cSAndrea Arcangeli 		 * the same anon_vma we won't take it again.
25817906d00cSAndrea Arcangeli 		 *
25827906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
25837906d00cSAndrea Arcangeli 		 * can't change from under us thanks to the
25842b575eb6SPeter Zijlstra 		 * anon_vma->root->mutex.
25857906d00cSAndrea Arcangeli 		 */
25867906d00cSAndrea Arcangeli 		if (__test_and_set_bit(0, (unsigned long *)
2587bf181b9fSMichel Lespinasse 				       &anon_vma->root->rb_root.rb_node))
25887906d00cSAndrea Arcangeli 			BUG();
25897906d00cSAndrea Arcangeli 	}
25907906d00cSAndrea Arcangeli }
25917906d00cSAndrea Arcangeli 
2592454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
25937906d00cSAndrea Arcangeli {
25947906d00cSAndrea Arcangeli 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
25957906d00cSAndrea Arcangeli 		/*
25967906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change from under us because
25977906d00cSAndrea Arcangeli 		 * we hold the mm_all_locks_mutex.
25987906d00cSAndrea Arcangeli 		 *
25997906d00cSAndrea Arcangeli 		 * Operations on ->flags have to be atomic because
26007906d00cSAndrea Arcangeli 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
26017906d00cSAndrea Arcangeli 		 * mm_all_locks_mutex, there may be other cpus
26027906d00cSAndrea Arcangeli 		 * changing other bitflags in parallel to us.
26037906d00cSAndrea Arcangeli 		 */
26047906d00cSAndrea Arcangeli 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
26057906d00cSAndrea Arcangeli 			BUG();
26063d48ae45SPeter Zijlstra 		mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
26077906d00cSAndrea Arcangeli 	}
26087906d00cSAndrea Arcangeli }
26097906d00cSAndrea Arcangeli 
26107906d00cSAndrea Arcangeli /*
26117906d00cSAndrea Arcangeli  * This operation locks against the VM for all pte/vma/mm related
26127906d00cSAndrea Arcangeli  * operations that could ever happen on a certain mm. This includes
26137906d00cSAndrea Arcangeli  * vmtruncate, try_to_unmap, and all page faults.
26147906d00cSAndrea Arcangeli  *
26157906d00cSAndrea Arcangeli  * The caller must take the mmap_sem in write mode before calling
26167906d00cSAndrea Arcangeli  * mm_take_all_locks(). The caller isn't allowed to release the
26177906d00cSAndrea Arcangeli  * mmap_sem until mm_drop_all_locks() returns.
26187906d00cSAndrea Arcangeli  *
26197906d00cSAndrea Arcangeli  * mmap_sem in write mode is required in order to block all operations
26207906d00cSAndrea Arcangeli  * that could modify pagetables and free pages without need of
26217906d00cSAndrea Arcangeli  * altering the vma layout (for example populate_range() with
26227906d00cSAndrea Arcangeli  * nonlinear vmas). It's also needed in write mode to avoid new
26237906d00cSAndrea Arcangeli  * anon_vmas to be associated with existing vmas.
26247906d00cSAndrea Arcangeli  *
26257906d00cSAndrea Arcangeli  * A single task can't take more than one mm_take_all_locks() in a row
26267906d00cSAndrea Arcangeli  * or it would deadlock.
26277906d00cSAndrea Arcangeli  *
2628bf181b9fSMichel Lespinasse  * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
26297906d00cSAndrea Arcangeli  * mapping->flags avoid to take the same lock twice, if more than one
26307906d00cSAndrea Arcangeli  * vma in this mm is backed by the same anon_vma or address_space.
26317906d00cSAndrea Arcangeli  *
26327906d00cSAndrea Arcangeli  * We can take all the locks in random order because the VM code
26332b575eb6SPeter Zijlstra  * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
26347906d00cSAndrea Arcangeli  * takes more than one of them in a row. Secondly we're protected
26357906d00cSAndrea Arcangeli  * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
26367906d00cSAndrea Arcangeli  *
26377906d00cSAndrea Arcangeli  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
26387906d00cSAndrea Arcangeli  * that may have to take thousand of locks.
26397906d00cSAndrea Arcangeli  *
26407906d00cSAndrea Arcangeli  * mm_take_all_locks() can fail if it's interrupted by signals.
26417906d00cSAndrea Arcangeli  */
26427906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm)
26437906d00cSAndrea Arcangeli {
26447906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
26455beb4930SRik van Riel 	struct anon_vma_chain *avc;
26467906d00cSAndrea Arcangeli 
26477906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
26487906d00cSAndrea Arcangeli 
26497906d00cSAndrea Arcangeli 	mutex_lock(&mm_all_locks_mutex);
26507906d00cSAndrea Arcangeli 
26517906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
26527906d00cSAndrea Arcangeli 		if (signal_pending(current))
26537906d00cSAndrea Arcangeli 			goto out_unlock;
26547906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
2655454ed842SPeter Zijlstra 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
26567906d00cSAndrea Arcangeli 	}
26577cd5a02fSPeter Zijlstra 
26587cd5a02fSPeter Zijlstra 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
26597cd5a02fSPeter Zijlstra 		if (signal_pending(current))
26607cd5a02fSPeter Zijlstra 			goto out_unlock;
26617cd5a02fSPeter Zijlstra 		if (vma->anon_vma)
26625beb4930SRik van Riel 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
26635beb4930SRik van Riel 				vm_lock_anon_vma(mm, avc->anon_vma);
26647cd5a02fSPeter Zijlstra 	}
26657cd5a02fSPeter Zijlstra 
2666584cff54SKautuk Consul 	return 0;
26677906d00cSAndrea Arcangeli 
26687906d00cSAndrea Arcangeli out_unlock:
26697906d00cSAndrea Arcangeli 	mm_drop_all_locks(mm);
2670584cff54SKautuk Consul 	return -EINTR;
26717906d00cSAndrea Arcangeli }
26727906d00cSAndrea Arcangeli 
26737906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
26747906d00cSAndrea Arcangeli {
2675bf181b9fSMichel Lespinasse 	if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
26767906d00cSAndrea Arcangeli 		/*
26777906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change to 0 from under
26787906d00cSAndrea Arcangeli 		 * us because we hold the mm_all_locks_mutex.
26797906d00cSAndrea Arcangeli 		 *
26807906d00cSAndrea Arcangeli 		 * We must however clear the bitflag before unlocking
2681bf181b9fSMichel Lespinasse 		 * the vma so the users using the anon_vma->rb_root will
26827906d00cSAndrea Arcangeli 		 * never see our bitflag.
26837906d00cSAndrea Arcangeli 		 *
26847906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
26857906d00cSAndrea Arcangeli 		 * can't change from under us until we release the
26862b575eb6SPeter Zijlstra 		 * anon_vma->root->mutex.
26877906d00cSAndrea Arcangeli 		 */
26887906d00cSAndrea Arcangeli 		if (!__test_and_clear_bit(0, (unsigned long *)
2689bf181b9fSMichel Lespinasse 					  &anon_vma->root->rb_root.rb_node))
26907906d00cSAndrea Arcangeli 			BUG();
2691cba48b98SRik van Riel 		anon_vma_unlock(anon_vma);
26927906d00cSAndrea Arcangeli 	}
26937906d00cSAndrea Arcangeli }
26947906d00cSAndrea Arcangeli 
26957906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping)
26967906d00cSAndrea Arcangeli {
26977906d00cSAndrea Arcangeli 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
26987906d00cSAndrea Arcangeli 		/*
26997906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
27007906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
27017906d00cSAndrea Arcangeli 		 */
27023d48ae45SPeter Zijlstra 		mutex_unlock(&mapping->i_mmap_mutex);
27037906d00cSAndrea Arcangeli 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
27047906d00cSAndrea Arcangeli 					&mapping->flags))
27057906d00cSAndrea Arcangeli 			BUG();
27067906d00cSAndrea Arcangeli 	}
27077906d00cSAndrea Arcangeli }
27087906d00cSAndrea Arcangeli 
27097906d00cSAndrea Arcangeli /*
27107906d00cSAndrea Arcangeli  * The mmap_sem cannot be released by the caller until
27117906d00cSAndrea Arcangeli  * mm_drop_all_locks() returns.
27127906d00cSAndrea Arcangeli  */
27137906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm)
27147906d00cSAndrea Arcangeli {
27157906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
27165beb4930SRik van Riel 	struct anon_vma_chain *avc;
27177906d00cSAndrea Arcangeli 
27187906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
27197906d00cSAndrea Arcangeli 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
27207906d00cSAndrea Arcangeli 
27217906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
27227906d00cSAndrea Arcangeli 		if (vma->anon_vma)
27235beb4930SRik van Riel 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
27245beb4930SRik van Riel 				vm_unlock_anon_vma(avc->anon_vma);
27257906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
27267906d00cSAndrea Arcangeli 			vm_unlock_mapping(vma->vm_file->f_mapping);
27277906d00cSAndrea Arcangeli 	}
27287906d00cSAndrea Arcangeli 
27297906d00cSAndrea Arcangeli 	mutex_unlock(&mm_all_locks_mutex);
27307906d00cSAndrea Arcangeli }
27318feae131SDavid Howells 
27328feae131SDavid Howells /*
27338feae131SDavid Howells  * initialise the VMA slab
27348feae131SDavid Howells  */
27358feae131SDavid Howells void __init mmap_init(void)
27368feae131SDavid Howells {
273700a62ce9SKOSAKI Motohiro 	int ret;
273800a62ce9SKOSAKI Motohiro 
273900a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
274000a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
27418feae131SDavid Howells }
2742