11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * mm/mmap.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by obz. 51da177e4SLinus Torvalds * 6046c6884SAlan Cox * Address space accounting code <alan@lxorguk.ukuu.org.uk> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/slab.h> 104af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/shm.h> 131da177e4SLinus Torvalds #include <linux/mman.h> 141da177e4SLinus Torvalds #include <linux/pagemap.h> 151da177e4SLinus Torvalds #include <linux/swap.h> 161da177e4SLinus Torvalds #include <linux/syscalls.h> 17c59ede7bSRandy.Dunlap #include <linux/capability.h> 181da177e4SLinus Torvalds #include <linux/init.h> 191da177e4SLinus Torvalds #include <linux/file.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/personality.h> 221da177e4SLinus Torvalds #include <linux/security.h> 231da177e4SLinus Torvalds #include <linux/hugetlb.h> 241da177e4SLinus Torvalds #include <linux/profile.h> 25b95f1b31SPaul Gortmaker #include <linux/export.h> 261da177e4SLinus Torvalds #include <linux/mount.h> 271da177e4SLinus Torvalds #include <linux/mempolicy.h> 281da177e4SLinus Torvalds #include <linux/rmap.h> 29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 30cdd6c482SIngo Molnar #include <linux/perf_event.h> 31120a795dSAl Viro #include <linux/audit.h> 32b15d00b6SAndrea Arcangeli #include <linux/khugepaged.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <asm/uaccess.h> 351da177e4SLinus Torvalds #include <asm/cacheflush.h> 361da177e4SLinus Torvalds #include <asm/tlb.h> 37d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h> 381da177e4SLinus Torvalds 3942b77728SJan Beulich #include "internal.h" 4042b77728SJan Beulich 413a459756SKirill Korotaev #ifndef arch_mmap_check 423a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags) (0) 433a459756SKirill Korotaev #endif 443a459756SKirill Korotaev 4508e7d9b5SMartin Schwidefsky #ifndef arch_rebalance_pgtables 4608e7d9b5SMartin Schwidefsky #define arch_rebalance_pgtables(addr, len) (addr) 4708e7d9b5SMartin Schwidefsky #endif 4808e7d9b5SMartin Schwidefsky 49e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm, 50e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 51e0da382cSHugh Dickins unsigned long start, unsigned long end); 52e0da382cSHugh Dickins 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * WARNING: the debugging will use recursive algorithms so never enable this 551da177e4SLinus Torvalds * unless you know what you are doing. 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds #undef DEBUG_MM_RB 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation. 601da177e4SLinus Torvalds * this is due to the limited x86 page protection hardware. The expected 611da177e4SLinus Torvalds * behavior is in parens: 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * map_type prot 641da177e4SLinus Torvalds * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC 651da177e4SLinus Torvalds * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes 661da177e4SLinus Torvalds * w: (no) no w: (no) no w: (yes) yes w: (no) no 671da177e4SLinus Torvalds * x: (no) no x: (no) yes x: (no) yes x: (yes) yes 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes 701da177e4SLinus Torvalds * w: (no) no w: (no) no w: (copy) copy w: (no) no 711da177e4SLinus Torvalds * x: (no) no x: (no) yes x: (no) yes x: (yes) yes 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds pgprot_t protection_map[16] = { 751da177e4SLinus Torvalds __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111, 761da177e4SLinus Torvalds __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111 771da177e4SLinus Torvalds }; 781da177e4SLinus Torvalds 79804af2cfSHugh Dickins pgprot_t vm_get_page_prot(unsigned long vm_flags) 80804af2cfSHugh Dickins { 81b845f313SDave Kleikamp return __pgprot(pgprot_val(protection_map[vm_flags & 82b845f313SDave Kleikamp (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) | 83b845f313SDave Kleikamp pgprot_val(arch_vm_get_page_prot(vm_flags))); 84804af2cfSHugh Dickins } 85804af2cfSHugh Dickins EXPORT_SYMBOL(vm_get_page_prot); 86804af2cfSHugh Dickins 8734679d7eSShaohua Li int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */ 8834679d7eSShaohua Li int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */ 89c3d8c141SChristoph Lameter int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT; 9034679d7eSShaohua Li /* 9134679d7eSShaohua Li * Make sure vm_committed_as in one cacheline and not cacheline shared with 9234679d7eSShaohua Li * other variables. It can be updated by several CPUs frequently. 9334679d7eSShaohua Li */ 9434679d7eSShaohua Li struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 971da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 981da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 991da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * We currently support three overcommit policies, which are set via the 1021da177e4SLinus Torvalds * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 1051da177e4SLinus Torvalds * Additional code 2002 Jul 20 by Robert Love. 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 1081da177e4SLinus Torvalds * 1091da177e4SLinus Torvalds * Note this is a helper function intended to be used by LSMs which 1101da177e4SLinus Torvalds * wish to use this logic. 1111da177e4SLinus Torvalds */ 11234b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds unsigned long free, allowed; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds vm_acct_memory(pages); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 1191da177e4SLinus Torvalds * Sometimes we want to use more memory than we have 1201da177e4SLinus Torvalds */ 1211da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 1221da177e4SLinus Torvalds return 0; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 125c15bef30SDmitry Fink free = global_page_state(NR_FREE_PAGES); 126c15bef30SDmitry Fink free += global_page_state(NR_FILE_PAGES); 1271da177e4SLinus Torvalds 128c15bef30SDmitry Fink /* 129c15bef30SDmitry Fink * shmem pages shouldn't be counted as free in this 130c15bef30SDmitry Fink * case, they can't be purged, only swapped out, and 131c15bef30SDmitry Fink * that won't affect the overall amount of available 132c15bef30SDmitry Fink * memory in the system. 133c15bef30SDmitry Fink */ 134c15bef30SDmitry Fink free -= global_page_state(NR_SHMEM); 135c15bef30SDmitry Fink 1361da177e4SLinus Torvalds free += nr_swap_pages; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* 1391da177e4SLinus Torvalds * Any slabs which are created with the 1401da177e4SLinus Torvalds * SLAB_RECLAIM_ACCOUNT flag claim to have contents 1411da177e4SLinus Torvalds * which are reclaimable, under pressure. The dentry 1421da177e4SLinus Torvalds * cache and most inode caches should fall into this 1431da177e4SLinus Torvalds */ 144972d1a7bSChristoph Lameter free += global_page_state(NR_SLAB_RECLAIMABLE); 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 147c15bef30SDmitry Fink * Leave reserved pages. The pages are not for anonymous pages. 148c15bef30SDmitry Fink */ 149c15bef30SDmitry Fink if (free <= totalreserve_pages) 150c15bef30SDmitry Fink goto error; 151c15bef30SDmitry Fink else 152c15bef30SDmitry Fink free -= totalreserve_pages; 153c15bef30SDmitry Fink 154c15bef30SDmitry Fink /* 1551da177e4SLinus Torvalds * Leave the last 3% for root 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds if (!cap_sys_admin) 1581da177e4SLinus Torvalds free -= free / 32; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds if (free > pages) 1611da177e4SLinus Torvalds return 0; 1621da177e4SLinus Torvalds 1636d9f7839SHideo AOKI goto error; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds allowed = (totalram_pages - hugetlb_total_pages()) 1671da177e4SLinus Torvalds * sysctl_overcommit_ratio / 100; 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * Leave the last 3% for root 1701da177e4SLinus Torvalds */ 1711da177e4SLinus Torvalds if (!cap_sys_admin) 1721da177e4SLinus Torvalds allowed -= allowed / 32; 1731da177e4SLinus Torvalds allowed += total_swap_pages; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* Don't let a single process grow too big: 1761da177e4SLinus Torvalds leave 3% of the size of this process for other processes */ 177731572d3SAlan Cox if (mm) 17834b4e4aaSAlan Cox allowed -= mm->total_vm / 32; 1791da177e4SLinus Torvalds 18000a62ce9SKOSAKI Motohiro if (percpu_counter_read_positive(&vm_committed_as) < allowed) 1811da177e4SLinus Torvalds return 0; 1826d9f7839SHideo AOKI error: 1831da177e4SLinus Torvalds vm_unacct_memory(pages); 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds return -ENOMEM; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* 1893d48ae45SPeter Zijlstra * Requires inode->i_mapping->i_mmap_mutex 1901da177e4SLinus Torvalds */ 1911da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1921da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds if (vma->vm_flags & VM_DENYWRITE) 195d3ac7f89SJosef "Jeff" Sipek atomic_inc(&file->f_path.dentry->d_inode->i_writecount); 1961da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1971da177e4SLinus Torvalds mapping->i_mmap_writable--; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 2001da177e4SLinus Torvalds if (unlikely(vma->vm_flags & VM_NONLINEAR)) 2011da177e4SLinus Torvalds list_del_init(&vma->shared.vm_set.list); 2021da177e4SLinus Torvalds else 2031da177e4SLinus Torvalds vma_prio_tree_remove(vma, &mapping->i_mmap); 2041da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /* 208a8fb5618SHugh Dickins * Unlink a file-based vm structure from its prio_tree, to hide 209a8fb5618SHugh Dickins * vma from rmap and vmtruncate before freeing its page tables. 2101da177e4SLinus Torvalds */ 211a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds struct file *file = vma->vm_file; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds if (file) { 2161da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 2173d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 2181da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 2193d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 2201da177e4SLinus Torvalds } 221a8fb5618SHugh Dickins } 222a8fb5618SHugh Dickins 223a8fb5618SHugh Dickins /* 224a8fb5618SHugh Dickins * Close a vm structure and free it, returning the next. 225a8fb5618SHugh Dickins */ 226a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) 227a8fb5618SHugh Dickins { 228a8fb5618SHugh Dickins struct vm_area_struct *next = vma->vm_next; 229a8fb5618SHugh Dickins 230a8fb5618SHugh Dickins might_sleep(); 2311da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 2321da177e4SLinus Torvalds vma->vm_ops->close(vma); 233925d1c40SMatt Helsley if (vma->vm_file) { 234a8fb5618SHugh Dickins fput(vma->vm_file); 235925d1c40SMatt Helsley if (vma->vm_flags & VM_EXECUTABLE) 236925d1c40SMatt Helsley removed_exe_file_vma(vma->vm_mm); 237925d1c40SMatt Helsley } 238f0be3d32SLee Schermerhorn mpol_put(vma_policy(vma)); 2391da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 240a8fb5618SHugh Dickins return next; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2436a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds unsigned long rlim, retval; 2461da177e4SLinus Torvalds unsigned long newbrk, oldbrk; 2471da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 248a5b4592cSJiri Kosina unsigned long min_brk; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds down_write(&mm->mmap_sem); 2511da177e4SLinus Torvalds 252a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 2535520e894SJiri Kosina /* 2545520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 2555520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 2565520e894SJiri Kosina * to be arbitrarily shifted 2575520e894SJiri Kosina */ 2584471a675SJiri Kosina if (current->brk_randomized) 2595520e894SJiri Kosina min_brk = mm->start_brk; 2605520e894SJiri Kosina else 2615520e894SJiri Kosina min_brk = mm->end_data; 262a5b4592cSJiri Kosina #else 263a5b4592cSJiri Kosina min_brk = mm->start_brk; 264a5b4592cSJiri Kosina #endif 265a5b4592cSJiri Kosina if (brk < min_brk) 2661da177e4SLinus Torvalds goto out; 2671e624196SRam Gupta 2681e624196SRam Gupta /* 2691e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2701e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2711e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2721e624196SRam Gupta * not page aligned -Ram Gupta 2731e624196SRam Gupta */ 27459e99e5bSJiri Slaby rlim = rlimit(RLIMIT_DATA); 275c1d171a0SJiri Kosina if (rlim < RLIM_INFINITY && (brk - mm->start_brk) + 276c1d171a0SJiri Kosina (mm->end_data - mm->start_data) > rlim) 2771e624196SRam Gupta goto out; 2781e624196SRam Gupta 2791da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2801da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2811da177e4SLinus Torvalds if (oldbrk == newbrk) 2821da177e4SLinus Torvalds goto set_brk; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* Always allow shrinking brk. */ 2851da177e4SLinus Torvalds if (brk <= mm->brk) { 2861da177e4SLinus Torvalds if (!do_munmap(mm, newbrk, oldbrk-newbrk)) 2871da177e4SLinus Torvalds goto set_brk; 2881da177e4SLinus Torvalds goto out; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* Check against existing mmap mappings. */ 2921da177e4SLinus Torvalds if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE)) 2931da177e4SLinus Torvalds goto out; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 2961da177e4SLinus Torvalds if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk) 2971da177e4SLinus Torvalds goto out; 2981da177e4SLinus Torvalds set_brk: 2991da177e4SLinus Torvalds mm->brk = brk; 3001da177e4SLinus Torvalds out: 3011da177e4SLinus Torvalds retval = mm->brk; 3021da177e4SLinus Torvalds up_write(&mm->mmap_sem); 3031da177e4SLinus Torvalds return retval; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds #ifdef DEBUG_MM_RB 3071da177e4SLinus Torvalds static int browse_rb(struct rb_root *root) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds int i = 0, j; 3101da177e4SLinus Torvalds struct rb_node *nd, *pn = NULL; 3111da177e4SLinus Torvalds unsigned long prev = 0, pend = 0; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds for (nd = rb_first(root); nd; nd = rb_next(nd)) { 3141da177e4SLinus Torvalds struct vm_area_struct *vma; 3151da177e4SLinus Torvalds vma = rb_entry(nd, struct vm_area_struct, vm_rb); 3161da177e4SLinus Torvalds if (vma->vm_start < prev) 3171da177e4SLinus Torvalds printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1; 3181da177e4SLinus Torvalds if (vma->vm_start < pend) 3191da177e4SLinus Torvalds printk("vm_start %lx pend %lx\n", vma->vm_start, pend); 3201da177e4SLinus Torvalds if (vma->vm_start > vma->vm_end) 3211da177e4SLinus Torvalds printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start); 3221da177e4SLinus Torvalds i++; 3231da177e4SLinus Torvalds pn = nd; 324d1af65d1SDavid Miller prev = vma->vm_start; 325d1af65d1SDavid Miller pend = vma->vm_end; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds j = 0; 3281da177e4SLinus Torvalds for (nd = pn; nd; nd = rb_prev(nd)) { 3291da177e4SLinus Torvalds j++; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds if (i != j) 3321da177e4SLinus Torvalds printk("backwards %d, forwards %d\n", j, i), i = 0; 3331da177e4SLinus Torvalds return i; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds void validate_mm(struct mm_struct *mm) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds int bug = 0; 3391da177e4SLinus Torvalds int i = 0; 3401da177e4SLinus Torvalds struct vm_area_struct *tmp = mm->mmap; 3411da177e4SLinus Torvalds while (tmp) { 3421da177e4SLinus Torvalds tmp = tmp->vm_next; 3431da177e4SLinus Torvalds i++; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds if (i != mm->map_count) 3461da177e4SLinus Torvalds printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1; 3471da177e4SLinus Torvalds i = browse_rb(&mm->mm_rb); 3481da177e4SLinus Torvalds if (i != mm->map_count) 3491da177e4SLinus Torvalds printk("map_count %d rb %d\n", mm->map_count, i), bug = 1; 35046a350efSEric Sesterhenn BUG_ON(bug); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds #else 3531da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 3541da177e4SLinus Torvalds #endif 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds static struct vm_area_struct * 3571da177e4SLinus Torvalds find_vma_prepare(struct mm_struct *mm, unsigned long addr, 3581da177e4SLinus Torvalds struct vm_area_struct **pprev, struct rb_node ***rb_link, 3591da177e4SLinus Torvalds struct rb_node ** rb_parent) 3601da177e4SLinus Torvalds { 3611da177e4SLinus Torvalds struct vm_area_struct * vma; 3621da177e4SLinus Torvalds struct rb_node ** __rb_link, * __rb_parent, * rb_prev; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds __rb_link = &mm->mm_rb.rb_node; 3651da177e4SLinus Torvalds rb_prev = __rb_parent = NULL; 3661da177e4SLinus Torvalds vma = NULL; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds while (*__rb_link) { 3691da177e4SLinus Torvalds struct vm_area_struct *vma_tmp; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds __rb_parent = *__rb_link; 3721da177e4SLinus Torvalds vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (vma_tmp->vm_end > addr) { 3751da177e4SLinus Torvalds vma = vma_tmp; 3761da177e4SLinus Torvalds if (vma_tmp->vm_start <= addr) 377dfe195fbSBenny Halevy break; 3781da177e4SLinus Torvalds __rb_link = &__rb_parent->rb_left; 3791da177e4SLinus Torvalds } else { 3801da177e4SLinus Torvalds rb_prev = __rb_parent; 3811da177e4SLinus Torvalds __rb_link = &__rb_parent->rb_right; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds *pprev = NULL; 3861da177e4SLinus Torvalds if (rb_prev) 3871da177e4SLinus Torvalds *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); 3881da177e4SLinus Torvalds *rb_link = __rb_link; 3891da177e4SLinus Torvalds *rb_parent = __rb_parent; 3901da177e4SLinus Torvalds return vma; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma, 3941da177e4SLinus Torvalds struct rb_node **rb_link, struct rb_node *rb_parent) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds rb_link_node(&vma->vm_rb, rb_parent, rb_link); 3971da177e4SLinus Torvalds rb_insert_color(&vma->vm_rb, &mm->mm_rb); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 400cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds struct file *file; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds file = vma->vm_file; 4051da177e4SLinus Torvalds if (file) { 4061da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds if (vma->vm_flags & VM_DENYWRITE) 409d3ac7f89SJosef "Jeff" Sipek atomic_dec(&file->f_path.dentry->d_inode->i_writecount); 4101da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 4111da177e4SLinus Torvalds mapping->i_mmap_writable++; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 4141da177e4SLinus Torvalds if (unlikely(vma->vm_flags & VM_NONLINEAR)) 4151da177e4SLinus Torvalds vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear); 4161da177e4SLinus Torvalds else 4171da177e4SLinus Torvalds vma_prio_tree_insert(vma, &mapping->i_mmap); 4181da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds static void 4231da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 4241da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node **rb_link, 4251da177e4SLinus Torvalds struct rb_node *rb_parent) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds __vma_link_list(mm, vma, prev, rb_parent); 4281da177e4SLinus Torvalds __vma_link_rb(mm, vma, rb_link, rb_parent); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 4321da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node **rb_link, 4331da177e4SLinus Torvalds struct rb_node *rb_parent) 4341da177e4SLinus Torvalds { 4351da177e4SLinus Torvalds struct address_space *mapping = NULL; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds if (vma->vm_file) 4381da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 4391da177e4SLinus Torvalds 44097a89413SPeter Zijlstra if (mapping) 4413d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds __vma_link(mm, vma, prev, rb_link, rb_parent); 4441da177e4SLinus Torvalds __vma_link_file(vma); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds if (mapping) 4473d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds mm->map_count++; 4501da177e4SLinus Torvalds validate_mm(mm); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds /* 4541da177e4SLinus Torvalds * Helper for vma_adjust in the split_vma insert case: 4551da177e4SLinus Torvalds * insert vm structure into list and rbtree and anon_vma, 4561da177e4SLinus Torvalds * but it has already been inserted into prio_tree earlier. 4571da177e4SLinus Torvalds */ 45848aae425SZhenwenXu static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds struct vm_area_struct *__vma, *prev; 4611da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent); 46446a350efSEric Sesterhenn BUG_ON(__vma && __vma->vm_start < vma->vm_end); 4651da177e4SLinus Torvalds __vma_link(mm, vma, prev, rb_link, rb_parent); 4661da177e4SLinus Torvalds mm->map_count++; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds static inline void 4701da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma, 4711da177e4SLinus Torvalds struct vm_area_struct *prev) 4721da177e4SLinus Torvalds { 473297c5eeeSLinus Torvalds struct vm_area_struct *next = vma->vm_next; 474297c5eeeSLinus Torvalds 475297c5eeeSLinus Torvalds prev->vm_next = next; 476297c5eeeSLinus Torvalds if (next) 477297c5eeeSLinus Torvalds next->vm_prev = prev; 4781da177e4SLinus Torvalds rb_erase(&vma->vm_rb, &mm->mm_rb); 4791da177e4SLinus Torvalds if (mm->mmap_cache == vma) 4801da177e4SLinus Torvalds mm->mmap_cache = prev; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* 4841da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 4851da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 4861da177e4SLinus Torvalds * The following helper function should be used when such adjustments 4871da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 4881da177e4SLinus Torvalds * before we drop the necessary locks. 4891da177e4SLinus Torvalds */ 4905beb4930SRik van Riel int vma_adjust(struct vm_area_struct *vma, unsigned long start, 4911da177e4SLinus Torvalds unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 4941da177e4SLinus Torvalds struct vm_area_struct *next = vma->vm_next; 4951da177e4SLinus Torvalds struct vm_area_struct *importer = NULL; 4961da177e4SLinus Torvalds struct address_space *mapping = NULL; 4971da177e4SLinus Torvalds struct prio_tree_root *root = NULL; 498012f1800SRik van Riel struct anon_vma *anon_vma = NULL; 4991da177e4SLinus Torvalds struct file *file = vma->vm_file; 5001da177e4SLinus Torvalds long adjust_next = 0; 5011da177e4SLinus Torvalds int remove_next = 0; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (next && !insert) { 504287d97acSLinus Torvalds struct vm_area_struct *exporter = NULL; 505287d97acSLinus Torvalds 5061da177e4SLinus Torvalds if (end >= next->vm_end) { 5071da177e4SLinus Torvalds /* 5081da177e4SLinus Torvalds * vma expands, overlapping all the next, and 5091da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 5101da177e4SLinus Torvalds */ 5111da177e4SLinus Torvalds again: remove_next = 1 + (end > next->vm_end); 5121da177e4SLinus Torvalds end = next->vm_end; 513287d97acSLinus Torvalds exporter = next; 5141da177e4SLinus Torvalds importer = vma; 5151da177e4SLinus Torvalds } else if (end > next->vm_start) { 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * vma expands, overlapping part of the next: 5181da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds adjust_next = (end - next->vm_start) >> PAGE_SHIFT; 521287d97acSLinus Torvalds exporter = next; 5221da177e4SLinus Torvalds importer = vma; 5231da177e4SLinus Torvalds } else if (end < vma->vm_end) { 5241da177e4SLinus Torvalds /* 5251da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 5261da177e4SLinus Torvalds * split_vma inserting another: so it must be 5271da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT); 530287d97acSLinus Torvalds exporter = vma; 5311da177e4SLinus Torvalds importer = next; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5345beb4930SRik van Riel /* 5355beb4930SRik van Riel * Easily overlooked: when mprotect shifts the boundary, 5365beb4930SRik van Riel * make sure the expanding vma has anon_vma set if the 5375beb4930SRik van Riel * shrinking vma had, to cover any anon pages imported. 5385beb4930SRik van Riel */ 539287d97acSLinus Torvalds if (exporter && exporter->anon_vma && !importer->anon_vma) { 540287d97acSLinus Torvalds if (anon_vma_clone(importer, exporter)) 5415beb4930SRik van Riel return -ENOMEM; 542287d97acSLinus Torvalds importer->anon_vma = exporter->anon_vma; 5435beb4930SRik van Riel } 5445beb4930SRik van Riel } 5455beb4930SRik van Riel 5461da177e4SLinus Torvalds if (file) { 5471da177e4SLinus Torvalds mapping = file->f_mapping; 5481da177e4SLinus Torvalds if (!(vma->vm_flags & VM_NONLINEAR)) 5491da177e4SLinus Torvalds root = &mapping->i_mmap; 5503d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 5511da177e4SLinus Torvalds if (insert) { 5521da177e4SLinus Torvalds /* 5531da177e4SLinus Torvalds * Put into prio_tree now, so instantiated pages 5541da177e4SLinus Torvalds * are visible to arm/parisc __flush_dcache_page 5551da177e4SLinus Torvalds * throughout; but we cannot insert into address 5561da177e4SLinus Torvalds * space until vma start or end is updated. 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds __vma_link_file(insert); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 56294fcc585SAndrea Arcangeli vma_adjust_trans_huge(vma, start, end, adjust_next); 56394fcc585SAndrea Arcangeli 564012f1800SRik van Riel /* 565012f1800SRik van Riel * When changing only vma->vm_end, we don't really need anon_vma 566012f1800SRik van Riel * lock. This is a fairly rare case by itself, but the anon_vma 567012f1800SRik van Riel * lock may be shared between many sibling processes. Skipping 568012f1800SRik van Riel * the lock for brk adjustments makes a difference sometimes. 569012f1800SRik van Riel */ 5705f70b962SShaohua Li if (vma->anon_vma && (importer || start != vma->vm_start)) { 571012f1800SRik van Riel anon_vma = vma->anon_vma; 572012f1800SRik van Riel anon_vma_lock(anon_vma); 573012f1800SRik van Riel } 574012f1800SRik van Riel 5751da177e4SLinus Torvalds if (root) { 5761da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 5771da177e4SLinus Torvalds vma_prio_tree_remove(vma, root); 5781da177e4SLinus Torvalds if (adjust_next) 5791da177e4SLinus Torvalds vma_prio_tree_remove(next, root); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds vma->vm_start = start; 5831da177e4SLinus Torvalds vma->vm_end = end; 5841da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 5851da177e4SLinus Torvalds if (adjust_next) { 5861da177e4SLinus Torvalds next->vm_start += adjust_next << PAGE_SHIFT; 5871da177e4SLinus Torvalds next->vm_pgoff += adjust_next; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds if (root) { 5911da177e4SLinus Torvalds if (adjust_next) 5921da177e4SLinus Torvalds vma_prio_tree_insert(next, root); 5931da177e4SLinus Torvalds vma_prio_tree_insert(vma, root); 5941da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds if (remove_next) { 5981da177e4SLinus Torvalds /* 5991da177e4SLinus Torvalds * vma_merge has merged next into vma, and needs 6001da177e4SLinus Torvalds * us to remove next before dropping the locks. 6011da177e4SLinus Torvalds */ 6021da177e4SLinus Torvalds __vma_unlink(mm, next, vma); 6031da177e4SLinus Torvalds if (file) 6041da177e4SLinus Torvalds __remove_shared_vm_struct(next, file, mapping); 6051da177e4SLinus Torvalds } else if (insert) { 6061da177e4SLinus Torvalds /* 6071da177e4SLinus Torvalds * split_vma has split insert from vma, and needs 6081da177e4SLinus Torvalds * us to insert it before dropping the locks 6091da177e4SLinus Torvalds * (it may either follow vma or precede it). 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds __insert_vm_struct(mm, insert); 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 614012f1800SRik van Riel if (anon_vma) 615012f1800SRik van Riel anon_vma_unlock(anon_vma); 6161da177e4SLinus Torvalds if (mapping) 6173d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds if (remove_next) { 620925d1c40SMatt Helsley if (file) { 6211da177e4SLinus Torvalds fput(file); 622925d1c40SMatt Helsley if (next->vm_flags & VM_EXECUTABLE) 623925d1c40SMatt Helsley removed_exe_file_vma(mm); 624925d1c40SMatt Helsley } 6255beb4930SRik van Riel if (next->anon_vma) 6265beb4930SRik van Riel anon_vma_merge(vma, next); 6271da177e4SLinus Torvalds mm->map_count--; 628f0be3d32SLee Schermerhorn mpol_put(vma_policy(next)); 6291da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, next); 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * In mprotect's case 6 (see comments on vma_merge), 6321da177e4SLinus Torvalds * we must remove another next too. It would clutter 6331da177e4SLinus Torvalds * up the code too much to do both in one go. 6341da177e4SLinus Torvalds */ 6351da177e4SLinus Torvalds if (remove_next == 2) { 6361da177e4SLinus Torvalds next = vma->vm_next; 6371da177e4SLinus Torvalds goto again; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds validate_mm(mm); 6425beb4930SRik van Riel 6435beb4930SRik van Riel return 0; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* 6471da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 6481da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 6511da177e4SLinus Torvalds struct file *file, unsigned long vm_flags) 6521da177e4SLinus Torvalds { 6538314c4f2SHugh Dickins /* VM_CAN_NONLINEAR may get set later by f_op->mmap() */ 6548314c4f2SHugh Dickins if ((vma->vm_flags ^ vm_flags) & ~VM_CAN_NONLINEAR) 6551da177e4SLinus Torvalds return 0; 6561da177e4SLinus Torvalds if (vma->vm_file != file) 6571da177e4SLinus Torvalds return 0; 6581da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 6591da177e4SLinus Torvalds return 0; 6601da177e4SLinus Torvalds return 1; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 664965f55deSShaohua Li struct anon_vma *anon_vma2, 665965f55deSShaohua Li struct vm_area_struct *vma) 6661da177e4SLinus Torvalds { 667965f55deSShaohua Li /* 668965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 669965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 670965f55deSShaohua Li */ 671965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 672965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 673965f55deSShaohua Li return 1; 674965f55deSShaohua Li return anon_vma1 == anon_vma2; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /* 6781da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 6791da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 6801da177e4SLinus Torvalds * 6811da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 6821da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 6831da177e4SLinus Torvalds * 6841da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 6851da177e4SLinus Torvalds * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which 6861da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds static int 6891da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 6901da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) 6911da177e4SLinus Torvalds { 6921da177e4SLinus Torvalds if (is_mergeable_vma(vma, file, vm_flags) && 693965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 6941da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 6951da177e4SLinus Torvalds return 1; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds return 0; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* 7011da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 7021da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 7031da177e4SLinus Torvalds * 7041da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 7051da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 7061da177e4SLinus Torvalds */ 7071da177e4SLinus Torvalds static int 7081da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 7091da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds if (is_mergeable_vma(vma, file, vm_flags) && 712965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 7131da177e4SLinus Torvalds pgoff_t vm_pglen; 7141da177e4SLinus Torvalds vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 7151da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 7161da177e4SLinus Torvalds return 1; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds return 0; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* 7221da177e4SLinus Torvalds * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out 7231da177e4SLinus Torvalds * whether that can be merged with its predecessor or its successor. 7241da177e4SLinus Torvalds * Or both (it neatly fills a hole). 7251da177e4SLinus Torvalds * 7261da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 7271da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 7281da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 7291da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 7301da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 7311da177e4SLinus Torvalds * case has already been eliminated. 7321da177e4SLinus Torvalds * 7331da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 7341da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 7351da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 7361da177e4SLinus Torvalds * 7371da177e4SLinus Torvalds * AAAA AAAA AAAA AAAA 7381da177e4SLinus Torvalds * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX 7391da177e4SLinus Torvalds * cannot merge might become might become might become 7401da177e4SLinus Torvalds * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or 7411da177e4SLinus Torvalds * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or 7421da177e4SLinus Torvalds * mremap move: PPPPNNNNNNNN 8 7431da177e4SLinus Torvalds * AAAA 7441da177e4SLinus Torvalds * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN 7451da177e4SLinus Torvalds * might become case 1 below case 2 below case 3 below 7461da177e4SLinus Torvalds * 7471da177e4SLinus Torvalds * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX: 7481da177e4SLinus Torvalds * mprotect_fixup updates vm_flags & vm_page_prot on successful return. 7491da177e4SLinus Torvalds */ 7501da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm, 7511da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 7521da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 7531da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 7541da177e4SLinus Torvalds pgoff_t pgoff, struct mempolicy *policy) 7551da177e4SLinus Torvalds { 7561da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 7571da177e4SLinus Torvalds struct vm_area_struct *area, *next; 7585beb4930SRik van Riel int err; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* 7611da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 7621da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 7651da177e4SLinus Torvalds return NULL; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds if (prev) 7681da177e4SLinus Torvalds next = prev->vm_next; 7691da177e4SLinus Torvalds else 7701da177e4SLinus Torvalds next = mm->mmap; 7711da177e4SLinus Torvalds area = next; 7721da177e4SLinus Torvalds if (next && next->vm_end == end) /* cases 6, 7, 8 */ 7731da177e4SLinus Torvalds next = next->vm_next; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * Can it merge with the predecessor? 7771da177e4SLinus Torvalds */ 7781da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 7791da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 7801da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 7811da177e4SLinus Torvalds anon_vma, file, pgoff)) { 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * OK, it can. Can we now merge in the successor as well? 7841da177e4SLinus Torvalds */ 7851da177e4SLinus Torvalds if (next && end == next->vm_start && 7861da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 7871da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 7881da177e4SLinus Torvalds anon_vma, file, pgoff+pglen) && 7891da177e4SLinus Torvalds is_mergeable_anon_vma(prev->anon_vma, 790965f55deSShaohua Li next->anon_vma, NULL)) { 7911da177e4SLinus Torvalds /* cases 1, 6 */ 7925beb4930SRik van Riel err = vma_adjust(prev, prev->vm_start, 7931da177e4SLinus Torvalds next->vm_end, prev->vm_pgoff, NULL); 7941da177e4SLinus Torvalds } else /* cases 2, 5, 7 */ 7955beb4930SRik van Riel err = vma_adjust(prev, prev->vm_start, 7961da177e4SLinus Torvalds end, prev->vm_pgoff, NULL); 7975beb4930SRik van Riel if (err) 7985beb4930SRik van Riel return NULL; 799b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(prev); 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 */ 8115beb4930SRik van Riel err = vma_adjust(prev, prev->vm_start, 8121da177e4SLinus Torvalds addr, prev->vm_pgoff, NULL); 8131da177e4SLinus Torvalds else /* cases 3, 8 */ 8145beb4930SRik van Riel err = vma_adjust(area, addr, next->vm_end, 8151da177e4SLinus Torvalds next->vm_pgoff - pglen, NULL); 8165beb4930SRik van Riel if (err) 8175beb4930SRik van Riel return NULL; 818b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(area); 8191da177e4SLinus Torvalds return area; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds return NULL; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 826d0e9fe17SLinus Torvalds * Rough compatbility check to quickly see if it's even worth looking 827d0e9fe17SLinus Torvalds * at sharing an anon_vma. 828d0e9fe17SLinus Torvalds * 829d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 830d0e9fe17SLinus Torvalds * in things that mprotect may change. 831d0e9fe17SLinus Torvalds * 832d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 833d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 834d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 835d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 836d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 837d0e9fe17SLinus Torvalds */ 838d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 839d0e9fe17SLinus Torvalds { 840d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 841d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 842d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 843d0e9fe17SLinus Torvalds !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) && 844d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 845d0e9fe17SLinus Torvalds } 846d0e9fe17SLinus Torvalds 847d0e9fe17SLinus Torvalds /* 848d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 849d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 850d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 851d0e9fe17SLinus Torvalds * to share the anon_vma. 852d0e9fe17SLinus Torvalds * 853d0e9fe17SLinus Torvalds * NOTE! This runs with mm_sem held for reading, so it is possible that 854d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 855d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 856d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 857d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 858d0e9fe17SLinus Torvalds * we do that ACCESS_ONCE() to make sure that we never re-load the pointer. 859d0e9fe17SLinus Torvalds * 860d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 861d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 862d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 863d0e9fe17SLinus Torvalds * a fork). 864d0e9fe17SLinus Torvalds * 865d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 866d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 867d0e9fe17SLinus Torvalds * a read lock on the mm_sem. 868d0e9fe17SLinus Torvalds */ 869d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 870d0e9fe17SLinus Torvalds { 871d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 872d0e9fe17SLinus Torvalds struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma); 873d0e9fe17SLinus Torvalds 874d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 875d0e9fe17SLinus Torvalds return anon_vma; 876d0e9fe17SLinus Torvalds } 877d0e9fe17SLinus Torvalds return NULL; 878d0e9fe17SLinus Torvalds } 879d0e9fe17SLinus Torvalds 880d0e9fe17SLinus Torvalds /* 8811da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 8821da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 8831da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 8841da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 8851da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 8861da177e4SLinus Torvalds * mprotect. 8871da177e4SLinus Torvalds */ 8881da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 8891da177e4SLinus Torvalds { 890d0e9fe17SLinus Torvalds struct anon_vma *anon_vma; 8911da177e4SLinus Torvalds struct vm_area_struct *near; 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds near = vma->vm_next; 8941da177e4SLinus Torvalds if (!near) 8951da177e4SLinus Torvalds goto try_prev; 8961da177e4SLinus Torvalds 897d0e9fe17SLinus Torvalds anon_vma = reusable_anon_vma(near, vma, near); 898d0e9fe17SLinus Torvalds if (anon_vma) 899d0e9fe17SLinus Torvalds return anon_vma; 9001da177e4SLinus Torvalds try_prev: 9019be34c9dSLinus Torvalds near = vma->vm_prev; 9021da177e4SLinus Torvalds if (!near) 9031da177e4SLinus Torvalds goto none; 9041da177e4SLinus Torvalds 905d0e9fe17SLinus Torvalds anon_vma = reusable_anon_vma(near, near, vma); 906d0e9fe17SLinus Torvalds if (anon_vma) 907d0e9fe17SLinus Torvalds return anon_vma; 9081da177e4SLinus Torvalds none: 9091da177e4SLinus Torvalds /* 9101da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 9111da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 9121da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 9131da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 9141da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 9151da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 9161da177e4SLinus Torvalds */ 9171da177e4SLinus Torvalds return NULL; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 921ab50b8edSHugh Dickins void vm_stat_account(struct mm_struct *mm, unsigned long flags, 9221da177e4SLinus Torvalds struct file *file, long pages) 9231da177e4SLinus Torvalds { 9241da177e4SLinus Torvalds const unsigned long stack_flags 9251da177e4SLinus Torvalds = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN); 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds if (file) { 9281da177e4SLinus Torvalds mm->shared_vm += pages; 9291da177e4SLinus Torvalds if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC) 9301da177e4SLinus Torvalds mm->exec_vm += pages; 9311da177e4SLinus Torvalds } else if (flags & stack_flags) 9321da177e4SLinus Torvalds mm->stack_vm += pages; 9331da177e4SLinus Torvalds if (flags & (VM_RESERVED|VM_IO)) 9341da177e4SLinus Torvalds mm->reserved_vm += pages; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* 93927f5de79SJianjun Kong * The caller must hold down_write(¤t->mm->mmap_sem). 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, 9431da177e4SLinus Torvalds unsigned long len, unsigned long prot, 9441da177e4SLinus Torvalds unsigned long flags, unsigned long pgoff) 9451da177e4SLinus Torvalds { 9461da177e4SLinus Torvalds struct mm_struct * mm = current->mm; 9471da177e4SLinus Torvalds struct inode *inode; 948ca16d140SKOSAKI Motohiro vm_flags_t vm_flags; 9491da177e4SLinus Torvalds int error; 9500165ab44SMiklos Szeredi unsigned long reqprot = prot; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds /* 9531da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 9541da177e4SLinus Torvalds * 9551da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 9561da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 9571da177e4SLinus Torvalds */ 9581da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 959d3ac7f89SJosef "Jeff" Sipek if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC))) 9601da177e4SLinus Torvalds prot |= PROT_EXEC; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds if (!len) 9631da177e4SLinus Torvalds return -EINVAL; 9641da177e4SLinus Torvalds 9657cd94146SEric Paris if (!(flags & MAP_FIXED)) 9667cd94146SEric Paris addr = round_hint_to_min(addr); 9677cd94146SEric Paris 9681da177e4SLinus Torvalds /* Careful about overflows.. */ 9691da177e4SLinus Torvalds len = PAGE_ALIGN(len); 9709206de95SAl Viro if (!len) 9711da177e4SLinus Torvalds return -ENOMEM; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* offset overflow? */ 9741da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 9751da177e4SLinus Torvalds return -EOVERFLOW; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /* Too many mappings? */ 9781da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 9791da177e4SLinus Torvalds return -ENOMEM; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 9821da177e4SLinus Torvalds * that it represents a valid section of the address space. 9831da177e4SLinus Torvalds */ 9841da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 9851da177e4SLinus Torvalds if (addr & ~PAGE_MASK) 9861da177e4SLinus Torvalds return addr; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 9891da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 9901da177e4SLinus Torvalds * of the memory object, so we don't do any here. 9911da177e4SLinus Torvalds */ 9921da177e4SLinus Torvalds vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) | 9931da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 9941da177e4SLinus Torvalds 995cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 9961da177e4SLinus Torvalds if (!can_do_mlock()) 9971da177e4SLinus Torvalds return -EPERM; 998ba470de4SRik van Riel 9991da177e4SLinus Torvalds /* mlock MCL_FUTURE? */ 10001da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 10011da177e4SLinus Torvalds unsigned long locked, lock_limit; 100293ea1d0aSChris Wright locked = len >> PAGE_SHIFT; 100393ea1d0aSChris Wright locked += mm->locked_vm; 100459e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK); 100593ea1d0aSChris Wright lock_limit >>= PAGE_SHIFT; 10061da177e4SLinus Torvalds if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 10071da177e4SLinus Torvalds return -EAGAIN; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 1010d3ac7f89SJosef "Jeff" Sipek inode = file ? file->f_path.dentry->d_inode : NULL; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds if (file) { 10131da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 10141da177e4SLinus Torvalds case MAP_SHARED: 10151da177e4SLinus Torvalds if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) 10161da177e4SLinus Torvalds return -EACCES; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds /* 10191da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 10201da177e4SLinus Torvalds * file.. 10211da177e4SLinus Torvalds */ 10221da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 10231da177e4SLinus Torvalds return -EACCES; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* 10261da177e4SLinus Torvalds * Make sure there are no mandatory locks on the file. 10271da177e4SLinus Torvalds */ 10281da177e4SLinus Torvalds if (locks_verify_locked(inode)) 10291da177e4SLinus Torvalds return -EAGAIN; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 10321da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 10331da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* fall through */ 10361da177e4SLinus Torvalds case MAP_PRIVATE: 10371da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 10381da177e4SLinus Torvalds return -EACCES; 1039d3ac7f89SJosef "Jeff" Sipek if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { 104080c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 104180c5606cSLinus Torvalds return -EPERM; 104280c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 104380c5606cSLinus Torvalds } 104480c5606cSLinus Torvalds 104580c5606cSLinus Torvalds if (!file->f_op || !file->f_op->mmap) 104680c5606cSLinus Torvalds return -ENODEV; 10471da177e4SLinus Torvalds break; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds default: 10501da177e4SLinus Torvalds return -EINVAL; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds } else { 10531da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 10541da177e4SLinus Torvalds case MAP_SHARED: 1055ce363942STejun Heo /* 1056ce363942STejun Heo * Ignore pgoff. 1057ce363942STejun Heo */ 1058ce363942STejun Heo pgoff = 0; 10591da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 10601da177e4SLinus Torvalds break; 10611da177e4SLinus Torvalds case MAP_PRIVATE: 10621da177e4SLinus Torvalds /* 10631da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 10641da177e4SLinus Torvalds */ 10651da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 10661da177e4SLinus Torvalds break; 10671da177e4SLinus Torvalds default: 10681da177e4SLinus Torvalds return -EINVAL; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 1072ed032189SEric Paris error = security_file_mmap(file, reqprot, prot, flags, addr, 0); 10731da177e4SLinus Torvalds if (error) 10741da177e4SLinus Torvalds return error; 10751da177e4SLinus Torvalds 10765a6fe125SMel Gorman return mmap_region(file, addr, len, flags, vm_flags, pgoff); 10770165ab44SMiklos Szeredi } 10780165ab44SMiklos Szeredi EXPORT_SYMBOL(do_mmap_pgoff); 10790165ab44SMiklos Szeredi 108066f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 108166f0dc48SHugh Dickins unsigned long, prot, unsigned long, flags, 108266f0dc48SHugh Dickins unsigned long, fd, unsigned long, pgoff) 108366f0dc48SHugh Dickins { 108466f0dc48SHugh Dickins struct file *file = NULL; 108566f0dc48SHugh Dickins unsigned long retval = -EBADF; 108666f0dc48SHugh Dickins 108766f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1088120a795dSAl Viro audit_mmap_fd(fd, flags); 108966f0dc48SHugh Dickins if (unlikely(flags & MAP_HUGETLB)) 109066f0dc48SHugh Dickins return -EINVAL; 109166f0dc48SHugh Dickins file = fget(fd); 109266f0dc48SHugh Dickins if (!file) 109366f0dc48SHugh Dickins goto out; 109466f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 109566f0dc48SHugh Dickins struct user_struct *user = NULL; 109666f0dc48SHugh Dickins /* 109766f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 109866f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 109966f0dc48SHugh Dickins * A dummy user value is used because we are not locking 110066f0dc48SHugh Dickins * memory so no accounting is necessary 110166f0dc48SHugh Dickins */ 110266f0dc48SHugh Dickins len = ALIGN(len, huge_page_size(&default_hstate)); 110366f0dc48SHugh Dickins file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, VM_NORESERVE, 110466f0dc48SHugh Dickins &user, HUGETLB_ANONHUGE_INODE); 110566f0dc48SHugh Dickins if (IS_ERR(file)) 110666f0dc48SHugh Dickins return PTR_ERR(file); 110766f0dc48SHugh Dickins } 110866f0dc48SHugh Dickins 110966f0dc48SHugh Dickins flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 111066f0dc48SHugh Dickins 111166f0dc48SHugh Dickins down_write(¤t->mm->mmap_sem); 111266f0dc48SHugh Dickins retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); 111366f0dc48SHugh Dickins up_write(¤t->mm->mmap_sem); 111466f0dc48SHugh Dickins 111566f0dc48SHugh Dickins if (file) 111666f0dc48SHugh Dickins fput(file); 111766f0dc48SHugh Dickins out: 111866f0dc48SHugh Dickins return retval; 111966f0dc48SHugh Dickins } 112066f0dc48SHugh Dickins 1121a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1122a4679373SChristoph Hellwig struct mmap_arg_struct { 1123a4679373SChristoph Hellwig unsigned long addr; 1124a4679373SChristoph Hellwig unsigned long len; 1125a4679373SChristoph Hellwig unsigned long prot; 1126a4679373SChristoph Hellwig unsigned long flags; 1127a4679373SChristoph Hellwig unsigned long fd; 1128a4679373SChristoph Hellwig unsigned long offset; 1129a4679373SChristoph Hellwig }; 1130a4679373SChristoph Hellwig 1131a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1132a4679373SChristoph Hellwig { 1133a4679373SChristoph Hellwig struct mmap_arg_struct a; 1134a4679373SChristoph Hellwig 1135a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1136a4679373SChristoph Hellwig return -EFAULT; 1137a4679373SChristoph Hellwig if (a.offset & ~PAGE_MASK) 1138a4679373SChristoph Hellwig return -EINVAL; 1139a4679373SChristoph Hellwig 1140a4679373SChristoph Hellwig return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1141a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1142a4679373SChristoph Hellwig } 1143a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1144a4679373SChristoph Hellwig 11454e950f6fSAlexey Dobriyan /* 11464e950f6fSAlexey Dobriyan * Some shared mappigns will want the pages marked read-only 11474e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 11484e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 11494e950f6fSAlexey Dobriyan * VM_SHARED bit). 11504e950f6fSAlexey Dobriyan */ 11514e950f6fSAlexey Dobriyan int vma_wants_writenotify(struct vm_area_struct *vma) 11524e950f6fSAlexey Dobriyan { 1153ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 11544e950f6fSAlexey Dobriyan 11554e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 11564e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 11574e950f6fSAlexey Dobriyan return 0; 11584e950f6fSAlexey Dobriyan 11594e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 11604e950f6fSAlexey Dobriyan if (vma->vm_ops && vma->vm_ops->page_mkwrite) 11614e950f6fSAlexey Dobriyan return 1; 11624e950f6fSAlexey Dobriyan 11634e950f6fSAlexey Dobriyan /* The open routine did something to the protections already? */ 11644e950f6fSAlexey Dobriyan if (pgprot_val(vma->vm_page_prot) != 11653ed75eb8SColy Li pgprot_val(vm_get_page_prot(vm_flags))) 11664e950f6fSAlexey Dobriyan return 0; 11674e950f6fSAlexey Dobriyan 11684e950f6fSAlexey Dobriyan /* Specialty mapping? */ 11694e950f6fSAlexey Dobriyan if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE)) 11704e950f6fSAlexey Dobriyan return 0; 11714e950f6fSAlexey Dobriyan 11724e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 11734e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 11744e950f6fSAlexey Dobriyan mapping_cap_account_dirty(vma->vm_file->f_mapping); 11754e950f6fSAlexey Dobriyan } 11764e950f6fSAlexey Dobriyan 1177fc8744adSLinus Torvalds /* 1178fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 11795a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1180fc8744adSLinus Torvalds */ 1181ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1182fc8744adSLinus Torvalds { 11835a6fe125SMel Gorman /* 11845a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 11855a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 11865a6fe125SMel Gorman */ 11875a6fe125SMel Gorman if (file && is_file_hugepages(file)) 11885a6fe125SMel Gorman return 0; 11895a6fe125SMel Gorman 1190fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1191fc8744adSLinus Torvalds } 1192fc8744adSLinus Torvalds 11930165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr, 11940165ab44SMiklos Szeredi unsigned long len, unsigned long flags, 1195ca16d140SKOSAKI Motohiro vm_flags_t vm_flags, unsigned long pgoff) 11960165ab44SMiklos Szeredi { 11970165ab44SMiklos Szeredi struct mm_struct *mm = current->mm; 11980165ab44SMiklos Szeredi struct vm_area_struct *vma, *prev; 11990165ab44SMiklos Szeredi int correct_wcount = 0; 12000165ab44SMiklos Szeredi int error; 12010165ab44SMiklos Szeredi struct rb_node **rb_link, *rb_parent; 12020165ab44SMiklos Szeredi unsigned long charged = 0; 12030165ab44SMiklos Szeredi struct inode *inode = file ? file->f_path.dentry->d_inode : NULL; 12040165ab44SMiklos Szeredi 12051da177e4SLinus Torvalds /* Clear old maps */ 12061da177e4SLinus Torvalds error = -ENOMEM; 12071da177e4SLinus Torvalds munmap_back: 12081da177e4SLinus Torvalds vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 12091da177e4SLinus Torvalds if (vma && vma->vm_start < addr + len) { 12101da177e4SLinus Torvalds if (do_munmap(mm, addr, len)) 12111da177e4SLinus Torvalds return -ENOMEM; 12121da177e4SLinus Torvalds goto munmap_back; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* Check against address space limit. */ 1216119f657cSakpm@osdl.org if (!may_expand_vm(mm, len >> PAGE_SHIFT)) 12171da177e4SLinus Torvalds return -ENOMEM; 12181da177e4SLinus Torvalds 1219fc8744adSLinus Torvalds /* 1220fc8744adSLinus Torvalds * Set 'VM_NORESERVE' if we should not account for the 12215a6fe125SMel Gorman * memory use of this mapping. 1222fc8744adSLinus Torvalds */ 12235a6fe125SMel Gorman if ((flags & MAP_NORESERVE)) { 12245a6fe125SMel Gorman /* We honor MAP_NORESERVE if allowed to overcommit */ 12255a6fe125SMel Gorman if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1226fc8744adSLinus Torvalds vm_flags |= VM_NORESERVE; 12275a6fe125SMel Gorman 12285a6fe125SMel Gorman /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 12295a6fe125SMel Gorman if (file && is_file_hugepages(file)) 1230cdfd4325SAndy Whitcroft vm_flags |= VM_NORESERVE; 12315a6fe125SMel Gorman } 1232cdfd4325SAndy Whitcroft 12331da177e4SLinus Torvalds /* 12341da177e4SLinus Torvalds * Private writable mapping: check memory availability 12351da177e4SLinus Torvalds */ 12365a6fe125SMel Gorman if (accountable_mapping(file, vm_flags)) { 12371da177e4SLinus Torvalds charged = len >> PAGE_SHIFT; 12381da177e4SLinus Torvalds if (security_vm_enough_memory(charged)) 12391da177e4SLinus Torvalds return -ENOMEM; 12401da177e4SLinus Torvalds vm_flags |= VM_ACCOUNT; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds /* 1244de33c8dbSLinus Torvalds * Can we just expand an old mapping? 12451da177e4SLinus Torvalds */ 1246de33c8dbSLinus Torvalds vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL); 1247ba470de4SRik van Riel if (vma) 12481da177e4SLinus Torvalds goto out; 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds /* 12511da177e4SLinus Torvalds * Determine the object being mapped and call the appropriate 12521da177e4SLinus Torvalds * specific mapper. the address has already been validated, but 12531da177e4SLinus Torvalds * not unmapped, but the maps are removed from the list. 12541da177e4SLinus Torvalds */ 1255c5e3b83eSPekka Enberg vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 12561da177e4SLinus Torvalds if (!vma) { 12571da177e4SLinus Torvalds error = -ENOMEM; 12581da177e4SLinus Torvalds goto unacct_error; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds vma->vm_mm = mm; 12621da177e4SLinus Torvalds vma->vm_start = addr; 12631da177e4SLinus Torvalds vma->vm_end = addr + len; 12641da177e4SLinus Torvalds vma->vm_flags = vm_flags; 12653ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vm_flags); 12661da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 12675beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 12681da177e4SLinus Torvalds 1269ce8fea7aSHugh Dickins error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */ 1270ce8fea7aSHugh Dickins 12711da177e4SLinus Torvalds if (file) { 12721da177e4SLinus Torvalds if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 12731da177e4SLinus Torvalds goto free_vma; 12741da177e4SLinus Torvalds if (vm_flags & VM_DENYWRITE) { 12751da177e4SLinus Torvalds error = deny_write_access(file); 12761da177e4SLinus Torvalds if (error) 12771da177e4SLinus Torvalds goto free_vma; 12781da177e4SLinus Torvalds correct_wcount = 1; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds vma->vm_file = file; 12811da177e4SLinus Torvalds get_file(file); 12821da177e4SLinus Torvalds error = file->f_op->mmap(file, vma); 12831da177e4SLinus Torvalds if (error) 12841da177e4SLinus Torvalds goto unmap_and_free_vma; 1285925d1c40SMatt Helsley if (vm_flags & VM_EXECUTABLE) 1286925d1c40SMatt Helsley added_exe_file_vma(mm); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /* Can addr have changed?? 12891da177e4SLinus Torvalds * 12901da177e4SLinus Torvalds * Answer: Yes, several device drivers can do it in their 12911da177e4SLinus Torvalds * f_op->mmap method. -DaveM 12921da177e4SLinus Torvalds */ 12931da177e4SLinus Torvalds addr = vma->vm_start; 12941da177e4SLinus Torvalds pgoff = vma->vm_pgoff; 12951da177e4SLinus Torvalds vm_flags = vma->vm_flags; 1296f8dbf0a7SHuang Shijie } else if (vm_flags & VM_SHARED) { 1297835ee797SAl Viro if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP))) 1298835ee797SAl Viro goto free_vma; 1299f8dbf0a7SHuang Shijie error = shmem_zero_setup(vma); 1300f8dbf0a7SHuang Shijie if (error) 1301f8dbf0a7SHuang Shijie goto free_vma; 1302f8dbf0a7SHuang Shijie } 13031da177e4SLinus Torvalds 1304c9d0bf24SMagnus Damm if (vma_wants_writenotify(vma)) { 1305c9d0bf24SMagnus Damm pgprot_t pprot = vma->vm_page_prot; 1306c9d0bf24SMagnus Damm 1307c9d0bf24SMagnus Damm /* Can vma->vm_page_prot have changed?? 1308c9d0bf24SMagnus Damm * 1309c9d0bf24SMagnus Damm * Answer: Yes, drivers may have changed it in their 1310c9d0bf24SMagnus Damm * f_op->mmap method. 1311c9d0bf24SMagnus Damm * 1312c9d0bf24SMagnus Damm * Ensures that vmas marked as uncached stay that way. 1313c9d0bf24SMagnus Damm */ 13141ddd439eSHugh Dickins vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED); 1315c9d0bf24SMagnus Damm if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot))) 1316c9d0bf24SMagnus Damm vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1317c9d0bf24SMagnus Damm } 1318d08b3851SPeter Zijlstra 13194d3d5b41SOleg Nesterov vma_link(mm, vma, prev, rb_link, rb_parent); 13204d3d5b41SOleg Nesterov file = vma->vm_file; 13214d3d5b41SOleg Nesterov 13224d3d5b41SOleg Nesterov /* Once vma denies write, undo our temporary denial count */ 13234d3d5b41SOleg Nesterov if (correct_wcount) 13244d3d5b41SOleg Nesterov atomic_inc(&inode->i_writecount); 13251da177e4SLinus Torvalds out: 1326cdd6c482SIngo Molnar perf_event_mmap(vma); 13270a4a9391SPeter Zijlstra 13281da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 1329ab50b8edSHugh Dickins vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT); 13301da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 133106f9d8c2SKOSAKI Motohiro if (!mlock_vma_pages_range(vma, addr, addr + len)) 133206f9d8c2SKOSAKI Motohiro mm->locked_vm += (len >> PAGE_SHIFT); 1333ba470de4SRik van Riel } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK)) 133454cb8821SNick Piggin make_pages_present(addr, addr + len); 13351da177e4SLinus Torvalds return addr; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds unmap_and_free_vma: 13381da177e4SLinus Torvalds if (correct_wcount) 13391da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 13401da177e4SLinus Torvalds vma->vm_file = NULL; 13411da177e4SLinus Torvalds fput(file); 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /* Undo any partial mapping done by a device driver. */ 1344e0da382cSHugh Dickins unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); 1345e0da382cSHugh Dickins charged = 0; 13461da177e4SLinus Torvalds free_vma: 13471da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 13481da177e4SLinus Torvalds unacct_error: 13491da177e4SLinus Torvalds if (charged) 13501da177e4SLinus Torvalds vm_unacct_memory(charged); 13511da177e4SLinus Torvalds return error; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 13551da177e4SLinus Torvalds * For shmat() with addr=0. 13561da177e4SLinus Torvalds * 13571da177e4SLinus Torvalds * Ugly calling convention alert: 13581da177e4SLinus Torvalds * Return value with the low bits set means error value, 13591da177e4SLinus Torvalds * ie 13601da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 13611da177e4SLinus Torvalds * error = ret; 13621da177e4SLinus Torvalds * 13631da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 13641da177e4SLinus Torvalds */ 13651da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA 13661da177e4SLinus Torvalds unsigned long 13671da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr, 13681da177e4SLinus Torvalds unsigned long len, unsigned long pgoff, unsigned long flags) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 13711da177e4SLinus Torvalds struct vm_area_struct *vma; 13721da177e4SLinus Torvalds unsigned long start_addr; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds if (len > TASK_SIZE) 13751da177e4SLinus Torvalds return -ENOMEM; 13761da177e4SLinus Torvalds 137706abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 137806abdfb4SBenjamin Herrenschmidt return addr; 137906abdfb4SBenjamin Herrenschmidt 13801da177e4SLinus Torvalds if (addr) { 13811da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 13821da177e4SLinus Torvalds vma = find_vma(mm, addr); 13831da177e4SLinus Torvalds if (TASK_SIZE - len >= addr && 13841da177e4SLinus Torvalds (!vma || addr + len <= vma->vm_start)) 13851da177e4SLinus Torvalds return addr; 13861da177e4SLinus Torvalds } 13871363c3cdSWolfgang Wander if (len > mm->cached_hole_size) { 13881da177e4SLinus Torvalds start_addr = addr = mm->free_area_cache; 13891363c3cdSWolfgang Wander } else { 13901363c3cdSWolfgang Wander start_addr = addr = TASK_UNMAPPED_BASE; 13911363c3cdSWolfgang Wander mm->cached_hole_size = 0; 13921363c3cdSWolfgang Wander } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds full_search: 13951da177e4SLinus Torvalds for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { 13961da177e4SLinus Torvalds /* At this point: (!vma || addr < vma->vm_end). */ 13971da177e4SLinus Torvalds if (TASK_SIZE - len < addr) { 13981da177e4SLinus Torvalds /* 13991da177e4SLinus Torvalds * Start a new search - just in case we missed 14001da177e4SLinus Torvalds * some holes. 14011da177e4SLinus Torvalds */ 14021da177e4SLinus Torvalds if (start_addr != TASK_UNMAPPED_BASE) { 14031363c3cdSWolfgang Wander addr = TASK_UNMAPPED_BASE; 14041363c3cdSWolfgang Wander start_addr = addr; 14051363c3cdSWolfgang Wander mm->cached_hole_size = 0; 14061da177e4SLinus Torvalds goto full_search; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds return -ENOMEM; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds if (!vma || addr + len <= vma->vm_start) { 14111da177e4SLinus Torvalds /* 14121da177e4SLinus Torvalds * Remember the place where we stopped the search: 14131da177e4SLinus Torvalds */ 14141da177e4SLinus Torvalds mm->free_area_cache = addr + len; 14151da177e4SLinus Torvalds return addr; 14161da177e4SLinus Torvalds } 14171363c3cdSWolfgang Wander if (addr + mm->cached_hole_size < vma->vm_start) 14181363c3cdSWolfgang Wander mm->cached_hole_size = vma->vm_start - addr; 14191da177e4SLinus Torvalds addr = vma->vm_end; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds #endif 14231da177e4SLinus Torvalds 14241363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr) 14251da177e4SLinus Torvalds { 14261da177e4SLinus Torvalds /* 14271da177e4SLinus Torvalds * Is this a new hole at the lowest possible address? 14281da177e4SLinus Torvalds */ 14291363c3cdSWolfgang Wander if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) { 14301363c3cdSWolfgang Wander mm->free_area_cache = addr; 14311363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 14321363c3cdSWolfgang Wander } 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds /* 14361da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 14371da177e4SLinus Torvalds * stack's low limit (the base): 14381da177e4SLinus Torvalds */ 14391da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 14401da177e4SLinus Torvalds unsigned long 14411da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, 14421da177e4SLinus Torvalds const unsigned long len, const unsigned long pgoff, 14431da177e4SLinus Torvalds const unsigned long flags) 14441da177e4SLinus Torvalds { 14451da177e4SLinus Torvalds struct vm_area_struct *vma; 14461da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 14471da177e4SLinus Torvalds unsigned long addr = addr0; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* requested length too big for entire address space */ 14501da177e4SLinus Torvalds if (len > TASK_SIZE) 14511da177e4SLinus Torvalds return -ENOMEM; 14521da177e4SLinus Torvalds 145306abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 145406abdfb4SBenjamin Herrenschmidt return addr; 145506abdfb4SBenjamin Herrenschmidt 14561da177e4SLinus Torvalds /* requesting a specific address */ 14571da177e4SLinus Torvalds if (addr) { 14581da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 14591da177e4SLinus Torvalds vma = find_vma(mm, addr); 14601da177e4SLinus Torvalds if (TASK_SIZE - len >= addr && 14611da177e4SLinus Torvalds (!vma || addr + len <= vma->vm_start)) 14621da177e4SLinus Torvalds return addr; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 14651363c3cdSWolfgang Wander /* check if free_area_cache is useful for us */ 14661363c3cdSWolfgang Wander if (len <= mm->cached_hole_size) { 14671363c3cdSWolfgang Wander mm->cached_hole_size = 0; 14681363c3cdSWolfgang Wander mm->free_area_cache = mm->mmap_base; 14691363c3cdSWolfgang Wander } 14701363c3cdSWolfgang Wander 14711da177e4SLinus Torvalds /* either no address requested or can't fit in requested address hole */ 14721da177e4SLinus Torvalds addr = mm->free_area_cache; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds /* make sure it can fit in the remaining address space */ 147549a43876SLinus Torvalds if (addr > len) { 14761da177e4SLinus Torvalds vma = find_vma(mm, addr-len); 14771da177e4SLinus Torvalds if (!vma || addr <= vma->vm_start) 14781da177e4SLinus Torvalds /* remember the address as a hint for next time */ 14791da177e4SLinus Torvalds return (mm->free_area_cache = addr-len); 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 148273219d17SChris Wright if (mm->mmap_base < len) 148373219d17SChris Wright goto bottomup; 148473219d17SChris Wright 14851da177e4SLinus Torvalds addr = mm->mmap_base-len; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds do { 14881da177e4SLinus Torvalds /* 14891da177e4SLinus Torvalds * Lookup failure means no vma is above this address, 14901da177e4SLinus Torvalds * else if new region fits below vma->vm_start, 14911da177e4SLinus Torvalds * return with success: 14921da177e4SLinus Torvalds */ 14931da177e4SLinus Torvalds vma = find_vma(mm, addr); 14941da177e4SLinus Torvalds if (!vma || addr+len <= vma->vm_start) 14951da177e4SLinus Torvalds /* remember the address as a hint for next time */ 14961da177e4SLinus Torvalds return (mm->free_area_cache = addr); 14971da177e4SLinus Torvalds 14981363c3cdSWolfgang Wander /* remember the largest hole we saw so far */ 14991363c3cdSWolfgang Wander if (addr + mm->cached_hole_size < vma->vm_start) 15001363c3cdSWolfgang Wander mm->cached_hole_size = vma->vm_start - addr; 15011363c3cdSWolfgang Wander 15021da177e4SLinus Torvalds /* try just below the current vma->vm_start */ 15031da177e4SLinus Torvalds addr = vma->vm_start-len; 150449a43876SLinus Torvalds } while (len < vma->vm_start); 15051da177e4SLinus Torvalds 150673219d17SChris Wright bottomup: 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 15091da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 15101da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 15111da177e4SLinus Torvalds * allocations. 15121da177e4SLinus Torvalds */ 15131363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 15141da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 15151da177e4SLinus Torvalds addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); 15161da177e4SLinus Torvalds /* 15171da177e4SLinus Torvalds * Restore the topdown base: 15181da177e4SLinus Torvalds */ 15191da177e4SLinus Torvalds mm->free_area_cache = mm->mmap_base; 15201363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds return addr; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds #endif 15251da177e4SLinus Torvalds 15261363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr) 15271da177e4SLinus Torvalds { 15281da177e4SLinus Torvalds /* 15291da177e4SLinus Torvalds * Is this a new hole at the highest possible address? 15301da177e4SLinus Torvalds */ 15311363c3cdSWolfgang Wander if (addr > mm->free_area_cache) 15321363c3cdSWolfgang Wander mm->free_area_cache = addr; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds /* dont allow allocations above current base */ 15351363c3cdSWolfgang Wander if (mm->free_area_cache > mm->mmap_base) 15361363c3cdSWolfgang Wander mm->free_area_cache = mm->mmap_base; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds unsigned long 15401da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 15411da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 15421da177e4SLinus Torvalds { 154306abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 154406abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 154507ab67c8SLinus Torvalds 15469206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 15479206de95SAl Viro if (error) 15489206de95SAl Viro return error; 15499206de95SAl Viro 15509206de95SAl Viro /* Careful about overflows.. */ 15519206de95SAl Viro if (len > TASK_SIZE) 15529206de95SAl Viro return -ENOMEM; 15539206de95SAl Viro 155407ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 155507ab67c8SLinus Torvalds if (file && file->f_op && file->f_op->get_unmapped_area) 155607ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 155707ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 155807ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 155907ab67c8SLinus Torvalds return addr; 156007ab67c8SLinus Torvalds 15611da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 15621da177e4SLinus Torvalds return -ENOMEM; 15631da177e4SLinus Torvalds if (addr & ~PAGE_MASK) 15641da177e4SLinus Torvalds return -EINVAL; 156506abdfb4SBenjamin Herrenschmidt 156608e7d9b5SMartin Schwidefsky return arch_rebalance_pgtables(addr, len); 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ 15721da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds struct vm_area_struct *vma = NULL; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds if (mm) { 15771da177e4SLinus Torvalds /* Check the cache first. */ 15781da177e4SLinus Torvalds /* (Cache hit rate is typically around 35%.) */ 15791da177e4SLinus Torvalds vma = mm->mmap_cache; 15801da177e4SLinus Torvalds if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) { 15811da177e4SLinus Torvalds struct rb_node * rb_node; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds rb_node = mm->mm_rb.rb_node; 15841da177e4SLinus Torvalds vma = NULL; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds while (rb_node) { 15871da177e4SLinus Torvalds struct vm_area_struct * vma_tmp; 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds vma_tmp = rb_entry(rb_node, 15901da177e4SLinus Torvalds struct vm_area_struct, vm_rb); 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds if (vma_tmp->vm_end > addr) { 15931da177e4SLinus Torvalds vma = vma_tmp; 15941da177e4SLinus Torvalds if (vma_tmp->vm_start <= addr) 15951da177e4SLinus Torvalds break; 15961da177e4SLinus Torvalds rb_node = rb_node->rb_left; 15971da177e4SLinus Torvalds } else 15981da177e4SLinus Torvalds rb_node = rb_node->rb_right; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds if (vma) 16011da177e4SLinus Torvalds mm->mmap_cache = vma; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds return vma; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 16081da177e4SLinus Torvalds 16096bd4837dSKOSAKI Motohiro /* 16106bd4837dSKOSAKI Motohiro * Same as find_vma, but also return a pointer to the previous VMA in *pprev. 16116bd4837dSKOSAKI Motohiro */ 16121da177e4SLinus Torvalds struct vm_area_struct * 16131da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 16141da177e4SLinus Torvalds struct vm_area_struct **pprev) 16151da177e4SLinus Torvalds { 16166bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 16171da177e4SLinus Torvalds 16186bd4837dSKOSAKI Motohiro vma = find_vma(mm, addr); 1619*83cd904dSMikulas Patocka if (vma) { 1620*83cd904dSMikulas Patocka *pprev = vma->vm_prev; 1621*83cd904dSMikulas Patocka } else { 1622*83cd904dSMikulas Patocka struct rb_node *rb_node = mm->mm_rb.rb_node; 1623*83cd904dSMikulas Patocka *pprev = NULL; 1624*83cd904dSMikulas Patocka while (rb_node) { 1625*83cd904dSMikulas Patocka *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb); 1626*83cd904dSMikulas Patocka rb_node = rb_node->rb_right; 1627*83cd904dSMikulas Patocka } 1628*83cd904dSMikulas Patocka } 16296bd4837dSKOSAKI Motohiro return vma; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds /* 16331da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 16341da177e4SLinus Torvalds * update accounting. This is shared with both the 16351da177e4SLinus Torvalds * grow-up and grow-down cases. 16361da177e4SLinus Torvalds */ 16371da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 16401da177e4SLinus Torvalds struct rlimit *rlim = current->signal->rlim; 16410d59a01bSAdam Litke unsigned long new_start; 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds /* address space limit tests */ 1644119f657cSakpm@osdl.org if (!may_expand_vm(mm, grow)) 16451da177e4SLinus Torvalds return -ENOMEM; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds /* Stack limit test */ 164859e99e5bSJiri Slaby if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur)) 16491da177e4SLinus Torvalds return -ENOMEM; 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds /* mlock limit tests */ 16521da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) { 16531da177e4SLinus Torvalds unsigned long locked; 16541da177e4SLinus Torvalds unsigned long limit; 16551da177e4SLinus Torvalds locked = mm->locked_vm + grow; 165659e99e5bSJiri Slaby limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur); 165759e99e5bSJiri Slaby limit >>= PAGE_SHIFT; 16581da177e4SLinus Torvalds if (locked > limit && !capable(CAP_IPC_LOCK)) 16591da177e4SLinus Torvalds return -ENOMEM; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16620d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 16630d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 16640d59a01bSAdam Litke vma->vm_end - size; 16650d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 16660d59a01bSAdam Litke return -EFAULT; 16670d59a01bSAdam Litke 16681da177e4SLinus Torvalds /* 16691da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 16701da177e4SLinus Torvalds * update security statistics. 16711da177e4SLinus Torvalds */ 167205fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 16731da177e4SLinus Torvalds return -ENOMEM; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds /* Ok, everything looks good - let it rip */ 16761da177e4SLinus Torvalds mm->total_vm += grow; 16771da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) 16781da177e4SLinus Torvalds mm->locked_vm += grow; 1679ab50b8edSHugh Dickins vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow); 16801da177e4SLinus Torvalds return 0; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 168346dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 16841da177e4SLinus Torvalds /* 168546dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 168646dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 16871da177e4SLinus Torvalds */ 168846dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds int error; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 16931da177e4SLinus Torvalds return -EFAULT; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* 16961da177e4SLinus Torvalds * We must make sure the anon_vma is allocated 16971da177e4SLinus Torvalds * so that the anon_vma locking is not a noop. 16981da177e4SLinus Torvalds */ 16991da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 17001da177e4SLinus Torvalds return -ENOMEM; 1701bb4a340eSRik van Riel vma_lock_anon_vma(vma); 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds /* 17041da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 17051da177e4SLinus Torvalds * is required to hold the mmap_sem in read mode. We need the 17061da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 170706b32f3aSHelge Deller * Also guard against wrapping around to address 0. 17081da177e4SLinus Torvalds */ 170906b32f3aSHelge Deller if (address < PAGE_ALIGN(address+4)) 171006b32f3aSHelge Deller address = PAGE_ALIGN(address+4); 171106b32f3aSHelge Deller else { 1712bb4a340eSRik van Riel vma_unlock_anon_vma(vma); 171306b32f3aSHelge Deller return -ENOMEM; 171406b32f3aSHelge Deller } 17151da177e4SLinus Torvalds error = 0; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 17181da177e4SLinus Torvalds if (address > vma->vm_end) { 17191da177e4SLinus Torvalds unsigned long size, grow; 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds size = address - vma->vm_start; 17221da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 17231da177e4SLinus Torvalds 172442c36f63SHugh Dickins error = -ENOMEM; 172542c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 17261da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 17273af9e859SEric B Munson if (!error) { 17281da177e4SLinus Torvalds vma->vm_end = address; 17293af9e859SEric B Munson perf_event_mmap(vma); 17303af9e859SEric B Munson } 17311da177e4SLinus Torvalds } 173242c36f63SHugh Dickins } 1733bb4a340eSRik van Riel vma_unlock_anon_vma(vma); 1734b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(vma); 17351da177e4SLinus Torvalds return error; 17361da177e4SLinus Torvalds } 173746dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 173846dea3d0SHugh Dickins 17391da177e4SLinus Torvalds /* 17401da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 17411da177e4SLinus Torvalds */ 1742d05f3169SMichal Hocko int expand_downwards(struct vm_area_struct *vma, 1743b6a2fea3SOllie Wild unsigned long address) 17441da177e4SLinus Torvalds { 17451da177e4SLinus Torvalds int error; 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds /* 17481da177e4SLinus Torvalds * We must make sure the anon_vma is allocated 17491da177e4SLinus Torvalds * so that the anon_vma locking is not a noop. 17501da177e4SLinus Torvalds */ 17511da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 17521da177e4SLinus Torvalds return -ENOMEM; 17538869477aSEric Paris 17548869477aSEric Paris address &= PAGE_MASK; 175588c3f7a8SRichard Knutsson error = security_file_mmap(NULL, 0, 0, 0, address, 1); 17568869477aSEric Paris if (error) 17578869477aSEric Paris return error; 17588869477aSEric Paris 1759bb4a340eSRik van Riel vma_lock_anon_vma(vma); 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* 17621da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 17631da177e4SLinus Torvalds * is required to hold the mmap_sem in read mode. We need the 17641da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 17651da177e4SLinus Torvalds */ 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 17681da177e4SLinus Torvalds if (address < vma->vm_start) { 17691da177e4SLinus Torvalds unsigned long size, grow; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds size = vma->vm_end - address; 17721da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 17731da177e4SLinus Torvalds 1774a626ca6aSLinus Torvalds error = -ENOMEM; 1775a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 17761da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 17771da177e4SLinus Torvalds if (!error) { 17781da177e4SLinus Torvalds vma->vm_start = address; 17791da177e4SLinus Torvalds vma->vm_pgoff -= grow; 17803af9e859SEric B Munson perf_event_mmap(vma); 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds } 1783a626ca6aSLinus Torvalds } 1784bb4a340eSRik van Riel vma_unlock_anon_vma(vma); 1785b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(vma); 17861da177e4SLinus Torvalds return error; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 1789b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 1790b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 1791b6a2fea3SOllie Wild { 1792b6a2fea3SOllie Wild return expand_upwards(vma, address); 1793b6a2fea3SOllie Wild } 1794b6a2fea3SOllie Wild 1795b6a2fea3SOllie Wild struct vm_area_struct * 1796b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 1797b6a2fea3SOllie Wild { 1798b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 1799b6a2fea3SOllie Wild 1800b6a2fea3SOllie Wild addr &= PAGE_MASK; 1801b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 1802b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 1803b6a2fea3SOllie Wild return vma; 18041c127185SDenys Vlasenko if (!prev || expand_stack(prev, addr)) 1805b6a2fea3SOllie Wild return NULL; 1806ba470de4SRik van Riel if (prev->vm_flags & VM_LOCKED) { 1807c58267c3SKOSAKI Motohiro mlock_vma_pages_range(prev, addr, prev->vm_end); 1808ba470de4SRik van Riel } 1809b6a2fea3SOllie Wild return prev; 1810b6a2fea3SOllie Wild } 1811b6a2fea3SOllie Wild #else 1812b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 1813b6a2fea3SOllie Wild { 1814b6a2fea3SOllie Wild return expand_downwards(vma, address); 1815b6a2fea3SOllie Wild } 1816b6a2fea3SOllie Wild 18171da177e4SLinus Torvalds struct vm_area_struct * 18181da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr) 18191da177e4SLinus Torvalds { 18201da177e4SLinus Torvalds struct vm_area_struct * vma; 18211da177e4SLinus Torvalds unsigned long start; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds addr &= PAGE_MASK; 18241da177e4SLinus Torvalds vma = find_vma(mm,addr); 18251da177e4SLinus Torvalds if (!vma) 18261da177e4SLinus Torvalds return NULL; 18271da177e4SLinus Torvalds if (vma->vm_start <= addr) 18281da177e4SLinus Torvalds return vma; 18291da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 18301da177e4SLinus Torvalds return NULL; 18311da177e4SLinus Torvalds start = vma->vm_start; 18321da177e4SLinus Torvalds if (expand_stack(vma, addr)) 18331da177e4SLinus Torvalds return NULL; 1834ba470de4SRik van Riel if (vma->vm_flags & VM_LOCKED) { 1835c58267c3SKOSAKI Motohiro mlock_vma_pages_range(vma, addr, start); 1836ba470de4SRik van Riel } 18371da177e4SLinus Torvalds return vma; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds #endif 18401da177e4SLinus Torvalds 18412c0b3814SHugh Dickins /* 18422c0b3814SHugh Dickins * Ok - we have the memory areas we should free on the vma list, 18432c0b3814SHugh Dickins * so release them, and do the vma updates. 18441da177e4SLinus Torvalds * 18452c0b3814SHugh Dickins * Called with the mm semaphore held. 18461da177e4SLinus Torvalds */ 18472c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) 18481da177e4SLinus Torvalds { 1849365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 1850365e9c87SHugh Dickins update_hiwater_vm(mm); 18512c0b3814SHugh Dickins do { 1852ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 18531da177e4SLinus Torvalds 1854ab50b8edSHugh Dickins mm->total_vm -= nrpages; 1855ab50b8edSHugh Dickins vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages); 1856a8fb5618SHugh Dickins vma = remove_vma(vma); 1857146425a3SHugh Dickins } while (vma); 18581da177e4SLinus Torvalds validate_mm(mm); 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds /* 18621da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 18631da177e4SLinus Torvalds * 1864f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 18651da177e4SLinus Torvalds */ 18661da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm, 1867e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 1868e0da382cSHugh Dickins unsigned long start, unsigned long end) 18691da177e4SLinus Torvalds { 1870e0da382cSHugh Dickins struct vm_area_struct *next = prev? prev->vm_next: mm->mmap; 1871d16dfc55SPeter Zijlstra struct mmu_gather tlb; 18721da177e4SLinus Torvalds unsigned long nr_accounted = 0; 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds lru_add_drain(); 1875d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 0); 1876365e9c87SHugh Dickins update_hiwater_rss(mm); 1877508034a3SHugh Dickins unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL); 18781da177e4SLinus Torvalds vm_unacct_memory(nr_accounted); 1879d16dfc55SPeter Zijlstra free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 1880e0da382cSHugh Dickins next ? next->vm_start : 0); 1881d16dfc55SPeter Zijlstra tlb_finish_mmu(&tlb, start, end); 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds /* 18851da177e4SLinus Torvalds * Create a list of vma's touched by the unmap, removing them from the mm's 18861da177e4SLinus Torvalds * vma list as we go.. 18871da177e4SLinus Torvalds */ 18881da177e4SLinus Torvalds static void 18891da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, 18901da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long end) 18911da177e4SLinus Torvalds { 18921da177e4SLinus Torvalds struct vm_area_struct **insertion_point; 18931da177e4SLinus Torvalds struct vm_area_struct *tail_vma = NULL; 18941363c3cdSWolfgang Wander unsigned long addr; 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds insertion_point = (prev ? &prev->vm_next : &mm->mmap); 1897297c5eeeSLinus Torvalds vma->vm_prev = NULL; 18981da177e4SLinus Torvalds do { 18991da177e4SLinus Torvalds rb_erase(&vma->vm_rb, &mm->mm_rb); 19001da177e4SLinus Torvalds mm->map_count--; 19011da177e4SLinus Torvalds tail_vma = vma; 19021da177e4SLinus Torvalds vma = vma->vm_next; 19031da177e4SLinus Torvalds } while (vma && vma->vm_start < end); 19041da177e4SLinus Torvalds *insertion_point = vma; 1905297c5eeeSLinus Torvalds if (vma) 1906297c5eeeSLinus Torvalds vma->vm_prev = prev; 19071da177e4SLinus Torvalds tail_vma->vm_next = NULL; 19081363c3cdSWolfgang Wander if (mm->unmap_area == arch_unmap_area) 19091363c3cdSWolfgang Wander addr = prev ? prev->vm_end : mm->mmap_base; 19101363c3cdSWolfgang Wander else 19111363c3cdSWolfgang Wander addr = vma ? vma->vm_start : mm->mmap_base; 19121363c3cdSWolfgang Wander mm->unmap_area(mm, addr); 19131da177e4SLinus Torvalds mm->mmap_cache = NULL; /* Kill the cache. */ 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds /* 1917659ace58SKOSAKI Motohiro * __split_vma() bypasses sysctl_max_map_count checking. We use this on the 1918659ace58SKOSAKI Motohiro * munmap path where it doesn't make sense to fail. 19191da177e4SLinus Torvalds */ 1920659ace58SKOSAKI Motohiro static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma, 19211da177e4SLinus Torvalds unsigned long addr, int new_below) 19221da177e4SLinus Torvalds { 19231da177e4SLinus Torvalds struct mempolicy *pol; 19241da177e4SLinus Torvalds struct vm_area_struct *new; 19255beb4930SRik van Riel int err = -ENOMEM; 19261da177e4SLinus Torvalds 1927a5516438SAndi Kleen if (is_vm_hugetlb_page(vma) && (addr & 1928a5516438SAndi Kleen ~(huge_page_mask(hstate_vma(vma))))) 19291da177e4SLinus Torvalds return -EINVAL; 19301da177e4SLinus Torvalds 1931e94b1766SChristoph Lameter new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 19321da177e4SLinus Torvalds if (!new) 19335beb4930SRik van Riel goto out_err; 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* most fields are the same, copy all, and then fixup */ 19361da177e4SLinus Torvalds *new = *vma; 19371da177e4SLinus Torvalds 19385beb4930SRik van Riel INIT_LIST_HEAD(&new->anon_vma_chain); 19395beb4930SRik van Riel 19401da177e4SLinus Torvalds if (new_below) 19411da177e4SLinus Torvalds new->vm_end = addr; 19421da177e4SLinus Torvalds else { 19431da177e4SLinus Torvalds new->vm_start = addr; 19441da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 1947846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(vma)); 19481da177e4SLinus Torvalds if (IS_ERR(pol)) { 19495beb4930SRik van Riel err = PTR_ERR(pol); 19505beb4930SRik van Riel goto out_free_vma; 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds vma_set_policy(new, pol); 19531da177e4SLinus Torvalds 19545beb4930SRik van Riel if (anon_vma_clone(new, vma)) 19555beb4930SRik van Riel goto out_free_mpol; 19565beb4930SRik van Riel 1957925d1c40SMatt Helsley if (new->vm_file) { 19581da177e4SLinus Torvalds get_file(new->vm_file); 1959925d1c40SMatt Helsley if (vma->vm_flags & VM_EXECUTABLE) 1960925d1c40SMatt Helsley added_exe_file_vma(mm); 1961925d1c40SMatt Helsley } 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 19641da177e4SLinus Torvalds new->vm_ops->open(new); 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds if (new_below) 19675beb4930SRik van Riel err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + 19681da177e4SLinus Torvalds ((addr - new->vm_start) >> PAGE_SHIFT), new); 19691da177e4SLinus Torvalds else 19705beb4930SRik van Riel err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); 19711da177e4SLinus Torvalds 19725beb4930SRik van Riel /* Success. */ 19735beb4930SRik van Riel if (!err) 19741da177e4SLinus Torvalds return 0; 19755beb4930SRik van Riel 19765beb4930SRik van Riel /* Clean everything up if vma_adjust failed. */ 197758927533SRik van Riel if (new->vm_ops && new->vm_ops->close) 19785beb4930SRik van Riel new->vm_ops->close(new); 19795beb4930SRik van Riel if (new->vm_file) { 19805beb4930SRik van Riel if (vma->vm_flags & VM_EXECUTABLE) 19815beb4930SRik van Riel removed_exe_file_vma(mm); 19825beb4930SRik van Riel fput(new->vm_file); 19835beb4930SRik van Riel } 19842aeadc30SAndrea Arcangeli unlink_anon_vmas(new); 19855beb4930SRik van Riel out_free_mpol: 19865beb4930SRik van Riel mpol_put(pol); 19875beb4930SRik van Riel out_free_vma: 19885beb4930SRik van Riel kmem_cache_free(vm_area_cachep, new); 19895beb4930SRik van Riel out_err: 19905beb4930SRik van Riel return err; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 1993659ace58SKOSAKI Motohiro /* 1994659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 1995659ace58SKOSAKI Motohiro * either for the first part or the tail. 1996659ace58SKOSAKI Motohiro */ 1997659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 1998659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 1999659ace58SKOSAKI Motohiro { 2000659ace58SKOSAKI Motohiro if (mm->map_count >= sysctl_max_map_count) 2001659ace58SKOSAKI Motohiro return -ENOMEM; 2002659ace58SKOSAKI Motohiro 2003659ace58SKOSAKI Motohiro return __split_vma(mm, vma, addr, new_below); 2004659ace58SKOSAKI Motohiro } 2005659ace58SKOSAKI Motohiro 20061da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds 20071da177e4SLinus Torvalds * what needs doing, and the areas themselves, which do the 20081da177e4SLinus Torvalds * work. This now handles partial unmappings. 20091da177e4SLinus Torvalds * Jeremy Fitzhardinge <jeremy@goop.org> 20101da177e4SLinus Torvalds */ 20111da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 20121da177e4SLinus Torvalds { 20131da177e4SLinus Torvalds unsigned long end; 2014146425a3SHugh Dickins struct vm_area_struct *vma, *prev, *last; 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start) 20171da177e4SLinus Torvalds return -EINVAL; 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds if ((len = PAGE_ALIGN(len)) == 0) 20201da177e4SLinus Torvalds return -EINVAL; 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds /* Find the first overlapping VMA */ 20239be34c9dSLinus Torvalds vma = find_vma(mm, start); 2024146425a3SHugh Dickins if (!vma) 20251da177e4SLinus Torvalds return 0; 20269be34c9dSLinus Torvalds prev = vma->vm_prev; 2027146425a3SHugh Dickins /* we have start < vma->vm_end */ 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds /* if it doesn't overlap, we have nothing.. */ 20301da177e4SLinus Torvalds end = start + len; 2031146425a3SHugh Dickins if (vma->vm_start >= end) 20321da177e4SLinus Torvalds return 0; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds /* 20351da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 20361da177e4SLinus Torvalds * 20371da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 20381da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 20391da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 20401da177e4SLinus Torvalds */ 2041146425a3SHugh Dickins if (start > vma->vm_start) { 2042659ace58SKOSAKI Motohiro int error; 2043659ace58SKOSAKI Motohiro 2044659ace58SKOSAKI Motohiro /* 2045659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2046659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2047659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2048659ace58SKOSAKI Motohiro */ 2049659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2050659ace58SKOSAKI Motohiro return -ENOMEM; 2051659ace58SKOSAKI Motohiro 2052659ace58SKOSAKI Motohiro error = __split_vma(mm, vma, start, 0); 20531da177e4SLinus Torvalds if (error) 20541da177e4SLinus Torvalds return error; 2055146425a3SHugh Dickins prev = vma; 20561da177e4SLinus Torvalds } 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds /* Does it split the last one? */ 20591da177e4SLinus Torvalds last = find_vma(mm, end); 20601da177e4SLinus Torvalds if (last && end > last->vm_start) { 2061659ace58SKOSAKI Motohiro int error = __split_vma(mm, last, end, 1); 20621da177e4SLinus Torvalds if (error) 20631da177e4SLinus Torvalds return error; 20641da177e4SLinus Torvalds } 2065146425a3SHugh Dickins vma = prev? prev->vm_next: mm->mmap; 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds /* 2068ba470de4SRik van Riel * unlock any mlock()ed ranges before detaching vmas 2069ba470de4SRik van Riel */ 2070ba470de4SRik van Riel if (mm->locked_vm) { 2071ba470de4SRik van Riel struct vm_area_struct *tmp = vma; 2072ba470de4SRik van Riel while (tmp && tmp->vm_start < end) { 2073ba470de4SRik van Riel if (tmp->vm_flags & VM_LOCKED) { 2074ba470de4SRik van Riel mm->locked_vm -= vma_pages(tmp); 2075ba470de4SRik van Riel munlock_vma_pages_all(tmp); 2076ba470de4SRik van Riel } 2077ba470de4SRik van Riel tmp = tmp->vm_next; 2078ba470de4SRik van Riel } 2079ba470de4SRik van Riel } 2080ba470de4SRik van Riel 2081ba470de4SRik van Riel /* 20821da177e4SLinus Torvalds * Remove the vma's, and unmap the actual pages 20831da177e4SLinus Torvalds */ 2084146425a3SHugh Dickins detach_vmas_to_be_unmapped(mm, vma, prev, end); 2085146425a3SHugh Dickins unmap_region(mm, vma, prev, start, end); 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds /* Fix up all other VM information */ 20882c0b3814SHugh Dickins remove_vma_list(mm, vma); 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds return 0; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds EXPORT_SYMBOL(do_munmap); 20941da177e4SLinus Torvalds 20956a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 20961da177e4SLinus Torvalds { 20971da177e4SLinus Torvalds int ret; 20981da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds profile_munmap(addr); 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds down_write(&mm->mmap_sem); 21031da177e4SLinus Torvalds ret = do_munmap(mm, addr, len); 21041da177e4SLinus Torvalds up_write(&mm->mmap_sem); 21051da177e4SLinus Torvalds return ret; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm) 21091da177e4SLinus Torvalds { 2110a241ec65SPaul E. McKenney #ifdef CONFIG_DEBUG_VM 21111da177e4SLinus Torvalds if (unlikely(down_read_trylock(&mm->mmap_sem))) { 21121da177e4SLinus Torvalds WARN_ON(1); 21131da177e4SLinus Torvalds up_read(&mm->mmap_sem); 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds #endif 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds /* 21191da177e4SLinus Torvalds * this is really a simplified "do_mmap". it only handles 21201da177e4SLinus Torvalds * anonymous maps. eventually we may be able to do some 21211da177e4SLinus Torvalds * brk-specific accounting here. 21221da177e4SLinus Torvalds */ 21231da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len) 21241da177e4SLinus Torvalds { 21251da177e4SLinus Torvalds struct mm_struct * mm = current->mm; 21261da177e4SLinus Torvalds struct vm_area_struct * vma, * prev; 21271da177e4SLinus Torvalds unsigned long flags; 21281da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 21291da177e4SLinus Torvalds pgoff_t pgoff = addr >> PAGE_SHIFT; 21303a459756SKirill Korotaev int error; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds len = PAGE_ALIGN(len); 21331da177e4SLinus Torvalds if (!len) 21341da177e4SLinus Torvalds return addr; 21351da177e4SLinus Torvalds 213688c3f7a8SRichard Knutsson error = security_file_mmap(NULL, 0, 0, 0, addr, 1); 21375a211a5dSEric Paris if (error) 21385a211a5dSEric Paris return error; 21395a211a5dSEric Paris 21403a459756SKirill Korotaev flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 21413a459756SKirill Korotaev 21422c6a1016SAl Viro error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 21432c6a1016SAl Viro if (error & ~PAGE_MASK) 21443a459756SKirill Korotaev return error; 21453a459756SKirill Korotaev 21461da177e4SLinus Torvalds /* 21471da177e4SLinus Torvalds * mlock MCL_FUTURE? 21481da177e4SLinus Torvalds */ 21491da177e4SLinus Torvalds if (mm->def_flags & VM_LOCKED) { 21501da177e4SLinus Torvalds unsigned long locked, lock_limit; 215193ea1d0aSChris Wright locked = len >> PAGE_SHIFT; 215293ea1d0aSChris Wright locked += mm->locked_vm; 215359e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK); 215493ea1d0aSChris Wright lock_limit >>= PAGE_SHIFT; 21551da177e4SLinus Torvalds if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 21561da177e4SLinus Torvalds return -EAGAIN; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds /* 21601da177e4SLinus Torvalds * mm->mmap_sem is required to protect against another thread 21611da177e4SLinus Torvalds * changing the mappings in case we sleep. 21621da177e4SLinus Torvalds */ 21631da177e4SLinus Torvalds verify_mm_writelocked(mm); 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds /* 21661da177e4SLinus Torvalds * Clear old maps. this also does some error checking for us 21671da177e4SLinus Torvalds */ 21681da177e4SLinus Torvalds munmap_back: 21691da177e4SLinus Torvalds vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 21701da177e4SLinus Torvalds if (vma && vma->vm_start < addr + len) { 21711da177e4SLinus Torvalds if (do_munmap(mm, addr, len)) 21721da177e4SLinus Torvalds return -ENOMEM; 21731da177e4SLinus Torvalds goto munmap_back; 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds /* Check against address space limits *after* clearing old maps... */ 2177119f657cSakpm@osdl.org if (!may_expand_vm(mm, len >> PAGE_SHIFT)) 21781da177e4SLinus Torvalds return -ENOMEM; 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 21811da177e4SLinus Torvalds return -ENOMEM; 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds if (security_vm_enough_memory(len >> PAGE_SHIFT)) 21841da177e4SLinus Torvalds return -ENOMEM; 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds /* Can we just expand an old private anonymous mapping? */ 2187ba470de4SRik van Riel vma = vma_merge(mm, prev, addr, addr + len, flags, 2188ba470de4SRik van Riel NULL, NULL, pgoff, NULL); 2189ba470de4SRik van Riel if (vma) 21901da177e4SLinus Torvalds goto out; 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds /* 21931da177e4SLinus Torvalds * create a vma struct for an anonymous mapping 21941da177e4SLinus Torvalds */ 2195c5e3b83eSPekka Enberg vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 21961da177e4SLinus Torvalds if (!vma) { 21971da177e4SLinus Torvalds vm_unacct_memory(len >> PAGE_SHIFT); 21981da177e4SLinus Torvalds return -ENOMEM; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 22015beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 22021da177e4SLinus Torvalds vma->vm_mm = mm; 22031da177e4SLinus Torvalds vma->vm_start = addr; 22041da177e4SLinus Torvalds vma->vm_end = addr + len; 22051da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 22061da177e4SLinus Torvalds vma->vm_flags = flags; 22073ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 22081da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 22091da177e4SLinus Torvalds out: 22103af9e859SEric B Munson perf_event_mmap(vma); 22111da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 22121da177e4SLinus Torvalds if (flags & VM_LOCKED) { 2213ba470de4SRik van Riel if (!mlock_vma_pages_range(vma, addr, addr + len)) 2214ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds return addr; 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds EXPORT_SYMBOL(do_brk); 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds /* Release all mmaps. */ 22221da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 22231da177e4SLinus Torvalds { 2224d16dfc55SPeter Zijlstra struct mmu_gather tlb; 2225ba470de4SRik van Riel struct vm_area_struct *vma; 22261da177e4SLinus Torvalds unsigned long nr_accounted = 0; 2227ee39b37bSHugh Dickins unsigned long end; 22281da177e4SLinus Torvalds 2229d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 2230cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 2231d6dd61c8SJeremy Fitzhardinge 2232ba470de4SRik van Riel if (mm->locked_vm) { 2233ba470de4SRik van Riel vma = mm->mmap; 2234ba470de4SRik van Riel while (vma) { 2235ba470de4SRik van Riel if (vma->vm_flags & VM_LOCKED) 2236ba470de4SRik van Riel munlock_vma_pages_all(vma); 2237ba470de4SRik van Riel vma = vma->vm_next; 2238ba470de4SRik van Riel } 2239ba470de4SRik van Riel } 22409480c53eSJeremy Fitzhardinge 22419480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 22429480c53eSJeremy Fitzhardinge 2243ba470de4SRik van Riel vma = mm->mmap; 22449480c53eSJeremy Fitzhardinge if (!vma) /* Can happen if dup_mmap() received an OOM */ 22459480c53eSJeremy Fitzhardinge return; 22469480c53eSJeremy Fitzhardinge 22471da177e4SLinus Torvalds lru_add_drain(); 22481da177e4SLinus Torvalds flush_cache_mm(mm); 2249d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 1); 2250901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 2251e0da382cSHugh Dickins /* Use -1 here to ensure all VMAs in the mm are unmapped */ 2252508034a3SHugh Dickins end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL); 22531da177e4SLinus Torvalds vm_unacct_memory(nr_accounted); 22549ba69294SHugh Dickins 2255d16dfc55SPeter Zijlstra free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0); 2256d16dfc55SPeter Zijlstra tlb_finish_mmu(&tlb, 0, end); 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds /* 22598f4f8c16SHugh Dickins * Walk the list again, actually closing and freeing it, 22608f4f8c16SHugh Dickins * with preemption enabled, without holding any MM locks. 22611da177e4SLinus Torvalds */ 2262a8fb5618SHugh Dickins while (vma) 2263a8fb5618SHugh Dickins vma = remove_vma(vma); 2264e0da382cSHugh Dickins 2265e2cdef8cSHugh Dickins BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT); 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 22691da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 22703d48ae45SPeter Zijlstra * then i_mmap_mutex is taken here. 22711da177e4SLinus Torvalds */ 22721da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma) 22731da177e4SLinus Torvalds { 22741da177e4SLinus Torvalds struct vm_area_struct * __vma, * prev; 22751da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds /* 22781da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 22791da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 22801da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 22811da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 22821da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 22831da177e4SLinus Torvalds * 22841da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 22851da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 22861da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 22871da177e4SLinus Torvalds * Similarly in do_mmap_pgoff and in do_brk. 22881da177e4SLinus Torvalds */ 22891da177e4SLinus Torvalds if (!vma->vm_file) { 22901da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 22911da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 22921da177e4SLinus Torvalds } 22931da177e4SLinus Torvalds __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent); 22941da177e4SLinus Torvalds if (__vma && __vma->vm_start < vma->vm_end) 22951da177e4SLinus Torvalds return -ENOMEM; 22962fd4ef85SHugh Dickins if ((vma->vm_flags & VM_ACCOUNT) && 229734b4e4aaSAlan Cox security_vm_enough_memory_mm(mm, vma_pages(vma))) 22982fd4ef85SHugh Dickins return -ENOMEM; 22991da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 23001da177e4SLinus Torvalds return 0; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds /* 23041da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 23051da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 23061da177e4SLinus Torvalds */ 23071da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 23081da177e4SLinus Torvalds unsigned long addr, unsigned long len, pgoff_t pgoff) 23091da177e4SLinus Torvalds { 23101da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 23111da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 23121da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 23131da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 23141da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 23151da177e4SLinus Torvalds struct mempolicy *pol; 2316948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds /* 23191da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 23201da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 23211da177e4SLinus Torvalds */ 2322948f017bSAndrea Arcangeli if (unlikely(!vma->vm_file && !vma->anon_vma)) { 23231da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 2324948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 2325948f017bSAndrea Arcangeli } 23261da177e4SLinus Torvalds 23271da177e4SLinus Torvalds find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 23281da177e4SLinus Torvalds new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, 23291da177e4SLinus Torvalds vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma)); 23301da177e4SLinus Torvalds if (new_vma) { 23311da177e4SLinus Torvalds /* 23321da177e4SLinus Torvalds * Source vma may have been merged into new_vma 23331da177e4SLinus Torvalds */ 2334948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 2335948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 2336948f017bSAndrea Arcangeli /* 2337948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 2338948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 2339948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 2340948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 2341948f017bSAndrea Arcangeli * destination address of the mremap to allow 2342948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 2343948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 2344948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 2345948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 2346948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 2347948f017bSAndrea Arcangeli */ 2348948f017bSAndrea Arcangeli VM_BUG_ON(faulted_in_anon_vma); 23491da177e4SLinus Torvalds *vmap = new_vma; 2350948f017bSAndrea Arcangeli } else 2351948f017bSAndrea Arcangeli anon_vma_moveto_tail(new_vma); 23521da177e4SLinus Torvalds } else { 2353e94b1766SChristoph Lameter new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 23541da177e4SLinus Torvalds if (new_vma) { 23551da177e4SLinus Torvalds *new_vma = *vma; 2356846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(vma)); 23575beb4930SRik van Riel if (IS_ERR(pol)) 23585beb4930SRik van Riel goto out_free_vma; 23595beb4930SRik van Riel INIT_LIST_HEAD(&new_vma->anon_vma_chain); 23605beb4930SRik van Riel if (anon_vma_clone(new_vma, vma)) 23615beb4930SRik van Riel goto out_free_mempol; 23621da177e4SLinus Torvalds vma_set_policy(new_vma, pol); 23631da177e4SLinus Torvalds new_vma->vm_start = addr; 23641da177e4SLinus Torvalds new_vma->vm_end = addr + len; 23651da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 2366925d1c40SMatt Helsley if (new_vma->vm_file) { 23671da177e4SLinus Torvalds get_file(new_vma->vm_file); 2368925d1c40SMatt Helsley if (vma->vm_flags & VM_EXECUTABLE) 2369925d1c40SMatt Helsley added_exe_file_vma(mm); 2370925d1c40SMatt Helsley } 23711da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 23721da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 23731da177e4SLinus Torvalds vma_link(mm, new_vma, prev, rb_link, rb_parent); 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds return new_vma; 23775beb4930SRik van Riel 23785beb4930SRik van Riel out_free_mempol: 23795beb4930SRik van Riel mpol_put(pol); 23805beb4930SRik van Riel out_free_vma: 23815beb4930SRik van Riel kmem_cache_free(vm_area_cachep, new_vma); 23825beb4930SRik van Riel return NULL; 23831da177e4SLinus Torvalds } 2384119f657cSakpm@osdl.org 2385119f657cSakpm@osdl.org /* 2386119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 2387119f657cSakpm@osdl.org * number of pages 2388119f657cSakpm@osdl.org */ 2389119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages) 2390119f657cSakpm@osdl.org { 2391119f657cSakpm@osdl.org unsigned long cur = mm->total_vm; /* pages */ 2392119f657cSakpm@osdl.org unsigned long lim; 2393119f657cSakpm@osdl.org 239459e99e5bSJiri Slaby lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT; 2395119f657cSakpm@osdl.org 2396119f657cSakpm@osdl.org if (cur + npages > lim) 2397119f657cSakpm@osdl.org return 0; 2398119f657cSakpm@osdl.org return 1; 2399119f657cSakpm@osdl.org } 2400fa5dc22fSRoland McGrath 2401fa5dc22fSRoland McGrath 2402b1d0e4f5SNick Piggin static int special_mapping_fault(struct vm_area_struct *vma, 2403b1d0e4f5SNick Piggin struct vm_fault *vmf) 2404fa5dc22fSRoland McGrath { 2405b1d0e4f5SNick Piggin pgoff_t pgoff; 2406fa5dc22fSRoland McGrath struct page **pages; 2407fa5dc22fSRoland McGrath 2408b1d0e4f5SNick Piggin /* 2409b1d0e4f5SNick Piggin * special mappings have no vm_file, and in that case, the mm 2410b1d0e4f5SNick Piggin * uses vm_pgoff internally. So we have to subtract it from here. 2411b1d0e4f5SNick Piggin * We are allowed to do this because we are the mm; do not copy 2412b1d0e4f5SNick Piggin * this code into drivers! 2413b1d0e4f5SNick Piggin */ 2414b1d0e4f5SNick Piggin pgoff = vmf->pgoff - vma->vm_pgoff; 2415fa5dc22fSRoland McGrath 2416b1d0e4f5SNick Piggin for (pages = vma->vm_private_data; pgoff && *pages; ++pages) 2417b1d0e4f5SNick Piggin pgoff--; 2418fa5dc22fSRoland McGrath 2419fa5dc22fSRoland McGrath if (*pages) { 2420fa5dc22fSRoland McGrath struct page *page = *pages; 2421fa5dc22fSRoland McGrath get_page(page); 2422b1d0e4f5SNick Piggin vmf->page = page; 2423b1d0e4f5SNick Piggin return 0; 2424fa5dc22fSRoland McGrath } 2425fa5dc22fSRoland McGrath 2426b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 2427fa5dc22fSRoland McGrath } 2428fa5dc22fSRoland McGrath 2429fa5dc22fSRoland McGrath /* 2430fa5dc22fSRoland McGrath * Having a close hook prevents vma merging regardless of flags. 2431fa5dc22fSRoland McGrath */ 2432fa5dc22fSRoland McGrath static void special_mapping_close(struct vm_area_struct *vma) 2433fa5dc22fSRoland McGrath { 2434fa5dc22fSRoland McGrath } 2435fa5dc22fSRoland McGrath 2436f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct special_mapping_vmops = { 2437fa5dc22fSRoland McGrath .close = special_mapping_close, 2438b1d0e4f5SNick Piggin .fault = special_mapping_fault, 2439fa5dc22fSRoland McGrath }; 2440fa5dc22fSRoland McGrath 2441fa5dc22fSRoland McGrath /* 2442fa5dc22fSRoland McGrath * Called with mm->mmap_sem held for writing. 2443fa5dc22fSRoland McGrath * Insert a new vma covering the given region, with the given flags. 2444fa5dc22fSRoland McGrath * Its pages are supplied by the given array of struct page *. 2445fa5dc22fSRoland McGrath * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 2446fa5dc22fSRoland McGrath * The region past the last page supplied will always produce SIGBUS. 2447fa5dc22fSRoland McGrath * The array pointer and the pages it points to are assumed to stay alive 2448fa5dc22fSRoland McGrath * for as long as this mapping might exist. 2449fa5dc22fSRoland McGrath */ 2450fa5dc22fSRoland McGrath int install_special_mapping(struct mm_struct *mm, 2451fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 2452fa5dc22fSRoland McGrath unsigned long vm_flags, struct page **pages) 2453fa5dc22fSRoland McGrath { 2454462e635eSTavis Ormandy int ret; 2455fa5dc22fSRoland McGrath struct vm_area_struct *vma; 2456fa5dc22fSRoland McGrath 2457fa5dc22fSRoland McGrath vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 2458fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 2459fa5dc22fSRoland McGrath return -ENOMEM; 2460fa5dc22fSRoland McGrath 24615beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 2462fa5dc22fSRoland McGrath vma->vm_mm = mm; 2463fa5dc22fSRoland McGrath vma->vm_start = addr; 2464fa5dc22fSRoland McGrath vma->vm_end = addr + len; 2465fa5dc22fSRoland McGrath 24662f98735cSNick Piggin vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND; 24673ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 2468fa5dc22fSRoland McGrath 2469fa5dc22fSRoland McGrath vma->vm_ops = &special_mapping_vmops; 2470fa5dc22fSRoland McGrath vma->vm_private_data = pages; 2471fa5dc22fSRoland McGrath 2472462e635eSTavis Ormandy ret = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1); 2473462e635eSTavis Ormandy if (ret) 2474462e635eSTavis Ormandy goto out; 2475462e635eSTavis Ormandy 2476462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 2477462e635eSTavis Ormandy if (ret) 2478462e635eSTavis Ormandy goto out; 2479fa5dc22fSRoland McGrath 2480fa5dc22fSRoland McGrath mm->total_vm += len >> PAGE_SHIFT; 2481fa5dc22fSRoland McGrath 2482cdd6c482SIngo Molnar perf_event_mmap(vma); 2483089dd79dSPeter Zijlstra 2484fa5dc22fSRoland McGrath return 0; 2485462e635eSTavis Ormandy 2486462e635eSTavis Ormandy out: 2487462e635eSTavis Ormandy kmem_cache_free(vm_area_cachep, vma); 2488462e635eSTavis Ormandy return ret; 2489fa5dc22fSRoland McGrath } 24907906d00cSAndrea Arcangeli 24917906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 24927906d00cSAndrea Arcangeli 2493454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 24947906d00cSAndrea Arcangeli { 2495012f1800SRik van Riel if (!test_bit(0, (unsigned long *) &anon_vma->root->head.next)) { 24967906d00cSAndrea Arcangeli /* 24977906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 24987906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 24997906d00cSAndrea Arcangeli */ 25002b575eb6SPeter Zijlstra mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem); 25017906d00cSAndrea Arcangeli /* 25027906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 25032b575eb6SPeter Zijlstra * anon_vma->root->mutex. If some other vma in this mm shares 25047906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 25057906d00cSAndrea Arcangeli * 25067906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 25077906d00cSAndrea Arcangeli * can't change from under us thanks to the 25082b575eb6SPeter Zijlstra * anon_vma->root->mutex. 25097906d00cSAndrea Arcangeli */ 25107906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 2511012f1800SRik van Riel &anon_vma->root->head.next)) 25127906d00cSAndrea Arcangeli BUG(); 25137906d00cSAndrea Arcangeli } 25147906d00cSAndrea Arcangeli } 25157906d00cSAndrea Arcangeli 2516454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 25177906d00cSAndrea Arcangeli { 25187906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 25197906d00cSAndrea Arcangeli /* 25207906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 25217906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 25227906d00cSAndrea Arcangeli * 25237906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 25247906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 25257906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 25267906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 25277906d00cSAndrea Arcangeli */ 25287906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 25297906d00cSAndrea Arcangeli BUG(); 25303d48ae45SPeter Zijlstra mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem); 25317906d00cSAndrea Arcangeli } 25327906d00cSAndrea Arcangeli } 25337906d00cSAndrea Arcangeli 25347906d00cSAndrea Arcangeli /* 25357906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 25367906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 25377906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 25387906d00cSAndrea Arcangeli * 25397906d00cSAndrea Arcangeli * The caller must take the mmap_sem in write mode before calling 25407906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 25417906d00cSAndrea Arcangeli * mmap_sem until mm_drop_all_locks() returns. 25427906d00cSAndrea Arcangeli * 25437906d00cSAndrea Arcangeli * mmap_sem in write mode is required in order to block all operations 25447906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 25457906d00cSAndrea Arcangeli * altering the vma layout (for example populate_range() with 25467906d00cSAndrea Arcangeli * nonlinear vmas). It's also needed in write mode to avoid new 25477906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 25487906d00cSAndrea Arcangeli * 25497906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 25507906d00cSAndrea Arcangeli * or it would deadlock. 25517906d00cSAndrea Arcangeli * 25527906d00cSAndrea Arcangeli * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in 25537906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 25547906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 25557906d00cSAndrea Arcangeli * 25567906d00cSAndrea Arcangeli * We can take all the locks in random order because the VM code 25572b575eb6SPeter Zijlstra * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never 25587906d00cSAndrea Arcangeli * takes more than one of them in a row. Secondly we're protected 25597906d00cSAndrea Arcangeli * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex. 25607906d00cSAndrea Arcangeli * 25617906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 25627906d00cSAndrea Arcangeli * that may have to take thousand of locks. 25637906d00cSAndrea Arcangeli * 25647906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 25657906d00cSAndrea Arcangeli */ 25667906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 25677906d00cSAndrea Arcangeli { 25687906d00cSAndrea Arcangeli struct vm_area_struct *vma; 25695beb4930SRik van Riel struct anon_vma_chain *avc; 25707906d00cSAndrea Arcangeli 25717906d00cSAndrea Arcangeli BUG_ON(down_read_trylock(&mm->mmap_sem)); 25727906d00cSAndrea Arcangeli 25737906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 25747906d00cSAndrea Arcangeli 25757906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 25767906d00cSAndrea Arcangeli if (signal_pending(current)) 25777906d00cSAndrea Arcangeli goto out_unlock; 25787906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 2579454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 25807906d00cSAndrea Arcangeli } 25817cd5a02fSPeter Zijlstra 25827cd5a02fSPeter Zijlstra for (vma = mm->mmap; vma; vma = vma->vm_next) { 25837cd5a02fSPeter Zijlstra if (signal_pending(current)) 25847cd5a02fSPeter Zijlstra goto out_unlock; 25857cd5a02fSPeter Zijlstra if (vma->anon_vma) 25865beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 25875beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 25887cd5a02fSPeter Zijlstra } 25897cd5a02fSPeter Zijlstra 2590584cff54SKautuk Consul return 0; 25917906d00cSAndrea Arcangeli 25927906d00cSAndrea Arcangeli out_unlock: 25937906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 2594584cff54SKautuk Consul return -EINTR; 25957906d00cSAndrea Arcangeli } 25967906d00cSAndrea Arcangeli 25977906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 25987906d00cSAndrea Arcangeli { 2599012f1800SRik van Riel if (test_bit(0, (unsigned long *) &anon_vma->root->head.next)) { 26007906d00cSAndrea Arcangeli /* 26017906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 26027906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 26037906d00cSAndrea Arcangeli * 26047906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 26057906d00cSAndrea Arcangeli * the vma so the users using the anon_vma->head will 26067906d00cSAndrea Arcangeli * never see our bitflag. 26077906d00cSAndrea Arcangeli * 26087906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 26097906d00cSAndrea Arcangeli * can't change from under us until we release the 26102b575eb6SPeter Zijlstra * anon_vma->root->mutex. 26117906d00cSAndrea Arcangeli */ 26127906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 2613012f1800SRik van Riel &anon_vma->root->head.next)) 26147906d00cSAndrea Arcangeli BUG(); 2615cba48b98SRik van Riel anon_vma_unlock(anon_vma); 26167906d00cSAndrea Arcangeli } 26177906d00cSAndrea Arcangeli } 26187906d00cSAndrea Arcangeli 26197906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 26207906d00cSAndrea Arcangeli { 26217906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 26227906d00cSAndrea Arcangeli /* 26237906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 26247906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 26257906d00cSAndrea Arcangeli */ 26263d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 26277906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 26287906d00cSAndrea Arcangeli &mapping->flags)) 26297906d00cSAndrea Arcangeli BUG(); 26307906d00cSAndrea Arcangeli } 26317906d00cSAndrea Arcangeli } 26327906d00cSAndrea Arcangeli 26337906d00cSAndrea Arcangeli /* 26347906d00cSAndrea Arcangeli * The mmap_sem cannot be released by the caller until 26357906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 26367906d00cSAndrea Arcangeli */ 26377906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 26387906d00cSAndrea Arcangeli { 26397906d00cSAndrea Arcangeli struct vm_area_struct *vma; 26405beb4930SRik van Riel struct anon_vma_chain *avc; 26417906d00cSAndrea Arcangeli 26427906d00cSAndrea Arcangeli BUG_ON(down_read_trylock(&mm->mmap_sem)); 26437906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 26447906d00cSAndrea Arcangeli 26457906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 26467906d00cSAndrea Arcangeli if (vma->anon_vma) 26475beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 26485beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 26497906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 26507906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 26517906d00cSAndrea Arcangeli } 26527906d00cSAndrea Arcangeli 26537906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 26547906d00cSAndrea Arcangeli } 26558feae131SDavid Howells 26568feae131SDavid Howells /* 26578feae131SDavid Howells * initialise the VMA slab 26588feae131SDavid Howells */ 26598feae131SDavid Howells void __init mmap_init(void) 26608feae131SDavid Howells { 266100a62ce9SKOSAKI Motohiro int ret; 266200a62ce9SKOSAKI Motohiro 266300a62ce9SKOSAKI Motohiro ret = percpu_counter_init(&vm_committed_as, 0); 266400a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 26658feae131SDavid Howells } 2666