xref: /linux/mm/mmap.c (revision 66f0dc481e5b802ab363b979fc1753410c7d82b5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * mm/mmap.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Written by obz.
51da177e4SLinus Torvalds  *
6046c6884SAlan Cox  * Address space accounting code	<alan@lxorguk.ukuu.org.uk>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/slab.h>
104af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/shm.h>
131da177e4SLinus Torvalds #include <linux/mman.h>
141da177e4SLinus Torvalds #include <linux/pagemap.h>
151da177e4SLinus Torvalds #include <linux/swap.h>
161da177e4SLinus Torvalds #include <linux/syscalls.h>
17c59ede7bSRandy.Dunlap #include <linux/capability.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/file.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/personality.h>
221da177e4SLinus Torvalds #include <linux/security.h>
231da177e4SLinus Torvalds #include <linux/hugetlb.h>
241da177e4SLinus Torvalds #include <linux/profile.h>
251da177e4SLinus Torvalds #include <linux/module.h>
261da177e4SLinus Torvalds #include <linux/mount.h>
271da177e4SLinus Torvalds #include <linux/mempolicy.h>
281da177e4SLinus Torvalds #include <linux/rmap.h>
29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
30cdd6c482SIngo Molnar #include <linux/perf_event.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/uaccess.h>
331da177e4SLinus Torvalds #include <asm/cacheflush.h>
341da177e4SLinus Torvalds #include <asm/tlb.h>
35d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h>
361da177e4SLinus Torvalds 
3742b77728SJan Beulich #include "internal.h"
3842b77728SJan Beulich 
393a459756SKirill Korotaev #ifndef arch_mmap_check
403a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags)	(0)
413a459756SKirill Korotaev #endif
423a459756SKirill Korotaev 
4308e7d9b5SMartin Schwidefsky #ifndef arch_rebalance_pgtables
4408e7d9b5SMartin Schwidefsky #define arch_rebalance_pgtables(addr, len)		(addr)
4508e7d9b5SMartin Schwidefsky #endif
4608e7d9b5SMartin Schwidefsky 
47e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm,
48e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
49e0da382cSHugh Dickins 		unsigned long start, unsigned long end);
50e0da382cSHugh Dickins 
511da177e4SLinus Torvalds /*
521da177e4SLinus Torvalds  * WARNING: the debugging will use recursive algorithms so never enable this
531da177e4SLinus Torvalds  * unless you know what you are doing.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds #undef DEBUG_MM_RB
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation.
581da177e4SLinus Torvalds  * this is due to the limited x86 page protection hardware.  The expected
591da177e4SLinus Torvalds  * behavior is in parens:
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * map_type	prot
621da177e4SLinus Torvalds  *		PROT_NONE	PROT_READ	PROT_WRITE	PROT_EXEC
631da177e4SLinus Torvalds  * MAP_SHARED	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
641da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (yes) yes	w: (no) no
651da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * MAP_PRIVATE	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
681da177e4SLinus Torvalds  *		w: (no) no	w: (no) no	w: (copy) copy	w: (no) no
691da177e4SLinus Torvalds  *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds pgprot_t protection_map[16] = {
731da177e4SLinus Torvalds 	__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
741da177e4SLinus Torvalds 	__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
751da177e4SLinus Torvalds };
761da177e4SLinus Torvalds 
77804af2cfSHugh Dickins pgprot_t vm_get_page_prot(unsigned long vm_flags)
78804af2cfSHugh Dickins {
79b845f313SDave Kleikamp 	return __pgprot(pgprot_val(protection_map[vm_flags &
80b845f313SDave Kleikamp 				(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
81b845f313SDave Kleikamp 			pgprot_val(arch_vm_get_page_prot(vm_flags)));
82804af2cfSHugh Dickins }
83804af2cfSHugh Dickins EXPORT_SYMBOL(vm_get_page_prot);
84804af2cfSHugh Dickins 
851da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS;  /* heuristic overcommit */
861da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50;	/* default is 50% */
87c3d8c141SChristoph Lameter int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
8800a62ce9SKOSAKI Motohiro struct percpu_counter vm_committed_as;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
921da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
931da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
941da177e4SLinus Torvalds  *
951da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
961da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
991da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1021da177e4SLinus Torvalds  *
1031da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
1041da177e4SLinus Torvalds  * wish to use this logic.
1051da177e4SLinus Torvalds  */
10634b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	unsigned long free, allowed;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	vm_acct_memory(pages);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/*
1131da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
1141da177e4SLinus Torvalds 	 */
1151da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1161da177e4SLinus Torvalds 		return 0;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1191da177e4SLinus Torvalds 		unsigned long n;
1201da177e4SLinus Torvalds 
121347ce434SChristoph Lameter 		free = global_page_state(NR_FILE_PAGES);
1221da177e4SLinus Torvalds 		free += nr_swap_pages;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 		/*
1251da177e4SLinus Torvalds 		 * Any slabs which are created with the
1261da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1271da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
1281da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
1291da177e4SLinus Torvalds 		 */
130972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 		/*
1331da177e4SLinus Torvalds 		 * Leave the last 3% for root
1341da177e4SLinus Torvalds 		 */
1351da177e4SLinus Torvalds 		if (!cap_sys_admin)
1361da177e4SLinus Torvalds 			free -= free / 32;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 		if (free > pages)
1391da177e4SLinus Torvalds 			return 0;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 		/*
1421da177e4SLinus Torvalds 		 * nr_free_pages() is very expensive on large systems,
1431da177e4SLinus Torvalds 		 * only call if we're about to fail.
1441da177e4SLinus Torvalds 		 */
1451da177e4SLinus Torvalds 		n = nr_free_pages();
1466d9f7839SHideo AOKI 
1476d9f7839SHideo AOKI 		/*
1486d9f7839SHideo AOKI 		 * Leave reserved pages. The pages are not for anonymous pages.
1496d9f7839SHideo AOKI 		 */
1506d9f7839SHideo AOKI 		if (n <= totalreserve_pages)
1516d9f7839SHideo AOKI 			goto error;
1526d9f7839SHideo AOKI 		else
1536d9f7839SHideo AOKI 			n -= totalreserve_pages;
1546d9f7839SHideo AOKI 
1556d9f7839SHideo AOKI 		/*
1566d9f7839SHideo AOKI 		 * Leave the last 3% for root
1576d9f7839SHideo AOKI 		 */
1581da177e4SLinus Torvalds 		if (!cap_sys_admin)
1591da177e4SLinus Torvalds 			n -= n / 32;
1601da177e4SLinus Torvalds 		free += n;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 		if (free > pages)
1631da177e4SLinus Torvalds 			return 0;
1646d9f7839SHideo AOKI 
1656d9f7839SHideo AOKI 		goto error;
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	allowed = (totalram_pages - hugetlb_total_pages())
1691da177e4SLinus Torvalds 	       	* sysctl_overcommit_ratio / 100;
1701da177e4SLinus Torvalds 	/*
1711da177e4SLinus Torvalds 	 * Leave the last 3% for root
1721da177e4SLinus Torvalds 	 */
1731da177e4SLinus Torvalds 	if (!cap_sys_admin)
1741da177e4SLinus Torvalds 		allowed -= allowed / 32;
1751da177e4SLinus Torvalds 	allowed += total_swap_pages;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
1781da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
179731572d3SAlan Cox 	if (mm)
18034b4e4aaSAlan Cox 		allowed -= mm->total_vm / 32;
1811da177e4SLinus Torvalds 
18200a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1831da177e4SLinus Torvalds 		return 0;
1846d9f7839SHideo AOKI error:
1851da177e4SLinus Torvalds 	vm_unacct_memory(pages);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	return -ENOMEM;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Requires inode->i_mapping->i_mmap_lock
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma,
1941da177e4SLinus Torvalds 		struct file *file, struct address_space *mapping)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	if (vma->vm_flags & VM_DENYWRITE)
197d3ac7f89SJosef "Jeff" Sipek 		atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
1981da177e4SLinus Torvalds 	if (vma->vm_flags & VM_SHARED)
1991da177e4SLinus Torvalds 		mapping->i_mmap_writable--;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	flush_dcache_mmap_lock(mapping);
2021da177e4SLinus Torvalds 	if (unlikely(vma->vm_flags & VM_NONLINEAR))
2031da177e4SLinus Torvalds 		list_del_init(&vma->shared.vm_set.list);
2041da177e4SLinus Torvalds 	else
2051da177e4SLinus Torvalds 		vma_prio_tree_remove(vma, &mapping->i_mmap);
2061da177e4SLinus Torvalds 	flush_dcache_mmap_unlock(mapping);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /*
210a8fb5618SHugh Dickins  * Unlink a file-based vm structure from its prio_tree, to hide
211a8fb5618SHugh Dickins  * vma from rmap and vmtruncate before freeing its page tables.
2121da177e4SLinus Torvalds  */
213a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (file) {
2181da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
2191da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
2201da177e4SLinus Torvalds 		__remove_shared_vm_struct(vma, file, mapping);
2211da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
2221da177e4SLinus Torvalds 	}
223a8fb5618SHugh Dickins }
224a8fb5618SHugh Dickins 
225a8fb5618SHugh Dickins /*
226a8fb5618SHugh Dickins  * Close a vm structure and free it, returning the next.
227a8fb5618SHugh Dickins  */
228a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
229a8fb5618SHugh Dickins {
230a8fb5618SHugh Dickins 	struct vm_area_struct *next = vma->vm_next;
231a8fb5618SHugh Dickins 
232a8fb5618SHugh Dickins 	might_sleep();
2331da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
2341da177e4SLinus Torvalds 		vma->vm_ops->close(vma);
235925d1c40SMatt Helsley 	if (vma->vm_file) {
236a8fb5618SHugh Dickins 		fput(vma->vm_file);
237925d1c40SMatt Helsley 		if (vma->vm_flags & VM_EXECUTABLE)
238925d1c40SMatt Helsley 			removed_exe_file_vma(vma->vm_mm);
239925d1c40SMatt Helsley 	}
240f0be3d32SLee Schermerhorn 	mpol_put(vma_policy(vma));
2411da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
242a8fb5618SHugh Dickins 	return next;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2456a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	unsigned long rlim, retval;
2481da177e4SLinus Torvalds 	unsigned long newbrk, oldbrk;
2491da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
250a5b4592cSJiri Kosina 	unsigned long min_brk;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
2531da177e4SLinus Torvalds 
254a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK
255a5b4592cSJiri Kosina 	min_brk = mm->end_code;
256a5b4592cSJiri Kosina #else
257a5b4592cSJiri Kosina 	min_brk = mm->start_brk;
258a5b4592cSJiri Kosina #endif
259a5b4592cSJiri Kosina 	if (brk < min_brk)
2601da177e4SLinus Torvalds 		goto out;
2611e624196SRam Gupta 
2621e624196SRam Gupta 	/*
2631e624196SRam Gupta 	 * Check against rlimit here. If this check is done later after the test
2641e624196SRam Gupta 	 * of oldbrk with newbrk then it can escape the test and let the data
2651e624196SRam Gupta 	 * segment grow beyond its set limit the in case where the limit is
2661e624196SRam Gupta 	 * not page aligned -Ram Gupta
2671e624196SRam Gupta 	 */
2681e624196SRam Gupta 	rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
269c1d171a0SJiri Kosina 	if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
270c1d171a0SJiri Kosina 			(mm->end_data - mm->start_data) > rlim)
2711e624196SRam Gupta 		goto out;
2721e624196SRam Gupta 
2731da177e4SLinus Torvalds 	newbrk = PAGE_ALIGN(brk);
2741da177e4SLinus Torvalds 	oldbrk = PAGE_ALIGN(mm->brk);
2751da177e4SLinus Torvalds 	if (oldbrk == newbrk)
2761da177e4SLinus Torvalds 		goto set_brk;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	/* Always allow shrinking brk. */
2791da177e4SLinus Torvalds 	if (brk <= mm->brk) {
2801da177e4SLinus Torvalds 		if (!do_munmap(mm, newbrk, oldbrk-newbrk))
2811da177e4SLinus Torvalds 			goto set_brk;
2821da177e4SLinus Torvalds 		goto out;
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* Check against existing mmap mappings. */
2861da177e4SLinus Torvalds 	if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
2871da177e4SLinus Torvalds 		goto out;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	/* Ok, looks good - let it rip. */
2901da177e4SLinus Torvalds 	if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
2911da177e4SLinus Torvalds 		goto out;
2921da177e4SLinus Torvalds set_brk:
2931da177e4SLinus Torvalds 	mm->brk = brk;
2941da177e4SLinus Torvalds out:
2951da177e4SLinus Torvalds 	retval = mm->brk;
2961da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
2971da177e4SLinus Torvalds 	return retval;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds #ifdef DEBUG_MM_RB
3011da177e4SLinus Torvalds static int browse_rb(struct rb_root *root)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	int i = 0, j;
3041da177e4SLinus Torvalds 	struct rb_node *nd, *pn = NULL;
3051da177e4SLinus Torvalds 	unsigned long prev = 0, pend = 0;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
3081da177e4SLinus Torvalds 		struct vm_area_struct *vma;
3091da177e4SLinus Torvalds 		vma = rb_entry(nd, struct vm_area_struct, vm_rb);
3101da177e4SLinus Torvalds 		if (vma->vm_start < prev)
3111da177e4SLinus Torvalds 			printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
3121da177e4SLinus Torvalds 		if (vma->vm_start < pend)
3131da177e4SLinus Torvalds 			printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
3141da177e4SLinus Torvalds 		if (vma->vm_start > vma->vm_end)
3151da177e4SLinus Torvalds 			printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
3161da177e4SLinus Torvalds 		i++;
3171da177e4SLinus Torvalds 		pn = nd;
318d1af65d1SDavid Miller 		prev = vma->vm_start;
319d1af65d1SDavid Miller 		pend = vma->vm_end;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	j = 0;
3221da177e4SLinus Torvalds 	for (nd = pn; nd; nd = rb_prev(nd)) {
3231da177e4SLinus Torvalds 		j++;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 	if (i != j)
3261da177e4SLinus Torvalds 		printk("backwards %d, forwards %d\n", j, i), i = 0;
3271da177e4SLinus Torvalds 	return i;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds void validate_mm(struct mm_struct *mm)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	int bug = 0;
3331da177e4SLinus Torvalds 	int i = 0;
3341da177e4SLinus Torvalds 	struct vm_area_struct *tmp = mm->mmap;
3351da177e4SLinus Torvalds 	while (tmp) {
3361da177e4SLinus Torvalds 		tmp = tmp->vm_next;
3371da177e4SLinus Torvalds 		i++;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 	if (i != mm->map_count)
3401da177e4SLinus Torvalds 		printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
3411da177e4SLinus Torvalds 	i = browse_rb(&mm->mm_rb);
3421da177e4SLinus Torvalds 	if (i != mm->map_count)
3431da177e4SLinus Torvalds 		printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
34446a350efSEric Sesterhenn 	BUG_ON(bug);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds #else
3471da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0)
3481da177e4SLinus Torvalds #endif
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds static struct vm_area_struct *
3511da177e4SLinus Torvalds find_vma_prepare(struct mm_struct *mm, unsigned long addr,
3521da177e4SLinus Torvalds 		struct vm_area_struct **pprev, struct rb_node ***rb_link,
3531da177e4SLinus Torvalds 		struct rb_node ** rb_parent)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct vm_area_struct * vma;
3561da177e4SLinus Torvalds 	struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	__rb_link = &mm->mm_rb.rb_node;
3591da177e4SLinus Torvalds 	rb_prev = __rb_parent = NULL;
3601da177e4SLinus Torvalds 	vma = NULL;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	while (*__rb_link) {
3631da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 		__rb_parent = *__rb_link;
3661da177e4SLinus Torvalds 		vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		if (vma_tmp->vm_end > addr) {
3691da177e4SLinus Torvalds 			vma = vma_tmp;
3701da177e4SLinus Torvalds 			if (vma_tmp->vm_start <= addr)
371dfe195fbSBenny Halevy 				break;
3721da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_left;
3731da177e4SLinus Torvalds 		} else {
3741da177e4SLinus Torvalds 			rb_prev = __rb_parent;
3751da177e4SLinus Torvalds 			__rb_link = &__rb_parent->rb_right;
3761da177e4SLinus Torvalds 		}
3771da177e4SLinus Torvalds 	}
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	*pprev = NULL;
3801da177e4SLinus Torvalds 	if (rb_prev)
3811da177e4SLinus Torvalds 		*pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
3821da177e4SLinus Torvalds 	*rb_link = __rb_link;
3831da177e4SLinus Torvalds 	*rb_parent = __rb_parent;
3841da177e4SLinus Torvalds 	return vma;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds static inline void
3881da177e4SLinus Torvalds __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
3891da177e4SLinus Torvalds 		struct vm_area_struct *prev, struct rb_node *rb_parent)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	if (prev) {
3921da177e4SLinus Torvalds 		vma->vm_next = prev->vm_next;
3931da177e4SLinus Torvalds 		prev->vm_next = vma;
3941da177e4SLinus Torvalds 	} else {
3951da177e4SLinus Torvalds 		mm->mmap = vma;
3961da177e4SLinus Torvalds 		if (rb_parent)
3971da177e4SLinus Torvalds 			vma->vm_next = rb_entry(rb_parent,
3981da177e4SLinus Torvalds 					struct vm_area_struct, vm_rb);
3991da177e4SLinus Torvalds 		else
4001da177e4SLinus Torvalds 			vma->vm_next = NULL;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
4051da177e4SLinus Torvalds 		struct rb_node **rb_link, struct rb_node *rb_parent)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	rb_link_node(&vma->vm_rb, rb_parent, rb_link);
4081da177e4SLinus Torvalds 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
411cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct file *file;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	file = vma->vm_file;
4161da177e4SLinus Torvalds 	if (file) {
4171da177e4SLinus Torvalds 		struct address_space *mapping = file->f_mapping;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		if (vma->vm_flags & VM_DENYWRITE)
420d3ac7f89SJosef "Jeff" Sipek 			atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
4211da177e4SLinus Torvalds 		if (vma->vm_flags & VM_SHARED)
4221da177e4SLinus Torvalds 			mapping->i_mmap_writable++;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
4251da177e4SLinus Torvalds 		if (unlikely(vma->vm_flags & VM_NONLINEAR))
4261da177e4SLinus Torvalds 			vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
4271da177e4SLinus Torvalds 		else
4281da177e4SLinus Torvalds 			vma_prio_tree_insert(vma, &mapping->i_mmap);
4291da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static void
4341da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4351da177e4SLinus Torvalds 	struct vm_area_struct *prev, struct rb_node **rb_link,
4361da177e4SLinus Torvalds 	struct rb_node *rb_parent)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	__vma_link_list(mm, vma, prev, rb_parent);
4391da177e4SLinus Torvalds 	__vma_link_rb(mm, vma, rb_link, rb_parent);
4401da177e4SLinus Torvalds 	__anon_vma_link(vma);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
4441da177e4SLinus Torvalds 			struct vm_area_struct *prev, struct rb_node **rb_link,
4451da177e4SLinus Torvalds 			struct rb_node *rb_parent)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (vma->vm_file)
4501da177e4SLinus Torvalds 		mapping = vma->vm_file->f_mapping;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if (mapping) {
4531da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
4541da177e4SLinus Torvalds 		vma->vm_truncate_count = mapping->truncate_count;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 	anon_vma_lock(vma);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4591da177e4SLinus Torvalds 	__vma_link_file(vma);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	anon_vma_unlock(vma);
4621da177e4SLinus Torvalds 	if (mapping)
4631da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	mm->map_count++;
4661da177e4SLinus Torvalds 	validate_mm(mm);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * Helper for vma_adjust in the split_vma insert case:
4711da177e4SLinus Torvalds  * insert vm structure into list and rbtree and anon_vma,
4721da177e4SLinus Torvalds  * but it has already been inserted into prio_tree earlier.
4731da177e4SLinus Torvalds  */
47448aae425SZhenwenXu static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	struct vm_area_struct *__vma, *prev;
4771da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	__vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
48046a350efSEric Sesterhenn 	BUG_ON(__vma && __vma->vm_start < vma->vm_end);
4811da177e4SLinus Torvalds 	__vma_link(mm, vma, prev, rb_link, rb_parent);
4821da177e4SLinus Torvalds 	mm->map_count++;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds static inline void
4861da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
4871da177e4SLinus Torvalds 		struct vm_area_struct *prev)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	prev->vm_next = vma->vm_next;
4901da177e4SLinus Torvalds 	rb_erase(&vma->vm_rb, &mm->mm_rb);
4911da177e4SLinus Torvalds 	if (mm->mmap_cache == vma)
4921da177e4SLinus Torvalds 		mm->mmap_cache = prev;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /*
4961da177e4SLinus Torvalds  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
4971da177e4SLinus Torvalds  * is already present in an i_mmap tree without adjusting the tree.
4981da177e4SLinus Torvalds  * The following helper function should be used when such adjustments
4991da177e4SLinus Torvalds  * are necessary.  The "insert" vma (if any) is to be inserted
5001da177e4SLinus Torvalds  * before we drop the necessary locks.
5011da177e4SLinus Torvalds  */
5021da177e4SLinus Torvalds void vma_adjust(struct vm_area_struct *vma, unsigned long start,
5031da177e4SLinus Torvalds 	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
5061da177e4SLinus Torvalds 	struct vm_area_struct *next = vma->vm_next;
5071da177e4SLinus Torvalds 	struct vm_area_struct *importer = NULL;
5081da177e4SLinus Torvalds 	struct address_space *mapping = NULL;
5091da177e4SLinus Torvalds 	struct prio_tree_root *root = NULL;
5101da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
5111da177e4SLinus Torvalds 	struct anon_vma *anon_vma = NULL;
5121da177e4SLinus Torvalds 	long adjust_next = 0;
5131da177e4SLinus Torvalds 	int remove_next = 0;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (next && !insert) {
5161da177e4SLinus Torvalds 		if (end >= next->vm_end) {
5171da177e4SLinus Torvalds 			/*
5181da177e4SLinus Torvalds 			 * vma expands, overlapping all the next, and
5191da177e4SLinus Torvalds 			 * perhaps the one after too (mprotect case 6).
5201da177e4SLinus Torvalds 			 */
5211da177e4SLinus Torvalds again:			remove_next = 1 + (end > next->vm_end);
5221da177e4SLinus Torvalds 			end = next->vm_end;
5231da177e4SLinus Torvalds 			anon_vma = next->anon_vma;
5241da177e4SLinus Torvalds 			importer = vma;
5251da177e4SLinus Torvalds 		} else if (end > next->vm_start) {
5261da177e4SLinus Torvalds 			/*
5271da177e4SLinus Torvalds 			 * vma expands, overlapping part of the next:
5281da177e4SLinus Torvalds 			 * mprotect case 5 shifting the boundary up.
5291da177e4SLinus Torvalds 			 */
5301da177e4SLinus Torvalds 			adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
5311da177e4SLinus Torvalds 			anon_vma = next->anon_vma;
5321da177e4SLinus Torvalds 			importer = vma;
5331da177e4SLinus Torvalds 		} else if (end < vma->vm_end) {
5341da177e4SLinus Torvalds 			/*
5351da177e4SLinus Torvalds 			 * vma shrinks, and !insert tells it's not
5361da177e4SLinus Torvalds 			 * split_vma inserting another: so it must be
5371da177e4SLinus Torvalds 			 * mprotect case 4 shifting the boundary down.
5381da177e4SLinus Torvalds 			 */
5391da177e4SLinus Torvalds 			adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
5401da177e4SLinus Torvalds 			anon_vma = next->anon_vma;
5411da177e4SLinus Torvalds 			importer = next;
5421da177e4SLinus Torvalds 		}
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	if (file) {
5461da177e4SLinus Torvalds 		mapping = file->f_mapping;
5471da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_NONLINEAR))
5481da177e4SLinus Torvalds 			root = &mapping->i_mmap;
5491da177e4SLinus Torvalds 		spin_lock(&mapping->i_mmap_lock);
5501da177e4SLinus Torvalds 		if (importer &&
5511da177e4SLinus Torvalds 		    vma->vm_truncate_count != next->vm_truncate_count) {
5521da177e4SLinus Torvalds 			/*
5531da177e4SLinus Torvalds 			 * unmap_mapping_range might be in progress:
5541da177e4SLinus Torvalds 			 * ensure that the expanding vma is rescanned.
5551da177e4SLinus Torvalds 			 */
5561da177e4SLinus Torvalds 			importer->vm_truncate_count = 0;
5571da177e4SLinus Torvalds 		}
5581da177e4SLinus Torvalds 		if (insert) {
5591da177e4SLinus Torvalds 			insert->vm_truncate_count = vma->vm_truncate_count;
5601da177e4SLinus Torvalds 			/*
5611da177e4SLinus Torvalds 			 * Put into prio_tree now, so instantiated pages
5621da177e4SLinus Torvalds 			 * are visible to arm/parisc __flush_dcache_page
5631da177e4SLinus Torvalds 			 * throughout; but we cannot insert into address
5641da177e4SLinus Torvalds 			 * space until vma start or end is updated.
5651da177e4SLinus Torvalds 			 */
5661da177e4SLinus Torvalds 			__vma_link_file(insert);
5671da177e4SLinus Torvalds 		}
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/*
5711da177e4SLinus Torvalds 	 * When changing only vma->vm_end, we don't really need
572252c5f94SLee Schermerhorn 	 * anon_vma lock.
5731da177e4SLinus Torvalds 	 */
574252c5f94SLee Schermerhorn 	if (vma->anon_vma && (insert || importer || start != vma->vm_start))
5751da177e4SLinus Torvalds 		anon_vma = vma->anon_vma;
5761da177e4SLinus Torvalds 	if (anon_vma) {
5771da177e4SLinus Torvalds 		spin_lock(&anon_vma->lock);
5781da177e4SLinus Torvalds 		/*
5791da177e4SLinus Torvalds 		 * Easily overlooked: when mprotect shifts the boundary,
5801da177e4SLinus Torvalds 		 * make sure the expanding vma has anon_vma set if the
5811da177e4SLinus Torvalds 		 * shrinking vma had, to cover any anon pages imported.
5821da177e4SLinus Torvalds 		 */
5831da177e4SLinus Torvalds 		if (importer && !importer->anon_vma) {
5841da177e4SLinus Torvalds 			importer->anon_vma = anon_vma;
5851da177e4SLinus Torvalds 			__anon_vma_link(importer);
5861da177e4SLinus Torvalds 		}
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	if (root) {
5901da177e4SLinus Torvalds 		flush_dcache_mmap_lock(mapping);
5911da177e4SLinus Torvalds 		vma_prio_tree_remove(vma, root);
5921da177e4SLinus Torvalds 		if (adjust_next)
5931da177e4SLinus Torvalds 			vma_prio_tree_remove(next, root);
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	vma->vm_start = start;
5971da177e4SLinus Torvalds 	vma->vm_end = end;
5981da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
5991da177e4SLinus Torvalds 	if (adjust_next) {
6001da177e4SLinus Torvalds 		next->vm_start += adjust_next << PAGE_SHIFT;
6011da177e4SLinus Torvalds 		next->vm_pgoff += adjust_next;
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	if (root) {
6051da177e4SLinus Torvalds 		if (adjust_next)
6061da177e4SLinus Torvalds 			vma_prio_tree_insert(next, root);
6071da177e4SLinus Torvalds 		vma_prio_tree_insert(vma, root);
6081da177e4SLinus Torvalds 		flush_dcache_mmap_unlock(mapping);
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	if (remove_next) {
6121da177e4SLinus Torvalds 		/*
6131da177e4SLinus Torvalds 		 * vma_merge has merged next into vma, and needs
6141da177e4SLinus Torvalds 		 * us to remove next before dropping the locks.
6151da177e4SLinus Torvalds 		 */
6161da177e4SLinus Torvalds 		__vma_unlink(mm, next, vma);
6171da177e4SLinus Torvalds 		if (file)
6181da177e4SLinus Torvalds 			__remove_shared_vm_struct(next, file, mapping);
6191da177e4SLinus Torvalds 		if (next->anon_vma)
6201da177e4SLinus Torvalds 			__anon_vma_merge(vma, next);
6211da177e4SLinus Torvalds 	} else if (insert) {
6221da177e4SLinus Torvalds 		/*
6231da177e4SLinus Torvalds 		 * split_vma has split insert from vma, and needs
6241da177e4SLinus Torvalds 		 * us to insert it before dropping the locks
6251da177e4SLinus Torvalds 		 * (it may either follow vma or precede it).
6261da177e4SLinus Torvalds 		 */
6271da177e4SLinus Torvalds 		__insert_vm_struct(mm, insert);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	if (anon_vma)
6311da177e4SLinus Torvalds 		spin_unlock(&anon_vma->lock);
6321da177e4SLinus Torvalds 	if (mapping)
6331da177e4SLinus Torvalds 		spin_unlock(&mapping->i_mmap_lock);
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	if (remove_next) {
636925d1c40SMatt Helsley 		if (file) {
6371da177e4SLinus Torvalds 			fput(file);
638925d1c40SMatt Helsley 			if (next->vm_flags & VM_EXECUTABLE)
639925d1c40SMatt Helsley 				removed_exe_file_vma(mm);
640925d1c40SMatt Helsley 		}
6411da177e4SLinus Torvalds 		mm->map_count--;
642f0be3d32SLee Schermerhorn 		mpol_put(vma_policy(next));
6431da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, next);
6441da177e4SLinus Torvalds 		/*
6451da177e4SLinus Torvalds 		 * In mprotect's case 6 (see comments on vma_merge),
6461da177e4SLinus Torvalds 		 * we must remove another next too. It would clutter
6471da177e4SLinus Torvalds 		 * up the code too much to do both in one go.
6481da177e4SLinus Torvalds 		 */
6491da177e4SLinus Torvalds 		if (remove_next == 2) {
6501da177e4SLinus Torvalds 			next = vma->vm_next;
6511da177e4SLinus Torvalds 			goto again;
6521da177e4SLinus Torvalds 		}
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	validate_mm(mm);
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds /*
6591da177e4SLinus Torvalds  * If the vma has a ->close operation then the driver probably needs to release
6601da177e4SLinus Torvalds  * per-vma resources, so we don't attempt to merge those.
6611da177e4SLinus Torvalds  */
6621da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma,
6631da177e4SLinus Torvalds 			struct file *file, unsigned long vm_flags)
6641da177e4SLinus Torvalds {
6658314c4f2SHugh Dickins 	/* VM_CAN_NONLINEAR may get set later by f_op->mmap() */
6668314c4f2SHugh Dickins 	if ((vma->vm_flags ^ vm_flags) & ~VM_CAN_NONLINEAR)
6671da177e4SLinus Torvalds 		return 0;
6681da177e4SLinus Torvalds 	if (vma->vm_file != file)
6691da177e4SLinus Torvalds 		return 0;
6701da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->close)
6711da177e4SLinus Torvalds 		return 0;
6721da177e4SLinus Torvalds 	return 1;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
6761da177e4SLinus Torvalds 					struct anon_vma *anon_vma2)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds 	return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds /*
6821da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
6831da177e4SLinus Torvalds  * in front of (at a lower virtual address and file offset than) the vma.
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
6861da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
6871da177e4SLinus Torvalds  *
6881da177e4SLinus Torvalds  * We don't check here for the merged mmap wrapping around the end of pagecache
6891da177e4SLinus Torvalds  * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
6901da177e4SLinus Torvalds  * wrap, nor mmaps which cover the final page at index -1UL.
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds static int
6931da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
6941da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
6971da177e4SLinus Torvalds 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
6981da177e4SLinus Torvalds 		if (vma->vm_pgoff == vm_pgoff)
6991da177e4SLinus Torvalds 			return 1;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 	return 0;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
7061da177e4SLinus Torvalds  * beyond (at a higher virtual address and file offset than) the vma.
7071da177e4SLinus Torvalds  *
7081da177e4SLinus Torvalds  * We cannot merge two vmas if they have differently assigned (non-NULL)
7091da177e4SLinus Torvalds  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
7101da177e4SLinus Torvalds  */
7111da177e4SLinus Torvalds static int
7121da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
7131da177e4SLinus Torvalds 	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	if (is_mergeable_vma(vma, file, vm_flags) &&
7161da177e4SLinus Torvalds 	    is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
7171da177e4SLinus Torvalds 		pgoff_t vm_pglen;
7181da177e4SLinus Torvalds 		vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
7191da177e4SLinus Torvalds 		if (vma->vm_pgoff + vm_pglen == vm_pgoff)
7201da177e4SLinus Torvalds 			return 1;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	return 0;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /*
7261da177e4SLinus Torvalds  * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
7271da177e4SLinus Torvalds  * whether that can be merged with its predecessor or its successor.
7281da177e4SLinus Torvalds  * Or both (it neatly fills a hole).
7291da177e4SLinus Torvalds  *
7301da177e4SLinus Torvalds  * In most cases - when called for mmap, brk or mremap - [addr,end) is
7311da177e4SLinus Torvalds  * certain not to be mapped by the time vma_merge is called; but when
7321da177e4SLinus Torvalds  * called for mprotect, it is certain to be already mapped (either at
7331da177e4SLinus Torvalds  * an offset within prev, or at the start of next), and the flags of
7341da177e4SLinus Torvalds  * this area are about to be changed to vm_flags - and the no-change
7351da177e4SLinus Torvalds  * case has already been eliminated.
7361da177e4SLinus Torvalds  *
7371da177e4SLinus Torvalds  * The following mprotect cases have to be considered, where AAAA is
7381da177e4SLinus Torvalds  * the area passed down from mprotect_fixup, never extending beyond one
7391da177e4SLinus Torvalds  * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
7401da177e4SLinus Torvalds  *
7411da177e4SLinus Torvalds  *     AAAA             AAAA                AAAA          AAAA
7421da177e4SLinus Torvalds  *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
7431da177e4SLinus Torvalds  *    cannot merge    might become    might become    might become
7441da177e4SLinus Torvalds  *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
7451da177e4SLinus Torvalds  *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
7461da177e4SLinus Torvalds  *    mremap move:                                    PPPPNNNNNNNN 8
7471da177e4SLinus Torvalds  *        AAAA
7481da177e4SLinus Torvalds  *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
7491da177e4SLinus Torvalds  *    might become    case 1 below    case 2 below    case 3 below
7501da177e4SLinus Torvalds  *
7511da177e4SLinus Torvalds  * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
7521da177e4SLinus Torvalds  * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
7531da177e4SLinus Torvalds  */
7541da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm,
7551da177e4SLinus Torvalds 			struct vm_area_struct *prev, unsigned long addr,
7561da177e4SLinus Torvalds 			unsigned long end, unsigned long vm_flags,
7571da177e4SLinus Torvalds 		     	struct anon_vma *anon_vma, struct file *file,
7581da177e4SLinus Torvalds 			pgoff_t pgoff, struct mempolicy *policy)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
7611da177e4SLinus Torvalds 	struct vm_area_struct *area, *next;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	/*
7641da177e4SLinus Torvalds 	 * We later require that vma->vm_flags == vm_flags,
7651da177e4SLinus Torvalds 	 * so this tests vma->vm_flags & VM_SPECIAL, too.
7661da177e4SLinus Torvalds 	 */
7671da177e4SLinus Torvalds 	if (vm_flags & VM_SPECIAL)
7681da177e4SLinus Torvalds 		return NULL;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (prev)
7711da177e4SLinus Torvalds 		next = prev->vm_next;
7721da177e4SLinus Torvalds 	else
7731da177e4SLinus Torvalds 		next = mm->mmap;
7741da177e4SLinus Torvalds 	area = next;
7751da177e4SLinus Torvalds 	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
7761da177e4SLinus Torvalds 		next = next->vm_next;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/*
7791da177e4SLinus Torvalds 	 * Can it merge with the predecessor?
7801da177e4SLinus Torvalds 	 */
7811da177e4SLinus Torvalds 	if (prev && prev->vm_end == addr &&
7821da177e4SLinus Torvalds   			mpol_equal(vma_policy(prev), policy) &&
7831da177e4SLinus Torvalds 			can_vma_merge_after(prev, vm_flags,
7841da177e4SLinus Torvalds 						anon_vma, file, pgoff)) {
7851da177e4SLinus Torvalds 		/*
7861da177e4SLinus Torvalds 		 * OK, it can.  Can we now merge in the successor as well?
7871da177e4SLinus Torvalds 		 */
7881da177e4SLinus Torvalds 		if (next && end == next->vm_start &&
7891da177e4SLinus Torvalds 				mpol_equal(policy, vma_policy(next)) &&
7901da177e4SLinus Torvalds 				can_vma_merge_before(next, vm_flags,
7911da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen) &&
7921da177e4SLinus Torvalds 				is_mergeable_anon_vma(prev->anon_vma,
7931da177e4SLinus Torvalds 						      next->anon_vma)) {
7941da177e4SLinus Torvalds 							/* cases 1, 6 */
7951da177e4SLinus Torvalds 			vma_adjust(prev, prev->vm_start,
7961da177e4SLinus Torvalds 				next->vm_end, prev->vm_pgoff, NULL);
7971da177e4SLinus Torvalds 		} else					/* cases 2, 5, 7 */
7981da177e4SLinus Torvalds 			vma_adjust(prev, prev->vm_start,
7991da177e4SLinus Torvalds 				end, prev->vm_pgoff, NULL);
8001da177e4SLinus Torvalds 		return prev;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/*
8041da177e4SLinus Torvalds 	 * Can this new request be merged in front of next?
8051da177e4SLinus Torvalds 	 */
8061da177e4SLinus Torvalds 	if (next && end == next->vm_start &&
8071da177e4SLinus Torvalds  			mpol_equal(policy, vma_policy(next)) &&
8081da177e4SLinus Torvalds 			can_vma_merge_before(next, vm_flags,
8091da177e4SLinus Torvalds 					anon_vma, file, pgoff+pglen)) {
8101da177e4SLinus Torvalds 		if (prev && addr < prev->vm_end)	/* case 4 */
8111da177e4SLinus Torvalds 			vma_adjust(prev, prev->vm_start,
8121da177e4SLinus Torvalds 				addr, prev->vm_pgoff, NULL);
8131da177e4SLinus Torvalds 		else					/* cases 3, 8 */
8141da177e4SLinus Torvalds 			vma_adjust(area, addr, next->vm_end,
8151da177e4SLinus Torvalds 				next->vm_pgoff - pglen, NULL);
8161da177e4SLinus Torvalds 		return area;
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	return NULL;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds /*
8231da177e4SLinus Torvalds  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
8241da177e4SLinus Torvalds  * neighbouring vmas for a suitable anon_vma, before it goes off
8251da177e4SLinus Torvalds  * to allocate a new anon_vma.  It checks because a repetitive
8261da177e4SLinus Torvalds  * sequence of mprotects and faults may otherwise lead to distinct
8271da177e4SLinus Torvalds  * anon_vmas being allocated, preventing vma merge in subsequent
8281da177e4SLinus Torvalds  * mprotect.
8291da177e4SLinus Torvalds  */
8301da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
8311da177e4SLinus Torvalds {
8321da177e4SLinus Torvalds 	struct vm_area_struct *near;
8331da177e4SLinus Torvalds 	unsigned long vm_flags;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	near = vma->vm_next;
8361da177e4SLinus Torvalds 	if (!near)
8371da177e4SLinus Torvalds 		goto try_prev;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	/*
8401da177e4SLinus Torvalds 	 * Since only mprotect tries to remerge vmas, match flags
8411da177e4SLinus Torvalds 	 * which might be mprotected into each other later on.
8421da177e4SLinus Torvalds 	 * Neither mlock nor madvise tries to remerge at present,
8431da177e4SLinus Torvalds 	 * so leave their flags as obstructing a merge.
8441da177e4SLinus Torvalds 	 */
8451da177e4SLinus Torvalds 	vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
8461da177e4SLinus Torvalds 	vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	if (near->anon_vma && vma->vm_end == near->vm_start &&
8491da177e4SLinus Torvalds  			mpol_equal(vma_policy(vma), vma_policy(near)) &&
8501da177e4SLinus Torvalds 			can_vma_merge_before(near, vm_flags,
8511da177e4SLinus Torvalds 				NULL, vma->vm_file, vma->vm_pgoff +
8521da177e4SLinus Torvalds 				((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
8531da177e4SLinus Torvalds 		return near->anon_vma;
8541da177e4SLinus Torvalds try_prev:
8551da177e4SLinus Torvalds 	/*
8561da177e4SLinus Torvalds 	 * It is potentially slow to have to call find_vma_prev here.
8571da177e4SLinus Torvalds 	 * But it's only on the first write fault on the vma, not
8581da177e4SLinus Torvalds 	 * every time, and we could devise a way to avoid it later
8591da177e4SLinus Torvalds 	 * (e.g. stash info in next's anon_vma_node when assigning
8601da177e4SLinus Torvalds 	 * an anon_vma, or when trying vma_merge).  Another time.
8611da177e4SLinus Torvalds 	 */
86246a350efSEric Sesterhenn 	BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
8631da177e4SLinus Torvalds 	if (!near)
8641da177e4SLinus Torvalds 		goto none;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
8671da177e4SLinus Torvalds 	vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	if (near->anon_vma && near->vm_end == vma->vm_start &&
8701da177e4SLinus Torvalds   			mpol_equal(vma_policy(near), vma_policy(vma)) &&
8711da177e4SLinus Torvalds 			can_vma_merge_after(near, vm_flags,
8721da177e4SLinus Torvalds 				NULL, vma->vm_file, vma->vm_pgoff))
8731da177e4SLinus Torvalds 		return near->anon_vma;
8741da177e4SLinus Torvalds none:
8751da177e4SLinus Torvalds 	/*
8761da177e4SLinus Torvalds 	 * There's no absolute need to look only at touching neighbours:
8771da177e4SLinus Torvalds 	 * we could search further afield for "compatible" anon_vmas.
8781da177e4SLinus Torvalds 	 * But it would probably just be a waste of time searching,
8791da177e4SLinus Torvalds 	 * or lead to too many vmas hanging off the same anon_vma.
8801da177e4SLinus Torvalds 	 * We're trying to allow mprotect remerging later on,
8811da177e4SLinus Torvalds 	 * not trying to minimize memory used for anon_vmas.
8821da177e4SLinus Torvalds 	 */
8831da177e4SLinus Torvalds 	return NULL;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
887ab50b8edSHugh Dickins void vm_stat_account(struct mm_struct *mm, unsigned long flags,
8881da177e4SLinus Torvalds 						struct file *file, long pages)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	const unsigned long stack_flags
8911da177e4SLinus Torvalds 		= VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	if (file) {
8941da177e4SLinus Torvalds 		mm->shared_vm += pages;
8951da177e4SLinus Torvalds 		if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
8961da177e4SLinus Torvalds 			mm->exec_vm += pages;
8971da177e4SLinus Torvalds 	} else if (flags & stack_flags)
8981da177e4SLinus Torvalds 		mm->stack_vm += pages;
8991da177e4SLinus Torvalds 	if (flags & (VM_RESERVED|VM_IO))
9001da177e4SLinus Torvalds 		mm->reserved_vm += pages;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds /*
90527f5de79SJianjun Kong  * The caller must hold down_write(&current->mm->mmap_sem).
9061da177e4SLinus Torvalds  */
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
9091da177e4SLinus Torvalds 			unsigned long len, unsigned long prot,
9101da177e4SLinus Torvalds 			unsigned long flags, unsigned long pgoff)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
9131da177e4SLinus Torvalds 	struct inode *inode;
9141da177e4SLinus Torvalds 	unsigned int vm_flags;
9151da177e4SLinus Torvalds 	int error;
9160165ab44SMiklos Szeredi 	unsigned long reqprot = prot;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/*
9191da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC?
9201da177e4SLinus Torvalds 	 *
9211da177e4SLinus Torvalds 	 * (the exception is when the underlying filesystem is noexec
9221da177e4SLinus Torvalds 	 *  mounted, in which case we dont add PROT_EXEC.)
9231da177e4SLinus Torvalds 	 */
9241da177e4SLinus Torvalds 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
925d3ac7f89SJosef "Jeff" Sipek 		if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
9261da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	if (!len)
9291da177e4SLinus Torvalds 		return -EINVAL;
9301da177e4SLinus Torvalds 
9317cd94146SEric Paris 	if (!(flags & MAP_FIXED))
9327cd94146SEric Paris 		addr = round_hint_to_min(addr);
9337cd94146SEric Paris 
9341da177e4SLinus Torvalds 	/* Careful about overflows.. */
9351da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
9369206de95SAl Viro 	if (!len)
9371da177e4SLinus Torvalds 		return -ENOMEM;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	/* offset overflow? */
9401da177e4SLinus Torvalds 	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
9411da177e4SLinus Torvalds                return -EOVERFLOW;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/* Too many mappings? */
9441da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
9451da177e4SLinus Torvalds 		return -ENOMEM;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/* Obtain the address to map to. we verify (or select) it and ensure
9481da177e4SLinus Torvalds 	 * that it represents a valid section of the address space.
9491da177e4SLinus Torvalds 	 */
9501da177e4SLinus Torvalds 	addr = get_unmapped_area(file, addr, len, pgoff, flags);
9511da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
9521da177e4SLinus Torvalds 		return addr;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/* Do simple checking here so the lower-level routines won't have
9551da177e4SLinus Torvalds 	 * to. we assume access permissions have been handled by the open
9561da177e4SLinus Torvalds 	 * of the memory object, so we don't do any here.
9571da177e4SLinus Torvalds 	 */
9581da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
9591da177e4SLinus Torvalds 			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9601da177e4SLinus Torvalds 
961cdf7b341SHuang Shijie 	if (flags & MAP_LOCKED)
9621da177e4SLinus Torvalds 		if (!can_do_mlock())
9631da177e4SLinus Torvalds 			return -EPERM;
964ba470de4SRik van Riel 
9651da177e4SLinus Torvalds 	/* mlock MCL_FUTURE? */
9661da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
9671da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
96893ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
96993ea1d0aSChris Wright 		locked += mm->locked_vm;
9701da177e4SLinus Torvalds 		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
97193ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
9721da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
9731da177e4SLinus Torvalds 			return -EAGAIN;
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 
976d3ac7f89SJosef "Jeff" Sipek 	inode = file ? file->f_path.dentry->d_inode : NULL;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if (file) {
9791da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
9801da177e4SLinus Torvalds 		case MAP_SHARED:
9811da177e4SLinus Torvalds 			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
9821da177e4SLinus Torvalds 				return -EACCES;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 			/*
9851da177e4SLinus Torvalds 			 * Make sure we don't allow writing to an append-only
9861da177e4SLinus Torvalds 			 * file..
9871da177e4SLinus Torvalds 			 */
9881da177e4SLinus Torvalds 			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
9891da177e4SLinus Torvalds 				return -EACCES;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 			/*
9921da177e4SLinus Torvalds 			 * Make sure there are no mandatory locks on the file.
9931da177e4SLinus Torvalds 			 */
9941da177e4SLinus Torvalds 			if (locks_verify_locked(inode))
9951da177e4SLinus Torvalds 				return -EAGAIN;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
9981da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_WRITE))
9991da177e4SLinus Torvalds 				vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 			/* fall through */
10021da177e4SLinus Torvalds 		case MAP_PRIVATE:
10031da177e4SLinus Torvalds 			if (!(file->f_mode & FMODE_READ))
10041da177e4SLinus Torvalds 				return -EACCES;
1005d3ac7f89SJosef "Jeff" Sipek 			if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
100680c5606cSLinus Torvalds 				if (vm_flags & VM_EXEC)
100780c5606cSLinus Torvalds 					return -EPERM;
100880c5606cSLinus Torvalds 				vm_flags &= ~VM_MAYEXEC;
100980c5606cSLinus Torvalds 			}
101080c5606cSLinus Torvalds 
101180c5606cSLinus Torvalds 			if (!file->f_op || !file->f_op->mmap)
101280c5606cSLinus Torvalds 				return -ENODEV;
10131da177e4SLinus Torvalds 			break;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 		default:
10161da177e4SLinus Torvalds 			return -EINVAL;
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 	} else {
10191da177e4SLinus Torvalds 		switch (flags & MAP_TYPE) {
10201da177e4SLinus Torvalds 		case MAP_SHARED:
1021ce363942STejun Heo 			/*
1022ce363942STejun Heo 			 * Ignore pgoff.
1023ce363942STejun Heo 			 */
1024ce363942STejun Heo 			pgoff = 0;
10251da177e4SLinus Torvalds 			vm_flags |= VM_SHARED | VM_MAYSHARE;
10261da177e4SLinus Torvalds 			break;
10271da177e4SLinus Torvalds 		case MAP_PRIVATE:
10281da177e4SLinus Torvalds 			/*
10291da177e4SLinus Torvalds 			 * Set pgoff according to addr for anon_vma.
10301da177e4SLinus Torvalds 			 */
10311da177e4SLinus Torvalds 			pgoff = addr >> PAGE_SHIFT;
10321da177e4SLinus Torvalds 			break;
10331da177e4SLinus Torvalds 		default:
10341da177e4SLinus Torvalds 			return -EINVAL;
10351da177e4SLinus Torvalds 		}
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
1038ed032189SEric Paris 	error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
10391da177e4SLinus Torvalds 	if (error)
10401da177e4SLinus Torvalds 		return error;
10411da177e4SLinus Torvalds 
10425a6fe125SMel Gorman 	return mmap_region(file, addr, len, flags, vm_flags, pgoff);
10430165ab44SMiklos Szeredi }
10440165ab44SMiklos Szeredi EXPORT_SYMBOL(do_mmap_pgoff);
10450165ab44SMiklos Szeredi 
1046*66f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1047*66f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
1048*66f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
1049*66f0dc48SHugh Dickins {
1050*66f0dc48SHugh Dickins 	struct file *file = NULL;
1051*66f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
1052*66f0dc48SHugh Dickins 
1053*66f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
1054*66f0dc48SHugh Dickins 		if (unlikely(flags & MAP_HUGETLB))
1055*66f0dc48SHugh Dickins 			return -EINVAL;
1056*66f0dc48SHugh Dickins 		file = fget(fd);
1057*66f0dc48SHugh Dickins 		if (!file)
1058*66f0dc48SHugh Dickins 			goto out;
1059*66f0dc48SHugh Dickins 	} else if (flags & MAP_HUGETLB) {
1060*66f0dc48SHugh Dickins 		struct user_struct *user = NULL;
1061*66f0dc48SHugh Dickins 		/*
1062*66f0dc48SHugh Dickins 		 * VM_NORESERVE is used because the reservations will be
1063*66f0dc48SHugh Dickins 		 * taken when vm_ops->mmap() is called
1064*66f0dc48SHugh Dickins 		 * A dummy user value is used because we are not locking
1065*66f0dc48SHugh Dickins 		 * memory so no accounting is necessary
1066*66f0dc48SHugh Dickins 		 */
1067*66f0dc48SHugh Dickins 		len = ALIGN(len, huge_page_size(&default_hstate));
1068*66f0dc48SHugh Dickins 		file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, VM_NORESERVE,
1069*66f0dc48SHugh Dickins 						&user, HUGETLB_ANONHUGE_INODE);
1070*66f0dc48SHugh Dickins 		if (IS_ERR(file))
1071*66f0dc48SHugh Dickins 			return PTR_ERR(file);
1072*66f0dc48SHugh Dickins 	}
1073*66f0dc48SHugh Dickins 
1074*66f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1075*66f0dc48SHugh Dickins 
1076*66f0dc48SHugh Dickins 	down_write(&current->mm->mmap_sem);
1077*66f0dc48SHugh Dickins 	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1078*66f0dc48SHugh Dickins 	up_write(&current->mm->mmap_sem);
1079*66f0dc48SHugh Dickins 
1080*66f0dc48SHugh Dickins 	if (file)
1081*66f0dc48SHugh Dickins 		fput(file);
1082*66f0dc48SHugh Dickins out:
1083*66f0dc48SHugh Dickins 	return retval;
1084*66f0dc48SHugh Dickins }
1085*66f0dc48SHugh Dickins 
10864e950f6fSAlexey Dobriyan /*
10874e950f6fSAlexey Dobriyan  * Some shared mappigns will want the pages marked read-only
10884e950f6fSAlexey Dobriyan  * to track write events. If so, we'll downgrade vm_page_prot
10894e950f6fSAlexey Dobriyan  * to the private version (using protection_map[] without the
10904e950f6fSAlexey Dobriyan  * VM_SHARED bit).
10914e950f6fSAlexey Dobriyan  */
10924e950f6fSAlexey Dobriyan int vma_wants_writenotify(struct vm_area_struct *vma)
10934e950f6fSAlexey Dobriyan {
10944e950f6fSAlexey Dobriyan 	unsigned int vm_flags = vma->vm_flags;
10954e950f6fSAlexey Dobriyan 
10964e950f6fSAlexey Dobriyan 	/* If it was private or non-writable, the write bit is already clear */
10974e950f6fSAlexey Dobriyan 	if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
10984e950f6fSAlexey Dobriyan 		return 0;
10994e950f6fSAlexey Dobriyan 
11004e950f6fSAlexey Dobriyan 	/* The backer wishes to know when pages are first written to? */
11014e950f6fSAlexey Dobriyan 	if (vma->vm_ops && vma->vm_ops->page_mkwrite)
11024e950f6fSAlexey Dobriyan 		return 1;
11034e950f6fSAlexey Dobriyan 
11044e950f6fSAlexey Dobriyan 	/* The open routine did something to the protections already? */
11054e950f6fSAlexey Dobriyan 	if (pgprot_val(vma->vm_page_prot) !=
11063ed75eb8SColy Li 	    pgprot_val(vm_get_page_prot(vm_flags)))
11074e950f6fSAlexey Dobriyan 		return 0;
11084e950f6fSAlexey Dobriyan 
11094e950f6fSAlexey Dobriyan 	/* Specialty mapping? */
11104e950f6fSAlexey Dobriyan 	if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
11114e950f6fSAlexey Dobriyan 		return 0;
11124e950f6fSAlexey Dobriyan 
11134e950f6fSAlexey Dobriyan 	/* Can the mapping track the dirty pages? */
11144e950f6fSAlexey Dobriyan 	return vma->vm_file && vma->vm_file->f_mapping &&
11154e950f6fSAlexey Dobriyan 		mapping_cap_account_dirty(vma->vm_file->f_mapping);
11164e950f6fSAlexey Dobriyan }
11174e950f6fSAlexey Dobriyan 
1118fc8744adSLinus Torvalds /*
1119fc8744adSLinus Torvalds  * We account for memory if it's a private writeable mapping,
11205a6fe125SMel Gorman  * not hugepages and VM_NORESERVE wasn't set.
1121fc8744adSLinus Torvalds  */
11225a6fe125SMel Gorman static inline int accountable_mapping(struct file *file, unsigned int vm_flags)
1123fc8744adSLinus Torvalds {
11245a6fe125SMel Gorman 	/*
11255a6fe125SMel Gorman 	 * hugetlb has its own accounting separate from the core VM
11265a6fe125SMel Gorman 	 * VM_HUGETLB may not be set yet so we cannot check for that flag.
11275a6fe125SMel Gorman 	 */
11285a6fe125SMel Gorman 	if (file && is_file_hugepages(file))
11295a6fe125SMel Gorman 		return 0;
11305a6fe125SMel Gorman 
1131fc8744adSLinus Torvalds 	return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1132fc8744adSLinus Torvalds }
1133fc8744adSLinus Torvalds 
11340165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr,
11350165ab44SMiklos Szeredi 			  unsigned long len, unsigned long flags,
11365a6fe125SMel Gorman 			  unsigned int vm_flags, unsigned long pgoff)
11370165ab44SMiklos Szeredi {
11380165ab44SMiklos Szeredi 	struct mm_struct *mm = current->mm;
11390165ab44SMiklos Szeredi 	struct vm_area_struct *vma, *prev;
11400165ab44SMiklos Szeredi 	int correct_wcount = 0;
11410165ab44SMiklos Szeredi 	int error;
11420165ab44SMiklos Szeredi 	struct rb_node **rb_link, *rb_parent;
11430165ab44SMiklos Szeredi 	unsigned long charged = 0;
11440165ab44SMiklos Szeredi 	struct inode *inode =  file ? file->f_path.dentry->d_inode : NULL;
11450165ab44SMiklos Szeredi 
11461da177e4SLinus Torvalds 	/* Clear old maps */
11471da177e4SLinus Torvalds 	error = -ENOMEM;
11481da177e4SLinus Torvalds munmap_back:
11491da177e4SLinus Torvalds 	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
11501da177e4SLinus Torvalds 	if (vma && vma->vm_start < addr + len) {
11511da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
11521da177e4SLinus Torvalds 			return -ENOMEM;
11531da177e4SLinus Torvalds 		goto munmap_back;
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	/* Check against address space limit. */
1157119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
11581da177e4SLinus Torvalds 		return -ENOMEM;
11591da177e4SLinus Torvalds 
1160fc8744adSLinus Torvalds 	/*
1161fc8744adSLinus Torvalds 	 * Set 'VM_NORESERVE' if we should not account for the
11625a6fe125SMel Gorman 	 * memory use of this mapping.
1163fc8744adSLinus Torvalds 	 */
11645a6fe125SMel Gorman 	if ((flags & MAP_NORESERVE)) {
11655a6fe125SMel Gorman 		/* We honor MAP_NORESERVE if allowed to overcommit */
11665a6fe125SMel Gorman 		if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1167fc8744adSLinus Torvalds 			vm_flags |= VM_NORESERVE;
11685a6fe125SMel Gorman 
11695a6fe125SMel Gorman 		/* hugetlb applies strict overcommit unless MAP_NORESERVE */
11705a6fe125SMel Gorman 		if (file && is_file_hugepages(file))
1171cdfd4325SAndy Whitcroft 			vm_flags |= VM_NORESERVE;
11725a6fe125SMel Gorman 	}
1173cdfd4325SAndy Whitcroft 
11741da177e4SLinus Torvalds 	/*
11751da177e4SLinus Torvalds 	 * Private writable mapping: check memory availability
11761da177e4SLinus Torvalds 	 */
11775a6fe125SMel Gorman 	if (accountable_mapping(file, vm_flags)) {
11781da177e4SLinus Torvalds 		charged = len >> PAGE_SHIFT;
11791da177e4SLinus Torvalds 		if (security_vm_enough_memory(charged))
11801da177e4SLinus Torvalds 			return -ENOMEM;
11811da177e4SLinus Torvalds 		vm_flags |= VM_ACCOUNT;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	/*
1185de33c8dbSLinus Torvalds 	 * Can we just expand an old mapping?
11861da177e4SLinus Torvalds 	 */
1187de33c8dbSLinus Torvalds 	vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1188ba470de4SRik van Riel 	if (vma)
11891da177e4SLinus Torvalds 		goto out;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	/*
11921da177e4SLinus Torvalds 	 * Determine the object being mapped and call the appropriate
11931da177e4SLinus Torvalds 	 * specific mapper. the address has already been validated, but
11941da177e4SLinus Torvalds 	 * not unmapped, but the maps are removed from the list.
11951da177e4SLinus Torvalds 	 */
1196c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
11971da177e4SLinus Torvalds 	if (!vma) {
11981da177e4SLinus Torvalds 		error = -ENOMEM;
11991da177e4SLinus Torvalds 		goto unacct_error;
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	vma->vm_mm = mm;
12031da177e4SLinus Torvalds 	vma->vm_start = addr;
12041da177e4SLinus Torvalds 	vma->vm_end = addr + len;
12051da177e4SLinus Torvalds 	vma->vm_flags = vm_flags;
12063ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vm_flags);
12071da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	if (file) {
12101da177e4SLinus Torvalds 		error = -EINVAL;
12111da177e4SLinus Torvalds 		if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
12121da177e4SLinus Torvalds 			goto free_vma;
12131da177e4SLinus Torvalds 		if (vm_flags & VM_DENYWRITE) {
12141da177e4SLinus Torvalds 			error = deny_write_access(file);
12151da177e4SLinus Torvalds 			if (error)
12161da177e4SLinus Torvalds 				goto free_vma;
12171da177e4SLinus Torvalds 			correct_wcount = 1;
12181da177e4SLinus Torvalds 		}
12191da177e4SLinus Torvalds 		vma->vm_file = file;
12201da177e4SLinus Torvalds 		get_file(file);
12211da177e4SLinus Torvalds 		error = file->f_op->mmap(file, vma);
12221da177e4SLinus Torvalds 		if (error)
12231da177e4SLinus Torvalds 			goto unmap_and_free_vma;
1224925d1c40SMatt Helsley 		if (vm_flags & VM_EXECUTABLE)
1225925d1c40SMatt Helsley 			added_exe_file_vma(mm);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 		/* Can addr have changed??
12281da177e4SLinus Torvalds 		 *
12291da177e4SLinus Torvalds 		 * Answer: Yes, several device drivers can do it in their
12301da177e4SLinus Torvalds 		 *         f_op->mmap method. -DaveM
12311da177e4SLinus Torvalds 		 */
12321da177e4SLinus Torvalds 		addr = vma->vm_start;
12331da177e4SLinus Torvalds 		pgoff = vma->vm_pgoff;
12341da177e4SLinus Torvalds 		vm_flags = vma->vm_flags;
1235f8dbf0a7SHuang Shijie 	} else if (vm_flags & VM_SHARED) {
1236f8dbf0a7SHuang Shijie 		error = shmem_zero_setup(vma);
1237f8dbf0a7SHuang Shijie 		if (error)
1238f8dbf0a7SHuang Shijie 			goto free_vma;
1239f8dbf0a7SHuang Shijie 	}
12401da177e4SLinus Torvalds 
1241c9d0bf24SMagnus Damm 	if (vma_wants_writenotify(vma)) {
1242c9d0bf24SMagnus Damm 		pgprot_t pprot = vma->vm_page_prot;
1243c9d0bf24SMagnus Damm 
1244c9d0bf24SMagnus Damm 		/* Can vma->vm_page_prot have changed??
1245c9d0bf24SMagnus Damm 		 *
1246c9d0bf24SMagnus Damm 		 * Answer: Yes, drivers may have changed it in their
1247c9d0bf24SMagnus Damm 		 *         f_op->mmap method.
1248c9d0bf24SMagnus Damm 		 *
1249c9d0bf24SMagnus Damm 		 * Ensures that vmas marked as uncached stay that way.
1250c9d0bf24SMagnus Damm 		 */
12511ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
1252c9d0bf24SMagnus Damm 		if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1253c9d0bf24SMagnus Damm 			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1254c9d0bf24SMagnus Damm 	}
1255d08b3851SPeter Zijlstra 
12564d3d5b41SOleg Nesterov 	vma_link(mm, vma, prev, rb_link, rb_parent);
12574d3d5b41SOleg Nesterov 	file = vma->vm_file;
12584d3d5b41SOleg Nesterov 
12594d3d5b41SOleg Nesterov 	/* Once vma denies write, undo our temporary denial count */
12604d3d5b41SOleg Nesterov 	if (correct_wcount)
12614d3d5b41SOleg Nesterov 		atomic_inc(&inode->i_writecount);
12621da177e4SLinus Torvalds out:
1263cdd6c482SIngo Molnar 	perf_event_mmap(vma);
12640a4a9391SPeter Zijlstra 
12651da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
1266ab50b8edSHugh Dickins 	vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
12671da177e4SLinus Torvalds 	if (vm_flags & VM_LOCKED) {
1268ba470de4SRik van Riel 		/*
1269ba470de4SRik van Riel 		 * makes pages present; downgrades, drops, reacquires mmap_sem
1270ba470de4SRik van Riel 		 */
1271ba470de4SRik van Riel 		long nr_pages = mlock_vma_pages_range(vma, addr, addr + len);
1272ba470de4SRik van Riel 		if (nr_pages < 0)
1273ba470de4SRik van Riel 			return nr_pages;	/* vma gone! */
1274ba470de4SRik van Riel 		mm->locked_vm += (len >> PAGE_SHIFT) - nr_pages;
1275ba470de4SRik van Riel 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
127654cb8821SNick Piggin 		make_pages_present(addr, addr + len);
12771da177e4SLinus Torvalds 	return addr;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds unmap_and_free_vma:
12801da177e4SLinus Torvalds 	if (correct_wcount)
12811da177e4SLinus Torvalds 		atomic_inc(&inode->i_writecount);
12821da177e4SLinus Torvalds 	vma->vm_file = NULL;
12831da177e4SLinus Torvalds 	fput(file);
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	/* Undo any partial mapping done by a device driver. */
1286e0da382cSHugh Dickins 	unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1287e0da382cSHugh Dickins 	charged = 0;
12881da177e4SLinus Torvalds free_vma:
12891da177e4SLinus Torvalds 	kmem_cache_free(vm_area_cachep, vma);
12901da177e4SLinus Torvalds unacct_error:
12911da177e4SLinus Torvalds 	if (charged)
12921da177e4SLinus Torvalds 		vm_unacct_memory(charged);
12931da177e4SLinus Torvalds 	return error;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds /* Get an address range which is currently unmapped.
12971da177e4SLinus Torvalds  * For shmat() with addr=0.
12981da177e4SLinus Torvalds  *
12991da177e4SLinus Torvalds  * Ugly calling convention alert:
13001da177e4SLinus Torvalds  * Return value with the low bits set means error value,
13011da177e4SLinus Torvalds  * ie
13021da177e4SLinus Torvalds  *	if (ret & ~PAGE_MASK)
13031da177e4SLinus Torvalds  *		error = ret;
13041da177e4SLinus Torvalds  *
13051da177e4SLinus Torvalds  * This function "knows" that -ENOMEM has the bits set.
13061da177e4SLinus Torvalds  */
13071da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA
13081da177e4SLinus Torvalds unsigned long
13091da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr,
13101da177e4SLinus Torvalds 		unsigned long len, unsigned long pgoff, unsigned long flags)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
13131da177e4SLinus Torvalds 	struct vm_area_struct *vma;
13141da177e4SLinus Torvalds 	unsigned long start_addr;
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	if (len > TASK_SIZE)
13171da177e4SLinus Torvalds 		return -ENOMEM;
13181da177e4SLinus Torvalds 
131906abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
132006abdfb4SBenjamin Herrenschmidt 		return addr;
132106abdfb4SBenjamin Herrenschmidt 
13221da177e4SLinus Torvalds 	if (addr) {
13231da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
13241da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
13251da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
13261da177e4SLinus Torvalds 		    (!vma || addr + len <= vma->vm_start))
13271da177e4SLinus Torvalds 			return addr;
13281da177e4SLinus Torvalds 	}
13291363c3cdSWolfgang Wander 	if (len > mm->cached_hole_size) {
13301da177e4SLinus Torvalds 	        start_addr = addr = mm->free_area_cache;
13311363c3cdSWolfgang Wander 	} else {
13321363c3cdSWolfgang Wander 	        start_addr = addr = TASK_UNMAPPED_BASE;
13331363c3cdSWolfgang Wander 	        mm->cached_hole_size = 0;
13341363c3cdSWolfgang Wander 	}
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds full_search:
13371da177e4SLinus Torvalds 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
13381da177e4SLinus Torvalds 		/* At this point:  (!vma || addr < vma->vm_end). */
13391da177e4SLinus Torvalds 		if (TASK_SIZE - len < addr) {
13401da177e4SLinus Torvalds 			/*
13411da177e4SLinus Torvalds 			 * Start a new search - just in case we missed
13421da177e4SLinus Torvalds 			 * some holes.
13431da177e4SLinus Torvalds 			 */
13441da177e4SLinus Torvalds 			if (start_addr != TASK_UNMAPPED_BASE) {
13451363c3cdSWolfgang Wander 				addr = TASK_UNMAPPED_BASE;
13461363c3cdSWolfgang Wander 			        start_addr = addr;
13471363c3cdSWolfgang Wander 				mm->cached_hole_size = 0;
13481da177e4SLinus Torvalds 				goto full_search;
13491da177e4SLinus Torvalds 			}
13501da177e4SLinus Torvalds 			return -ENOMEM;
13511da177e4SLinus Torvalds 		}
13521da177e4SLinus Torvalds 		if (!vma || addr + len <= vma->vm_start) {
13531da177e4SLinus Torvalds 			/*
13541da177e4SLinus Torvalds 			 * Remember the place where we stopped the search:
13551da177e4SLinus Torvalds 			 */
13561da177e4SLinus Torvalds 			mm->free_area_cache = addr + len;
13571da177e4SLinus Torvalds 			return addr;
13581da177e4SLinus Torvalds 		}
13591363c3cdSWolfgang Wander 		if (addr + mm->cached_hole_size < vma->vm_start)
13601363c3cdSWolfgang Wander 		        mm->cached_hole_size = vma->vm_start - addr;
13611da177e4SLinus Torvalds 		addr = vma->vm_end;
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds #endif
13651da177e4SLinus Torvalds 
13661363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
13671da177e4SLinus Torvalds {
13681da177e4SLinus Torvalds 	/*
13691da177e4SLinus Torvalds 	 * Is this a new hole at the lowest possible address?
13701da177e4SLinus Torvalds 	 */
13711363c3cdSWolfgang Wander 	if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
13721363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
13731363c3cdSWolfgang Wander 		mm->cached_hole_size = ~0UL;
13741363c3cdSWolfgang Wander 	}
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds /*
13781da177e4SLinus Torvalds  * This mmap-allocator allocates new areas top-down from below the
13791da177e4SLinus Torvalds  * stack's low limit (the base):
13801da177e4SLinus Torvalds  */
13811da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
13821da177e4SLinus Torvalds unsigned long
13831da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
13841da177e4SLinus Torvalds 			  const unsigned long len, const unsigned long pgoff,
13851da177e4SLinus Torvalds 			  const unsigned long flags)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds 	struct vm_area_struct *vma;
13881da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
13891da177e4SLinus Torvalds 	unsigned long addr = addr0;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	/* requested length too big for entire address space */
13921da177e4SLinus Torvalds 	if (len > TASK_SIZE)
13931da177e4SLinus Torvalds 		return -ENOMEM;
13941da177e4SLinus Torvalds 
139506abdfb4SBenjamin Herrenschmidt 	if (flags & MAP_FIXED)
139606abdfb4SBenjamin Herrenschmidt 		return addr;
139706abdfb4SBenjamin Herrenschmidt 
13981da177e4SLinus Torvalds 	/* requesting a specific address */
13991da177e4SLinus Torvalds 	if (addr) {
14001da177e4SLinus Torvalds 		addr = PAGE_ALIGN(addr);
14011da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
14021da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
14031da177e4SLinus Torvalds 				(!vma || addr + len <= vma->vm_start))
14041da177e4SLinus Torvalds 			return addr;
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
14071363c3cdSWolfgang Wander 	/* check if free_area_cache is useful for us */
14081363c3cdSWolfgang Wander 	if (len <= mm->cached_hole_size) {
14091363c3cdSWolfgang Wander  	        mm->cached_hole_size = 0;
14101363c3cdSWolfgang Wander  		mm->free_area_cache = mm->mmap_base;
14111363c3cdSWolfgang Wander  	}
14121363c3cdSWolfgang Wander 
14131da177e4SLinus Torvalds 	/* either no address requested or can't fit in requested address hole */
14141da177e4SLinus Torvalds 	addr = mm->free_area_cache;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	/* make sure it can fit in the remaining address space */
141749a43876SLinus Torvalds 	if (addr > len) {
14181da177e4SLinus Torvalds 		vma = find_vma(mm, addr-len);
14191da177e4SLinus Torvalds 		if (!vma || addr <= vma->vm_start)
14201da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
14211da177e4SLinus Torvalds 			return (mm->free_area_cache = addr-len);
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds 
142473219d17SChris Wright 	if (mm->mmap_base < len)
142573219d17SChris Wright 		goto bottomup;
142673219d17SChris Wright 
14271da177e4SLinus Torvalds 	addr = mm->mmap_base-len;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	do {
14301da177e4SLinus Torvalds 		/*
14311da177e4SLinus Torvalds 		 * Lookup failure means no vma is above this address,
14321da177e4SLinus Torvalds 		 * else if new region fits below vma->vm_start,
14331da177e4SLinus Torvalds 		 * return with success:
14341da177e4SLinus Torvalds 		 */
14351da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
14361da177e4SLinus Torvalds 		if (!vma || addr+len <= vma->vm_start)
14371da177e4SLinus Torvalds 			/* remember the address as a hint for next time */
14381da177e4SLinus Torvalds 			return (mm->free_area_cache = addr);
14391da177e4SLinus Torvalds 
14401363c3cdSWolfgang Wander  		/* remember the largest hole we saw so far */
14411363c3cdSWolfgang Wander  		if (addr + mm->cached_hole_size < vma->vm_start)
14421363c3cdSWolfgang Wander  		        mm->cached_hole_size = vma->vm_start - addr;
14431363c3cdSWolfgang Wander 
14441da177e4SLinus Torvalds 		/* try just below the current vma->vm_start */
14451da177e4SLinus Torvalds 		addr = vma->vm_start-len;
144649a43876SLinus Torvalds 	} while (len < vma->vm_start);
14471da177e4SLinus Torvalds 
144873219d17SChris Wright bottomup:
14491da177e4SLinus Torvalds 	/*
14501da177e4SLinus Torvalds 	 * A failed mmap() very likely causes application failure,
14511da177e4SLinus Torvalds 	 * so fall back to the bottom-up function here. This scenario
14521da177e4SLinus Torvalds 	 * can happen with large stack limits and large mmap()
14531da177e4SLinus Torvalds 	 * allocations.
14541da177e4SLinus Torvalds 	 */
14551363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
14561da177e4SLinus Torvalds   	mm->free_area_cache = TASK_UNMAPPED_BASE;
14571da177e4SLinus Torvalds 	addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
14581da177e4SLinus Torvalds 	/*
14591da177e4SLinus Torvalds 	 * Restore the topdown base:
14601da177e4SLinus Torvalds 	 */
14611da177e4SLinus Torvalds 	mm->free_area_cache = mm->mmap_base;
14621363c3cdSWolfgang Wander 	mm->cached_hole_size = ~0UL;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	return addr;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds #endif
14671da177e4SLinus Torvalds 
14681363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
14691da177e4SLinus Torvalds {
14701da177e4SLinus Torvalds 	/*
14711da177e4SLinus Torvalds 	 * Is this a new hole at the highest possible address?
14721da177e4SLinus Torvalds 	 */
14731363c3cdSWolfgang Wander 	if (addr > mm->free_area_cache)
14741363c3cdSWolfgang Wander 		mm->free_area_cache = addr;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	/* dont allow allocations above current base */
14771363c3cdSWolfgang Wander 	if (mm->free_area_cache > mm->mmap_base)
14781363c3cdSWolfgang Wander 		mm->free_area_cache = mm->mmap_base;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds unsigned long
14821da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
14831da177e4SLinus Torvalds 		unsigned long pgoff, unsigned long flags)
14841da177e4SLinus Torvalds {
148506abdfb4SBenjamin Herrenschmidt 	unsigned long (*get_area)(struct file *, unsigned long,
148606abdfb4SBenjamin Herrenschmidt 				  unsigned long, unsigned long, unsigned long);
148707ab67c8SLinus Torvalds 
14889206de95SAl Viro 	unsigned long error = arch_mmap_check(addr, len, flags);
14899206de95SAl Viro 	if (error)
14909206de95SAl Viro 		return error;
14919206de95SAl Viro 
14929206de95SAl Viro 	/* Careful about overflows.. */
14939206de95SAl Viro 	if (len > TASK_SIZE)
14949206de95SAl Viro 		return -ENOMEM;
14959206de95SAl Viro 
149607ab67c8SLinus Torvalds 	get_area = current->mm->get_unmapped_area;
149707ab67c8SLinus Torvalds 	if (file && file->f_op && file->f_op->get_unmapped_area)
149807ab67c8SLinus Torvalds 		get_area = file->f_op->get_unmapped_area;
149907ab67c8SLinus Torvalds 	addr = get_area(file, addr, len, pgoff, flags);
150007ab67c8SLinus Torvalds 	if (IS_ERR_VALUE(addr))
150107ab67c8SLinus Torvalds 		return addr;
150207ab67c8SLinus Torvalds 
15031da177e4SLinus Torvalds 	if (addr > TASK_SIZE - len)
15041da177e4SLinus Torvalds 		return -ENOMEM;
15051da177e4SLinus Torvalds 	if (addr & ~PAGE_MASK)
15061da177e4SLinus Torvalds 		return -EINVAL;
150706abdfb4SBenjamin Herrenschmidt 
150808e7d9b5SMartin Schwidefsky 	return arch_rebalance_pgtables(addr, len);
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area);
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
15141da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	if (mm) {
15191da177e4SLinus Torvalds 		/* Check the cache first. */
15201da177e4SLinus Torvalds 		/* (Cache hit rate is typically around 35%.) */
15211da177e4SLinus Torvalds 		vma = mm->mmap_cache;
15221da177e4SLinus Torvalds 		if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
15231da177e4SLinus Torvalds 			struct rb_node * rb_node;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 			rb_node = mm->mm_rb.rb_node;
15261da177e4SLinus Torvalds 			vma = NULL;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 			while (rb_node) {
15291da177e4SLinus Torvalds 				struct vm_area_struct * vma_tmp;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 				vma_tmp = rb_entry(rb_node,
15321da177e4SLinus Torvalds 						struct vm_area_struct, vm_rb);
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 				if (vma_tmp->vm_end > addr) {
15351da177e4SLinus Torvalds 					vma = vma_tmp;
15361da177e4SLinus Torvalds 					if (vma_tmp->vm_start <= addr)
15371da177e4SLinus Torvalds 						break;
15381da177e4SLinus Torvalds 					rb_node = rb_node->rb_left;
15391da177e4SLinus Torvalds 				} else
15401da177e4SLinus Torvalds 					rb_node = rb_node->rb_right;
15411da177e4SLinus Torvalds 			}
15421da177e4SLinus Torvalds 			if (vma)
15431da177e4SLinus Torvalds 				mm->mmap_cache = vma;
15441da177e4SLinus Torvalds 		}
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 	return vma;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma);
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
15521da177e4SLinus Torvalds struct vm_area_struct *
15531da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr,
15541da177e4SLinus Torvalds 			struct vm_area_struct **pprev)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL, *prev = NULL;
15571da177e4SLinus Torvalds 	struct rb_node *rb_node;
15581da177e4SLinus Torvalds 	if (!mm)
15591da177e4SLinus Torvalds 		goto out;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/* Guard against addr being lower than the first VMA */
15621da177e4SLinus Torvalds 	vma = mm->mmap;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	/* Go through the RB tree quickly. */
15651da177e4SLinus Torvalds 	rb_node = mm->mm_rb.rb_node;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	while (rb_node) {
15681da177e4SLinus Torvalds 		struct vm_area_struct *vma_tmp;
15691da177e4SLinus Torvalds 		vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		if (addr < vma_tmp->vm_end) {
15721da177e4SLinus Torvalds 			rb_node = rb_node->rb_left;
15731da177e4SLinus Torvalds 		} else {
15741da177e4SLinus Torvalds 			prev = vma_tmp;
15751da177e4SLinus Torvalds 			if (!prev->vm_next || (addr < prev->vm_next->vm_end))
15761da177e4SLinus Torvalds 				break;
15771da177e4SLinus Torvalds 			rb_node = rb_node->rb_right;
15781da177e4SLinus Torvalds 		}
15791da177e4SLinus Torvalds 	}
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds out:
15821da177e4SLinus Torvalds 	*pprev = prev;
15831da177e4SLinus Torvalds 	return prev ? prev->vm_next : vma;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds /*
15871da177e4SLinus Torvalds  * Verify that the stack growth is acceptable and
15881da177e4SLinus Torvalds  * update accounting. This is shared with both the
15891da177e4SLinus Torvalds  * grow-up and grow-down cases.
15901da177e4SLinus Torvalds  */
15911da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
15941da177e4SLinus Torvalds 	struct rlimit *rlim = current->signal->rlim;
15950d59a01bSAdam Litke 	unsigned long new_start;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	/* address space limit tests */
1598119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, grow))
15991da177e4SLinus Torvalds 		return -ENOMEM;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/* Stack limit test */
16021da177e4SLinus Torvalds 	if (size > rlim[RLIMIT_STACK].rlim_cur)
16031da177e4SLinus Torvalds 		return -ENOMEM;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	/* mlock limit tests */
16061da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED) {
16071da177e4SLinus Torvalds 		unsigned long locked;
16081da177e4SLinus Torvalds 		unsigned long limit;
16091da177e4SLinus Torvalds 		locked = mm->locked_vm + grow;
16101da177e4SLinus Torvalds 		limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
16111da177e4SLinus Torvalds 		if (locked > limit && !capable(CAP_IPC_LOCK))
16121da177e4SLinus Torvalds 			return -ENOMEM;
16131da177e4SLinus Torvalds 	}
16141da177e4SLinus Torvalds 
16150d59a01bSAdam Litke 	/* Check to ensure the stack will not grow into a hugetlb-only region */
16160d59a01bSAdam Litke 	new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
16170d59a01bSAdam Litke 			vma->vm_end - size;
16180d59a01bSAdam Litke 	if (is_hugepage_only_range(vma->vm_mm, new_start, size))
16190d59a01bSAdam Litke 		return -EFAULT;
16200d59a01bSAdam Litke 
16211da177e4SLinus Torvalds 	/*
16221da177e4SLinus Torvalds 	 * Overcommit..  This must be the final test, as it will
16231da177e4SLinus Torvalds 	 * update security statistics.
16241da177e4SLinus Torvalds 	 */
162505fa199dSHugh Dickins 	if (security_vm_enough_memory_mm(mm, grow))
16261da177e4SLinus Torvalds 		return -ENOMEM;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	/* Ok, everything looks good - let it rip */
16291da177e4SLinus Torvalds 	mm->total_vm += grow;
16301da177e4SLinus Torvalds 	if (vma->vm_flags & VM_LOCKED)
16311da177e4SLinus Torvalds 		mm->locked_vm += grow;
1632ab50b8edSHugh Dickins 	vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
16331da177e4SLinus Torvalds 	return 0;
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
163646dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
16371da177e4SLinus Torvalds /*
163846dea3d0SHugh Dickins  * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
163946dea3d0SHugh Dickins  * vma is the last one with address > vma->vm_end.  Have to extend vma.
16401da177e4SLinus Torvalds  */
16419ab88515SMatthew Wilcox #ifndef CONFIG_IA64
1642cb8f488cSDenys Vlasenko static
164346dea3d0SHugh Dickins #endif
164446dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	int error;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSUP))
16491da177e4SLinus Torvalds 		return -EFAULT;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	/*
16521da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
16531da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
16541da177e4SLinus Torvalds 	 */
16551da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
16561da177e4SLinus Torvalds 		return -ENOMEM;
16571da177e4SLinus Torvalds 	anon_vma_lock(vma);
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	/*
16601da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
16611da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
16621da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
166306b32f3aSHelge Deller 	 * Also guard against wrapping around to address 0.
16641da177e4SLinus Torvalds 	 */
166506b32f3aSHelge Deller 	if (address < PAGE_ALIGN(address+4))
166606b32f3aSHelge Deller 		address = PAGE_ALIGN(address+4);
166706b32f3aSHelge Deller 	else {
166806b32f3aSHelge Deller 		anon_vma_unlock(vma);
166906b32f3aSHelge Deller 		return -ENOMEM;
167006b32f3aSHelge Deller 	}
16711da177e4SLinus Torvalds 	error = 0;
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
16741da177e4SLinus Torvalds 	if (address > vma->vm_end) {
16751da177e4SLinus Torvalds 		unsigned long size, grow;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 		size = address - vma->vm_start;
16781da177e4SLinus Torvalds 		grow = (address - vma->vm_end) >> PAGE_SHIFT;
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 		error = acct_stack_growth(vma, size, grow);
16811da177e4SLinus Torvalds 		if (!error)
16821da177e4SLinus Torvalds 			vma->vm_end = address;
16831da177e4SLinus Torvalds 	}
16841da177e4SLinus Torvalds 	anon_vma_unlock(vma);
16851da177e4SLinus Torvalds 	return error;
16861da177e4SLinus Torvalds }
168746dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
168846dea3d0SHugh Dickins 
16891da177e4SLinus Torvalds /*
16901da177e4SLinus Torvalds  * vma is the first one with address < vma->vm_start.  Have to extend vma.
16911da177e4SLinus Torvalds  */
1692cb8f488cSDenys Vlasenko static int expand_downwards(struct vm_area_struct *vma,
1693b6a2fea3SOllie Wild 				   unsigned long address)
16941da177e4SLinus Torvalds {
16951da177e4SLinus Torvalds 	int error;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	/*
16981da177e4SLinus Torvalds 	 * We must make sure the anon_vma is allocated
16991da177e4SLinus Torvalds 	 * so that the anon_vma locking is not a noop.
17001da177e4SLinus Torvalds 	 */
17011da177e4SLinus Torvalds 	if (unlikely(anon_vma_prepare(vma)))
17021da177e4SLinus Torvalds 		return -ENOMEM;
17038869477aSEric Paris 
17048869477aSEric Paris 	address &= PAGE_MASK;
170588c3f7a8SRichard Knutsson 	error = security_file_mmap(NULL, 0, 0, 0, address, 1);
17068869477aSEric Paris 	if (error)
17078869477aSEric Paris 		return error;
17088869477aSEric Paris 
17091da177e4SLinus Torvalds 	anon_vma_lock(vma);
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	/*
17121da177e4SLinus Torvalds 	 * vma->vm_start/vm_end cannot change under us because the caller
17131da177e4SLinus Torvalds 	 * is required to hold the mmap_sem in read mode.  We need the
17141da177e4SLinus Torvalds 	 * anon_vma lock to serialize against concurrent expand_stacks.
17151da177e4SLinus Torvalds 	 */
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	/* Somebody else might have raced and expanded it already */
17181da177e4SLinus Torvalds 	if (address < vma->vm_start) {
17191da177e4SLinus Torvalds 		unsigned long size, grow;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 		size = vma->vm_end - address;
17221da177e4SLinus Torvalds 		grow = (vma->vm_start - address) >> PAGE_SHIFT;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 		error = acct_stack_growth(vma, size, grow);
17251da177e4SLinus Torvalds 		if (!error) {
17261da177e4SLinus Torvalds 			vma->vm_start = address;
17271da177e4SLinus Torvalds 			vma->vm_pgoff -= grow;
17281da177e4SLinus Torvalds 		}
17291da177e4SLinus Torvalds 	}
17301da177e4SLinus Torvalds 	anon_vma_unlock(vma);
17311da177e4SLinus Torvalds 	return error;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
1734b6a2fea3SOllie Wild int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
1735b6a2fea3SOllie Wild {
1736b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1737b6a2fea3SOllie Wild }
1738b6a2fea3SOllie Wild 
1739b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP
1740b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1741b6a2fea3SOllie Wild {
1742b6a2fea3SOllie Wild 	return expand_upwards(vma, address);
1743b6a2fea3SOllie Wild }
1744b6a2fea3SOllie Wild 
1745b6a2fea3SOllie Wild struct vm_area_struct *
1746b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr)
1747b6a2fea3SOllie Wild {
1748b6a2fea3SOllie Wild 	struct vm_area_struct *vma, *prev;
1749b6a2fea3SOllie Wild 
1750b6a2fea3SOllie Wild 	addr &= PAGE_MASK;
1751b6a2fea3SOllie Wild 	vma = find_vma_prev(mm, addr, &prev);
1752b6a2fea3SOllie Wild 	if (vma && (vma->vm_start <= addr))
1753b6a2fea3SOllie Wild 		return vma;
17541c127185SDenys Vlasenko 	if (!prev || expand_stack(prev, addr))
1755b6a2fea3SOllie Wild 		return NULL;
1756ba470de4SRik van Riel 	if (prev->vm_flags & VM_LOCKED) {
1757ba470de4SRik van Riel 		if (mlock_vma_pages_range(prev, addr, prev->vm_end) < 0)
1758ba470de4SRik van Riel 			return NULL;	/* vma gone! */
1759ba470de4SRik van Riel 	}
1760b6a2fea3SOllie Wild 	return prev;
1761b6a2fea3SOllie Wild }
1762b6a2fea3SOllie Wild #else
1763b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address)
1764b6a2fea3SOllie Wild {
1765b6a2fea3SOllie Wild 	return expand_downwards(vma, address);
1766b6a2fea3SOllie Wild }
1767b6a2fea3SOllie Wild 
17681da177e4SLinus Torvalds struct vm_area_struct *
17691da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr)
17701da177e4SLinus Torvalds {
17711da177e4SLinus Torvalds 	struct vm_area_struct * vma;
17721da177e4SLinus Torvalds 	unsigned long start;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	addr &= PAGE_MASK;
17751da177e4SLinus Torvalds 	vma = find_vma(mm,addr);
17761da177e4SLinus Torvalds 	if (!vma)
17771da177e4SLinus Torvalds 		return NULL;
17781da177e4SLinus Torvalds 	if (vma->vm_start <= addr)
17791da177e4SLinus Torvalds 		return vma;
17801da177e4SLinus Torvalds 	if (!(vma->vm_flags & VM_GROWSDOWN))
17811da177e4SLinus Torvalds 		return NULL;
17821da177e4SLinus Torvalds 	start = vma->vm_start;
17831da177e4SLinus Torvalds 	if (expand_stack(vma, addr))
17841da177e4SLinus Torvalds 		return NULL;
1785ba470de4SRik van Riel 	if (vma->vm_flags & VM_LOCKED) {
1786ba470de4SRik van Riel 		if (mlock_vma_pages_range(vma, addr, start) < 0)
1787ba470de4SRik van Riel 			return NULL;	/* vma gone! */
1788ba470de4SRik van Riel 	}
17891da177e4SLinus Torvalds 	return vma;
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds #endif
17921da177e4SLinus Torvalds 
17932c0b3814SHugh Dickins /*
17942c0b3814SHugh Dickins  * Ok - we have the memory areas we should free on the vma list,
17952c0b3814SHugh Dickins  * so release them, and do the vma updates.
17961da177e4SLinus Torvalds  *
17972c0b3814SHugh Dickins  * Called with the mm semaphore held.
17981da177e4SLinus Torvalds  */
17992c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
18001da177e4SLinus Torvalds {
1801365e9c87SHugh Dickins 	/* Update high watermark before we lower total_vm */
1802365e9c87SHugh Dickins 	update_hiwater_vm(mm);
18032c0b3814SHugh Dickins 	do {
1804ab50b8edSHugh Dickins 		long nrpages = vma_pages(vma);
18051da177e4SLinus Torvalds 
1806ab50b8edSHugh Dickins 		mm->total_vm -= nrpages;
1807ab50b8edSHugh Dickins 		vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
1808a8fb5618SHugh Dickins 		vma = remove_vma(vma);
1809146425a3SHugh Dickins 	} while (vma);
18101da177e4SLinus Torvalds 	validate_mm(mm);
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds /*
18141da177e4SLinus Torvalds  * Get rid of page table information in the indicated region.
18151da177e4SLinus Torvalds  *
1816f10df686SPaolo 'Blaisorblade' Giarrusso  * Called with the mm semaphore held.
18171da177e4SLinus Torvalds  */
18181da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm,
1819e0da382cSHugh Dickins 		struct vm_area_struct *vma, struct vm_area_struct *prev,
1820e0da382cSHugh Dickins 		unsigned long start, unsigned long end)
18211da177e4SLinus Torvalds {
1822e0da382cSHugh Dickins 	struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
18231da177e4SLinus Torvalds 	struct mmu_gather *tlb;
18241da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	lru_add_drain();
18271da177e4SLinus Torvalds 	tlb = tlb_gather_mmu(mm, 0);
1828365e9c87SHugh Dickins 	update_hiwater_rss(mm);
1829508034a3SHugh Dickins 	unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
18301da177e4SLinus Torvalds 	vm_unacct_memory(nr_accounted);
183142b77728SJan Beulich 	free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1832e0da382cSHugh Dickins 				 next? next->vm_start: 0);
18331da177e4SLinus Torvalds 	tlb_finish_mmu(tlb, start, end);
18341da177e4SLinus Torvalds }
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds /*
18371da177e4SLinus Torvalds  * Create a list of vma's touched by the unmap, removing them from the mm's
18381da177e4SLinus Torvalds  * vma list as we go..
18391da177e4SLinus Torvalds  */
18401da177e4SLinus Torvalds static void
18411da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
18421da177e4SLinus Torvalds 	struct vm_area_struct *prev, unsigned long end)
18431da177e4SLinus Torvalds {
18441da177e4SLinus Torvalds 	struct vm_area_struct **insertion_point;
18451da177e4SLinus Torvalds 	struct vm_area_struct *tail_vma = NULL;
18461363c3cdSWolfgang Wander 	unsigned long addr;
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	insertion_point = (prev ? &prev->vm_next : &mm->mmap);
18491da177e4SLinus Torvalds 	do {
18501da177e4SLinus Torvalds 		rb_erase(&vma->vm_rb, &mm->mm_rb);
18511da177e4SLinus Torvalds 		mm->map_count--;
18521da177e4SLinus Torvalds 		tail_vma = vma;
18531da177e4SLinus Torvalds 		vma = vma->vm_next;
18541da177e4SLinus Torvalds 	} while (vma && vma->vm_start < end);
18551da177e4SLinus Torvalds 	*insertion_point = vma;
18561da177e4SLinus Torvalds 	tail_vma->vm_next = NULL;
18571363c3cdSWolfgang Wander 	if (mm->unmap_area == arch_unmap_area)
18581363c3cdSWolfgang Wander 		addr = prev ? prev->vm_end : mm->mmap_base;
18591363c3cdSWolfgang Wander 	else
18601363c3cdSWolfgang Wander 		addr = vma ?  vma->vm_start : mm->mmap_base;
18611363c3cdSWolfgang Wander 	mm->unmap_area(mm, addr);
18621da177e4SLinus Torvalds 	mm->mmap_cache = NULL;		/* Kill the cache. */
18631da177e4SLinus Torvalds }
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds /*
1866659ace58SKOSAKI Motohiro  * __split_vma() bypasses sysctl_max_map_count checking.  We use this on the
1867659ace58SKOSAKI Motohiro  * munmap path where it doesn't make sense to fail.
18681da177e4SLinus Torvalds  */
1869659ace58SKOSAKI Motohiro static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
18701da177e4SLinus Torvalds 	      unsigned long addr, int new_below)
18711da177e4SLinus Torvalds {
18721da177e4SLinus Torvalds 	struct mempolicy *pol;
18731da177e4SLinus Torvalds 	struct vm_area_struct *new;
18741da177e4SLinus Torvalds 
1875a5516438SAndi Kleen 	if (is_vm_hugetlb_page(vma) && (addr &
1876a5516438SAndi Kleen 					~(huge_page_mask(hstate_vma(vma)))))
18771da177e4SLinus Torvalds 		return -EINVAL;
18781da177e4SLinus Torvalds 
1879e94b1766SChristoph Lameter 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
18801da177e4SLinus Torvalds 	if (!new)
18811da177e4SLinus Torvalds 		return -ENOMEM;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	/* most fields are the same, copy all, and then fixup */
18841da177e4SLinus Torvalds 	*new = *vma;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	if (new_below)
18871da177e4SLinus Torvalds 		new->vm_end = addr;
18881da177e4SLinus Torvalds 	else {
18891da177e4SLinus Torvalds 		new->vm_start = addr;
18901da177e4SLinus Torvalds 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
18911da177e4SLinus Torvalds 	}
18921da177e4SLinus Torvalds 
1893846a16bfSLee Schermerhorn 	pol = mpol_dup(vma_policy(vma));
18941da177e4SLinus Torvalds 	if (IS_ERR(pol)) {
18951da177e4SLinus Torvalds 		kmem_cache_free(vm_area_cachep, new);
18961da177e4SLinus Torvalds 		return PTR_ERR(pol);
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 	vma_set_policy(new, pol);
18991da177e4SLinus Torvalds 
1900925d1c40SMatt Helsley 	if (new->vm_file) {
19011da177e4SLinus Torvalds 		get_file(new->vm_file);
1902925d1c40SMatt Helsley 		if (vma->vm_flags & VM_EXECUTABLE)
1903925d1c40SMatt Helsley 			added_exe_file_vma(mm);
1904925d1c40SMatt Helsley 	}
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	if (new->vm_ops && new->vm_ops->open)
19071da177e4SLinus Torvalds 		new->vm_ops->open(new);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	if (new_below)
19101da177e4SLinus Torvalds 		vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
19111da177e4SLinus Torvalds 			((addr - new->vm_start) >> PAGE_SHIFT), new);
19121da177e4SLinus Torvalds 	else
19131da177e4SLinus Torvalds 		vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	return 0;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
1918659ace58SKOSAKI Motohiro /*
1919659ace58SKOSAKI Motohiro  * Split a vma into two pieces at address 'addr', a new vma is allocated
1920659ace58SKOSAKI Motohiro  * either for the first part or the tail.
1921659ace58SKOSAKI Motohiro  */
1922659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1923659ace58SKOSAKI Motohiro 	      unsigned long addr, int new_below)
1924659ace58SKOSAKI Motohiro {
1925659ace58SKOSAKI Motohiro 	if (mm->map_count >= sysctl_max_map_count)
1926659ace58SKOSAKI Motohiro 		return -ENOMEM;
1927659ace58SKOSAKI Motohiro 
1928659ace58SKOSAKI Motohiro 	return __split_vma(mm, vma, addr, new_below);
1929659ace58SKOSAKI Motohiro }
1930659ace58SKOSAKI Motohiro 
19311da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds
19321da177e4SLinus Torvalds  * what needs doing, and the areas themselves, which do the
19331da177e4SLinus Torvalds  * work.  This now handles partial unmappings.
19341da177e4SLinus Torvalds  * Jeremy Fitzhardinge <jeremy@goop.org>
19351da177e4SLinus Torvalds  */
19361da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
19371da177e4SLinus Torvalds {
19381da177e4SLinus Torvalds 	unsigned long end;
1939146425a3SHugh Dickins 	struct vm_area_struct *vma, *prev, *last;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
19421da177e4SLinus Torvalds 		return -EINVAL;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	if ((len = PAGE_ALIGN(len)) == 0)
19451da177e4SLinus Torvalds 		return -EINVAL;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	/* Find the first overlapping VMA */
1948146425a3SHugh Dickins 	vma = find_vma_prev(mm, start, &prev);
1949146425a3SHugh Dickins 	if (!vma)
19501da177e4SLinus Torvalds 		return 0;
1951146425a3SHugh Dickins 	/* we have  start < vma->vm_end  */
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	/* if it doesn't overlap, we have nothing.. */
19541da177e4SLinus Torvalds 	end = start + len;
1955146425a3SHugh Dickins 	if (vma->vm_start >= end)
19561da177e4SLinus Torvalds 		return 0;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	/*
19591da177e4SLinus Torvalds 	 * If we need to split any vma, do it now to save pain later.
19601da177e4SLinus Torvalds 	 *
19611da177e4SLinus Torvalds 	 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
19621da177e4SLinus Torvalds 	 * unmapped vm_area_struct will remain in use: so lower split_vma
19631da177e4SLinus Torvalds 	 * places tmp vma above, and higher split_vma places tmp vma below.
19641da177e4SLinus Torvalds 	 */
1965146425a3SHugh Dickins 	if (start > vma->vm_start) {
1966659ace58SKOSAKI Motohiro 		int error;
1967659ace58SKOSAKI Motohiro 
1968659ace58SKOSAKI Motohiro 		/*
1969659ace58SKOSAKI Motohiro 		 * Make sure that map_count on return from munmap() will
1970659ace58SKOSAKI Motohiro 		 * not exceed its limit; but let map_count go just above
1971659ace58SKOSAKI Motohiro 		 * its limit temporarily, to help free resources as expected.
1972659ace58SKOSAKI Motohiro 		 */
1973659ace58SKOSAKI Motohiro 		if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
1974659ace58SKOSAKI Motohiro 			return -ENOMEM;
1975659ace58SKOSAKI Motohiro 
1976659ace58SKOSAKI Motohiro 		error = __split_vma(mm, vma, start, 0);
19771da177e4SLinus Torvalds 		if (error)
19781da177e4SLinus Torvalds 			return error;
1979146425a3SHugh Dickins 		prev = vma;
19801da177e4SLinus Torvalds 	}
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	/* Does it split the last one? */
19831da177e4SLinus Torvalds 	last = find_vma(mm, end);
19841da177e4SLinus Torvalds 	if (last && end > last->vm_start) {
1985659ace58SKOSAKI Motohiro 		int error = __split_vma(mm, last, end, 1);
19861da177e4SLinus Torvalds 		if (error)
19871da177e4SLinus Torvalds 			return error;
19881da177e4SLinus Torvalds 	}
1989146425a3SHugh Dickins 	vma = prev? prev->vm_next: mm->mmap;
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 	/*
1992ba470de4SRik van Riel 	 * unlock any mlock()ed ranges before detaching vmas
1993ba470de4SRik van Riel 	 */
1994ba470de4SRik van Riel 	if (mm->locked_vm) {
1995ba470de4SRik van Riel 		struct vm_area_struct *tmp = vma;
1996ba470de4SRik van Riel 		while (tmp && tmp->vm_start < end) {
1997ba470de4SRik van Riel 			if (tmp->vm_flags & VM_LOCKED) {
1998ba470de4SRik van Riel 				mm->locked_vm -= vma_pages(tmp);
1999ba470de4SRik van Riel 				munlock_vma_pages_all(tmp);
2000ba470de4SRik van Riel 			}
2001ba470de4SRik van Riel 			tmp = tmp->vm_next;
2002ba470de4SRik van Riel 		}
2003ba470de4SRik van Riel 	}
2004ba470de4SRik van Riel 
2005ba470de4SRik van Riel 	/*
20061da177e4SLinus Torvalds 	 * Remove the vma's, and unmap the actual pages
20071da177e4SLinus Torvalds 	 */
2008146425a3SHugh Dickins 	detach_vmas_to_be_unmapped(mm, vma, prev, end);
2009146425a3SHugh Dickins 	unmap_region(mm, vma, prev, start, end);
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	/* Fix up all other VM information */
20122c0b3814SHugh Dickins 	remove_vma_list(mm, vma);
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	return 0;
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds EXPORT_SYMBOL(do_munmap);
20181da177e4SLinus Torvalds 
20196a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
20201da177e4SLinus Torvalds {
20211da177e4SLinus Torvalds 	int ret;
20221da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	profile_munmap(addr);
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
20271da177e4SLinus Torvalds 	ret = do_munmap(mm, addr, len);
20281da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
20291da177e4SLinus Torvalds 	return ret;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm)
20331da177e4SLinus Torvalds {
2034a241ec65SPaul E. McKenney #ifdef CONFIG_DEBUG_VM
20351da177e4SLinus Torvalds 	if (unlikely(down_read_trylock(&mm->mmap_sem))) {
20361da177e4SLinus Torvalds 		WARN_ON(1);
20371da177e4SLinus Torvalds 		up_read(&mm->mmap_sem);
20381da177e4SLinus Torvalds 	}
20391da177e4SLinus Torvalds #endif
20401da177e4SLinus Torvalds }
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds /*
20431da177e4SLinus Torvalds  *  this is really a simplified "do_mmap".  it only handles
20441da177e4SLinus Torvalds  *  anonymous maps.  eventually we may be able to do some
20451da177e4SLinus Torvalds  *  brk-specific accounting here.
20461da177e4SLinus Torvalds  */
20471da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	struct mm_struct * mm = current->mm;
20501da177e4SLinus Torvalds 	struct vm_area_struct * vma, * prev;
20511da177e4SLinus Torvalds 	unsigned long flags;
20521da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
20531da177e4SLinus Torvalds 	pgoff_t pgoff = addr >> PAGE_SHIFT;
20543a459756SKirill Korotaev 	int error;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
20571da177e4SLinus Torvalds 	if (!len)
20581da177e4SLinus Torvalds 		return addr;
20591da177e4SLinus Torvalds 
206088c3f7a8SRichard Knutsson 	error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
20615a211a5dSEric Paris 	if (error)
20625a211a5dSEric Paris 		return error;
20635a211a5dSEric Paris 
20643a459756SKirill Korotaev 	flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
20653a459756SKirill Korotaev 
20662c6a1016SAl Viro 	error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
20672c6a1016SAl Viro 	if (error & ~PAGE_MASK)
20683a459756SKirill Korotaev 		return error;
20693a459756SKirill Korotaev 
20701da177e4SLinus Torvalds 	/*
20711da177e4SLinus Torvalds 	 * mlock MCL_FUTURE?
20721da177e4SLinus Torvalds 	 */
20731da177e4SLinus Torvalds 	if (mm->def_flags & VM_LOCKED) {
20741da177e4SLinus Torvalds 		unsigned long locked, lock_limit;
207593ea1d0aSChris Wright 		locked = len >> PAGE_SHIFT;
207693ea1d0aSChris Wright 		locked += mm->locked_vm;
20771da177e4SLinus Torvalds 		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
207893ea1d0aSChris Wright 		lock_limit >>= PAGE_SHIFT;
20791da177e4SLinus Torvalds 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
20801da177e4SLinus Torvalds 			return -EAGAIN;
20811da177e4SLinus Torvalds 	}
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	/*
20841da177e4SLinus Torvalds 	 * mm->mmap_sem is required to protect against another thread
20851da177e4SLinus Torvalds 	 * changing the mappings in case we sleep.
20861da177e4SLinus Torvalds 	 */
20871da177e4SLinus Torvalds 	verify_mm_writelocked(mm);
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	/*
20901da177e4SLinus Torvalds 	 * Clear old maps.  this also does some error checking for us
20911da177e4SLinus Torvalds 	 */
20921da177e4SLinus Torvalds  munmap_back:
20931da177e4SLinus Torvalds 	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
20941da177e4SLinus Torvalds 	if (vma && vma->vm_start < addr + len) {
20951da177e4SLinus Torvalds 		if (do_munmap(mm, addr, len))
20961da177e4SLinus Torvalds 			return -ENOMEM;
20971da177e4SLinus Torvalds 		goto munmap_back;
20981da177e4SLinus Torvalds 	}
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	/* Check against address space limits *after* clearing old maps... */
2101119f657cSakpm@osdl.org 	if (!may_expand_vm(mm, len >> PAGE_SHIFT))
21021da177e4SLinus Torvalds 		return -ENOMEM;
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	if (mm->map_count > sysctl_max_map_count)
21051da177e4SLinus Torvalds 		return -ENOMEM;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 	if (security_vm_enough_memory(len >> PAGE_SHIFT))
21081da177e4SLinus Torvalds 		return -ENOMEM;
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	/* Can we just expand an old private anonymous mapping? */
2111ba470de4SRik van Riel 	vma = vma_merge(mm, prev, addr, addr + len, flags,
2112ba470de4SRik van Riel 					NULL, NULL, pgoff, NULL);
2113ba470de4SRik van Riel 	if (vma)
21141da177e4SLinus Torvalds 		goto out;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/*
21171da177e4SLinus Torvalds 	 * create a vma struct for an anonymous mapping
21181da177e4SLinus Torvalds 	 */
2119c5e3b83eSPekka Enberg 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
21201da177e4SLinus Torvalds 	if (!vma) {
21211da177e4SLinus Torvalds 		vm_unacct_memory(len >> PAGE_SHIFT);
21221da177e4SLinus Torvalds 		return -ENOMEM;
21231da177e4SLinus Torvalds 	}
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 	vma->vm_mm = mm;
21261da177e4SLinus Torvalds 	vma->vm_start = addr;
21271da177e4SLinus Torvalds 	vma->vm_end = addr + len;
21281da177e4SLinus Torvalds 	vma->vm_pgoff = pgoff;
21291da177e4SLinus Torvalds 	vma->vm_flags = flags;
21303ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(flags);
21311da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
21321da177e4SLinus Torvalds out:
21331da177e4SLinus Torvalds 	mm->total_vm += len >> PAGE_SHIFT;
21341da177e4SLinus Torvalds 	if (flags & VM_LOCKED) {
2135ba470de4SRik van Riel 		if (!mlock_vma_pages_range(vma, addr, addr + len))
2136ba470de4SRik van Riel 			mm->locked_vm += (len >> PAGE_SHIFT);
21371da177e4SLinus Torvalds 	}
21381da177e4SLinus Torvalds 	return addr;
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds EXPORT_SYMBOL(do_brk);
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds /* Release all mmaps. */
21441da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
21451da177e4SLinus Torvalds {
21461da177e4SLinus Torvalds 	struct mmu_gather *tlb;
2147ba470de4SRik van Riel 	struct vm_area_struct *vma;
21481da177e4SLinus Torvalds 	unsigned long nr_accounted = 0;
2149ee39b37bSHugh Dickins 	unsigned long end;
21501da177e4SLinus Torvalds 
2151d6dd61c8SJeremy Fitzhardinge 	/* mm's last user has gone, and its about to be pulled down */
2152cddb8a5cSAndrea Arcangeli 	mmu_notifier_release(mm);
2153d6dd61c8SJeremy Fitzhardinge 
2154ba470de4SRik van Riel 	if (mm->locked_vm) {
2155ba470de4SRik van Riel 		vma = mm->mmap;
2156ba470de4SRik van Riel 		while (vma) {
2157ba470de4SRik van Riel 			if (vma->vm_flags & VM_LOCKED)
2158ba470de4SRik van Riel 				munlock_vma_pages_all(vma);
2159ba470de4SRik van Riel 			vma = vma->vm_next;
2160ba470de4SRik van Riel 		}
2161ba470de4SRik van Riel 	}
21629480c53eSJeremy Fitzhardinge 
21639480c53eSJeremy Fitzhardinge 	arch_exit_mmap(mm);
21649480c53eSJeremy Fitzhardinge 
2165ba470de4SRik van Riel 	vma = mm->mmap;
21669480c53eSJeremy Fitzhardinge 	if (!vma)	/* Can happen if dup_mmap() received an OOM */
21679480c53eSJeremy Fitzhardinge 		return;
21689480c53eSJeremy Fitzhardinge 
21691da177e4SLinus Torvalds 	lru_add_drain();
21701da177e4SLinus Torvalds 	flush_cache_mm(mm);
2171e0da382cSHugh Dickins 	tlb = tlb_gather_mmu(mm, 1);
2172901608d9SOleg Nesterov 	/* update_hiwater_rss(mm) here? but nobody should be looking */
2173e0da382cSHugh Dickins 	/* Use -1 here to ensure all VMAs in the mm are unmapped */
2174508034a3SHugh Dickins 	end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
21751da177e4SLinus Torvalds 	vm_unacct_memory(nr_accounted);
21769ba69294SHugh Dickins 
217742b77728SJan Beulich 	free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
2178ee39b37bSHugh Dickins 	tlb_finish_mmu(tlb, 0, end);
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	/*
21818f4f8c16SHugh Dickins 	 * Walk the list again, actually closing and freeing it,
21828f4f8c16SHugh Dickins 	 * with preemption enabled, without holding any MM locks.
21831da177e4SLinus Torvalds 	 */
2184a8fb5618SHugh Dickins 	while (vma)
2185a8fb5618SHugh Dickins 		vma = remove_vma(vma);
2186e0da382cSHugh Dickins 
2187e2cdef8cSHugh Dickins 	BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address
21911da177e4SLinus Torvalds  * and into the inode's i_mmap tree.  If vm_file is non-NULL
21921da177e4SLinus Torvalds  * then i_mmap_lock is taken here.
21931da177e4SLinus Torvalds  */
21941da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
21951da177e4SLinus Torvalds {
21961da177e4SLinus Torvalds 	struct vm_area_struct * __vma, * prev;
21971da177e4SLinus Torvalds 	struct rb_node ** rb_link, * rb_parent;
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	/*
22001da177e4SLinus Torvalds 	 * The vm_pgoff of a purely anonymous vma should be irrelevant
22011da177e4SLinus Torvalds 	 * until its first write fault, when page's anon_vma and index
22021da177e4SLinus Torvalds 	 * are set.  But now set the vm_pgoff it will almost certainly
22031da177e4SLinus Torvalds 	 * end up with (unless mremap moves it elsewhere before that
22041da177e4SLinus Torvalds 	 * first wfault), so /proc/pid/maps tells a consistent story.
22051da177e4SLinus Torvalds 	 *
22061da177e4SLinus Torvalds 	 * By setting it to reflect the virtual start address of the
22071da177e4SLinus Torvalds 	 * vma, merges and splits can happen in a seamless way, just
22081da177e4SLinus Torvalds 	 * using the existing file pgoff checks and manipulations.
22091da177e4SLinus Torvalds 	 * Similarly in do_mmap_pgoff and in do_brk.
22101da177e4SLinus Torvalds 	 */
22111da177e4SLinus Torvalds 	if (!vma->vm_file) {
22121da177e4SLinus Torvalds 		BUG_ON(vma->anon_vma);
22131da177e4SLinus Torvalds 		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
22141da177e4SLinus Torvalds 	}
22151da177e4SLinus Torvalds 	__vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
22161da177e4SLinus Torvalds 	if (__vma && __vma->vm_start < vma->vm_end)
22171da177e4SLinus Torvalds 		return -ENOMEM;
22182fd4ef85SHugh Dickins 	if ((vma->vm_flags & VM_ACCOUNT) &&
221934b4e4aaSAlan Cox 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
22202fd4ef85SHugh Dickins 		return -ENOMEM;
22211da177e4SLinus Torvalds 	vma_link(mm, vma, prev, rb_link, rb_parent);
22221da177e4SLinus Torvalds 	return 0;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds /*
22261da177e4SLinus Torvalds  * Copy the vma structure to a new location in the same mm,
22271da177e4SLinus Torvalds  * prior to moving page table entries, to effect an mremap move.
22281da177e4SLinus Torvalds  */
22291da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
22301da177e4SLinus Torvalds 	unsigned long addr, unsigned long len, pgoff_t pgoff)
22311da177e4SLinus Torvalds {
22321da177e4SLinus Torvalds 	struct vm_area_struct *vma = *vmap;
22331da177e4SLinus Torvalds 	unsigned long vma_start = vma->vm_start;
22341da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
22351da177e4SLinus Torvalds 	struct vm_area_struct *new_vma, *prev;
22361da177e4SLinus Torvalds 	struct rb_node **rb_link, *rb_parent;
22371da177e4SLinus Torvalds 	struct mempolicy *pol;
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	/*
22401da177e4SLinus Torvalds 	 * If anonymous vma has not yet been faulted, update new pgoff
22411da177e4SLinus Torvalds 	 * to match new location, to increase its chance of merging.
22421da177e4SLinus Torvalds 	 */
22431da177e4SLinus Torvalds 	if (!vma->vm_file && !vma->anon_vma)
22441da177e4SLinus Torvalds 		pgoff = addr >> PAGE_SHIFT;
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
22471da177e4SLinus Torvalds 	new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
22481da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
22491da177e4SLinus Torvalds 	if (new_vma) {
22501da177e4SLinus Torvalds 		/*
22511da177e4SLinus Torvalds 		 * Source vma may have been merged into new_vma
22521da177e4SLinus Torvalds 		 */
22531da177e4SLinus Torvalds 		if (vma_start >= new_vma->vm_start &&
22541da177e4SLinus Torvalds 		    vma_start < new_vma->vm_end)
22551da177e4SLinus Torvalds 			*vmap = new_vma;
22561da177e4SLinus Torvalds 	} else {
2257e94b1766SChristoph Lameter 		new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
22581da177e4SLinus Torvalds 		if (new_vma) {
22591da177e4SLinus Torvalds 			*new_vma = *vma;
2260846a16bfSLee Schermerhorn 			pol = mpol_dup(vma_policy(vma));
22611da177e4SLinus Torvalds 			if (IS_ERR(pol)) {
22621da177e4SLinus Torvalds 				kmem_cache_free(vm_area_cachep, new_vma);
22631da177e4SLinus Torvalds 				return NULL;
22641da177e4SLinus Torvalds 			}
22651da177e4SLinus Torvalds 			vma_set_policy(new_vma, pol);
22661da177e4SLinus Torvalds 			new_vma->vm_start = addr;
22671da177e4SLinus Torvalds 			new_vma->vm_end = addr + len;
22681da177e4SLinus Torvalds 			new_vma->vm_pgoff = pgoff;
2269925d1c40SMatt Helsley 			if (new_vma->vm_file) {
22701da177e4SLinus Torvalds 				get_file(new_vma->vm_file);
2271925d1c40SMatt Helsley 				if (vma->vm_flags & VM_EXECUTABLE)
2272925d1c40SMatt Helsley 					added_exe_file_vma(mm);
2273925d1c40SMatt Helsley 			}
22741da177e4SLinus Torvalds 			if (new_vma->vm_ops && new_vma->vm_ops->open)
22751da177e4SLinus Torvalds 				new_vma->vm_ops->open(new_vma);
22761da177e4SLinus Torvalds 			vma_link(mm, new_vma, prev, rb_link, rb_parent);
22771da177e4SLinus Torvalds 		}
22781da177e4SLinus Torvalds 	}
22791da177e4SLinus Torvalds 	return new_vma;
22801da177e4SLinus Torvalds }
2281119f657cSakpm@osdl.org 
2282119f657cSakpm@osdl.org /*
2283119f657cSakpm@osdl.org  * Return true if the calling process may expand its vm space by the passed
2284119f657cSakpm@osdl.org  * number of pages
2285119f657cSakpm@osdl.org  */
2286119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2287119f657cSakpm@osdl.org {
2288119f657cSakpm@osdl.org 	unsigned long cur = mm->total_vm;	/* pages */
2289119f657cSakpm@osdl.org 	unsigned long lim;
2290119f657cSakpm@osdl.org 
2291119f657cSakpm@osdl.org 	lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2292119f657cSakpm@osdl.org 
2293119f657cSakpm@osdl.org 	if (cur + npages > lim)
2294119f657cSakpm@osdl.org 		return 0;
2295119f657cSakpm@osdl.org 	return 1;
2296119f657cSakpm@osdl.org }
2297fa5dc22fSRoland McGrath 
2298fa5dc22fSRoland McGrath 
2299b1d0e4f5SNick Piggin static int special_mapping_fault(struct vm_area_struct *vma,
2300b1d0e4f5SNick Piggin 				struct vm_fault *vmf)
2301fa5dc22fSRoland McGrath {
2302b1d0e4f5SNick Piggin 	pgoff_t pgoff;
2303fa5dc22fSRoland McGrath 	struct page **pages;
2304fa5dc22fSRoland McGrath 
2305b1d0e4f5SNick Piggin 	/*
2306b1d0e4f5SNick Piggin 	 * special mappings have no vm_file, and in that case, the mm
2307b1d0e4f5SNick Piggin 	 * uses vm_pgoff internally. So we have to subtract it from here.
2308b1d0e4f5SNick Piggin 	 * We are allowed to do this because we are the mm; do not copy
2309b1d0e4f5SNick Piggin 	 * this code into drivers!
2310b1d0e4f5SNick Piggin 	 */
2311b1d0e4f5SNick Piggin 	pgoff = vmf->pgoff - vma->vm_pgoff;
2312fa5dc22fSRoland McGrath 
2313b1d0e4f5SNick Piggin 	for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2314b1d0e4f5SNick Piggin 		pgoff--;
2315fa5dc22fSRoland McGrath 
2316fa5dc22fSRoland McGrath 	if (*pages) {
2317fa5dc22fSRoland McGrath 		struct page *page = *pages;
2318fa5dc22fSRoland McGrath 		get_page(page);
2319b1d0e4f5SNick Piggin 		vmf->page = page;
2320b1d0e4f5SNick Piggin 		return 0;
2321fa5dc22fSRoland McGrath 	}
2322fa5dc22fSRoland McGrath 
2323b1d0e4f5SNick Piggin 	return VM_FAULT_SIGBUS;
2324fa5dc22fSRoland McGrath }
2325fa5dc22fSRoland McGrath 
2326fa5dc22fSRoland McGrath /*
2327fa5dc22fSRoland McGrath  * Having a close hook prevents vma merging regardless of flags.
2328fa5dc22fSRoland McGrath  */
2329fa5dc22fSRoland McGrath static void special_mapping_close(struct vm_area_struct *vma)
2330fa5dc22fSRoland McGrath {
2331fa5dc22fSRoland McGrath }
2332fa5dc22fSRoland McGrath 
2333f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct special_mapping_vmops = {
2334fa5dc22fSRoland McGrath 	.close = special_mapping_close,
2335b1d0e4f5SNick Piggin 	.fault = special_mapping_fault,
2336fa5dc22fSRoland McGrath };
2337fa5dc22fSRoland McGrath 
2338fa5dc22fSRoland McGrath /*
2339fa5dc22fSRoland McGrath  * Called with mm->mmap_sem held for writing.
2340fa5dc22fSRoland McGrath  * Insert a new vma covering the given region, with the given flags.
2341fa5dc22fSRoland McGrath  * Its pages are supplied by the given array of struct page *.
2342fa5dc22fSRoland McGrath  * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2343fa5dc22fSRoland McGrath  * The region past the last page supplied will always produce SIGBUS.
2344fa5dc22fSRoland McGrath  * The array pointer and the pages it points to are assumed to stay alive
2345fa5dc22fSRoland McGrath  * for as long as this mapping might exist.
2346fa5dc22fSRoland McGrath  */
2347fa5dc22fSRoland McGrath int install_special_mapping(struct mm_struct *mm,
2348fa5dc22fSRoland McGrath 			    unsigned long addr, unsigned long len,
2349fa5dc22fSRoland McGrath 			    unsigned long vm_flags, struct page **pages)
2350fa5dc22fSRoland McGrath {
2351fa5dc22fSRoland McGrath 	struct vm_area_struct *vma;
2352fa5dc22fSRoland McGrath 
2353fa5dc22fSRoland McGrath 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2354fa5dc22fSRoland McGrath 	if (unlikely(vma == NULL))
2355fa5dc22fSRoland McGrath 		return -ENOMEM;
2356fa5dc22fSRoland McGrath 
2357fa5dc22fSRoland McGrath 	vma->vm_mm = mm;
2358fa5dc22fSRoland McGrath 	vma->vm_start = addr;
2359fa5dc22fSRoland McGrath 	vma->vm_end = addr + len;
2360fa5dc22fSRoland McGrath 
23612f98735cSNick Piggin 	vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
23623ed75eb8SColy Li 	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2363fa5dc22fSRoland McGrath 
2364fa5dc22fSRoland McGrath 	vma->vm_ops = &special_mapping_vmops;
2365fa5dc22fSRoland McGrath 	vma->vm_private_data = pages;
2366fa5dc22fSRoland McGrath 
2367fa5dc22fSRoland McGrath 	if (unlikely(insert_vm_struct(mm, vma))) {
2368fa5dc22fSRoland McGrath 		kmem_cache_free(vm_area_cachep, vma);
2369fa5dc22fSRoland McGrath 		return -ENOMEM;
2370fa5dc22fSRoland McGrath 	}
2371fa5dc22fSRoland McGrath 
2372fa5dc22fSRoland McGrath 	mm->total_vm += len >> PAGE_SHIFT;
2373fa5dc22fSRoland McGrath 
2374cdd6c482SIngo Molnar 	perf_event_mmap(vma);
2375089dd79dSPeter Zijlstra 
2376fa5dc22fSRoland McGrath 	return 0;
2377fa5dc22fSRoland McGrath }
23787906d00cSAndrea Arcangeli 
23797906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex);
23807906d00cSAndrea Arcangeli 
2381454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
23827906d00cSAndrea Arcangeli {
23837906d00cSAndrea Arcangeli 	if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
23847906d00cSAndrea Arcangeli 		/*
23857906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change from under us
23867906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
23877906d00cSAndrea Arcangeli 		 */
2388454ed842SPeter Zijlstra 		spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
23897906d00cSAndrea Arcangeli 		/*
23907906d00cSAndrea Arcangeli 		 * We can safely modify head.next after taking the
23917906d00cSAndrea Arcangeli 		 * anon_vma->lock. If some other vma in this mm shares
23927906d00cSAndrea Arcangeli 		 * the same anon_vma we won't take it again.
23937906d00cSAndrea Arcangeli 		 *
23947906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
23957906d00cSAndrea Arcangeli 		 * can't change from under us thanks to the
23967906d00cSAndrea Arcangeli 		 * anon_vma->lock.
23977906d00cSAndrea Arcangeli 		 */
23987906d00cSAndrea Arcangeli 		if (__test_and_set_bit(0, (unsigned long *)
23997906d00cSAndrea Arcangeli 				       &anon_vma->head.next))
24007906d00cSAndrea Arcangeli 			BUG();
24017906d00cSAndrea Arcangeli 	}
24027906d00cSAndrea Arcangeli }
24037906d00cSAndrea Arcangeli 
2404454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
24057906d00cSAndrea Arcangeli {
24067906d00cSAndrea Arcangeli 	if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
24077906d00cSAndrea Arcangeli 		/*
24087906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change from under us because
24097906d00cSAndrea Arcangeli 		 * we hold the mm_all_locks_mutex.
24107906d00cSAndrea Arcangeli 		 *
24117906d00cSAndrea Arcangeli 		 * Operations on ->flags have to be atomic because
24127906d00cSAndrea Arcangeli 		 * even if AS_MM_ALL_LOCKS is stable thanks to the
24137906d00cSAndrea Arcangeli 		 * mm_all_locks_mutex, there may be other cpus
24147906d00cSAndrea Arcangeli 		 * changing other bitflags in parallel to us.
24157906d00cSAndrea Arcangeli 		 */
24167906d00cSAndrea Arcangeli 		if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
24177906d00cSAndrea Arcangeli 			BUG();
2418454ed842SPeter Zijlstra 		spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
24197906d00cSAndrea Arcangeli 	}
24207906d00cSAndrea Arcangeli }
24217906d00cSAndrea Arcangeli 
24227906d00cSAndrea Arcangeli /*
24237906d00cSAndrea Arcangeli  * This operation locks against the VM for all pte/vma/mm related
24247906d00cSAndrea Arcangeli  * operations that could ever happen on a certain mm. This includes
24257906d00cSAndrea Arcangeli  * vmtruncate, try_to_unmap, and all page faults.
24267906d00cSAndrea Arcangeli  *
24277906d00cSAndrea Arcangeli  * The caller must take the mmap_sem in write mode before calling
24287906d00cSAndrea Arcangeli  * mm_take_all_locks(). The caller isn't allowed to release the
24297906d00cSAndrea Arcangeli  * mmap_sem until mm_drop_all_locks() returns.
24307906d00cSAndrea Arcangeli  *
24317906d00cSAndrea Arcangeli  * mmap_sem in write mode is required in order to block all operations
24327906d00cSAndrea Arcangeli  * that could modify pagetables and free pages without need of
24337906d00cSAndrea Arcangeli  * altering the vma layout (for example populate_range() with
24347906d00cSAndrea Arcangeli  * nonlinear vmas). It's also needed in write mode to avoid new
24357906d00cSAndrea Arcangeli  * anon_vmas to be associated with existing vmas.
24367906d00cSAndrea Arcangeli  *
24377906d00cSAndrea Arcangeli  * A single task can't take more than one mm_take_all_locks() in a row
24387906d00cSAndrea Arcangeli  * or it would deadlock.
24397906d00cSAndrea Arcangeli  *
24407906d00cSAndrea Arcangeli  * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
24417906d00cSAndrea Arcangeli  * mapping->flags avoid to take the same lock twice, if more than one
24427906d00cSAndrea Arcangeli  * vma in this mm is backed by the same anon_vma or address_space.
24437906d00cSAndrea Arcangeli  *
24447906d00cSAndrea Arcangeli  * We can take all the locks in random order because the VM code
24457906d00cSAndrea Arcangeli  * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
24467906d00cSAndrea Arcangeli  * takes more than one of them in a row. Secondly we're protected
24477906d00cSAndrea Arcangeli  * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
24487906d00cSAndrea Arcangeli  *
24497906d00cSAndrea Arcangeli  * mm_take_all_locks() and mm_drop_all_locks are expensive operations
24507906d00cSAndrea Arcangeli  * that may have to take thousand of locks.
24517906d00cSAndrea Arcangeli  *
24527906d00cSAndrea Arcangeli  * mm_take_all_locks() can fail if it's interrupted by signals.
24537906d00cSAndrea Arcangeli  */
24547906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm)
24557906d00cSAndrea Arcangeli {
24567906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
24577906d00cSAndrea Arcangeli 	int ret = -EINTR;
24587906d00cSAndrea Arcangeli 
24597906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
24607906d00cSAndrea Arcangeli 
24617906d00cSAndrea Arcangeli 	mutex_lock(&mm_all_locks_mutex);
24627906d00cSAndrea Arcangeli 
24637906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
24647906d00cSAndrea Arcangeli 		if (signal_pending(current))
24657906d00cSAndrea Arcangeli 			goto out_unlock;
24667906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
2467454ed842SPeter Zijlstra 			vm_lock_mapping(mm, vma->vm_file->f_mapping);
24687906d00cSAndrea Arcangeli 	}
24697cd5a02fSPeter Zijlstra 
24707cd5a02fSPeter Zijlstra 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
24717cd5a02fSPeter Zijlstra 		if (signal_pending(current))
24727cd5a02fSPeter Zijlstra 			goto out_unlock;
24737cd5a02fSPeter Zijlstra 		if (vma->anon_vma)
24747cd5a02fSPeter Zijlstra 			vm_lock_anon_vma(mm, vma->anon_vma);
24757cd5a02fSPeter Zijlstra 	}
24767cd5a02fSPeter Zijlstra 
24777906d00cSAndrea Arcangeli 	ret = 0;
24787906d00cSAndrea Arcangeli 
24797906d00cSAndrea Arcangeli out_unlock:
24807906d00cSAndrea Arcangeli 	if (ret)
24817906d00cSAndrea Arcangeli 		mm_drop_all_locks(mm);
24827906d00cSAndrea Arcangeli 
24837906d00cSAndrea Arcangeli 	return ret;
24847906d00cSAndrea Arcangeli }
24857906d00cSAndrea Arcangeli 
24867906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
24877906d00cSAndrea Arcangeli {
24887906d00cSAndrea Arcangeli 	if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
24897906d00cSAndrea Arcangeli 		/*
24907906d00cSAndrea Arcangeli 		 * The LSB of head.next can't change to 0 from under
24917906d00cSAndrea Arcangeli 		 * us because we hold the mm_all_locks_mutex.
24927906d00cSAndrea Arcangeli 		 *
24937906d00cSAndrea Arcangeli 		 * We must however clear the bitflag before unlocking
24947906d00cSAndrea Arcangeli 		 * the vma so the users using the anon_vma->head will
24957906d00cSAndrea Arcangeli 		 * never see our bitflag.
24967906d00cSAndrea Arcangeli 		 *
24977906d00cSAndrea Arcangeli 		 * No need of atomic instructions here, head.next
24987906d00cSAndrea Arcangeli 		 * can't change from under us until we release the
24997906d00cSAndrea Arcangeli 		 * anon_vma->lock.
25007906d00cSAndrea Arcangeli 		 */
25017906d00cSAndrea Arcangeli 		if (!__test_and_clear_bit(0, (unsigned long *)
25027906d00cSAndrea Arcangeli 					  &anon_vma->head.next))
25037906d00cSAndrea Arcangeli 			BUG();
25047906d00cSAndrea Arcangeli 		spin_unlock(&anon_vma->lock);
25057906d00cSAndrea Arcangeli 	}
25067906d00cSAndrea Arcangeli }
25077906d00cSAndrea Arcangeli 
25087906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping)
25097906d00cSAndrea Arcangeli {
25107906d00cSAndrea Arcangeli 	if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
25117906d00cSAndrea Arcangeli 		/*
25127906d00cSAndrea Arcangeli 		 * AS_MM_ALL_LOCKS can't change to 0 from under us
25137906d00cSAndrea Arcangeli 		 * because we hold the mm_all_locks_mutex.
25147906d00cSAndrea Arcangeli 		 */
25157906d00cSAndrea Arcangeli 		spin_unlock(&mapping->i_mmap_lock);
25167906d00cSAndrea Arcangeli 		if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
25177906d00cSAndrea Arcangeli 					&mapping->flags))
25187906d00cSAndrea Arcangeli 			BUG();
25197906d00cSAndrea Arcangeli 	}
25207906d00cSAndrea Arcangeli }
25217906d00cSAndrea Arcangeli 
25227906d00cSAndrea Arcangeli /*
25237906d00cSAndrea Arcangeli  * The mmap_sem cannot be released by the caller until
25247906d00cSAndrea Arcangeli  * mm_drop_all_locks() returns.
25257906d00cSAndrea Arcangeli  */
25267906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm)
25277906d00cSAndrea Arcangeli {
25287906d00cSAndrea Arcangeli 	struct vm_area_struct *vma;
25297906d00cSAndrea Arcangeli 
25307906d00cSAndrea Arcangeli 	BUG_ON(down_read_trylock(&mm->mmap_sem));
25317906d00cSAndrea Arcangeli 	BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
25327906d00cSAndrea Arcangeli 
25337906d00cSAndrea Arcangeli 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
25347906d00cSAndrea Arcangeli 		if (vma->anon_vma)
25357906d00cSAndrea Arcangeli 			vm_unlock_anon_vma(vma->anon_vma);
25367906d00cSAndrea Arcangeli 		if (vma->vm_file && vma->vm_file->f_mapping)
25377906d00cSAndrea Arcangeli 			vm_unlock_mapping(vma->vm_file->f_mapping);
25387906d00cSAndrea Arcangeli 	}
25397906d00cSAndrea Arcangeli 
25407906d00cSAndrea Arcangeli 	mutex_unlock(&mm_all_locks_mutex);
25417906d00cSAndrea Arcangeli }
25428feae131SDavid Howells 
25438feae131SDavid Howells /*
25448feae131SDavid Howells  * initialise the VMA slab
25458feae131SDavid Howells  */
25468feae131SDavid Howells void __init mmap_init(void)
25478feae131SDavid Howells {
254800a62ce9SKOSAKI Motohiro 	int ret;
254900a62ce9SKOSAKI Motohiro 
255000a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
255100a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
25528feae131SDavid Howells }
2553