11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * mm/mmap.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by obz. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Address space accounting code <alan@redhat.com> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/slab.h> 101da177e4SLinus Torvalds #include <linux/mm.h> 111da177e4SLinus Torvalds #include <linux/shm.h> 121da177e4SLinus Torvalds #include <linux/mman.h> 131da177e4SLinus Torvalds #include <linux/pagemap.h> 141da177e4SLinus Torvalds #include <linux/swap.h> 151da177e4SLinus Torvalds #include <linux/syscalls.h> 161da177e4SLinus Torvalds #include <linux/init.h> 171da177e4SLinus Torvalds #include <linux/file.h> 181da177e4SLinus Torvalds #include <linux/fs.h> 191da177e4SLinus Torvalds #include <linux/personality.h> 201da177e4SLinus Torvalds #include <linux/security.h> 211da177e4SLinus Torvalds #include <linux/hugetlb.h> 221da177e4SLinus Torvalds #include <linux/profile.h> 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/mount.h> 251da177e4SLinus Torvalds #include <linux/mempolicy.h> 261da177e4SLinus Torvalds #include <linux/rmap.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <asm/uaccess.h> 291da177e4SLinus Torvalds #include <asm/cacheflush.h> 301da177e4SLinus Torvalds #include <asm/tlb.h> 311da177e4SLinus Torvalds 32e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm, 33e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 34e0da382cSHugh Dickins unsigned long start, unsigned long end); 35e0da382cSHugh Dickins 361da177e4SLinus Torvalds /* 371da177e4SLinus Torvalds * WARNING: the debugging will use recursive algorithms so never enable this 381da177e4SLinus Torvalds * unless you know what you are doing. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds #undef DEBUG_MM_RB 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation. 431da177e4SLinus Torvalds * this is due to the limited x86 page protection hardware. The expected 441da177e4SLinus Torvalds * behavior is in parens: 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * map_type prot 471da177e4SLinus Torvalds * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC 481da177e4SLinus Torvalds * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes 491da177e4SLinus Torvalds * w: (no) no w: (no) no w: (yes) yes w: (no) no 501da177e4SLinus Torvalds * x: (no) no x: (no) yes x: (no) yes x: (yes) yes 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes 531da177e4SLinus Torvalds * w: (no) no w: (no) no w: (copy) copy w: (no) no 541da177e4SLinus Torvalds * x: (no) no x: (no) yes x: (no) yes x: (yes) yes 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds pgprot_t protection_map[16] = { 581da177e4SLinus Torvalds __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111, 591da177e4SLinus Torvalds __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111 601da177e4SLinus Torvalds }; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */ 631da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */ 641da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; 651da177e4SLinus Torvalds atomic_t vm_committed_space = ATOMIC_INIT(0); 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 691da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 701da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 711da177e4SLinus Torvalds * 721da177e4SLinus Torvalds * We currently support three overcommit policies, which are set via the 731da177e4SLinus Torvalds * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 761da177e4SLinus Torvalds * Additional code 2002 Jul 20 by Robert Love. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Note this is a helper function intended to be used by LSMs which 811da177e4SLinus Torvalds * wish to use this logic. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds int __vm_enough_memory(long pages, int cap_sys_admin) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds unsigned long free, allowed; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds vm_acct_memory(pages); 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* 901da177e4SLinus Torvalds * Sometimes we want to use more memory than we have 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 931da177e4SLinus Torvalds return 0; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 961da177e4SLinus Torvalds unsigned long n; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds free = get_page_cache_size(); 991da177e4SLinus Torvalds free += nr_swap_pages; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* 1021da177e4SLinus Torvalds * Any slabs which are created with the 1031da177e4SLinus Torvalds * SLAB_RECLAIM_ACCOUNT flag claim to have contents 1041da177e4SLinus Torvalds * which are reclaimable, under pressure. The dentry 1051da177e4SLinus Torvalds * cache and most inode caches should fall into this 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds free += atomic_read(&slab_reclaim_pages); 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* 1101da177e4SLinus Torvalds * Leave the last 3% for root 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds if (!cap_sys_admin) 1131da177e4SLinus Torvalds free -= free / 32; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds if (free > pages) 1161da177e4SLinus Torvalds return 0; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 1191da177e4SLinus Torvalds * nr_free_pages() is very expensive on large systems, 1201da177e4SLinus Torvalds * only call if we're about to fail. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds n = nr_free_pages(); 1231da177e4SLinus Torvalds if (!cap_sys_admin) 1241da177e4SLinus Torvalds n -= n / 32; 1251da177e4SLinus Torvalds free += n; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds if (free > pages) 1281da177e4SLinus Torvalds return 0; 1291da177e4SLinus Torvalds vm_unacct_memory(pages); 1301da177e4SLinus Torvalds return -ENOMEM; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds allowed = (totalram_pages - hugetlb_total_pages()) 1341da177e4SLinus Torvalds * sysctl_overcommit_ratio / 100; 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Leave the last 3% for root 1371da177e4SLinus Torvalds */ 1381da177e4SLinus Torvalds if (!cap_sys_admin) 1391da177e4SLinus Torvalds allowed -= allowed / 32; 1401da177e4SLinus Torvalds allowed += total_swap_pages; 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Don't let a single process grow too big: 1431da177e4SLinus Torvalds leave 3% of the size of this process for other processes */ 1441da177e4SLinus Torvalds allowed -= current->mm->total_vm / 32; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds if (atomic_read(&vm_committed_space) < allowed) 1471da177e4SLinus Torvalds return 0; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds vm_unacct_memory(pages); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds return -ENOMEM; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_overcommit_memory); 1551da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_overcommit_ratio); 1561da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_max_map_count); 1571da177e4SLinus Torvalds EXPORT_SYMBOL(vm_committed_space); 1581da177e4SLinus Torvalds EXPORT_SYMBOL(__vm_enough_memory); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* 1611da177e4SLinus Torvalds * Requires inode->i_mapping->i_mmap_lock 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1641da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds if (vma->vm_flags & VM_DENYWRITE) 1671da177e4SLinus Torvalds atomic_inc(&file->f_dentry->d_inode->i_writecount); 1681da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1691da177e4SLinus Torvalds mapping->i_mmap_writable--; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 1721da177e4SLinus Torvalds if (unlikely(vma->vm_flags & VM_NONLINEAR)) 1731da177e4SLinus Torvalds list_del_init(&vma->shared.vm_set.list); 1741da177e4SLinus Torvalds else 1751da177e4SLinus Torvalds vma_prio_tree_remove(vma, &mapping->i_mmap); 1761da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /* 1801da177e4SLinus Torvalds * Remove one vm structure and free it. 1811da177e4SLinus Torvalds */ 1821da177e4SLinus Torvalds static void remove_vm_struct(struct vm_area_struct *vma) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds struct file *file = vma->vm_file; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds might_sleep(); 1871da177e4SLinus Torvalds if (file) { 1881da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 1891da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 1901da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 1911da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 1941da177e4SLinus Torvalds vma->vm_ops->close(vma); 1951da177e4SLinus Torvalds if (file) 1961da177e4SLinus Torvalds fput(file); 1971da177e4SLinus Torvalds anon_vma_unlink(vma); 1981da177e4SLinus Torvalds mpol_free(vma_policy(vma)); 1991da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* 2031da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel 2041da177e4SLinus Torvalds * lock, except when an application is doing something nasty 2051da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped 2061da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need 2071da177e4SLinus Torvalds * to invoke file system routines that need the global lock. 2081da177e4SLinus Torvalds */ 2091da177e4SLinus Torvalds asmlinkage unsigned long sys_brk(unsigned long brk) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds unsigned long rlim, retval; 2121da177e4SLinus Torvalds unsigned long newbrk, oldbrk; 2131da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds down_write(&mm->mmap_sem); 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if (brk < mm->end_code) 2181da177e4SLinus Torvalds goto out; 2191da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2201da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2211da177e4SLinus Torvalds if (oldbrk == newbrk) 2221da177e4SLinus Torvalds goto set_brk; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* Always allow shrinking brk. */ 2251da177e4SLinus Torvalds if (brk <= mm->brk) { 2261da177e4SLinus Torvalds if (!do_munmap(mm, newbrk, oldbrk-newbrk)) 2271da177e4SLinus Torvalds goto set_brk; 2281da177e4SLinus Torvalds goto out; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* Check against rlimit.. */ 2321da177e4SLinus Torvalds rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur; 2331da177e4SLinus Torvalds if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim) 2341da177e4SLinus Torvalds goto out; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds /* Check against existing mmap mappings. */ 2371da177e4SLinus Torvalds if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE)) 2381da177e4SLinus Torvalds goto out; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 2411da177e4SLinus Torvalds if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk) 2421da177e4SLinus Torvalds goto out; 2431da177e4SLinus Torvalds set_brk: 2441da177e4SLinus Torvalds mm->brk = brk; 2451da177e4SLinus Torvalds out: 2461da177e4SLinus Torvalds retval = mm->brk; 2471da177e4SLinus Torvalds up_write(&mm->mmap_sem); 2481da177e4SLinus Torvalds return retval; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds #ifdef DEBUG_MM_RB 2521da177e4SLinus Torvalds static int browse_rb(struct rb_root *root) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds int i = 0, j; 2551da177e4SLinus Torvalds struct rb_node *nd, *pn = NULL; 2561da177e4SLinus Torvalds unsigned long prev = 0, pend = 0; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds for (nd = rb_first(root); nd; nd = rb_next(nd)) { 2591da177e4SLinus Torvalds struct vm_area_struct *vma; 2601da177e4SLinus Torvalds vma = rb_entry(nd, struct vm_area_struct, vm_rb); 2611da177e4SLinus Torvalds if (vma->vm_start < prev) 2621da177e4SLinus Torvalds printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1; 2631da177e4SLinus Torvalds if (vma->vm_start < pend) 2641da177e4SLinus Torvalds printk("vm_start %lx pend %lx\n", vma->vm_start, pend); 2651da177e4SLinus Torvalds if (vma->vm_start > vma->vm_end) 2661da177e4SLinus Torvalds printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start); 2671da177e4SLinus Torvalds i++; 2681da177e4SLinus Torvalds pn = nd; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds j = 0; 2711da177e4SLinus Torvalds for (nd = pn; nd; nd = rb_prev(nd)) { 2721da177e4SLinus Torvalds j++; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds if (i != j) 2751da177e4SLinus Torvalds printk("backwards %d, forwards %d\n", j, i), i = 0; 2761da177e4SLinus Torvalds return i; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds void validate_mm(struct mm_struct *mm) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds int bug = 0; 2821da177e4SLinus Torvalds int i = 0; 2831da177e4SLinus Torvalds struct vm_area_struct *tmp = mm->mmap; 2841da177e4SLinus Torvalds while (tmp) { 2851da177e4SLinus Torvalds tmp = tmp->vm_next; 2861da177e4SLinus Torvalds i++; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds if (i != mm->map_count) 2891da177e4SLinus Torvalds printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1; 2901da177e4SLinus Torvalds i = browse_rb(&mm->mm_rb); 2911da177e4SLinus Torvalds if (i != mm->map_count) 2921da177e4SLinus Torvalds printk("map_count %d rb %d\n", mm->map_count, i), bug = 1; 2931da177e4SLinus Torvalds if (bug) 2941da177e4SLinus Torvalds BUG(); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds #else 2971da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 2981da177e4SLinus Torvalds #endif 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static struct vm_area_struct * 3011da177e4SLinus Torvalds find_vma_prepare(struct mm_struct *mm, unsigned long addr, 3021da177e4SLinus Torvalds struct vm_area_struct **pprev, struct rb_node ***rb_link, 3031da177e4SLinus Torvalds struct rb_node ** rb_parent) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds struct vm_area_struct * vma; 3061da177e4SLinus Torvalds struct rb_node ** __rb_link, * __rb_parent, * rb_prev; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds __rb_link = &mm->mm_rb.rb_node; 3091da177e4SLinus Torvalds rb_prev = __rb_parent = NULL; 3101da177e4SLinus Torvalds vma = NULL; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds while (*__rb_link) { 3131da177e4SLinus Torvalds struct vm_area_struct *vma_tmp; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds __rb_parent = *__rb_link; 3161da177e4SLinus Torvalds vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (vma_tmp->vm_end > addr) { 3191da177e4SLinus Torvalds vma = vma_tmp; 3201da177e4SLinus Torvalds if (vma_tmp->vm_start <= addr) 3211da177e4SLinus Torvalds return vma; 3221da177e4SLinus Torvalds __rb_link = &__rb_parent->rb_left; 3231da177e4SLinus Torvalds } else { 3241da177e4SLinus Torvalds rb_prev = __rb_parent; 3251da177e4SLinus Torvalds __rb_link = &__rb_parent->rb_right; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds *pprev = NULL; 3301da177e4SLinus Torvalds if (rb_prev) 3311da177e4SLinus Torvalds *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); 3321da177e4SLinus Torvalds *rb_link = __rb_link; 3331da177e4SLinus Torvalds *rb_parent = __rb_parent; 3341da177e4SLinus Torvalds return vma; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds static inline void 3381da177e4SLinus Torvalds __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, 3391da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node *rb_parent) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds if (prev) { 3421da177e4SLinus Torvalds vma->vm_next = prev->vm_next; 3431da177e4SLinus Torvalds prev->vm_next = vma; 3441da177e4SLinus Torvalds } else { 3451da177e4SLinus Torvalds mm->mmap = vma; 3461da177e4SLinus Torvalds if (rb_parent) 3471da177e4SLinus Torvalds vma->vm_next = rb_entry(rb_parent, 3481da177e4SLinus Torvalds struct vm_area_struct, vm_rb); 3491da177e4SLinus Torvalds else 3501da177e4SLinus Torvalds vma->vm_next = NULL; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma, 3551da177e4SLinus Torvalds struct rb_node **rb_link, struct rb_node *rb_parent) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds rb_link_node(&vma->vm_rb, rb_parent, rb_link); 3581da177e4SLinus Torvalds rb_insert_color(&vma->vm_rb, &mm->mm_rb); 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds static inline void __vma_link_file(struct vm_area_struct *vma) 3621da177e4SLinus Torvalds { 3631da177e4SLinus Torvalds struct file * file; 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds file = vma->vm_file; 3661da177e4SLinus Torvalds if (file) { 3671da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (vma->vm_flags & VM_DENYWRITE) 3701da177e4SLinus Torvalds atomic_dec(&file->f_dentry->d_inode->i_writecount); 3711da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 3721da177e4SLinus Torvalds mapping->i_mmap_writable++; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 3751da177e4SLinus Torvalds if (unlikely(vma->vm_flags & VM_NONLINEAR)) 3761da177e4SLinus Torvalds vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear); 3771da177e4SLinus Torvalds else 3781da177e4SLinus Torvalds vma_prio_tree_insert(vma, &mapping->i_mmap); 3791da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds static void 3841da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 3851da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node **rb_link, 3861da177e4SLinus Torvalds struct rb_node *rb_parent) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds __vma_link_list(mm, vma, prev, rb_parent); 3891da177e4SLinus Torvalds __vma_link_rb(mm, vma, rb_link, rb_parent); 3901da177e4SLinus Torvalds __anon_vma_link(vma); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 3941da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node **rb_link, 3951da177e4SLinus Torvalds struct rb_node *rb_parent) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds struct address_space *mapping = NULL; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds if (vma->vm_file) 4001da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (mapping) { 4031da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 4041da177e4SLinus Torvalds vma->vm_truncate_count = mapping->truncate_count; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds anon_vma_lock(vma); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds __vma_link(mm, vma, prev, rb_link, rb_parent); 4091da177e4SLinus Torvalds __vma_link_file(vma); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds anon_vma_unlock(vma); 4121da177e4SLinus Torvalds if (mapping) 4131da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds mm->map_count++; 4161da177e4SLinus Torvalds validate_mm(mm); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* 4201da177e4SLinus Torvalds * Helper for vma_adjust in the split_vma insert case: 4211da177e4SLinus Torvalds * insert vm structure into list and rbtree and anon_vma, 4221da177e4SLinus Torvalds * but it has already been inserted into prio_tree earlier. 4231da177e4SLinus Torvalds */ 4241da177e4SLinus Torvalds static void 4251da177e4SLinus Torvalds __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds struct vm_area_struct * __vma, * prev; 4281da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent); 4311da177e4SLinus Torvalds if (__vma && __vma->vm_start < vma->vm_end) 4321da177e4SLinus Torvalds BUG(); 4331da177e4SLinus Torvalds __vma_link(mm, vma, prev, rb_link, rb_parent); 4341da177e4SLinus Torvalds mm->map_count++; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds static inline void 4381da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma, 4391da177e4SLinus Torvalds struct vm_area_struct *prev) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds prev->vm_next = vma->vm_next; 4421da177e4SLinus Torvalds rb_erase(&vma->vm_rb, &mm->mm_rb); 4431da177e4SLinus Torvalds if (mm->mmap_cache == vma) 4441da177e4SLinus Torvalds mm->mmap_cache = prev; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* 4481da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 4491da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 4501da177e4SLinus Torvalds * The following helper function should be used when such adjustments 4511da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 4521da177e4SLinus Torvalds * before we drop the necessary locks. 4531da177e4SLinus Torvalds */ 4541da177e4SLinus Torvalds void vma_adjust(struct vm_area_struct *vma, unsigned long start, 4551da177e4SLinus Torvalds unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 4581da177e4SLinus Torvalds struct vm_area_struct *next = vma->vm_next; 4591da177e4SLinus Torvalds struct vm_area_struct *importer = NULL; 4601da177e4SLinus Torvalds struct address_space *mapping = NULL; 4611da177e4SLinus Torvalds struct prio_tree_root *root = NULL; 4621da177e4SLinus Torvalds struct file *file = vma->vm_file; 4631da177e4SLinus Torvalds struct anon_vma *anon_vma = NULL; 4641da177e4SLinus Torvalds long adjust_next = 0; 4651da177e4SLinus Torvalds int remove_next = 0; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds if (next && !insert) { 4681da177e4SLinus Torvalds if (end >= next->vm_end) { 4691da177e4SLinus Torvalds /* 4701da177e4SLinus Torvalds * vma expands, overlapping all the next, and 4711da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 4721da177e4SLinus Torvalds */ 4731da177e4SLinus Torvalds again: remove_next = 1 + (end > next->vm_end); 4741da177e4SLinus Torvalds end = next->vm_end; 4751da177e4SLinus Torvalds anon_vma = next->anon_vma; 4761da177e4SLinus Torvalds importer = vma; 4771da177e4SLinus Torvalds } else if (end > next->vm_start) { 4781da177e4SLinus Torvalds /* 4791da177e4SLinus Torvalds * vma expands, overlapping part of the next: 4801da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 4811da177e4SLinus Torvalds */ 4821da177e4SLinus Torvalds adjust_next = (end - next->vm_start) >> PAGE_SHIFT; 4831da177e4SLinus Torvalds anon_vma = next->anon_vma; 4841da177e4SLinus Torvalds importer = vma; 4851da177e4SLinus Torvalds } else if (end < vma->vm_end) { 4861da177e4SLinus Torvalds /* 4871da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 4881da177e4SLinus Torvalds * split_vma inserting another: so it must be 4891da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 4901da177e4SLinus Torvalds */ 4911da177e4SLinus Torvalds adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT); 4921da177e4SLinus Torvalds anon_vma = next->anon_vma; 4931da177e4SLinus Torvalds importer = next; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds if (file) { 4981da177e4SLinus Torvalds mapping = file->f_mapping; 4991da177e4SLinus Torvalds if (!(vma->vm_flags & VM_NONLINEAR)) 5001da177e4SLinus Torvalds root = &mapping->i_mmap; 5011da177e4SLinus Torvalds spin_lock(&mapping->i_mmap_lock); 5021da177e4SLinus Torvalds if (importer && 5031da177e4SLinus Torvalds vma->vm_truncate_count != next->vm_truncate_count) { 5041da177e4SLinus Torvalds /* 5051da177e4SLinus Torvalds * unmap_mapping_range might be in progress: 5061da177e4SLinus Torvalds * ensure that the expanding vma is rescanned. 5071da177e4SLinus Torvalds */ 5081da177e4SLinus Torvalds importer->vm_truncate_count = 0; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds if (insert) { 5111da177e4SLinus Torvalds insert->vm_truncate_count = vma->vm_truncate_count; 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds * Put into prio_tree now, so instantiated pages 5141da177e4SLinus Torvalds * are visible to arm/parisc __flush_dcache_page 5151da177e4SLinus Torvalds * throughout; but we cannot insert into address 5161da177e4SLinus Torvalds * space until vma start or end is updated. 5171da177e4SLinus Torvalds */ 5181da177e4SLinus Torvalds __vma_link_file(insert); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* 5231da177e4SLinus Torvalds * When changing only vma->vm_end, we don't really need 5241da177e4SLinus Torvalds * anon_vma lock: but is that case worth optimizing out? 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds if (vma->anon_vma) 5271da177e4SLinus Torvalds anon_vma = vma->anon_vma; 5281da177e4SLinus Torvalds if (anon_vma) { 5291da177e4SLinus Torvalds spin_lock(&anon_vma->lock); 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * Easily overlooked: when mprotect shifts the boundary, 5321da177e4SLinus Torvalds * make sure the expanding vma has anon_vma set if the 5331da177e4SLinus Torvalds * shrinking vma had, to cover any anon pages imported. 5341da177e4SLinus Torvalds */ 5351da177e4SLinus Torvalds if (importer && !importer->anon_vma) { 5361da177e4SLinus Torvalds importer->anon_vma = anon_vma; 5371da177e4SLinus Torvalds __anon_vma_link(importer); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds if (root) { 5421da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 5431da177e4SLinus Torvalds vma_prio_tree_remove(vma, root); 5441da177e4SLinus Torvalds if (adjust_next) 5451da177e4SLinus Torvalds vma_prio_tree_remove(next, root); 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds vma->vm_start = start; 5491da177e4SLinus Torvalds vma->vm_end = end; 5501da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 5511da177e4SLinus Torvalds if (adjust_next) { 5521da177e4SLinus Torvalds next->vm_start += adjust_next << PAGE_SHIFT; 5531da177e4SLinus Torvalds next->vm_pgoff += adjust_next; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds if (root) { 5571da177e4SLinus Torvalds if (adjust_next) 5581da177e4SLinus Torvalds vma_prio_tree_insert(next, root); 5591da177e4SLinus Torvalds vma_prio_tree_insert(vma, root); 5601da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds if (remove_next) { 5641da177e4SLinus Torvalds /* 5651da177e4SLinus Torvalds * vma_merge has merged next into vma, and needs 5661da177e4SLinus Torvalds * us to remove next before dropping the locks. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds __vma_unlink(mm, next, vma); 5691da177e4SLinus Torvalds if (file) 5701da177e4SLinus Torvalds __remove_shared_vm_struct(next, file, mapping); 5711da177e4SLinus Torvalds if (next->anon_vma) 5721da177e4SLinus Torvalds __anon_vma_merge(vma, next); 5731da177e4SLinus Torvalds } else if (insert) { 5741da177e4SLinus Torvalds /* 5751da177e4SLinus Torvalds * split_vma has split insert from vma, and needs 5761da177e4SLinus Torvalds * us to insert it before dropping the locks 5771da177e4SLinus Torvalds * (it may either follow vma or precede it). 5781da177e4SLinus Torvalds */ 5791da177e4SLinus Torvalds __insert_vm_struct(mm, insert); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds if (anon_vma) 5831da177e4SLinus Torvalds spin_unlock(&anon_vma->lock); 5841da177e4SLinus Torvalds if (mapping) 5851da177e4SLinus Torvalds spin_unlock(&mapping->i_mmap_lock); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds if (remove_next) { 5881da177e4SLinus Torvalds if (file) 5891da177e4SLinus Torvalds fput(file); 5901da177e4SLinus Torvalds mm->map_count--; 5911da177e4SLinus Torvalds mpol_free(vma_policy(next)); 5921da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, next); 5931da177e4SLinus Torvalds /* 5941da177e4SLinus Torvalds * In mprotect's case 6 (see comments on vma_merge), 5951da177e4SLinus Torvalds * we must remove another next too. It would clutter 5961da177e4SLinus Torvalds * up the code too much to do both in one go. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds if (remove_next == 2) { 5991da177e4SLinus Torvalds next = vma->vm_next; 6001da177e4SLinus Torvalds goto again; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds validate_mm(mm); 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* 6081da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 6091da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED) 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 6141da177e4SLinus Torvalds struct file *file, unsigned long vm_flags) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds if (vma->vm_flags != vm_flags) 6171da177e4SLinus Torvalds return 0; 6181da177e4SLinus Torvalds if (vma->vm_file != file) 6191da177e4SLinus Torvalds return 0; 6201da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 6211da177e4SLinus Torvalds return 0; 6221da177e4SLinus Torvalds return 1; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 6261da177e4SLinus Torvalds struct anon_vma *anon_vma2) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2); 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds /* 6321da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 6331da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 6361da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 6371da177e4SLinus Torvalds * 6381da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 6391da177e4SLinus Torvalds * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which 6401da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 6411da177e4SLinus Torvalds */ 6421da177e4SLinus Torvalds static int 6431da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 6441da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) 6451da177e4SLinus Torvalds { 6461da177e4SLinus Torvalds if (is_mergeable_vma(vma, file, vm_flags) && 6471da177e4SLinus Torvalds is_mergeable_anon_vma(anon_vma, vma->anon_vma)) { 6481da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 6491da177e4SLinus Torvalds return 1; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds return 0; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds /* 6551da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 6561da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 6571da177e4SLinus Torvalds * 6581da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 6591da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 6601da177e4SLinus Torvalds */ 6611da177e4SLinus Torvalds static int 6621da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 6631da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) 6641da177e4SLinus Torvalds { 6651da177e4SLinus Torvalds if (is_mergeable_vma(vma, file, vm_flags) && 6661da177e4SLinus Torvalds is_mergeable_anon_vma(anon_vma, vma->anon_vma)) { 6671da177e4SLinus Torvalds pgoff_t vm_pglen; 6681da177e4SLinus Torvalds vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 6691da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 6701da177e4SLinus Torvalds return 1; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds return 0; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* 6761da177e4SLinus Torvalds * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out 6771da177e4SLinus Torvalds * whether that can be merged with its predecessor or its successor. 6781da177e4SLinus Torvalds * Or both (it neatly fills a hole). 6791da177e4SLinus Torvalds * 6801da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 6811da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 6821da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 6831da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 6841da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 6851da177e4SLinus Torvalds * case has already been eliminated. 6861da177e4SLinus Torvalds * 6871da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 6881da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 6891da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 6901da177e4SLinus Torvalds * 6911da177e4SLinus Torvalds * AAAA AAAA AAAA AAAA 6921da177e4SLinus Torvalds * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX 6931da177e4SLinus Torvalds * cannot merge might become might become might become 6941da177e4SLinus Torvalds * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or 6951da177e4SLinus Torvalds * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or 6961da177e4SLinus Torvalds * mremap move: PPPPNNNNNNNN 8 6971da177e4SLinus Torvalds * AAAA 6981da177e4SLinus Torvalds * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN 6991da177e4SLinus Torvalds * might become case 1 below case 2 below case 3 below 7001da177e4SLinus Torvalds * 7011da177e4SLinus Torvalds * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX: 7021da177e4SLinus Torvalds * mprotect_fixup updates vm_flags & vm_page_prot on successful return. 7031da177e4SLinus Torvalds */ 7041da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm, 7051da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 7061da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 7071da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 7081da177e4SLinus Torvalds pgoff_t pgoff, struct mempolicy *policy) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 7111da177e4SLinus Torvalds struct vm_area_struct *area, *next; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /* 7141da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 7151da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 7161da177e4SLinus Torvalds */ 7171da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 7181da177e4SLinus Torvalds return NULL; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds if (prev) 7211da177e4SLinus Torvalds next = prev->vm_next; 7221da177e4SLinus Torvalds else 7231da177e4SLinus Torvalds next = mm->mmap; 7241da177e4SLinus Torvalds area = next; 7251da177e4SLinus Torvalds if (next && next->vm_end == end) /* cases 6, 7, 8 */ 7261da177e4SLinus Torvalds next = next->vm_next; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* 7291da177e4SLinus Torvalds * Can it merge with the predecessor? 7301da177e4SLinus Torvalds */ 7311da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 7321da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 7331da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 7341da177e4SLinus Torvalds anon_vma, file, pgoff)) { 7351da177e4SLinus Torvalds /* 7361da177e4SLinus Torvalds * OK, it can. Can we now merge in the successor as well? 7371da177e4SLinus Torvalds */ 7381da177e4SLinus Torvalds if (next && end == next->vm_start && 7391da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 7401da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 7411da177e4SLinus Torvalds anon_vma, file, pgoff+pglen) && 7421da177e4SLinus Torvalds is_mergeable_anon_vma(prev->anon_vma, 7431da177e4SLinus Torvalds next->anon_vma)) { 7441da177e4SLinus Torvalds /* cases 1, 6 */ 7451da177e4SLinus Torvalds vma_adjust(prev, prev->vm_start, 7461da177e4SLinus Torvalds next->vm_end, prev->vm_pgoff, NULL); 7471da177e4SLinus Torvalds } else /* cases 2, 5, 7 */ 7481da177e4SLinus Torvalds vma_adjust(prev, prev->vm_start, 7491da177e4SLinus Torvalds end, prev->vm_pgoff, NULL); 7501da177e4SLinus Torvalds return prev; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * Can this new request be merged in front of next? 7551da177e4SLinus Torvalds */ 7561da177e4SLinus Torvalds if (next && end == next->vm_start && 7571da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 7581da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 7591da177e4SLinus Torvalds anon_vma, file, pgoff+pglen)) { 7601da177e4SLinus Torvalds if (prev && addr < prev->vm_end) /* case 4 */ 7611da177e4SLinus Torvalds vma_adjust(prev, prev->vm_start, 7621da177e4SLinus Torvalds addr, prev->vm_pgoff, NULL); 7631da177e4SLinus Torvalds else /* cases 3, 8 */ 7641da177e4SLinus Torvalds vma_adjust(area, addr, next->vm_end, 7651da177e4SLinus Torvalds next->vm_pgoff - pglen, NULL); 7661da177e4SLinus Torvalds return area; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds return NULL; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds /* 7731da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 7741da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 7751da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 7761da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 7771da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 7781da177e4SLinus Torvalds * mprotect. 7791da177e4SLinus Torvalds */ 7801da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 7811da177e4SLinus Torvalds { 7821da177e4SLinus Torvalds struct vm_area_struct *near; 7831da177e4SLinus Torvalds unsigned long vm_flags; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds near = vma->vm_next; 7861da177e4SLinus Torvalds if (!near) 7871da177e4SLinus Torvalds goto try_prev; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds /* 7901da177e4SLinus Torvalds * Since only mprotect tries to remerge vmas, match flags 7911da177e4SLinus Torvalds * which might be mprotected into each other later on. 7921da177e4SLinus Torvalds * Neither mlock nor madvise tries to remerge at present, 7931da177e4SLinus Torvalds * so leave their flags as obstructing a merge. 7941da177e4SLinus Torvalds */ 7951da177e4SLinus Torvalds vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC); 7961da177e4SLinus Torvalds vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC); 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds if (near->anon_vma && vma->vm_end == near->vm_start && 7991da177e4SLinus Torvalds mpol_equal(vma_policy(vma), vma_policy(near)) && 8001da177e4SLinus Torvalds can_vma_merge_before(near, vm_flags, 8011da177e4SLinus Torvalds NULL, vma->vm_file, vma->vm_pgoff + 8021da177e4SLinus Torvalds ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT))) 8031da177e4SLinus Torvalds return near->anon_vma; 8041da177e4SLinus Torvalds try_prev: 8051da177e4SLinus Torvalds /* 8061da177e4SLinus Torvalds * It is potentially slow to have to call find_vma_prev here. 8071da177e4SLinus Torvalds * But it's only on the first write fault on the vma, not 8081da177e4SLinus Torvalds * every time, and we could devise a way to avoid it later 8091da177e4SLinus Torvalds * (e.g. stash info in next's anon_vma_node when assigning 8101da177e4SLinus Torvalds * an anon_vma, or when trying vma_merge). Another time. 8111da177e4SLinus Torvalds */ 8121da177e4SLinus Torvalds if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma) 8131da177e4SLinus Torvalds BUG(); 8141da177e4SLinus Torvalds if (!near) 8151da177e4SLinus Torvalds goto none; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC); 8181da177e4SLinus Torvalds vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC); 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds if (near->anon_vma && near->vm_end == vma->vm_start && 8211da177e4SLinus Torvalds mpol_equal(vma_policy(near), vma_policy(vma)) && 8221da177e4SLinus Torvalds can_vma_merge_after(near, vm_flags, 8231da177e4SLinus Torvalds NULL, vma->vm_file, vma->vm_pgoff)) 8241da177e4SLinus Torvalds return near->anon_vma; 8251da177e4SLinus Torvalds none: 8261da177e4SLinus Torvalds /* 8271da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 8281da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 8291da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 8301da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 8311da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 8321da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 8331da177e4SLinus Torvalds */ 8341da177e4SLinus Torvalds return NULL; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 8381da177e4SLinus Torvalds void __vm_stat_account(struct mm_struct *mm, unsigned long flags, 8391da177e4SLinus Torvalds struct file *file, long pages) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds const unsigned long stack_flags 8421da177e4SLinus Torvalds = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds #ifdef CONFIG_HUGETLB 8451da177e4SLinus Torvalds if (flags & VM_HUGETLB) { 8461da177e4SLinus Torvalds if (!(flags & VM_DONTCOPY)) 8471da177e4SLinus Torvalds mm->shared_vm += pages; 8481da177e4SLinus Torvalds return; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds #endif /* CONFIG_HUGETLB */ 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds if (file) { 8531da177e4SLinus Torvalds mm->shared_vm += pages; 8541da177e4SLinus Torvalds if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC) 8551da177e4SLinus Torvalds mm->exec_vm += pages; 8561da177e4SLinus Torvalds } else if (flags & stack_flags) 8571da177e4SLinus Torvalds mm->stack_vm += pages; 8581da177e4SLinus Torvalds if (flags & (VM_RESERVED|VM_IO)) 8591da177e4SLinus Torvalds mm->reserved_vm += pages; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* 8641da177e4SLinus Torvalds * The caller must hold down_write(current->mm->mmap_sem). 8651da177e4SLinus Torvalds */ 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file * file, unsigned long addr, 8681da177e4SLinus Torvalds unsigned long len, unsigned long prot, 8691da177e4SLinus Torvalds unsigned long flags, unsigned long pgoff) 8701da177e4SLinus Torvalds { 8711da177e4SLinus Torvalds struct mm_struct * mm = current->mm; 8721da177e4SLinus Torvalds struct vm_area_struct * vma, * prev; 8731da177e4SLinus Torvalds struct inode *inode; 8741da177e4SLinus Torvalds unsigned int vm_flags; 8751da177e4SLinus Torvalds int correct_wcount = 0; 8761da177e4SLinus Torvalds int error; 8771da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 8781da177e4SLinus Torvalds int accountable = 1; 8791da177e4SLinus Torvalds unsigned long charged = 0, reqprot = prot; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds if (file) { 8821da177e4SLinus Torvalds if (is_file_hugepages(file)) 8831da177e4SLinus Torvalds accountable = 0; 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds if (!file->f_op || !file->f_op->mmap) 8861da177e4SLinus Torvalds return -ENODEV; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds if ((prot & PROT_EXEC) && 8891da177e4SLinus Torvalds (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)) 8901da177e4SLinus Torvalds return -EPERM; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds /* 8931da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 8941da177e4SLinus Torvalds * 8951da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 8961da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 8971da177e4SLinus Torvalds */ 8981da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 8991da177e4SLinus Torvalds if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))) 9001da177e4SLinus Torvalds prot |= PROT_EXEC; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds if (!len) 9031da177e4SLinus Torvalds return -EINVAL; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds /* Careful about overflows.. */ 9061da177e4SLinus Torvalds len = PAGE_ALIGN(len); 9071da177e4SLinus Torvalds if (!len || len > TASK_SIZE) 9081da177e4SLinus Torvalds return -ENOMEM; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds /* offset overflow? */ 9111da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 9121da177e4SLinus Torvalds return -EOVERFLOW; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* Too many mappings? */ 9151da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 9161da177e4SLinus Torvalds return -ENOMEM; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 9191da177e4SLinus Torvalds * that it represents a valid section of the address space. 9201da177e4SLinus Torvalds */ 9211da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 9221da177e4SLinus Torvalds if (addr & ~PAGE_MASK) 9231da177e4SLinus Torvalds return addr; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 9261da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 9271da177e4SLinus Torvalds * of the memory object, so we don't do any here. 9281da177e4SLinus Torvalds */ 9291da177e4SLinus Torvalds vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) | 9301da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds if (flags & MAP_LOCKED) { 9331da177e4SLinus Torvalds if (!can_do_mlock()) 9341da177e4SLinus Torvalds return -EPERM; 9351da177e4SLinus Torvalds vm_flags |= VM_LOCKED; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds /* mlock MCL_FUTURE? */ 9381da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 9391da177e4SLinus Torvalds unsigned long locked, lock_limit; 94093ea1d0aSChris Wright locked = len >> PAGE_SHIFT; 94193ea1d0aSChris Wright locked += mm->locked_vm; 9421da177e4SLinus Torvalds lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; 94393ea1d0aSChris Wright lock_limit >>= PAGE_SHIFT; 9441da177e4SLinus Torvalds if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 9451da177e4SLinus Torvalds return -EAGAIN; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds inode = file ? file->f_dentry->d_inode : NULL; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds if (file) { 9511da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 9521da177e4SLinus Torvalds case MAP_SHARED: 9531da177e4SLinus Torvalds if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) 9541da177e4SLinus Torvalds return -EACCES; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* 9571da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 9581da177e4SLinus Torvalds * file.. 9591da177e4SLinus Torvalds */ 9601da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 9611da177e4SLinus Torvalds return -EACCES; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds /* 9641da177e4SLinus Torvalds * Make sure there are no mandatory locks on the file. 9651da177e4SLinus Torvalds */ 9661da177e4SLinus Torvalds if (locks_verify_locked(inode)) 9671da177e4SLinus Torvalds return -EAGAIN; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 9701da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 9711da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* fall through */ 9741da177e4SLinus Torvalds case MAP_PRIVATE: 9751da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 9761da177e4SLinus Torvalds return -EACCES; 9771da177e4SLinus Torvalds break; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds default: 9801da177e4SLinus Torvalds return -EINVAL; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds } else { 9831da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 9841da177e4SLinus Torvalds case MAP_SHARED: 9851da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 9861da177e4SLinus Torvalds break; 9871da177e4SLinus Torvalds case MAP_PRIVATE: 9881da177e4SLinus Torvalds /* 9891da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 9901da177e4SLinus Torvalds */ 9911da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 9921da177e4SLinus Torvalds break; 9931da177e4SLinus Torvalds default: 9941da177e4SLinus Torvalds return -EINVAL; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds error = security_file_mmap(file, reqprot, prot, flags); 9991da177e4SLinus Torvalds if (error) 10001da177e4SLinus Torvalds return error; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /* Clear old maps */ 10031da177e4SLinus Torvalds error = -ENOMEM; 10041da177e4SLinus Torvalds munmap_back: 10051da177e4SLinus Torvalds vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 10061da177e4SLinus Torvalds if (vma && vma->vm_start < addr + len) { 10071da177e4SLinus Torvalds if (do_munmap(mm, addr, len)) 10081da177e4SLinus Torvalds return -ENOMEM; 10091da177e4SLinus Torvalds goto munmap_back; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds /* Check against address space limit. */ 1013119f657cSakpm@osdl.org if (!may_expand_vm(mm, len >> PAGE_SHIFT)) 10141da177e4SLinus Torvalds return -ENOMEM; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds if (accountable && (!(flags & MAP_NORESERVE) || 10171da177e4SLinus Torvalds sysctl_overcommit_memory == OVERCOMMIT_NEVER)) { 10181da177e4SLinus Torvalds if (vm_flags & VM_SHARED) { 10191da177e4SLinus Torvalds /* Check memory availability in shmem_file_setup? */ 10201da177e4SLinus Torvalds vm_flags |= VM_ACCOUNT; 10211da177e4SLinus Torvalds } else if (vm_flags & VM_WRITE) { 10221da177e4SLinus Torvalds /* 10231da177e4SLinus Torvalds * Private writable mapping: check memory availability 10241da177e4SLinus Torvalds */ 10251da177e4SLinus Torvalds charged = len >> PAGE_SHIFT; 10261da177e4SLinus Torvalds if (security_vm_enough_memory(charged)) 10271da177e4SLinus Torvalds return -ENOMEM; 10281da177e4SLinus Torvalds vm_flags |= VM_ACCOUNT; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* 10331da177e4SLinus Torvalds * Can we just expand an old private anonymous mapping? 10341da177e4SLinus Torvalds * The VM_SHARED test is necessary because shmem_zero_setup 10351da177e4SLinus Torvalds * will create the file object for a shared anonymous map below. 10361da177e4SLinus Torvalds */ 10371da177e4SLinus Torvalds if (!file && !(vm_flags & VM_SHARED) && 10381da177e4SLinus Torvalds vma_merge(mm, prev, addr, addr + len, vm_flags, 10391da177e4SLinus Torvalds NULL, NULL, pgoff, NULL)) 10401da177e4SLinus Torvalds goto out; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds /* 10431da177e4SLinus Torvalds * Determine the object being mapped and call the appropriate 10441da177e4SLinus Torvalds * specific mapper. the address has already been validated, but 10451da177e4SLinus Torvalds * not unmapped, but the maps are removed from the list. 10461da177e4SLinus Torvalds */ 10471da177e4SLinus Torvalds vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); 10481da177e4SLinus Torvalds if (!vma) { 10491da177e4SLinus Torvalds error = -ENOMEM; 10501da177e4SLinus Torvalds goto unacct_error; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds memset(vma, 0, sizeof(*vma)); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds vma->vm_mm = mm; 10551da177e4SLinus Torvalds vma->vm_start = addr; 10561da177e4SLinus Torvalds vma->vm_end = addr + len; 10571da177e4SLinus Torvalds vma->vm_flags = vm_flags; 10581da177e4SLinus Torvalds vma->vm_page_prot = protection_map[vm_flags & 0x0f]; 10591da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds if (file) { 10621da177e4SLinus Torvalds error = -EINVAL; 10631da177e4SLinus Torvalds if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 10641da177e4SLinus Torvalds goto free_vma; 10651da177e4SLinus Torvalds if (vm_flags & VM_DENYWRITE) { 10661da177e4SLinus Torvalds error = deny_write_access(file); 10671da177e4SLinus Torvalds if (error) 10681da177e4SLinus Torvalds goto free_vma; 10691da177e4SLinus Torvalds correct_wcount = 1; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds vma->vm_file = file; 10721da177e4SLinus Torvalds get_file(file); 10731da177e4SLinus Torvalds error = file->f_op->mmap(file, vma); 10741da177e4SLinus Torvalds if (error) 10751da177e4SLinus Torvalds goto unmap_and_free_vma; 10761da177e4SLinus Torvalds } else if (vm_flags & VM_SHARED) { 10771da177e4SLinus Torvalds error = shmem_zero_setup(vma); 10781da177e4SLinus Torvalds if (error) 10791da177e4SLinus Torvalds goto free_vma; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform 10831da177e4SLinus Torvalds * shmem_zero_setup (perhaps called through /dev/zero's ->mmap) 10841da177e4SLinus Torvalds * that memory reservation must be checked; but that reservation 10851da177e4SLinus Torvalds * belongs to shared memory object, not to vma: so now clear it. 10861da177e4SLinus Torvalds */ 10871da177e4SLinus Torvalds if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT)) 10881da177e4SLinus Torvalds vma->vm_flags &= ~VM_ACCOUNT; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* Can addr have changed?? 10911da177e4SLinus Torvalds * 10921da177e4SLinus Torvalds * Answer: Yes, several device drivers can do it in their 10931da177e4SLinus Torvalds * f_op->mmap method. -DaveM 10941da177e4SLinus Torvalds */ 10951da177e4SLinus Torvalds addr = vma->vm_start; 10961da177e4SLinus Torvalds pgoff = vma->vm_pgoff; 10971da177e4SLinus Torvalds vm_flags = vma->vm_flags; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds if (!file || !vma_merge(mm, prev, addr, vma->vm_end, 11001da177e4SLinus Torvalds vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) { 11011da177e4SLinus Torvalds file = vma->vm_file; 11021da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 11031da177e4SLinus Torvalds if (correct_wcount) 11041da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 11051da177e4SLinus Torvalds } else { 11061da177e4SLinus Torvalds if (file) { 11071da177e4SLinus Torvalds if (correct_wcount) 11081da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 11091da177e4SLinus Torvalds fput(file); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds mpol_free(vma_policy(vma)); 11121da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds out: 11151da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 11161da177e4SLinus Torvalds __vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT); 11171da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 11181da177e4SLinus Torvalds mm->locked_vm += len >> PAGE_SHIFT; 11191da177e4SLinus Torvalds make_pages_present(addr, addr + len); 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds if (flags & MAP_POPULATE) { 11221da177e4SLinus Torvalds up_write(&mm->mmap_sem); 11231da177e4SLinus Torvalds sys_remap_file_pages(addr, len, 0, 11241da177e4SLinus Torvalds pgoff, flags & MAP_NONBLOCK); 11251da177e4SLinus Torvalds down_write(&mm->mmap_sem); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds return addr; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds unmap_and_free_vma: 11301da177e4SLinus Torvalds if (correct_wcount) 11311da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 11321da177e4SLinus Torvalds vma->vm_file = NULL; 11331da177e4SLinus Torvalds fput(file); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds /* Undo any partial mapping done by a device driver. */ 1136e0da382cSHugh Dickins unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); 1137e0da382cSHugh Dickins charged = 0; 11381da177e4SLinus Torvalds free_vma: 11391da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 11401da177e4SLinus Torvalds unacct_error: 11411da177e4SLinus Torvalds if (charged) 11421da177e4SLinus Torvalds vm_unacct_memory(charged); 11431da177e4SLinus Torvalds return error; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds EXPORT_SYMBOL(do_mmap_pgoff); 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 11491da177e4SLinus Torvalds * For shmat() with addr=0. 11501da177e4SLinus Torvalds * 11511da177e4SLinus Torvalds * Ugly calling convention alert: 11521da177e4SLinus Torvalds * Return value with the low bits set means error value, 11531da177e4SLinus Torvalds * ie 11541da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 11551da177e4SLinus Torvalds * error = ret; 11561da177e4SLinus Torvalds * 11571da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 11581da177e4SLinus Torvalds */ 11591da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA 11601da177e4SLinus Torvalds unsigned long 11611da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr, 11621da177e4SLinus Torvalds unsigned long len, unsigned long pgoff, unsigned long flags) 11631da177e4SLinus Torvalds { 11641da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 11651da177e4SLinus Torvalds struct vm_area_struct *vma; 11661da177e4SLinus Torvalds unsigned long start_addr; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds if (len > TASK_SIZE) 11691da177e4SLinus Torvalds return -ENOMEM; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds if (addr) { 11721da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 11731da177e4SLinus Torvalds vma = find_vma(mm, addr); 11741da177e4SLinus Torvalds if (TASK_SIZE - len >= addr && 11751da177e4SLinus Torvalds (!vma || addr + len <= vma->vm_start)) 11761da177e4SLinus Torvalds return addr; 11771da177e4SLinus Torvalds } 11781363c3cdSWolfgang Wander if (len > mm->cached_hole_size) { 11791da177e4SLinus Torvalds start_addr = addr = mm->free_area_cache; 11801363c3cdSWolfgang Wander } else { 11811363c3cdSWolfgang Wander start_addr = addr = TASK_UNMAPPED_BASE; 11821363c3cdSWolfgang Wander mm->cached_hole_size = 0; 11831363c3cdSWolfgang Wander } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds full_search: 11861da177e4SLinus Torvalds for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { 11871da177e4SLinus Torvalds /* At this point: (!vma || addr < vma->vm_end). */ 11881da177e4SLinus Torvalds if (TASK_SIZE - len < addr) { 11891da177e4SLinus Torvalds /* 11901da177e4SLinus Torvalds * Start a new search - just in case we missed 11911da177e4SLinus Torvalds * some holes. 11921da177e4SLinus Torvalds */ 11931da177e4SLinus Torvalds if (start_addr != TASK_UNMAPPED_BASE) { 11941363c3cdSWolfgang Wander addr = TASK_UNMAPPED_BASE; 11951363c3cdSWolfgang Wander start_addr = addr; 11961363c3cdSWolfgang Wander mm->cached_hole_size = 0; 11971da177e4SLinus Torvalds goto full_search; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds return -ENOMEM; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds if (!vma || addr + len <= vma->vm_start) { 12021da177e4SLinus Torvalds /* 12031da177e4SLinus Torvalds * Remember the place where we stopped the search: 12041da177e4SLinus Torvalds */ 12051da177e4SLinus Torvalds mm->free_area_cache = addr + len; 12061da177e4SLinus Torvalds return addr; 12071da177e4SLinus Torvalds } 12081363c3cdSWolfgang Wander if (addr + mm->cached_hole_size < vma->vm_start) 12091363c3cdSWolfgang Wander mm->cached_hole_size = vma->vm_start - addr; 12101da177e4SLinus Torvalds addr = vma->vm_end; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds #endif 12141da177e4SLinus Torvalds 12151363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds /* 12181da177e4SLinus Torvalds * Is this a new hole at the lowest possible address? 12191da177e4SLinus Torvalds */ 12201363c3cdSWolfgang Wander if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) { 12211363c3cdSWolfgang Wander mm->free_area_cache = addr; 12221363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 12231363c3cdSWolfgang Wander } 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds /* 12271da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 12281da177e4SLinus Torvalds * stack's low limit (the base): 12291da177e4SLinus Torvalds */ 12301da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 12311da177e4SLinus Torvalds unsigned long 12321da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, 12331da177e4SLinus Torvalds const unsigned long len, const unsigned long pgoff, 12341da177e4SLinus Torvalds const unsigned long flags) 12351da177e4SLinus Torvalds { 12361da177e4SLinus Torvalds struct vm_area_struct *vma; 12371da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 12381da177e4SLinus Torvalds unsigned long addr = addr0; 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /* requested length too big for entire address space */ 12411da177e4SLinus Torvalds if (len > TASK_SIZE) 12421da177e4SLinus Torvalds return -ENOMEM; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds /* requesting a specific address */ 12451da177e4SLinus Torvalds if (addr) { 12461da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 12471da177e4SLinus Torvalds vma = find_vma(mm, addr); 12481da177e4SLinus Torvalds if (TASK_SIZE - len >= addr && 12491da177e4SLinus Torvalds (!vma || addr + len <= vma->vm_start)) 12501da177e4SLinus Torvalds return addr; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531363c3cdSWolfgang Wander /* check if free_area_cache is useful for us */ 12541363c3cdSWolfgang Wander if (len <= mm->cached_hole_size) { 12551363c3cdSWolfgang Wander mm->cached_hole_size = 0; 12561363c3cdSWolfgang Wander mm->free_area_cache = mm->mmap_base; 12571363c3cdSWolfgang Wander } 12581363c3cdSWolfgang Wander 12591da177e4SLinus Torvalds /* either no address requested or can't fit in requested address hole */ 12601da177e4SLinus Torvalds addr = mm->free_area_cache; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* make sure it can fit in the remaining address space */ 126349a43876SLinus Torvalds if (addr > len) { 12641da177e4SLinus Torvalds vma = find_vma(mm, addr-len); 12651da177e4SLinus Torvalds if (!vma || addr <= vma->vm_start) 12661da177e4SLinus Torvalds /* remember the address as a hint for next time */ 12671da177e4SLinus Torvalds return (mm->free_area_cache = addr-len); 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 1270*73219d17SChris Wright if (mm->mmap_base < len) 1271*73219d17SChris Wright goto bottomup; 1272*73219d17SChris Wright 12731da177e4SLinus Torvalds addr = mm->mmap_base-len; 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds do { 12761da177e4SLinus Torvalds /* 12771da177e4SLinus Torvalds * Lookup failure means no vma is above this address, 12781da177e4SLinus Torvalds * else if new region fits below vma->vm_start, 12791da177e4SLinus Torvalds * return with success: 12801da177e4SLinus Torvalds */ 12811da177e4SLinus Torvalds vma = find_vma(mm, addr); 12821da177e4SLinus Torvalds if (!vma || addr+len <= vma->vm_start) 12831da177e4SLinus Torvalds /* remember the address as a hint for next time */ 12841da177e4SLinus Torvalds return (mm->free_area_cache = addr); 12851da177e4SLinus Torvalds 12861363c3cdSWolfgang Wander /* remember the largest hole we saw so far */ 12871363c3cdSWolfgang Wander if (addr + mm->cached_hole_size < vma->vm_start) 12881363c3cdSWolfgang Wander mm->cached_hole_size = vma->vm_start - addr; 12891363c3cdSWolfgang Wander 12901da177e4SLinus Torvalds /* try just below the current vma->vm_start */ 12911da177e4SLinus Torvalds addr = vma->vm_start-len; 129249a43876SLinus Torvalds } while (len < vma->vm_start); 12931da177e4SLinus Torvalds 1294*73219d17SChris Wright bottomup: 12951da177e4SLinus Torvalds /* 12961da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 12971da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 12981da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 12991da177e4SLinus Torvalds * allocations. 13001da177e4SLinus Torvalds */ 13011363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 13021da177e4SLinus Torvalds mm->free_area_cache = TASK_UNMAPPED_BASE; 13031da177e4SLinus Torvalds addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); 13041da177e4SLinus Torvalds /* 13051da177e4SLinus Torvalds * Restore the topdown base: 13061da177e4SLinus Torvalds */ 13071da177e4SLinus Torvalds mm->free_area_cache = mm->mmap_base; 13081363c3cdSWolfgang Wander mm->cached_hole_size = ~0UL; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds return addr; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds #endif 13131da177e4SLinus Torvalds 13141363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr) 13151da177e4SLinus Torvalds { 13161da177e4SLinus Torvalds /* 13171da177e4SLinus Torvalds * Is this a new hole at the highest possible address? 13181da177e4SLinus Torvalds */ 13191363c3cdSWolfgang Wander if (addr > mm->free_area_cache) 13201363c3cdSWolfgang Wander mm->free_area_cache = addr; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* dont allow allocations above current base */ 13231363c3cdSWolfgang Wander if (mm->free_area_cache > mm->mmap_base) 13241363c3cdSWolfgang Wander mm->free_area_cache = mm->mmap_base; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds unsigned long 13281da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 13291da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds unsigned long ret; 13321da177e4SLinus Torvalds 133307ab67c8SLinus Torvalds if (!(flags & MAP_FIXED)) { 133407ab67c8SLinus Torvalds unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); 133507ab67c8SLinus Torvalds 133607ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 133707ab67c8SLinus Torvalds if (file && file->f_op && file->f_op->get_unmapped_area) 133807ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 133907ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 134007ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 134107ab67c8SLinus Torvalds return addr; 134207ab67c8SLinus Torvalds } 134307ab67c8SLinus Torvalds 13441da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 13451da177e4SLinus Torvalds return -ENOMEM; 13461da177e4SLinus Torvalds if (addr & ~PAGE_MASK) 13471da177e4SLinus Torvalds return -EINVAL; 13481da177e4SLinus Torvalds if (file && is_file_hugepages(file)) { 13491da177e4SLinus Torvalds /* 13501da177e4SLinus Torvalds * Check if the given range is hugepage aligned, and 13511da177e4SLinus Torvalds * can be made suitable for hugepages. 13521da177e4SLinus Torvalds */ 13531da177e4SLinus Torvalds ret = prepare_hugepage_range(addr, len); 13541da177e4SLinus Torvalds } else { 13551da177e4SLinus Torvalds /* 13561da177e4SLinus Torvalds * Ensure that a normal request is not falling in a 13571da177e4SLinus Torvalds * reserved hugepage range. For some archs like IA-64, 13581da177e4SLinus Torvalds * there is a separate region for hugepages. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds ret = is_hugepage_only_range(current->mm, addr, len); 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds if (ret) 13631da177e4SLinus Torvalds return -EINVAL; 13641da177e4SLinus Torvalds return addr; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ 13701da177e4SLinus Torvalds struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr) 13711da177e4SLinus Torvalds { 13721da177e4SLinus Torvalds struct vm_area_struct *vma = NULL; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds if (mm) { 13751da177e4SLinus Torvalds /* Check the cache first. */ 13761da177e4SLinus Torvalds /* (Cache hit rate is typically around 35%.) */ 13771da177e4SLinus Torvalds vma = mm->mmap_cache; 13781da177e4SLinus Torvalds if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) { 13791da177e4SLinus Torvalds struct rb_node * rb_node; 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds rb_node = mm->mm_rb.rb_node; 13821da177e4SLinus Torvalds vma = NULL; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds while (rb_node) { 13851da177e4SLinus Torvalds struct vm_area_struct * vma_tmp; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds vma_tmp = rb_entry(rb_node, 13881da177e4SLinus Torvalds struct vm_area_struct, vm_rb); 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds if (vma_tmp->vm_end > addr) { 13911da177e4SLinus Torvalds vma = vma_tmp; 13921da177e4SLinus Torvalds if (vma_tmp->vm_start <= addr) 13931da177e4SLinus Torvalds break; 13941da177e4SLinus Torvalds rb_node = rb_node->rb_left; 13951da177e4SLinus Torvalds } else 13961da177e4SLinus Torvalds rb_node = rb_node->rb_right; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds if (vma) 13991da177e4SLinus Torvalds mm->mmap_cache = vma; 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds return vma; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */ 14081da177e4SLinus Torvalds struct vm_area_struct * 14091da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 14101da177e4SLinus Torvalds struct vm_area_struct **pprev) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds struct vm_area_struct *vma = NULL, *prev = NULL; 14131da177e4SLinus Torvalds struct rb_node * rb_node; 14141da177e4SLinus Torvalds if (!mm) 14151da177e4SLinus Torvalds goto out; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds /* Guard against addr being lower than the first VMA */ 14181da177e4SLinus Torvalds vma = mm->mmap; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds /* Go through the RB tree quickly. */ 14211da177e4SLinus Torvalds rb_node = mm->mm_rb.rb_node; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds while (rb_node) { 14241da177e4SLinus Torvalds struct vm_area_struct *vma_tmp; 14251da177e4SLinus Torvalds vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb); 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds if (addr < vma_tmp->vm_end) { 14281da177e4SLinus Torvalds rb_node = rb_node->rb_left; 14291da177e4SLinus Torvalds } else { 14301da177e4SLinus Torvalds prev = vma_tmp; 14311da177e4SLinus Torvalds if (!prev->vm_next || (addr < prev->vm_next->vm_end)) 14321da177e4SLinus Torvalds break; 14331da177e4SLinus Torvalds rb_node = rb_node->rb_right; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds out: 14381da177e4SLinus Torvalds *pprev = prev; 14391da177e4SLinus Torvalds return prev ? prev->vm_next : vma; 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds /* 14431da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 14441da177e4SLinus Torvalds * update accounting. This is shared with both the 14451da177e4SLinus Torvalds * grow-up and grow-down cases. 14461da177e4SLinus Torvalds */ 14471da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow) 14481da177e4SLinus Torvalds { 14491da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 14501da177e4SLinus Torvalds struct rlimit *rlim = current->signal->rlim; 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds /* address space limit tests */ 1453119f657cSakpm@osdl.org if (!may_expand_vm(mm, grow)) 14541da177e4SLinus Torvalds return -ENOMEM; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds /* Stack limit test */ 14571da177e4SLinus Torvalds if (size > rlim[RLIMIT_STACK].rlim_cur) 14581da177e4SLinus Torvalds return -ENOMEM; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds /* mlock limit tests */ 14611da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) { 14621da177e4SLinus Torvalds unsigned long locked; 14631da177e4SLinus Torvalds unsigned long limit; 14641da177e4SLinus Torvalds locked = mm->locked_vm + grow; 14651da177e4SLinus Torvalds limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT; 14661da177e4SLinus Torvalds if (locked > limit && !capable(CAP_IPC_LOCK)) 14671da177e4SLinus Torvalds return -ENOMEM; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds /* 14711da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 14721da177e4SLinus Torvalds * update security statistics. 14731da177e4SLinus Torvalds */ 14741da177e4SLinus Torvalds if (security_vm_enough_memory(grow)) 14751da177e4SLinus Torvalds return -ENOMEM; 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds /* Ok, everything looks good - let it rip */ 14781da177e4SLinus Torvalds mm->total_vm += grow; 14791da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) 14801da177e4SLinus Torvalds mm->locked_vm += grow; 14811da177e4SLinus Torvalds __vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow); 14821da177e4SLinus Torvalds return 0; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds #ifdef CONFIG_STACK_GROWSUP 14861da177e4SLinus Torvalds /* 14871da177e4SLinus Torvalds * vma is the first one with address > vma->vm_end. Have to extend vma. 14881da177e4SLinus Torvalds */ 14891da177e4SLinus Torvalds int expand_stack(struct vm_area_struct * vma, unsigned long address) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds int error; 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 14941da177e4SLinus Torvalds return -EFAULT; 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds /* 14971da177e4SLinus Torvalds * We must make sure the anon_vma is allocated 14981da177e4SLinus Torvalds * so that the anon_vma locking is not a noop. 14991da177e4SLinus Torvalds */ 15001da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 15011da177e4SLinus Torvalds return -ENOMEM; 15021da177e4SLinus Torvalds anon_vma_lock(vma); 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* 15051da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 15061da177e4SLinus Torvalds * is required to hold the mmap_sem in read mode. We need the 15071da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 15081da177e4SLinus Torvalds */ 15091da177e4SLinus Torvalds address += 4 + PAGE_SIZE - 1; 15101da177e4SLinus Torvalds address &= PAGE_MASK; 15111da177e4SLinus Torvalds error = 0; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 15141da177e4SLinus Torvalds if (address > vma->vm_end) { 15151da177e4SLinus Torvalds unsigned long size, grow; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds size = address - vma->vm_start; 15181da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 15211da177e4SLinus Torvalds if (!error) 15221da177e4SLinus Torvalds vma->vm_end = address; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds anon_vma_unlock(vma); 15251da177e4SLinus Torvalds return error; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds struct vm_area_struct * 15291da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 15301da177e4SLinus Torvalds { 15311da177e4SLinus Torvalds struct vm_area_struct *vma, *prev; 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds addr &= PAGE_MASK; 15341da177e4SLinus Torvalds vma = find_vma_prev(mm, addr, &prev); 15351da177e4SLinus Torvalds if (vma && (vma->vm_start <= addr)) 15361da177e4SLinus Torvalds return vma; 15371da177e4SLinus Torvalds if (!prev || expand_stack(prev, addr)) 15381da177e4SLinus Torvalds return NULL; 15391da177e4SLinus Torvalds if (prev->vm_flags & VM_LOCKED) { 15401da177e4SLinus Torvalds make_pages_present(addr, prev->vm_end); 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds return prev; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds #else 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 15471da177e4SLinus Torvalds */ 15481da177e4SLinus Torvalds int expand_stack(struct vm_area_struct *vma, unsigned long address) 15491da177e4SLinus Torvalds { 15501da177e4SLinus Torvalds int error; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds /* 15531da177e4SLinus Torvalds * We must make sure the anon_vma is allocated 15541da177e4SLinus Torvalds * so that the anon_vma locking is not a noop. 15551da177e4SLinus Torvalds */ 15561da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 15571da177e4SLinus Torvalds return -ENOMEM; 15581da177e4SLinus Torvalds anon_vma_lock(vma); 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds /* 15611da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 15621da177e4SLinus Torvalds * is required to hold the mmap_sem in read mode. We need the 15631da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 15641da177e4SLinus Torvalds */ 15651da177e4SLinus Torvalds address &= PAGE_MASK; 15661da177e4SLinus Torvalds error = 0; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 15691da177e4SLinus Torvalds if (address < vma->vm_start) { 15701da177e4SLinus Torvalds unsigned long size, grow; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds size = vma->vm_end - address; 15731da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 15761da177e4SLinus Torvalds if (!error) { 15771da177e4SLinus Torvalds vma->vm_start = address; 15781da177e4SLinus Torvalds vma->vm_pgoff -= grow; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds anon_vma_unlock(vma); 15821da177e4SLinus Torvalds return error; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds struct vm_area_struct * 15861da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds struct vm_area_struct * vma; 15891da177e4SLinus Torvalds unsigned long start; 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds addr &= PAGE_MASK; 15921da177e4SLinus Torvalds vma = find_vma(mm,addr); 15931da177e4SLinus Torvalds if (!vma) 15941da177e4SLinus Torvalds return NULL; 15951da177e4SLinus Torvalds if (vma->vm_start <= addr) 15961da177e4SLinus Torvalds return vma; 15971da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 15981da177e4SLinus Torvalds return NULL; 15991da177e4SLinus Torvalds start = vma->vm_start; 16001da177e4SLinus Torvalds if (expand_stack(vma, addr)) 16011da177e4SLinus Torvalds return NULL; 16021da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) { 16031da177e4SLinus Torvalds make_pages_present(addr, start); 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds return vma; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds #endif 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds /* Normal function to fix up a mapping 16101da177e4SLinus Torvalds * This function is the default for when an area has no specific 16111da177e4SLinus Torvalds * function. This may be used as part of a more specific routine. 16121da177e4SLinus Torvalds * 16131da177e4SLinus Torvalds * By the time this function is called, the area struct has been 16141da177e4SLinus Torvalds * removed from the process mapping list. 16151da177e4SLinus Torvalds */ 16161da177e4SLinus Torvalds static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area) 16171da177e4SLinus Torvalds { 16181da177e4SLinus Torvalds size_t len = area->vm_end - area->vm_start; 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds area->vm_mm->total_vm -= len >> PAGE_SHIFT; 16211da177e4SLinus Torvalds if (area->vm_flags & VM_LOCKED) 16221da177e4SLinus Torvalds area->vm_mm->locked_vm -= len >> PAGE_SHIFT; 16231da177e4SLinus Torvalds vm_stat_unaccount(area); 16241da177e4SLinus Torvalds remove_vm_struct(area); 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds /* 16281da177e4SLinus Torvalds * Update the VMA and inode share lists. 16291da177e4SLinus Torvalds * 16301da177e4SLinus Torvalds * Ok - we have the memory areas we should free on the 'free' list, 16311da177e4SLinus Torvalds * so release them, and do the vma updates. 16321da177e4SLinus Torvalds */ 1633146425a3SHugh Dickins static void unmap_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) 16341da177e4SLinus Torvalds { 16351da177e4SLinus Torvalds do { 1636146425a3SHugh Dickins struct vm_area_struct *next = vma->vm_next; 1637146425a3SHugh Dickins unmap_vma(mm, vma); 1638146425a3SHugh Dickins vma = next; 1639146425a3SHugh Dickins } while (vma); 16401da177e4SLinus Torvalds validate_mm(mm); 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds /* 16441da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 16451da177e4SLinus Torvalds * 16461da177e4SLinus Torvalds * Called with the page table lock held. 16471da177e4SLinus Torvalds */ 16481da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm, 1649e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 1650e0da382cSHugh Dickins unsigned long start, unsigned long end) 16511da177e4SLinus Torvalds { 1652e0da382cSHugh Dickins struct vm_area_struct *next = prev? prev->vm_next: mm->mmap; 16531da177e4SLinus Torvalds struct mmu_gather *tlb; 16541da177e4SLinus Torvalds unsigned long nr_accounted = 0; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds lru_add_drain(); 1657e0da382cSHugh Dickins spin_lock(&mm->page_table_lock); 16581da177e4SLinus Torvalds tlb = tlb_gather_mmu(mm, 0); 16591da177e4SLinus Torvalds unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL); 16601da177e4SLinus Torvalds vm_unacct_memory(nr_accounted); 1661e2cdef8cSHugh Dickins free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS, 1662e0da382cSHugh Dickins next? next->vm_start: 0); 16631da177e4SLinus Torvalds tlb_finish_mmu(tlb, start, end); 1664e0da382cSHugh Dickins spin_unlock(&mm->page_table_lock); 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds /* 16681da177e4SLinus Torvalds * Create a list of vma's touched by the unmap, removing them from the mm's 16691da177e4SLinus Torvalds * vma list as we go.. 16701da177e4SLinus Torvalds */ 16711da177e4SLinus Torvalds static void 16721da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, 16731da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long end) 16741da177e4SLinus Torvalds { 16751da177e4SLinus Torvalds struct vm_area_struct **insertion_point; 16761da177e4SLinus Torvalds struct vm_area_struct *tail_vma = NULL; 16771363c3cdSWolfgang Wander unsigned long addr; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds insertion_point = (prev ? &prev->vm_next : &mm->mmap); 16801da177e4SLinus Torvalds do { 16811da177e4SLinus Torvalds rb_erase(&vma->vm_rb, &mm->mm_rb); 16821da177e4SLinus Torvalds mm->map_count--; 16831da177e4SLinus Torvalds tail_vma = vma; 16841da177e4SLinus Torvalds vma = vma->vm_next; 16851da177e4SLinus Torvalds } while (vma && vma->vm_start < end); 16861da177e4SLinus Torvalds *insertion_point = vma; 16871da177e4SLinus Torvalds tail_vma->vm_next = NULL; 16881363c3cdSWolfgang Wander if (mm->unmap_area == arch_unmap_area) 16891363c3cdSWolfgang Wander addr = prev ? prev->vm_end : mm->mmap_base; 16901363c3cdSWolfgang Wander else 16911363c3cdSWolfgang Wander addr = vma ? vma->vm_start : mm->mmap_base; 16921363c3cdSWolfgang Wander mm->unmap_area(mm, addr); 16931da177e4SLinus Torvalds mm->mmap_cache = NULL; /* Kill the cache. */ 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds /* 16971da177e4SLinus Torvalds * Split a vma into two pieces at address 'addr', a new vma is allocated 16981da177e4SLinus Torvalds * either for the first part or the the tail. 16991da177e4SLinus Torvalds */ 17001da177e4SLinus Torvalds int split_vma(struct mm_struct * mm, struct vm_area_struct * vma, 17011da177e4SLinus Torvalds unsigned long addr, int new_below) 17021da177e4SLinus Torvalds { 17031da177e4SLinus Torvalds struct mempolicy *pol; 17041da177e4SLinus Torvalds struct vm_area_struct *new; 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds if (is_vm_hugetlb_page(vma) && (addr & ~HPAGE_MASK)) 17071da177e4SLinus Torvalds return -EINVAL; 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds if (mm->map_count >= sysctl_max_map_count) 17101da177e4SLinus Torvalds return -ENOMEM; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); 17131da177e4SLinus Torvalds if (!new) 17141da177e4SLinus Torvalds return -ENOMEM; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* most fields are the same, copy all, and then fixup */ 17171da177e4SLinus Torvalds *new = *vma; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds if (new_below) 17201da177e4SLinus Torvalds new->vm_end = addr; 17211da177e4SLinus Torvalds else { 17221da177e4SLinus Torvalds new->vm_start = addr; 17231da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds pol = mpol_copy(vma_policy(vma)); 17271da177e4SLinus Torvalds if (IS_ERR(pol)) { 17281da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, new); 17291da177e4SLinus Torvalds return PTR_ERR(pol); 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds vma_set_policy(new, pol); 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds if (new->vm_file) 17341da177e4SLinus Torvalds get_file(new->vm_file); 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 17371da177e4SLinus Torvalds new->vm_ops->open(new); 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds if (new_below) 17401da177e4SLinus Torvalds vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + 17411da177e4SLinus Torvalds ((addr - new->vm_start) >> PAGE_SHIFT), new); 17421da177e4SLinus Torvalds else 17431da177e4SLinus Torvalds vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds return 0; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds 17491da177e4SLinus Torvalds * what needs doing, and the areas themselves, which do the 17501da177e4SLinus Torvalds * work. This now handles partial unmappings. 17511da177e4SLinus Torvalds * Jeremy Fitzhardinge <jeremy@goop.org> 17521da177e4SLinus Torvalds */ 17531da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 17541da177e4SLinus Torvalds { 17551da177e4SLinus Torvalds unsigned long end; 1756146425a3SHugh Dickins struct vm_area_struct *vma, *prev, *last; 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start) 17591da177e4SLinus Torvalds return -EINVAL; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds if ((len = PAGE_ALIGN(len)) == 0) 17621da177e4SLinus Torvalds return -EINVAL; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds /* Find the first overlapping VMA */ 1765146425a3SHugh Dickins vma = find_vma_prev(mm, start, &prev); 1766146425a3SHugh Dickins if (!vma) 17671da177e4SLinus Torvalds return 0; 1768146425a3SHugh Dickins /* we have start < vma->vm_end */ 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds /* if it doesn't overlap, we have nothing.. */ 17711da177e4SLinus Torvalds end = start + len; 1772146425a3SHugh Dickins if (vma->vm_start >= end) 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds /* 17761da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 17771da177e4SLinus Torvalds * 17781da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 17791da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 17801da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 17811da177e4SLinus Torvalds */ 1782146425a3SHugh Dickins if (start > vma->vm_start) { 1783146425a3SHugh Dickins int error = split_vma(mm, vma, start, 0); 17841da177e4SLinus Torvalds if (error) 17851da177e4SLinus Torvalds return error; 1786146425a3SHugh Dickins prev = vma; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /* Does it split the last one? */ 17901da177e4SLinus Torvalds last = find_vma(mm, end); 17911da177e4SLinus Torvalds if (last && end > last->vm_start) { 17921da177e4SLinus Torvalds int error = split_vma(mm, last, end, 1); 17931da177e4SLinus Torvalds if (error) 17941da177e4SLinus Torvalds return error; 17951da177e4SLinus Torvalds } 1796146425a3SHugh Dickins vma = prev? prev->vm_next: mm->mmap; 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds /* 17991da177e4SLinus Torvalds * Remove the vma's, and unmap the actual pages 18001da177e4SLinus Torvalds */ 1801146425a3SHugh Dickins detach_vmas_to_be_unmapped(mm, vma, prev, end); 1802146425a3SHugh Dickins unmap_region(mm, vma, prev, start, end); 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* Fix up all other VM information */ 1805146425a3SHugh Dickins unmap_vma_list(mm, vma); 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds return 0; 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds EXPORT_SYMBOL(do_munmap); 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds asmlinkage long sys_munmap(unsigned long addr, size_t len) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds int ret; 18151da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds profile_munmap(addr); 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds down_write(&mm->mmap_sem); 18201da177e4SLinus Torvalds ret = do_munmap(mm, addr, len); 18211da177e4SLinus Torvalds up_write(&mm->mmap_sem); 18221da177e4SLinus Torvalds return ret; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm) 18261da177e4SLinus Torvalds { 18271da177e4SLinus Torvalds #ifdef CONFIG_DEBUG_KERNEL 18281da177e4SLinus Torvalds if (unlikely(down_read_trylock(&mm->mmap_sem))) { 18291da177e4SLinus Torvalds WARN_ON(1); 18301da177e4SLinus Torvalds up_read(&mm->mmap_sem); 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds #endif 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds /* 18361da177e4SLinus Torvalds * this is really a simplified "do_mmap". it only handles 18371da177e4SLinus Torvalds * anonymous maps. eventually we may be able to do some 18381da177e4SLinus Torvalds * brk-specific accounting here. 18391da177e4SLinus Torvalds */ 18401da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len) 18411da177e4SLinus Torvalds { 18421da177e4SLinus Torvalds struct mm_struct * mm = current->mm; 18431da177e4SLinus Torvalds struct vm_area_struct * vma, * prev; 18441da177e4SLinus Torvalds unsigned long flags; 18451da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 18461da177e4SLinus Torvalds pgoff_t pgoff = addr >> PAGE_SHIFT; 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds len = PAGE_ALIGN(len); 18491da177e4SLinus Torvalds if (!len) 18501da177e4SLinus Torvalds return addr; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds if ((addr + len) > TASK_SIZE || (addr + len) < addr) 18531da177e4SLinus Torvalds return -EINVAL; 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds /* 18561da177e4SLinus Torvalds * mlock MCL_FUTURE? 18571da177e4SLinus Torvalds */ 18581da177e4SLinus Torvalds if (mm->def_flags & VM_LOCKED) { 18591da177e4SLinus Torvalds unsigned long locked, lock_limit; 186093ea1d0aSChris Wright locked = len >> PAGE_SHIFT; 186193ea1d0aSChris Wright locked += mm->locked_vm; 18621da177e4SLinus Torvalds lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; 186393ea1d0aSChris Wright lock_limit >>= PAGE_SHIFT; 18641da177e4SLinus Torvalds if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 18651da177e4SLinus Torvalds return -EAGAIN; 18661da177e4SLinus Torvalds } 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds /* 18691da177e4SLinus Torvalds * mm->mmap_sem is required to protect against another thread 18701da177e4SLinus Torvalds * changing the mappings in case we sleep. 18711da177e4SLinus Torvalds */ 18721da177e4SLinus Torvalds verify_mm_writelocked(mm); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds /* 18751da177e4SLinus Torvalds * Clear old maps. this also does some error checking for us 18761da177e4SLinus Torvalds */ 18771da177e4SLinus Torvalds munmap_back: 18781da177e4SLinus Torvalds vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 18791da177e4SLinus Torvalds if (vma && vma->vm_start < addr + len) { 18801da177e4SLinus Torvalds if (do_munmap(mm, addr, len)) 18811da177e4SLinus Torvalds return -ENOMEM; 18821da177e4SLinus Torvalds goto munmap_back; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds /* Check against address space limits *after* clearing old maps... */ 1886119f657cSakpm@osdl.org if (!may_expand_vm(mm, len >> PAGE_SHIFT)) 18871da177e4SLinus Torvalds return -ENOMEM; 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 18901da177e4SLinus Torvalds return -ENOMEM; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds if (security_vm_enough_memory(len >> PAGE_SHIFT)) 18931da177e4SLinus Torvalds return -ENOMEM; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds /* Can we just expand an old private anonymous mapping? */ 18981da177e4SLinus Torvalds if (vma_merge(mm, prev, addr, addr + len, flags, 18991da177e4SLinus Torvalds NULL, NULL, pgoff, NULL)) 19001da177e4SLinus Torvalds goto out; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /* 19031da177e4SLinus Torvalds * create a vma struct for an anonymous mapping 19041da177e4SLinus Torvalds */ 19051da177e4SLinus Torvalds vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); 19061da177e4SLinus Torvalds if (!vma) { 19071da177e4SLinus Torvalds vm_unacct_memory(len >> PAGE_SHIFT); 19081da177e4SLinus Torvalds return -ENOMEM; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds memset(vma, 0, sizeof(*vma)); 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds vma->vm_mm = mm; 19131da177e4SLinus Torvalds vma->vm_start = addr; 19141da177e4SLinus Torvalds vma->vm_end = addr + len; 19151da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 19161da177e4SLinus Torvalds vma->vm_flags = flags; 19171da177e4SLinus Torvalds vma->vm_page_prot = protection_map[flags & 0x0f]; 19181da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 19191da177e4SLinus Torvalds out: 19201da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 19211da177e4SLinus Torvalds if (flags & VM_LOCKED) { 19221da177e4SLinus Torvalds mm->locked_vm += len >> PAGE_SHIFT; 19231da177e4SLinus Torvalds make_pages_present(addr, addr + len); 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds return addr; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds EXPORT_SYMBOL(do_brk); 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds /* Release all mmaps. */ 19311da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 19321da177e4SLinus Torvalds { 19331da177e4SLinus Torvalds struct mmu_gather *tlb; 1934e0da382cSHugh Dickins struct vm_area_struct *vma = mm->mmap; 19351da177e4SLinus Torvalds unsigned long nr_accounted = 0; 1936ee39b37bSHugh Dickins unsigned long end; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds lru_add_drain(); 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds spin_lock(&mm->page_table_lock); 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds flush_cache_mm(mm); 1943e0da382cSHugh Dickins tlb = tlb_gather_mmu(mm, 1); 1944e0da382cSHugh Dickins /* Use -1 here to ensure all VMAs in the mm are unmapped */ 1945ee39b37bSHugh Dickins end = unmap_vmas(&tlb, mm, vma, 0, -1, &nr_accounted, NULL); 19461da177e4SLinus Torvalds vm_unacct_memory(nr_accounted); 1947e2cdef8cSHugh Dickins free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0); 1948ee39b37bSHugh Dickins tlb_finish_mmu(tlb, 0, end); 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds mm->mmap = mm->mmap_cache = NULL; 19511da177e4SLinus Torvalds mm->mm_rb = RB_ROOT; 19521da177e4SLinus Torvalds set_mm_counter(mm, rss, 0); 19531da177e4SLinus Torvalds mm->total_vm = 0; 19541da177e4SLinus Torvalds mm->locked_vm = 0; 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds /* 19591da177e4SLinus Torvalds * Walk the list again, actually closing and freeing it 19601da177e4SLinus Torvalds * without holding any MM locks. 19611da177e4SLinus Torvalds */ 19621da177e4SLinus Torvalds while (vma) { 19631da177e4SLinus Torvalds struct vm_area_struct *next = vma->vm_next; 19641da177e4SLinus Torvalds remove_vm_struct(vma); 19651da177e4SLinus Torvalds vma = next; 19661da177e4SLinus Torvalds } 1967e0da382cSHugh Dickins 1968e2cdef8cSHugh Dickins BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT); 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 19721da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 19731da177e4SLinus Torvalds * then i_mmap_lock is taken here. 19741da177e4SLinus Torvalds */ 19751da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma) 19761da177e4SLinus Torvalds { 19771da177e4SLinus Torvalds struct vm_area_struct * __vma, * prev; 19781da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds /* 19811da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 19821da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 19831da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 19841da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 19851da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 19861da177e4SLinus Torvalds * 19871da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 19881da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 19891da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 19901da177e4SLinus Torvalds * Similarly in do_mmap_pgoff and in do_brk. 19911da177e4SLinus Torvalds */ 19921da177e4SLinus Torvalds if (!vma->vm_file) { 19931da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 19941da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent); 19971da177e4SLinus Torvalds if (__vma && __vma->vm_start < vma->vm_end) 19981da177e4SLinus Torvalds return -ENOMEM; 19991da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 20001da177e4SLinus Torvalds return 0; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds /* 20041da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 20051da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 20061da177e4SLinus Torvalds */ 20071da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 20081da177e4SLinus Torvalds unsigned long addr, unsigned long len, pgoff_t pgoff) 20091da177e4SLinus Torvalds { 20101da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 20111da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 20121da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 20131da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 20141da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 20151da177e4SLinus Torvalds struct mempolicy *pol; 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds /* 20181da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 20191da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 20201da177e4SLinus Torvalds */ 20211da177e4SLinus Torvalds if (!vma->vm_file && !vma->anon_vma) 20221da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 20251da177e4SLinus Torvalds new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, 20261da177e4SLinus Torvalds vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma)); 20271da177e4SLinus Torvalds if (new_vma) { 20281da177e4SLinus Torvalds /* 20291da177e4SLinus Torvalds * Source vma may have been merged into new_vma 20301da177e4SLinus Torvalds */ 20311da177e4SLinus Torvalds if (vma_start >= new_vma->vm_start && 20321da177e4SLinus Torvalds vma_start < new_vma->vm_end) 20331da177e4SLinus Torvalds *vmap = new_vma; 20341da177e4SLinus Torvalds } else { 20351da177e4SLinus Torvalds new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL); 20361da177e4SLinus Torvalds if (new_vma) { 20371da177e4SLinus Torvalds *new_vma = *vma; 20381da177e4SLinus Torvalds pol = mpol_copy(vma_policy(vma)); 20391da177e4SLinus Torvalds if (IS_ERR(pol)) { 20401da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, new_vma); 20411da177e4SLinus Torvalds return NULL; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds vma_set_policy(new_vma, pol); 20441da177e4SLinus Torvalds new_vma->vm_start = addr; 20451da177e4SLinus Torvalds new_vma->vm_end = addr + len; 20461da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 20471da177e4SLinus Torvalds if (new_vma->vm_file) 20481da177e4SLinus Torvalds get_file(new_vma->vm_file); 20491da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 20501da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 20511da177e4SLinus Torvalds vma_link(mm, new_vma, prev, rb_link, rb_parent); 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds return new_vma; 20551da177e4SLinus Torvalds } 2056119f657cSakpm@osdl.org 2057119f657cSakpm@osdl.org /* 2058119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 2059119f657cSakpm@osdl.org * number of pages 2060119f657cSakpm@osdl.org */ 2061119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages) 2062119f657cSakpm@osdl.org { 2063119f657cSakpm@osdl.org unsigned long cur = mm->total_vm; /* pages */ 2064119f657cSakpm@osdl.org unsigned long lim; 2065119f657cSakpm@osdl.org 2066119f657cSakpm@osdl.org lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT; 2067119f657cSakpm@osdl.org 2068119f657cSakpm@osdl.org if (cur + npages > lim) 2069119f657cSakpm@osdl.org return 0; 2070119f657cSakpm@osdl.org return 1; 2071119f657cSakpm@osdl.org } 2072