xref: /linux/mm/mmap.c (revision 454ed842d55740160334efc9ad56cfef54ed37bc)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * mm/mmap.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Written by obz.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Address space accounting code	<alan@redhat.com>
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>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/uaccess.h>
321da177e4SLinus Torvalds #include <asm/cacheflush.h>
331da177e4SLinus Torvalds #include <asm/tlb.h>
34d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h>
351da177e4SLinus Torvalds 
3642b77728SJan Beulich #include "internal.h"
3742b77728SJan Beulich 
383a459756SKirill Korotaev #ifndef arch_mmap_check
393a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags)	(0)
403a459756SKirill Korotaev #endif
413a459756SKirill Korotaev 
4208e7d9b5SMartin Schwidefsky #ifndef arch_rebalance_pgtables
4308e7d9b5SMartin Schwidefsky #define arch_rebalance_pgtables(addr, len)		(addr)
4408e7d9b5SMartin Schwidefsky #endif
4508e7d9b5SMartin Schwidefsky 
46e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm,
47e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
48e0da382cSHugh Dickins 		unsigned long start, unsigned long end);
49e0da382cSHugh Dickins 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * WARNING: the debugging will use recursive algorithms so never enable this
521da177e4SLinus Torvalds  * unless you know what you are doing.
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds #undef DEBUG_MM_RB
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation.
571da177e4SLinus Torvalds  * this is due to the limited x86 page protection hardware.  The expected
581da177e4SLinus Torvalds  * behavior is in parens:
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * map_type	prot
611da177e4SLinus Torvalds  *		PROT_NONE	PROT_READ	PROT_WRITE	PROT_EXEC
621da177e4SLinus Torvalds  * MAP_SHARED	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
631da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (yes) yes	w: (no) no
641da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  * MAP_PRIVATE	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
671da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (copy) copy	w: (no) no
681da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds pgprot_t protection_map[16] = {
721da177e4SLinus Torvalds 	__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
731da177e4SLinus Torvalds 	__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
741da177e4SLinus Torvalds };
751da177e4SLinus Torvalds 
76804af2cfSHugh Dickins pgprot_t vm_get_page_prot(unsigned long vm_flags)
77804af2cfSHugh Dickins {
78b845f313SDave Kleikamp 	return __pgprot(pgprot_val(protection_map[vm_flags &
79b845f313SDave Kleikamp 				(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
80b845f313SDave Kleikamp 			pgprot_val(arch_vm_get_page_prot(vm_flags)));
81804af2cfSHugh Dickins }
82804af2cfSHugh Dickins EXPORT_SYMBOL(vm_get_page_prot);
83804af2cfSHugh Dickins 
841da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS;  /* heuristic overcommit */
851da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50;	/* default is 50% */
86c3d8c141SChristoph Lameter int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
8780119ef5SAlan Cox atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /*
901da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
911da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
921da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
951da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
981da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
1031da177e4SLinus Torvalds  * wish to use this logic.
1041da177e4SLinus Torvalds  */
10534b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	unsigned long free, allowed;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	vm_acct_memory(pages);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	/*
1121da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
1131da177e4SLinus Torvalds 	 */
1141da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1151da177e4SLinus Torvalds 		return 0;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1181da177e4SLinus Torvalds 		unsigned long n;
1191da177e4SLinus Torvalds 
120347ce434SChristoph Lameter 		free = global_page_state(NR_FILE_PAGES);
1211da177e4SLinus Torvalds 		free += nr_swap_pages;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 		/*
1241da177e4SLinus Torvalds 		 * Any slabs which are created with the
1251da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1261da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
1271da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
1281da177e4SLinus Torvalds 		 */
129972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 		/*
1321da177e4SLinus Torvalds 		 * Leave the last 3% for root
1331da177e4SLinus Torvalds 		 */
1341da177e4SLinus Torvalds 		if (!cap_sys_admin)
1351da177e4SLinus Torvalds 			free -= free / 32;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 		if (free > pages)
1381da177e4SLinus Torvalds 			return 0;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 		/*
1411da177e4SLinus Torvalds 		 * nr_free_pages() is very expensive on large systems,
1421da177e4SLinus Torvalds 		 * only call if we're about to fail.
1431da177e4SLinus Torvalds 		 */
1441da177e4SLinus Torvalds 		n = nr_free_pages();
1456d9f7839SHideo AOKI 
1466d9f7839SHideo AOKI 		/*
1476d9f7839SHideo AOKI 		 * Leave reserved pages. The pages are not for anonymous pages.
1486d9f7839SHideo AOKI 		 */
1496d9f7839SHideo AOKI 		if (n <= totalreserve_pages)
1506d9f7839SHideo AOKI 			goto error;
1516d9f7839SHideo AOKI 		else
1526d9f7839SHideo AOKI 			n -= totalreserve_pages;
1536d9f7839SHideo AOKI 
1546d9f7839SHideo AOKI 		/*
1556d9f7839SHideo AOKI 		 * Leave the last 3% for root
1566d9f7839SHideo AOKI 		 */
1571da177e4SLinus Torvalds 		if (!cap_sys_admin)
1581da177e4SLinus Torvalds 			n -= n / 32;
1591da177e4SLinus Torvalds 		free += n;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 		if (free > pages)
1621da177e4SLinus Torvalds 			return 0;
1636d9f7839SHideo AOKI 
1646d9f7839SHideo AOKI 		goto error;
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	allowed = (totalram_pages - hugetlb_total_pages())
1681da177e4SLinus Torvalds 	       	* sysctl_overcommit_ratio / 100;
1691da177e4SLinus Torvalds 	/*
1701da177e4SLinus Torvalds 	 * Leave the last 3% for root
1711da177e4SLinus Torvalds 	 */
1721da177e4SLinus Torvalds 	if (!cap_sys_admin)
1731da177e4SLinus Torvalds 		allowed -= allowed / 32;
1741da177e4SLinus Torvalds 	allowed += total_swap_pages;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
1771da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
17834b4e4aaSAlan Cox 	allowed -= mm->total_vm / 32;
1791da177e4SLinus Torvalds 
1802f60f8d3SSimon Derr 	/*
1812f60f8d3SSimon Derr 	 * cast `allowed' as a signed long because vm_committed_space
1822f60f8d3SSimon Derr 	 * sometimes has a negative value
1832f60f8d3SSimon Derr 	 */
18480119ef5SAlan Cox 	if (atomic_long_read(&vm_committed_space) < (long)allowed)
1851da177e4SLinus Torvalds 		return 0;
1866d9f7839SHideo AOKI error:
1871da177e4SLinus Torvalds 	vm_unacct_memory(pages);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	return -ENOMEM;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  * Requires inode->i_mapping->i_mmap_lock
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma,
1961da177e4SLinus Torvalds 		struct file *file, struct address_space *mapping)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	if (vma->vm_flags & VM_DENYWRITE)
199d3ac7f89SJosef "Jeff" Sipek 		atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
2001da177e4SLinus Torvalds 	if (vma->vm_flags & VM_SHARED)
2011da177e4SLinus Torvalds 		mapping->i_mmap_writable--;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	flush_dcache_mmap_lock(mapping);
2041da177e4SLinus Torvalds 	if (unlikely(vma->vm_flags & VM_NONLINEAR))
2051da177e4SLinus Torvalds 		list_del_init(&vma->shared.vm_set.list);
2061da177e4SLinus Torvalds 	else
2071da177e4SLinus Torvalds 		vma_prio_tree_remove(vma, &mapping->i_mmap);
2081da177e4SLinus Torvalds 	flush_dcache_mmap_unlock(mapping);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /*
212a8fb5618SHugh Dickins  * Unlink a file-based vm structure from its prio_tree, to hide
213a8fb5618SHugh Dickins  * vma from rmap and vmtruncate before freeing its page tables.
2141da177e4SLinus Torvalds  */
215a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (file) {
2201da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
2211da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
2221da177e4SLinus Torvalds 		__remove_shared_vm_struct(vma, file, mapping);
2231da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
2241da177e4SLinus Torvalds 	}
225a8fb5618SHugh Dickins }
226a8fb5618SHugh Dickins 
227a8fb5618SHugh Dickins /*
228a8fb5618SHugh Dickins  * Close a vm structure and free it, returning the next.
229a8fb5618SHugh Dickins  */
230a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
231a8fb5618SHugh Dickins {
232a8fb5618SHugh Dickins 	struct vm_area_struct *next = vma->vm_next;
233a8fb5618SHugh Dickins 
234a8fb5618SHugh Dickins 	might_sleep();
2351da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
2361da177e4SLinus Torvalds 		vma->vm_ops->close(vma);
237925d1c40SMatt Helsley 	if (vma->vm_file) {
238a8fb5618SHugh Dickins 		fput(vma->vm_file);
239925d1c40SMatt Helsley 		if (vma->vm_flags & VM_EXECUTABLE)
240925d1c40SMatt Helsley 			removed_exe_file_vma(vma->vm_mm);
241925d1c40SMatt Helsley 	}
242f0be3d32SLee Schermerhorn 	mpol_put(vma_policy(vma));
2431da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
244a8fb5618SHugh Dickins 	return next;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds asmlinkage unsigned long sys_brk(unsigned long brk)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	unsigned long rlim, retval;
2501da177e4SLinus Torvalds 	unsigned long newbrk, oldbrk;
2511da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
252a5b4592cSJiri Kosina 	unsigned long min_brk;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
2551da177e4SLinus Torvalds 
256a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK
257a5b4592cSJiri Kosina 	min_brk = mm->end_code;
258a5b4592cSJiri Kosina #else
259a5b4592cSJiri Kosina 	min_brk = mm->start_brk;
260a5b4592cSJiri Kosina #endif
261a5b4592cSJiri Kosina 	if (brk < min_brk)
2621da177e4SLinus Torvalds 		goto out;
2631e624196SRam Gupta 
2641e624196SRam Gupta 	/*
2651e624196SRam Gupta 	 * Check against rlimit here. If this check is done later after the test
2661e624196SRam Gupta 	 * of oldbrk with newbrk then it can escape the test and let the data
2671e624196SRam Gupta 	 * segment grow beyond its set limit the in case where the limit is
2681e624196SRam Gupta 	 * not page aligned -Ram Gupta
2691e624196SRam Gupta 	 */
2701e624196SRam Gupta 	rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
271c1d171a0SJiri Kosina 	if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
272c1d171a0SJiri Kosina 			(mm->end_data - mm->start_data) > rlim)
2731e624196SRam Gupta 		goto out;
2741e624196SRam Gupta 
2751da177e4SLinus Torvalds 	newbrk = PAGE_ALIGN(brk);
2761da177e4SLinus Torvalds 	oldbrk = PAGE_ALIGN(mm->brk);
2771da177e4SLinus Torvalds 	if (oldbrk == newbrk)
2781da177e4SLinus Torvalds 		goto set_brk;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* Always allow shrinking brk. */
2811da177e4SLinus Torvalds 	if (brk <= mm->brk) {
2821da177e4SLinus Torvalds 		if (!do_munmap(mm, newbrk, oldbrk-newbrk))
2831da177e4SLinus Torvalds 			goto set_brk;
2841da177e4SLinus Torvalds 		goto out;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	/* Check against existing mmap mappings. */
2881da177e4SLinus Torvalds 	if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
2891da177e4SLinus Torvalds 		goto out;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* Ok, looks good - let it rip. */
2921da177e4SLinus Torvalds 	if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
2931da177e4SLinus Torvalds 		goto out;
2941da177e4SLinus Torvalds set_brk:
2951da177e4SLinus Torvalds 	mm->brk = brk;
2961da177e4SLinus Torvalds out:
2971da177e4SLinus Torvalds 	retval = mm->brk;
2981da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
2991da177e4SLinus Torvalds 	return retval;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds #ifdef DEBUG_MM_RB
3031da177e4SLinus Torvalds static int browse_rb(struct rb_root *root)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	int i = 0, j;
3061da177e4SLinus Torvalds 	struct rb_node *nd, *pn = NULL;
3071da177e4SLinus Torvalds 	unsigned long prev = 0, pend = 0;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
3101da177e4SLinus Torvalds 		struct vm_area_struct *vma;
3111da177e4SLinus Torvalds 		vma = rb_entry(nd, struct vm_area_struct, vm_rb);
3121da177e4SLinus Torvalds 		if (vma->vm_start < prev)
3131da177e4SLinus Torvalds 			printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
3141da177e4SLinus Torvalds 		if (vma->vm_start < pend)
3151da177e4SLinus Torvalds 			printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
3161da177e4SLinus Torvalds 		if (vma->vm_start > vma->vm_end)
3171da177e4SLinus Torvalds 			printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
3181da177e4SLinus Torvalds 		i++;
3191da177e4SLinus Torvalds 		pn = nd;
320d1af65d1SDavid Miller 		prev = vma->vm_start;
321d1af65d1SDavid Miller 		pend = vma->vm_end;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 	j = 0;
3241da177e4SLinus Torvalds 	for (nd = pn; nd; nd = rb_prev(nd)) {
3251da177e4SLinus Torvalds 		j++;
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 	if (i != j)
3281da177e4SLinus Torvalds 		printk("backwards %d, forwards %d\n", j, i), i = 0;
3291da177e4SLinus Torvalds 	return i;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds void validate_mm(struct mm_struct *mm)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	int bug = 0;
3351da177e4SLinus Torvalds 	int i = 0;
3361da177e4SLinus Torvalds 	struct vm_area_struct *tmp = mm->mmap;
3371da177e4SLinus Torvalds 	while (tmp) {
3381da177e4SLinus Torvalds 		tmp = tmp->vm_next;
3391da177e4SLinus Torvalds 		i++;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 	if (i != mm->map_count)
3421da177e4SLinus Torvalds 		printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
3431da177e4SLinus Torvalds 	i = browse_rb(&mm->mm_rb);
3441da177e4SLinus Torvalds 	if (i != mm->map_count)
3451da177e4SLinus Torvalds 		printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
34646a350efSEric Sesterhenn 	BUG_ON(bug);
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds #else
3491da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0)
3501da177e4SLinus Torvalds #endif
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static struct vm_area_struct *
3531da177e4SLinus Torvalds find_vma_prepare(struct mm_struct *mm, unsigned long addr,
3541da177e4SLinus Torvalds 		struct vm_area_struct **pprev, struct rb_node ***rb_link,
3551da177e4SLinus Torvalds 		struct rb_node ** rb_parent)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	struct vm_area_struct * vma;
3581da177e4SLinus Torvalds 	struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	__rb_link = &mm->mm_rb.rb_node;
3611da177e4SLinus Torvalds 	rb_prev = __rb_parent = NULL;
3621da177e4SLinus Torvalds 	vma = NULL;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	while (*__rb_link) {
3651da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		__rb_parent = *__rb_link;
3681da177e4SLinus Torvalds 		vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		if (vma_tmp->vm_end > addr) {
3711da177e4SLinus Torvalds 			vma = vma_tmp;
3721da177e4SLinus Torvalds 			if (vma_tmp->vm_start <= addr)
3731da177e4SLinus Torvalds 				return vma;
3741da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_left;
3751da177e4SLinus Torvalds 		} else {
3761da177e4SLinus Torvalds 			rb_prev = __rb_parent;
3771da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_right;
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	*pprev = NULL;
3821da177e4SLinus Torvalds 	if (rb_prev)
3831da177e4SLinus Torvalds 		*pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
3841da177e4SLinus Torvalds 	*rb_link = __rb_link;
3851da177e4SLinus Torvalds 	*rb_parent = __rb_parent;
3861da177e4SLinus Torvalds 	return vma;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static inline void
3901da177e4SLinus Torvalds __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
3911da177e4SLinus Torvalds 		struct vm_area_struct *prev, struct rb_node *rb_parent)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	if (prev) {
3941da177e4SLinus Torvalds 		vma->vm_next = prev->vm_next;
3951da177e4SLinus Torvalds 		prev->vm_next = vma;
3961da177e4SLinus Torvalds 	} else {
3971da177e4SLinus Torvalds 		mm->mmap = vma;
3981da177e4SLinus Torvalds 		if (rb_parent)
3991da177e4SLinus Torvalds 			vma->vm_next = rb_entry(rb_parent,
4001da177e4SLinus Torvalds 					struct vm_area_struct, vm_rb);
4011da177e4SLinus Torvalds 		else
4021da177e4SLinus Torvalds 			vma->vm_next = NULL;
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
4071da177e4SLinus Torvalds 		struct rb_node **rb_link, struct rb_node *rb_parent)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	rb_link_node(&vma->vm_rb, rb_parent, rb_link);
4101da177e4SLinus Torvalds 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static inline void __vma_link_file(struct vm_area_struct *vma)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct file * file;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	file = vma->vm_file;
4181da177e4SLinus Torvalds 	if (file) {
4191da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		if (vma->vm_flags & VM_DENYWRITE)
422d3ac7f89SJosef "Jeff" Sipek 			atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
4231da177e4SLinus Torvalds 		if (vma->vm_flags & VM_SHARED)
4241da177e4SLinus Torvalds 			mapping->i_mmap_writable++;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
4271da177e4SLinus Torvalds 		if (unlikely(vma->vm_flags & VM_NONLINEAR))
4281da177e4SLinus Torvalds 			vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
4291da177e4SLinus Torvalds 		else
4301da177e4SLinus Torvalds 			vma_prio_tree_insert(vma, &mapping->i_mmap);
4311da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static void
4361da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4371da177e4SLinus Torvalds 	struct vm_area_struct *prev, struct rb_node **rb_link,
4381da177e4SLinus Torvalds 	struct rb_node *rb_parent)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	__vma_link_list(mm, vma, prev, rb_parent);
4411da177e4SLinus Torvalds 	__vma_link_rb(mm, vma, rb_link, rb_parent);
4421da177e4SLinus Torvalds 	__anon_vma_link(vma);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4461da177e4SLinus Torvalds 			struct vm_area_struct *prev, struct rb_node **rb_link,
4471da177e4SLinus Torvalds 			struct rb_node *rb_parent)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	if (vma->vm_file)
4521da177e4SLinus Torvalds 		mapping = vma->vm_file->f_mapping;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	if (mapping) {
4551da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
4561da177e4SLinus Torvalds 		vma->vm_truncate_count = mapping->truncate_count;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 	anon_vma_lock(vma);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4611da177e4SLinus Torvalds 	__vma_link_file(vma);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	anon_vma_unlock(vma);
4641da177e4SLinus Torvalds 	if (mapping)
4651da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	mm->map_count++;
4681da177e4SLinus Torvalds 	validate_mm(mm);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /*
4721da177e4SLinus Torvalds  * Helper for vma_adjust in the split_vma insert case:
4731da177e4SLinus Torvalds  * insert vm structure into list and rbtree and anon_vma,
4741da177e4SLinus Torvalds  * but it has already been inserted into prio_tree earlier.
4751da177e4SLinus Torvalds  */
4761da177e4SLinus Torvalds static void
4771da177e4SLinus Torvalds __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	struct vm_area_struct * __vma, * prev;
4801da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	__vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
48346a350efSEric Sesterhenn 	BUG_ON(__vma && __vma->vm_start < vma->vm_end);
4841da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4851da177e4SLinus Torvalds 	mm->map_count++;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static inline void
4891da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
4901da177e4SLinus Torvalds 		struct vm_area_struct *prev)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	prev->vm_next = vma->vm_next;
4931da177e4SLinus Torvalds 	rb_erase(&vma->vm_rb, &mm->mm_rb);
4941da177e4SLinus Torvalds 	if (mm->mmap_cache == vma)
4951da177e4SLinus Torvalds 		mm->mmap_cache = prev;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /*
4991da177e4SLinus Torvalds  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
5001da177e4SLinus Torvalds  * is already present in an i_mmap tree without adjusting the tree.
5011da177e4SLinus Torvalds  * The following helper function should be used when such adjustments
5021da177e4SLinus Torvalds  * are necessary.  The "insert" vma (if any) is to be inserted
5031da177e4SLinus Torvalds  * before we drop the necessary locks.
5041da177e4SLinus Torvalds  */
5051da177e4SLinus Torvalds void vma_adjust(struct vm_area_struct *vma, unsigned long start,
5061da177e4SLinus Torvalds 	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
5091da177e4SLinus Torvalds 	struct vm_area_struct *next = vma->vm_next;
5101da177e4SLinus Torvalds 	struct vm_area_struct *importer = NULL;
5111da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
5121da177e4SLinus Torvalds 	struct prio_tree_root *root = NULL;
5131da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
5141da177e4SLinus Torvalds 	struct anon_vma *anon_vma = NULL;
5151da177e4SLinus Torvalds 	long adjust_next = 0;
5161da177e4SLinus Torvalds 	int remove_next = 0;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (next && !insert) {
5191da177e4SLinus Torvalds 		if (end >= next->vm_end) {
5201da177e4SLinus Torvalds 			/*
5211da177e4SLinus Torvalds 			 * vma expands, overlapping all the next, and
5221da177e4SLinus Torvalds 			 * perhaps the one after too (mprotect case 6).
5231da177e4SLinus Torvalds 			 */
5241da177e4SLinus Torvalds again:			remove_next = 1 + (end > next->vm_end);
5251da177e4SLinus Torvalds 			end = next->vm_end;
5261da177e4SLinus Torvalds 			anon_vma = next->anon_vma;
5271da177e4SLinus Torvalds 			importer = vma;
5281da177e4SLinus Torvalds 		} else if (end > next->vm_start) {
5291da177e4SLinus Torvalds 			/*
5301da177e4SLinus Torvalds 			 * vma expands, overlapping part of the next:
5311da177e4SLinus Torvalds 			 * mprotect case 5 shifting the boundary up.
5321da177e4SLinus Torvalds 			 */
5331da177e4SLinus Torvalds 			adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
5341da177e4SLinus Torvalds 			anon_vma = next->anon_vma;
5351da177e4SLinus Torvalds 			importer = vma;
5361da177e4SLinus Torvalds 		} else if (end < vma->vm_end) {
5371da177e4SLinus Torvalds 			/*
5381da177e4SLinus Torvalds 			 * vma shrinks, and !insert tells it's not
5391da177e4SLinus Torvalds 			 * split_vma inserting another: so it must be
5401da177e4SLinus Torvalds 			 * mprotect case 4 shifting the boundary down.
5411da177e4SLinus Torvalds 			 */
5421da177e4SLinus Torvalds 			adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
5431da177e4SLinus Torvalds 			anon_vma = next->anon_vma;
5441da177e4SLinus Torvalds 			importer = next;
5451da177e4SLinus Torvalds 		}
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	if (file) {
5491da177e4SLinus Torvalds 		mapping = file->f_mapping;
5501da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_NONLINEAR))
5511da177e4SLinus Torvalds 			root = &mapping->i_mmap;
5521da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
5531da177e4SLinus Torvalds 		if (importer &&
5541da177e4SLinus Torvalds 		    vma->vm_truncate_count != next->vm_truncate_count) {
5551da177e4SLinus Torvalds 			/*
5561da177e4SLinus Torvalds 			 * unmap_mapping_range might be in progress:
5571da177e4SLinus Torvalds 			 * ensure that the expanding vma is rescanned.
5581da177e4SLinus Torvalds 			 */
5591da177e4SLinus Torvalds 			importer->vm_truncate_count = 0;
5601da177e4SLinus Torvalds 		}
5611da177e4SLinus Torvalds 		if (insert) {
5621da177e4SLinus Torvalds 			insert->vm_truncate_count = vma->vm_truncate_count;
5631da177e4SLinus Torvalds 			/*
5641da177e4SLinus Torvalds 			 * Put into prio_tree now, so instantiated pages
5651da177e4SLinus Torvalds 			 * are visible to arm/parisc __flush_dcache_page
5661da177e4SLinus Torvalds 			 * throughout; but we cannot insert into address
5671da177e4SLinus Torvalds 			 * space until vma start or end is updated.
5681da177e4SLinus Torvalds 			 */
5691da177e4SLinus Torvalds 			__vma_link_file(insert);
5701da177e4SLinus Torvalds 		}
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	/*
5741da177e4SLinus Torvalds 	 * When changing only vma->vm_end, we don't really need
5751da177e4SLinus Torvalds 	 * anon_vma lock: but is that case worth optimizing out?
5761da177e4SLinus Torvalds 	 */
5771da177e4SLinus Torvalds 	if (vma->anon_vma)
5781da177e4SLinus Torvalds 		anon_vma = vma->anon_vma;
5791da177e4SLinus Torvalds 	if (anon_vma) {
5801da177e4SLinus Torvalds 		spin_lock(&anon_vma->lock);
5811da177e4SLinus Torvalds 		/*
5821da177e4SLinus Torvalds 		 * Easily overlooked: when mprotect shifts the boundary,
5831da177e4SLinus Torvalds 		 * make sure the expanding vma has anon_vma set if the
5841da177e4SLinus Torvalds 		 * shrinking vma had, to cover any anon pages imported.
5851da177e4SLinus Torvalds 		 */
5861da177e4SLinus Torvalds 		if (importer && !importer->anon_vma) {
5871da177e4SLinus Torvalds 			importer->anon_vma = anon_vma;
5881da177e4SLinus Torvalds 			__anon_vma_link(importer);
5891da177e4SLinus Torvalds 		}
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (root) {
5931da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
5941da177e4SLinus Torvalds 		vma_prio_tree_remove(vma, root);
5951da177e4SLinus Torvalds 		if (adjust_next)
5961da177e4SLinus Torvalds 			vma_prio_tree_remove(next, root);
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	vma->vm_start = start;
6001da177e4SLinus Torvalds 	vma->vm_end = end;
6011da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
6021da177e4SLinus Torvalds 	if (adjust_next) {
6031da177e4SLinus Torvalds 		next->vm_start += adjust_next << PAGE_SHIFT;
6041da177e4SLinus Torvalds 		next->vm_pgoff += adjust_next;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (root) {
6081da177e4SLinus Torvalds 		if (adjust_next)
6091da177e4SLinus Torvalds 			vma_prio_tree_insert(next, root);
6101da177e4SLinus Torvalds 		vma_prio_tree_insert(vma, root);
6111da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (remove_next) {
6151da177e4SLinus Torvalds 		/*
6161da177e4SLinus Torvalds 		 * vma_merge has merged next into vma, and needs
6171da177e4SLinus Torvalds 		 * us to remove next before dropping the locks.
6181da177e4SLinus Torvalds 		 */
6191da177e4SLinus Torvalds 		__vma_unlink(mm, next, vma);
6201da177e4SLinus Torvalds 		if (file)
6211da177e4SLinus Torvalds 			__remove_shared_vm_struct(next, file, mapping);
6221da177e4SLinus Torvalds 		if (next->anon_vma)
6231da177e4SLinus Torvalds 			__anon_vma_merge(vma, next);
6241da177e4SLinus Torvalds 	} else if (insert) {
6251da177e4SLinus Torvalds 		/*
6261da177e4SLinus Torvalds 		 * split_vma has split insert from vma, and needs
6271da177e4SLinus Torvalds 		 * us to insert it before dropping the locks
6281da177e4SLinus Torvalds 		 * (it may either follow vma or precede it).
6291da177e4SLinus Torvalds 		 */
6301da177e4SLinus Torvalds 		__insert_vm_struct(mm, insert);
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (anon_vma)
6341da177e4SLinus Torvalds 		spin_unlock(&anon_vma->lock);
6351da177e4SLinus Torvalds 	if (mapping)
6361da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (remove_next) {
639925d1c40SMatt Helsley 		if (file) {
6401da177e4SLinus Torvalds 			fput(file);
641925d1c40SMatt Helsley 			if (next->vm_flags & VM_EXECUTABLE)
642925d1c40SMatt Helsley 				removed_exe_file_vma(mm);
643925d1c40SMatt Helsley 		}
6441da177e4SLinus Torvalds 		mm->map_count--;
645f0be3d32SLee Schermerhorn 		mpol_put(vma_policy(next));
6461da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, next);
6471da177e4SLinus Torvalds 		/*
6481da177e4SLinus Torvalds 		 * In mprotect's case 6 (see comments on vma_merge),
6491da177e4SLinus Torvalds 		 * we must remove another next too. It would clutter
6501da177e4SLinus Torvalds 		 * up the code too much to do both in one go.
6511da177e4SLinus Torvalds 		 */
6521da177e4SLinus Torvalds 		if (remove_next == 2) {
6531da177e4SLinus Torvalds 			next = vma->vm_next;
6541da177e4SLinus Torvalds 			goto again;
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	validate_mm(mm);
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
6621da177e4SLinus Torvalds  * If the vma has a ->close operation then the driver probably needs to release
6631da177e4SLinus Torvalds  * per-vma resources, so we don't attempt to merge those.
6641da177e4SLinus Torvalds  */
665a6f563dbSHugh Dickins #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP)
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma,
6681da177e4SLinus Torvalds 			struct file *file, unsigned long vm_flags)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	if (vma->vm_flags != vm_flags)
6711da177e4SLinus Torvalds 		return 0;
6721da177e4SLinus Torvalds 	if (vma->vm_file != file)
6731da177e4SLinus Torvalds 		return 0;
6741da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
6751da177e4SLinus Torvalds 		return 0;
6761da177e4SLinus Torvalds 	return 1;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
6801da177e4SLinus Torvalds 					struct anon_vma *anon_vma2)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /*
6861da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
6871da177e4SLinus Torvalds  * in front of (at a lower virtual address and file offset than) the vma.
6881da177e4SLinus Torvalds  *
6891da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
6901da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
6911da177e4SLinus Torvalds  *
6921da177e4SLinus Torvalds  * We don't check here for the merged mmap wrapping around the end of pagecache
6931da177e4SLinus Torvalds  * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
6941da177e4SLinus Torvalds  * wrap, nor mmaps which cover the final page at index -1UL.
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds static int
6971da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
6981da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
7011da177e4SLinus Torvalds 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
7021da177e4SLinus Torvalds 		if (vma->vm_pgoff == vm_pgoff)
7031da177e4SLinus Torvalds 			return 1;
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 	return 0;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /*
7091da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
7101da177e4SLinus Torvalds  * beyond (at a higher virtual address and file offset than) the vma.
7111da177e4SLinus Torvalds  *
7121da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
7131da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
7141da177e4SLinus Torvalds  */
7151da177e4SLinus Torvalds static int
7161da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
7171da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
7201da177e4SLinus Torvalds 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
7211da177e4SLinus Torvalds 		pgoff_t vm_pglen;
7221da177e4SLinus Torvalds 		vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
7231da177e4SLinus Torvalds 		if (vma->vm_pgoff + vm_pglen == vm_pgoff)
7241da177e4SLinus Torvalds 			return 1;
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds 	return 0;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds /*
7301da177e4SLinus Torvalds  * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
7311da177e4SLinus Torvalds  * whether that can be merged with its predecessor or its successor.
7321da177e4SLinus Torvalds  * Or both (it neatly fills a hole).
7331da177e4SLinus Torvalds  *
7341da177e4SLinus Torvalds  * In most cases - when called for mmap, brk or mremap - [addr,end) is
7351da177e4SLinus Torvalds  * certain not to be mapped by the time vma_merge is called; but when
7361da177e4SLinus Torvalds  * called for mprotect, it is certain to be already mapped (either at
7371da177e4SLinus Torvalds  * an offset within prev, or at the start of next), and the flags of
7381da177e4SLinus Torvalds  * this area are about to be changed to vm_flags - and the no-change
7391da177e4SLinus Torvalds  * case has already been eliminated.
7401da177e4SLinus Torvalds  *
7411da177e4SLinus Torvalds  * The following mprotect cases have to be considered, where AAAA is
7421da177e4SLinus Torvalds  * the area passed down from mprotect_fixup, never extending beyond one
7431da177e4SLinus Torvalds  * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
7441da177e4SLinus Torvalds  *
7451da177e4SLinus Torvalds  *     AAAA             AAAA                AAAA          AAAA
7461da177e4SLinus Torvalds  *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
7471da177e4SLinus Torvalds  *    cannot merge    might become    might become    might become
7481da177e4SLinus Torvalds  *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
7491da177e4SLinus Torvalds  *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
7501da177e4SLinus Torvalds  *    mremap move:                                    PPPPNNNNNNNN 8
7511da177e4SLinus Torvalds  *        AAAA
7521da177e4SLinus Torvalds  *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
7531da177e4SLinus Torvalds  *    might become    case 1 below    case 2 below    case 3 below
7541da177e4SLinus Torvalds  *
7551da177e4SLinus Torvalds  * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
7561da177e4SLinus Torvalds  * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
7571da177e4SLinus Torvalds  */
7581da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm,
7591da177e4SLinus Torvalds 			struct vm_area_struct *prev, unsigned long addr,
7601da177e4SLinus Torvalds 			unsigned long end, unsigned long vm_flags,
7611da177e4SLinus Torvalds 		     	struct anon_vma *anon_vma, struct file *file,
7621da177e4SLinus Torvalds 			pgoff_t pgoff, struct mempolicy *policy)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
7651da177e4SLinus Torvalds 	struct vm_area_struct *area, *next;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * We later require that vma->vm_flags == vm_flags,
7691da177e4SLinus Torvalds 	 * so this tests vma->vm_flags & VM_SPECIAL, too.
7701da177e4SLinus Torvalds 	 */
7711da177e4SLinus Torvalds 	if (vm_flags & VM_SPECIAL)
7721da177e4SLinus Torvalds 		return NULL;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (prev)
7751da177e4SLinus Torvalds 		next = prev->vm_next;
7761da177e4SLinus Torvalds 	else
7771da177e4SLinus Torvalds 		next = mm->mmap;
7781da177e4SLinus Torvalds 	area = next;
7791da177e4SLinus Torvalds 	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
7801da177e4SLinus Torvalds 		next = next->vm_next;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/*
7831da177e4SLinus Torvalds 	 * Can it merge with the predecessor?
7841da177e4SLinus Torvalds 	 */
7851da177e4SLinus Torvalds 	if (prev && prev->vm_end == addr &&
7861da177e4SLinus Torvalds   			mpol_equal(vma_policy(prev), policy) &&
7871da177e4SLinus Torvalds 			can_vma_merge_after(prev, vm_flags,
7881da177e4SLinus Torvalds 						anon_vma, file, pgoff)) {
7891da177e4SLinus Torvalds 		/*
7901da177e4SLinus Torvalds 		 * OK, it can.  Can we now merge in the successor as well?
7911da177e4SLinus Torvalds 		 */
7921da177e4SLinus Torvalds 		if (next && end == next->vm_start &&
7931da177e4SLinus Torvalds 				mpol_equal(policy, vma_policy(next)) &&
7941da177e4SLinus Torvalds 				can_vma_merge_before(next, vm_flags,
7951da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen) &&
7961da177e4SLinus Torvalds 				is_mergeable_anon_vma(prev->anon_vma,
7971da177e4SLinus Torvalds 						      next->anon_vma)) {
7981da177e4SLinus Torvalds 							/* cases 1, 6 */
7991da177e4SLinus Torvalds 			vma_adjust(prev, prev->vm_start,
8001da177e4SLinus Torvalds 				next->vm_end, prev->vm_pgoff, NULL);
8011da177e4SLinus Torvalds 		} else					/* cases 2, 5, 7 */
8021da177e4SLinus Torvalds 			vma_adjust(prev, prev->vm_start,
8031da177e4SLinus Torvalds 				end, prev->vm_pgoff, NULL);
8041da177e4SLinus Torvalds 		return prev;
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	/*
8081da177e4SLinus Torvalds 	 * Can this new request be merged in front of next?
8091da177e4SLinus Torvalds 	 */
8101da177e4SLinus Torvalds 	if (next && end == next->vm_start &&
8111da177e4SLinus Torvalds  			mpol_equal(policy, vma_policy(next)) &&
8121da177e4SLinus Torvalds 			can_vma_merge_before(next, vm_flags,
8131da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen)) {
8141da177e4SLinus Torvalds 		if (prev && addr < prev->vm_end)	/* case 4 */
8151da177e4SLinus Torvalds 			vma_adjust(prev, prev->vm_start,
8161da177e4SLinus Torvalds 				addr, prev->vm_pgoff, NULL);
8171da177e4SLinus Torvalds 		else					/* cases 3, 8 */
8181da177e4SLinus Torvalds 			vma_adjust(area, addr, next->vm_end,
8191da177e4SLinus Torvalds 				next->vm_pgoff - pglen, NULL);
8201da177e4SLinus Torvalds 		return area;
8211da177e4SLinus Torvalds 	}
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	return NULL;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /*
8271da177e4SLinus Torvalds  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
8281da177e4SLinus Torvalds  * neighbouring vmas for a suitable anon_vma, before it goes off
8291da177e4SLinus Torvalds  * to allocate a new anon_vma.  It checks because a repetitive
8301da177e4SLinus Torvalds  * sequence of mprotects and faults may otherwise lead to distinct
8311da177e4SLinus Torvalds  * anon_vmas being allocated, preventing vma merge in subsequent
8321da177e4SLinus Torvalds  * mprotect.
8331da177e4SLinus Torvalds  */
8341da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	struct vm_area_struct *near;
8371da177e4SLinus Torvalds 	unsigned long vm_flags;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	near = vma->vm_next;
8401da177e4SLinus Torvalds 	if (!near)
8411da177e4SLinus Torvalds 		goto try_prev;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	/*
8441da177e4SLinus Torvalds 	 * Since only mprotect tries to remerge vmas, match flags
8451da177e4SLinus Torvalds 	 * which might be mprotected into each other later on.
8461da177e4SLinus Torvalds 	 * Neither mlock nor madvise tries to remerge at present,
8471da177e4SLinus Torvalds 	 * so leave their flags as obstructing a merge.
8481da177e4SLinus Torvalds 	 */
8491da177e4SLinus Torvalds 	vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
8501da177e4SLinus Torvalds 	vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (near->anon_vma && vma->vm_end == near->vm_start &&
8531da177e4SLinus Torvalds  			mpol_equal(vma_policy(vma), vma_policy(near)) &&
8541da177e4SLinus Torvalds 			can_vma_merge_before(near, vm_flags,
8551da177e4SLinus Torvalds 				NULL, vma->vm_file, vma->vm_pgoff +
8561da177e4SLinus Torvalds 				((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
8571da177e4SLinus Torvalds 		return near->anon_vma;
8581da177e4SLinus Torvalds try_prev:
8591da177e4SLinus Torvalds 	/*
8601da177e4SLinus Torvalds 	 * It is potentially slow to have to call find_vma_prev here.
8611da177e4SLinus Torvalds 	 * But it's only on the first write fault on the vma, not
8621da177e4SLinus Torvalds 	 * every time, and we could devise a way to avoid it later
8631da177e4SLinus Torvalds 	 * (e.g. stash info in next's anon_vma_node when assigning
8641da177e4SLinus Torvalds 	 * an anon_vma, or when trying vma_merge).  Another time.
8651da177e4SLinus Torvalds 	 */
86646a350efSEric Sesterhenn 	BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
8671da177e4SLinus Torvalds 	if (!near)
8681da177e4SLinus Torvalds 		goto none;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
8711da177e4SLinus Torvalds 	vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (near->anon_vma && near->vm_end == vma->vm_start &&
8741da177e4SLinus Torvalds   			mpol_equal(vma_policy(near), vma_policy(vma)) &&
8751da177e4SLinus Torvalds 			can_vma_merge_after(near, vm_flags,
8761da177e4SLinus Torvalds 				NULL, vma->vm_file, vma->vm_pgoff))
8771da177e4SLinus Torvalds 		return near->anon_vma;
8781da177e4SLinus Torvalds none:
8791da177e4SLinus Torvalds 	/*
8801da177e4SLinus Torvalds 	 * There's no absolute need to look only at touching neighbours:
8811da177e4SLinus Torvalds 	 * we could search further afield for "compatible" anon_vmas.
8821da177e4SLinus Torvalds 	 * But it would probably just be a waste of time searching,
8831da177e4SLinus Torvalds 	 * or lead to too many vmas hanging off the same anon_vma.
8841da177e4SLinus Torvalds 	 * We're trying to allow mprotect remerging later on,
8851da177e4SLinus Torvalds 	 * not trying to minimize memory used for anon_vmas.
8861da177e4SLinus Torvalds 	 */
8871da177e4SLinus Torvalds 	return NULL;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
891ab50b8edSHugh Dickins void vm_stat_account(struct mm_struct *mm, unsigned long flags,
8921da177e4SLinus Torvalds 						struct file *file, long pages)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	const unsigned long stack_flags
8951da177e4SLinus Torvalds 		= VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	if (file) {
8981da177e4SLinus Torvalds 		mm->shared_vm += pages;
8991da177e4SLinus Torvalds 		if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
9001da177e4SLinus Torvalds 			mm->exec_vm += pages;
9011da177e4SLinus Torvalds 	} else if (flags & stack_flags)
9021da177e4SLinus Torvalds 		mm->stack_vm += pages;
9031da177e4SLinus Torvalds 	if (flags & (VM_RESERVED|VM_IO))
9041da177e4SLinus Torvalds 		mm->reserved_vm += pages;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds /*
9091da177e4SLinus Torvalds  * The caller must hold down_write(current->mm->mmap_sem).
9101da177e4SLinus Torvalds  */
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
9131da177e4SLinus Torvalds 			unsigned long len, unsigned long prot,
9141da177e4SLinus Torvalds 			unsigned long flags, unsigned long pgoff)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
9171da177e4SLinus Torvalds 	struct inode *inode;
9181da177e4SLinus Torvalds 	unsigned int vm_flags;
9191da177e4SLinus Torvalds 	int error;
9201da177e4SLinus Torvalds 	int accountable = 1;
9210165ab44SMiklos Szeredi 	unsigned long reqprot = prot;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	/*
9241da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC?
9251da177e4SLinus Torvalds 	 *
9261da177e4SLinus Torvalds 	 * (the exception is when the underlying filesystem is noexec
9271da177e4SLinus Torvalds 	 *  mounted, in which case we dont add PROT_EXEC.)
9281da177e4SLinus Torvalds 	 */
9291da177e4SLinus Torvalds 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
930d3ac7f89SJosef "Jeff" Sipek 		if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
9311da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (!len)
9341da177e4SLinus Torvalds 		return -EINVAL;
9351da177e4SLinus Torvalds 
9367cd94146SEric Paris 	if (!(flags & MAP_FIXED))
9377cd94146SEric Paris 		addr = round_hint_to_min(addr);
9387cd94146SEric Paris 
9393a459756SKirill Korotaev 	error = arch_mmap_check(addr, len, flags);
9403a459756SKirill Korotaev 	if (error)
9413a459756SKirill Korotaev 		return error;
9423a459756SKirill Korotaev 
9431da177e4SLinus Torvalds 	/* Careful about overflows.. */
9441da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
9451da177e4SLinus Torvalds 	if (!len || len > TASK_SIZE)
9461da177e4SLinus Torvalds 		return -ENOMEM;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	/* offset overflow? */
9491da177e4SLinus Torvalds 	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
9501da177e4SLinus Torvalds                return -EOVERFLOW;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	/* Too many mappings? */
9531da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
9541da177e4SLinus Torvalds 		return -ENOMEM;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	/* Obtain the address to map to. we verify (or select) it and ensure
9571da177e4SLinus Torvalds 	 * that it represents a valid section of the address space.
9581da177e4SLinus Torvalds 	 */
9591da177e4SLinus Torvalds 	addr = get_unmapped_area(file, addr, len, pgoff, flags);
9601da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
9611da177e4SLinus Torvalds 		return addr;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	/* Do simple checking here so the lower-level routines won't have
9641da177e4SLinus Torvalds 	 * to. we assume access permissions have been handled by the open
9651da177e4SLinus Torvalds 	 * of the memory object, so we don't do any here.
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
9681da177e4SLinus Torvalds 			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (flags & MAP_LOCKED) {
9711da177e4SLinus Torvalds 		if (!can_do_mlock())
9721da177e4SLinus Torvalds 			return -EPERM;
9731da177e4SLinus Torvalds 		vm_flags |= VM_LOCKED;
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 	/* mlock MCL_FUTURE? */
9761da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
9771da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
97893ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
97993ea1d0aSChris Wright 		locked += mm->locked_vm;
9801da177e4SLinus Torvalds 		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
98193ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
9821da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
9831da177e4SLinus Torvalds 			return -EAGAIN;
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 
986d3ac7f89SJosef "Jeff" Sipek 	inode = file ? file->f_path.dentry->d_inode : NULL;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (file) {
9891da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
9901da177e4SLinus Torvalds 		case MAP_SHARED:
9911da177e4SLinus Torvalds 			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
9921da177e4SLinus Torvalds 				return -EACCES;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 			/*
9951da177e4SLinus Torvalds 			 * Make sure we don't allow writing to an append-only
9961da177e4SLinus Torvalds 			 * file..
9971da177e4SLinus Torvalds 			 */
9981da177e4SLinus Torvalds 			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
9991da177e4SLinus Torvalds 				return -EACCES;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 			/*
10021da177e4SLinus Torvalds 			 * Make sure there are no mandatory locks on the file.
10031da177e4SLinus Torvalds 			 */
10041da177e4SLinus Torvalds 			if (locks_verify_locked(inode))
10051da177e4SLinus Torvalds 				return -EAGAIN;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
10081da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_WRITE))
10091da177e4SLinus Torvalds 				vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 			/* fall through */
10121da177e4SLinus Torvalds 		case MAP_PRIVATE:
10131da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_READ))
10141da177e4SLinus Torvalds 				return -EACCES;
1015d3ac7f89SJosef "Jeff" Sipek 			if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
101680c5606cSLinus Torvalds 				if (vm_flags & VM_EXEC)
101780c5606cSLinus Torvalds 					return -EPERM;
101880c5606cSLinus Torvalds 				vm_flags &= ~VM_MAYEXEC;
101980c5606cSLinus Torvalds 			}
102080c5606cSLinus Torvalds 			if (is_file_hugepages(file))
102180c5606cSLinus Torvalds 				accountable = 0;
102280c5606cSLinus Torvalds 
102380c5606cSLinus Torvalds 			if (!file->f_op || !file->f_op->mmap)
102480c5606cSLinus Torvalds 				return -ENODEV;
10251da177e4SLinus Torvalds 			break;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 		default:
10281da177e4SLinus Torvalds 			return -EINVAL;
10291da177e4SLinus Torvalds 		}
10301da177e4SLinus Torvalds 	} else {
10311da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
10321da177e4SLinus Torvalds 		case MAP_SHARED:
10331da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
10341da177e4SLinus Torvalds 			break;
10351da177e4SLinus Torvalds 		case MAP_PRIVATE:
10361da177e4SLinus Torvalds 			/*
10371da177e4SLinus Torvalds 			 * Set pgoff according to addr for anon_vma.
10381da177e4SLinus Torvalds 			 */
10391da177e4SLinus Torvalds 			pgoff = addr >> PAGE_SHIFT;
10401da177e4SLinus Torvalds 			break;
10411da177e4SLinus Torvalds 		default:
10421da177e4SLinus Torvalds 			return -EINVAL;
10431da177e4SLinus Torvalds 		}
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 
1046ed032189SEric Paris 	error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
10471da177e4SLinus Torvalds 	if (error)
10481da177e4SLinus Torvalds 		return error;
10491da177e4SLinus Torvalds 
10500165ab44SMiklos Szeredi 	return mmap_region(file, addr, len, flags, vm_flags, pgoff,
10510165ab44SMiklos Szeredi 			   accountable);
10520165ab44SMiklos Szeredi }
10530165ab44SMiklos Szeredi EXPORT_SYMBOL(do_mmap_pgoff);
10540165ab44SMiklos Szeredi 
10554e950f6fSAlexey Dobriyan /*
10564e950f6fSAlexey Dobriyan  * Some shared mappigns will want the pages marked read-only
10574e950f6fSAlexey Dobriyan  * to track write events. If so, we'll downgrade vm_page_prot
10584e950f6fSAlexey Dobriyan  * to the private version (using protection_map[] without the
10594e950f6fSAlexey Dobriyan  * VM_SHARED bit).
10604e950f6fSAlexey Dobriyan  */
10614e950f6fSAlexey Dobriyan int vma_wants_writenotify(struct vm_area_struct *vma)
10624e950f6fSAlexey Dobriyan {
10634e950f6fSAlexey Dobriyan 	unsigned int vm_flags = vma->vm_flags;
10644e950f6fSAlexey Dobriyan 
10654e950f6fSAlexey Dobriyan 	/* If it was private or non-writable, the write bit is already clear */
10664e950f6fSAlexey Dobriyan 	if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
10674e950f6fSAlexey Dobriyan 		return 0;
10684e950f6fSAlexey Dobriyan 
10694e950f6fSAlexey Dobriyan 	/* The backer wishes to know when pages are first written to? */
10704e950f6fSAlexey Dobriyan 	if (vma->vm_ops && vma->vm_ops->page_mkwrite)
10714e950f6fSAlexey Dobriyan 		return 1;
10724e950f6fSAlexey Dobriyan 
10734e950f6fSAlexey Dobriyan 	/* The open routine did something to the protections already? */
10744e950f6fSAlexey Dobriyan 	if (pgprot_val(vma->vm_page_prot) !=
10753ed75eb8SColy Li 	    pgprot_val(vm_get_page_prot(vm_flags)))
10764e950f6fSAlexey Dobriyan 		return 0;
10774e950f6fSAlexey Dobriyan 
10784e950f6fSAlexey Dobriyan 	/* Specialty mapping? */
10794e950f6fSAlexey Dobriyan 	if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
10804e950f6fSAlexey Dobriyan 		return 0;
10814e950f6fSAlexey Dobriyan 
10824e950f6fSAlexey Dobriyan 	/* Can the mapping track the dirty pages? */
10834e950f6fSAlexey Dobriyan 	return vma->vm_file && vma->vm_file->f_mapping &&
10844e950f6fSAlexey Dobriyan 		mapping_cap_account_dirty(vma->vm_file->f_mapping);
10854e950f6fSAlexey Dobriyan }
10864e950f6fSAlexey Dobriyan 
10870165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr,
10880165ab44SMiklos Szeredi 			  unsigned long len, unsigned long flags,
10890165ab44SMiklos Szeredi 			  unsigned int vm_flags, unsigned long pgoff,
10900165ab44SMiklos Szeredi 			  int accountable)
10910165ab44SMiklos Szeredi {
10920165ab44SMiklos Szeredi 	struct mm_struct *mm = current->mm;
10930165ab44SMiklos Szeredi 	struct vm_area_struct *vma, *prev;
10940165ab44SMiklos Szeredi 	int correct_wcount = 0;
10950165ab44SMiklos Szeredi 	int error;
10960165ab44SMiklos Szeredi 	struct rb_node **rb_link, *rb_parent;
10970165ab44SMiklos Szeredi 	unsigned long charged = 0;
10980165ab44SMiklos Szeredi 	struct inode *inode =  file ? file->f_path.dentry->d_inode : NULL;
10990165ab44SMiklos Szeredi 
11001da177e4SLinus Torvalds 	/* Clear old maps */
11011da177e4SLinus Torvalds 	error = -ENOMEM;
11021da177e4SLinus Torvalds munmap_back:
11031da177e4SLinus Torvalds 	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
11041da177e4SLinus Torvalds 	if (vma && vma->vm_start < addr + len) {
11051da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
11061da177e4SLinus Torvalds 			return -ENOMEM;
11071da177e4SLinus Torvalds 		goto munmap_back;
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	/* Check against address space limit. */
1111119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
11121da177e4SLinus Torvalds 		return -ENOMEM;
11131da177e4SLinus Torvalds 
1114cdfd4325SAndy Whitcroft 	if (flags & MAP_NORESERVE)
1115cdfd4325SAndy Whitcroft 		vm_flags |= VM_NORESERVE;
1116cdfd4325SAndy Whitcroft 
11171da177e4SLinus Torvalds 	if (accountable && (!(flags & MAP_NORESERVE) ||
11181da177e4SLinus Torvalds 			    sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
11191da177e4SLinus Torvalds 		if (vm_flags & VM_SHARED) {
11201da177e4SLinus Torvalds 			/* Check memory availability in shmem_file_setup? */
11211da177e4SLinus Torvalds 			vm_flags |= VM_ACCOUNT;
11221da177e4SLinus Torvalds 		} else if (vm_flags & VM_WRITE) {
11231da177e4SLinus Torvalds 			/*
11241da177e4SLinus Torvalds 			 * Private writable mapping: check memory availability
11251da177e4SLinus Torvalds 			 */
11261da177e4SLinus Torvalds 			charged = len >> PAGE_SHIFT;
11271da177e4SLinus Torvalds 			if (security_vm_enough_memory(charged))
11281da177e4SLinus Torvalds 				return -ENOMEM;
11291da177e4SLinus Torvalds 			vm_flags |= VM_ACCOUNT;
11301da177e4SLinus Torvalds 		}
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	/*
11341da177e4SLinus Torvalds 	 * Can we just expand an old private anonymous mapping?
11351da177e4SLinus Torvalds 	 * The VM_SHARED test is necessary because shmem_zero_setup
11361da177e4SLinus Torvalds 	 * will create the file object for a shared anonymous map below.
11371da177e4SLinus Torvalds 	 */
11381da177e4SLinus Torvalds 	if (!file && !(vm_flags & VM_SHARED) &&
11391da177e4SLinus Torvalds 	    vma_merge(mm, prev, addr, addr + len, vm_flags,
11401da177e4SLinus Torvalds 					NULL, NULL, pgoff, NULL))
11411da177e4SLinus Torvalds 		goto out;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	/*
11441da177e4SLinus Torvalds 	 * Determine the object being mapped and call the appropriate
11451da177e4SLinus Torvalds 	 * specific mapper. the address has already been validated, but
11461da177e4SLinus Torvalds 	 * not unmapped, but the maps are removed from the list.
11471da177e4SLinus Torvalds 	 */
1148c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
11491da177e4SLinus Torvalds 	if (!vma) {
11501da177e4SLinus Torvalds 		error = -ENOMEM;
11511da177e4SLinus Torvalds 		goto unacct_error;
11521da177e4SLinus Torvalds 	}
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	vma->vm_mm = mm;
11551da177e4SLinus Torvalds 	vma->vm_start = addr;
11561da177e4SLinus Torvalds 	vma->vm_end = addr + len;
11571da177e4SLinus Torvalds 	vma->vm_flags = vm_flags;
11583ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vm_flags);
11591da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	if (file) {
11621da177e4SLinus Torvalds 		error = -EINVAL;
11631da177e4SLinus Torvalds 		if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
11641da177e4SLinus Torvalds 			goto free_vma;
11651da177e4SLinus Torvalds 		if (vm_flags & VM_DENYWRITE) {
11661da177e4SLinus Torvalds 			error = deny_write_access(file);
11671da177e4SLinus Torvalds 			if (error)
11681da177e4SLinus Torvalds 				goto free_vma;
11691da177e4SLinus Torvalds 			correct_wcount = 1;
11701da177e4SLinus Torvalds 		}
11711da177e4SLinus Torvalds 		vma->vm_file = file;
11721da177e4SLinus Torvalds 		get_file(file);
11731da177e4SLinus Torvalds 		error = file->f_op->mmap(file, vma);
11741da177e4SLinus Torvalds 		if (error)
11751da177e4SLinus Torvalds 			goto unmap_and_free_vma;
1176925d1c40SMatt Helsley 		if (vm_flags & VM_EXECUTABLE)
1177925d1c40SMatt Helsley 			added_exe_file_vma(mm);
11781da177e4SLinus Torvalds 	} else if (vm_flags & VM_SHARED) {
11791da177e4SLinus Torvalds 		error = shmem_zero_setup(vma);
11801da177e4SLinus Torvalds 		if (error)
11811da177e4SLinus Torvalds 			goto free_vma;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	/* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
11851da177e4SLinus Torvalds 	 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
11861da177e4SLinus Torvalds 	 * that memory reservation must be checked; but that reservation
11871da177e4SLinus Torvalds 	 * belongs to shared memory object, not to vma: so now clear it.
11881da177e4SLinus Torvalds 	 */
11891da177e4SLinus Torvalds 	if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
11901da177e4SLinus Torvalds 		vma->vm_flags &= ~VM_ACCOUNT;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	/* Can addr have changed??
11931da177e4SLinus Torvalds 	 *
11941da177e4SLinus Torvalds 	 * Answer: Yes, several device drivers can do it in their
11951da177e4SLinus Torvalds 	 *         f_op->mmap method. -DaveM
11961da177e4SLinus Torvalds 	 */
11971da177e4SLinus Torvalds 	addr = vma->vm_start;
11981da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff;
11991da177e4SLinus Torvalds 	vm_flags = vma->vm_flags;
12001da177e4SLinus Torvalds 
1201d08b3851SPeter Zijlstra 	if (vma_wants_writenotify(vma))
12021ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
1203d08b3851SPeter Zijlstra 
12044d3d5b41SOleg Nesterov 	if (file && vma_merge(mm, prev, addr, vma->vm_end,
12051da177e4SLinus Torvalds 			vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
1206f0be3d32SLee Schermerhorn 		mpol_put(vma_policy(vma));
12071da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, vma);
12084d3d5b41SOleg Nesterov 		fput(file);
1209925d1c40SMatt Helsley 		if (vm_flags & VM_EXECUTABLE)
1210925d1c40SMatt Helsley 			removed_exe_file_vma(mm);
12114d3d5b41SOleg Nesterov 	} else {
12124d3d5b41SOleg Nesterov 		vma_link(mm, vma, prev, rb_link, rb_parent);
12134d3d5b41SOleg Nesterov 		file = vma->vm_file;
12141da177e4SLinus Torvalds 	}
12154d3d5b41SOleg Nesterov 
12164d3d5b41SOleg Nesterov 	/* Once vma denies write, undo our temporary denial count */
12174d3d5b41SOleg Nesterov 	if (correct_wcount)
12184d3d5b41SOleg Nesterov 		atomic_inc(&inode->i_writecount);
12191da177e4SLinus Torvalds out:
12201da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
1221ab50b8edSHugh Dickins 	vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
12221da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
12231da177e4SLinus Torvalds 		mm->locked_vm += len >> PAGE_SHIFT;
12241da177e4SLinus Torvalds 		make_pages_present(addr, addr + len);
12251da177e4SLinus Torvalds 	}
122654cb8821SNick Piggin 	if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
122754cb8821SNick Piggin 		make_pages_present(addr, addr + len);
12281da177e4SLinus Torvalds 	return addr;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds unmap_and_free_vma:
12311da177e4SLinus Torvalds 	if (correct_wcount)
12321da177e4SLinus Torvalds 		atomic_inc(&inode->i_writecount);
12331da177e4SLinus Torvalds 	vma->vm_file = NULL;
12341da177e4SLinus Torvalds 	fput(file);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/* Undo any partial mapping done by a device driver. */
1237e0da382cSHugh Dickins 	unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1238e0da382cSHugh Dickins 	charged = 0;
12391da177e4SLinus Torvalds free_vma:
12401da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
12411da177e4SLinus Torvalds unacct_error:
12421da177e4SLinus Torvalds 	if (charged)
12431da177e4SLinus Torvalds 		vm_unacct_memory(charged);
12441da177e4SLinus Torvalds 	return error;
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds /* Get an address range which is currently unmapped.
12481da177e4SLinus Torvalds  * For shmat() with addr=0.
12491da177e4SLinus Torvalds  *
12501da177e4SLinus Torvalds  * Ugly calling convention alert:
12511da177e4SLinus Torvalds  * Return value with the low bits set means error value,
12521da177e4SLinus Torvalds  * ie
12531da177e4SLinus Torvalds  *	if (ret & ~PAGE_MASK)
12541da177e4SLinus Torvalds  *		error = ret;
12551da177e4SLinus Torvalds  *
12561da177e4SLinus Torvalds  * This function "knows" that -ENOMEM has the bits set.
12571da177e4SLinus Torvalds  */
12581da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA
12591da177e4SLinus Torvalds unsigned long
12601da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr,
12611da177e4SLinus Torvalds 		unsigned long len, unsigned long pgoff, unsigned long flags)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
12641da177e4SLinus Torvalds 	struct vm_area_struct *vma;
12651da177e4SLinus Torvalds 	unsigned long start_addr;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	if (len > TASK_SIZE)
12681da177e4SLinus Torvalds 		return -ENOMEM;
12691da177e4SLinus Torvalds 
127006abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
127106abdfb4SBenjamin Herrenschmidt 		return addr;
127206abdfb4SBenjamin Herrenschmidt 
12731da177e4SLinus Torvalds 	if (addr) {
12741da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
12751da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
12761da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
12771da177e4SLinus Torvalds 		    (!vma || addr + len <= vma->vm_start))
12781da177e4SLinus Torvalds 			return addr;
12791da177e4SLinus Torvalds 	}
12801363c3cdSWolfgang Wander 	if (len > mm->cached_hole_size) {
12811da177e4SLinus Torvalds 	        start_addr = addr = mm->free_area_cache;
12821363c3cdSWolfgang Wander 	} else {
12831363c3cdSWolfgang Wander 	        start_addr = addr = TASK_UNMAPPED_BASE;
12841363c3cdSWolfgang Wander 	        mm->cached_hole_size = 0;
12851363c3cdSWolfgang Wander 	}
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds full_search:
12881da177e4SLinus Torvalds 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
12891da177e4SLinus Torvalds 		/* At this point:  (!vma || addr < vma->vm_end). */
12901da177e4SLinus Torvalds 		if (TASK_SIZE - len < addr) {
12911da177e4SLinus Torvalds 			/*
12921da177e4SLinus Torvalds 			 * Start a new search - just in case we missed
12931da177e4SLinus Torvalds 			 * some holes.
12941da177e4SLinus Torvalds 			 */
12951da177e4SLinus Torvalds 			if (start_addr != TASK_UNMAPPED_BASE) {
12961363c3cdSWolfgang Wander 				addr = TASK_UNMAPPED_BASE;
12971363c3cdSWolfgang Wander 			        start_addr = addr;
12981363c3cdSWolfgang Wander 				mm->cached_hole_size = 0;
12991da177e4SLinus Torvalds 				goto full_search;
13001da177e4SLinus Torvalds 			}
13011da177e4SLinus Torvalds 			return -ENOMEM;
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds 		if (!vma || addr + len <= vma->vm_start) {
13041da177e4SLinus Torvalds 			/*
13051da177e4SLinus Torvalds 			 * Remember the place where we stopped the search:
13061da177e4SLinus Torvalds 			 */
13071da177e4SLinus Torvalds 			mm->free_area_cache = addr + len;
13081da177e4SLinus Torvalds 			return addr;
13091da177e4SLinus Torvalds 		}
13101363c3cdSWolfgang Wander 		if (addr + mm->cached_hole_size < vma->vm_start)
13111363c3cdSWolfgang Wander 		        mm->cached_hole_size = vma->vm_start - addr;
13121da177e4SLinus Torvalds 		addr = vma->vm_end;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds #endif
13161da177e4SLinus Torvalds 
13171363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	/*
13201da177e4SLinus Torvalds 	 * Is this a new hole at the lowest possible address?
13211da177e4SLinus Torvalds 	 */
13221363c3cdSWolfgang Wander 	if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
13231363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
13241363c3cdSWolfgang Wander 		mm->cached_hole_size = ~0UL;
13251363c3cdSWolfgang Wander 	}
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds /*
13291da177e4SLinus Torvalds  * This mmap-allocator allocates new areas top-down from below the
13301da177e4SLinus Torvalds  * stack's low limit (the base):
13311da177e4SLinus Torvalds  */
13321da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
13331da177e4SLinus Torvalds unsigned long
13341da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
13351da177e4SLinus Torvalds 			  const unsigned long len, const unsigned long pgoff,
13361da177e4SLinus Torvalds 			  const unsigned long flags)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	struct vm_area_struct *vma;
13391da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
13401da177e4SLinus Torvalds 	unsigned long addr = addr0;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	/* requested length too big for entire address space */
13431da177e4SLinus Torvalds 	if (len > TASK_SIZE)
13441da177e4SLinus Torvalds 		return -ENOMEM;
13451da177e4SLinus Torvalds 
134606abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
134706abdfb4SBenjamin Herrenschmidt 		return addr;
134806abdfb4SBenjamin Herrenschmidt 
13491da177e4SLinus Torvalds 	/* requesting a specific address */
13501da177e4SLinus Torvalds 	if (addr) {
13511da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
13521da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
13531da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
13541da177e4SLinus Torvalds 				(!vma || addr + len <= vma->vm_start))
13551da177e4SLinus Torvalds 			return addr;
13561da177e4SLinus Torvalds 	}
13571da177e4SLinus Torvalds 
13581363c3cdSWolfgang Wander 	/* check if free_area_cache is useful for us */
13591363c3cdSWolfgang Wander 	if (len <= mm->cached_hole_size) {
13601363c3cdSWolfgang Wander  	        mm->cached_hole_size = 0;
13611363c3cdSWolfgang Wander  		mm->free_area_cache = mm->mmap_base;
13621363c3cdSWolfgang Wander  	}
13631363c3cdSWolfgang Wander 
13641da177e4SLinus Torvalds 	/* either no address requested or can't fit in requested address hole */
13651da177e4SLinus Torvalds 	addr = mm->free_area_cache;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	/* make sure it can fit in the remaining address space */
136849a43876SLinus Torvalds 	if (addr > len) {
13691da177e4SLinus Torvalds 		vma = find_vma(mm, addr-len);
13701da177e4SLinus Torvalds 		if (!vma || addr <= vma->vm_start)
13711da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
13721da177e4SLinus Torvalds 			return (mm->free_area_cache = addr-len);
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 
137573219d17SChris Wright 	if (mm->mmap_base < len)
137673219d17SChris Wright 		goto bottomup;
137773219d17SChris Wright 
13781da177e4SLinus Torvalds 	addr = mm->mmap_base-len;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	do {
13811da177e4SLinus Torvalds 		/*
13821da177e4SLinus Torvalds 		 * Lookup failure means no vma is above this address,
13831da177e4SLinus Torvalds 		 * else if new region fits below vma->vm_start,
13841da177e4SLinus Torvalds 		 * return with success:
13851da177e4SLinus Torvalds 		 */
13861da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
13871da177e4SLinus Torvalds 		if (!vma || addr+len <= vma->vm_start)
13881da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
13891da177e4SLinus Torvalds 			return (mm->free_area_cache = addr);
13901da177e4SLinus Torvalds 
13911363c3cdSWolfgang Wander  		/* remember the largest hole we saw so far */
13921363c3cdSWolfgang Wander  		if (addr + mm->cached_hole_size < vma->vm_start)
13931363c3cdSWolfgang Wander  		        mm->cached_hole_size = vma->vm_start - addr;
13941363c3cdSWolfgang Wander 
13951da177e4SLinus Torvalds 		/* try just below the current vma->vm_start */
13961da177e4SLinus Torvalds 		addr = vma->vm_start-len;
139749a43876SLinus Torvalds 	} while (len < vma->vm_start);
13981da177e4SLinus Torvalds 
139973219d17SChris Wright bottomup:
14001da177e4SLinus Torvalds 	/*
14011da177e4SLinus Torvalds 	 * A failed mmap() very likely causes application failure,
14021da177e4SLinus Torvalds 	 * so fall back to the bottom-up function here. This scenario
14031da177e4SLinus Torvalds 	 * can happen with large stack limits and large mmap()
14041da177e4SLinus Torvalds 	 * allocations.
14051da177e4SLinus Torvalds 	 */
14061363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
14071da177e4SLinus Torvalds   	mm->free_area_cache = TASK_UNMAPPED_BASE;
14081da177e4SLinus Torvalds 	addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
14091da177e4SLinus Torvalds 	/*
14101da177e4SLinus Torvalds 	 * Restore the topdown base:
14111da177e4SLinus Torvalds 	 */
14121da177e4SLinus Torvalds 	mm->free_area_cache = mm->mmap_base;
14131363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	return addr;
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds #endif
14181da177e4SLinus Torvalds 
14191363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	/*
14221da177e4SLinus Torvalds 	 * Is this a new hole at the highest possible address?
14231da177e4SLinus Torvalds 	 */
14241363c3cdSWolfgang Wander 	if (addr > mm->free_area_cache)
14251363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	/* dont allow allocations above current base */
14281363c3cdSWolfgang Wander 	if (mm->free_area_cache > mm->mmap_base)
14291363c3cdSWolfgang Wander 		mm->free_area_cache = mm->mmap_base;
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds unsigned long
14331da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
14341da177e4SLinus Torvalds 		unsigned long pgoff, unsigned long flags)
14351da177e4SLinus Torvalds {
143606abdfb4SBenjamin Herrenschmidt 	unsigned long (*get_area)(struct file *, unsigned long,
143706abdfb4SBenjamin Herrenschmidt 				  unsigned long, unsigned long, unsigned long);
143807ab67c8SLinus Torvalds 
143907ab67c8SLinus Torvalds 	get_area = current->mm->get_unmapped_area;
144007ab67c8SLinus Torvalds 	if (file && file->f_op && file->f_op->get_unmapped_area)
144107ab67c8SLinus Torvalds 		get_area = file->f_op->get_unmapped_area;
144207ab67c8SLinus Torvalds 	addr = get_area(file, addr, len, pgoff, flags);
144307ab67c8SLinus Torvalds 	if (IS_ERR_VALUE(addr))
144407ab67c8SLinus Torvalds 		return addr;
144507ab67c8SLinus Torvalds 
14461da177e4SLinus Torvalds 	if (addr > TASK_SIZE - len)
14471da177e4SLinus Torvalds 		return -ENOMEM;
14481da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
14491da177e4SLinus Torvalds 		return -EINVAL;
145006abdfb4SBenjamin Herrenschmidt 
145108e7d9b5SMartin Schwidefsky 	return arch_rebalance_pgtables(addr, len);
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area);
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
14571da177e4SLinus Torvalds struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	if (mm) {
14621da177e4SLinus Torvalds 		/* Check the cache first. */
14631da177e4SLinus Torvalds 		/* (Cache hit rate is typically around 35%.) */
14641da177e4SLinus Torvalds 		vma = mm->mmap_cache;
14651da177e4SLinus Torvalds 		if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
14661da177e4SLinus Torvalds 			struct rb_node * rb_node;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 			rb_node = mm->mm_rb.rb_node;
14691da177e4SLinus Torvalds 			vma = NULL;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 			while (rb_node) {
14721da177e4SLinus Torvalds 				struct vm_area_struct * vma_tmp;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 				vma_tmp = rb_entry(rb_node,
14751da177e4SLinus Torvalds 						struct vm_area_struct, vm_rb);
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 				if (vma_tmp->vm_end > addr) {
14781da177e4SLinus Torvalds 					vma = vma_tmp;
14791da177e4SLinus Torvalds 					if (vma_tmp->vm_start <= addr)
14801da177e4SLinus Torvalds 						break;
14811da177e4SLinus Torvalds 					rb_node = rb_node->rb_left;
14821da177e4SLinus Torvalds 				} else
14831da177e4SLinus Torvalds 					rb_node = rb_node->rb_right;
14841da177e4SLinus Torvalds 			}
14851da177e4SLinus Torvalds 			if (vma)
14861da177e4SLinus Torvalds 				mm->mmap_cache = vma;
14871da177e4SLinus Torvalds 		}
14881da177e4SLinus Torvalds 	}
14891da177e4SLinus Torvalds 	return vma;
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma);
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
14951da177e4SLinus Torvalds struct vm_area_struct *
14961da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr,
14971da177e4SLinus Torvalds 			struct vm_area_struct **pprev)
14981da177e4SLinus Torvalds {
14991da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL, *prev = NULL;
15001da177e4SLinus Torvalds 	struct rb_node * rb_node;
15011da177e4SLinus Torvalds 	if (!mm)
15021da177e4SLinus Torvalds 		goto out;
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 	/* Guard against addr being lower than the first VMA */
15051da177e4SLinus Torvalds 	vma = mm->mmap;
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	/* Go through the RB tree quickly. */
15081da177e4SLinus Torvalds 	rb_node = mm->mm_rb.rb_node;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	while (rb_node) {
15111da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
15121da177e4SLinus Torvalds 		vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 		if (addr < vma_tmp->vm_end) {
15151da177e4SLinus Torvalds 			rb_node = rb_node->rb_left;
15161da177e4SLinus Torvalds 		} else {
15171da177e4SLinus Torvalds 			prev = vma_tmp;
15181da177e4SLinus Torvalds 			if (!prev->vm_next || (addr < prev->vm_next->vm_end))
15191da177e4SLinus Torvalds 				break;
15201da177e4SLinus Torvalds 			rb_node = rb_node->rb_right;
15211da177e4SLinus Torvalds 		}
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds out:
15251da177e4SLinus Torvalds 	*pprev = prev;
15261da177e4SLinus Torvalds 	return prev ? prev->vm_next : vma;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds /*
15301da177e4SLinus Torvalds  * Verify that the stack growth is acceptable and
15311da177e4SLinus Torvalds  * update accounting. This is shared with both the
15321da177e4SLinus Torvalds  * grow-up and grow-down cases.
15331da177e4SLinus Torvalds  */
15341da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
15371da177e4SLinus Torvalds 	struct rlimit *rlim = current->signal->rlim;
15380d59a01bSAdam Litke 	unsigned long new_start;
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	/* address space limit tests */
1541119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, grow))
15421da177e4SLinus Torvalds 		return -ENOMEM;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	/* Stack limit test */
15451da177e4SLinus Torvalds 	if (size > rlim[RLIMIT_STACK].rlim_cur)
15461da177e4SLinus Torvalds 		return -ENOMEM;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	/* mlock limit tests */
15491da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED) {
15501da177e4SLinus Torvalds 		unsigned long locked;
15511da177e4SLinus Torvalds 		unsigned long limit;
15521da177e4SLinus Torvalds 		locked = mm->locked_vm + grow;
15531da177e4SLinus Torvalds 		limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
15541da177e4SLinus Torvalds 		if (locked > limit && !capable(CAP_IPC_LOCK))
15551da177e4SLinus Torvalds 			return -ENOMEM;
15561da177e4SLinus Torvalds 	}
15571da177e4SLinus Torvalds 
15580d59a01bSAdam Litke 	/* Check to ensure the stack will not grow into a hugetlb-only region */
15590d59a01bSAdam Litke 	new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
15600d59a01bSAdam Litke 			vma->vm_end - size;
15610d59a01bSAdam Litke 	if (is_hugepage_only_range(vma->vm_mm, new_start, size))
15620d59a01bSAdam Litke 		return -EFAULT;
15630d59a01bSAdam Litke 
15641da177e4SLinus Torvalds 	/*
15651da177e4SLinus Torvalds 	 * Overcommit..  This must be the final test, as it will
15661da177e4SLinus Torvalds 	 * update security statistics.
15671da177e4SLinus Torvalds 	 */
15681da177e4SLinus Torvalds 	if (security_vm_enough_memory(grow))
15691da177e4SLinus Torvalds 		return -ENOMEM;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	/* Ok, everything looks good - let it rip */
15721da177e4SLinus Torvalds 	mm->total_vm += grow;
15731da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED)
15741da177e4SLinus Torvalds 		mm->locked_vm += grow;
1575ab50b8edSHugh Dickins 	vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
15761da177e4SLinus Torvalds 	return 0;
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
157946dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
15801da177e4SLinus Torvalds /*
158146dea3d0SHugh Dickins  * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
158246dea3d0SHugh Dickins  * vma is the last one with address > vma->vm_end.  Have to extend vma.
15831da177e4SLinus Torvalds  */
15849ab88515SMatthew Wilcox #ifndef CONFIG_IA64
158546dea3d0SHugh Dickins static inline
158646dea3d0SHugh Dickins #endif
158746dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	int error;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSUP))
15921da177e4SLinus Torvalds 		return -EFAULT;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	/*
15951da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
15961da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
15971da177e4SLinus Torvalds 	 */
15981da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
15991da177e4SLinus Torvalds 		return -ENOMEM;
16001da177e4SLinus Torvalds 	anon_vma_lock(vma);
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	/*
16031da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
16041da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
16051da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
160606b32f3aSHelge Deller 	 * Also guard against wrapping around to address 0.
16071da177e4SLinus Torvalds 	 */
160806b32f3aSHelge Deller 	if (address < PAGE_ALIGN(address+4))
160906b32f3aSHelge Deller 		address = PAGE_ALIGN(address+4);
161006b32f3aSHelge Deller 	else {
161106b32f3aSHelge Deller 		anon_vma_unlock(vma);
161206b32f3aSHelge Deller 		return -ENOMEM;
161306b32f3aSHelge Deller 	}
16141da177e4SLinus Torvalds 	error = 0;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
16171da177e4SLinus Torvalds 	if (address > vma->vm_end) {
16181da177e4SLinus Torvalds 		unsigned long size, grow;
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 		size = address - vma->vm_start;
16211da177e4SLinus Torvalds 		grow = (address - vma->vm_end) >> PAGE_SHIFT;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 		error = acct_stack_growth(vma, size, grow);
16241da177e4SLinus Torvalds 		if (!error)
16251da177e4SLinus Torvalds 			vma->vm_end = address;
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 	anon_vma_unlock(vma);
16281da177e4SLinus Torvalds 	return error;
16291da177e4SLinus Torvalds }
163046dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
163146dea3d0SHugh Dickins 
16321da177e4SLinus Torvalds /*
16331da177e4SLinus Torvalds  * vma is the first one with address < vma->vm_start.  Have to extend vma.
16341da177e4SLinus Torvalds  */
1635b6a2fea3SOllie Wild static inline int expand_downwards(struct vm_area_struct *vma,
1636b6a2fea3SOllie Wild 				   unsigned long address)
16371da177e4SLinus Torvalds {
16381da177e4SLinus Torvalds 	int error;
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 	/*
16411da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
16421da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
16431da177e4SLinus Torvalds 	 */
16441da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
16451da177e4SLinus Torvalds 		return -ENOMEM;
16468869477aSEric Paris 
16478869477aSEric Paris 	address &= PAGE_MASK;
164888c3f7a8SRichard Knutsson 	error = security_file_mmap(NULL, 0, 0, 0, address, 1);
16498869477aSEric Paris 	if (error)
16508869477aSEric Paris 		return error;
16518869477aSEric Paris 
16521da177e4SLinus Torvalds 	anon_vma_lock(vma);
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	/*
16551da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
16561da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
16571da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
16581da177e4SLinus Torvalds 	 */
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
16611da177e4SLinus Torvalds 	if (address < vma->vm_start) {
16621da177e4SLinus Torvalds 		unsigned long size, grow;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 		size = vma->vm_end - address;
16651da177e4SLinus Torvalds 		grow = (vma->vm_start - address) >> PAGE_SHIFT;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 		error = acct_stack_growth(vma, size, grow);
16681da177e4SLinus Torvalds 		if (!error) {
16691da177e4SLinus Torvalds 			vma->vm_start = address;
16701da177e4SLinus Torvalds 			vma->vm_pgoff -= grow;
16711da177e4SLinus Torvalds 		}
16721da177e4SLinus Torvalds 	}
16731da177e4SLinus Torvalds 	anon_vma_unlock(vma);
16741da177e4SLinus Torvalds 	return error;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
1677b6a2fea3SOllie Wild int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
1678b6a2fea3SOllie Wild {
1679b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1680b6a2fea3SOllie Wild }
1681b6a2fea3SOllie Wild 
1682b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP
1683b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1684b6a2fea3SOllie Wild {
1685b6a2fea3SOllie Wild 	return expand_upwards(vma, address);
1686b6a2fea3SOllie Wild }
1687b6a2fea3SOllie Wild 
1688b6a2fea3SOllie Wild struct vm_area_struct *
1689b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr)
1690b6a2fea3SOllie Wild {
1691b6a2fea3SOllie Wild 	struct vm_area_struct *vma, *prev;
1692b6a2fea3SOllie Wild 
1693b6a2fea3SOllie Wild 	addr &= PAGE_MASK;
1694b6a2fea3SOllie Wild 	vma = find_vma_prev(mm, addr, &prev);
1695b6a2fea3SOllie Wild 	if (vma && (vma->vm_start <= addr))
1696b6a2fea3SOllie Wild 		return vma;
1697b6a2fea3SOllie Wild 	if (!prev || expand_stack(prev, addr))
1698b6a2fea3SOllie Wild 		return NULL;
1699b6a2fea3SOllie Wild 	if (prev->vm_flags & VM_LOCKED)
1700b6a2fea3SOllie Wild 		make_pages_present(addr, prev->vm_end);
1701b6a2fea3SOllie Wild 	return prev;
1702b6a2fea3SOllie Wild }
1703b6a2fea3SOllie Wild #else
1704b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1705b6a2fea3SOllie Wild {
1706b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1707b6a2fea3SOllie Wild }
1708b6a2fea3SOllie Wild 
17091da177e4SLinus Torvalds struct vm_area_struct *
17101da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr)
17111da177e4SLinus Torvalds {
17121da177e4SLinus Torvalds 	struct vm_area_struct * vma;
17131da177e4SLinus Torvalds 	unsigned long start;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	addr &= PAGE_MASK;
17161da177e4SLinus Torvalds 	vma = find_vma(mm,addr);
17171da177e4SLinus Torvalds 	if (!vma)
17181da177e4SLinus Torvalds 		return NULL;
17191da177e4SLinus Torvalds 	if (vma->vm_start <= addr)
17201da177e4SLinus Torvalds 		return vma;
17211da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSDOWN))
17221da177e4SLinus Torvalds 		return NULL;
17231da177e4SLinus Torvalds 	start = vma->vm_start;
17241da177e4SLinus Torvalds 	if (expand_stack(vma, addr))
17251da177e4SLinus Torvalds 		return NULL;
1726b6a2fea3SOllie Wild 	if (vma->vm_flags & VM_LOCKED)
17271da177e4SLinus Torvalds 		make_pages_present(addr, start);
17281da177e4SLinus Torvalds 	return vma;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds #endif
17311da177e4SLinus Torvalds 
17322c0b3814SHugh Dickins /*
17332c0b3814SHugh Dickins  * Ok - we have the memory areas we should free on the vma list,
17342c0b3814SHugh Dickins  * so release them, and do the vma updates.
17351da177e4SLinus Torvalds  *
17362c0b3814SHugh Dickins  * Called with the mm semaphore held.
17371da177e4SLinus Torvalds  */
17382c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
17391da177e4SLinus Torvalds {
1740365e9c87SHugh Dickins 	/* Update high watermark before we lower total_vm */
1741365e9c87SHugh Dickins 	update_hiwater_vm(mm);
17422c0b3814SHugh Dickins 	do {
1743ab50b8edSHugh Dickins 		long nrpages = vma_pages(vma);
17441da177e4SLinus Torvalds 
1745ab50b8edSHugh Dickins 		mm->total_vm -= nrpages;
1746ab50b8edSHugh Dickins 		if (vma->vm_flags & VM_LOCKED)
1747ab50b8edSHugh Dickins 			mm->locked_vm -= nrpages;
1748ab50b8edSHugh Dickins 		vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
1749a8fb5618SHugh Dickins 		vma = remove_vma(vma);
1750146425a3SHugh Dickins 	} while (vma);
17511da177e4SLinus Torvalds 	validate_mm(mm);
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds /*
17551da177e4SLinus Torvalds  * Get rid of page table information in the indicated region.
17561da177e4SLinus Torvalds  *
1757f10df686SPaolo 'Blaisorblade' Giarrusso  * Called with the mm semaphore held.
17581da177e4SLinus Torvalds  */
17591da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm,
1760e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
1761e0da382cSHugh Dickins 		unsigned long start, unsigned long end)
17621da177e4SLinus Torvalds {
1763e0da382cSHugh Dickins 	struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
17641da177e4SLinus Torvalds 	struct mmu_gather *tlb;
17651da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	lru_add_drain();
17681da177e4SLinus Torvalds 	tlb = tlb_gather_mmu(mm, 0);
1769365e9c87SHugh Dickins 	update_hiwater_rss(mm);
1770508034a3SHugh Dickins 	unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
17711da177e4SLinus Torvalds 	vm_unacct_memory(nr_accounted);
177242b77728SJan Beulich 	free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1773e0da382cSHugh Dickins 				 next? next->vm_start: 0);
17741da177e4SLinus Torvalds 	tlb_finish_mmu(tlb, start, end);
17751da177e4SLinus Torvalds }
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds /*
17781da177e4SLinus Torvalds  * Create a list of vma's touched by the unmap, removing them from the mm's
17791da177e4SLinus Torvalds  * vma list as we go..
17801da177e4SLinus Torvalds  */
17811da177e4SLinus Torvalds static void
17821da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
17831da177e4SLinus Torvalds 	struct vm_area_struct *prev, unsigned long end)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	struct vm_area_struct **insertion_point;
17861da177e4SLinus Torvalds 	struct vm_area_struct *tail_vma = NULL;
17871363c3cdSWolfgang Wander 	unsigned long addr;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	insertion_point = (prev ? &prev->vm_next : &mm->mmap);
17901da177e4SLinus Torvalds 	do {
17911da177e4SLinus Torvalds 		rb_erase(&vma->vm_rb, &mm->mm_rb);
17921da177e4SLinus Torvalds 		mm->map_count--;
17931da177e4SLinus Torvalds 		tail_vma = vma;
17941da177e4SLinus Torvalds 		vma = vma->vm_next;
17951da177e4SLinus Torvalds 	} while (vma && vma->vm_start < end);
17961da177e4SLinus Torvalds 	*insertion_point = vma;
17971da177e4SLinus Torvalds 	tail_vma->vm_next = NULL;
17981363c3cdSWolfgang Wander 	if (mm->unmap_area == arch_unmap_area)
17991363c3cdSWolfgang Wander 		addr = prev ? prev->vm_end : mm->mmap_base;
18001363c3cdSWolfgang Wander 	else
18011363c3cdSWolfgang Wander 		addr = vma ?  vma->vm_start : mm->mmap_base;
18021363c3cdSWolfgang Wander 	mm->unmap_area(mm, addr);
18031da177e4SLinus Torvalds 	mm->mmap_cache = NULL;		/* Kill the cache. */
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds /*
18071da177e4SLinus Torvalds  * Split a vma into two pieces at address 'addr', a new vma is allocated
180859c51591SMichael Opdenacker  * either for the first part or the tail.
18091da177e4SLinus Torvalds  */
18101da177e4SLinus Torvalds int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
18111da177e4SLinus Torvalds 	      unsigned long addr, int new_below)
18121da177e4SLinus Torvalds {
18131da177e4SLinus Torvalds 	struct mempolicy *pol;
18141da177e4SLinus Torvalds 	struct vm_area_struct *new;
18151da177e4SLinus Torvalds 
1816a5516438SAndi Kleen 	if (is_vm_hugetlb_page(vma) && (addr &
1817a5516438SAndi Kleen 					~(huge_page_mask(hstate_vma(vma)))))
18181da177e4SLinus Torvalds 		return -EINVAL;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (mm->map_count >= sysctl_max_map_count)
18211da177e4SLinus Torvalds 		return -ENOMEM;
18221da177e4SLinus Torvalds 
1823e94b1766SChristoph Lameter 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
18241da177e4SLinus Torvalds 	if (!new)
18251da177e4SLinus Torvalds 		return -ENOMEM;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	/* most fields are the same, copy all, and then fixup */
18281da177e4SLinus Torvalds 	*new = *vma;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	if (new_below)
18311da177e4SLinus Torvalds 		new->vm_end = addr;
18321da177e4SLinus Torvalds 	else {
18331da177e4SLinus Torvalds 		new->vm_start = addr;
18341da177e4SLinus Torvalds 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 
1837846a16bfSLee Schermerhorn 	pol = mpol_dup(vma_policy(vma));
18381da177e4SLinus Torvalds 	if (IS_ERR(pol)) {
18391da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, new);
18401da177e4SLinus Torvalds 		return PTR_ERR(pol);
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 	vma_set_policy(new, pol);
18431da177e4SLinus Torvalds 
1844925d1c40SMatt Helsley 	if (new->vm_file) {
18451da177e4SLinus Torvalds 		get_file(new->vm_file);
1846925d1c40SMatt Helsley 		if (vma->vm_flags & VM_EXECUTABLE)
1847925d1c40SMatt Helsley 			added_exe_file_vma(mm);
1848925d1c40SMatt Helsley 	}
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	if (new->vm_ops && new->vm_ops->open)
18511da177e4SLinus Torvalds 		new->vm_ops->open(new);
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	if (new_below)
18541da177e4SLinus Torvalds 		vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
18551da177e4SLinus Torvalds 			((addr - new->vm_start) >> PAGE_SHIFT), new);
18561da177e4SLinus Torvalds 	else
18571da177e4SLinus Torvalds 		vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	return 0;
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds
18631da177e4SLinus Torvalds  * what needs doing, and the areas themselves, which do the
18641da177e4SLinus Torvalds  * work.  This now handles partial unmappings.
18651da177e4SLinus Torvalds  * Jeremy Fitzhardinge <jeremy@goop.org>
18661da177e4SLinus Torvalds  */
18671da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
18681da177e4SLinus Torvalds {
18691da177e4SLinus Torvalds 	unsigned long end;
1870146425a3SHugh Dickins 	struct vm_area_struct *vma, *prev, *last;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
18731da177e4SLinus Torvalds 		return -EINVAL;
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	if ((len = PAGE_ALIGN(len)) == 0)
18761da177e4SLinus Torvalds 		return -EINVAL;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	/* Find the first overlapping VMA */
1879146425a3SHugh Dickins 	vma = find_vma_prev(mm, start, &prev);
1880146425a3SHugh Dickins 	if (!vma)
18811da177e4SLinus Torvalds 		return 0;
1882146425a3SHugh Dickins 	/* we have  start < vma->vm_end  */
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	/* if it doesn't overlap, we have nothing.. */
18851da177e4SLinus Torvalds 	end = start + len;
1886146425a3SHugh Dickins 	if (vma->vm_start >= end)
18871da177e4SLinus Torvalds 		return 0;
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 	/*
18901da177e4SLinus Torvalds 	 * If we need to split any vma, do it now to save pain later.
18911da177e4SLinus Torvalds 	 *
18921da177e4SLinus Torvalds 	 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
18931da177e4SLinus Torvalds 	 * unmapped vm_area_struct will remain in use: so lower split_vma
18941da177e4SLinus Torvalds 	 * places tmp vma above, and higher split_vma places tmp vma below.
18951da177e4SLinus Torvalds 	 */
1896146425a3SHugh Dickins 	if (start > vma->vm_start) {
1897146425a3SHugh Dickins 		int error = split_vma(mm, vma, start, 0);
18981da177e4SLinus Torvalds 		if (error)
18991da177e4SLinus Torvalds 			return error;
1900146425a3SHugh Dickins 		prev = vma;
19011da177e4SLinus Torvalds 	}
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	/* Does it split the last one? */
19041da177e4SLinus Torvalds 	last = find_vma(mm, end);
19051da177e4SLinus Torvalds 	if (last && end > last->vm_start) {
19061da177e4SLinus Torvalds 		int error = split_vma(mm, last, end, 1);
19071da177e4SLinus Torvalds 		if (error)
19081da177e4SLinus Torvalds 			return error;
19091da177e4SLinus Torvalds 	}
1910146425a3SHugh Dickins 	vma = prev? prev->vm_next: mm->mmap;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	/*
19131da177e4SLinus Torvalds 	 * Remove the vma's, and unmap the actual pages
19141da177e4SLinus Torvalds 	 */
1915146425a3SHugh Dickins 	detach_vmas_to_be_unmapped(mm, vma, prev, end);
1916146425a3SHugh Dickins 	unmap_region(mm, vma, prev, start, end);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	/* Fix up all other VM information */
19192c0b3814SHugh Dickins 	remove_vma_list(mm, vma);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	return 0;
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds EXPORT_SYMBOL(do_munmap);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds asmlinkage long sys_munmap(unsigned long addr, size_t len)
19271da177e4SLinus Torvalds {
19281da177e4SLinus Torvalds 	int ret;
19291da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	profile_munmap(addr);
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
19341da177e4SLinus Torvalds 	ret = do_munmap(mm, addr, len);
19351da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
19361da177e4SLinus Torvalds 	return ret;
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm)
19401da177e4SLinus Torvalds {
1941a241ec65SPaul E. McKenney #ifdef CONFIG_DEBUG_VM
19421da177e4SLinus Torvalds 	if (unlikely(down_read_trylock(&mm->mmap_sem))) {
19431da177e4SLinus Torvalds 		WARN_ON(1);
19441da177e4SLinus Torvalds 		up_read(&mm->mmap_sem);
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds #endif
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds /*
19501da177e4SLinus Torvalds  *  this is really a simplified "do_mmap".  it only handles
19511da177e4SLinus Torvalds  *  anonymous maps.  eventually we may be able to do some
19521da177e4SLinus Torvalds  *  brk-specific accounting here.
19531da177e4SLinus Torvalds  */
19541da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len)
19551da177e4SLinus Torvalds {
19561da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
19571da177e4SLinus Torvalds 	struct vm_area_struct * vma, * prev;
19581da177e4SLinus Torvalds 	unsigned long flags;
19591da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
19601da177e4SLinus Torvalds 	pgoff_t pgoff = addr >> PAGE_SHIFT;
19613a459756SKirill Korotaev 	int error;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
19641da177e4SLinus Torvalds 	if (!len)
19651da177e4SLinus Torvalds 		return addr;
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	if ((addr + len) > TASK_SIZE || (addr + len) < addr)
19681da177e4SLinus Torvalds 		return -EINVAL;
19691da177e4SLinus Torvalds 
1970cd2579d7SHugh Dickins 	if (is_hugepage_only_range(mm, addr, len))
1971cd2579d7SHugh Dickins 		return -EINVAL;
1972cb07c9a1SDavid Gibson 
197388c3f7a8SRichard Knutsson 	error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
19745a211a5dSEric Paris 	if (error)
19755a211a5dSEric Paris 		return error;
19765a211a5dSEric Paris 
19773a459756SKirill Korotaev 	flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
19783a459756SKirill Korotaev 
19793a459756SKirill Korotaev 	error = arch_mmap_check(addr, len, flags);
19803a459756SKirill Korotaev 	if (error)
19813a459756SKirill Korotaev 		return error;
19823a459756SKirill Korotaev 
19831da177e4SLinus Torvalds 	/*
19841da177e4SLinus Torvalds 	 * mlock MCL_FUTURE?
19851da177e4SLinus Torvalds 	 */
19861da177e4SLinus Torvalds 	if (mm->def_flags & VM_LOCKED) {
19871da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
198893ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
198993ea1d0aSChris Wright 		locked += mm->locked_vm;
19901da177e4SLinus Torvalds 		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
199193ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
19921da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
19931da177e4SLinus Torvalds 			return -EAGAIN;
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	/*
19971da177e4SLinus Torvalds 	 * mm->mmap_sem is required to protect against another thread
19981da177e4SLinus Torvalds 	 * changing the mappings in case we sleep.
19991da177e4SLinus Torvalds 	 */
20001da177e4SLinus Torvalds 	verify_mm_writelocked(mm);
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	/*
20031da177e4SLinus Torvalds 	 * Clear old maps.  this also does some error checking for us
20041da177e4SLinus Torvalds 	 */
20051da177e4SLinus Torvalds  munmap_back:
20061da177e4SLinus Torvalds 	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
20071da177e4SLinus Torvalds 	if (vma && vma->vm_start < addr + len) {
20081da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
20091da177e4SLinus Torvalds 			return -ENOMEM;
20101da177e4SLinus Torvalds 		goto munmap_back;
20111da177e4SLinus Torvalds 	}
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	/* Check against address space limits *after* clearing old maps... */
2014119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
20151da177e4SLinus Torvalds 		return -ENOMEM;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
20181da177e4SLinus Torvalds 		return -ENOMEM;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	if (security_vm_enough_memory(len >> PAGE_SHIFT))
20211da177e4SLinus Torvalds 		return -ENOMEM;
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	/* Can we just expand an old private anonymous mapping? */
20241da177e4SLinus Torvalds 	if (vma_merge(mm, prev, addr, addr + len, flags,
20251da177e4SLinus Torvalds 					NULL, NULL, pgoff, NULL))
20261da177e4SLinus Torvalds 		goto out;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	/*
20291da177e4SLinus Torvalds 	 * create a vma struct for an anonymous mapping
20301da177e4SLinus Torvalds 	 */
2031c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
20321da177e4SLinus Torvalds 	if (!vma) {
20331da177e4SLinus Torvalds 		vm_unacct_memory(len >> PAGE_SHIFT);
20341da177e4SLinus Torvalds 		return -ENOMEM;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	vma->vm_mm = mm;
20381da177e4SLinus Torvalds 	vma->vm_start = addr;
20391da177e4SLinus Torvalds 	vma->vm_end = addr + len;
20401da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
20411da177e4SLinus Torvalds 	vma->vm_flags = flags;
20423ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(flags);
20431da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
20441da177e4SLinus Torvalds out:
20451da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
20461da177e4SLinus Torvalds 	if (flags & VM_LOCKED) {
20471da177e4SLinus Torvalds 		mm->locked_vm += len >> PAGE_SHIFT;
20481da177e4SLinus Torvalds 		make_pages_present(addr, addr + len);
20491da177e4SLinus Torvalds 	}
20501da177e4SLinus Torvalds 	return addr;
20511da177e4SLinus Torvalds }
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds EXPORT_SYMBOL(do_brk);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds /* Release all mmaps. */
20561da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
20571da177e4SLinus Torvalds {
20581da177e4SLinus Torvalds 	struct mmu_gather *tlb;
2059e0da382cSHugh Dickins 	struct vm_area_struct *vma = mm->mmap;
20601da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
2061ee39b37bSHugh Dickins 	unsigned long end;
20621da177e4SLinus Torvalds 
2063d6dd61c8SJeremy Fitzhardinge 	/* mm's last user has gone, and its about to be pulled down */
2064d6dd61c8SJeremy Fitzhardinge 	arch_exit_mmap(mm);
2065cddb8a5cSAndrea Arcangeli 	mmu_notifier_release(mm);
2066d6dd61c8SJeremy Fitzhardinge 
20671da177e4SLinus Torvalds 	lru_add_drain();
20681da177e4SLinus Torvalds 	flush_cache_mm(mm);
2069e0da382cSHugh Dickins 	tlb = tlb_gather_mmu(mm, 1);
2070365e9c87SHugh Dickins 	/* Don't update_hiwater_rss(mm) here, do_exit already did */
2071e0da382cSHugh Dickins 	/* Use -1 here to ensure all VMAs in the mm are unmapped */
2072508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
20731da177e4SLinus Torvalds 	vm_unacct_memory(nr_accounted);
207442b77728SJan Beulich 	free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
2075ee39b37bSHugh Dickins 	tlb_finish_mmu(tlb, 0, end);
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	/*
20788f4f8c16SHugh Dickins 	 * Walk the list again, actually closing and freeing it,
20798f4f8c16SHugh Dickins 	 * with preemption enabled, without holding any MM locks.
20801da177e4SLinus Torvalds 	 */
2081a8fb5618SHugh Dickins 	while (vma)
2082a8fb5618SHugh Dickins 		vma = remove_vma(vma);
2083e0da382cSHugh Dickins 
2084e2cdef8cSHugh Dickins 	BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address
20881da177e4SLinus Torvalds  * and into the inode's i_mmap tree.  If vm_file is non-NULL
20891da177e4SLinus Torvalds  * then i_mmap_lock is taken here.
20901da177e4SLinus Torvalds  */
20911da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
20921da177e4SLinus Torvalds {
20931da177e4SLinus Torvalds 	struct vm_area_struct * __vma, * prev;
20941da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	/*
20971da177e4SLinus Torvalds 	 * The vm_pgoff of a purely anonymous vma should be irrelevant
20981da177e4SLinus Torvalds 	 * until its first write fault, when page's anon_vma and index
20991da177e4SLinus Torvalds 	 * are set.  But now set the vm_pgoff it will almost certainly
21001da177e4SLinus Torvalds 	 * end up with (unless mremap moves it elsewhere before that
21011da177e4SLinus Torvalds 	 * first wfault), so /proc/pid/maps tells a consistent story.
21021da177e4SLinus Torvalds 	 *
21031da177e4SLinus Torvalds 	 * By setting it to reflect the virtual start address of the
21041da177e4SLinus Torvalds 	 * vma, merges and splits can happen in a seamless way, just
21051da177e4SLinus Torvalds 	 * using the existing file pgoff checks and manipulations.
21061da177e4SLinus Torvalds 	 * Similarly in do_mmap_pgoff and in do_brk.
21071da177e4SLinus Torvalds 	 */
21081da177e4SLinus Torvalds 	if (!vma->vm_file) {
21091da177e4SLinus Torvalds 		BUG_ON(vma->anon_vma);
21101da177e4SLinus Torvalds 		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
21111da177e4SLinus Torvalds 	}
21121da177e4SLinus Torvalds 	__vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
21131da177e4SLinus Torvalds 	if (__vma && __vma->vm_start < vma->vm_end)
21141da177e4SLinus Torvalds 		return -ENOMEM;
21152fd4ef85SHugh Dickins 	if ((vma->vm_flags & VM_ACCOUNT) &&
211634b4e4aaSAlan Cox 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
21172fd4ef85SHugh Dickins 		return -ENOMEM;
21181da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
21191da177e4SLinus Torvalds 	return 0;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds /*
21231da177e4SLinus Torvalds  * Copy the vma structure to a new location in the same mm,
21241da177e4SLinus Torvalds  * prior to moving page table entries, to effect an mremap move.
21251da177e4SLinus Torvalds  */
21261da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
21271da177e4SLinus Torvalds 	unsigned long addr, unsigned long len, pgoff_t pgoff)
21281da177e4SLinus Torvalds {
21291da177e4SLinus Torvalds 	struct vm_area_struct *vma = *vmap;
21301da177e4SLinus Torvalds 	unsigned long vma_start = vma->vm_start;
21311da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
21321da177e4SLinus Torvalds 	struct vm_area_struct *new_vma, *prev;
21331da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
21341da177e4SLinus Torvalds 	struct mempolicy *pol;
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	/*
21371da177e4SLinus Torvalds 	 * If anonymous vma has not yet been faulted, update new pgoff
21381da177e4SLinus Torvalds 	 * to match new location, to increase its chance of merging.
21391da177e4SLinus Torvalds 	 */
21401da177e4SLinus Torvalds 	if (!vma->vm_file && !vma->anon_vma)
21411da177e4SLinus Torvalds 		pgoff = addr >> PAGE_SHIFT;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
21441da177e4SLinus Torvalds 	new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
21451da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
21461da177e4SLinus Torvalds 	if (new_vma) {
21471da177e4SLinus Torvalds 		/*
21481da177e4SLinus Torvalds 		 * Source vma may have been merged into new_vma
21491da177e4SLinus Torvalds 		 */
21501da177e4SLinus Torvalds 		if (vma_start >= new_vma->vm_start &&
21511da177e4SLinus Torvalds 		    vma_start < new_vma->vm_end)
21521da177e4SLinus Torvalds 			*vmap = new_vma;
21531da177e4SLinus Torvalds 	} else {
2154e94b1766SChristoph Lameter 		new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
21551da177e4SLinus Torvalds 		if (new_vma) {
21561da177e4SLinus Torvalds 			*new_vma = *vma;
2157846a16bfSLee Schermerhorn 			pol = mpol_dup(vma_policy(vma));
21581da177e4SLinus Torvalds 			if (IS_ERR(pol)) {
21591da177e4SLinus Torvalds 				kmem_cache_free(vm_area_cachep, new_vma);
21601da177e4SLinus Torvalds 				return NULL;
21611da177e4SLinus Torvalds 			}
21621da177e4SLinus Torvalds 			vma_set_policy(new_vma, pol);
21631da177e4SLinus Torvalds 			new_vma->vm_start = addr;
21641da177e4SLinus Torvalds 			new_vma->vm_end = addr + len;
21651da177e4SLinus Torvalds 			new_vma->vm_pgoff = pgoff;
2166925d1c40SMatt Helsley 			if (new_vma->vm_file) {
21671da177e4SLinus Torvalds 				get_file(new_vma->vm_file);
2168925d1c40SMatt Helsley 				if (vma->vm_flags & VM_EXECUTABLE)
2169925d1c40SMatt Helsley 					added_exe_file_vma(mm);
2170925d1c40SMatt Helsley 			}
21711da177e4SLinus Torvalds 			if (new_vma->vm_ops && new_vma->vm_ops->open)
21721da177e4SLinus Torvalds 				new_vma->vm_ops->open(new_vma);
21731da177e4SLinus Torvalds 			vma_link(mm, new_vma, prev, rb_link, rb_parent);
21741da177e4SLinus Torvalds 		}
21751da177e4SLinus Torvalds 	}
21761da177e4SLinus Torvalds 	return new_vma;
21771da177e4SLinus Torvalds }
2178119f657cSakpm@osdl.org 
2179119f657cSakpm@osdl.org /*
2180119f657cSakpm@osdl.org  * Return true if the calling process may expand its vm space by the passed
2181119f657cSakpm@osdl.org  * number of pages
2182119f657cSakpm@osdl.org  */
2183119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2184119f657cSakpm@osdl.org {
2185119f657cSakpm@osdl.org 	unsigned long cur = mm->total_vm;	/* pages */
2186119f657cSakpm@osdl.org 	unsigned long lim;
2187119f657cSakpm@osdl.org 
2188119f657cSakpm@osdl.org 	lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2189119f657cSakpm@osdl.org 
2190119f657cSakpm@osdl.org 	if (cur + npages > lim)
2191119f657cSakpm@osdl.org 		return 0;
2192119f657cSakpm@osdl.org 	return 1;
2193119f657cSakpm@osdl.org }
2194fa5dc22fSRoland McGrath 
2195fa5dc22fSRoland McGrath 
2196b1d0e4f5SNick Piggin static int special_mapping_fault(struct vm_area_struct *vma,
2197b1d0e4f5SNick Piggin 				struct vm_fault *vmf)
2198fa5dc22fSRoland McGrath {
2199b1d0e4f5SNick Piggin 	pgoff_t pgoff;
2200fa5dc22fSRoland McGrath 	struct page **pages;
2201fa5dc22fSRoland McGrath 
2202b1d0e4f5SNick Piggin 	/*
2203b1d0e4f5SNick Piggin 	 * special mappings have no vm_file, and in that case, the mm
2204b1d0e4f5SNick Piggin 	 * uses vm_pgoff internally. So we have to subtract it from here.
2205b1d0e4f5SNick Piggin 	 * We are allowed to do this because we are the mm; do not copy
2206b1d0e4f5SNick Piggin 	 * this code into drivers!
2207b1d0e4f5SNick Piggin 	 */
2208b1d0e4f5SNick Piggin 	pgoff = vmf->pgoff - vma->vm_pgoff;
2209fa5dc22fSRoland McGrath 
2210b1d0e4f5SNick Piggin 	for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2211b1d0e4f5SNick Piggin 		pgoff--;
2212fa5dc22fSRoland McGrath 
2213fa5dc22fSRoland McGrath 	if (*pages) {
2214fa5dc22fSRoland McGrath 		struct page *page = *pages;
2215fa5dc22fSRoland McGrath 		get_page(page);
2216b1d0e4f5SNick Piggin 		vmf->page = page;
2217b1d0e4f5SNick Piggin 		return 0;
2218fa5dc22fSRoland McGrath 	}
2219fa5dc22fSRoland McGrath 
2220b1d0e4f5SNick Piggin 	return VM_FAULT_SIGBUS;
2221fa5dc22fSRoland McGrath }
2222fa5dc22fSRoland McGrath 
2223fa5dc22fSRoland McGrath /*
2224fa5dc22fSRoland McGrath  * Having a close hook prevents vma merging regardless of flags.
2225fa5dc22fSRoland McGrath  */
2226fa5dc22fSRoland McGrath static void special_mapping_close(struct vm_area_struct *vma)
2227fa5dc22fSRoland McGrath {
2228fa5dc22fSRoland McGrath }
2229fa5dc22fSRoland McGrath 
2230fa5dc22fSRoland McGrath static struct vm_operations_struct special_mapping_vmops = {
2231fa5dc22fSRoland McGrath 	.close = special_mapping_close,
2232b1d0e4f5SNick Piggin 	.fault = special_mapping_fault,
2233fa5dc22fSRoland McGrath };
2234fa5dc22fSRoland McGrath 
2235fa5dc22fSRoland McGrath /*
2236fa5dc22fSRoland McGrath  * Called with mm->mmap_sem held for writing.
2237fa5dc22fSRoland McGrath  * Insert a new vma covering the given region, with the given flags.
2238fa5dc22fSRoland McGrath  * Its pages are supplied by the given array of struct page *.
2239fa5dc22fSRoland McGrath  * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2240fa5dc22fSRoland McGrath  * The region past the last page supplied will always produce SIGBUS.
2241fa5dc22fSRoland McGrath  * The array pointer and the pages it points to are assumed to stay alive
2242fa5dc22fSRoland McGrath  * for as long as this mapping might exist.
2243fa5dc22fSRoland McGrath  */
2244fa5dc22fSRoland McGrath int install_special_mapping(struct mm_struct *mm,
2245fa5dc22fSRoland McGrath 			    unsigned long addr, unsigned long len,
2246fa5dc22fSRoland McGrath 			    unsigned long vm_flags, struct page **pages)
2247fa5dc22fSRoland McGrath {
2248fa5dc22fSRoland McGrath 	struct vm_area_struct *vma;
2249fa5dc22fSRoland McGrath 
2250fa5dc22fSRoland McGrath 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2251fa5dc22fSRoland McGrath 	if (unlikely(vma == NULL))
2252fa5dc22fSRoland McGrath 		return -ENOMEM;
2253fa5dc22fSRoland McGrath 
2254fa5dc22fSRoland McGrath 	vma->vm_mm = mm;
2255fa5dc22fSRoland McGrath 	vma->vm_start = addr;
2256fa5dc22fSRoland McGrath 	vma->vm_end = addr + len;
2257fa5dc22fSRoland McGrath 
22582f98735cSNick Piggin 	vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
22593ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2260fa5dc22fSRoland McGrath 
2261fa5dc22fSRoland McGrath 	vma->vm_ops = &special_mapping_vmops;
2262fa5dc22fSRoland McGrath 	vma->vm_private_data = pages;
2263fa5dc22fSRoland McGrath 
2264fa5dc22fSRoland McGrath 	if (unlikely(insert_vm_struct(mm, vma))) {
2265fa5dc22fSRoland McGrath 		kmem_cache_free(vm_area_cachep, vma);
2266fa5dc22fSRoland McGrath 		return -ENOMEM;
2267fa5dc22fSRoland McGrath 	}
2268fa5dc22fSRoland McGrath 
2269fa5dc22fSRoland McGrath 	mm->total_vm += len >> PAGE_SHIFT;
2270fa5dc22fSRoland McGrath 
2271fa5dc22fSRoland McGrath 	return 0;
2272fa5dc22fSRoland McGrath }
22737906d00cSAndrea Arcangeli 
22747906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex);
22757906d00cSAndrea Arcangeli 
2276*454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
22777906d00cSAndrea Arcangeli {
22787906d00cSAndrea Arcangeli 	if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
22797906d00cSAndrea Arcangeli 		/*
22807906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change from under us
22817906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
22827906d00cSAndrea Arcangeli 		 */
2283*454ed842SPeter Zijlstra 		spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
22847906d00cSAndrea Arcangeli 		/*
22857906d00cSAndrea Arcangeli 		 * We can safely modify head.next after taking the
22867906d00cSAndrea Arcangeli 		 * anon_vma->lock. If some other vma in this mm shares
22877906d00cSAndrea Arcangeli 		 * the same anon_vma we won't take it again.
22887906d00cSAndrea Arcangeli 		 *
22897906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
22907906d00cSAndrea Arcangeli 		 * can't change from under us thanks to the
22917906d00cSAndrea Arcangeli 		 * anon_vma->lock.
22927906d00cSAndrea Arcangeli 		 */
22937906d00cSAndrea Arcangeli 		if (__test_and_set_bit(0, (unsigned long *)
22947906d00cSAndrea Arcangeli 				       &anon_vma->head.next))
22957906d00cSAndrea Arcangeli 			BUG();
22967906d00cSAndrea Arcangeli 	}
22977906d00cSAndrea Arcangeli }
22987906d00cSAndrea Arcangeli 
2299*454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
23007906d00cSAndrea Arcangeli {
23017906d00cSAndrea Arcangeli 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
23027906d00cSAndrea Arcangeli 		/*
23037906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change from under us because
23047906d00cSAndrea Arcangeli 		 * we hold the mm_all_locks_mutex.
23057906d00cSAndrea Arcangeli 		 *
23067906d00cSAndrea Arcangeli 		 * Operations on ->flags have to be atomic because
23077906d00cSAndrea Arcangeli 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
23087906d00cSAndrea Arcangeli 		 * mm_all_locks_mutex, there may be other cpus
23097906d00cSAndrea Arcangeli 		 * changing other bitflags in parallel to us.
23107906d00cSAndrea Arcangeli 		 */
23117906d00cSAndrea Arcangeli 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
23127906d00cSAndrea Arcangeli 			BUG();
2313*454ed842SPeter Zijlstra 		spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
23147906d00cSAndrea Arcangeli 	}
23157906d00cSAndrea Arcangeli }
23167906d00cSAndrea Arcangeli 
23177906d00cSAndrea Arcangeli /*
23187906d00cSAndrea Arcangeli  * This operation locks against the VM for all pte/vma/mm related
23197906d00cSAndrea Arcangeli  * operations that could ever happen on a certain mm. This includes
23207906d00cSAndrea Arcangeli  * vmtruncate, try_to_unmap, and all page faults.
23217906d00cSAndrea Arcangeli  *
23227906d00cSAndrea Arcangeli  * The caller must take the mmap_sem in write mode before calling
23237906d00cSAndrea Arcangeli  * mm_take_all_locks(). The caller isn't allowed to release the
23247906d00cSAndrea Arcangeli  * mmap_sem until mm_drop_all_locks() returns.
23257906d00cSAndrea Arcangeli  *
23267906d00cSAndrea Arcangeli  * mmap_sem in write mode is required in order to block all operations
23277906d00cSAndrea Arcangeli  * that could modify pagetables and free pages without need of
23287906d00cSAndrea Arcangeli  * altering the vma layout (for example populate_range() with
23297906d00cSAndrea Arcangeli  * nonlinear vmas). It's also needed in write mode to avoid new
23307906d00cSAndrea Arcangeli  * anon_vmas to be associated with existing vmas.
23317906d00cSAndrea Arcangeli  *
23327906d00cSAndrea Arcangeli  * A single task can't take more than one mm_take_all_locks() in a row
23337906d00cSAndrea Arcangeli  * or it would deadlock.
23347906d00cSAndrea Arcangeli  *
23357906d00cSAndrea Arcangeli  * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
23367906d00cSAndrea Arcangeli  * mapping->flags avoid to take the same lock twice, if more than one
23377906d00cSAndrea Arcangeli  * vma in this mm is backed by the same anon_vma or address_space.
23387906d00cSAndrea Arcangeli  *
23397906d00cSAndrea Arcangeli  * We can take all the locks in random order because the VM code
23407906d00cSAndrea Arcangeli  * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
23417906d00cSAndrea Arcangeli  * takes more than one of them in a row. Secondly we're protected
23427906d00cSAndrea Arcangeli  * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
23437906d00cSAndrea Arcangeli  *
23447906d00cSAndrea Arcangeli  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
23457906d00cSAndrea Arcangeli  * that may have to take thousand of locks.
23467906d00cSAndrea Arcangeli  *
23477906d00cSAndrea Arcangeli  * mm_take_all_locks() can fail if it's interrupted by signals.
23487906d00cSAndrea Arcangeli  */
23497906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm)
23507906d00cSAndrea Arcangeli {
23517906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
23527906d00cSAndrea Arcangeli 	int ret = -EINTR;
23537906d00cSAndrea Arcangeli 
23547906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
23557906d00cSAndrea Arcangeli 
23567906d00cSAndrea Arcangeli 	mutex_lock(&mm_all_locks_mutex);
23577906d00cSAndrea Arcangeli 
23587906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
23597906d00cSAndrea Arcangeli 		if (signal_pending(current))
23607906d00cSAndrea Arcangeli 			goto out_unlock;
23617906d00cSAndrea Arcangeli 		if (vma->anon_vma)
2362*454ed842SPeter Zijlstra 			vm_lock_anon_vma(mm, vma->anon_vma);
23637906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
2364*454ed842SPeter Zijlstra 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
23657906d00cSAndrea Arcangeli 	}
23667906d00cSAndrea Arcangeli 	ret = 0;
23677906d00cSAndrea Arcangeli 
23687906d00cSAndrea Arcangeli out_unlock:
23697906d00cSAndrea Arcangeli 	if (ret)
23707906d00cSAndrea Arcangeli 		mm_drop_all_locks(mm);
23717906d00cSAndrea Arcangeli 
23727906d00cSAndrea Arcangeli 	return ret;
23737906d00cSAndrea Arcangeli }
23747906d00cSAndrea Arcangeli 
23757906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
23767906d00cSAndrea Arcangeli {
23777906d00cSAndrea Arcangeli 	if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
23787906d00cSAndrea Arcangeli 		/*
23797906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change to 0 from under
23807906d00cSAndrea Arcangeli 		 * us because we hold the mm_all_locks_mutex.
23817906d00cSAndrea Arcangeli 		 *
23827906d00cSAndrea Arcangeli 		 * We must however clear the bitflag before unlocking
23837906d00cSAndrea Arcangeli 		 * the vma so the users using the anon_vma->head will
23847906d00cSAndrea Arcangeli 		 * never see our bitflag.
23857906d00cSAndrea Arcangeli 		 *
23867906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
23877906d00cSAndrea Arcangeli 		 * can't change from under us until we release the
23887906d00cSAndrea Arcangeli 		 * anon_vma->lock.
23897906d00cSAndrea Arcangeli 		 */
23907906d00cSAndrea Arcangeli 		if (!__test_and_clear_bit(0, (unsigned long *)
23917906d00cSAndrea Arcangeli 					  &anon_vma->head.next))
23927906d00cSAndrea Arcangeli 			BUG();
23937906d00cSAndrea Arcangeli 		spin_unlock(&anon_vma->lock);
23947906d00cSAndrea Arcangeli 	}
23957906d00cSAndrea Arcangeli }
23967906d00cSAndrea Arcangeli 
23977906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping)
23987906d00cSAndrea Arcangeli {
23997906d00cSAndrea Arcangeli 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
24007906d00cSAndrea Arcangeli 		/*
24017906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
24027906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
24037906d00cSAndrea Arcangeli 		 */
24047906d00cSAndrea Arcangeli 		spin_unlock(&mapping->i_mmap_lock);
24057906d00cSAndrea Arcangeli 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
24067906d00cSAndrea Arcangeli 					&mapping->flags))
24077906d00cSAndrea Arcangeli 			BUG();
24087906d00cSAndrea Arcangeli 	}
24097906d00cSAndrea Arcangeli }
24107906d00cSAndrea Arcangeli 
24117906d00cSAndrea Arcangeli /*
24127906d00cSAndrea Arcangeli  * The mmap_sem cannot be released by the caller until
24137906d00cSAndrea Arcangeli  * mm_drop_all_locks() returns.
24147906d00cSAndrea Arcangeli  */
24157906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm)
24167906d00cSAndrea Arcangeli {
24177906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
24187906d00cSAndrea Arcangeli 
24197906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
24207906d00cSAndrea Arcangeli 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
24217906d00cSAndrea Arcangeli 
24227906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
24237906d00cSAndrea Arcangeli 		if (vma->anon_vma)
24247906d00cSAndrea Arcangeli 			vm_unlock_anon_vma(vma->anon_vma);
24257906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
24267906d00cSAndrea Arcangeli 			vm_unlock_mapping(vma->vm_file->f_mapping);
24277906d00cSAndrea Arcangeli 	}
24287906d00cSAndrea Arcangeli 
24297906d00cSAndrea Arcangeli 	mutex_unlock(&mm_all_locks_mutex);
24307906d00cSAndrea Arcangeli }
2431