xref: /linux/mm/mmap.c (revision 965f55dea0e331152fa53941a51e4e16f9f06fae)
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>
251da177e4SLinus Torvalds #include <linux/module.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>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds #include <asm/cacheflush.h>
361da177e4SLinus Torvalds #include <asm/tlb.h>
37d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h>
381da177e4SLinus Torvalds 
3942b77728SJan Beulich #include "internal.h"
4042b77728SJan Beulich 
413a459756SKirill Korotaev #ifndef arch_mmap_check
423a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags)	(0)
433a459756SKirill Korotaev #endif
443a459756SKirill Korotaev 
4508e7d9b5SMartin Schwidefsky #ifndef arch_rebalance_pgtables
4608e7d9b5SMartin Schwidefsky #define arch_rebalance_pgtables(addr, len)		(addr)
4708e7d9b5SMartin Schwidefsky #endif
4808e7d9b5SMartin Schwidefsky 
49e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm,
50e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
51e0da382cSHugh Dickins 		unsigned long start, unsigned long end);
52e0da382cSHugh Dickins 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * WARNING: the debugging will use recursive algorithms so never enable this
551da177e4SLinus Torvalds  * unless you know what you are doing.
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds #undef DEBUG_MM_RB
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation.
601da177e4SLinus Torvalds  * this is due to the limited x86 page protection hardware.  The expected
611da177e4SLinus Torvalds  * behavior is in parens:
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  * map_type	prot
641da177e4SLinus Torvalds  *		PROT_NONE	PROT_READ	PROT_WRITE	PROT_EXEC
651da177e4SLinus Torvalds  * MAP_SHARED	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
661da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (yes) yes	w: (no) no
671da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
681da177e4SLinus Torvalds  *
691da177e4SLinus Torvalds  * MAP_PRIVATE	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
701da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (copy) copy	w: (no) no
711da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
721da177e4SLinus Torvalds  *
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds pgprot_t protection_map[16] = {
751da177e4SLinus Torvalds 	__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
761da177e4SLinus Torvalds 	__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
79804af2cfSHugh Dickins pgprot_t vm_get_page_prot(unsigned long vm_flags)
80804af2cfSHugh Dickins {
81b845f313SDave Kleikamp 	return __pgprot(pgprot_val(protection_map[vm_flags &
82b845f313SDave Kleikamp 				(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
83b845f313SDave Kleikamp 			pgprot_val(arch_vm_get_page_prot(vm_flags)));
84804af2cfSHugh Dickins }
85804af2cfSHugh Dickins EXPORT_SYMBOL(vm_get_page_prot);
86804af2cfSHugh Dickins 
8734679d7eSShaohua Li int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;  /* heuristic overcommit */
8834679d7eSShaohua Li int sysctl_overcommit_ratio __read_mostly = 50;	/* default is 50% */
89c3d8c141SChristoph Lameter int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
9034679d7eSShaohua Li /*
9134679d7eSShaohua Li  * Make sure vm_committed_as in one cacheline and not cacheline shared with
9234679d7eSShaohua Li  * other variables. It can be updated by several CPUs frequently.
9334679d7eSShaohua Li  */
9434679d7eSShaohua Li struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /*
971da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
981da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
991da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
1021da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1051da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1081da177e4SLinus Torvalds  *
1091da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
1101da177e4SLinus Torvalds  * wish to use this logic.
1111da177e4SLinus Torvalds  */
11234b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	unsigned long free, allowed;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	vm_acct_memory(pages);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	/*
1191da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
1201da177e4SLinus Torvalds 	 */
1211da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1221da177e4SLinus Torvalds 		return 0;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1251da177e4SLinus Torvalds 		unsigned long n;
1261da177e4SLinus Torvalds 
127347ce434SChristoph Lameter 		free = global_page_state(NR_FILE_PAGES);
1281da177e4SLinus Torvalds 		free += nr_swap_pages;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 		/*
1311da177e4SLinus Torvalds 		 * Any slabs which are created with the
1321da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1331da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
1341da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
1351da177e4SLinus Torvalds 		 */
136972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 		/*
1391da177e4SLinus Torvalds 		 * Leave the last 3% for root
1401da177e4SLinus Torvalds 		 */
1411da177e4SLinus Torvalds 		if (!cap_sys_admin)
1421da177e4SLinus Torvalds 			free -= free / 32;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 		if (free > pages)
1451da177e4SLinus Torvalds 			return 0;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 		/*
1481da177e4SLinus Torvalds 		 * nr_free_pages() is very expensive on large systems,
1491da177e4SLinus Torvalds 		 * only call if we're about to fail.
1501da177e4SLinus Torvalds 		 */
1511da177e4SLinus Torvalds 		n = nr_free_pages();
1526d9f7839SHideo AOKI 
1536d9f7839SHideo AOKI 		/*
1546d9f7839SHideo AOKI 		 * Leave reserved pages. The pages are not for anonymous pages.
1556d9f7839SHideo AOKI 		 */
1566d9f7839SHideo AOKI 		if (n <= totalreserve_pages)
1576d9f7839SHideo AOKI 			goto error;
1586d9f7839SHideo AOKI 		else
1596d9f7839SHideo AOKI 			n -= totalreserve_pages;
1606d9f7839SHideo AOKI 
1616d9f7839SHideo AOKI 		/*
1626d9f7839SHideo AOKI 		 * Leave the last 3% for root
1636d9f7839SHideo AOKI 		 */
1641da177e4SLinus Torvalds 		if (!cap_sys_admin)
1651da177e4SLinus Torvalds 			n -= n / 32;
1661da177e4SLinus Torvalds 		free += n;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 		if (free > pages)
1691da177e4SLinus Torvalds 			return 0;
1706d9f7839SHideo AOKI 
1716d9f7839SHideo AOKI 		goto error;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	allowed = (totalram_pages - hugetlb_total_pages())
1751da177e4SLinus Torvalds 	       	* sysctl_overcommit_ratio / 100;
1761da177e4SLinus Torvalds 	/*
1771da177e4SLinus Torvalds 	 * Leave the last 3% for root
1781da177e4SLinus Torvalds 	 */
1791da177e4SLinus Torvalds 	if (!cap_sys_admin)
1801da177e4SLinus Torvalds 		allowed -= allowed / 32;
1811da177e4SLinus Torvalds 	allowed += total_swap_pages;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
1841da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
185731572d3SAlan Cox 	if (mm)
18634b4e4aaSAlan Cox 		allowed -= mm->total_vm / 32;
1871da177e4SLinus Torvalds 
18800a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1891da177e4SLinus Torvalds 		return 0;
1906d9f7839SHideo AOKI error:
1911da177e4SLinus Torvalds 	vm_unacct_memory(pages);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	return -ENOMEM;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  * Requires inode->i_mapping->i_mmap_lock
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma,
2001da177e4SLinus Torvalds 		struct file *file, struct address_space *mapping)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	if (vma->vm_flags & VM_DENYWRITE)
203d3ac7f89SJosef "Jeff" Sipek 		atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
2041da177e4SLinus Torvalds 	if (vma->vm_flags & VM_SHARED)
2051da177e4SLinus Torvalds 		mapping->i_mmap_writable--;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	flush_dcache_mmap_lock(mapping);
2081da177e4SLinus Torvalds 	if (unlikely(vma->vm_flags & VM_NONLINEAR))
2091da177e4SLinus Torvalds 		list_del_init(&vma->shared.vm_set.list);
2101da177e4SLinus Torvalds 	else
2111da177e4SLinus Torvalds 		vma_prio_tree_remove(vma, &mapping->i_mmap);
2121da177e4SLinus Torvalds 	flush_dcache_mmap_unlock(mapping);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
216a8fb5618SHugh Dickins  * Unlink a file-based vm structure from its prio_tree, to hide
217a8fb5618SHugh Dickins  * vma from rmap and vmtruncate before freeing its page tables.
2181da177e4SLinus Torvalds  */
219a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	if (file) {
2241da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
2251da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
2261da177e4SLinus Torvalds 		__remove_shared_vm_struct(vma, file, mapping);
2271da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
2281da177e4SLinus Torvalds 	}
229a8fb5618SHugh Dickins }
230a8fb5618SHugh Dickins 
231a8fb5618SHugh Dickins /*
232a8fb5618SHugh Dickins  * Close a vm structure and free it, returning the next.
233a8fb5618SHugh Dickins  */
234a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
235a8fb5618SHugh Dickins {
236a8fb5618SHugh Dickins 	struct vm_area_struct *next = vma->vm_next;
237a8fb5618SHugh Dickins 
238a8fb5618SHugh Dickins 	might_sleep();
2391da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
2401da177e4SLinus Torvalds 		vma->vm_ops->close(vma);
241925d1c40SMatt Helsley 	if (vma->vm_file) {
242a8fb5618SHugh Dickins 		fput(vma->vm_file);
243925d1c40SMatt Helsley 		if (vma->vm_flags & VM_EXECUTABLE)
244925d1c40SMatt Helsley 			removed_exe_file_vma(vma->vm_mm);
245925d1c40SMatt Helsley 	}
246f0be3d32SLee Schermerhorn 	mpol_put(vma_policy(vma));
2471da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
248a8fb5618SHugh Dickins 	return next;
2491da177e4SLinus Torvalds }
2501da177e4SLinus 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 
3141da177e4SLinus Torvalds #ifdef DEBUG_MM_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;
3481da177e4SLinus Torvalds 	struct vm_area_struct *tmp = mm->mmap;
3491da177e4SLinus Torvalds 	while (tmp) {
3501da177e4SLinus Torvalds 		tmp = tmp->vm_next;
3511da177e4SLinus Torvalds 		i++;
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 	if (i != mm->map_count)
3541da177e4SLinus Torvalds 		printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
3551da177e4SLinus Torvalds 	i = browse_rb(&mm->mm_rb);
3561da177e4SLinus Torvalds 	if (i != mm->map_count)
3571da177e4SLinus Torvalds 		printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
35846a350efSEric Sesterhenn 	BUG_ON(bug);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds #else
3611da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0)
3621da177e4SLinus Torvalds #endif
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds static struct vm_area_struct *
3651da177e4SLinus Torvalds find_vma_prepare(struct mm_struct *mm, unsigned long addr,
3661da177e4SLinus Torvalds 		struct vm_area_struct **pprev, struct rb_node ***rb_link,
3671da177e4SLinus Torvalds 		struct rb_node ** rb_parent)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct vm_area_struct * vma;
3701da177e4SLinus Torvalds 	struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	__rb_link = &mm->mm_rb.rb_node;
3731da177e4SLinus Torvalds 	rb_prev = __rb_parent = NULL;
3741da177e4SLinus Torvalds 	vma = NULL;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	while (*__rb_link) {
3771da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		__rb_parent = *__rb_link;
3801da177e4SLinus Torvalds 		vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		if (vma_tmp->vm_end > addr) {
3831da177e4SLinus Torvalds 			vma = vma_tmp;
3841da177e4SLinus Torvalds 			if (vma_tmp->vm_start <= addr)
385dfe195fbSBenny Halevy 				break;
3861da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_left;
3871da177e4SLinus Torvalds 		} else {
3881da177e4SLinus Torvalds 			rb_prev = __rb_parent;
3891da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_right;
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	*pprev = NULL;
3941da177e4SLinus Torvalds 	if (rb_prev)
3951da177e4SLinus Torvalds 		*pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
3961da177e4SLinus Torvalds 	*rb_link = __rb_link;
3971da177e4SLinus Torvalds 	*rb_parent = __rb_parent;
3981da177e4SLinus Torvalds 	return vma;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static inline void
4021da177e4SLinus Torvalds __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
4031da177e4SLinus Torvalds 		struct vm_area_struct *prev, struct rb_node *rb_parent)
4041da177e4SLinus Torvalds {
405297c5eeeSLinus Torvalds 	struct vm_area_struct *next;
406297c5eeeSLinus Torvalds 
407297c5eeeSLinus Torvalds 	vma->vm_prev = prev;
4081da177e4SLinus Torvalds 	if (prev) {
409297c5eeeSLinus Torvalds 		next = prev->vm_next;
4101da177e4SLinus Torvalds 		prev->vm_next = vma;
4111da177e4SLinus Torvalds 	} else {
4121da177e4SLinus Torvalds 		mm->mmap = vma;
4131da177e4SLinus Torvalds 		if (rb_parent)
414297c5eeeSLinus Torvalds 			next = rb_entry(rb_parent,
4151da177e4SLinus Torvalds 					struct vm_area_struct, vm_rb);
4161da177e4SLinus Torvalds 		else
417297c5eeeSLinus Torvalds 			next = NULL;
4181da177e4SLinus Torvalds 	}
419297c5eeeSLinus Torvalds 	vma->vm_next = next;
420297c5eeeSLinus Torvalds 	if (next)
421297c5eeeSLinus Torvalds 		next->vm_prev = vma;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
4251da177e4SLinus Torvalds 		struct rb_node **rb_link, struct rb_node *rb_parent)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	rb_link_node(&vma->vm_rb, rb_parent, rb_link);
4281da177e4SLinus Torvalds 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
431cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct file *file;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	file = vma->vm_file;
4361da177e4SLinus Torvalds 	if (file) {
4371da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 		if (vma->vm_flags & VM_DENYWRITE)
440d3ac7f89SJosef "Jeff" Sipek 			atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
4411da177e4SLinus Torvalds 		if (vma->vm_flags & VM_SHARED)
4421da177e4SLinus Torvalds 			mapping->i_mmap_writable++;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
4451da177e4SLinus Torvalds 		if (unlikely(vma->vm_flags & VM_NONLINEAR))
4461da177e4SLinus Torvalds 			vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
4471da177e4SLinus Torvalds 		else
4481da177e4SLinus Torvalds 			vma_prio_tree_insert(vma, &mapping->i_mmap);
4491da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds static void
4541da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4551da177e4SLinus Torvalds 	struct vm_area_struct *prev, struct rb_node **rb_link,
4561da177e4SLinus Torvalds 	struct rb_node *rb_parent)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	__vma_link_list(mm, vma, prev, rb_parent);
4591da177e4SLinus Torvalds 	__vma_link_rb(mm, vma, rb_link, rb_parent);
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4631da177e4SLinus Torvalds 			struct vm_area_struct *prev, struct rb_node **rb_link,
4641da177e4SLinus Torvalds 			struct rb_node *rb_parent)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	if (vma->vm_file)
4691da177e4SLinus Torvalds 		mapping = vma->vm_file->f_mapping;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (mapping) {
4721da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
4731da177e4SLinus Torvalds 		vma->vm_truncate_count = mapping->truncate_count;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4771da177e4SLinus Torvalds 	__vma_link_file(vma);
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	if (mapping)
4801da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	mm->map_count++;
4831da177e4SLinus Torvalds 	validate_mm(mm);
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds  * Helper for vma_adjust in the split_vma insert case:
4881da177e4SLinus Torvalds  * insert vm structure into list and rbtree and anon_vma,
4891da177e4SLinus Torvalds  * but it has already been inserted into prio_tree earlier.
4901da177e4SLinus Torvalds  */
49148aae425SZhenwenXu static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	struct vm_area_struct *__vma, *prev;
4941da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	__vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
49746a350efSEric Sesterhenn 	BUG_ON(__vma && __vma->vm_start < vma->vm_end);
4981da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4991da177e4SLinus Torvalds 	mm->map_count++;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static inline void
5031da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
5041da177e4SLinus Torvalds 		struct vm_area_struct *prev)
5051da177e4SLinus Torvalds {
506297c5eeeSLinus Torvalds 	struct vm_area_struct *next = vma->vm_next;
507297c5eeeSLinus Torvalds 
508297c5eeeSLinus Torvalds 	prev->vm_next = next;
509297c5eeeSLinus Torvalds 	if (next)
510297c5eeeSLinus Torvalds 		next->vm_prev = prev;
5111da177e4SLinus Torvalds 	rb_erase(&vma->vm_rb, &mm->mm_rb);
5121da177e4SLinus Torvalds 	if (mm->mmap_cache == vma)
5131da177e4SLinus Torvalds 		mm->mmap_cache = prev;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
5181da177e4SLinus Torvalds  * is already present in an i_mmap tree without adjusting the tree.
5191da177e4SLinus Torvalds  * The following helper function should be used when such adjustments
5201da177e4SLinus Torvalds  * are necessary.  The "insert" vma (if any) is to be inserted
5211da177e4SLinus Torvalds  * before we drop the necessary locks.
5221da177e4SLinus Torvalds  */
5235beb4930SRik van Riel int vma_adjust(struct vm_area_struct *vma, unsigned long start,
5241da177e4SLinus Torvalds 	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
5271da177e4SLinus Torvalds 	struct vm_area_struct *next = vma->vm_next;
5281da177e4SLinus Torvalds 	struct vm_area_struct *importer = NULL;
5291da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
5301da177e4SLinus Torvalds 	struct prio_tree_root *root = NULL;
531012f1800SRik van Riel 	struct anon_vma *anon_vma = NULL;
5321da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
5331da177e4SLinus Torvalds 	long adjust_next = 0;
5341da177e4SLinus Torvalds 	int remove_next = 0;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (next && !insert) {
537287d97acSLinus Torvalds 		struct vm_area_struct *exporter = NULL;
538287d97acSLinus Torvalds 
5391da177e4SLinus Torvalds 		if (end >= next->vm_end) {
5401da177e4SLinus Torvalds 			/*
5411da177e4SLinus Torvalds 			 * vma expands, overlapping all the next, and
5421da177e4SLinus Torvalds 			 * perhaps the one after too (mprotect case 6).
5431da177e4SLinus Torvalds 			 */
5441da177e4SLinus Torvalds again:			remove_next = 1 + (end > next->vm_end);
5451da177e4SLinus Torvalds 			end = next->vm_end;
546287d97acSLinus Torvalds 			exporter = next;
5471da177e4SLinus Torvalds 			importer = vma;
5481da177e4SLinus Torvalds 		} else if (end > next->vm_start) {
5491da177e4SLinus Torvalds 			/*
5501da177e4SLinus Torvalds 			 * vma expands, overlapping part of the next:
5511da177e4SLinus Torvalds 			 * mprotect case 5 shifting the boundary up.
5521da177e4SLinus Torvalds 			 */
5531da177e4SLinus Torvalds 			adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
554287d97acSLinus Torvalds 			exporter = next;
5551da177e4SLinus Torvalds 			importer = vma;
5561da177e4SLinus Torvalds 		} else if (end < vma->vm_end) {
5571da177e4SLinus Torvalds 			/*
5581da177e4SLinus Torvalds 			 * vma shrinks, and !insert tells it's not
5591da177e4SLinus Torvalds 			 * split_vma inserting another: so it must be
5601da177e4SLinus Torvalds 			 * mprotect case 4 shifting the boundary down.
5611da177e4SLinus Torvalds 			 */
5621da177e4SLinus Torvalds 			adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
563287d97acSLinus Torvalds 			exporter = vma;
5641da177e4SLinus Torvalds 			importer = next;
5651da177e4SLinus Torvalds 		}
5661da177e4SLinus Torvalds 
5675beb4930SRik van Riel 		/*
5685beb4930SRik van Riel 		 * Easily overlooked: when mprotect shifts the boundary,
5695beb4930SRik van Riel 		 * make sure the expanding vma has anon_vma set if the
5705beb4930SRik van Riel 		 * shrinking vma had, to cover any anon pages imported.
5715beb4930SRik van Riel 		 */
572287d97acSLinus Torvalds 		if (exporter && exporter->anon_vma && !importer->anon_vma) {
573287d97acSLinus Torvalds 			if (anon_vma_clone(importer, exporter))
5745beb4930SRik van Riel 				return -ENOMEM;
575287d97acSLinus Torvalds 			importer->anon_vma = exporter->anon_vma;
5765beb4930SRik van Riel 		}
5775beb4930SRik van Riel 	}
5785beb4930SRik van Riel 
5791da177e4SLinus Torvalds 	if (file) {
5801da177e4SLinus Torvalds 		mapping = file->f_mapping;
5811da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_NONLINEAR))
5821da177e4SLinus Torvalds 			root = &mapping->i_mmap;
5831da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
5841da177e4SLinus Torvalds 		if (importer &&
5851da177e4SLinus Torvalds 		    vma->vm_truncate_count != next->vm_truncate_count) {
5861da177e4SLinus Torvalds 			/*
5871da177e4SLinus Torvalds 			 * unmap_mapping_range might be in progress:
5881da177e4SLinus Torvalds 			 * ensure that the expanding vma is rescanned.
5891da177e4SLinus Torvalds 			 */
5901da177e4SLinus Torvalds 			importer->vm_truncate_count = 0;
5911da177e4SLinus Torvalds 		}
5921da177e4SLinus Torvalds 		if (insert) {
5931da177e4SLinus Torvalds 			insert->vm_truncate_count = vma->vm_truncate_count;
5941da177e4SLinus Torvalds 			/*
5951da177e4SLinus Torvalds 			 * Put into prio_tree now, so instantiated pages
5961da177e4SLinus Torvalds 			 * are visible to arm/parisc __flush_dcache_page
5971da177e4SLinus Torvalds 			 * throughout; but we cannot insert into address
5981da177e4SLinus Torvalds 			 * space until vma start or end is updated.
5991da177e4SLinus Torvalds 			 */
6001da177e4SLinus Torvalds 			__vma_link_file(insert);
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 
60494fcc585SAndrea Arcangeli 	vma_adjust_trans_huge(vma, start, end, adjust_next);
60594fcc585SAndrea Arcangeli 
606012f1800SRik van Riel 	/*
607012f1800SRik van Riel 	 * When changing only vma->vm_end, we don't really need anon_vma
608012f1800SRik van Riel 	 * lock. This is a fairly rare case by itself, but the anon_vma
609012f1800SRik van Riel 	 * lock may be shared between many sibling processes.  Skipping
610012f1800SRik van Riel 	 * the lock for brk adjustments makes a difference sometimes.
611012f1800SRik van Riel 	 */
6125f70b962SShaohua Li 	if (vma->anon_vma && (importer || start != vma->vm_start)) {
613012f1800SRik van Riel 		anon_vma = vma->anon_vma;
614012f1800SRik van Riel 		anon_vma_lock(anon_vma);
615012f1800SRik van Riel 	}
616012f1800SRik van Riel 
6171da177e4SLinus Torvalds 	if (root) {
6181da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
6191da177e4SLinus Torvalds 		vma_prio_tree_remove(vma, root);
6201da177e4SLinus Torvalds 		if (adjust_next)
6211da177e4SLinus Torvalds 			vma_prio_tree_remove(next, root);
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	vma->vm_start = start;
6251da177e4SLinus Torvalds 	vma->vm_end = end;
6261da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
6271da177e4SLinus Torvalds 	if (adjust_next) {
6281da177e4SLinus Torvalds 		next->vm_start += adjust_next << PAGE_SHIFT;
6291da177e4SLinus Torvalds 		next->vm_pgoff += adjust_next;
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (root) {
6331da177e4SLinus Torvalds 		if (adjust_next)
6341da177e4SLinus Torvalds 			vma_prio_tree_insert(next, root);
6351da177e4SLinus Torvalds 		vma_prio_tree_insert(vma, root);
6361da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (remove_next) {
6401da177e4SLinus Torvalds 		/*
6411da177e4SLinus Torvalds 		 * vma_merge has merged next into vma, and needs
6421da177e4SLinus Torvalds 		 * us to remove next before dropping the locks.
6431da177e4SLinus Torvalds 		 */
6441da177e4SLinus Torvalds 		__vma_unlink(mm, next, vma);
6451da177e4SLinus Torvalds 		if (file)
6461da177e4SLinus Torvalds 			__remove_shared_vm_struct(next, file, mapping);
6471da177e4SLinus Torvalds 	} else if (insert) {
6481da177e4SLinus Torvalds 		/*
6491da177e4SLinus Torvalds 		 * split_vma has split insert from vma, and needs
6501da177e4SLinus Torvalds 		 * us to insert it before dropping the locks
6511da177e4SLinus Torvalds 		 * (it may either follow vma or precede it).
6521da177e4SLinus Torvalds 		 */
6531da177e4SLinus Torvalds 		__insert_vm_struct(mm, insert);
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 
656012f1800SRik van Riel 	if (anon_vma)
657012f1800SRik van Riel 		anon_vma_unlock(anon_vma);
6581da177e4SLinus Torvalds 	if (mapping)
6591da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (remove_next) {
662925d1c40SMatt Helsley 		if (file) {
6631da177e4SLinus Torvalds 			fput(file);
664925d1c40SMatt Helsley 			if (next->vm_flags & VM_EXECUTABLE)
665925d1c40SMatt Helsley 				removed_exe_file_vma(mm);
666925d1c40SMatt Helsley 		}
6675beb4930SRik van Riel 		if (next->anon_vma)
6685beb4930SRik van Riel 			anon_vma_merge(vma, next);
6691da177e4SLinus Torvalds 		mm->map_count--;
670f0be3d32SLee Schermerhorn 		mpol_put(vma_policy(next));
6711da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, next);
6721da177e4SLinus Torvalds 		/*
6731da177e4SLinus Torvalds 		 * In mprotect's case 6 (see comments on vma_merge),
6741da177e4SLinus Torvalds 		 * we must remove another next too. It would clutter
6751da177e4SLinus Torvalds 		 * up the code too much to do both in one go.
6761da177e4SLinus Torvalds 		 */
6771da177e4SLinus Torvalds 		if (remove_next == 2) {
6781da177e4SLinus Torvalds 			next = vma->vm_next;
6791da177e4SLinus Torvalds 			goto again;
6801da177e4SLinus Torvalds 		}
6811da177e4SLinus Torvalds 	}
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	validate_mm(mm);
6845beb4930SRik van Riel 
6855beb4930SRik van Riel 	return 0;
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds /*
6891da177e4SLinus Torvalds  * If the vma has a ->close operation then the driver probably needs to release
6901da177e4SLinus Torvalds  * per-vma resources, so we don't attempt to merge those.
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma,
6931da177e4SLinus Torvalds 			struct file *file, unsigned long vm_flags)
6941da177e4SLinus Torvalds {
6958314c4f2SHugh Dickins 	/* VM_CAN_NONLINEAR may get set later by f_op->mmap() */
6968314c4f2SHugh Dickins 	if ((vma->vm_flags ^ vm_flags) & ~VM_CAN_NONLINEAR)
6971da177e4SLinus Torvalds 		return 0;
6981da177e4SLinus Torvalds 	if (vma->vm_file != file)
6991da177e4SLinus Torvalds 		return 0;
7001da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
7011da177e4SLinus Torvalds 		return 0;
7021da177e4SLinus Torvalds 	return 1;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
706*965f55deSShaohua Li 					struct anon_vma *anon_vma2,
707*965f55deSShaohua Li 					struct vm_area_struct *vma)
7081da177e4SLinus Torvalds {
709*965f55deSShaohua Li 	/*
710*965f55deSShaohua Li 	 * The list_is_singular() test is to avoid merging VMA cloned from
711*965f55deSShaohua Li 	 * parents. This can improve scalability caused by anon_vma lock.
712*965f55deSShaohua Li 	 */
713*965f55deSShaohua Li 	if ((!anon_vma1 || !anon_vma2) && (!vma ||
714*965f55deSShaohua Li 		list_is_singular(&vma->anon_vma_chain)))
715*965f55deSShaohua Li 		return 1;
716*965f55deSShaohua Li 	return anon_vma1 == anon_vma2;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds /*
7201da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
7211da177e4SLinus Torvalds  * in front of (at a lower virtual address and file offset than) the vma.
7221da177e4SLinus Torvalds  *
7231da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
7241da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
7251da177e4SLinus Torvalds  *
7261da177e4SLinus Torvalds  * We don't check here for the merged mmap wrapping around the end of pagecache
7271da177e4SLinus Torvalds  * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
7281da177e4SLinus Torvalds  * wrap, nor mmaps which cover the final page at index -1UL.
7291da177e4SLinus Torvalds  */
7301da177e4SLinus Torvalds static int
7311da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
7321da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
735*965f55deSShaohua Li 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
7361da177e4SLinus Torvalds 		if (vma->vm_pgoff == vm_pgoff)
7371da177e4SLinus Torvalds 			return 1;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	return 0;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds /*
7431da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
7441da177e4SLinus Torvalds  * beyond (at a higher virtual address and file offset than) the vma.
7451da177e4SLinus Torvalds  *
7461da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
7471da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static int
7501da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
7511da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
754*965f55deSShaohua Li 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
7551da177e4SLinus Torvalds 		pgoff_t vm_pglen;
7561da177e4SLinus Torvalds 		vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
7571da177e4SLinus Torvalds 		if (vma->vm_pgoff + vm_pglen == vm_pgoff)
7581da177e4SLinus Torvalds 			return 1;
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 	return 0;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds  * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
7651da177e4SLinus Torvalds  * whether that can be merged with its predecessor or its successor.
7661da177e4SLinus Torvalds  * Or both (it neatly fills a hole).
7671da177e4SLinus Torvalds  *
7681da177e4SLinus Torvalds  * In most cases - when called for mmap, brk or mremap - [addr,end) is
7691da177e4SLinus Torvalds  * certain not to be mapped by the time vma_merge is called; but when
7701da177e4SLinus Torvalds  * called for mprotect, it is certain to be already mapped (either at
7711da177e4SLinus Torvalds  * an offset within prev, or at the start of next), and the flags of
7721da177e4SLinus Torvalds  * this area are about to be changed to vm_flags - and the no-change
7731da177e4SLinus Torvalds  * case has already been eliminated.
7741da177e4SLinus Torvalds  *
7751da177e4SLinus Torvalds  * The following mprotect cases have to be considered, where AAAA is
7761da177e4SLinus Torvalds  * the area passed down from mprotect_fixup, never extending beyond one
7771da177e4SLinus Torvalds  * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
7781da177e4SLinus Torvalds  *
7791da177e4SLinus Torvalds  *     AAAA             AAAA                AAAA          AAAA
7801da177e4SLinus Torvalds  *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
7811da177e4SLinus Torvalds  *    cannot merge    might become    might become    might become
7821da177e4SLinus Torvalds  *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
7831da177e4SLinus Torvalds  *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
7841da177e4SLinus Torvalds  *    mremap move:                                    PPPPNNNNNNNN 8
7851da177e4SLinus Torvalds  *        AAAA
7861da177e4SLinus Torvalds  *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
7871da177e4SLinus Torvalds  *    might become    case 1 below    case 2 below    case 3 below
7881da177e4SLinus Torvalds  *
7891da177e4SLinus Torvalds  * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
7901da177e4SLinus Torvalds  * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
7911da177e4SLinus Torvalds  */
7921da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm,
7931da177e4SLinus Torvalds 			struct vm_area_struct *prev, unsigned long addr,
7941da177e4SLinus Torvalds 			unsigned long end, unsigned long vm_flags,
7951da177e4SLinus Torvalds 		     	struct anon_vma *anon_vma, struct file *file,
7961da177e4SLinus Torvalds 			pgoff_t pgoff, struct mempolicy *policy)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
7991da177e4SLinus Torvalds 	struct vm_area_struct *area, *next;
8005beb4930SRik van Riel 	int err;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/*
8031da177e4SLinus Torvalds 	 * We later require that vma->vm_flags == vm_flags,
8041da177e4SLinus Torvalds 	 * so this tests vma->vm_flags & VM_SPECIAL, too.
8051da177e4SLinus Torvalds 	 */
8061da177e4SLinus Torvalds 	if (vm_flags & VM_SPECIAL)
8071da177e4SLinus Torvalds 		return NULL;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	if (prev)
8101da177e4SLinus Torvalds 		next = prev->vm_next;
8111da177e4SLinus Torvalds 	else
8121da177e4SLinus Torvalds 		next = mm->mmap;
8131da177e4SLinus Torvalds 	area = next;
8141da177e4SLinus Torvalds 	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
8151da177e4SLinus Torvalds 		next = next->vm_next;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/*
8181da177e4SLinus Torvalds 	 * Can it merge with the predecessor?
8191da177e4SLinus Torvalds 	 */
8201da177e4SLinus Torvalds 	if (prev && prev->vm_end == addr &&
8211da177e4SLinus Torvalds   			mpol_equal(vma_policy(prev), policy) &&
8221da177e4SLinus Torvalds 			can_vma_merge_after(prev, vm_flags,
8231da177e4SLinus Torvalds 						anon_vma, file, pgoff)) {
8241da177e4SLinus Torvalds 		/*
8251da177e4SLinus Torvalds 		 * OK, it can.  Can we now merge in the successor as well?
8261da177e4SLinus Torvalds 		 */
8271da177e4SLinus Torvalds 		if (next && end == next->vm_start &&
8281da177e4SLinus Torvalds 				mpol_equal(policy, vma_policy(next)) &&
8291da177e4SLinus Torvalds 				can_vma_merge_before(next, vm_flags,
8301da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen) &&
8311da177e4SLinus Torvalds 				is_mergeable_anon_vma(prev->anon_vma,
832*965f55deSShaohua Li 						      next->anon_vma, NULL)) {
8331da177e4SLinus Torvalds 							/* cases 1, 6 */
8345beb4930SRik van Riel 			err = vma_adjust(prev, prev->vm_start,
8351da177e4SLinus Torvalds 				next->vm_end, prev->vm_pgoff, NULL);
8361da177e4SLinus Torvalds 		} else					/* cases 2, 5, 7 */
8375beb4930SRik van Riel 			err = vma_adjust(prev, prev->vm_start,
8381da177e4SLinus Torvalds 				end, prev->vm_pgoff, NULL);
8395beb4930SRik van Riel 		if (err)
8405beb4930SRik van Riel 			return NULL;
841b15d00b6SAndrea Arcangeli 		khugepaged_enter_vma_merge(prev);
8421da177e4SLinus Torvalds 		return prev;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	/*
8461da177e4SLinus Torvalds 	 * Can this new request be merged in front of next?
8471da177e4SLinus Torvalds 	 */
8481da177e4SLinus Torvalds 	if (next && end == next->vm_start &&
8491da177e4SLinus Torvalds  			mpol_equal(policy, vma_policy(next)) &&
8501da177e4SLinus Torvalds 			can_vma_merge_before(next, vm_flags,
8511da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen)) {
8521da177e4SLinus Torvalds 		if (prev && addr < prev->vm_end)	/* case 4 */
8535beb4930SRik van Riel 			err = vma_adjust(prev, prev->vm_start,
8541da177e4SLinus Torvalds 				addr, prev->vm_pgoff, NULL);
8551da177e4SLinus Torvalds 		else					/* cases 3, 8 */
8565beb4930SRik van Riel 			err = vma_adjust(area, addr, next->vm_end,
8571da177e4SLinus Torvalds 				next->vm_pgoff - pglen, NULL);
8585beb4930SRik van Riel 		if (err)
8595beb4930SRik van Riel 			return NULL;
860b15d00b6SAndrea Arcangeli 		khugepaged_enter_vma_merge(area);
8611da177e4SLinus Torvalds 		return area;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	return NULL;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds /*
868d0e9fe17SLinus Torvalds  * Rough compatbility check to quickly see if it's even worth looking
869d0e9fe17SLinus Torvalds  * at sharing an anon_vma.
870d0e9fe17SLinus Torvalds  *
871d0e9fe17SLinus Torvalds  * They need to have the same vm_file, and the flags can only differ
872d0e9fe17SLinus Torvalds  * in things that mprotect may change.
873d0e9fe17SLinus Torvalds  *
874d0e9fe17SLinus Torvalds  * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
875d0e9fe17SLinus Torvalds  * we can merge the two vma's. For example, we refuse to merge a vma if
876d0e9fe17SLinus Torvalds  * there is a vm_ops->close() function, because that indicates that the
877d0e9fe17SLinus Torvalds  * driver is doing some kind of reference counting. But that doesn't
878d0e9fe17SLinus Torvalds  * really matter for the anon_vma sharing case.
879d0e9fe17SLinus Torvalds  */
880d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
881d0e9fe17SLinus Torvalds {
882d0e9fe17SLinus Torvalds 	return a->vm_end == b->vm_start &&
883d0e9fe17SLinus Torvalds 		mpol_equal(vma_policy(a), vma_policy(b)) &&
884d0e9fe17SLinus Torvalds 		a->vm_file == b->vm_file &&
885d0e9fe17SLinus Torvalds 		!((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
886d0e9fe17SLinus Torvalds 		b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
887d0e9fe17SLinus Torvalds }
888d0e9fe17SLinus Torvalds 
889d0e9fe17SLinus Torvalds /*
890d0e9fe17SLinus Torvalds  * Do some basic sanity checking to see if we can re-use the anon_vma
891d0e9fe17SLinus Torvalds  * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
892d0e9fe17SLinus Torvalds  * the same as 'old', the other will be the new one that is trying
893d0e9fe17SLinus Torvalds  * to share the anon_vma.
894d0e9fe17SLinus Torvalds  *
895d0e9fe17SLinus Torvalds  * NOTE! This runs with mm_sem held for reading, so it is possible that
896d0e9fe17SLinus Torvalds  * the anon_vma of 'old' is concurrently in the process of being set up
897d0e9fe17SLinus Torvalds  * by another page fault trying to merge _that_. But that's ok: if it
898d0e9fe17SLinus Torvalds  * is being set up, that automatically means that it will be a singleton
899d0e9fe17SLinus Torvalds  * acceptable for merging, so we can do all of this optimistically. But
900d0e9fe17SLinus Torvalds  * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
901d0e9fe17SLinus Torvalds  *
902d0e9fe17SLinus Torvalds  * IOW: that the "list_is_singular()" test on the anon_vma_chain only
903d0e9fe17SLinus Torvalds  * matters for the 'stable anon_vma' case (ie the thing we want to avoid
904d0e9fe17SLinus Torvalds  * is to return an anon_vma that is "complex" due to having gone through
905d0e9fe17SLinus Torvalds  * a fork).
906d0e9fe17SLinus Torvalds  *
907d0e9fe17SLinus Torvalds  * We also make sure that the two vma's are compatible (adjacent,
908d0e9fe17SLinus Torvalds  * and with the same memory policies). That's all stable, even with just
909d0e9fe17SLinus Torvalds  * a read lock on the mm_sem.
910d0e9fe17SLinus Torvalds  */
911d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
912d0e9fe17SLinus Torvalds {
913d0e9fe17SLinus Torvalds 	if (anon_vma_compatible(a, b)) {
914d0e9fe17SLinus Torvalds 		struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
915d0e9fe17SLinus Torvalds 
916d0e9fe17SLinus Torvalds 		if (anon_vma && list_is_singular(&old->anon_vma_chain))
917d0e9fe17SLinus Torvalds 			return anon_vma;
918d0e9fe17SLinus Torvalds 	}
919d0e9fe17SLinus Torvalds 	return NULL;
920d0e9fe17SLinus Torvalds }
921d0e9fe17SLinus Torvalds 
922d0e9fe17SLinus Torvalds /*
9231da177e4SLinus Torvalds  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
9241da177e4SLinus Torvalds  * neighbouring vmas for a suitable anon_vma, before it goes off
9251da177e4SLinus Torvalds  * to allocate a new anon_vma.  It checks because a repetitive
9261da177e4SLinus Torvalds  * sequence of mprotects and faults may otherwise lead to distinct
9271da177e4SLinus Torvalds  * anon_vmas being allocated, preventing vma merge in subsequent
9281da177e4SLinus Torvalds  * mprotect.
9291da177e4SLinus Torvalds  */
9301da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
9311da177e4SLinus Torvalds {
932d0e9fe17SLinus Torvalds 	struct anon_vma *anon_vma;
9331da177e4SLinus Torvalds 	struct vm_area_struct *near;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	near = vma->vm_next;
9361da177e4SLinus Torvalds 	if (!near)
9371da177e4SLinus Torvalds 		goto try_prev;
9381da177e4SLinus Torvalds 
939d0e9fe17SLinus Torvalds 	anon_vma = reusable_anon_vma(near, vma, near);
940d0e9fe17SLinus Torvalds 	if (anon_vma)
941d0e9fe17SLinus Torvalds 		return anon_vma;
9421da177e4SLinus Torvalds try_prev:
9431da177e4SLinus Torvalds 	/*
9441da177e4SLinus Torvalds 	 * It is potentially slow to have to call find_vma_prev here.
9451da177e4SLinus Torvalds 	 * But it's only on the first write fault on the vma, not
9461da177e4SLinus Torvalds 	 * every time, and we could devise a way to avoid it later
9471da177e4SLinus Torvalds 	 * (e.g. stash info in next's anon_vma_node when assigning
9481da177e4SLinus Torvalds 	 * an anon_vma, or when trying vma_merge).  Another time.
9491da177e4SLinus Torvalds 	 */
95046a350efSEric Sesterhenn 	BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
9511da177e4SLinus Torvalds 	if (!near)
9521da177e4SLinus Torvalds 		goto none;
9531da177e4SLinus Torvalds 
954d0e9fe17SLinus Torvalds 	anon_vma = reusable_anon_vma(near, near, vma);
955d0e9fe17SLinus Torvalds 	if (anon_vma)
956d0e9fe17SLinus Torvalds 		return anon_vma;
9571da177e4SLinus Torvalds none:
9581da177e4SLinus Torvalds 	/*
9591da177e4SLinus Torvalds 	 * There's no absolute need to look only at touching neighbours:
9601da177e4SLinus Torvalds 	 * we could search further afield for "compatible" anon_vmas.
9611da177e4SLinus Torvalds 	 * But it would probably just be a waste of time searching,
9621da177e4SLinus Torvalds 	 * or lead to too many vmas hanging off the same anon_vma.
9631da177e4SLinus Torvalds 	 * We're trying to allow mprotect remerging later on,
9641da177e4SLinus Torvalds 	 * not trying to minimize memory used for anon_vmas.
9651da177e4SLinus Torvalds 	 */
9661da177e4SLinus Torvalds 	return NULL;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
970ab50b8edSHugh Dickins void vm_stat_account(struct mm_struct *mm, unsigned long flags,
9711da177e4SLinus Torvalds 						struct file *file, long pages)
9721da177e4SLinus Torvalds {
9731da177e4SLinus Torvalds 	const unsigned long stack_flags
9741da177e4SLinus Torvalds 		= VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	if (file) {
9771da177e4SLinus Torvalds 		mm->shared_vm += pages;
9781da177e4SLinus Torvalds 		if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
9791da177e4SLinus Torvalds 			mm->exec_vm += pages;
9801da177e4SLinus Torvalds 	} else if (flags & stack_flags)
9811da177e4SLinus Torvalds 		mm->stack_vm += pages;
9821da177e4SLinus Torvalds 	if (flags & (VM_RESERVED|VM_IO))
9831da177e4SLinus Torvalds 		mm->reserved_vm += pages;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds /*
98827f5de79SJianjun Kong  * The caller must hold down_write(&current->mm->mmap_sem).
9891da177e4SLinus Torvalds  */
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
9921da177e4SLinus Torvalds 			unsigned long len, unsigned long prot,
9931da177e4SLinus Torvalds 			unsigned long flags, unsigned long pgoff)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
9961da177e4SLinus Torvalds 	struct inode *inode;
9971da177e4SLinus Torvalds 	unsigned int vm_flags;
9981da177e4SLinus Torvalds 	int error;
9990165ab44SMiklos Szeredi 	unsigned long reqprot = prot;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	/*
10021da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC?
10031da177e4SLinus Torvalds 	 *
10041da177e4SLinus Torvalds 	 * (the exception is when the underlying filesystem is noexec
10051da177e4SLinus Torvalds 	 *  mounted, in which case we dont add PROT_EXEC.)
10061da177e4SLinus Torvalds 	 */
10071da177e4SLinus Torvalds 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
1008d3ac7f89SJosef "Jeff" Sipek 		if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
10091da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	if (!len)
10121da177e4SLinus Torvalds 		return -EINVAL;
10131da177e4SLinus Torvalds 
10147cd94146SEric Paris 	if (!(flags & MAP_FIXED))
10157cd94146SEric Paris 		addr = round_hint_to_min(addr);
10167cd94146SEric Paris 
10171da177e4SLinus Torvalds 	/* Careful about overflows.. */
10181da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
10199206de95SAl Viro 	if (!len)
10201da177e4SLinus Torvalds 		return -ENOMEM;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	/* offset overflow? */
10231da177e4SLinus Torvalds 	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
10241da177e4SLinus Torvalds                return -EOVERFLOW;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/* Too many mappings? */
10271da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
10281da177e4SLinus Torvalds 		return -ENOMEM;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	/* Obtain the address to map to. we verify (or select) it and ensure
10311da177e4SLinus Torvalds 	 * that it represents a valid section of the address space.
10321da177e4SLinus Torvalds 	 */
10331da177e4SLinus Torvalds 	addr = get_unmapped_area(file, addr, len, pgoff, flags);
10341da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
10351da177e4SLinus Torvalds 		return addr;
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	/* Do simple checking here so the lower-level routines won't have
10381da177e4SLinus Torvalds 	 * to. we assume access permissions have been handled by the open
10391da177e4SLinus Torvalds 	 * of the memory object, so we don't do any here.
10401da177e4SLinus Torvalds 	 */
10411da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
10421da177e4SLinus Torvalds 			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10431da177e4SLinus Torvalds 
1044cdf7b341SHuang Shijie 	if (flags & MAP_LOCKED)
10451da177e4SLinus Torvalds 		if (!can_do_mlock())
10461da177e4SLinus Torvalds 			return -EPERM;
1047ba470de4SRik van Riel 
10481da177e4SLinus Torvalds 	/* mlock MCL_FUTURE? */
10491da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
10501da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
105193ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
105293ea1d0aSChris Wright 		locked += mm->locked_vm;
105359e99e5bSJiri Slaby 		lock_limit = rlimit(RLIMIT_MEMLOCK);
105493ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
10551da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
10561da177e4SLinus Torvalds 			return -EAGAIN;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 
1059d3ac7f89SJosef "Jeff" Sipek 	inode = file ? file->f_path.dentry->d_inode : NULL;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	if (file) {
10621da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
10631da177e4SLinus Torvalds 		case MAP_SHARED:
10641da177e4SLinus Torvalds 			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
10651da177e4SLinus Torvalds 				return -EACCES;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 			/*
10681da177e4SLinus Torvalds 			 * Make sure we don't allow writing to an append-only
10691da177e4SLinus Torvalds 			 * file..
10701da177e4SLinus Torvalds 			 */
10711da177e4SLinus Torvalds 			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
10721da177e4SLinus Torvalds 				return -EACCES;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 			/*
10751da177e4SLinus Torvalds 			 * Make sure there are no mandatory locks on the file.
10761da177e4SLinus Torvalds 			 */
10771da177e4SLinus Torvalds 			if (locks_verify_locked(inode))
10781da177e4SLinus Torvalds 				return -EAGAIN;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
10811da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_WRITE))
10821da177e4SLinus Torvalds 				vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 			/* fall through */
10851da177e4SLinus Torvalds 		case MAP_PRIVATE:
10861da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_READ))
10871da177e4SLinus Torvalds 				return -EACCES;
1088d3ac7f89SJosef "Jeff" Sipek 			if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
108980c5606cSLinus Torvalds 				if (vm_flags & VM_EXEC)
109080c5606cSLinus Torvalds 					return -EPERM;
109180c5606cSLinus Torvalds 				vm_flags &= ~VM_MAYEXEC;
109280c5606cSLinus Torvalds 			}
109380c5606cSLinus Torvalds 
109480c5606cSLinus Torvalds 			if (!file->f_op || !file->f_op->mmap)
109580c5606cSLinus Torvalds 				return -ENODEV;
10961da177e4SLinus Torvalds 			break;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 		default:
10991da177e4SLinus Torvalds 			return -EINVAL;
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 	} else {
11021da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
11031da177e4SLinus Torvalds 		case MAP_SHARED:
1104ce363942STejun Heo 			/*
1105ce363942STejun Heo 			 * Ignore pgoff.
1106ce363942STejun Heo 			 */
1107ce363942STejun Heo 			pgoff = 0;
11081da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
11091da177e4SLinus Torvalds 			break;
11101da177e4SLinus Torvalds 		case MAP_PRIVATE:
11111da177e4SLinus Torvalds 			/*
11121da177e4SLinus Torvalds 			 * Set pgoff according to addr for anon_vma.
11131da177e4SLinus Torvalds 			 */
11141da177e4SLinus Torvalds 			pgoff = addr >> PAGE_SHIFT;
11151da177e4SLinus Torvalds 			break;
11161da177e4SLinus Torvalds 		default:
11171da177e4SLinus Torvalds 			return -EINVAL;
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
1121ed032189SEric Paris 	error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
11221da177e4SLinus Torvalds 	if (error)
11231da177e4SLinus Torvalds 		return error;
11241da177e4SLinus Torvalds 
11255a6fe125SMel Gorman 	return mmap_region(file, addr, len, flags, vm_flags, pgoff);
11260165ab44SMiklos Szeredi }
11270165ab44SMiklos Szeredi EXPORT_SYMBOL(do_mmap_pgoff);
11280165ab44SMiklos Szeredi 
112966f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
113066f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
113166f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
113266f0dc48SHugh Dickins {
113366f0dc48SHugh Dickins 	struct file *file = NULL;
113466f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
113566f0dc48SHugh Dickins 
113666f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
1137120a795dSAl Viro 		audit_mmap_fd(fd, flags);
113866f0dc48SHugh Dickins 		if (unlikely(flags & MAP_HUGETLB))
113966f0dc48SHugh Dickins 			return -EINVAL;
114066f0dc48SHugh Dickins 		file = fget(fd);
114166f0dc48SHugh Dickins 		if (!file)
114266f0dc48SHugh Dickins 			goto out;
114366f0dc48SHugh Dickins 	} else if (flags & MAP_HUGETLB) {
114466f0dc48SHugh Dickins 		struct user_struct *user = NULL;
114566f0dc48SHugh Dickins 		/*
114666f0dc48SHugh Dickins 		 * VM_NORESERVE is used because the reservations will be
114766f0dc48SHugh Dickins 		 * taken when vm_ops->mmap() is called
114866f0dc48SHugh Dickins 		 * A dummy user value is used because we are not locking
114966f0dc48SHugh Dickins 		 * memory so no accounting is necessary
115066f0dc48SHugh Dickins 		 */
115166f0dc48SHugh Dickins 		len = ALIGN(len, huge_page_size(&default_hstate));
115266f0dc48SHugh Dickins 		file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, VM_NORESERVE,
115366f0dc48SHugh Dickins 						&user, HUGETLB_ANONHUGE_INODE);
115466f0dc48SHugh Dickins 		if (IS_ERR(file))
115566f0dc48SHugh Dickins 			return PTR_ERR(file);
115666f0dc48SHugh Dickins 	}
115766f0dc48SHugh Dickins 
115866f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
115966f0dc48SHugh Dickins 
116066f0dc48SHugh Dickins 	down_write(&current->mm->mmap_sem);
116166f0dc48SHugh Dickins 	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
116266f0dc48SHugh Dickins 	up_write(&current->mm->mmap_sem);
116366f0dc48SHugh Dickins 
116466f0dc48SHugh Dickins 	if (file)
116566f0dc48SHugh Dickins 		fput(file);
116666f0dc48SHugh Dickins out:
116766f0dc48SHugh Dickins 	return retval;
116866f0dc48SHugh Dickins }
116966f0dc48SHugh Dickins 
1170a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1171a4679373SChristoph Hellwig struct mmap_arg_struct {
1172a4679373SChristoph Hellwig 	unsigned long addr;
1173a4679373SChristoph Hellwig 	unsigned long len;
1174a4679373SChristoph Hellwig 	unsigned long prot;
1175a4679373SChristoph Hellwig 	unsigned long flags;
1176a4679373SChristoph Hellwig 	unsigned long fd;
1177a4679373SChristoph Hellwig 	unsigned long offset;
1178a4679373SChristoph Hellwig };
1179a4679373SChristoph Hellwig 
1180a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1181a4679373SChristoph Hellwig {
1182a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1183a4679373SChristoph Hellwig 
1184a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1185a4679373SChristoph Hellwig 		return -EFAULT;
1186a4679373SChristoph Hellwig 	if (a.offset & ~PAGE_MASK)
1187a4679373SChristoph Hellwig 		return -EINVAL;
1188a4679373SChristoph Hellwig 
1189a4679373SChristoph Hellwig 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1190a4679373SChristoph Hellwig 			      a.offset >> PAGE_SHIFT);
1191a4679373SChristoph Hellwig }
1192a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1193a4679373SChristoph Hellwig 
11944e950f6fSAlexey Dobriyan /*
11954e950f6fSAlexey Dobriyan  * Some shared mappigns will want the pages marked read-only
11964e950f6fSAlexey Dobriyan  * to track write events. If so, we'll downgrade vm_page_prot
11974e950f6fSAlexey Dobriyan  * to the private version (using protection_map[] without the
11984e950f6fSAlexey Dobriyan  * VM_SHARED bit).
11994e950f6fSAlexey Dobriyan  */
12004e950f6fSAlexey Dobriyan int vma_wants_writenotify(struct vm_area_struct *vma)
12014e950f6fSAlexey Dobriyan {
12024e950f6fSAlexey Dobriyan 	unsigned int vm_flags = vma->vm_flags;
12034e950f6fSAlexey Dobriyan 
12044e950f6fSAlexey Dobriyan 	/* If it was private or non-writable, the write bit is already clear */
12054e950f6fSAlexey Dobriyan 	if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
12064e950f6fSAlexey Dobriyan 		return 0;
12074e950f6fSAlexey Dobriyan 
12084e950f6fSAlexey Dobriyan 	/* The backer wishes to know when pages are first written to? */
12094e950f6fSAlexey Dobriyan 	if (vma->vm_ops && vma->vm_ops->page_mkwrite)
12104e950f6fSAlexey Dobriyan 		return 1;
12114e950f6fSAlexey Dobriyan 
12124e950f6fSAlexey Dobriyan 	/* The open routine did something to the protections already? */
12134e950f6fSAlexey Dobriyan 	if (pgprot_val(vma->vm_page_prot) !=
12143ed75eb8SColy Li 	    pgprot_val(vm_get_page_prot(vm_flags)))
12154e950f6fSAlexey Dobriyan 		return 0;
12164e950f6fSAlexey Dobriyan 
12174e950f6fSAlexey Dobriyan 	/* Specialty mapping? */
12184e950f6fSAlexey Dobriyan 	if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
12194e950f6fSAlexey Dobriyan 		return 0;
12204e950f6fSAlexey Dobriyan 
12214e950f6fSAlexey Dobriyan 	/* Can the mapping track the dirty pages? */
12224e950f6fSAlexey Dobriyan 	return vma->vm_file && vma->vm_file->f_mapping &&
12234e950f6fSAlexey Dobriyan 		mapping_cap_account_dirty(vma->vm_file->f_mapping);
12244e950f6fSAlexey Dobriyan }
12254e950f6fSAlexey Dobriyan 
1226fc8744adSLinus Torvalds /*
1227fc8744adSLinus Torvalds  * We account for memory if it's a private writeable mapping,
12285a6fe125SMel Gorman  * not hugepages and VM_NORESERVE wasn't set.
1229fc8744adSLinus Torvalds  */
12305a6fe125SMel Gorman static inline int accountable_mapping(struct file *file, unsigned int vm_flags)
1231fc8744adSLinus Torvalds {
12325a6fe125SMel Gorman 	/*
12335a6fe125SMel Gorman 	 * hugetlb has its own accounting separate from the core VM
12345a6fe125SMel Gorman 	 * VM_HUGETLB may not be set yet so we cannot check for that flag.
12355a6fe125SMel Gorman 	 */
12365a6fe125SMel Gorman 	if (file && is_file_hugepages(file))
12375a6fe125SMel Gorman 		return 0;
12385a6fe125SMel Gorman 
1239fc8744adSLinus Torvalds 	return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1240fc8744adSLinus Torvalds }
1241fc8744adSLinus Torvalds 
12420165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr,
12430165ab44SMiklos Szeredi 			  unsigned long len, unsigned long flags,
12445a6fe125SMel Gorman 			  unsigned int vm_flags, unsigned long pgoff)
12450165ab44SMiklos Szeredi {
12460165ab44SMiklos Szeredi 	struct mm_struct *mm = current->mm;
12470165ab44SMiklos Szeredi 	struct vm_area_struct *vma, *prev;
12480165ab44SMiklos Szeredi 	int correct_wcount = 0;
12490165ab44SMiklos Szeredi 	int error;
12500165ab44SMiklos Szeredi 	struct rb_node **rb_link, *rb_parent;
12510165ab44SMiklos Szeredi 	unsigned long charged = 0;
12520165ab44SMiklos Szeredi 	struct inode *inode =  file ? file->f_path.dentry->d_inode : NULL;
12530165ab44SMiklos Szeredi 
12541da177e4SLinus Torvalds 	/* Clear old maps */
12551da177e4SLinus Torvalds 	error = -ENOMEM;
12561da177e4SLinus Torvalds munmap_back:
12571da177e4SLinus Torvalds 	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
12581da177e4SLinus Torvalds 	if (vma && vma->vm_start < addr + len) {
12591da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
12601da177e4SLinus Torvalds 			return -ENOMEM;
12611da177e4SLinus Torvalds 		goto munmap_back;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	/* Check against address space limit. */
1265119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
12661da177e4SLinus Torvalds 		return -ENOMEM;
12671da177e4SLinus Torvalds 
1268fc8744adSLinus Torvalds 	/*
1269fc8744adSLinus Torvalds 	 * Set 'VM_NORESERVE' if we should not account for the
12705a6fe125SMel Gorman 	 * memory use of this mapping.
1271fc8744adSLinus Torvalds 	 */
12725a6fe125SMel Gorman 	if ((flags & MAP_NORESERVE)) {
12735a6fe125SMel Gorman 		/* We honor MAP_NORESERVE if allowed to overcommit */
12745a6fe125SMel Gorman 		if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1275fc8744adSLinus Torvalds 			vm_flags |= VM_NORESERVE;
12765a6fe125SMel Gorman 
12775a6fe125SMel Gorman 		/* hugetlb applies strict overcommit unless MAP_NORESERVE */
12785a6fe125SMel Gorman 		if (file && is_file_hugepages(file))
1279cdfd4325SAndy Whitcroft 			vm_flags |= VM_NORESERVE;
12805a6fe125SMel Gorman 	}
1281cdfd4325SAndy Whitcroft 
12821da177e4SLinus Torvalds 	/*
12831da177e4SLinus Torvalds 	 * Private writable mapping: check memory availability
12841da177e4SLinus Torvalds 	 */
12855a6fe125SMel Gorman 	if (accountable_mapping(file, vm_flags)) {
12861da177e4SLinus Torvalds 		charged = len >> PAGE_SHIFT;
12871da177e4SLinus Torvalds 		if (security_vm_enough_memory(charged))
12881da177e4SLinus Torvalds 			return -ENOMEM;
12891da177e4SLinus Torvalds 		vm_flags |= VM_ACCOUNT;
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	/*
1293de33c8dbSLinus Torvalds 	 * Can we just expand an old mapping?
12941da177e4SLinus Torvalds 	 */
1295de33c8dbSLinus Torvalds 	vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1296ba470de4SRik van Riel 	if (vma)
12971da177e4SLinus Torvalds 		goto out;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	/*
13001da177e4SLinus Torvalds 	 * Determine the object being mapped and call the appropriate
13011da177e4SLinus Torvalds 	 * specific mapper. the address has already been validated, but
13021da177e4SLinus Torvalds 	 * not unmapped, but the maps are removed from the list.
13031da177e4SLinus Torvalds 	 */
1304c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
13051da177e4SLinus Torvalds 	if (!vma) {
13061da177e4SLinus Torvalds 		error = -ENOMEM;
13071da177e4SLinus Torvalds 		goto unacct_error;
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	vma->vm_mm = mm;
13111da177e4SLinus Torvalds 	vma->vm_start = addr;
13121da177e4SLinus Torvalds 	vma->vm_end = addr + len;
13131da177e4SLinus Torvalds 	vma->vm_flags = vm_flags;
13143ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vm_flags);
13151da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
13165beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	if (file) {
13191da177e4SLinus Torvalds 		error = -EINVAL;
13201da177e4SLinus Torvalds 		if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
13211da177e4SLinus Torvalds 			goto free_vma;
13221da177e4SLinus Torvalds 		if (vm_flags & VM_DENYWRITE) {
13231da177e4SLinus Torvalds 			error = deny_write_access(file);
13241da177e4SLinus Torvalds 			if (error)
13251da177e4SLinus Torvalds 				goto free_vma;
13261da177e4SLinus Torvalds 			correct_wcount = 1;
13271da177e4SLinus Torvalds 		}
13281da177e4SLinus Torvalds 		vma->vm_file = file;
13291da177e4SLinus Torvalds 		get_file(file);
13301da177e4SLinus Torvalds 		error = file->f_op->mmap(file, vma);
13311da177e4SLinus Torvalds 		if (error)
13321da177e4SLinus Torvalds 			goto unmap_and_free_vma;
1333925d1c40SMatt Helsley 		if (vm_flags & VM_EXECUTABLE)
1334925d1c40SMatt Helsley 			added_exe_file_vma(mm);
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 		/* Can addr have changed??
13371da177e4SLinus Torvalds 		 *
13381da177e4SLinus Torvalds 		 * Answer: Yes, several device drivers can do it in their
13391da177e4SLinus Torvalds 		 *         f_op->mmap method. -DaveM
13401da177e4SLinus Torvalds 		 */
13411da177e4SLinus Torvalds 		addr = vma->vm_start;
13421da177e4SLinus Torvalds 		pgoff = vma->vm_pgoff;
13431da177e4SLinus Torvalds 		vm_flags = vma->vm_flags;
1344f8dbf0a7SHuang Shijie 	} else if (vm_flags & VM_SHARED) {
1345f8dbf0a7SHuang Shijie 		error = shmem_zero_setup(vma);
1346f8dbf0a7SHuang Shijie 		if (error)
1347f8dbf0a7SHuang Shijie 			goto free_vma;
1348f8dbf0a7SHuang Shijie 	}
13491da177e4SLinus Torvalds 
1350c9d0bf24SMagnus Damm 	if (vma_wants_writenotify(vma)) {
1351c9d0bf24SMagnus Damm 		pgprot_t pprot = vma->vm_page_prot;
1352c9d0bf24SMagnus Damm 
1353c9d0bf24SMagnus Damm 		/* Can vma->vm_page_prot have changed??
1354c9d0bf24SMagnus Damm 		 *
1355c9d0bf24SMagnus Damm 		 * Answer: Yes, drivers may have changed it in their
1356c9d0bf24SMagnus Damm 		 *         f_op->mmap method.
1357c9d0bf24SMagnus Damm 		 *
1358c9d0bf24SMagnus Damm 		 * Ensures that vmas marked as uncached stay that way.
1359c9d0bf24SMagnus Damm 		 */
13601ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
1361c9d0bf24SMagnus Damm 		if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1362c9d0bf24SMagnus Damm 			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1363c9d0bf24SMagnus Damm 	}
1364d08b3851SPeter Zijlstra 
13654d3d5b41SOleg Nesterov 	vma_link(mm, vma, prev, rb_link, rb_parent);
13664d3d5b41SOleg Nesterov 	file = vma->vm_file;
13674d3d5b41SOleg Nesterov 
13684d3d5b41SOleg Nesterov 	/* Once vma denies write, undo our temporary denial count */
13694d3d5b41SOleg Nesterov 	if (correct_wcount)
13704d3d5b41SOleg Nesterov 		atomic_inc(&inode->i_writecount);
13711da177e4SLinus Torvalds out:
1372cdd6c482SIngo Molnar 	perf_event_mmap(vma);
13730a4a9391SPeter Zijlstra 
13741da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
1375ab50b8edSHugh Dickins 	vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
13761da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
137706f9d8c2SKOSAKI Motohiro 		if (!mlock_vma_pages_range(vma, addr, addr + len))
137806f9d8c2SKOSAKI Motohiro 			mm->locked_vm += (len >> PAGE_SHIFT);
1379ba470de4SRik van Riel 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
138054cb8821SNick Piggin 		make_pages_present(addr, addr + len);
13811da177e4SLinus Torvalds 	return addr;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds unmap_and_free_vma:
13841da177e4SLinus Torvalds 	if (correct_wcount)
13851da177e4SLinus Torvalds 		atomic_inc(&inode->i_writecount);
13861da177e4SLinus Torvalds 	vma->vm_file = NULL;
13871da177e4SLinus Torvalds 	fput(file);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	/* Undo any partial mapping done by a device driver. */
1390e0da382cSHugh Dickins 	unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1391e0da382cSHugh Dickins 	charged = 0;
13921da177e4SLinus Torvalds free_vma:
13931da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
13941da177e4SLinus Torvalds unacct_error:
13951da177e4SLinus Torvalds 	if (charged)
13961da177e4SLinus Torvalds 		vm_unacct_memory(charged);
13971da177e4SLinus Torvalds 	return error;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds /* Get an address range which is currently unmapped.
14011da177e4SLinus Torvalds  * For shmat() with addr=0.
14021da177e4SLinus Torvalds  *
14031da177e4SLinus Torvalds  * Ugly calling convention alert:
14041da177e4SLinus Torvalds  * Return value with the low bits set means error value,
14051da177e4SLinus Torvalds  * ie
14061da177e4SLinus Torvalds  *	if (ret & ~PAGE_MASK)
14071da177e4SLinus Torvalds  *		error = ret;
14081da177e4SLinus Torvalds  *
14091da177e4SLinus Torvalds  * This function "knows" that -ENOMEM has the bits set.
14101da177e4SLinus Torvalds  */
14111da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA
14121da177e4SLinus Torvalds unsigned long
14131da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr,
14141da177e4SLinus Torvalds 		unsigned long len, unsigned long pgoff, unsigned long flags)
14151da177e4SLinus Torvalds {
14161da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
14171da177e4SLinus Torvalds 	struct vm_area_struct *vma;
14181da177e4SLinus Torvalds 	unsigned long start_addr;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	if (len > TASK_SIZE)
14211da177e4SLinus Torvalds 		return -ENOMEM;
14221da177e4SLinus Torvalds 
142306abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
142406abdfb4SBenjamin Herrenschmidt 		return addr;
142506abdfb4SBenjamin Herrenschmidt 
14261da177e4SLinus Torvalds 	if (addr) {
14271da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
14281da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
14291da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
14301da177e4SLinus Torvalds 		    (!vma || addr + len <= vma->vm_start))
14311da177e4SLinus Torvalds 			return addr;
14321da177e4SLinus Torvalds 	}
14331363c3cdSWolfgang Wander 	if (len > mm->cached_hole_size) {
14341da177e4SLinus Torvalds 	        start_addr = addr = mm->free_area_cache;
14351363c3cdSWolfgang Wander 	} else {
14361363c3cdSWolfgang Wander 	        start_addr = addr = TASK_UNMAPPED_BASE;
14371363c3cdSWolfgang Wander 	        mm->cached_hole_size = 0;
14381363c3cdSWolfgang Wander 	}
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds full_search:
14411da177e4SLinus Torvalds 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
14421da177e4SLinus Torvalds 		/* At this point:  (!vma || addr < vma->vm_end). */
14431da177e4SLinus Torvalds 		if (TASK_SIZE - len < addr) {
14441da177e4SLinus Torvalds 			/*
14451da177e4SLinus Torvalds 			 * Start a new search - just in case we missed
14461da177e4SLinus Torvalds 			 * some holes.
14471da177e4SLinus Torvalds 			 */
14481da177e4SLinus Torvalds 			if (start_addr != TASK_UNMAPPED_BASE) {
14491363c3cdSWolfgang Wander 				addr = TASK_UNMAPPED_BASE;
14501363c3cdSWolfgang Wander 			        start_addr = addr;
14511363c3cdSWolfgang Wander 				mm->cached_hole_size = 0;
14521da177e4SLinus Torvalds 				goto full_search;
14531da177e4SLinus Torvalds 			}
14541da177e4SLinus Torvalds 			return -ENOMEM;
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 		if (!vma || addr + len <= vma->vm_start) {
14571da177e4SLinus Torvalds 			/*
14581da177e4SLinus Torvalds 			 * Remember the place where we stopped the search:
14591da177e4SLinus Torvalds 			 */
14601da177e4SLinus Torvalds 			mm->free_area_cache = addr + len;
14611da177e4SLinus Torvalds 			return addr;
14621da177e4SLinus Torvalds 		}
14631363c3cdSWolfgang Wander 		if (addr + mm->cached_hole_size < vma->vm_start)
14641363c3cdSWolfgang Wander 		        mm->cached_hole_size = vma->vm_start - addr;
14651da177e4SLinus Torvalds 		addr = vma->vm_end;
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds }
14681da177e4SLinus Torvalds #endif
14691da177e4SLinus Torvalds 
14701363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	/*
14731da177e4SLinus Torvalds 	 * Is this a new hole at the lowest possible address?
14741da177e4SLinus Torvalds 	 */
14751363c3cdSWolfgang Wander 	if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
14761363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
14771363c3cdSWolfgang Wander 		mm->cached_hole_size = ~0UL;
14781363c3cdSWolfgang Wander 	}
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds /*
14821da177e4SLinus Torvalds  * This mmap-allocator allocates new areas top-down from below the
14831da177e4SLinus Torvalds  * stack's low limit (the base):
14841da177e4SLinus Torvalds  */
14851da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
14861da177e4SLinus Torvalds unsigned long
14871da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
14881da177e4SLinus Torvalds 			  const unsigned long len, const unsigned long pgoff,
14891da177e4SLinus Torvalds 			  const unsigned long flags)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	struct vm_area_struct *vma;
14921da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
14931da177e4SLinus Torvalds 	unsigned long addr = addr0;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	/* requested length too big for entire address space */
14961da177e4SLinus Torvalds 	if (len > TASK_SIZE)
14971da177e4SLinus Torvalds 		return -ENOMEM;
14981da177e4SLinus Torvalds 
149906abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
150006abdfb4SBenjamin Herrenschmidt 		return addr;
150106abdfb4SBenjamin Herrenschmidt 
15021da177e4SLinus Torvalds 	/* requesting a specific address */
15031da177e4SLinus Torvalds 	if (addr) {
15041da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
15051da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
15061da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
15071da177e4SLinus Torvalds 				(!vma || addr + len <= vma->vm_start))
15081da177e4SLinus Torvalds 			return addr;
15091da177e4SLinus Torvalds 	}
15101da177e4SLinus Torvalds 
15111363c3cdSWolfgang Wander 	/* check if free_area_cache is useful for us */
15121363c3cdSWolfgang Wander 	if (len <= mm->cached_hole_size) {
15131363c3cdSWolfgang Wander  	        mm->cached_hole_size = 0;
15141363c3cdSWolfgang Wander  		mm->free_area_cache = mm->mmap_base;
15151363c3cdSWolfgang Wander  	}
15161363c3cdSWolfgang Wander 
15171da177e4SLinus Torvalds 	/* either no address requested or can't fit in requested address hole */
15181da177e4SLinus Torvalds 	addr = mm->free_area_cache;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	/* make sure it can fit in the remaining address space */
152149a43876SLinus Torvalds 	if (addr > len) {
15221da177e4SLinus Torvalds 		vma = find_vma(mm, addr-len);
15231da177e4SLinus Torvalds 		if (!vma || addr <= vma->vm_start)
15241da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
15251da177e4SLinus Torvalds 			return (mm->free_area_cache = addr-len);
15261da177e4SLinus Torvalds 	}
15271da177e4SLinus Torvalds 
152873219d17SChris Wright 	if (mm->mmap_base < len)
152973219d17SChris Wright 		goto bottomup;
153073219d17SChris Wright 
15311da177e4SLinus Torvalds 	addr = mm->mmap_base-len;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	do {
15341da177e4SLinus Torvalds 		/*
15351da177e4SLinus Torvalds 		 * Lookup failure means no vma is above this address,
15361da177e4SLinus Torvalds 		 * else if new region fits below vma->vm_start,
15371da177e4SLinus Torvalds 		 * return with success:
15381da177e4SLinus Torvalds 		 */
15391da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
15401da177e4SLinus Torvalds 		if (!vma || addr+len <= vma->vm_start)
15411da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
15421da177e4SLinus Torvalds 			return (mm->free_area_cache = addr);
15431da177e4SLinus Torvalds 
15441363c3cdSWolfgang Wander  		/* remember the largest hole we saw so far */
15451363c3cdSWolfgang Wander  		if (addr + mm->cached_hole_size < vma->vm_start)
15461363c3cdSWolfgang Wander  		        mm->cached_hole_size = vma->vm_start - addr;
15471363c3cdSWolfgang Wander 
15481da177e4SLinus Torvalds 		/* try just below the current vma->vm_start */
15491da177e4SLinus Torvalds 		addr = vma->vm_start-len;
155049a43876SLinus Torvalds 	} while (len < vma->vm_start);
15511da177e4SLinus Torvalds 
155273219d17SChris Wright bottomup:
15531da177e4SLinus Torvalds 	/*
15541da177e4SLinus Torvalds 	 * A failed mmap() very likely causes application failure,
15551da177e4SLinus Torvalds 	 * so fall back to the bottom-up function here. This scenario
15561da177e4SLinus Torvalds 	 * can happen with large stack limits and large mmap()
15571da177e4SLinus Torvalds 	 * allocations.
15581da177e4SLinus Torvalds 	 */
15591363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
15601da177e4SLinus Torvalds   	mm->free_area_cache = TASK_UNMAPPED_BASE;
15611da177e4SLinus Torvalds 	addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
15621da177e4SLinus Torvalds 	/*
15631da177e4SLinus Torvalds 	 * Restore the topdown base:
15641da177e4SLinus Torvalds 	 */
15651da177e4SLinus Torvalds 	mm->free_area_cache = mm->mmap_base;
15661363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	return addr;
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds #endif
15711da177e4SLinus Torvalds 
15721363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
15731da177e4SLinus Torvalds {
15741da177e4SLinus Torvalds 	/*
15751da177e4SLinus Torvalds 	 * Is this a new hole at the highest possible address?
15761da177e4SLinus Torvalds 	 */
15771363c3cdSWolfgang Wander 	if (addr > mm->free_area_cache)
15781363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	/* dont allow allocations above current base */
15811363c3cdSWolfgang Wander 	if (mm->free_area_cache > mm->mmap_base)
15821363c3cdSWolfgang Wander 		mm->free_area_cache = mm->mmap_base;
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds unsigned long
15861da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
15871da177e4SLinus Torvalds 		unsigned long pgoff, unsigned long flags)
15881da177e4SLinus Torvalds {
158906abdfb4SBenjamin Herrenschmidt 	unsigned long (*get_area)(struct file *, unsigned long,
159006abdfb4SBenjamin Herrenschmidt 				  unsigned long, unsigned long, unsigned long);
159107ab67c8SLinus Torvalds 
15929206de95SAl Viro 	unsigned long error = arch_mmap_check(addr, len, flags);
15939206de95SAl Viro 	if (error)
15949206de95SAl Viro 		return error;
15959206de95SAl Viro 
15969206de95SAl Viro 	/* Careful about overflows.. */
15979206de95SAl Viro 	if (len > TASK_SIZE)
15989206de95SAl Viro 		return -ENOMEM;
15999206de95SAl Viro 
160007ab67c8SLinus Torvalds 	get_area = current->mm->get_unmapped_area;
160107ab67c8SLinus Torvalds 	if (file && file->f_op && file->f_op->get_unmapped_area)
160207ab67c8SLinus Torvalds 		get_area = file->f_op->get_unmapped_area;
160307ab67c8SLinus Torvalds 	addr = get_area(file, addr, len, pgoff, flags);
160407ab67c8SLinus Torvalds 	if (IS_ERR_VALUE(addr))
160507ab67c8SLinus Torvalds 		return addr;
160607ab67c8SLinus Torvalds 
16071da177e4SLinus Torvalds 	if (addr > TASK_SIZE - len)
16081da177e4SLinus Torvalds 		return -ENOMEM;
16091da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
16101da177e4SLinus Torvalds 		return -EINVAL;
161106abdfb4SBenjamin Herrenschmidt 
161208e7d9b5SMartin Schwidefsky 	return arch_rebalance_pgtables(addr, len);
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area);
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
16181da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	if (mm) {
16231da177e4SLinus Torvalds 		/* Check the cache first. */
16241da177e4SLinus Torvalds 		/* (Cache hit rate is typically around 35%.) */
16251da177e4SLinus Torvalds 		vma = mm->mmap_cache;
16261da177e4SLinus Torvalds 		if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
16271da177e4SLinus Torvalds 			struct rb_node * rb_node;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 			rb_node = mm->mm_rb.rb_node;
16301da177e4SLinus Torvalds 			vma = NULL;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 			while (rb_node) {
16331da177e4SLinus Torvalds 				struct vm_area_struct * vma_tmp;
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 				vma_tmp = rb_entry(rb_node,
16361da177e4SLinus Torvalds 						struct vm_area_struct, vm_rb);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 				if (vma_tmp->vm_end > addr) {
16391da177e4SLinus Torvalds 					vma = vma_tmp;
16401da177e4SLinus Torvalds 					if (vma_tmp->vm_start <= addr)
16411da177e4SLinus Torvalds 						break;
16421da177e4SLinus Torvalds 					rb_node = rb_node->rb_left;
16431da177e4SLinus Torvalds 				} else
16441da177e4SLinus Torvalds 					rb_node = rb_node->rb_right;
16451da177e4SLinus Torvalds 			}
16461da177e4SLinus Torvalds 			if (vma)
16471da177e4SLinus Torvalds 				mm->mmap_cache = vma;
16481da177e4SLinus Torvalds 		}
16491da177e4SLinus Torvalds 	}
16501da177e4SLinus Torvalds 	return vma;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma);
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
16561da177e4SLinus Torvalds struct vm_area_struct *
16571da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr,
16581da177e4SLinus Torvalds 			struct vm_area_struct **pprev)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL, *prev = NULL;
16611da177e4SLinus Torvalds 	struct rb_node *rb_node;
16621da177e4SLinus Torvalds 	if (!mm)
16631da177e4SLinus Torvalds 		goto out;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	/* Guard against addr being lower than the first VMA */
16661da177e4SLinus Torvalds 	vma = mm->mmap;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	/* Go through the RB tree quickly. */
16691da177e4SLinus Torvalds 	rb_node = mm->mm_rb.rb_node;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	while (rb_node) {
16721da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
16731da177e4SLinus Torvalds 		vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 		if (addr < vma_tmp->vm_end) {
16761da177e4SLinus Torvalds 			rb_node = rb_node->rb_left;
16771da177e4SLinus Torvalds 		} else {
16781da177e4SLinus Torvalds 			prev = vma_tmp;
16791da177e4SLinus Torvalds 			if (!prev->vm_next || (addr < prev->vm_next->vm_end))
16801da177e4SLinus Torvalds 				break;
16811da177e4SLinus Torvalds 			rb_node = rb_node->rb_right;
16821da177e4SLinus Torvalds 		}
16831da177e4SLinus Torvalds 	}
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds out:
16861da177e4SLinus Torvalds 	*pprev = prev;
16871da177e4SLinus Torvalds 	return prev ? prev->vm_next : vma;
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds /*
16911da177e4SLinus Torvalds  * Verify that the stack growth is acceptable and
16921da177e4SLinus Torvalds  * update accounting. This is shared with both the
16931da177e4SLinus Torvalds  * grow-up and grow-down cases.
16941da177e4SLinus Torvalds  */
16951da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
16981da177e4SLinus Torvalds 	struct rlimit *rlim = current->signal->rlim;
16990d59a01bSAdam Litke 	unsigned long new_start;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	/* address space limit tests */
1702119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, grow))
17031da177e4SLinus Torvalds 		return -ENOMEM;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	/* Stack limit test */
170659e99e5bSJiri Slaby 	if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
17071da177e4SLinus Torvalds 		return -ENOMEM;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	/* mlock limit tests */
17101da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED) {
17111da177e4SLinus Torvalds 		unsigned long locked;
17121da177e4SLinus Torvalds 		unsigned long limit;
17131da177e4SLinus Torvalds 		locked = mm->locked_vm + grow;
171459e99e5bSJiri Slaby 		limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
171559e99e5bSJiri Slaby 		limit >>= PAGE_SHIFT;
17161da177e4SLinus Torvalds 		if (locked > limit && !capable(CAP_IPC_LOCK))
17171da177e4SLinus Torvalds 			return -ENOMEM;
17181da177e4SLinus Torvalds 	}
17191da177e4SLinus Torvalds 
17200d59a01bSAdam Litke 	/* Check to ensure the stack will not grow into a hugetlb-only region */
17210d59a01bSAdam Litke 	new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
17220d59a01bSAdam Litke 			vma->vm_end - size;
17230d59a01bSAdam Litke 	if (is_hugepage_only_range(vma->vm_mm, new_start, size))
17240d59a01bSAdam Litke 		return -EFAULT;
17250d59a01bSAdam Litke 
17261da177e4SLinus Torvalds 	/*
17271da177e4SLinus Torvalds 	 * Overcommit..  This must be the final test, as it will
17281da177e4SLinus Torvalds 	 * update security statistics.
17291da177e4SLinus Torvalds 	 */
173005fa199dSHugh Dickins 	if (security_vm_enough_memory_mm(mm, grow))
17311da177e4SLinus Torvalds 		return -ENOMEM;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	/* Ok, everything looks good - let it rip */
17341da177e4SLinus Torvalds 	mm->total_vm += grow;
17351da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED)
17361da177e4SLinus Torvalds 		mm->locked_vm += grow;
1737ab50b8edSHugh Dickins 	vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
17381da177e4SLinus Torvalds 	return 0;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
174146dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
17421da177e4SLinus Torvalds /*
174346dea3d0SHugh Dickins  * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
174446dea3d0SHugh Dickins  * vma is the last one with address > vma->vm_end.  Have to extend vma.
17451da177e4SLinus Torvalds  */
174646dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address)
17471da177e4SLinus Torvalds {
17481da177e4SLinus Torvalds 	int error;
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSUP))
17511da177e4SLinus Torvalds 		return -EFAULT;
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds 	/*
17541da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
17551da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
17561da177e4SLinus Torvalds 	 */
17571da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
17581da177e4SLinus Torvalds 		return -ENOMEM;
1759bb4a340eSRik van Riel 	vma_lock_anon_vma(vma);
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	/*
17621da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
17631da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
17641da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
176506b32f3aSHelge Deller 	 * Also guard against wrapping around to address 0.
17661da177e4SLinus Torvalds 	 */
176706b32f3aSHelge Deller 	if (address < PAGE_ALIGN(address+4))
176806b32f3aSHelge Deller 		address = PAGE_ALIGN(address+4);
176906b32f3aSHelge Deller 	else {
1770bb4a340eSRik van Riel 		vma_unlock_anon_vma(vma);
177106b32f3aSHelge Deller 		return -ENOMEM;
177206b32f3aSHelge Deller 	}
17731da177e4SLinus Torvalds 	error = 0;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
17761da177e4SLinus Torvalds 	if (address > vma->vm_end) {
17771da177e4SLinus Torvalds 		unsigned long size, grow;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 		size = address - vma->vm_start;
17801da177e4SLinus Torvalds 		grow = (address - vma->vm_end) >> PAGE_SHIFT;
17811da177e4SLinus Torvalds 
178242c36f63SHugh Dickins 		error = -ENOMEM;
178342c36f63SHugh Dickins 		if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
17841da177e4SLinus Torvalds 			error = acct_stack_growth(vma, size, grow);
17853af9e859SEric B Munson 			if (!error) {
17861da177e4SLinus Torvalds 				vma->vm_end = address;
17873af9e859SEric B Munson 				perf_event_mmap(vma);
17883af9e859SEric B Munson 			}
17891da177e4SLinus Torvalds 		}
179042c36f63SHugh Dickins 	}
1791bb4a340eSRik van Riel 	vma_unlock_anon_vma(vma);
1792b15d00b6SAndrea Arcangeli 	khugepaged_enter_vma_merge(vma);
17931da177e4SLinus Torvalds 	return error;
17941da177e4SLinus Torvalds }
179546dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
179646dea3d0SHugh Dickins 
17971da177e4SLinus Torvalds /*
17981da177e4SLinus Torvalds  * vma is the first one with address < vma->vm_start.  Have to extend vma.
17991da177e4SLinus Torvalds  */
1800cb8f488cSDenys Vlasenko static int expand_downwards(struct vm_area_struct *vma,
1801b6a2fea3SOllie Wild 				   unsigned long address)
18021da177e4SLinus Torvalds {
18031da177e4SLinus Torvalds 	int error;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	/*
18061da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
18071da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
18081da177e4SLinus Torvalds 	 */
18091da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
18101da177e4SLinus Torvalds 		return -ENOMEM;
18118869477aSEric Paris 
18128869477aSEric Paris 	address &= PAGE_MASK;
181388c3f7a8SRichard Knutsson 	error = security_file_mmap(NULL, 0, 0, 0, address, 1);
18148869477aSEric Paris 	if (error)
18158869477aSEric Paris 		return error;
18168869477aSEric Paris 
1817bb4a340eSRik van Riel 	vma_lock_anon_vma(vma);
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	/*
18201da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
18211da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
18221da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
18231da177e4SLinus Torvalds 	 */
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
18261da177e4SLinus Torvalds 	if (address < vma->vm_start) {
18271da177e4SLinus Torvalds 		unsigned long size, grow;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 		size = vma->vm_end - address;
18301da177e4SLinus Torvalds 		grow = (vma->vm_start - address) >> PAGE_SHIFT;
18311da177e4SLinus Torvalds 
1832a626ca6aSLinus Torvalds 		error = -ENOMEM;
1833a626ca6aSLinus Torvalds 		if (grow <= vma->vm_pgoff) {
18341da177e4SLinus Torvalds 			error = acct_stack_growth(vma, size, grow);
18351da177e4SLinus Torvalds 			if (!error) {
18361da177e4SLinus Torvalds 				vma->vm_start = address;
18371da177e4SLinus Torvalds 				vma->vm_pgoff -= grow;
18383af9e859SEric B Munson 				perf_event_mmap(vma);
18391da177e4SLinus Torvalds 			}
18401da177e4SLinus Torvalds 		}
1841a626ca6aSLinus Torvalds 	}
1842bb4a340eSRik van Riel 	vma_unlock_anon_vma(vma);
1843b15d00b6SAndrea Arcangeli 	khugepaged_enter_vma_merge(vma);
18441da177e4SLinus Torvalds 	return error;
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds 
1847b6a2fea3SOllie Wild int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
1848b6a2fea3SOllie Wild {
1849b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1850b6a2fea3SOllie Wild }
1851b6a2fea3SOllie Wild 
1852b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP
1853b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1854b6a2fea3SOllie Wild {
1855b6a2fea3SOllie Wild 	return expand_upwards(vma, address);
1856b6a2fea3SOllie Wild }
1857b6a2fea3SOllie Wild 
1858b6a2fea3SOllie Wild struct vm_area_struct *
1859b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr)
1860b6a2fea3SOllie Wild {
1861b6a2fea3SOllie Wild 	struct vm_area_struct *vma, *prev;
1862b6a2fea3SOllie Wild 
1863b6a2fea3SOllie Wild 	addr &= PAGE_MASK;
1864b6a2fea3SOllie Wild 	vma = find_vma_prev(mm, addr, &prev);
1865b6a2fea3SOllie Wild 	if (vma && (vma->vm_start <= addr))
1866b6a2fea3SOllie Wild 		return vma;
18671c127185SDenys Vlasenko 	if (!prev || expand_stack(prev, addr))
1868b6a2fea3SOllie Wild 		return NULL;
1869ba470de4SRik van Riel 	if (prev->vm_flags & VM_LOCKED) {
1870c58267c3SKOSAKI Motohiro 		mlock_vma_pages_range(prev, addr, prev->vm_end);
1871ba470de4SRik van Riel 	}
1872b6a2fea3SOllie Wild 	return prev;
1873b6a2fea3SOllie Wild }
1874b6a2fea3SOllie Wild #else
1875b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1876b6a2fea3SOllie Wild {
1877b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1878b6a2fea3SOllie Wild }
1879b6a2fea3SOllie Wild 
18801da177e4SLinus Torvalds struct vm_area_struct *
18811da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr)
18821da177e4SLinus Torvalds {
18831da177e4SLinus Torvalds 	struct vm_area_struct * vma;
18841da177e4SLinus Torvalds 	unsigned long start;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	addr &= PAGE_MASK;
18871da177e4SLinus Torvalds 	vma = find_vma(mm,addr);
18881da177e4SLinus Torvalds 	if (!vma)
18891da177e4SLinus Torvalds 		return NULL;
18901da177e4SLinus Torvalds 	if (vma->vm_start <= addr)
18911da177e4SLinus Torvalds 		return vma;
18921da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSDOWN))
18931da177e4SLinus Torvalds 		return NULL;
18941da177e4SLinus Torvalds 	start = vma->vm_start;
18951da177e4SLinus Torvalds 	if (expand_stack(vma, addr))
18961da177e4SLinus Torvalds 		return NULL;
1897ba470de4SRik van Riel 	if (vma->vm_flags & VM_LOCKED) {
1898c58267c3SKOSAKI Motohiro 		mlock_vma_pages_range(vma, addr, start);
1899ba470de4SRik van Riel 	}
19001da177e4SLinus Torvalds 	return vma;
19011da177e4SLinus Torvalds }
19021da177e4SLinus Torvalds #endif
19031da177e4SLinus Torvalds 
19042c0b3814SHugh Dickins /*
19052c0b3814SHugh Dickins  * Ok - we have the memory areas we should free on the vma list,
19062c0b3814SHugh Dickins  * so release them, and do the vma updates.
19071da177e4SLinus Torvalds  *
19082c0b3814SHugh Dickins  * Called with the mm semaphore held.
19091da177e4SLinus Torvalds  */
19102c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
19111da177e4SLinus Torvalds {
1912365e9c87SHugh Dickins 	/* Update high watermark before we lower total_vm */
1913365e9c87SHugh Dickins 	update_hiwater_vm(mm);
19142c0b3814SHugh Dickins 	do {
1915ab50b8edSHugh Dickins 		long nrpages = vma_pages(vma);
19161da177e4SLinus Torvalds 
1917ab50b8edSHugh Dickins 		mm->total_vm -= nrpages;
1918ab50b8edSHugh Dickins 		vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
1919a8fb5618SHugh Dickins 		vma = remove_vma(vma);
1920146425a3SHugh Dickins 	} while (vma);
19211da177e4SLinus Torvalds 	validate_mm(mm);
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds /*
19251da177e4SLinus Torvalds  * Get rid of page table information in the indicated region.
19261da177e4SLinus Torvalds  *
1927f10df686SPaolo 'Blaisorblade' Giarrusso  * Called with the mm semaphore held.
19281da177e4SLinus Torvalds  */
19291da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm,
1930e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
1931e0da382cSHugh Dickins 		unsigned long start, unsigned long end)
19321da177e4SLinus Torvalds {
1933e0da382cSHugh Dickins 	struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
19341da177e4SLinus Torvalds 	struct mmu_gather *tlb;
19351da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	lru_add_drain();
19381da177e4SLinus Torvalds 	tlb = tlb_gather_mmu(mm, 0);
1939365e9c87SHugh Dickins 	update_hiwater_rss(mm);
1940508034a3SHugh Dickins 	unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
19411da177e4SLinus Torvalds 	vm_unacct_memory(nr_accounted);
194242b77728SJan Beulich 	free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1943e0da382cSHugh Dickins 				 next? next->vm_start: 0);
19441da177e4SLinus Torvalds 	tlb_finish_mmu(tlb, start, end);
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds /*
19481da177e4SLinus Torvalds  * Create a list of vma's touched by the unmap, removing them from the mm's
19491da177e4SLinus Torvalds  * vma list as we go..
19501da177e4SLinus Torvalds  */
19511da177e4SLinus Torvalds static void
19521da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
19531da177e4SLinus Torvalds 	struct vm_area_struct *prev, unsigned long end)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	struct vm_area_struct **insertion_point;
19561da177e4SLinus Torvalds 	struct vm_area_struct *tail_vma = NULL;
19571363c3cdSWolfgang Wander 	unsigned long addr;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1960297c5eeeSLinus Torvalds 	vma->vm_prev = NULL;
19611da177e4SLinus Torvalds 	do {
19621da177e4SLinus Torvalds 		rb_erase(&vma->vm_rb, &mm->mm_rb);
19631da177e4SLinus Torvalds 		mm->map_count--;
19641da177e4SLinus Torvalds 		tail_vma = vma;
19651da177e4SLinus Torvalds 		vma = vma->vm_next;
19661da177e4SLinus Torvalds 	} while (vma && vma->vm_start < end);
19671da177e4SLinus Torvalds 	*insertion_point = vma;
1968297c5eeeSLinus Torvalds 	if (vma)
1969297c5eeeSLinus Torvalds 		vma->vm_prev = prev;
19701da177e4SLinus Torvalds 	tail_vma->vm_next = NULL;
19711363c3cdSWolfgang Wander 	if (mm->unmap_area == arch_unmap_area)
19721363c3cdSWolfgang Wander 		addr = prev ? prev->vm_end : mm->mmap_base;
19731363c3cdSWolfgang Wander 	else
19741363c3cdSWolfgang Wander 		addr = vma ?  vma->vm_start : mm->mmap_base;
19751363c3cdSWolfgang Wander 	mm->unmap_area(mm, addr);
19761da177e4SLinus Torvalds 	mm->mmap_cache = NULL;		/* Kill the cache. */
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds /*
1980659ace58SKOSAKI Motohiro  * __split_vma() bypasses sysctl_max_map_count checking.  We use this on the
1981659ace58SKOSAKI Motohiro  * munmap path where it doesn't make sense to fail.
19821da177e4SLinus Torvalds  */
1983659ace58SKOSAKI Motohiro static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
19841da177e4SLinus Torvalds 	      unsigned long addr, int new_below)
19851da177e4SLinus Torvalds {
19861da177e4SLinus Torvalds 	struct mempolicy *pol;
19871da177e4SLinus Torvalds 	struct vm_area_struct *new;
19885beb4930SRik van Riel 	int err = -ENOMEM;
19891da177e4SLinus Torvalds 
1990a5516438SAndi Kleen 	if (is_vm_hugetlb_page(vma) && (addr &
1991a5516438SAndi Kleen 					~(huge_page_mask(hstate_vma(vma)))))
19921da177e4SLinus Torvalds 		return -EINVAL;
19931da177e4SLinus Torvalds 
1994e94b1766SChristoph Lameter 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
19951da177e4SLinus Torvalds 	if (!new)
19965beb4930SRik van Riel 		goto out_err;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	/* most fields are the same, copy all, and then fixup */
19991da177e4SLinus Torvalds 	*new = *vma;
20001da177e4SLinus Torvalds 
20015beb4930SRik van Riel 	INIT_LIST_HEAD(&new->anon_vma_chain);
20025beb4930SRik van Riel 
20031da177e4SLinus Torvalds 	if (new_below)
20041da177e4SLinus Torvalds 		new->vm_end = addr;
20051da177e4SLinus Torvalds 	else {
20061da177e4SLinus Torvalds 		new->vm_start = addr;
20071da177e4SLinus Torvalds 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
20081da177e4SLinus Torvalds 	}
20091da177e4SLinus Torvalds 
2010846a16bfSLee Schermerhorn 	pol = mpol_dup(vma_policy(vma));
20111da177e4SLinus Torvalds 	if (IS_ERR(pol)) {
20125beb4930SRik van Riel 		err = PTR_ERR(pol);
20135beb4930SRik van Riel 		goto out_free_vma;
20141da177e4SLinus Torvalds 	}
20151da177e4SLinus Torvalds 	vma_set_policy(new, pol);
20161da177e4SLinus Torvalds 
20175beb4930SRik van Riel 	if (anon_vma_clone(new, vma))
20185beb4930SRik van Riel 		goto out_free_mpol;
20195beb4930SRik van Riel 
2020925d1c40SMatt Helsley 	if (new->vm_file) {
20211da177e4SLinus Torvalds 		get_file(new->vm_file);
2022925d1c40SMatt Helsley 		if (vma->vm_flags & VM_EXECUTABLE)
2023925d1c40SMatt Helsley 			added_exe_file_vma(mm);
2024925d1c40SMatt Helsley 	}
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	if (new->vm_ops && new->vm_ops->open)
20271da177e4SLinus Torvalds 		new->vm_ops->open(new);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	if (new_below)
20305beb4930SRik van Riel 		err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
20311da177e4SLinus Torvalds 			((addr - new->vm_start) >> PAGE_SHIFT), new);
20321da177e4SLinus Torvalds 	else
20335beb4930SRik van Riel 		err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
20341da177e4SLinus Torvalds 
20355beb4930SRik van Riel 	/* Success. */
20365beb4930SRik van Riel 	if (!err)
20371da177e4SLinus Torvalds 		return 0;
20385beb4930SRik van Riel 
20395beb4930SRik van Riel 	/* Clean everything up if vma_adjust failed. */
204058927533SRik van Riel 	if (new->vm_ops && new->vm_ops->close)
20415beb4930SRik van Riel 		new->vm_ops->close(new);
20425beb4930SRik van Riel 	if (new->vm_file) {
20435beb4930SRik van Riel 		if (vma->vm_flags & VM_EXECUTABLE)
20445beb4930SRik van Riel 			removed_exe_file_vma(mm);
20455beb4930SRik van Riel 		fput(new->vm_file);
20465beb4930SRik van Riel 	}
20472aeadc30SAndrea Arcangeli 	unlink_anon_vmas(new);
20485beb4930SRik van Riel  out_free_mpol:
20495beb4930SRik van Riel 	mpol_put(pol);
20505beb4930SRik van Riel  out_free_vma:
20515beb4930SRik van Riel 	kmem_cache_free(vm_area_cachep, new);
20525beb4930SRik van Riel  out_err:
20535beb4930SRik van Riel 	return err;
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds 
2056659ace58SKOSAKI Motohiro /*
2057659ace58SKOSAKI Motohiro  * Split a vma into two pieces at address 'addr', a new vma is allocated
2058659ace58SKOSAKI Motohiro  * either for the first part or the tail.
2059659ace58SKOSAKI Motohiro  */
2060659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2061659ace58SKOSAKI Motohiro 	      unsigned long addr, int new_below)
2062659ace58SKOSAKI Motohiro {
2063659ace58SKOSAKI Motohiro 	if (mm->map_count >= sysctl_max_map_count)
2064659ace58SKOSAKI Motohiro 		return -ENOMEM;
2065659ace58SKOSAKI Motohiro 
2066659ace58SKOSAKI Motohiro 	return __split_vma(mm, vma, addr, new_below);
2067659ace58SKOSAKI Motohiro }
2068659ace58SKOSAKI Motohiro 
20691da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds
20701da177e4SLinus Torvalds  * what needs doing, and the areas themselves, which do the
20711da177e4SLinus Torvalds  * work.  This now handles partial unmappings.
20721da177e4SLinus Torvalds  * Jeremy Fitzhardinge <jeremy@goop.org>
20731da177e4SLinus Torvalds  */
20741da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
20751da177e4SLinus Torvalds {
20761da177e4SLinus Torvalds 	unsigned long end;
2077146425a3SHugh Dickins 	struct vm_area_struct *vma, *prev, *last;
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
20801da177e4SLinus Torvalds 		return -EINVAL;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	if ((len = PAGE_ALIGN(len)) == 0)
20831da177e4SLinus Torvalds 		return -EINVAL;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Find the first overlapping VMA */
2086146425a3SHugh Dickins 	vma = find_vma_prev(mm, start, &prev);
2087146425a3SHugh Dickins 	if (!vma)
20881da177e4SLinus Torvalds 		return 0;
2089146425a3SHugh Dickins 	/* we have  start < vma->vm_end  */
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	/* if it doesn't overlap, we have nothing.. */
20921da177e4SLinus Torvalds 	end = start + len;
2093146425a3SHugh Dickins 	if (vma->vm_start >= end)
20941da177e4SLinus Torvalds 		return 0;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	/*
20971da177e4SLinus Torvalds 	 * If we need to split any vma, do it now to save pain later.
20981da177e4SLinus Torvalds 	 *
20991da177e4SLinus Torvalds 	 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
21001da177e4SLinus Torvalds 	 * unmapped vm_area_struct will remain in use: so lower split_vma
21011da177e4SLinus Torvalds 	 * places tmp vma above, and higher split_vma places tmp vma below.
21021da177e4SLinus Torvalds 	 */
2103146425a3SHugh Dickins 	if (start > vma->vm_start) {
2104659ace58SKOSAKI Motohiro 		int error;
2105659ace58SKOSAKI Motohiro 
2106659ace58SKOSAKI Motohiro 		/*
2107659ace58SKOSAKI Motohiro 		 * Make sure that map_count on return from munmap() will
2108659ace58SKOSAKI Motohiro 		 * not exceed its limit; but let map_count go just above
2109659ace58SKOSAKI Motohiro 		 * its limit temporarily, to help free resources as expected.
2110659ace58SKOSAKI Motohiro 		 */
2111659ace58SKOSAKI Motohiro 		if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2112659ace58SKOSAKI Motohiro 			return -ENOMEM;
2113659ace58SKOSAKI Motohiro 
2114659ace58SKOSAKI Motohiro 		error = __split_vma(mm, vma, start, 0);
21151da177e4SLinus Torvalds 		if (error)
21161da177e4SLinus Torvalds 			return error;
2117146425a3SHugh Dickins 		prev = vma;
21181da177e4SLinus Torvalds 	}
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 	/* Does it split the last one? */
21211da177e4SLinus Torvalds 	last = find_vma(mm, end);
21221da177e4SLinus Torvalds 	if (last && end > last->vm_start) {
2123659ace58SKOSAKI Motohiro 		int error = __split_vma(mm, last, end, 1);
21241da177e4SLinus Torvalds 		if (error)
21251da177e4SLinus Torvalds 			return error;
21261da177e4SLinus Torvalds 	}
2127146425a3SHugh Dickins 	vma = prev? prev->vm_next: mm->mmap;
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	/*
2130ba470de4SRik van Riel 	 * unlock any mlock()ed ranges before detaching vmas
2131ba470de4SRik van Riel 	 */
2132ba470de4SRik van Riel 	if (mm->locked_vm) {
2133ba470de4SRik van Riel 		struct vm_area_struct *tmp = vma;
2134ba470de4SRik van Riel 		while (tmp && tmp->vm_start < end) {
2135ba470de4SRik van Riel 			if (tmp->vm_flags & VM_LOCKED) {
2136ba470de4SRik van Riel 				mm->locked_vm -= vma_pages(tmp);
2137ba470de4SRik van Riel 				munlock_vma_pages_all(tmp);
2138ba470de4SRik van Riel 			}
2139ba470de4SRik van Riel 			tmp = tmp->vm_next;
2140ba470de4SRik van Riel 		}
2141ba470de4SRik van Riel 	}
2142ba470de4SRik van Riel 
2143ba470de4SRik van Riel 	/*
21441da177e4SLinus Torvalds 	 * Remove the vma's, and unmap the actual pages
21451da177e4SLinus Torvalds 	 */
2146146425a3SHugh Dickins 	detach_vmas_to_be_unmapped(mm, vma, prev, end);
2147146425a3SHugh Dickins 	unmap_region(mm, vma, prev, start, end);
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	/* Fix up all other VM information */
21502c0b3814SHugh Dickins 	remove_vma_list(mm, vma);
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	return 0;
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds EXPORT_SYMBOL(do_munmap);
21561da177e4SLinus Torvalds 
21576a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
21581da177e4SLinus Torvalds {
21591da177e4SLinus Torvalds 	int ret;
21601da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	profile_munmap(addr);
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
21651da177e4SLinus Torvalds 	ret = do_munmap(mm, addr, len);
21661da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
21671da177e4SLinus Torvalds 	return ret;
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm)
21711da177e4SLinus Torvalds {
2172a241ec65SPaul E. McKenney #ifdef CONFIG_DEBUG_VM
21731da177e4SLinus Torvalds 	if (unlikely(down_read_trylock(&mm->mmap_sem))) {
21741da177e4SLinus Torvalds 		WARN_ON(1);
21751da177e4SLinus Torvalds 		up_read(&mm->mmap_sem);
21761da177e4SLinus Torvalds 	}
21771da177e4SLinus Torvalds #endif
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds /*
21811da177e4SLinus Torvalds  *  this is really a simplified "do_mmap".  it only handles
21821da177e4SLinus Torvalds  *  anonymous maps.  eventually we may be able to do some
21831da177e4SLinus Torvalds  *  brk-specific accounting here.
21841da177e4SLinus Torvalds  */
21851da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len)
21861da177e4SLinus Torvalds {
21871da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
21881da177e4SLinus Torvalds 	struct vm_area_struct * vma, * prev;
21891da177e4SLinus Torvalds 	unsigned long flags;
21901da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
21911da177e4SLinus Torvalds 	pgoff_t pgoff = addr >> PAGE_SHIFT;
21923a459756SKirill Korotaev 	int error;
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
21951da177e4SLinus Torvalds 	if (!len)
21961da177e4SLinus Torvalds 		return addr;
21971da177e4SLinus Torvalds 
219888c3f7a8SRichard Knutsson 	error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
21995a211a5dSEric Paris 	if (error)
22005a211a5dSEric Paris 		return error;
22015a211a5dSEric Paris 
22023a459756SKirill Korotaev 	flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
22033a459756SKirill Korotaev 
22042c6a1016SAl Viro 	error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
22052c6a1016SAl Viro 	if (error & ~PAGE_MASK)
22063a459756SKirill Korotaev 		return error;
22073a459756SKirill Korotaev 
22081da177e4SLinus Torvalds 	/*
22091da177e4SLinus Torvalds 	 * mlock MCL_FUTURE?
22101da177e4SLinus Torvalds 	 */
22111da177e4SLinus Torvalds 	if (mm->def_flags & VM_LOCKED) {
22121da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
221393ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
221493ea1d0aSChris Wright 		locked += mm->locked_vm;
221559e99e5bSJiri Slaby 		lock_limit = rlimit(RLIMIT_MEMLOCK);
221693ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
22171da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
22181da177e4SLinus Torvalds 			return -EAGAIN;
22191da177e4SLinus Torvalds 	}
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	/*
22221da177e4SLinus Torvalds 	 * mm->mmap_sem is required to protect against another thread
22231da177e4SLinus Torvalds 	 * changing the mappings in case we sleep.
22241da177e4SLinus Torvalds 	 */
22251da177e4SLinus Torvalds 	verify_mm_writelocked(mm);
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	/*
22281da177e4SLinus Torvalds 	 * Clear old maps.  this also does some error checking for us
22291da177e4SLinus Torvalds 	 */
22301da177e4SLinus Torvalds  munmap_back:
22311da177e4SLinus Torvalds 	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
22321da177e4SLinus Torvalds 	if (vma && vma->vm_start < addr + len) {
22331da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
22341da177e4SLinus Torvalds 			return -ENOMEM;
22351da177e4SLinus Torvalds 		goto munmap_back;
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	/* Check against address space limits *after* clearing old maps... */
2239119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
22401da177e4SLinus Torvalds 		return -ENOMEM;
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
22431da177e4SLinus Torvalds 		return -ENOMEM;
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	if (security_vm_enough_memory(len >> PAGE_SHIFT))
22461da177e4SLinus Torvalds 		return -ENOMEM;
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 	/* Can we just expand an old private anonymous mapping? */
2249ba470de4SRik van Riel 	vma = vma_merge(mm, prev, addr, addr + len, flags,
2250ba470de4SRik van Riel 					NULL, NULL, pgoff, NULL);
2251ba470de4SRik van Riel 	if (vma)
22521da177e4SLinus Torvalds 		goto out;
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	/*
22551da177e4SLinus Torvalds 	 * create a vma struct for an anonymous mapping
22561da177e4SLinus Torvalds 	 */
2257c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
22581da177e4SLinus Torvalds 	if (!vma) {
22591da177e4SLinus Torvalds 		vm_unacct_memory(len >> PAGE_SHIFT);
22601da177e4SLinus Torvalds 		return -ENOMEM;
22611da177e4SLinus Torvalds 	}
22621da177e4SLinus Torvalds 
22635beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
22641da177e4SLinus Torvalds 	vma->vm_mm = mm;
22651da177e4SLinus Torvalds 	vma->vm_start = addr;
22661da177e4SLinus Torvalds 	vma->vm_end = addr + len;
22671da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
22681da177e4SLinus Torvalds 	vma->vm_flags = flags;
22693ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(flags);
22701da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
22711da177e4SLinus Torvalds out:
22723af9e859SEric B Munson 	perf_event_mmap(vma);
22731da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
22741da177e4SLinus Torvalds 	if (flags & VM_LOCKED) {
2275ba470de4SRik van Riel 		if (!mlock_vma_pages_range(vma, addr, addr + len))
2276ba470de4SRik van Riel 			mm->locked_vm += (len >> PAGE_SHIFT);
22771da177e4SLinus Torvalds 	}
22781da177e4SLinus Torvalds 	return addr;
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds EXPORT_SYMBOL(do_brk);
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds /* Release all mmaps. */
22841da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
22851da177e4SLinus Torvalds {
22861da177e4SLinus Torvalds 	struct mmu_gather *tlb;
2287ba470de4SRik van Riel 	struct vm_area_struct *vma;
22881da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
2289ee39b37bSHugh Dickins 	unsigned long end;
22901da177e4SLinus Torvalds 
2291d6dd61c8SJeremy Fitzhardinge 	/* mm's last user has gone, and its about to be pulled down */
2292cddb8a5cSAndrea Arcangeli 	mmu_notifier_release(mm);
2293d6dd61c8SJeremy Fitzhardinge 
2294ba470de4SRik van Riel 	if (mm->locked_vm) {
2295ba470de4SRik van Riel 		vma = mm->mmap;
2296ba470de4SRik van Riel 		while (vma) {
2297ba470de4SRik van Riel 			if (vma->vm_flags & VM_LOCKED)
2298ba470de4SRik van Riel 				munlock_vma_pages_all(vma);
2299ba470de4SRik van Riel 			vma = vma->vm_next;
2300ba470de4SRik van Riel 		}
2301ba470de4SRik van Riel 	}
23029480c53eSJeremy Fitzhardinge 
23039480c53eSJeremy Fitzhardinge 	arch_exit_mmap(mm);
23049480c53eSJeremy Fitzhardinge 
2305ba470de4SRik van Riel 	vma = mm->mmap;
23069480c53eSJeremy Fitzhardinge 	if (!vma)	/* Can happen if dup_mmap() received an OOM */
23079480c53eSJeremy Fitzhardinge 		return;
23089480c53eSJeremy Fitzhardinge 
23091da177e4SLinus Torvalds 	lru_add_drain();
23101da177e4SLinus Torvalds 	flush_cache_mm(mm);
2311e0da382cSHugh Dickins 	tlb = tlb_gather_mmu(mm, 1);
2312901608d9SOleg Nesterov 	/* update_hiwater_rss(mm) here? but nobody should be looking */
2313e0da382cSHugh Dickins 	/* Use -1 here to ensure all VMAs in the mm are unmapped */
2314508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
23151da177e4SLinus Torvalds 	vm_unacct_memory(nr_accounted);
23169ba69294SHugh Dickins 
231742b77728SJan Beulich 	free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
2318ee39b37bSHugh Dickins 	tlb_finish_mmu(tlb, 0, end);
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	/*
23218f4f8c16SHugh Dickins 	 * Walk the list again, actually closing and freeing it,
23228f4f8c16SHugh Dickins 	 * with preemption enabled, without holding any MM locks.
23231da177e4SLinus Torvalds 	 */
2324a8fb5618SHugh Dickins 	while (vma)
2325a8fb5618SHugh Dickins 		vma = remove_vma(vma);
2326e0da382cSHugh Dickins 
2327e2cdef8cSHugh Dickins 	BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address
23311da177e4SLinus Torvalds  * and into the inode's i_mmap tree.  If vm_file is non-NULL
23321da177e4SLinus Torvalds  * then i_mmap_lock is taken here.
23331da177e4SLinus Torvalds  */
23341da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
23351da177e4SLinus Torvalds {
23361da177e4SLinus Torvalds 	struct vm_area_struct * __vma, * prev;
23371da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	/*
23401da177e4SLinus Torvalds 	 * The vm_pgoff of a purely anonymous vma should be irrelevant
23411da177e4SLinus Torvalds 	 * until its first write fault, when page's anon_vma and index
23421da177e4SLinus Torvalds 	 * are set.  But now set the vm_pgoff it will almost certainly
23431da177e4SLinus Torvalds 	 * end up with (unless mremap moves it elsewhere before that
23441da177e4SLinus Torvalds 	 * first wfault), so /proc/pid/maps tells a consistent story.
23451da177e4SLinus Torvalds 	 *
23461da177e4SLinus Torvalds 	 * By setting it to reflect the virtual start address of the
23471da177e4SLinus Torvalds 	 * vma, merges and splits can happen in a seamless way, just
23481da177e4SLinus Torvalds 	 * using the existing file pgoff checks and manipulations.
23491da177e4SLinus Torvalds 	 * Similarly in do_mmap_pgoff and in do_brk.
23501da177e4SLinus Torvalds 	 */
23511da177e4SLinus Torvalds 	if (!vma->vm_file) {
23521da177e4SLinus Torvalds 		BUG_ON(vma->anon_vma);
23531da177e4SLinus Torvalds 		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
23541da177e4SLinus Torvalds 	}
23551da177e4SLinus Torvalds 	__vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
23561da177e4SLinus Torvalds 	if (__vma && __vma->vm_start < vma->vm_end)
23571da177e4SLinus Torvalds 		return -ENOMEM;
23582fd4ef85SHugh Dickins 	if ((vma->vm_flags & VM_ACCOUNT) &&
235934b4e4aaSAlan Cox 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
23602fd4ef85SHugh Dickins 		return -ENOMEM;
23611da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
23621da177e4SLinus Torvalds 	return 0;
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds /*
23661da177e4SLinus Torvalds  * Copy the vma structure to a new location in the same mm,
23671da177e4SLinus Torvalds  * prior to moving page table entries, to effect an mremap move.
23681da177e4SLinus Torvalds  */
23691da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
23701da177e4SLinus Torvalds 	unsigned long addr, unsigned long len, pgoff_t pgoff)
23711da177e4SLinus Torvalds {
23721da177e4SLinus Torvalds 	struct vm_area_struct *vma = *vmap;
23731da177e4SLinus Torvalds 	unsigned long vma_start = vma->vm_start;
23741da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
23751da177e4SLinus Torvalds 	struct vm_area_struct *new_vma, *prev;
23761da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
23771da177e4SLinus Torvalds 	struct mempolicy *pol;
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	/*
23801da177e4SLinus Torvalds 	 * If anonymous vma has not yet been faulted, update new pgoff
23811da177e4SLinus Torvalds 	 * to match new location, to increase its chance of merging.
23821da177e4SLinus Torvalds 	 */
23831da177e4SLinus Torvalds 	if (!vma->vm_file && !vma->anon_vma)
23841da177e4SLinus Torvalds 		pgoff = addr >> PAGE_SHIFT;
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
23871da177e4SLinus Torvalds 	new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
23881da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
23891da177e4SLinus Torvalds 	if (new_vma) {
23901da177e4SLinus Torvalds 		/*
23911da177e4SLinus Torvalds 		 * Source vma may have been merged into new_vma
23921da177e4SLinus Torvalds 		 */
23931da177e4SLinus Torvalds 		if (vma_start >= new_vma->vm_start &&
23941da177e4SLinus Torvalds 		    vma_start < new_vma->vm_end)
23951da177e4SLinus Torvalds 			*vmap = new_vma;
23961da177e4SLinus Torvalds 	} else {
2397e94b1766SChristoph Lameter 		new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
23981da177e4SLinus Torvalds 		if (new_vma) {
23991da177e4SLinus Torvalds 			*new_vma = *vma;
2400846a16bfSLee Schermerhorn 			pol = mpol_dup(vma_policy(vma));
24015beb4930SRik van Riel 			if (IS_ERR(pol))
24025beb4930SRik van Riel 				goto out_free_vma;
24035beb4930SRik van Riel 			INIT_LIST_HEAD(&new_vma->anon_vma_chain);
24045beb4930SRik van Riel 			if (anon_vma_clone(new_vma, vma))
24055beb4930SRik van Riel 				goto out_free_mempol;
24061da177e4SLinus Torvalds 			vma_set_policy(new_vma, pol);
24071da177e4SLinus Torvalds 			new_vma->vm_start = addr;
24081da177e4SLinus Torvalds 			new_vma->vm_end = addr + len;
24091da177e4SLinus Torvalds 			new_vma->vm_pgoff = pgoff;
2410925d1c40SMatt Helsley 			if (new_vma->vm_file) {
24111da177e4SLinus Torvalds 				get_file(new_vma->vm_file);
2412925d1c40SMatt Helsley 				if (vma->vm_flags & VM_EXECUTABLE)
2413925d1c40SMatt Helsley 					added_exe_file_vma(mm);
2414925d1c40SMatt Helsley 			}
24151da177e4SLinus Torvalds 			if (new_vma->vm_ops && new_vma->vm_ops->open)
24161da177e4SLinus Torvalds 				new_vma->vm_ops->open(new_vma);
24171da177e4SLinus Torvalds 			vma_link(mm, new_vma, prev, rb_link, rb_parent);
24181da177e4SLinus Torvalds 		}
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 	return new_vma;
24215beb4930SRik van Riel 
24225beb4930SRik van Riel  out_free_mempol:
24235beb4930SRik van Riel 	mpol_put(pol);
24245beb4930SRik van Riel  out_free_vma:
24255beb4930SRik van Riel 	kmem_cache_free(vm_area_cachep, new_vma);
24265beb4930SRik van Riel 	return NULL;
24271da177e4SLinus Torvalds }
2428119f657cSakpm@osdl.org 
2429119f657cSakpm@osdl.org /*
2430119f657cSakpm@osdl.org  * Return true if the calling process may expand its vm space by the passed
2431119f657cSakpm@osdl.org  * number of pages
2432119f657cSakpm@osdl.org  */
2433119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2434119f657cSakpm@osdl.org {
2435119f657cSakpm@osdl.org 	unsigned long cur = mm->total_vm;	/* pages */
2436119f657cSakpm@osdl.org 	unsigned long lim;
2437119f657cSakpm@osdl.org 
243859e99e5bSJiri Slaby 	lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
2439119f657cSakpm@osdl.org 
2440119f657cSakpm@osdl.org 	if (cur + npages > lim)
2441119f657cSakpm@osdl.org 		return 0;
2442119f657cSakpm@osdl.org 	return 1;
2443119f657cSakpm@osdl.org }
2444fa5dc22fSRoland McGrath 
2445fa5dc22fSRoland McGrath 
2446b1d0e4f5SNick Piggin static int special_mapping_fault(struct vm_area_struct *vma,
2447b1d0e4f5SNick Piggin 				struct vm_fault *vmf)
2448fa5dc22fSRoland McGrath {
2449b1d0e4f5SNick Piggin 	pgoff_t pgoff;
2450fa5dc22fSRoland McGrath 	struct page **pages;
2451fa5dc22fSRoland McGrath 
2452b1d0e4f5SNick Piggin 	/*
2453b1d0e4f5SNick Piggin 	 * special mappings have no vm_file, and in that case, the mm
2454b1d0e4f5SNick Piggin 	 * uses vm_pgoff internally. So we have to subtract it from here.
2455b1d0e4f5SNick Piggin 	 * We are allowed to do this because we are the mm; do not copy
2456b1d0e4f5SNick Piggin 	 * this code into drivers!
2457b1d0e4f5SNick Piggin 	 */
2458b1d0e4f5SNick Piggin 	pgoff = vmf->pgoff - vma->vm_pgoff;
2459fa5dc22fSRoland McGrath 
2460b1d0e4f5SNick Piggin 	for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2461b1d0e4f5SNick Piggin 		pgoff--;
2462fa5dc22fSRoland McGrath 
2463fa5dc22fSRoland McGrath 	if (*pages) {
2464fa5dc22fSRoland McGrath 		struct page *page = *pages;
2465fa5dc22fSRoland McGrath 		get_page(page);
2466b1d0e4f5SNick Piggin 		vmf->page = page;
2467b1d0e4f5SNick Piggin 		return 0;
2468fa5dc22fSRoland McGrath 	}
2469fa5dc22fSRoland McGrath 
2470b1d0e4f5SNick Piggin 	return VM_FAULT_SIGBUS;
2471fa5dc22fSRoland McGrath }
2472fa5dc22fSRoland McGrath 
2473fa5dc22fSRoland McGrath /*
2474fa5dc22fSRoland McGrath  * Having a close hook prevents vma merging regardless of flags.
2475fa5dc22fSRoland McGrath  */
2476fa5dc22fSRoland McGrath static void special_mapping_close(struct vm_area_struct *vma)
2477fa5dc22fSRoland McGrath {
2478fa5dc22fSRoland McGrath }
2479fa5dc22fSRoland McGrath 
2480f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct special_mapping_vmops = {
2481fa5dc22fSRoland McGrath 	.close = special_mapping_close,
2482b1d0e4f5SNick Piggin 	.fault = special_mapping_fault,
2483fa5dc22fSRoland McGrath };
2484fa5dc22fSRoland McGrath 
2485fa5dc22fSRoland McGrath /*
2486fa5dc22fSRoland McGrath  * Called with mm->mmap_sem held for writing.
2487fa5dc22fSRoland McGrath  * Insert a new vma covering the given region, with the given flags.
2488fa5dc22fSRoland McGrath  * Its pages are supplied by the given array of struct page *.
2489fa5dc22fSRoland McGrath  * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2490fa5dc22fSRoland McGrath  * The region past the last page supplied will always produce SIGBUS.
2491fa5dc22fSRoland McGrath  * The array pointer and the pages it points to are assumed to stay alive
2492fa5dc22fSRoland McGrath  * for as long as this mapping might exist.
2493fa5dc22fSRoland McGrath  */
2494fa5dc22fSRoland McGrath int install_special_mapping(struct mm_struct *mm,
2495fa5dc22fSRoland McGrath 			    unsigned long addr, unsigned long len,
2496fa5dc22fSRoland McGrath 			    unsigned long vm_flags, struct page **pages)
2497fa5dc22fSRoland McGrath {
2498462e635eSTavis Ormandy 	int ret;
2499fa5dc22fSRoland McGrath 	struct vm_area_struct *vma;
2500fa5dc22fSRoland McGrath 
2501fa5dc22fSRoland McGrath 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2502fa5dc22fSRoland McGrath 	if (unlikely(vma == NULL))
2503fa5dc22fSRoland McGrath 		return -ENOMEM;
2504fa5dc22fSRoland McGrath 
25055beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
2506fa5dc22fSRoland McGrath 	vma->vm_mm = mm;
2507fa5dc22fSRoland McGrath 	vma->vm_start = addr;
2508fa5dc22fSRoland McGrath 	vma->vm_end = addr + len;
2509fa5dc22fSRoland McGrath 
25102f98735cSNick Piggin 	vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
25113ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2512fa5dc22fSRoland McGrath 
2513fa5dc22fSRoland McGrath 	vma->vm_ops = &special_mapping_vmops;
2514fa5dc22fSRoland McGrath 	vma->vm_private_data = pages;
2515fa5dc22fSRoland McGrath 
2516462e635eSTavis Ormandy 	ret = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
2517462e635eSTavis Ormandy 	if (ret)
2518462e635eSTavis Ormandy 		goto out;
2519462e635eSTavis Ormandy 
2520462e635eSTavis Ormandy 	ret = insert_vm_struct(mm, vma);
2521462e635eSTavis Ormandy 	if (ret)
2522462e635eSTavis Ormandy 		goto out;
2523fa5dc22fSRoland McGrath 
2524fa5dc22fSRoland McGrath 	mm->total_vm += len >> PAGE_SHIFT;
2525fa5dc22fSRoland McGrath 
2526cdd6c482SIngo Molnar 	perf_event_mmap(vma);
2527089dd79dSPeter Zijlstra 
2528fa5dc22fSRoland McGrath 	return 0;
2529462e635eSTavis Ormandy 
2530462e635eSTavis Ormandy out:
2531462e635eSTavis Ormandy 	kmem_cache_free(vm_area_cachep, vma);
2532462e635eSTavis Ormandy 	return ret;
2533fa5dc22fSRoland McGrath }
25347906d00cSAndrea Arcangeli 
25357906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex);
25367906d00cSAndrea Arcangeli 
2537454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
25387906d00cSAndrea Arcangeli {
2539012f1800SRik van Riel 	if (!test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
25407906d00cSAndrea Arcangeli 		/*
25417906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change from under us
25427906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
25437906d00cSAndrea Arcangeli 		 */
2544012f1800SRik van Riel 		spin_lock_nest_lock(&anon_vma->root->lock, &mm->mmap_sem);
25457906d00cSAndrea Arcangeli 		/*
25467906d00cSAndrea Arcangeli 		 * We can safely modify head.next after taking the
2547012f1800SRik van Riel 		 * anon_vma->root->lock. If some other vma in this mm shares
25487906d00cSAndrea Arcangeli 		 * the same anon_vma we won't take it again.
25497906d00cSAndrea Arcangeli 		 *
25507906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
25517906d00cSAndrea Arcangeli 		 * can't change from under us thanks to the
2552012f1800SRik van Riel 		 * anon_vma->root->lock.
25537906d00cSAndrea Arcangeli 		 */
25547906d00cSAndrea Arcangeli 		if (__test_and_set_bit(0, (unsigned long *)
2555012f1800SRik van Riel 				       &anon_vma->root->head.next))
25567906d00cSAndrea Arcangeli 			BUG();
25577906d00cSAndrea Arcangeli 	}
25587906d00cSAndrea Arcangeli }
25597906d00cSAndrea Arcangeli 
2560454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
25617906d00cSAndrea Arcangeli {
25627906d00cSAndrea Arcangeli 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
25637906d00cSAndrea Arcangeli 		/*
25647906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change from under us because
25657906d00cSAndrea Arcangeli 		 * we hold the mm_all_locks_mutex.
25667906d00cSAndrea Arcangeli 		 *
25677906d00cSAndrea Arcangeli 		 * Operations on ->flags have to be atomic because
25687906d00cSAndrea Arcangeli 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
25697906d00cSAndrea Arcangeli 		 * mm_all_locks_mutex, there may be other cpus
25707906d00cSAndrea Arcangeli 		 * changing other bitflags in parallel to us.
25717906d00cSAndrea Arcangeli 		 */
25727906d00cSAndrea Arcangeli 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
25737906d00cSAndrea Arcangeli 			BUG();
2574454ed842SPeter Zijlstra 		spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
25757906d00cSAndrea Arcangeli 	}
25767906d00cSAndrea Arcangeli }
25777906d00cSAndrea Arcangeli 
25787906d00cSAndrea Arcangeli /*
25797906d00cSAndrea Arcangeli  * This operation locks against the VM for all pte/vma/mm related
25807906d00cSAndrea Arcangeli  * operations that could ever happen on a certain mm. This includes
25817906d00cSAndrea Arcangeli  * vmtruncate, try_to_unmap, and all page faults.
25827906d00cSAndrea Arcangeli  *
25837906d00cSAndrea Arcangeli  * The caller must take the mmap_sem in write mode before calling
25847906d00cSAndrea Arcangeli  * mm_take_all_locks(). The caller isn't allowed to release the
25857906d00cSAndrea Arcangeli  * mmap_sem until mm_drop_all_locks() returns.
25867906d00cSAndrea Arcangeli  *
25877906d00cSAndrea Arcangeli  * mmap_sem in write mode is required in order to block all operations
25887906d00cSAndrea Arcangeli  * that could modify pagetables and free pages without need of
25897906d00cSAndrea Arcangeli  * altering the vma layout (for example populate_range() with
25907906d00cSAndrea Arcangeli  * nonlinear vmas). It's also needed in write mode to avoid new
25917906d00cSAndrea Arcangeli  * anon_vmas to be associated with existing vmas.
25927906d00cSAndrea Arcangeli  *
25937906d00cSAndrea Arcangeli  * A single task can't take more than one mm_take_all_locks() in a row
25947906d00cSAndrea Arcangeli  * or it would deadlock.
25957906d00cSAndrea Arcangeli  *
25967906d00cSAndrea Arcangeli  * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
25977906d00cSAndrea Arcangeli  * mapping->flags avoid to take the same lock twice, if more than one
25987906d00cSAndrea Arcangeli  * vma in this mm is backed by the same anon_vma or address_space.
25997906d00cSAndrea Arcangeli  *
26007906d00cSAndrea Arcangeli  * We can take all the locks in random order because the VM code
26017906d00cSAndrea Arcangeli  * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
26027906d00cSAndrea Arcangeli  * takes more than one of them in a row. Secondly we're protected
26037906d00cSAndrea Arcangeli  * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
26047906d00cSAndrea Arcangeli  *
26057906d00cSAndrea Arcangeli  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
26067906d00cSAndrea Arcangeli  * that may have to take thousand of locks.
26077906d00cSAndrea Arcangeli  *
26087906d00cSAndrea Arcangeli  * mm_take_all_locks() can fail if it's interrupted by signals.
26097906d00cSAndrea Arcangeli  */
26107906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm)
26117906d00cSAndrea Arcangeli {
26127906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
26135beb4930SRik van Riel 	struct anon_vma_chain *avc;
26147906d00cSAndrea Arcangeli 	int ret = -EINTR;
26157906d00cSAndrea Arcangeli 
26167906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
26177906d00cSAndrea Arcangeli 
26187906d00cSAndrea Arcangeli 	mutex_lock(&mm_all_locks_mutex);
26197906d00cSAndrea Arcangeli 
26207906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
26217906d00cSAndrea Arcangeli 		if (signal_pending(current))
26227906d00cSAndrea Arcangeli 			goto out_unlock;
26237906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
2624454ed842SPeter Zijlstra 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
26257906d00cSAndrea Arcangeli 	}
26267cd5a02fSPeter Zijlstra 
26277cd5a02fSPeter Zijlstra 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
26287cd5a02fSPeter Zijlstra 		if (signal_pending(current))
26297cd5a02fSPeter Zijlstra 			goto out_unlock;
26307cd5a02fSPeter Zijlstra 		if (vma->anon_vma)
26315beb4930SRik van Riel 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
26325beb4930SRik van Riel 				vm_lock_anon_vma(mm, avc->anon_vma);
26337cd5a02fSPeter Zijlstra 	}
26347cd5a02fSPeter Zijlstra 
26357906d00cSAndrea Arcangeli 	ret = 0;
26367906d00cSAndrea Arcangeli 
26377906d00cSAndrea Arcangeli out_unlock:
26387906d00cSAndrea Arcangeli 	if (ret)
26397906d00cSAndrea Arcangeli 		mm_drop_all_locks(mm);
26407906d00cSAndrea Arcangeli 
26417906d00cSAndrea Arcangeli 	return ret;
26427906d00cSAndrea Arcangeli }
26437906d00cSAndrea Arcangeli 
26447906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
26457906d00cSAndrea Arcangeli {
2646012f1800SRik van Riel 	if (test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
26477906d00cSAndrea Arcangeli 		/*
26487906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change to 0 from under
26497906d00cSAndrea Arcangeli 		 * us because we hold the mm_all_locks_mutex.
26507906d00cSAndrea Arcangeli 		 *
26517906d00cSAndrea Arcangeli 		 * We must however clear the bitflag before unlocking
26527906d00cSAndrea Arcangeli 		 * the vma so the users using the anon_vma->head will
26537906d00cSAndrea Arcangeli 		 * never see our bitflag.
26547906d00cSAndrea Arcangeli 		 *
26557906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
26567906d00cSAndrea Arcangeli 		 * can't change from under us until we release the
2657012f1800SRik van Riel 		 * anon_vma->root->lock.
26587906d00cSAndrea Arcangeli 		 */
26597906d00cSAndrea Arcangeli 		if (!__test_and_clear_bit(0, (unsigned long *)
2660012f1800SRik van Riel 					  &anon_vma->root->head.next))
26617906d00cSAndrea Arcangeli 			BUG();
2662cba48b98SRik van Riel 		anon_vma_unlock(anon_vma);
26637906d00cSAndrea Arcangeli 	}
26647906d00cSAndrea Arcangeli }
26657906d00cSAndrea Arcangeli 
26667906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping)
26677906d00cSAndrea Arcangeli {
26687906d00cSAndrea Arcangeli 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
26697906d00cSAndrea Arcangeli 		/*
26707906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
26717906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
26727906d00cSAndrea Arcangeli 		 */
26737906d00cSAndrea Arcangeli 		spin_unlock(&mapping->i_mmap_lock);
26747906d00cSAndrea Arcangeli 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
26757906d00cSAndrea Arcangeli 					&mapping->flags))
26767906d00cSAndrea Arcangeli 			BUG();
26777906d00cSAndrea Arcangeli 	}
26787906d00cSAndrea Arcangeli }
26797906d00cSAndrea Arcangeli 
26807906d00cSAndrea Arcangeli /*
26817906d00cSAndrea Arcangeli  * The mmap_sem cannot be released by the caller until
26827906d00cSAndrea Arcangeli  * mm_drop_all_locks() returns.
26837906d00cSAndrea Arcangeli  */
26847906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm)
26857906d00cSAndrea Arcangeli {
26867906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
26875beb4930SRik van Riel 	struct anon_vma_chain *avc;
26887906d00cSAndrea Arcangeli 
26897906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
26907906d00cSAndrea Arcangeli 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
26917906d00cSAndrea Arcangeli 
26927906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
26937906d00cSAndrea Arcangeli 		if (vma->anon_vma)
26945beb4930SRik van Riel 			list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
26955beb4930SRik van Riel 				vm_unlock_anon_vma(avc->anon_vma);
26967906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
26977906d00cSAndrea Arcangeli 			vm_unlock_mapping(vma->vm_file->f_mapping);
26987906d00cSAndrea Arcangeli 	}
26997906d00cSAndrea Arcangeli 
27007906d00cSAndrea Arcangeli 	mutex_unlock(&mm_all_locks_mutex);
27017906d00cSAndrea Arcangeli }
27028feae131SDavid Howells 
27038feae131SDavid Howells /*
27048feae131SDavid Howells  * initialise the VMA slab
27058feae131SDavid Howells  */
27068feae131SDavid Howells void __init mmap_init(void)
27078feae131SDavid Howells {
270800a62ce9SKOSAKI Motohiro 	int ret;
270900a62ce9SKOSAKI Motohiro 
271000a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
271100a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
27128feae131SDavid Howells }
2713