11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * mm/mmap.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Written by obz. 51da177e4SLinus Torvalds * 6046c6884SAlan Cox * Address space accounting code <alan@lxorguk.ukuu.org.uk> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/slab.h> 104af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 111da177e4SLinus Torvalds #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/shm.h> 131da177e4SLinus Torvalds #include <linux/mman.h> 141da177e4SLinus Torvalds #include <linux/pagemap.h> 151da177e4SLinus Torvalds #include <linux/swap.h> 161da177e4SLinus Torvalds #include <linux/syscalls.h> 17c59ede7bSRandy.Dunlap #include <linux/capability.h> 181da177e4SLinus Torvalds #include <linux/init.h> 191da177e4SLinus Torvalds #include <linux/file.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/personality.h> 221da177e4SLinus Torvalds #include <linux/security.h> 231da177e4SLinus Torvalds #include <linux/hugetlb.h> 241da177e4SLinus Torvalds #include <linux/profile.h> 25b95f1b31SPaul Gortmaker #include <linux/export.h> 261da177e4SLinus Torvalds #include <linux/mount.h> 271da177e4SLinus Torvalds #include <linux/mempolicy.h> 281da177e4SLinus Torvalds #include <linux/rmap.h> 29cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 30cdd6c482SIngo Molnar #include <linux/perf_event.h> 31120a795dSAl Viro #include <linux/audit.h> 32b15d00b6SAndrea Arcangeli #include <linux/khugepaged.h> 332b144498SSrikar Dronamraju #include <linux/uprobes.h> 34d3737187SMichel Lespinasse #include <linux/rbtree_augmented.h> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds #include <asm/cacheflush.h> 381da177e4SLinus Torvalds #include <asm/tlb.h> 39d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h> 401da177e4SLinus Torvalds 4142b77728SJan Beulich #include "internal.h" 4242b77728SJan Beulich 433a459756SKirill Korotaev #ifndef arch_mmap_check 443a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags) (0) 453a459756SKirill Korotaev #endif 463a459756SKirill Korotaev 4708e7d9b5SMartin Schwidefsky #ifndef arch_rebalance_pgtables 4808e7d9b5SMartin Schwidefsky #define arch_rebalance_pgtables(addr, len) (addr) 4908e7d9b5SMartin Schwidefsky #endif 5008e7d9b5SMartin Schwidefsky 51e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm, 52e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 53e0da382cSHugh Dickins unsigned long start, unsigned long end); 54e0da382cSHugh Dickins 551da177e4SLinus Torvalds /* description of effects of mapping type and prot in current implementation. 561da177e4SLinus Torvalds * this is due to the limited x86 page protection hardware. The expected 571da177e4SLinus Torvalds * behavior is in parens: 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * map_type prot 601da177e4SLinus Torvalds * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC 611da177e4SLinus Torvalds * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes 621da177e4SLinus Torvalds * w: (no) no w: (no) no w: (yes) yes w: (no) no 631da177e4SLinus Torvalds * x: (no) no x: (no) yes x: (no) yes x: (yes) yes 641da177e4SLinus Torvalds * 651da177e4SLinus Torvalds * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes 661da177e4SLinus Torvalds * w: (no) no w: (no) no w: (copy) copy w: (no) no 671da177e4SLinus Torvalds * x: (no) no x: (no) yes x: (no) yes x: (yes) yes 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds */ 701da177e4SLinus Torvalds pgprot_t protection_map[16] = { 711da177e4SLinus Torvalds __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111, 721da177e4SLinus Torvalds __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111 731da177e4SLinus Torvalds }; 741da177e4SLinus Torvalds 75804af2cfSHugh Dickins pgprot_t vm_get_page_prot(unsigned long vm_flags) 76804af2cfSHugh Dickins { 77b845f313SDave Kleikamp return __pgprot(pgprot_val(protection_map[vm_flags & 78b845f313SDave Kleikamp (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) | 79b845f313SDave Kleikamp pgprot_val(arch_vm_get_page_prot(vm_flags))); 80804af2cfSHugh Dickins } 81804af2cfSHugh Dickins EXPORT_SYMBOL(vm_get_page_prot); 82804af2cfSHugh Dickins 8334679d7eSShaohua Li int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */ 8434679d7eSShaohua Li int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */ 85c3d8c141SChristoph Lameter int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT; 8634679d7eSShaohua Li /* 8734679d7eSShaohua Li * Make sure vm_committed_as in one cacheline and not cacheline shared with 8834679d7eSShaohua Li * other variables. It can be updated by several CPUs frequently. 8934679d7eSShaohua Li */ 9034679d7eSShaohua Li struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* 931da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 941da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 951da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 961da177e4SLinus Torvalds * 971da177e4SLinus Torvalds * We currently support three overcommit policies, which are set via the 981da177e4SLinus Torvalds * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 991da177e4SLinus Torvalds * 1001da177e4SLinus Torvalds * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 1011da177e4SLinus Torvalds * Additional code 2002 Jul 20 by Robert Love. 1021da177e4SLinus Torvalds * 1031da177e4SLinus Torvalds * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 1041da177e4SLinus Torvalds * 1051da177e4SLinus Torvalds * Note this is a helper function intended to be used by LSMs which 1061da177e4SLinus Torvalds * wish to use this logic. 1071da177e4SLinus Torvalds */ 10834b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds unsigned long free, allowed; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds vm_acct_memory(pages); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* 1151da177e4SLinus Torvalds * Sometimes we want to use more memory than we have 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 1181da177e4SLinus Torvalds return 0; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 121c15bef30SDmitry Fink free = global_page_state(NR_FREE_PAGES); 122c15bef30SDmitry Fink free += global_page_state(NR_FILE_PAGES); 1231da177e4SLinus Torvalds 124c15bef30SDmitry Fink /* 125c15bef30SDmitry Fink * shmem pages shouldn't be counted as free in this 126c15bef30SDmitry Fink * case, they can't be purged, only swapped out, and 127c15bef30SDmitry Fink * that won't affect the overall amount of available 128c15bef30SDmitry Fink * memory in the system. 129c15bef30SDmitry Fink */ 130c15bef30SDmitry Fink free -= global_page_state(NR_SHMEM); 131c15bef30SDmitry Fink 1321da177e4SLinus Torvalds free += nr_swap_pages; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * Any slabs which are created with the 1361da177e4SLinus Torvalds * SLAB_RECLAIM_ACCOUNT flag claim to have contents 1371da177e4SLinus Torvalds * which are reclaimable, under pressure. The dentry 1381da177e4SLinus Torvalds * cache and most inode caches should fall into this 1391da177e4SLinus Torvalds */ 140972d1a7bSChristoph Lameter free += global_page_state(NR_SLAB_RECLAIMABLE); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* 143c15bef30SDmitry Fink * Leave reserved pages. The pages are not for anonymous pages. 144c15bef30SDmitry Fink */ 145c15bef30SDmitry Fink if (free <= totalreserve_pages) 146c15bef30SDmitry Fink goto error; 147c15bef30SDmitry Fink else 148c15bef30SDmitry Fink free -= totalreserve_pages; 149c15bef30SDmitry Fink 150c15bef30SDmitry Fink /* 1511da177e4SLinus Torvalds * Leave the last 3% for root 1521da177e4SLinus Torvalds */ 1531da177e4SLinus Torvalds if (!cap_sys_admin) 1541da177e4SLinus Torvalds free -= free / 32; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds if (free > pages) 1571da177e4SLinus Torvalds return 0; 1581da177e4SLinus Torvalds 1596d9f7839SHideo AOKI goto error; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds allowed = (totalram_pages - hugetlb_total_pages()) 1631da177e4SLinus Torvalds * sysctl_overcommit_ratio / 100; 1641da177e4SLinus Torvalds /* 1651da177e4SLinus Torvalds * Leave the last 3% for root 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds if (!cap_sys_admin) 1681da177e4SLinus Torvalds allowed -= allowed / 32; 1691da177e4SLinus Torvalds allowed += total_swap_pages; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* Don't let a single process grow too big: 1721da177e4SLinus Torvalds leave 3% of the size of this process for other processes */ 173731572d3SAlan Cox if (mm) 17434b4e4aaSAlan Cox allowed -= mm->total_vm / 32; 1751da177e4SLinus Torvalds 17600a62ce9SKOSAKI Motohiro if (percpu_counter_read_positive(&vm_committed_as) < allowed) 1771da177e4SLinus Torvalds return 0; 1786d9f7839SHideo AOKI error: 1791da177e4SLinus Torvalds vm_unacct_memory(pages); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds return -ENOMEM; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* 1853d48ae45SPeter Zijlstra * Requires inode->i_mapping->i_mmap_mutex 1861da177e4SLinus Torvalds */ 1871da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1881da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds if (vma->vm_flags & VM_DENYWRITE) 191d3ac7f89SJosef "Jeff" Sipek atomic_inc(&file->f_path.dentry->d_inode->i_writecount); 1921da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1931da177e4SLinus Torvalds mapping->i_mmap_writable--; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 1961da177e4SLinus Torvalds if (unlikely(vma->vm_flags & VM_NONLINEAR)) 1976b2dbba8SMichel Lespinasse list_del_init(&vma->shared.nonlinear); 1981da177e4SLinus Torvalds else 1996b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 2001da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2046b2dbba8SMichel Lespinasse * Unlink a file-based vm structure from its interval tree, to hide 205a8fb5618SHugh Dickins * vma from rmap and vmtruncate before freeing its page tables. 2061da177e4SLinus Torvalds */ 207a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds struct file *file = vma->vm_file; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds if (file) { 2121da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 2133d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 2141da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 2153d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 2161da177e4SLinus Torvalds } 217a8fb5618SHugh Dickins } 218a8fb5618SHugh Dickins 219a8fb5618SHugh Dickins /* 220a8fb5618SHugh Dickins * Close a vm structure and free it, returning the next. 221a8fb5618SHugh Dickins */ 222a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) 223a8fb5618SHugh Dickins { 224a8fb5618SHugh Dickins struct vm_area_struct *next = vma->vm_next; 225a8fb5618SHugh Dickins 226a8fb5618SHugh Dickins might_sleep(); 2271da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 2281da177e4SLinus Torvalds vma->vm_ops->close(vma); 229e9714acfSKonstantin Khlebnikov if (vma->vm_file) 230a8fb5618SHugh Dickins fput(vma->vm_file); 231f0be3d32SLee Schermerhorn mpol_put(vma_policy(vma)); 2321da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 233a8fb5618SHugh Dickins return next; 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 236e4eb1ff6SLinus Torvalds static unsigned long do_brk(unsigned long addr, unsigned long len); 237e4eb1ff6SLinus Torvalds 2386a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds unsigned long rlim, retval; 2411da177e4SLinus Torvalds unsigned long newbrk, oldbrk; 2421da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 243a5b4592cSJiri Kosina unsigned long min_brk; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds down_write(&mm->mmap_sem); 2461da177e4SLinus Torvalds 247a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 2485520e894SJiri Kosina /* 2495520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 2505520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 2515520e894SJiri Kosina * to be arbitrarily shifted 2525520e894SJiri Kosina */ 2534471a675SJiri Kosina if (current->brk_randomized) 2545520e894SJiri Kosina min_brk = mm->start_brk; 2555520e894SJiri Kosina else 2565520e894SJiri Kosina min_brk = mm->end_data; 257a5b4592cSJiri Kosina #else 258a5b4592cSJiri Kosina min_brk = mm->start_brk; 259a5b4592cSJiri Kosina #endif 260a5b4592cSJiri Kosina if (brk < min_brk) 2611da177e4SLinus Torvalds goto out; 2621e624196SRam Gupta 2631e624196SRam Gupta /* 2641e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2651e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2661e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2671e624196SRam Gupta * not page aligned -Ram Gupta 2681e624196SRam Gupta */ 26959e99e5bSJiri Slaby rlim = rlimit(RLIMIT_DATA); 270c1d171a0SJiri Kosina if (rlim < RLIM_INFINITY && (brk - mm->start_brk) + 271c1d171a0SJiri Kosina (mm->end_data - mm->start_data) > rlim) 2721e624196SRam Gupta goto out; 2731e624196SRam Gupta 2741da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2751da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2761da177e4SLinus Torvalds if (oldbrk == newbrk) 2771da177e4SLinus Torvalds goto set_brk; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds /* Always allow shrinking brk. */ 2801da177e4SLinus Torvalds if (brk <= mm->brk) { 2811da177e4SLinus Torvalds if (!do_munmap(mm, newbrk, oldbrk-newbrk)) 2821da177e4SLinus Torvalds goto set_brk; 2831da177e4SLinus Torvalds goto out; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* Check against existing mmap mappings. */ 2871da177e4SLinus Torvalds if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE)) 2881da177e4SLinus Torvalds goto out; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 2911da177e4SLinus Torvalds if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk) 2921da177e4SLinus Torvalds goto out; 2931da177e4SLinus Torvalds set_brk: 2941da177e4SLinus Torvalds mm->brk = brk; 2951da177e4SLinus Torvalds out: 2961da177e4SLinus Torvalds retval = mm->brk; 2971da177e4SLinus Torvalds up_write(&mm->mmap_sem); 2981da177e4SLinus Torvalds return retval; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 301d3737187SMichel Lespinasse static long vma_compute_subtree_gap(struct vm_area_struct *vma) 302d3737187SMichel Lespinasse { 303d3737187SMichel Lespinasse unsigned long max, subtree_gap; 304d3737187SMichel Lespinasse max = vma->vm_start; 305d3737187SMichel Lespinasse if (vma->vm_prev) 306d3737187SMichel Lespinasse max -= vma->vm_prev->vm_end; 307d3737187SMichel Lespinasse if (vma->vm_rb.rb_left) { 308d3737187SMichel Lespinasse subtree_gap = rb_entry(vma->vm_rb.rb_left, 309d3737187SMichel Lespinasse struct vm_area_struct, vm_rb)->rb_subtree_gap; 310d3737187SMichel Lespinasse if (subtree_gap > max) 311d3737187SMichel Lespinasse max = subtree_gap; 312d3737187SMichel Lespinasse } 313d3737187SMichel Lespinasse if (vma->vm_rb.rb_right) { 314d3737187SMichel Lespinasse subtree_gap = rb_entry(vma->vm_rb.rb_right, 315d3737187SMichel Lespinasse struct vm_area_struct, vm_rb)->rb_subtree_gap; 316d3737187SMichel Lespinasse if (subtree_gap > max) 317d3737187SMichel Lespinasse max = subtree_gap; 318d3737187SMichel Lespinasse } 319d3737187SMichel Lespinasse return max; 320d3737187SMichel Lespinasse } 321d3737187SMichel Lespinasse 322ed8ea815SMichel Lespinasse #ifdef CONFIG_DEBUG_VM_RB 3231da177e4SLinus Torvalds static int browse_rb(struct rb_root *root) 3241da177e4SLinus Torvalds { 3255a0768f6SMichel Lespinasse int i = 0, j, bug = 0; 3261da177e4SLinus Torvalds struct rb_node *nd, *pn = NULL; 3271da177e4SLinus Torvalds unsigned long prev = 0, pend = 0; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds for (nd = rb_first(root); nd; nd = rb_next(nd)) { 3301da177e4SLinus Torvalds struct vm_area_struct *vma; 3311da177e4SLinus Torvalds vma = rb_entry(nd, struct vm_area_struct, vm_rb); 3325a0768f6SMichel Lespinasse if (vma->vm_start < prev) { 3335a0768f6SMichel Lespinasse printk("vm_start %lx prev %lx\n", vma->vm_start, prev); 3345a0768f6SMichel Lespinasse bug = 1; 3355a0768f6SMichel Lespinasse } 3365a0768f6SMichel Lespinasse if (vma->vm_start < pend) { 3371da177e4SLinus Torvalds printk("vm_start %lx pend %lx\n", vma->vm_start, pend); 3385a0768f6SMichel Lespinasse bug = 1; 3395a0768f6SMichel Lespinasse } 3405a0768f6SMichel Lespinasse if (vma->vm_start > vma->vm_end) { 3415a0768f6SMichel Lespinasse printk("vm_end %lx < vm_start %lx\n", 3425a0768f6SMichel Lespinasse vma->vm_end, vma->vm_start); 3435a0768f6SMichel Lespinasse bug = 1; 3445a0768f6SMichel Lespinasse } 3455a0768f6SMichel Lespinasse if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) { 3465a0768f6SMichel Lespinasse printk("free gap %lx, correct %lx\n", 3475a0768f6SMichel Lespinasse vma->rb_subtree_gap, 3485a0768f6SMichel Lespinasse vma_compute_subtree_gap(vma)); 3495a0768f6SMichel Lespinasse bug = 1; 3505a0768f6SMichel Lespinasse } 3511da177e4SLinus Torvalds i++; 3521da177e4SLinus Torvalds pn = nd; 353d1af65d1SDavid Miller prev = vma->vm_start; 354d1af65d1SDavid Miller pend = vma->vm_end; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds j = 0; 3575a0768f6SMichel Lespinasse for (nd = pn; nd; nd = rb_prev(nd)) 3581da177e4SLinus Torvalds j++; 3595a0768f6SMichel Lespinasse if (i != j) { 3605a0768f6SMichel Lespinasse printk("backwards %d, forwards %d\n", j, i); 3615a0768f6SMichel Lespinasse bug = 1; 3621da177e4SLinus Torvalds } 3635a0768f6SMichel Lespinasse return bug ? -1 : i; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 366d3737187SMichel Lespinasse static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore) 367d3737187SMichel Lespinasse { 368d3737187SMichel Lespinasse struct rb_node *nd; 369d3737187SMichel Lespinasse 370d3737187SMichel Lespinasse for (nd = rb_first(root); nd; nd = rb_next(nd)) { 371d3737187SMichel Lespinasse struct vm_area_struct *vma; 372d3737187SMichel Lespinasse vma = rb_entry(nd, struct vm_area_struct, vm_rb); 373d3737187SMichel Lespinasse BUG_ON(vma != ignore && 374d3737187SMichel Lespinasse vma->rb_subtree_gap != vma_compute_subtree_gap(vma)); 375d3737187SMichel Lespinasse } 376d3737187SMichel Lespinasse } 377d3737187SMichel Lespinasse 3781da177e4SLinus Torvalds void validate_mm(struct mm_struct *mm) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds int bug = 0; 3811da177e4SLinus Torvalds int i = 0; 3825a0768f6SMichel Lespinasse unsigned long highest_address = 0; 383ed8ea815SMichel Lespinasse struct vm_area_struct *vma = mm->mmap; 384ed8ea815SMichel Lespinasse while (vma) { 385ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 38663c3b902SMichel Lespinasse vma_lock_anon_vma(vma); 387ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 388ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 38963c3b902SMichel Lespinasse vma_unlock_anon_vma(vma); 3905a0768f6SMichel Lespinasse highest_address = vma->vm_end; 391ed8ea815SMichel Lespinasse vma = vma->vm_next; 3921da177e4SLinus Torvalds i++; 3931da177e4SLinus Torvalds } 3945a0768f6SMichel Lespinasse if (i != mm->map_count) { 3955a0768f6SMichel Lespinasse printk("map_count %d vm_next %d\n", mm->map_count, i); 3965a0768f6SMichel Lespinasse bug = 1; 3975a0768f6SMichel Lespinasse } 3985a0768f6SMichel Lespinasse if (highest_address != mm->highest_vm_end) { 3995a0768f6SMichel Lespinasse printk("mm->highest_vm_end %lx, found %lx\n", 4005a0768f6SMichel Lespinasse mm->highest_vm_end, highest_address); 4015a0768f6SMichel Lespinasse bug = 1; 4025a0768f6SMichel Lespinasse } 4031da177e4SLinus Torvalds i = browse_rb(&mm->mm_rb); 4045a0768f6SMichel Lespinasse if (i != mm->map_count) { 4055a0768f6SMichel Lespinasse printk("map_count %d rb %d\n", mm->map_count, i); 4065a0768f6SMichel Lespinasse bug = 1; 4075a0768f6SMichel Lespinasse } 40846a350efSEric Sesterhenn BUG_ON(bug); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds #else 411d3737187SMichel Lespinasse #define validate_mm_rb(root, ignore) do { } while (0) 4121da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 4131da177e4SLinus Torvalds #endif 4141da177e4SLinus Torvalds 415d3737187SMichel Lespinasse RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb, 416d3737187SMichel Lespinasse unsigned long, rb_subtree_gap, vma_compute_subtree_gap) 417d3737187SMichel Lespinasse 418d3737187SMichel Lespinasse /* 419d3737187SMichel Lespinasse * Update augmented rbtree rb_subtree_gap values after vma->vm_start or 420d3737187SMichel Lespinasse * vma->vm_prev->vm_end values changed, without modifying the vma's position 421d3737187SMichel Lespinasse * in the rbtree. 422d3737187SMichel Lespinasse */ 423d3737187SMichel Lespinasse static void vma_gap_update(struct vm_area_struct *vma) 424d3737187SMichel Lespinasse { 425d3737187SMichel Lespinasse /* 426d3737187SMichel Lespinasse * As it turns out, RB_DECLARE_CALLBACKS() already created a callback 427d3737187SMichel Lespinasse * function that does exacltly what we want. 428d3737187SMichel Lespinasse */ 429d3737187SMichel Lespinasse vma_gap_callbacks_propagate(&vma->vm_rb, NULL); 430d3737187SMichel Lespinasse } 431d3737187SMichel Lespinasse 432d3737187SMichel Lespinasse static inline void vma_rb_insert(struct vm_area_struct *vma, 433d3737187SMichel Lespinasse struct rb_root *root) 434d3737187SMichel Lespinasse { 435d3737187SMichel Lespinasse /* All rb_subtree_gap values must be consistent prior to insertion */ 436d3737187SMichel Lespinasse validate_mm_rb(root, NULL); 437d3737187SMichel Lespinasse 438d3737187SMichel Lespinasse rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks); 439d3737187SMichel Lespinasse } 440d3737187SMichel Lespinasse 441d3737187SMichel Lespinasse static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root) 442d3737187SMichel Lespinasse { 443d3737187SMichel Lespinasse /* 444d3737187SMichel Lespinasse * All rb_subtree_gap values must be consistent prior to erase, 445d3737187SMichel Lespinasse * with the possible exception of the vma being erased. 446d3737187SMichel Lespinasse */ 447d3737187SMichel Lespinasse validate_mm_rb(root, vma); 448d3737187SMichel Lespinasse 449d3737187SMichel Lespinasse /* 450d3737187SMichel Lespinasse * Note rb_erase_augmented is a fairly large inline function, 451d3737187SMichel Lespinasse * so make sure we instantiate it only once with our desired 452d3737187SMichel Lespinasse * augmented rbtree callbacks. 453d3737187SMichel Lespinasse */ 454d3737187SMichel Lespinasse rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks); 455d3737187SMichel Lespinasse } 456d3737187SMichel Lespinasse 457bf181b9fSMichel Lespinasse /* 458bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 459bf181b9fSMichel Lespinasse * anon_vma's interval trees. 460bf181b9fSMichel Lespinasse * 461bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 462bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 463bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 464bf181b9fSMichel Lespinasse * 465bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 466bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 467bf181b9fSMichel Lespinasse * 468bf181b9fSMichel Lespinasse * The entire update must be protected by exclusive mmap_sem and by 469bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 470bf181b9fSMichel Lespinasse */ 471bf181b9fSMichel Lespinasse static inline void 472bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 473bf181b9fSMichel Lespinasse { 474bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 475bf181b9fSMichel Lespinasse 476bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 477bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 478bf181b9fSMichel Lespinasse } 479bf181b9fSMichel Lespinasse 480bf181b9fSMichel Lespinasse static inline void 481bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 482bf181b9fSMichel Lespinasse { 483bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 484bf181b9fSMichel Lespinasse 485bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 486bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 487bf181b9fSMichel Lespinasse } 488bf181b9fSMichel Lespinasse 4896597d783SHugh Dickins static int find_vma_links(struct mm_struct *mm, unsigned long addr, 4906597d783SHugh Dickins unsigned long end, struct vm_area_struct **pprev, 4916597d783SHugh Dickins struct rb_node ***rb_link, struct rb_node **rb_parent) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct rb_node **__rb_link, *__rb_parent, *rb_prev; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds __rb_link = &mm->mm_rb.rb_node; 4961da177e4SLinus Torvalds rb_prev = __rb_parent = NULL; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds while (*__rb_link) { 4991da177e4SLinus Torvalds struct vm_area_struct *vma_tmp; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds __rb_parent = *__rb_link; 5021da177e4SLinus Torvalds vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds if (vma_tmp->vm_end > addr) { 5056597d783SHugh Dickins /* Fail if an existing vma overlaps the area */ 5066597d783SHugh Dickins if (vma_tmp->vm_start < end) 5076597d783SHugh Dickins return -ENOMEM; 5081da177e4SLinus Torvalds __rb_link = &__rb_parent->rb_left; 5091da177e4SLinus Torvalds } else { 5101da177e4SLinus Torvalds rb_prev = __rb_parent; 5111da177e4SLinus Torvalds __rb_link = &__rb_parent->rb_right; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds *pprev = NULL; 5161da177e4SLinus Torvalds if (rb_prev) 5171da177e4SLinus Torvalds *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); 5181da177e4SLinus Torvalds *rb_link = __rb_link; 5191da177e4SLinus Torvalds *rb_parent = __rb_parent; 5206597d783SHugh Dickins return 0; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma, 5241da177e4SLinus Torvalds struct rb_node **rb_link, struct rb_node *rb_parent) 5251da177e4SLinus Torvalds { 526d3737187SMichel Lespinasse /* Update tracking information for the gap following the new vma. */ 527d3737187SMichel Lespinasse if (vma->vm_next) 528d3737187SMichel Lespinasse vma_gap_update(vma->vm_next); 529d3737187SMichel Lespinasse else 530d3737187SMichel Lespinasse mm->highest_vm_end = vma->vm_end; 531d3737187SMichel Lespinasse 532d3737187SMichel Lespinasse /* 533d3737187SMichel Lespinasse * vma->vm_prev wasn't known when we followed the rbtree to find the 534d3737187SMichel Lespinasse * correct insertion point for that vma. As a result, we could not 535d3737187SMichel Lespinasse * update the vma vm_rb parents rb_subtree_gap values on the way down. 536d3737187SMichel Lespinasse * So, we first insert the vma with a zero rb_subtree_gap value 537d3737187SMichel Lespinasse * (to be consistent with what we did on the way down), and then 538d3737187SMichel Lespinasse * immediately update the gap to the correct value. Finally we 539d3737187SMichel Lespinasse * rebalance the rbtree after all augmented values have been set. 540d3737187SMichel Lespinasse */ 5411da177e4SLinus Torvalds rb_link_node(&vma->vm_rb, rb_parent, rb_link); 542d3737187SMichel Lespinasse vma->rb_subtree_gap = 0; 543d3737187SMichel Lespinasse vma_gap_update(vma); 544d3737187SMichel Lespinasse vma_rb_insert(vma, &mm->mm_rb); 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 547cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma) 5481da177e4SLinus Torvalds { 5491da177e4SLinus Torvalds struct file *file; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds file = vma->vm_file; 5521da177e4SLinus Torvalds if (file) { 5531da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds if (vma->vm_flags & VM_DENYWRITE) 556d3ac7f89SJosef "Jeff" Sipek atomic_dec(&file->f_path.dentry->d_inode->i_writecount); 5571da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 5581da177e4SLinus Torvalds mapping->i_mmap_writable++; 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 5611da177e4SLinus Torvalds if (unlikely(vma->vm_flags & VM_NONLINEAR)) 5621da177e4SLinus Torvalds vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear); 5631da177e4SLinus Torvalds else 5646b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5651da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds static void 5701da177e4SLinus Torvalds __vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 5711da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node **rb_link, 5721da177e4SLinus Torvalds struct rb_node *rb_parent) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds __vma_link_list(mm, vma, prev, rb_parent); 5751da177e4SLinus Torvalds __vma_link_rb(mm, vma, rb_link, rb_parent); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 5791da177e4SLinus Torvalds struct vm_area_struct *prev, struct rb_node **rb_link, 5801da177e4SLinus Torvalds struct rb_node *rb_parent) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct address_space *mapping = NULL; 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds if (vma->vm_file) 5851da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 5861da177e4SLinus Torvalds 58797a89413SPeter Zijlstra if (mapping) 5883d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds __vma_link(mm, vma, prev, rb_link, rb_parent); 5911da177e4SLinus Torvalds __vma_link_file(vma); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds if (mapping) 5943d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds mm->map_count++; 5971da177e4SLinus Torvalds validate_mm(mm); 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* 60188f6b4c3SKautuk Consul * Helper for vma_adjust() in the split_vma insert case: insert a vma into the 6026b2dbba8SMichel Lespinasse * mm's list and rbtree. It has already been inserted into the interval tree. 6031da177e4SLinus Torvalds */ 60448aae425SZhenwenXu static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 6051da177e4SLinus Torvalds { 6066597d783SHugh Dickins struct vm_area_struct *prev; 6071da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 6081da177e4SLinus Torvalds 6096597d783SHugh Dickins if (find_vma_links(mm, vma->vm_start, vma->vm_end, 6106597d783SHugh Dickins &prev, &rb_link, &rb_parent)) 6116597d783SHugh Dickins BUG(); 6121da177e4SLinus Torvalds __vma_link(mm, vma, prev, rb_link, rb_parent); 6131da177e4SLinus Torvalds mm->map_count++; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds static inline void 6171da177e4SLinus Torvalds __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma, 6181da177e4SLinus Torvalds struct vm_area_struct *prev) 6191da177e4SLinus Torvalds { 620d3737187SMichel Lespinasse struct vm_area_struct *next; 621297c5eeeSLinus Torvalds 622d3737187SMichel Lespinasse vma_rb_erase(vma, &mm->mm_rb); 623d3737187SMichel Lespinasse prev->vm_next = next = vma->vm_next; 624297c5eeeSLinus Torvalds if (next) 625297c5eeeSLinus Torvalds next->vm_prev = prev; 6261da177e4SLinus Torvalds if (mm->mmap_cache == vma) 6271da177e4SLinus Torvalds mm->mmap_cache = prev; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 6321da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 6331da177e4SLinus Torvalds * The following helper function should be used when such adjustments 6341da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 6351da177e4SLinus Torvalds * before we drop the necessary locks. 6361da177e4SLinus Torvalds */ 6375beb4930SRik van Riel int vma_adjust(struct vm_area_struct *vma, unsigned long start, 6381da177e4SLinus Torvalds unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 6411da177e4SLinus Torvalds struct vm_area_struct *next = vma->vm_next; 6421da177e4SLinus Torvalds struct vm_area_struct *importer = NULL; 6431da177e4SLinus Torvalds struct address_space *mapping = NULL; 6446b2dbba8SMichel Lespinasse struct rb_root *root = NULL; 645012f1800SRik van Riel struct anon_vma *anon_vma = NULL; 6461da177e4SLinus Torvalds struct file *file = vma->vm_file; 647d3737187SMichel Lespinasse bool start_changed = false, end_changed = false; 6481da177e4SLinus Torvalds long adjust_next = 0; 6491da177e4SLinus Torvalds int remove_next = 0; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (next && !insert) { 652287d97acSLinus Torvalds struct vm_area_struct *exporter = NULL; 653287d97acSLinus Torvalds 6541da177e4SLinus Torvalds if (end >= next->vm_end) { 6551da177e4SLinus Torvalds /* 6561da177e4SLinus Torvalds * vma expands, overlapping all the next, and 6571da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 6581da177e4SLinus Torvalds */ 6591da177e4SLinus Torvalds again: remove_next = 1 + (end > next->vm_end); 6601da177e4SLinus Torvalds end = next->vm_end; 661287d97acSLinus Torvalds exporter = next; 6621da177e4SLinus Torvalds importer = vma; 6631da177e4SLinus Torvalds } else if (end > next->vm_start) { 6641da177e4SLinus Torvalds /* 6651da177e4SLinus Torvalds * vma expands, overlapping part of the next: 6661da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 6671da177e4SLinus Torvalds */ 6681da177e4SLinus Torvalds adjust_next = (end - next->vm_start) >> PAGE_SHIFT; 669287d97acSLinus Torvalds exporter = next; 6701da177e4SLinus Torvalds importer = vma; 6711da177e4SLinus Torvalds } else if (end < vma->vm_end) { 6721da177e4SLinus Torvalds /* 6731da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 6741da177e4SLinus Torvalds * split_vma inserting another: so it must be 6751da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 6761da177e4SLinus Torvalds */ 6771da177e4SLinus Torvalds adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT); 678287d97acSLinus Torvalds exporter = vma; 6791da177e4SLinus Torvalds importer = next; 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds 6825beb4930SRik van Riel /* 6835beb4930SRik van Riel * Easily overlooked: when mprotect shifts the boundary, 6845beb4930SRik van Riel * make sure the expanding vma has anon_vma set if the 6855beb4930SRik van Riel * shrinking vma had, to cover any anon pages imported. 6865beb4930SRik van Riel */ 687287d97acSLinus Torvalds if (exporter && exporter->anon_vma && !importer->anon_vma) { 688287d97acSLinus Torvalds if (anon_vma_clone(importer, exporter)) 6895beb4930SRik van Riel return -ENOMEM; 690287d97acSLinus Torvalds importer->anon_vma = exporter->anon_vma; 6915beb4930SRik van Riel } 6925beb4930SRik van Riel } 6935beb4930SRik van Riel 6941da177e4SLinus Torvalds if (file) { 6951da177e4SLinus Torvalds mapping = file->f_mapping; 696682968e0SSrikar Dronamraju if (!(vma->vm_flags & VM_NONLINEAR)) { 6971da177e4SLinus Torvalds root = &mapping->i_mmap; 698cbc91f71SSrikar Dronamraju uprobe_munmap(vma, vma->vm_start, vma->vm_end); 699682968e0SSrikar Dronamraju 700682968e0SSrikar Dronamraju if (adjust_next) 701cbc91f71SSrikar Dronamraju uprobe_munmap(next, next->vm_start, 702cbc91f71SSrikar Dronamraju next->vm_end); 703682968e0SSrikar Dronamraju } 704682968e0SSrikar Dronamraju 7053d48ae45SPeter Zijlstra mutex_lock(&mapping->i_mmap_mutex); 7061da177e4SLinus Torvalds if (insert) { 7071da177e4SLinus Torvalds /* 7086b2dbba8SMichel Lespinasse * Put into interval tree now, so instantiated pages 7091da177e4SLinus Torvalds * are visible to arm/parisc __flush_dcache_page 7101da177e4SLinus Torvalds * throughout; but we cannot insert into address 7111da177e4SLinus Torvalds * space until vma start or end is updated. 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds __vma_link_file(insert); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 71794fcc585SAndrea Arcangeli vma_adjust_trans_huge(vma, start, end, adjust_next); 71894fcc585SAndrea Arcangeli 719012f1800SRik van Riel anon_vma = vma->anon_vma; 720bf181b9fSMichel Lespinasse if (!anon_vma && adjust_next) 721bf181b9fSMichel Lespinasse anon_vma = next->anon_vma; 722bf181b9fSMichel Lespinasse if (anon_vma) { 723ca42b26aSMichel Lespinasse VM_BUG_ON(adjust_next && next->anon_vma && 724ca42b26aSMichel Lespinasse anon_vma != next->anon_vma); 725012f1800SRik van Riel anon_vma_lock(anon_vma); 726bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 727bf181b9fSMichel Lespinasse if (adjust_next) 728bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(next); 729bf181b9fSMichel Lespinasse } 730012f1800SRik van Riel 7311da177e4SLinus Torvalds if (root) { 7321da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 7336b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, root); 7341da177e4SLinus Torvalds if (adjust_next) 7356b2dbba8SMichel Lespinasse vma_interval_tree_remove(next, root); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 738d3737187SMichel Lespinasse if (start != vma->vm_start) { 7391da177e4SLinus Torvalds vma->vm_start = start; 740d3737187SMichel Lespinasse start_changed = true; 741d3737187SMichel Lespinasse } 742d3737187SMichel Lespinasse if (end != vma->vm_end) { 7431da177e4SLinus Torvalds vma->vm_end = end; 744d3737187SMichel Lespinasse end_changed = true; 745d3737187SMichel Lespinasse } 7461da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 7471da177e4SLinus Torvalds if (adjust_next) { 7481da177e4SLinus Torvalds next->vm_start += adjust_next << PAGE_SHIFT; 7491da177e4SLinus Torvalds next->vm_pgoff += adjust_next; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds if (root) { 7531da177e4SLinus Torvalds if (adjust_next) 7546b2dbba8SMichel Lespinasse vma_interval_tree_insert(next, root); 7556b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, root); 7561da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds if (remove_next) { 7601da177e4SLinus Torvalds /* 7611da177e4SLinus Torvalds * vma_merge has merged next into vma, and needs 7621da177e4SLinus Torvalds * us to remove next before dropping the locks. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds __vma_unlink(mm, next, vma); 7651da177e4SLinus Torvalds if (file) 7661da177e4SLinus Torvalds __remove_shared_vm_struct(next, file, mapping); 7671da177e4SLinus Torvalds } else if (insert) { 7681da177e4SLinus Torvalds /* 7691da177e4SLinus Torvalds * split_vma has split insert from vma, and needs 7701da177e4SLinus Torvalds * us to insert it before dropping the locks 7711da177e4SLinus Torvalds * (it may either follow vma or precede it). 7721da177e4SLinus Torvalds */ 7731da177e4SLinus Torvalds __insert_vm_struct(mm, insert); 774d3737187SMichel Lespinasse } else { 775d3737187SMichel Lespinasse if (start_changed) 776d3737187SMichel Lespinasse vma_gap_update(vma); 777d3737187SMichel Lespinasse if (end_changed) { 778d3737187SMichel Lespinasse if (!next) 779d3737187SMichel Lespinasse mm->highest_vm_end = end; 780d3737187SMichel Lespinasse else if (!adjust_next) 781d3737187SMichel Lespinasse vma_gap_update(next); 782d3737187SMichel Lespinasse } 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 785bf181b9fSMichel Lespinasse if (anon_vma) { 786bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 787bf181b9fSMichel Lespinasse if (adjust_next) 788bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(next); 789012f1800SRik van Riel anon_vma_unlock(anon_vma); 790bf181b9fSMichel Lespinasse } 7911da177e4SLinus Torvalds if (mapping) 7923d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 7931da177e4SLinus Torvalds 7942b144498SSrikar Dronamraju if (root) { 7957b2d81d4SIngo Molnar uprobe_mmap(vma); 7962b144498SSrikar Dronamraju 7972b144498SSrikar Dronamraju if (adjust_next) 7987b2d81d4SIngo Molnar uprobe_mmap(next); 7992b144498SSrikar Dronamraju } 8002b144498SSrikar Dronamraju 8011da177e4SLinus Torvalds if (remove_next) { 802925d1c40SMatt Helsley if (file) { 803cbc91f71SSrikar Dronamraju uprobe_munmap(next, next->vm_start, next->vm_end); 8041da177e4SLinus Torvalds fput(file); 805925d1c40SMatt Helsley } 8065beb4930SRik van Riel if (next->anon_vma) 8075beb4930SRik van Riel anon_vma_merge(vma, next); 8081da177e4SLinus Torvalds mm->map_count--; 809f0be3d32SLee Schermerhorn mpol_put(vma_policy(next)); 8101da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, next); 8111da177e4SLinus Torvalds /* 8121da177e4SLinus Torvalds * In mprotect's case 6 (see comments on vma_merge), 8131da177e4SLinus Torvalds * we must remove another next too. It would clutter 8141da177e4SLinus Torvalds * up the code too much to do both in one go. 8151da177e4SLinus Torvalds */ 8161da177e4SLinus Torvalds next = vma->vm_next; 817d3737187SMichel Lespinasse if (remove_next == 2) 8181da177e4SLinus Torvalds goto again; 819d3737187SMichel Lespinasse else if (next) 820d3737187SMichel Lespinasse vma_gap_update(next); 821d3737187SMichel Lespinasse else 822d3737187SMichel Lespinasse mm->highest_vm_end = end; 8231da177e4SLinus Torvalds } 8242b144498SSrikar Dronamraju if (insert && file) 8257b2d81d4SIngo Molnar uprobe_mmap(insert); 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds validate_mm(mm); 8285beb4930SRik van Riel 8295beb4930SRik van Riel return 0; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* 8331da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 8341da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 8351da177e4SLinus Torvalds */ 8361da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 8371da177e4SLinus Torvalds struct file *file, unsigned long vm_flags) 8381da177e4SLinus Torvalds { 8390b173bc4SKonstantin Khlebnikov if (vma->vm_flags ^ vm_flags) 8401da177e4SLinus Torvalds return 0; 8411da177e4SLinus Torvalds if (vma->vm_file != file) 8421da177e4SLinus Torvalds return 0; 8431da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 8441da177e4SLinus Torvalds return 0; 8451da177e4SLinus Torvalds return 1; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 849965f55deSShaohua Li struct anon_vma *anon_vma2, 850965f55deSShaohua Li struct vm_area_struct *vma) 8511da177e4SLinus Torvalds { 852965f55deSShaohua Li /* 853965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 854965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 855965f55deSShaohua Li */ 856965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 857965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 858965f55deSShaohua Li return 1; 859965f55deSShaohua Li return anon_vma1 == anon_vma2; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds /* 8631da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 8641da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 8651da177e4SLinus Torvalds * 8661da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 8671da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 8681da177e4SLinus Torvalds * 8691da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 8701da177e4SLinus Torvalds * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which 8711da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds static int 8741da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 8751da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) 8761da177e4SLinus Torvalds { 8771da177e4SLinus Torvalds if (is_mergeable_vma(vma, file, vm_flags) && 878965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 8791da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 8801da177e4SLinus Torvalds return 1; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds return 0; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* 8861da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 8871da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 8881da177e4SLinus Torvalds * 8891da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 8901da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 8911da177e4SLinus Torvalds */ 8921da177e4SLinus Torvalds static int 8931da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 8941da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) 8951da177e4SLinus Torvalds { 8961da177e4SLinus Torvalds if (is_mergeable_vma(vma, file, vm_flags) && 897965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 8981da177e4SLinus Torvalds pgoff_t vm_pglen; 8991da177e4SLinus Torvalds vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 9001da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 9011da177e4SLinus Torvalds return 1; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds return 0; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out 9081da177e4SLinus Torvalds * whether that can be merged with its predecessor or its successor. 9091da177e4SLinus Torvalds * Or both (it neatly fills a hole). 9101da177e4SLinus Torvalds * 9111da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 9121da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 9131da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 9141da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 9151da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 9161da177e4SLinus Torvalds * case has already been eliminated. 9171da177e4SLinus Torvalds * 9181da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 9191da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 9201da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 9211da177e4SLinus Torvalds * 9221da177e4SLinus Torvalds * AAAA AAAA AAAA AAAA 9231da177e4SLinus Torvalds * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX 9241da177e4SLinus Torvalds * cannot merge might become might become might become 9251da177e4SLinus Torvalds * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or 9261da177e4SLinus Torvalds * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or 9271da177e4SLinus Torvalds * mremap move: PPPPNNNNNNNN 8 9281da177e4SLinus Torvalds * AAAA 9291da177e4SLinus Torvalds * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN 9301da177e4SLinus Torvalds * might become case 1 below case 2 below case 3 below 9311da177e4SLinus Torvalds * 9321da177e4SLinus Torvalds * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX: 9331da177e4SLinus Torvalds * mprotect_fixup updates vm_flags & vm_page_prot on successful return. 9341da177e4SLinus Torvalds */ 9351da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm, 9361da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 9371da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 9381da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 9391da177e4SLinus Torvalds pgoff_t pgoff, struct mempolicy *policy) 9401da177e4SLinus Torvalds { 9411da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 9421da177e4SLinus Torvalds struct vm_area_struct *area, *next; 9435beb4930SRik van Riel int err; 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds /* 9461da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 9471da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 9481da177e4SLinus Torvalds */ 9491da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 9501da177e4SLinus Torvalds return NULL; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds if (prev) 9531da177e4SLinus Torvalds next = prev->vm_next; 9541da177e4SLinus Torvalds else 9551da177e4SLinus Torvalds next = mm->mmap; 9561da177e4SLinus Torvalds area = next; 9571da177e4SLinus Torvalds if (next && next->vm_end == end) /* cases 6, 7, 8 */ 9581da177e4SLinus Torvalds next = next->vm_next; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* 9611da177e4SLinus Torvalds * Can it merge with the predecessor? 9621da177e4SLinus Torvalds */ 9631da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 9641da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 9651da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 9661da177e4SLinus Torvalds anon_vma, file, pgoff)) { 9671da177e4SLinus Torvalds /* 9681da177e4SLinus Torvalds * OK, it can. Can we now merge in the successor as well? 9691da177e4SLinus Torvalds */ 9701da177e4SLinus Torvalds if (next && end == next->vm_start && 9711da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 9721da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 9731da177e4SLinus Torvalds anon_vma, file, pgoff+pglen) && 9741da177e4SLinus Torvalds is_mergeable_anon_vma(prev->anon_vma, 975965f55deSShaohua Li next->anon_vma, NULL)) { 9761da177e4SLinus Torvalds /* cases 1, 6 */ 9775beb4930SRik van Riel err = vma_adjust(prev, prev->vm_start, 9781da177e4SLinus Torvalds next->vm_end, prev->vm_pgoff, NULL); 9791da177e4SLinus Torvalds } else /* cases 2, 5, 7 */ 9805beb4930SRik van Riel err = vma_adjust(prev, prev->vm_start, 9811da177e4SLinus Torvalds end, prev->vm_pgoff, NULL); 9825beb4930SRik van Riel if (err) 9835beb4930SRik van Riel return NULL; 984b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(prev); 9851da177e4SLinus Torvalds return prev; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /* 9891da177e4SLinus Torvalds * Can this new request be merged in front of next? 9901da177e4SLinus Torvalds */ 9911da177e4SLinus Torvalds if (next && end == next->vm_start && 9921da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 9931da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 9941da177e4SLinus Torvalds anon_vma, file, pgoff+pglen)) { 9951da177e4SLinus Torvalds if (prev && addr < prev->vm_end) /* case 4 */ 9965beb4930SRik van Riel err = vma_adjust(prev, prev->vm_start, 9971da177e4SLinus Torvalds addr, prev->vm_pgoff, NULL); 9981da177e4SLinus Torvalds else /* cases 3, 8 */ 9995beb4930SRik van Riel err = vma_adjust(area, addr, next->vm_end, 10001da177e4SLinus Torvalds next->vm_pgoff - pglen, NULL); 10015beb4930SRik van Riel if (err) 10025beb4930SRik van Riel return NULL; 1003b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(area); 10041da177e4SLinus Torvalds return area; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds return NULL; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds /* 1011d0e9fe17SLinus Torvalds * Rough compatbility check to quickly see if it's even worth looking 1012d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1013d0e9fe17SLinus Torvalds * 1014d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1015d0e9fe17SLinus Torvalds * in things that mprotect may change. 1016d0e9fe17SLinus Torvalds * 1017d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1018d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1019d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1020d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1021d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1022d0e9fe17SLinus Torvalds */ 1023d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1024d0e9fe17SLinus Torvalds { 1025d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1026d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1027d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 1028d0e9fe17SLinus Torvalds !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) && 1029d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1030d0e9fe17SLinus Torvalds } 1031d0e9fe17SLinus Torvalds 1032d0e9fe17SLinus Torvalds /* 1033d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1034d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1035d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1036d0e9fe17SLinus Torvalds * to share the anon_vma. 1037d0e9fe17SLinus Torvalds * 1038d0e9fe17SLinus Torvalds * NOTE! This runs with mm_sem held for reading, so it is possible that 1039d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1040d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1041d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1042d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 1043d0e9fe17SLinus Torvalds * we do that ACCESS_ONCE() to make sure that we never re-load the pointer. 1044d0e9fe17SLinus Torvalds * 1045d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1046d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1047d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1048d0e9fe17SLinus Torvalds * a fork). 1049d0e9fe17SLinus Torvalds * 1050d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1051d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 1052d0e9fe17SLinus Torvalds * a read lock on the mm_sem. 1053d0e9fe17SLinus Torvalds */ 1054d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1055d0e9fe17SLinus Torvalds { 1056d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 1057d0e9fe17SLinus Torvalds struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma); 1058d0e9fe17SLinus Torvalds 1059d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1060d0e9fe17SLinus Torvalds return anon_vma; 1061d0e9fe17SLinus Torvalds } 1062d0e9fe17SLinus Torvalds return NULL; 1063d0e9fe17SLinus Torvalds } 1064d0e9fe17SLinus Torvalds 1065d0e9fe17SLinus Torvalds /* 10661da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 10671da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 10681da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 10691da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 10701da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 10711da177e4SLinus Torvalds * mprotect. 10721da177e4SLinus Torvalds */ 10731da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 10741da177e4SLinus Torvalds { 1075d0e9fe17SLinus Torvalds struct anon_vma *anon_vma; 10761da177e4SLinus Torvalds struct vm_area_struct *near; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds near = vma->vm_next; 10791da177e4SLinus Torvalds if (!near) 10801da177e4SLinus Torvalds goto try_prev; 10811da177e4SLinus Torvalds 1082d0e9fe17SLinus Torvalds anon_vma = reusable_anon_vma(near, vma, near); 1083d0e9fe17SLinus Torvalds if (anon_vma) 1084d0e9fe17SLinus Torvalds return anon_vma; 10851da177e4SLinus Torvalds try_prev: 10869be34c9dSLinus Torvalds near = vma->vm_prev; 10871da177e4SLinus Torvalds if (!near) 10881da177e4SLinus Torvalds goto none; 10891da177e4SLinus Torvalds 1090d0e9fe17SLinus Torvalds anon_vma = reusable_anon_vma(near, near, vma); 1091d0e9fe17SLinus Torvalds if (anon_vma) 1092d0e9fe17SLinus Torvalds return anon_vma; 10931da177e4SLinus Torvalds none: 10941da177e4SLinus Torvalds /* 10951da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 10961da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 10971da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 10981da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 10991da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 11001da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 11011da177e4SLinus Torvalds */ 11021da177e4SLinus Torvalds return NULL; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 1106ab50b8edSHugh Dickins void vm_stat_account(struct mm_struct *mm, unsigned long flags, 11071da177e4SLinus Torvalds struct file *file, long pages) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds const unsigned long stack_flags 11101da177e4SLinus Torvalds = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN); 11111da177e4SLinus Torvalds 111244de9d0cSHuang Shijie mm->total_vm += pages; 111344de9d0cSHuang Shijie 11141da177e4SLinus Torvalds if (file) { 11151da177e4SLinus Torvalds mm->shared_vm += pages; 11161da177e4SLinus Torvalds if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC) 11171da177e4SLinus Torvalds mm->exec_vm += pages; 11181da177e4SLinus Torvalds } else if (flags & stack_flags) 11191da177e4SLinus Torvalds mm->stack_vm += pages; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds /* 112440401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 112540401530SAl Viro * low as possible but still greater than mmap_min_addr 112640401530SAl Viro */ 112740401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 112840401530SAl Viro { 112940401530SAl Viro hint &= PAGE_MASK; 113040401530SAl Viro if (((void *)hint != NULL) && 113140401530SAl Viro (hint < mmap_min_addr)) 113240401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 113340401530SAl Viro return hint; 113440401530SAl Viro } 113540401530SAl Viro 113640401530SAl Viro /* 113727f5de79SJianjun Kong * The caller must hold down_write(¤t->mm->mmap_sem). 11381da177e4SLinus Torvalds */ 11391da177e4SLinus Torvalds 1140e3fc629dSAl Viro unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, 11411da177e4SLinus Torvalds unsigned long len, unsigned long prot, 11421da177e4SLinus Torvalds unsigned long flags, unsigned long pgoff) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds struct mm_struct * mm = current->mm; 11451da177e4SLinus Torvalds struct inode *inode; 1146ca16d140SKOSAKI Motohiro vm_flags_t vm_flags; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* 11491da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 11501da177e4SLinus Torvalds * 11511da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 11521da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 1155d3ac7f89SJosef "Jeff" Sipek if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC))) 11561da177e4SLinus Torvalds prot |= PROT_EXEC; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds if (!len) 11591da177e4SLinus Torvalds return -EINVAL; 11601da177e4SLinus Torvalds 11617cd94146SEric Paris if (!(flags & MAP_FIXED)) 11627cd94146SEric Paris addr = round_hint_to_min(addr); 11637cd94146SEric Paris 11641da177e4SLinus Torvalds /* Careful about overflows.. */ 11651da177e4SLinus Torvalds len = PAGE_ALIGN(len); 11669206de95SAl Viro if (!len) 11671da177e4SLinus Torvalds return -ENOMEM; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* offset overflow? */ 11701da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 11711da177e4SLinus Torvalds return -EOVERFLOW; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* Too many mappings? */ 11741da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 11751da177e4SLinus Torvalds return -ENOMEM; 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 11781da177e4SLinus Torvalds * that it represents a valid section of the address space. 11791da177e4SLinus Torvalds */ 11801da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 11811da177e4SLinus Torvalds if (addr & ~PAGE_MASK) 11821da177e4SLinus Torvalds return addr; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 11851da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 11861da177e4SLinus Torvalds * of the memory object, so we don't do any here. 11871da177e4SLinus Torvalds */ 11881da177e4SLinus Torvalds vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) | 11891da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 11901da177e4SLinus Torvalds 1191cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 11921da177e4SLinus Torvalds if (!can_do_mlock()) 11931da177e4SLinus Torvalds return -EPERM; 1194ba470de4SRik van Riel 11951da177e4SLinus Torvalds /* mlock MCL_FUTURE? */ 11961da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 11971da177e4SLinus Torvalds unsigned long locked, lock_limit; 119893ea1d0aSChris Wright locked = len >> PAGE_SHIFT; 119993ea1d0aSChris Wright locked += mm->locked_vm; 120059e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK); 120193ea1d0aSChris Wright lock_limit >>= PAGE_SHIFT; 12021da177e4SLinus Torvalds if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 12031da177e4SLinus Torvalds return -EAGAIN; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 1206d3ac7f89SJosef "Jeff" Sipek inode = file ? file->f_path.dentry->d_inode : NULL; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds if (file) { 12091da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 12101da177e4SLinus Torvalds case MAP_SHARED: 12111da177e4SLinus Torvalds if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) 12121da177e4SLinus Torvalds return -EACCES; 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds /* 12151da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 12161da177e4SLinus Torvalds * file.. 12171da177e4SLinus Torvalds */ 12181da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 12191da177e4SLinus Torvalds return -EACCES; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* 12221da177e4SLinus Torvalds * Make sure there are no mandatory locks on the file. 12231da177e4SLinus Torvalds */ 12241da177e4SLinus Torvalds if (locks_verify_locked(inode)) 12251da177e4SLinus Torvalds return -EAGAIN; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 12281da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 12291da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds /* fall through */ 12321da177e4SLinus Torvalds case MAP_PRIVATE: 12331da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 12341da177e4SLinus Torvalds return -EACCES; 1235d3ac7f89SJosef "Jeff" Sipek if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { 123680c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 123780c5606cSLinus Torvalds return -EPERM; 123880c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 123980c5606cSLinus Torvalds } 124080c5606cSLinus Torvalds 124180c5606cSLinus Torvalds if (!file->f_op || !file->f_op->mmap) 124280c5606cSLinus Torvalds return -ENODEV; 12431da177e4SLinus Torvalds break; 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds default: 12461da177e4SLinus Torvalds return -EINVAL; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds } else { 12491da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 12501da177e4SLinus Torvalds case MAP_SHARED: 1251ce363942STejun Heo /* 1252ce363942STejun Heo * Ignore pgoff. 1253ce363942STejun Heo */ 1254ce363942STejun Heo pgoff = 0; 12551da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 12561da177e4SLinus Torvalds break; 12571da177e4SLinus Torvalds case MAP_PRIVATE: 12581da177e4SLinus Torvalds /* 12591da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 12601da177e4SLinus Torvalds */ 12611da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 12621da177e4SLinus Torvalds break; 12631da177e4SLinus Torvalds default: 12641da177e4SLinus Torvalds return -EINVAL; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12685a6fe125SMel Gorman return mmap_region(file, addr, len, flags, vm_flags, pgoff); 12690165ab44SMiklos Szeredi } 12706be5ceb0SLinus Torvalds 127166f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 127266f0dc48SHugh Dickins unsigned long, prot, unsigned long, flags, 127366f0dc48SHugh Dickins unsigned long, fd, unsigned long, pgoff) 127466f0dc48SHugh Dickins { 127566f0dc48SHugh Dickins struct file *file = NULL; 127666f0dc48SHugh Dickins unsigned long retval = -EBADF; 127766f0dc48SHugh Dickins 127866f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1279120a795dSAl Viro audit_mmap_fd(fd, flags); 128066f0dc48SHugh Dickins if (unlikely(flags & MAP_HUGETLB)) 128166f0dc48SHugh Dickins return -EINVAL; 128266f0dc48SHugh Dickins file = fget(fd); 128366f0dc48SHugh Dickins if (!file) 128466f0dc48SHugh Dickins goto out; 128566f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 128666f0dc48SHugh Dickins struct user_struct *user = NULL; 128766f0dc48SHugh Dickins /* 128866f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 128966f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 129066f0dc48SHugh Dickins * A dummy user value is used because we are not locking 129166f0dc48SHugh Dickins * memory so no accounting is necessary 129266f0dc48SHugh Dickins */ 129340716e29SSteven Truelove file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len, 129442d7395fSAndi Kleen VM_NORESERVE, 129542d7395fSAndi Kleen &user, HUGETLB_ANONHUGE_INODE, 129642d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 129766f0dc48SHugh Dickins if (IS_ERR(file)) 129866f0dc48SHugh Dickins return PTR_ERR(file); 129966f0dc48SHugh Dickins } 130066f0dc48SHugh Dickins 130166f0dc48SHugh Dickins flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 130266f0dc48SHugh Dickins 1303eb36c587SAl Viro retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 130466f0dc48SHugh Dickins if (file) 130566f0dc48SHugh Dickins fput(file); 130666f0dc48SHugh Dickins out: 130766f0dc48SHugh Dickins return retval; 130866f0dc48SHugh Dickins } 130966f0dc48SHugh Dickins 1310a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1311a4679373SChristoph Hellwig struct mmap_arg_struct { 1312a4679373SChristoph Hellwig unsigned long addr; 1313a4679373SChristoph Hellwig unsigned long len; 1314a4679373SChristoph Hellwig unsigned long prot; 1315a4679373SChristoph Hellwig unsigned long flags; 1316a4679373SChristoph Hellwig unsigned long fd; 1317a4679373SChristoph Hellwig unsigned long offset; 1318a4679373SChristoph Hellwig }; 1319a4679373SChristoph Hellwig 1320a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1321a4679373SChristoph Hellwig { 1322a4679373SChristoph Hellwig struct mmap_arg_struct a; 1323a4679373SChristoph Hellwig 1324a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1325a4679373SChristoph Hellwig return -EFAULT; 1326a4679373SChristoph Hellwig if (a.offset & ~PAGE_MASK) 1327a4679373SChristoph Hellwig return -EINVAL; 1328a4679373SChristoph Hellwig 1329a4679373SChristoph Hellwig return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1330a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1331a4679373SChristoph Hellwig } 1332a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1333a4679373SChristoph Hellwig 13344e950f6fSAlexey Dobriyan /* 13354e950f6fSAlexey Dobriyan * Some shared mappigns will want the pages marked read-only 13364e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 13374e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 13384e950f6fSAlexey Dobriyan * VM_SHARED bit). 13394e950f6fSAlexey Dobriyan */ 13404e950f6fSAlexey Dobriyan int vma_wants_writenotify(struct vm_area_struct *vma) 13414e950f6fSAlexey Dobriyan { 1342ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 13434e950f6fSAlexey Dobriyan 13444e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 13454e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 13464e950f6fSAlexey Dobriyan return 0; 13474e950f6fSAlexey Dobriyan 13484e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 13494e950f6fSAlexey Dobriyan if (vma->vm_ops && vma->vm_ops->page_mkwrite) 13504e950f6fSAlexey Dobriyan return 1; 13514e950f6fSAlexey Dobriyan 13524e950f6fSAlexey Dobriyan /* The open routine did something to the protections already? */ 13534e950f6fSAlexey Dobriyan if (pgprot_val(vma->vm_page_prot) != 13543ed75eb8SColy Li pgprot_val(vm_get_page_prot(vm_flags))) 13554e950f6fSAlexey Dobriyan return 0; 13564e950f6fSAlexey Dobriyan 13574e950f6fSAlexey Dobriyan /* Specialty mapping? */ 13584b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 13594e950f6fSAlexey Dobriyan return 0; 13604e950f6fSAlexey Dobriyan 13614e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 13624e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 13634e950f6fSAlexey Dobriyan mapping_cap_account_dirty(vma->vm_file->f_mapping); 13644e950f6fSAlexey Dobriyan } 13654e950f6fSAlexey Dobriyan 1366fc8744adSLinus Torvalds /* 1367fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 13685a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1369fc8744adSLinus Torvalds */ 1370ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1371fc8744adSLinus Torvalds { 13725a6fe125SMel Gorman /* 13735a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 13745a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 13755a6fe125SMel Gorman */ 13765a6fe125SMel Gorman if (file && is_file_hugepages(file)) 13775a6fe125SMel Gorman return 0; 13785a6fe125SMel Gorman 1379fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1380fc8744adSLinus Torvalds } 1381fc8744adSLinus Torvalds 13820165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr, 13830165ab44SMiklos Szeredi unsigned long len, unsigned long flags, 1384ca16d140SKOSAKI Motohiro vm_flags_t vm_flags, unsigned long pgoff) 13850165ab44SMiklos Szeredi { 13860165ab44SMiklos Szeredi struct mm_struct *mm = current->mm; 13870165ab44SMiklos Szeredi struct vm_area_struct *vma, *prev; 13880165ab44SMiklos Szeredi int correct_wcount = 0; 13890165ab44SMiklos Szeredi int error; 13900165ab44SMiklos Szeredi struct rb_node **rb_link, *rb_parent; 13910165ab44SMiklos Szeredi unsigned long charged = 0; 13920165ab44SMiklos Szeredi struct inode *inode = file ? file->f_path.dentry->d_inode : NULL; 13930165ab44SMiklos Szeredi 13941da177e4SLinus Torvalds /* Clear old maps */ 13951da177e4SLinus Torvalds error = -ENOMEM; 13961da177e4SLinus Torvalds munmap_back: 13976597d783SHugh Dickins if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) { 13981da177e4SLinus Torvalds if (do_munmap(mm, addr, len)) 13991da177e4SLinus Torvalds return -ENOMEM; 14001da177e4SLinus Torvalds goto munmap_back; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /* Check against address space limit. */ 1404119f657cSakpm@osdl.org if (!may_expand_vm(mm, len >> PAGE_SHIFT)) 14051da177e4SLinus Torvalds return -ENOMEM; 14061da177e4SLinus Torvalds 1407fc8744adSLinus Torvalds /* 1408fc8744adSLinus Torvalds * Set 'VM_NORESERVE' if we should not account for the 14095a6fe125SMel Gorman * memory use of this mapping. 1410fc8744adSLinus Torvalds */ 14115a6fe125SMel Gorman if ((flags & MAP_NORESERVE)) { 14125a6fe125SMel Gorman /* We honor MAP_NORESERVE if allowed to overcommit */ 14135a6fe125SMel Gorman if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1414fc8744adSLinus Torvalds vm_flags |= VM_NORESERVE; 14155a6fe125SMel Gorman 14165a6fe125SMel Gorman /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 14175a6fe125SMel Gorman if (file && is_file_hugepages(file)) 1418cdfd4325SAndy Whitcroft vm_flags |= VM_NORESERVE; 14195a6fe125SMel Gorman } 1420cdfd4325SAndy Whitcroft 14211da177e4SLinus Torvalds /* 14221da177e4SLinus Torvalds * Private writable mapping: check memory availability 14231da177e4SLinus Torvalds */ 14245a6fe125SMel Gorman if (accountable_mapping(file, vm_flags)) { 14251da177e4SLinus Torvalds charged = len >> PAGE_SHIFT; 1426191c5424SAl Viro if (security_vm_enough_memory_mm(mm, charged)) 14271da177e4SLinus Torvalds return -ENOMEM; 14281da177e4SLinus Torvalds vm_flags |= VM_ACCOUNT; 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds /* 1432de33c8dbSLinus Torvalds * Can we just expand an old mapping? 14331da177e4SLinus Torvalds */ 1434de33c8dbSLinus Torvalds vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL); 1435ba470de4SRik van Riel if (vma) 14361da177e4SLinus Torvalds goto out; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds /* 14391da177e4SLinus Torvalds * Determine the object being mapped and call the appropriate 14401da177e4SLinus Torvalds * specific mapper. the address has already been validated, but 14411da177e4SLinus Torvalds * not unmapped, but the maps are removed from the list. 14421da177e4SLinus Torvalds */ 1443c5e3b83eSPekka Enberg vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 14441da177e4SLinus Torvalds if (!vma) { 14451da177e4SLinus Torvalds error = -ENOMEM; 14461da177e4SLinus Torvalds goto unacct_error; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds vma->vm_mm = mm; 14501da177e4SLinus Torvalds vma->vm_start = addr; 14511da177e4SLinus Torvalds vma->vm_end = addr + len; 14521da177e4SLinus Torvalds vma->vm_flags = vm_flags; 14533ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vm_flags); 14541da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 14555beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 14561da177e4SLinus Torvalds 1457ce8fea7aSHugh Dickins error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */ 1458ce8fea7aSHugh Dickins 14591da177e4SLinus Torvalds if (file) { 14601da177e4SLinus Torvalds if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 14611da177e4SLinus Torvalds goto free_vma; 14621da177e4SLinus Torvalds if (vm_flags & VM_DENYWRITE) { 14631da177e4SLinus Torvalds error = deny_write_access(file); 14641da177e4SLinus Torvalds if (error) 14651da177e4SLinus Torvalds goto free_vma; 14661da177e4SLinus Torvalds correct_wcount = 1; 14671da177e4SLinus Torvalds } 1468cb0942b8SAl Viro vma->vm_file = get_file(file); 14691da177e4SLinus Torvalds error = file->f_op->mmap(file, vma); 14701da177e4SLinus Torvalds if (error) 14711da177e4SLinus Torvalds goto unmap_and_free_vma; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds /* Can addr have changed?? 14741da177e4SLinus Torvalds * 14751da177e4SLinus Torvalds * Answer: Yes, several device drivers can do it in their 14761da177e4SLinus Torvalds * f_op->mmap method. -DaveM 14771da177e4SLinus Torvalds */ 14781da177e4SLinus Torvalds addr = vma->vm_start; 14791da177e4SLinus Torvalds pgoff = vma->vm_pgoff; 14801da177e4SLinus Torvalds vm_flags = vma->vm_flags; 1481f8dbf0a7SHuang Shijie } else if (vm_flags & VM_SHARED) { 1482835ee797SAl Viro if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP))) 1483835ee797SAl Viro goto free_vma; 1484f8dbf0a7SHuang Shijie error = shmem_zero_setup(vma); 1485f8dbf0a7SHuang Shijie if (error) 1486f8dbf0a7SHuang Shijie goto free_vma; 1487f8dbf0a7SHuang Shijie } 14881da177e4SLinus Torvalds 1489c9d0bf24SMagnus Damm if (vma_wants_writenotify(vma)) { 1490c9d0bf24SMagnus Damm pgprot_t pprot = vma->vm_page_prot; 1491c9d0bf24SMagnus Damm 1492c9d0bf24SMagnus Damm /* Can vma->vm_page_prot have changed?? 1493c9d0bf24SMagnus Damm * 1494c9d0bf24SMagnus Damm * Answer: Yes, drivers may have changed it in their 1495c9d0bf24SMagnus Damm * f_op->mmap method. 1496c9d0bf24SMagnus Damm * 1497c9d0bf24SMagnus Damm * Ensures that vmas marked as uncached stay that way. 1498c9d0bf24SMagnus Damm */ 14991ddd439eSHugh Dickins vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED); 1500c9d0bf24SMagnus Damm if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot))) 1501c9d0bf24SMagnus Damm vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1502c9d0bf24SMagnus Damm } 1503d08b3851SPeter Zijlstra 15044d3d5b41SOleg Nesterov vma_link(mm, vma, prev, rb_link, rb_parent); 15054d3d5b41SOleg Nesterov file = vma->vm_file; 15064d3d5b41SOleg Nesterov 15074d3d5b41SOleg Nesterov /* Once vma denies write, undo our temporary denial count */ 15084d3d5b41SOleg Nesterov if (correct_wcount) 15094d3d5b41SOleg Nesterov atomic_inc(&inode->i_writecount); 15101da177e4SLinus Torvalds out: 1511cdd6c482SIngo Molnar perf_event_mmap(vma); 15120a4a9391SPeter Zijlstra 1513ab50b8edSHugh Dickins vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT); 15141da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 151506f9d8c2SKOSAKI Motohiro if (!mlock_vma_pages_range(vma, addr, addr + len)) 151606f9d8c2SKOSAKI Motohiro mm->locked_vm += (len >> PAGE_SHIFT); 1517ba470de4SRik van Riel } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK)) 151854cb8821SNick Piggin make_pages_present(addr, addr + len); 15192b144498SSrikar Dronamraju 1520c7a3a88cSOleg Nesterov if (file) 1521c7a3a88cSOleg Nesterov uprobe_mmap(vma); 15222b144498SSrikar Dronamraju 15231da177e4SLinus Torvalds return addr; 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds unmap_and_free_vma: 15261da177e4SLinus Torvalds if (correct_wcount) 15271da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 15281da177e4SLinus Torvalds vma->vm_file = NULL; 15291da177e4SLinus Torvalds fput(file); 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /* Undo any partial mapping done by a device driver. */ 1532e0da382cSHugh Dickins unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); 1533e0da382cSHugh Dickins charged = 0; 15341da177e4SLinus Torvalds free_vma: 15351da177e4SLinus Torvalds kmem_cache_free(vm_area_cachep, vma); 15361da177e4SLinus Torvalds unacct_error: 15371da177e4SLinus Torvalds if (charged) 15381da177e4SLinus Torvalds vm_unacct_memory(charged); 15391da177e4SLinus Torvalds return error; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 1542*db4fbfb9SMichel Lespinasse unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1543*db4fbfb9SMichel Lespinasse { 1544*db4fbfb9SMichel Lespinasse /* 1545*db4fbfb9SMichel Lespinasse * We implement the search by looking for an rbtree node that 1546*db4fbfb9SMichel Lespinasse * immediately follows a suitable gap. That is, 1547*db4fbfb9SMichel Lespinasse * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length; 1548*db4fbfb9SMichel Lespinasse * - gap_end = vma->vm_start >= info->low_limit + length; 1549*db4fbfb9SMichel Lespinasse * - gap_end - gap_start >= length 1550*db4fbfb9SMichel Lespinasse */ 1551*db4fbfb9SMichel Lespinasse 1552*db4fbfb9SMichel Lespinasse struct mm_struct *mm = current->mm; 1553*db4fbfb9SMichel Lespinasse struct vm_area_struct *vma; 1554*db4fbfb9SMichel Lespinasse unsigned long length, low_limit, high_limit, gap_start, gap_end; 1555*db4fbfb9SMichel Lespinasse 1556*db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1557*db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1558*db4fbfb9SMichel Lespinasse if (length < info->length) 1559*db4fbfb9SMichel Lespinasse return -ENOMEM; 1560*db4fbfb9SMichel Lespinasse 1561*db4fbfb9SMichel Lespinasse /* Adjust search limits by the desired length */ 1562*db4fbfb9SMichel Lespinasse if (info->high_limit < length) 1563*db4fbfb9SMichel Lespinasse return -ENOMEM; 1564*db4fbfb9SMichel Lespinasse high_limit = info->high_limit - length; 1565*db4fbfb9SMichel Lespinasse 1566*db4fbfb9SMichel Lespinasse if (info->low_limit > high_limit) 1567*db4fbfb9SMichel Lespinasse return -ENOMEM; 1568*db4fbfb9SMichel Lespinasse low_limit = info->low_limit + length; 1569*db4fbfb9SMichel Lespinasse 1570*db4fbfb9SMichel Lespinasse /* Check if rbtree root looks promising */ 1571*db4fbfb9SMichel Lespinasse if (RB_EMPTY_ROOT(&mm->mm_rb)) 1572*db4fbfb9SMichel Lespinasse goto check_highest; 1573*db4fbfb9SMichel Lespinasse vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); 1574*db4fbfb9SMichel Lespinasse if (vma->rb_subtree_gap < length) 1575*db4fbfb9SMichel Lespinasse goto check_highest; 1576*db4fbfb9SMichel Lespinasse 1577*db4fbfb9SMichel Lespinasse while (true) { 1578*db4fbfb9SMichel Lespinasse /* Visit left subtree if it looks promising */ 1579*db4fbfb9SMichel Lespinasse gap_end = vma->vm_start; 1580*db4fbfb9SMichel Lespinasse if (gap_end >= low_limit && vma->vm_rb.rb_left) { 1581*db4fbfb9SMichel Lespinasse struct vm_area_struct *left = 1582*db4fbfb9SMichel Lespinasse rb_entry(vma->vm_rb.rb_left, 1583*db4fbfb9SMichel Lespinasse struct vm_area_struct, vm_rb); 1584*db4fbfb9SMichel Lespinasse if (left->rb_subtree_gap >= length) { 1585*db4fbfb9SMichel Lespinasse vma = left; 1586*db4fbfb9SMichel Lespinasse continue; 1587*db4fbfb9SMichel Lespinasse } 1588*db4fbfb9SMichel Lespinasse } 1589*db4fbfb9SMichel Lespinasse 1590*db4fbfb9SMichel Lespinasse gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0; 1591*db4fbfb9SMichel Lespinasse check_current: 1592*db4fbfb9SMichel Lespinasse /* Check if current node has a suitable gap */ 1593*db4fbfb9SMichel Lespinasse if (gap_start > high_limit) 1594*db4fbfb9SMichel Lespinasse return -ENOMEM; 1595*db4fbfb9SMichel Lespinasse if (gap_end >= low_limit && gap_end - gap_start >= length) 1596*db4fbfb9SMichel Lespinasse goto found; 1597*db4fbfb9SMichel Lespinasse 1598*db4fbfb9SMichel Lespinasse /* Visit right subtree if it looks promising */ 1599*db4fbfb9SMichel Lespinasse if (vma->vm_rb.rb_right) { 1600*db4fbfb9SMichel Lespinasse struct vm_area_struct *right = 1601*db4fbfb9SMichel Lespinasse rb_entry(vma->vm_rb.rb_right, 1602*db4fbfb9SMichel Lespinasse struct vm_area_struct, vm_rb); 1603*db4fbfb9SMichel Lespinasse if (right->rb_subtree_gap >= length) { 1604*db4fbfb9SMichel Lespinasse vma = right; 1605*db4fbfb9SMichel Lespinasse continue; 1606*db4fbfb9SMichel Lespinasse } 1607*db4fbfb9SMichel Lespinasse } 1608*db4fbfb9SMichel Lespinasse 1609*db4fbfb9SMichel Lespinasse /* Go back up the rbtree to find next candidate node */ 1610*db4fbfb9SMichel Lespinasse while (true) { 1611*db4fbfb9SMichel Lespinasse struct rb_node *prev = &vma->vm_rb; 1612*db4fbfb9SMichel Lespinasse if (!rb_parent(prev)) 1613*db4fbfb9SMichel Lespinasse goto check_highest; 1614*db4fbfb9SMichel Lespinasse vma = rb_entry(rb_parent(prev), 1615*db4fbfb9SMichel Lespinasse struct vm_area_struct, vm_rb); 1616*db4fbfb9SMichel Lespinasse if (prev == vma->vm_rb.rb_left) { 1617*db4fbfb9SMichel Lespinasse gap_start = vma->vm_prev->vm_end; 1618*db4fbfb9SMichel Lespinasse gap_end = vma->vm_start; 1619*db4fbfb9SMichel Lespinasse goto check_current; 1620*db4fbfb9SMichel Lespinasse } 1621*db4fbfb9SMichel Lespinasse } 1622*db4fbfb9SMichel Lespinasse } 1623*db4fbfb9SMichel Lespinasse 1624*db4fbfb9SMichel Lespinasse check_highest: 1625*db4fbfb9SMichel Lespinasse /* Check highest gap, which does not precede any rbtree node */ 1626*db4fbfb9SMichel Lespinasse gap_start = mm->highest_vm_end; 1627*db4fbfb9SMichel Lespinasse gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */ 1628*db4fbfb9SMichel Lespinasse if (gap_start > high_limit) 1629*db4fbfb9SMichel Lespinasse return -ENOMEM; 1630*db4fbfb9SMichel Lespinasse 1631*db4fbfb9SMichel Lespinasse found: 1632*db4fbfb9SMichel Lespinasse /* We found a suitable gap. Clip it with the original low_limit. */ 1633*db4fbfb9SMichel Lespinasse if (gap_start < info->low_limit) 1634*db4fbfb9SMichel Lespinasse gap_start = info->low_limit; 1635*db4fbfb9SMichel Lespinasse 1636*db4fbfb9SMichel Lespinasse /* Adjust gap address to the desired alignment */ 1637*db4fbfb9SMichel Lespinasse gap_start += (info->align_offset - gap_start) & info->align_mask; 1638*db4fbfb9SMichel Lespinasse 1639*db4fbfb9SMichel Lespinasse VM_BUG_ON(gap_start + info->length > info->high_limit); 1640*db4fbfb9SMichel Lespinasse VM_BUG_ON(gap_start + info->length > gap_end); 1641*db4fbfb9SMichel Lespinasse return gap_start; 1642*db4fbfb9SMichel Lespinasse } 1643*db4fbfb9SMichel Lespinasse 1644*db4fbfb9SMichel Lespinasse unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 1645*db4fbfb9SMichel Lespinasse { 1646*db4fbfb9SMichel Lespinasse struct mm_struct *mm = current->mm; 1647*db4fbfb9SMichel Lespinasse struct vm_area_struct *vma; 1648*db4fbfb9SMichel Lespinasse unsigned long length, low_limit, high_limit, gap_start, gap_end; 1649*db4fbfb9SMichel Lespinasse 1650*db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1651*db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1652*db4fbfb9SMichel Lespinasse if (length < info->length) 1653*db4fbfb9SMichel Lespinasse return -ENOMEM; 1654*db4fbfb9SMichel Lespinasse 1655*db4fbfb9SMichel Lespinasse /* 1656*db4fbfb9SMichel Lespinasse * Adjust search limits by the desired length. 1657*db4fbfb9SMichel Lespinasse * See implementation comment at top of unmapped_area(). 1658*db4fbfb9SMichel Lespinasse */ 1659*db4fbfb9SMichel Lespinasse gap_end = info->high_limit; 1660*db4fbfb9SMichel Lespinasse if (gap_end < length) 1661*db4fbfb9SMichel Lespinasse return -ENOMEM; 1662*db4fbfb9SMichel Lespinasse high_limit = gap_end - length; 1663*db4fbfb9SMichel Lespinasse 1664*db4fbfb9SMichel Lespinasse if (info->low_limit > high_limit) 1665*db4fbfb9SMichel Lespinasse return -ENOMEM; 1666*db4fbfb9SMichel Lespinasse low_limit = info->low_limit + length; 1667*db4fbfb9SMichel Lespinasse 1668*db4fbfb9SMichel Lespinasse /* Check highest gap, which does not precede any rbtree node */ 1669*db4fbfb9SMichel Lespinasse gap_start = mm->highest_vm_end; 1670*db4fbfb9SMichel Lespinasse if (gap_start <= high_limit) 1671*db4fbfb9SMichel Lespinasse goto found_highest; 1672*db4fbfb9SMichel Lespinasse 1673*db4fbfb9SMichel Lespinasse /* Check if rbtree root looks promising */ 1674*db4fbfb9SMichel Lespinasse if (RB_EMPTY_ROOT(&mm->mm_rb)) 1675*db4fbfb9SMichel Lespinasse return -ENOMEM; 1676*db4fbfb9SMichel Lespinasse vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); 1677*db4fbfb9SMichel Lespinasse if (vma->rb_subtree_gap < length) 1678*db4fbfb9SMichel Lespinasse return -ENOMEM; 1679*db4fbfb9SMichel Lespinasse 1680*db4fbfb9SMichel Lespinasse while (true) { 1681*db4fbfb9SMichel Lespinasse /* Visit right subtree if it looks promising */ 1682*db4fbfb9SMichel Lespinasse gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0; 1683*db4fbfb9SMichel Lespinasse if (gap_start <= high_limit && vma->vm_rb.rb_right) { 1684*db4fbfb9SMichel Lespinasse struct vm_area_struct *right = 1685*db4fbfb9SMichel Lespinasse rb_entry(vma->vm_rb.rb_right, 1686*db4fbfb9SMichel Lespinasse struct vm_area_struct, vm_rb); 1687*db4fbfb9SMichel Lespinasse if (right->rb_subtree_gap >= length) { 1688*db4fbfb9SMichel Lespinasse vma = right; 1689*db4fbfb9SMichel Lespinasse continue; 1690*db4fbfb9SMichel Lespinasse } 1691*db4fbfb9SMichel Lespinasse } 1692*db4fbfb9SMichel Lespinasse 1693*db4fbfb9SMichel Lespinasse check_current: 1694*db4fbfb9SMichel Lespinasse /* Check if current node has a suitable gap */ 1695*db4fbfb9SMichel Lespinasse gap_end = vma->vm_start; 1696*db4fbfb9SMichel Lespinasse if (gap_end < low_limit) 1697*db4fbfb9SMichel Lespinasse return -ENOMEM; 1698*db4fbfb9SMichel Lespinasse if (gap_start <= high_limit && gap_end - gap_start >= length) 1699*db4fbfb9SMichel Lespinasse goto found; 1700*db4fbfb9SMichel Lespinasse 1701*db4fbfb9SMichel Lespinasse /* Visit left subtree if it looks promising */ 1702*db4fbfb9SMichel Lespinasse if (vma->vm_rb.rb_left) { 1703*db4fbfb9SMichel Lespinasse struct vm_area_struct *left = 1704*db4fbfb9SMichel Lespinasse rb_entry(vma->vm_rb.rb_left, 1705*db4fbfb9SMichel Lespinasse struct vm_area_struct, vm_rb); 1706*db4fbfb9SMichel Lespinasse if (left->rb_subtree_gap >= length) { 1707*db4fbfb9SMichel Lespinasse vma = left; 1708*db4fbfb9SMichel Lespinasse continue; 1709*db4fbfb9SMichel Lespinasse } 1710*db4fbfb9SMichel Lespinasse } 1711*db4fbfb9SMichel Lespinasse 1712*db4fbfb9SMichel Lespinasse /* Go back up the rbtree to find next candidate node */ 1713*db4fbfb9SMichel Lespinasse while (true) { 1714*db4fbfb9SMichel Lespinasse struct rb_node *prev = &vma->vm_rb; 1715*db4fbfb9SMichel Lespinasse if (!rb_parent(prev)) 1716*db4fbfb9SMichel Lespinasse return -ENOMEM; 1717*db4fbfb9SMichel Lespinasse vma = rb_entry(rb_parent(prev), 1718*db4fbfb9SMichel Lespinasse struct vm_area_struct, vm_rb); 1719*db4fbfb9SMichel Lespinasse if (prev == vma->vm_rb.rb_right) { 1720*db4fbfb9SMichel Lespinasse gap_start = vma->vm_prev ? 1721*db4fbfb9SMichel Lespinasse vma->vm_prev->vm_end : 0; 1722*db4fbfb9SMichel Lespinasse goto check_current; 1723*db4fbfb9SMichel Lespinasse } 1724*db4fbfb9SMichel Lespinasse } 1725*db4fbfb9SMichel Lespinasse } 1726*db4fbfb9SMichel Lespinasse 1727*db4fbfb9SMichel Lespinasse found: 1728*db4fbfb9SMichel Lespinasse /* We found a suitable gap. Clip it with the original high_limit. */ 1729*db4fbfb9SMichel Lespinasse if (gap_end > info->high_limit) 1730*db4fbfb9SMichel Lespinasse gap_end = info->high_limit; 1731*db4fbfb9SMichel Lespinasse 1732*db4fbfb9SMichel Lespinasse found_highest: 1733*db4fbfb9SMichel Lespinasse /* Compute highest gap address at the desired alignment */ 1734*db4fbfb9SMichel Lespinasse gap_end -= info->length; 1735*db4fbfb9SMichel Lespinasse gap_end -= (gap_end - info->align_offset) & info->align_mask; 1736*db4fbfb9SMichel Lespinasse 1737*db4fbfb9SMichel Lespinasse VM_BUG_ON(gap_end < info->low_limit); 1738*db4fbfb9SMichel Lespinasse VM_BUG_ON(gap_end < gap_start); 1739*db4fbfb9SMichel Lespinasse return gap_end; 1740*db4fbfb9SMichel Lespinasse } 1741*db4fbfb9SMichel Lespinasse 17421da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 17431da177e4SLinus Torvalds * For shmat() with addr=0. 17441da177e4SLinus Torvalds * 17451da177e4SLinus Torvalds * Ugly calling convention alert: 17461da177e4SLinus Torvalds * Return value with the low bits set means error value, 17471da177e4SLinus Torvalds * ie 17481da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 17491da177e4SLinus Torvalds * error = ret; 17501da177e4SLinus Torvalds * 17511da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 17521da177e4SLinus Torvalds */ 17531da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA 17541da177e4SLinus Torvalds unsigned long 17551da177e4SLinus Torvalds arch_get_unmapped_area(struct file *filp, unsigned long addr, 17561da177e4SLinus Torvalds unsigned long len, unsigned long pgoff, unsigned long flags) 17571da177e4SLinus Torvalds { 17581da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 17591da177e4SLinus Torvalds struct vm_area_struct *vma; 1760*db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds if (len > TASK_SIZE) 17631da177e4SLinus Torvalds return -ENOMEM; 17641da177e4SLinus Torvalds 176506abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 176606abdfb4SBenjamin Herrenschmidt return addr; 176706abdfb4SBenjamin Herrenschmidt 17681da177e4SLinus Torvalds if (addr) { 17691da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 17701da177e4SLinus Torvalds vma = find_vma(mm, addr); 17711da177e4SLinus Torvalds if (TASK_SIZE - len >= addr && 17721da177e4SLinus Torvalds (!vma || addr + len <= vma->vm_start)) 17731da177e4SLinus Torvalds return addr; 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 1776*db4fbfb9SMichel Lespinasse info.flags = 0; 1777*db4fbfb9SMichel Lespinasse info.length = len; 1778*db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1779*db4fbfb9SMichel Lespinasse info.high_limit = TASK_SIZE; 1780*db4fbfb9SMichel Lespinasse info.align_mask = 0; 1781*db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds #endif 17841da177e4SLinus Torvalds 17851363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr) 17861da177e4SLinus Torvalds { 17871da177e4SLinus Torvalds /* 17881da177e4SLinus Torvalds * Is this a new hole at the lowest possible address? 17891da177e4SLinus Torvalds */ 1790f44d2198SXiao Guangrong if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) 17911363c3cdSWolfgang Wander mm->free_area_cache = addr; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds /* 17951da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 17961da177e4SLinus Torvalds * stack's low limit (the base): 17971da177e4SLinus Torvalds */ 17981da177e4SLinus Torvalds #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 17991da177e4SLinus Torvalds unsigned long 18001da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, 18011da177e4SLinus Torvalds const unsigned long len, const unsigned long pgoff, 18021da177e4SLinus Torvalds const unsigned long flags) 18031da177e4SLinus Torvalds { 18041da177e4SLinus Torvalds struct vm_area_struct *vma; 18051da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1806*db4fbfb9SMichel Lespinasse unsigned long addr = addr0; 1807*db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds /* requested length too big for entire address space */ 18101da177e4SLinus Torvalds if (len > TASK_SIZE) 18111da177e4SLinus Torvalds return -ENOMEM; 18121da177e4SLinus Torvalds 181306abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 181406abdfb4SBenjamin Herrenschmidt return addr; 181506abdfb4SBenjamin Herrenschmidt 18161da177e4SLinus Torvalds /* requesting a specific address */ 18171da177e4SLinus Torvalds if (addr) { 18181da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 18191da177e4SLinus Torvalds vma = find_vma(mm, addr); 18201da177e4SLinus Torvalds if (TASK_SIZE - len >= addr && 18211da177e4SLinus Torvalds (!vma || addr + len <= vma->vm_start)) 18221da177e4SLinus Torvalds return addr; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 1825*db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 1826*db4fbfb9SMichel Lespinasse info.length = len; 1827*db4fbfb9SMichel Lespinasse info.low_limit = PAGE_SIZE; 1828*db4fbfb9SMichel Lespinasse info.high_limit = mm->mmap_base; 1829*db4fbfb9SMichel Lespinasse info.align_mask = 0; 1830*db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1831b716ad95SXiao Guangrong 18321da177e4SLinus Torvalds /* 18331da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 18341da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 18351da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 18361da177e4SLinus Torvalds * allocations. 18371da177e4SLinus Torvalds */ 1838*db4fbfb9SMichel Lespinasse if (addr & ~PAGE_MASK) { 1839*db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 1840*db4fbfb9SMichel Lespinasse info.flags = 0; 1841*db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1842*db4fbfb9SMichel Lespinasse info.high_limit = TASK_SIZE; 1843*db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1844*db4fbfb9SMichel Lespinasse } 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds return addr; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds #endif 18491da177e4SLinus Torvalds 18501363c3cdSWolfgang Wander void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr) 18511da177e4SLinus Torvalds { 18521da177e4SLinus Torvalds /* 18531da177e4SLinus Torvalds * Is this a new hole at the highest possible address? 18541da177e4SLinus Torvalds */ 18551363c3cdSWolfgang Wander if (addr > mm->free_area_cache) 18561363c3cdSWolfgang Wander mm->free_area_cache = addr; 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds /* dont allow allocations above current base */ 18591363c3cdSWolfgang Wander if (mm->free_area_cache > mm->mmap_base) 18601363c3cdSWolfgang Wander mm->free_area_cache = mm->mmap_base; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds unsigned long 18641da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 18651da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 18661da177e4SLinus Torvalds { 186706abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 186806abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 186907ab67c8SLinus Torvalds 18709206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 18719206de95SAl Viro if (error) 18729206de95SAl Viro return error; 18739206de95SAl Viro 18749206de95SAl Viro /* Careful about overflows.. */ 18759206de95SAl Viro if (len > TASK_SIZE) 18769206de95SAl Viro return -ENOMEM; 18779206de95SAl Viro 187807ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 187907ab67c8SLinus Torvalds if (file && file->f_op && file->f_op->get_unmapped_area) 188007ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 188107ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 188207ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 188307ab67c8SLinus Torvalds return addr; 188407ab67c8SLinus Torvalds 18851da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 18861da177e4SLinus Torvalds return -ENOMEM; 18871da177e4SLinus Torvalds if (addr & ~PAGE_MASK) 18881da177e4SLinus Torvalds return -EINVAL; 188906abdfb4SBenjamin Herrenschmidt 18909ac4ed4bSAl Viro addr = arch_rebalance_pgtables(addr, len); 18919ac4ed4bSAl Viro error = security_mmap_addr(addr); 18929ac4ed4bSAl Viro return error ? error : addr; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ 18981da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 18991da177e4SLinus Torvalds { 19001da177e4SLinus Torvalds struct vm_area_struct *vma = NULL; 19011da177e4SLinus Torvalds 1902841e31e5SRajman Mekaco if (WARN_ON_ONCE(!mm)) /* Remove this in linux-3.6 */ 1903841e31e5SRajman Mekaco return NULL; 1904841e31e5SRajman Mekaco 19051da177e4SLinus Torvalds /* Check the cache first. */ 19061da177e4SLinus Torvalds /* (Cache hit rate is typically around 35%.) */ 19071da177e4SLinus Torvalds vma = mm->mmap_cache; 19081da177e4SLinus Torvalds if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) { 19091da177e4SLinus Torvalds struct rb_node *rb_node; 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds rb_node = mm->mm_rb.rb_node; 19121da177e4SLinus Torvalds vma = NULL; 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds while (rb_node) { 19151da177e4SLinus Torvalds struct vm_area_struct *vma_tmp; 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds vma_tmp = rb_entry(rb_node, 19181da177e4SLinus Torvalds struct vm_area_struct, vm_rb); 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds if (vma_tmp->vm_end > addr) { 19211da177e4SLinus Torvalds vma = vma_tmp; 19221da177e4SLinus Torvalds if (vma_tmp->vm_start <= addr) 19231da177e4SLinus Torvalds break; 19241da177e4SLinus Torvalds rb_node = rb_node->rb_left; 19251da177e4SLinus Torvalds } else 19261da177e4SLinus Torvalds rb_node = rb_node->rb_right; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds if (vma) 19291da177e4SLinus Torvalds mm->mmap_cache = vma; 19301da177e4SLinus Torvalds } 19311da177e4SLinus Torvalds return vma; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 19351da177e4SLinus Torvalds 19366bd4837dSKOSAKI Motohiro /* 19376bd4837dSKOSAKI Motohiro * Same as find_vma, but also return a pointer to the previous VMA in *pprev. 19386bd4837dSKOSAKI Motohiro */ 19391da177e4SLinus Torvalds struct vm_area_struct * 19401da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 19411da177e4SLinus Torvalds struct vm_area_struct **pprev) 19421da177e4SLinus Torvalds { 19436bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 19441da177e4SLinus Torvalds 19456bd4837dSKOSAKI Motohiro vma = find_vma(mm, addr); 194683cd904dSMikulas Patocka if (vma) { 194783cd904dSMikulas Patocka *pprev = vma->vm_prev; 194883cd904dSMikulas Patocka } else { 194983cd904dSMikulas Patocka struct rb_node *rb_node = mm->mm_rb.rb_node; 195083cd904dSMikulas Patocka *pprev = NULL; 195183cd904dSMikulas Patocka while (rb_node) { 195283cd904dSMikulas Patocka *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb); 195383cd904dSMikulas Patocka rb_node = rb_node->rb_right; 195483cd904dSMikulas Patocka } 195583cd904dSMikulas Patocka } 19566bd4837dSKOSAKI Motohiro return vma; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds /* 19601da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 19611da177e4SLinus Torvalds * update accounting. This is shared with both the 19621da177e4SLinus Torvalds * grow-up and grow-down cases. 19631da177e4SLinus Torvalds */ 19641da177e4SLinus Torvalds static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow) 19651da177e4SLinus Torvalds { 19661da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 19671da177e4SLinus Torvalds struct rlimit *rlim = current->signal->rlim; 19680d59a01bSAdam Litke unsigned long new_start; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /* address space limit tests */ 1971119f657cSakpm@osdl.org if (!may_expand_vm(mm, grow)) 19721da177e4SLinus Torvalds return -ENOMEM; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds /* Stack limit test */ 197559e99e5bSJiri Slaby if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur)) 19761da177e4SLinus Torvalds return -ENOMEM; 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds /* mlock limit tests */ 19791da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) { 19801da177e4SLinus Torvalds unsigned long locked; 19811da177e4SLinus Torvalds unsigned long limit; 19821da177e4SLinus Torvalds locked = mm->locked_vm + grow; 198359e99e5bSJiri Slaby limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur); 198459e99e5bSJiri Slaby limit >>= PAGE_SHIFT; 19851da177e4SLinus Torvalds if (locked > limit && !capable(CAP_IPC_LOCK)) 19861da177e4SLinus Torvalds return -ENOMEM; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19890d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 19900d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 19910d59a01bSAdam Litke vma->vm_end - size; 19920d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 19930d59a01bSAdam Litke return -EFAULT; 19940d59a01bSAdam Litke 19951da177e4SLinus Torvalds /* 19961da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 19971da177e4SLinus Torvalds * update security statistics. 19981da177e4SLinus Torvalds */ 199905fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 20001da177e4SLinus Torvalds return -ENOMEM; 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds /* Ok, everything looks good - let it rip */ 20031da177e4SLinus Torvalds if (vma->vm_flags & VM_LOCKED) 20041da177e4SLinus Torvalds mm->locked_vm += grow; 2005ab50b8edSHugh Dickins vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow); 20061da177e4SLinus Torvalds return 0; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds 200946dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 20101da177e4SLinus Torvalds /* 201146dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 201246dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 20131da177e4SLinus Torvalds */ 201446dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds int error; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 20191da177e4SLinus Torvalds return -EFAULT; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* 20221da177e4SLinus Torvalds * We must make sure the anon_vma is allocated 20231da177e4SLinus Torvalds * so that the anon_vma locking is not a noop. 20241da177e4SLinus Torvalds */ 20251da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 20261da177e4SLinus Torvalds return -ENOMEM; 2027bb4a340eSRik van Riel vma_lock_anon_vma(vma); 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds /* 20301da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 20311da177e4SLinus Torvalds * is required to hold the mmap_sem in read mode. We need the 20321da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 203306b32f3aSHelge Deller * Also guard against wrapping around to address 0. 20341da177e4SLinus Torvalds */ 203506b32f3aSHelge Deller if (address < PAGE_ALIGN(address+4)) 203606b32f3aSHelge Deller address = PAGE_ALIGN(address+4); 203706b32f3aSHelge Deller else { 2038bb4a340eSRik van Riel vma_unlock_anon_vma(vma); 203906b32f3aSHelge Deller return -ENOMEM; 204006b32f3aSHelge Deller } 20411da177e4SLinus Torvalds error = 0; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 20441da177e4SLinus Torvalds if (address > vma->vm_end) { 20451da177e4SLinus Torvalds unsigned long size, grow; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds size = address - vma->vm_start; 20481da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 20491da177e4SLinus Torvalds 205042c36f63SHugh Dickins error = -ENOMEM; 205142c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 20521da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 20533af9e859SEric B Munson if (!error) { 2054bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 20551da177e4SLinus Torvalds vma->vm_end = address; 2056bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 2057d3737187SMichel Lespinasse if (vma->vm_next) 2058d3737187SMichel Lespinasse vma_gap_update(vma->vm_next); 2059d3737187SMichel Lespinasse else 2060d3737187SMichel Lespinasse vma->vm_mm->highest_vm_end = address; 20613af9e859SEric B Munson perf_event_mmap(vma); 20623af9e859SEric B Munson } 20631da177e4SLinus Torvalds } 206442c36f63SHugh Dickins } 2065bb4a340eSRik van Riel vma_unlock_anon_vma(vma); 2066b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(vma); 2067ed8ea815SMichel Lespinasse validate_mm(vma->vm_mm); 20681da177e4SLinus Torvalds return error; 20691da177e4SLinus Torvalds } 207046dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 207146dea3d0SHugh Dickins 20721da177e4SLinus Torvalds /* 20731da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 20741da177e4SLinus Torvalds */ 2075d05f3169SMichal Hocko int expand_downwards(struct vm_area_struct *vma, 2076b6a2fea3SOllie Wild unsigned long address) 20771da177e4SLinus Torvalds { 20781da177e4SLinus Torvalds int error; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /* 20811da177e4SLinus Torvalds * We must make sure the anon_vma is allocated 20821da177e4SLinus Torvalds * so that the anon_vma locking is not a noop. 20831da177e4SLinus Torvalds */ 20841da177e4SLinus Torvalds if (unlikely(anon_vma_prepare(vma))) 20851da177e4SLinus Torvalds return -ENOMEM; 20868869477aSEric Paris 20878869477aSEric Paris address &= PAGE_MASK; 2088e5467859SAl Viro error = security_mmap_addr(address); 20898869477aSEric Paris if (error) 20908869477aSEric Paris return error; 20918869477aSEric Paris 2092bb4a340eSRik van Riel vma_lock_anon_vma(vma); 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds /* 20951da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 20961da177e4SLinus Torvalds * is required to hold the mmap_sem in read mode. We need the 20971da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 20981da177e4SLinus Torvalds */ 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 21011da177e4SLinus Torvalds if (address < vma->vm_start) { 21021da177e4SLinus Torvalds unsigned long size, grow; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds size = vma->vm_end - address; 21051da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 21061da177e4SLinus Torvalds 2107a626ca6aSLinus Torvalds error = -ENOMEM; 2108a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 21091da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 21101da177e4SLinus Torvalds if (!error) { 2111bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 21121da177e4SLinus Torvalds vma->vm_start = address; 21131da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2114bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 2115d3737187SMichel Lespinasse vma_gap_update(vma); 21163af9e859SEric B Munson perf_event_mmap(vma); 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds } 2119a626ca6aSLinus Torvalds } 2120bb4a340eSRik van Riel vma_unlock_anon_vma(vma); 2121b15d00b6SAndrea Arcangeli khugepaged_enter_vma_merge(vma); 2122ed8ea815SMichel Lespinasse validate_mm(vma->vm_mm); 21231da177e4SLinus Torvalds return error; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 2126b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2127b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2128b6a2fea3SOllie Wild { 2129b6a2fea3SOllie Wild return expand_upwards(vma, address); 2130b6a2fea3SOllie Wild } 2131b6a2fea3SOllie Wild 2132b6a2fea3SOllie Wild struct vm_area_struct * 2133b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2134b6a2fea3SOllie Wild { 2135b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2136b6a2fea3SOllie Wild 2137b6a2fea3SOllie Wild addr &= PAGE_MASK; 2138b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2139b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2140b6a2fea3SOllie Wild return vma; 21411c127185SDenys Vlasenko if (!prev || expand_stack(prev, addr)) 2142b6a2fea3SOllie Wild return NULL; 2143ba470de4SRik van Riel if (prev->vm_flags & VM_LOCKED) { 2144c58267c3SKOSAKI Motohiro mlock_vma_pages_range(prev, addr, prev->vm_end); 2145ba470de4SRik van Riel } 2146b6a2fea3SOllie Wild return prev; 2147b6a2fea3SOllie Wild } 2148b6a2fea3SOllie Wild #else 2149b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2150b6a2fea3SOllie Wild { 2151b6a2fea3SOllie Wild return expand_downwards(vma, address); 2152b6a2fea3SOllie Wild } 2153b6a2fea3SOllie Wild 21541da177e4SLinus Torvalds struct vm_area_struct * 21551da177e4SLinus Torvalds find_extend_vma(struct mm_struct * mm, unsigned long addr) 21561da177e4SLinus Torvalds { 21571da177e4SLinus Torvalds struct vm_area_struct * vma; 21581da177e4SLinus Torvalds unsigned long start; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds addr &= PAGE_MASK; 21611da177e4SLinus Torvalds vma = find_vma(mm,addr); 21621da177e4SLinus Torvalds if (!vma) 21631da177e4SLinus Torvalds return NULL; 21641da177e4SLinus Torvalds if (vma->vm_start <= addr) 21651da177e4SLinus Torvalds return vma; 21661da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 21671da177e4SLinus Torvalds return NULL; 21681da177e4SLinus Torvalds start = vma->vm_start; 21691da177e4SLinus Torvalds if (expand_stack(vma, addr)) 21701da177e4SLinus Torvalds return NULL; 2171ba470de4SRik van Riel if (vma->vm_flags & VM_LOCKED) { 2172c58267c3SKOSAKI Motohiro mlock_vma_pages_range(vma, addr, start); 2173ba470de4SRik van Riel } 21741da177e4SLinus Torvalds return vma; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds #endif 21771da177e4SLinus Torvalds 21782c0b3814SHugh Dickins /* 21792c0b3814SHugh Dickins * Ok - we have the memory areas we should free on the vma list, 21802c0b3814SHugh Dickins * so release them, and do the vma updates. 21811da177e4SLinus Torvalds * 21822c0b3814SHugh Dickins * Called with the mm semaphore held. 21831da177e4SLinus Torvalds */ 21842c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) 21851da177e4SLinus Torvalds { 21864f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 21874f74d2c8SLinus Torvalds 2188365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2189365e9c87SHugh Dickins update_hiwater_vm(mm); 21902c0b3814SHugh Dickins do { 2191ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 21921da177e4SLinus Torvalds 21934f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 21944f74d2c8SLinus Torvalds nr_accounted += nrpages; 2195ab50b8edSHugh Dickins vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages); 2196a8fb5618SHugh Dickins vma = remove_vma(vma); 2197146425a3SHugh Dickins } while (vma); 21984f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 21991da177e4SLinus Torvalds validate_mm(mm); 22001da177e4SLinus Torvalds } 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds /* 22031da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 22041da177e4SLinus Torvalds * 2205f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 22061da177e4SLinus Torvalds */ 22071da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm, 2208e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2209e0da382cSHugh Dickins unsigned long start, unsigned long end) 22101da177e4SLinus Torvalds { 2211e0da382cSHugh Dickins struct vm_area_struct *next = prev? prev->vm_next: mm->mmap; 2212d16dfc55SPeter Zijlstra struct mmu_gather tlb; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds lru_add_drain(); 2215d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 0); 2216365e9c87SHugh Dickins update_hiwater_rss(mm); 22174f74d2c8SLinus Torvalds unmap_vmas(&tlb, vma, start, end); 2218d16dfc55SPeter Zijlstra free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 2219e0da382cSHugh Dickins next ? next->vm_start : 0); 2220d16dfc55SPeter Zijlstra tlb_finish_mmu(&tlb, start, end); 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds /* 22241da177e4SLinus Torvalds * Create a list of vma's touched by the unmap, removing them from the mm's 22251da177e4SLinus Torvalds * vma list as we go.. 22261da177e4SLinus Torvalds */ 22271da177e4SLinus Torvalds static void 22281da177e4SLinus Torvalds detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, 22291da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long end) 22301da177e4SLinus Torvalds { 22311da177e4SLinus Torvalds struct vm_area_struct **insertion_point; 22321da177e4SLinus Torvalds struct vm_area_struct *tail_vma = NULL; 22331363c3cdSWolfgang Wander unsigned long addr; 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds insertion_point = (prev ? &prev->vm_next : &mm->mmap); 2236297c5eeeSLinus Torvalds vma->vm_prev = NULL; 22371da177e4SLinus Torvalds do { 2238d3737187SMichel Lespinasse vma_rb_erase(vma, &mm->mm_rb); 22391da177e4SLinus Torvalds mm->map_count--; 22401da177e4SLinus Torvalds tail_vma = vma; 22411da177e4SLinus Torvalds vma = vma->vm_next; 22421da177e4SLinus Torvalds } while (vma && vma->vm_start < end); 22431da177e4SLinus Torvalds *insertion_point = vma; 2244d3737187SMichel Lespinasse if (vma) { 2245297c5eeeSLinus Torvalds vma->vm_prev = prev; 2246d3737187SMichel Lespinasse vma_gap_update(vma); 2247d3737187SMichel Lespinasse } else 2248d3737187SMichel Lespinasse mm->highest_vm_end = prev ? prev->vm_end : 0; 22491da177e4SLinus Torvalds tail_vma->vm_next = NULL; 22501363c3cdSWolfgang Wander if (mm->unmap_area == arch_unmap_area) 22511363c3cdSWolfgang Wander addr = prev ? prev->vm_end : mm->mmap_base; 22521363c3cdSWolfgang Wander else 22531363c3cdSWolfgang Wander addr = vma ? vma->vm_start : mm->mmap_base; 22541363c3cdSWolfgang Wander mm->unmap_area(mm, addr); 22551da177e4SLinus Torvalds mm->mmap_cache = NULL; /* Kill the cache. */ 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds /* 2259659ace58SKOSAKI Motohiro * __split_vma() bypasses sysctl_max_map_count checking. We use this on the 2260659ace58SKOSAKI Motohiro * munmap path where it doesn't make sense to fail. 22611da177e4SLinus Torvalds */ 2262659ace58SKOSAKI Motohiro static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma, 22631da177e4SLinus Torvalds unsigned long addr, int new_below) 22641da177e4SLinus Torvalds { 22651da177e4SLinus Torvalds struct mempolicy *pol; 22661da177e4SLinus Torvalds struct vm_area_struct *new; 22675beb4930SRik van Riel int err = -ENOMEM; 22681da177e4SLinus Torvalds 2269a5516438SAndi Kleen if (is_vm_hugetlb_page(vma) && (addr & 2270a5516438SAndi Kleen ~(huge_page_mask(hstate_vma(vma))))) 22711da177e4SLinus Torvalds return -EINVAL; 22721da177e4SLinus Torvalds 2273e94b1766SChristoph Lameter new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 22741da177e4SLinus Torvalds if (!new) 22755beb4930SRik van Riel goto out_err; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds /* most fields are the same, copy all, and then fixup */ 22781da177e4SLinus Torvalds *new = *vma; 22791da177e4SLinus Torvalds 22805beb4930SRik van Riel INIT_LIST_HEAD(&new->anon_vma_chain); 22815beb4930SRik van Riel 22821da177e4SLinus Torvalds if (new_below) 22831da177e4SLinus Torvalds new->vm_end = addr; 22841da177e4SLinus Torvalds else { 22851da177e4SLinus Torvalds new->vm_start = addr; 22861da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 2289846a16bfSLee Schermerhorn pol = mpol_dup(vma_policy(vma)); 22901da177e4SLinus Torvalds if (IS_ERR(pol)) { 22915beb4930SRik van Riel err = PTR_ERR(pol); 22925beb4930SRik van Riel goto out_free_vma; 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds vma_set_policy(new, pol); 22951da177e4SLinus Torvalds 22965beb4930SRik van Riel if (anon_vma_clone(new, vma)) 22975beb4930SRik van Riel goto out_free_mpol; 22985beb4930SRik van Riel 2299e9714acfSKonstantin Khlebnikov if (new->vm_file) 23001da177e4SLinus Torvalds get_file(new->vm_file); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 23031da177e4SLinus Torvalds new->vm_ops->open(new); 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds if (new_below) 23065beb4930SRik van Riel err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + 23071da177e4SLinus Torvalds ((addr - new->vm_start) >> PAGE_SHIFT), new); 23081da177e4SLinus Torvalds else 23095beb4930SRik van Riel err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); 23101da177e4SLinus Torvalds 23115beb4930SRik van Riel /* Success. */ 23125beb4930SRik van Riel if (!err) 23131da177e4SLinus Torvalds return 0; 23145beb4930SRik van Riel 23155beb4930SRik van Riel /* Clean everything up if vma_adjust failed. */ 231658927533SRik van Riel if (new->vm_ops && new->vm_ops->close) 23175beb4930SRik van Riel new->vm_ops->close(new); 2318e9714acfSKonstantin Khlebnikov if (new->vm_file) 23195beb4930SRik van Riel fput(new->vm_file); 23202aeadc30SAndrea Arcangeli unlink_anon_vmas(new); 23215beb4930SRik van Riel out_free_mpol: 23225beb4930SRik van Riel mpol_put(pol); 23235beb4930SRik van Riel out_free_vma: 23245beb4930SRik van Riel kmem_cache_free(vm_area_cachep, new); 23255beb4930SRik van Riel out_err: 23265beb4930SRik van Riel return err; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds 2329659ace58SKOSAKI Motohiro /* 2330659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2331659ace58SKOSAKI Motohiro * either for the first part or the tail. 2332659ace58SKOSAKI Motohiro */ 2333659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 2334659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2335659ace58SKOSAKI Motohiro { 2336659ace58SKOSAKI Motohiro if (mm->map_count >= sysctl_max_map_count) 2337659ace58SKOSAKI Motohiro return -ENOMEM; 2338659ace58SKOSAKI Motohiro 2339659ace58SKOSAKI Motohiro return __split_vma(mm, vma, addr, new_below); 2340659ace58SKOSAKI Motohiro } 2341659ace58SKOSAKI Motohiro 23421da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds 23431da177e4SLinus Torvalds * what needs doing, and the areas themselves, which do the 23441da177e4SLinus Torvalds * work. This now handles partial unmappings. 23451da177e4SLinus Torvalds * Jeremy Fitzhardinge <jeremy@goop.org> 23461da177e4SLinus Torvalds */ 23471da177e4SLinus Torvalds int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 23481da177e4SLinus Torvalds { 23491da177e4SLinus Torvalds unsigned long end; 2350146425a3SHugh Dickins struct vm_area_struct *vma, *prev, *last; 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start) 23531da177e4SLinus Torvalds return -EINVAL; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds if ((len = PAGE_ALIGN(len)) == 0) 23561da177e4SLinus Torvalds return -EINVAL; 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds /* Find the first overlapping VMA */ 23599be34c9dSLinus Torvalds vma = find_vma(mm, start); 2360146425a3SHugh Dickins if (!vma) 23611da177e4SLinus Torvalds return 0; 23629be34c9dSLinus Torvalds prev = vma->vm_prev; 2363146425a3SHugh Dickins /* we have start < vma->vm_end */ 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds /* if it doesn't overlap, we have nothing.. */ 23661da177e4SLinus Torvalds end = start + len; 2367146425a3SHugh Dickins if (vma->vm_start >= end) 23681da177e4SLinus Torvalds return 0; 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds /* 23711da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 23721da177e4SLinus Torvalds * 23731da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 23741da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 23751da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 23761da177e4SLinus Torvalds */ 2377146425a3SHugh Dickins if (start > vma->vm_start) { 2378659ace58SKOSAKI Motohiro int error; 2379659ace58SKOSAKI Motohiro 2380659ace58SKOSAKI Motohiro /* 2381659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2382659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2383659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2384659ace58SKOSAKI Motohiro */ 2385659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2386659ace58SKOSAKI Motohiro return -ENOMEM; 2387659ace58SKOSAKI Motohiro 2388659ace58SKOSAKI Motohiro error = __split_vma(mm, vma, start, 0); 23891da177e4SLinus Torvalds if (error) 23901da177e4SLinus Torvalds return error; 2391146425a3SHugh Dickins prev = vma; 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 23941da177e4SLinus Torvalds /* Does it split the last one? */ 23951da177e4SLinus Torvalds last = find_vma(mm, end); 23961da177e4SLinus Torvalds if (last && end > last->vm_start) { 2397659ace58SKOSAKI Motohiro int error = __split_vma(mm, last, end, 1); 23981da177e4SLinus Torvalds if (error) 23991da177e4SLinus Torvalds return error; 24001da177e4SLinus Torvalds } 2401146425a3SHugh Dickins vma = prev? prev->vm_next: mm->mmap; 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds /* 2404ba470de4SRik van Riel * unlock any mlock()ed ranges before detaching vmas 2405ba470de4SRik van Riel */ 2406ba470de4SRik van Riel if (mm->locked_vm) { 2407ba470de4SRik van Riel struct vm_area_struct *tmp = vma; 2408ba470de4SRik van Riel while (tmp && tmp->vm_start < end) { 2409ba470de4SRik van Riel if (tmp->vm_flags & VM_LOCKED) { 2410ba470de4SRik van Riel mm->locked_vm -= vma_pages(tmp); 2411ba470de4SRik van Riel munlock_vma_pages_all(tmp); 2412ba470de4SRik van Riel } 2413ba470de4SRik van Riel tmp = tmp->vm_next; 2414ba470de4SRik van Riel } 2415ba470de4SRik van Riel } 2416ba470de4SRik van Riel 2417ba470de4SRik van Riel /* 24181da177e4SLinus Torvalds * Remove the vma's, and unmap the actual pages 24191da177e4SLinus Torvalds */ 2420146425a3SHugh Dickins detach_vmas_to_be_unmapped(mm, vma, prev, end); 2421146425a3SHugh Dickins unmap_region(mm, vma, prev, start, end); 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds /* Fix up all other VM information */ 24242c0b3814SHugh Dickins remove_vma_list(mm, vma); 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds return 0; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 2429bfce281cSAl Viro int vm_munmap(unsigned long start, size_t len) 2430a46ef99dSLinus Torvalds { 2431a46ef99dSLinus Torvalds int ret; 2432bfce281cSAl Viro struct mm_struct *mm = current->mm; 2433a46ef99dSLinus Torvalds 2434a46ef99dSLinus Torvalds down_write(&mm->mmap_sem); 2435a46ef99dSLinus Torvalds ret = do_munmap(mm, start, len); 2436a46ef99dSLinus Torvalds up_write(&mm->mmap_sem); 2437a46ef99dSLinus Torvalds return ret; 2438a46ef99dSLinus Torvalds } 2439a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2440a46ef99dSLinus Torvalds 24416a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds profile_munmap(addr); 2444bfce281cSAl Viro return vm_munmap(addr, len); 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds static inline void verify_mm_writelocked(struct mm_struct *mm) 24481da177e4SLinus Torvalds { 2449a241ec65SPaul E. McKenney #ifdef CONFIG_DEBUG_VM 24501da177e4SLinus Torvalds if (unlikely(down_read_trylock(&mm->mmap_sem))) { 24511da177e4SLinus Torvalds WARN_ON(1); 24521da177e4SLinus Torvalds up_read(&mm->mmap_sem); 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds #endif 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds /* 24581da177e4SLinus Torvalds * this is really a simplified "do_mmap". it only handles 24591da177e4SLinus Torvalds * anonymous maps. eventually we may be able to do some 24601da177e4SLinus Torvalds * brk-specific accounting here. 24611da177e4SLinus Torvalds */ 2462e4eb1ff6SLinus Torvalds static unsigned long do_brk(unsigned long addr, unsigned long len) 24631da177e4SLinus Torvalds { 24641da177e4SLinus Torvalds struct mm_struct * mm = current->mm; 24651da177e4SLinus Torvalds struct vm_area_struct * vma, * prev; 24661da177e4SLinus Torvalds unsigned long flags; 24671da177e4SLinus Torvalds struct rb_node ** rb_link, * rb_parent; 24681da177e4SLinus Torvalds pgoff_t pgoff = addr >> PAGE_SHIFT; 24693a459756SKirill Korotaev int error; 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds len = PAGE_ALIGN(len); 24721da177e4SLinus Torvalds if (!len) 24731da177e4SLinus Torvalds return addr; 24741da177e4SLinus Torvalds 24753a459756SKirill Korotaev flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 24763a459756SKirill Korotaev 24772c6a1016SAl Viro error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 24782c6a1016SAl Viro if (error & ~PAGE_MASK) 24793a459756SKirill Korotaev return error; 24803a459756SKirill Korotaev 24811da177e4SLinus Torvalds /* 24821da177e4SLinus Torvalds * mlock MCL_FUTURE? 24831da177e4SLinus Torvalds */ 24841da177e4SLinus Torvalds if (mm->def_flags & VM_LOCKED) { 24851da177e4SLinus Torvalds unsigned long locked, lock_limit; 248693ea1d0aSChris Wright locked = len >> PAGE_SHIFT; 248793ea1d0aSChris Wright locked += mm->locked_vm; 248859e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK); 248993ea1d0aSChris Wright lock_limit >>= PAGE_SHIFT; 24901da177e4SLinus Torvalds if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 24911da177e4SLinus Torvalds return -EAGAIN; 24921da177e4SLinus Torvalds } 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds /* 24951da177e4SLinus Torvalds * mm->mmap_sem is required to protect against another thread 24961da177e4SLinus Torvalds * changing the mappings in case we sleep. 24971da177e4SLinus Torvalds */ 24981da177e4SLinus Torvalds verify_mm_writelocked(mm); 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds /* 25011da177e4SLinus Torvalds * Clear old maps. this also does some error checking for us 25021da177e4SLinus Torvalds */ 25031da177e4SLinus Torvalds munmap_back: 25046597d783SHugh Dickins if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) { 25051da177e4SLinus Torvalds if (do_munmap(mm, addr, len)) 25061da177e4SLinus Torvalds return -ENOMEM; 25071da177e4SLinus Torvalds goto munmap_back; 25081da177e4SLinus Torvalds } 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds /* Check against address space limits *after* clearing old maps... */ 2511119f657cSakpm@osdl.org if (!may_expand_vm(mm, len >> PAGE_SHIFT)) 25121da177e4SLinus Torvalds return -ENOMEM; 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 25151da177e4SLinus Torvalds return -ENOMEM; 25161da177e4SLinus Torvalds 2517191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 25181da177e4SLinus Torvalds return -ENOMEM; 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds /* Can we just expand an old private anonymous mapping? */ 2521ba470de4SRik van Riel vma = vma_merge(mm, prev, addr, addr + len, flags, 2522ba470de4SRik van Riel NULL, NULL, pgoff, NULL); 2523ba470de4SRik van Riel if (vma) 25241da177e4SLinus Torvalds goto out; 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds /* 25271da177e4SLinus Torvalds * create a vma struct for an anonymous mapping 25281da177e4SLinus Torvalds */ 2529c5e3b83eSPekka Enberg vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 25301da177e4SLinus Torvalds if (!vma) { 25311da177e4SLinus Torvalds vm_unacct_memory(len >> PAGE_SHIFT); 25321da177e4SLinus Torvalds return -ENOMEM; 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 25355beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 25361da177e4SLinus Torvalds vma->vm_mm = mm; 25371da177e4SLinus Torvalds vma->vm_start = addr; 25381da177e4SLinus Torvalds vma->vm_end = addr + len; 25391da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 25401da177e4SLinus Torvalds vma->vm_flags = flags; 25413ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 25421da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 25431da177e4SLinus Torvalds out: 25443af9e859SEric B Munson perf_event_mmap(vma); 25451da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 25461da177e4SLinus Torvalds if (flags & VM_LOCKED) { 2547ba470de4SRik van Riel if (!mlock_vma_pages_range(vma, addr, addr + len)) 2548ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds return addr; 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds 2553e4eb1ff6SLinus Torvalds unsigned long vm_brk(unsigned long addr, unsigned long len) 2554e4eb1ff6SLinus Torvalds { 2555e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 2556e4eb1ff6SLinus Torvalds unsigned long ret; 2557e4eb1ff6SLinus Torvalds 2558e4eb1ff6SLinus Torvalds down_write(&mm->mmap_sem); 2559e4eb1ff6SLinus Torvalds ret = do_brk(addr, len); 2560e4eb1ff6SLinus Torvalds up_write(&mm->mmap_sem); 2561e4eb1ff6SLinus Torvalds return ret; 2562e4eb1ff6SLinus Torvalds } 2563e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds /* Release all mmaps. */ 25661da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 25671da177e4SLinus Torvalds { 2568d16dfc55SPeter Zijlstra struct mmu_gather tlb; 2569ba470de4SRik van Riel struct vm_area_struct *vma; 25701da177e4SLinus Torvalds unsigned long nr_accounted = 0; 25711da177e4SLinus Torvalds 2572d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 2573cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 2574d6dd61c8SJeremy Fitzhardinge 2575ba470de4SRik van Riel if (mm->locked_vm) { 2576ba470de4SRik van Riel vma = mm->mmap; 2577ba470de4SRik van Riel while (vma) { 2578ba470de4SRik van Riel if (vma->vm_flags & VM_LOCKED) 2579ba470de4SRik van Riel munlock_vma_pages_all(vma); 2580ba470de4SRik van Riel vma = vma->vm_next; 2581ba470de4SRik van Riel } 2582ba470de4SRik van Riel } 25839480c53eSJeremy Fitzhardinge 25849480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 25859480c53eSJeremy Fitzhardinge 2586ba470de4SRik van Riel vma = mm->mmap; 25879480c53eSJeremy Fitzhardinge if (!vma) /* Can happen if dup_mmap() received an OOM */ 25889480c53eSJeremy Fitzhardinge return; 25899480c53eSJeremy Fitzhardinge 25901da177e4SLinus Torvalds lru_add_drain(); 25911da177e4SLinus Torvalds flush_cache_mm(mm); 2592d16dfc55SPeter Zijlstra tlb_gather_mmu(&tlb, mm, 1); 2593901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 2594e0da382cSHugh Dickins /* Use -1 here to ensure all VMAs in the mm are unmapped */ 25954f74d2c8SLinus Torvalds unmap_vmas(&tlb, vma, 0, -1); 25969ba69294SHugh Dickins 2597d16dfc55SPeter Zijlstra free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0); 2598853f5e26SAl Viro tlb_finish_mmu(&tlb, 0, -1); 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds /* 26018f4f8c16SHugh Dickins * Walk the list again, actually closing and freeing it, 26028f4f8c16SHugh Dickins * with preemption enabled, without holding any MM locks. 26031da177e4SLinus Torvalds */ 26044f74d2c8SLinus Torvalds while (vma) { 26054f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 26064f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 2607a8fb5618SHugh Dickins vma = remove_vma(vma); 26084f74d2c8SLinus Torvalds } 26094f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 2610e0da382cSHugh Dickins 2611f9aed62aSHugh Dickins WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT); 26121da177e4SLinus Torvalds } 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 26151da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 26163d48ae45SPeter Zijlstra * then i_mmap_mutex is taken here. 26171da177e4SLinus Torvalds */ 26181da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 26191da177e4SLinus Torvalds { 26206597d783SHugh Dickins struct vm_area_struct *prev; 26211da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds /* 26241da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 26251da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 26261da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 26271da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 26281da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 26291da177e4SLinus Torvalds * 26301da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 26311da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 26321da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 26331da177e4SLinus Torvalds * Similarly in do_mmap_pgoff and in do_brk. 26341da177e4SLinus Torvalds */ 26351da177e4SLinus Torvalds if (!vma->vm_file) { 26361da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 26371da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 26381da177e4SLinus Torvalds } 26396597d783SHugh Dickins if (find_vma_links(mm, vma->vm_start, vma->vm_end, 26406597d783SHugh Dickins &prev, &rb_link, &rb_parent)) 26411da177e4SLinus Torvalds return -ENOMEM; 26422fd4ef85SHugh Dickins if ((vma->vm_flags & VM_ACCOUNT) && 264334b4e4aaSAlan Cox security_vm_enough_memory_mm(mm, vma_pages(vma))) 26442fd4ef85SHugh Dickins return -ENOMEM; 26452b144498SSrikar Dronamraju 26461da177e4SLinus Torvalds vma_link(mm, vma, prev, rb_link, rb_parent); 26471da177e4SLinus Torvalds return 0; 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds /* 26511da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 26521da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 26531da177e4SLinus Torvalds */ 26541da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 265538a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 265638a76013SMichel Lespinasse bool *need_rmap_locks) 26571da177e4SLinus Torvalds { 26581da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 26591da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 26601da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 26611da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 26621da177e4SLinus Torvalds struct rb_node **rb_link, *rb_parent; 26631da177e4SLinus Torvalds struct mempolicy *pol; 2664948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds /* 26671da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 26681da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 26691da177e4SLinus Torvalds */ 2670948f017bSAndrea Arcangeli if (unlikely(!vma->vm_file && !vma->anon_vma)) { 26711da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 2672948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 2673948f017bSAndrea Arcangeli } 26741da177e4SLinus Torvalds 26756597d783SHugh Dickins if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) 26766597d783SHugh Dickins return NULL; /* should never get here */ 26771da177e4SLinus Torvalds new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, 26781da177e4SLinus Torvalds vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma)); 26791da177e4SLinus Torvalds if (new_vma) { 26801da177e4SLinus Torvalds /* 26811da177e4SLinus Torvalds * Source vma may have been merged into new_vma 26821da177e4SLinus Torvalds */ 2683948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 2684948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 2685948f017bSAndrea Arcangeli /* 2686948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 2687948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 2688948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 2689948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 2690948f017bSAndrea Arcangeli * destination address of the mremap to allow 2691948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 2692948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 2693948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 2694948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 2695948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 2696948f017bSAndrea Arcangeli */ 2697948f017bSAndrea Arcangeli VM_BUG_ON(faulted_in_anon_vma); 269838a76013SMichel Lespinasse *vmap = vma = new_vma; 2699108d6642SMichel Lespinasse } 270038a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 27011da177e4SLinus Torvalds } else { 2702e94b1766SChristoph Lameter new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 27031da177e4SLinus Torvalds if (new_vma) { 27041da177e4SLinus Torvalds *new_vma = *vma; 27051da177e4SLinus Torvalds new_vma->vm_start = addr; 27061da177e4SLinus Torvalds new_vma->vm_end = addr + len; 27071da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 2708523d4e20SMichel Lespinasse pol = mpol_dup(vma_policy(vma)); 2709523d4e20SMichel Lespinasse if (IS_ERR(pol)) 2710523d4e20SMichel Lespinasse goto out_free_vma; 2711523d4e20SMichel Lespinasse vma_set_policy(new_vma, pol); 2712523d4e20SMichel Lespinasse INIT_LIST_HEAD(&new_vma->anon_vma_chain); 2713523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 2714523d4e20SMichel Lespinasse goto out_free_mempol; 2715e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 27161da177e4SLinus Torvalds get_file(new_vma->vm_file); 27171da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 27181da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 27191da177e4SLinus Torvalds vma_link(mm, new_vma, prev, rb_link, rb_parent); 272038a76013SMichel Lespinasse *need_rmap_locks = false; 27211da177e4SLinus Torvalds } 27221da177e4SLinus Torvalds } 27231da177e4SLinus Torvalds return new_vma; 27245beb4930SRik van Riel 27255beb4930SRik van Riel out_free_mempol: 27265beb4930SRik van Riel mpol_put(pol); 27275beb4930SRik van Riel out_free_vma: 27285beb4930SRik van Riel kmem_cache_free(vm_area_cachep, new_vma); 27295beb4930SRik van Riel return NULL; 27301da177e4SLinus Torvalds } 2731119f657cSakpm@osdl.org 2732119f657cSakpm@osdl.org /* 2733119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 2734119f657cSakpm@osdl.org * number of pages 2735119f657cSakpm@osdl.org */ 2736119f657cSakpm@osdl.org int may_expand_vm(struct mm_struct *mm, unsigned long npages) 2737119f657cSakpm@osdl.org { 2738119f657cSakpm@osdl.org unsigned long cur = mm->total_vm; /* pages */ 2739119f657cSakpm@osdl.org unsigned long lim; 2740119f657cSakpm@osdl.org 274159e99e5bSJiri Slaby lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT; 2742119f657cSakpm@osdl.org 2743119f657cSakpm@osdl.org if (cur + npages > lim) 2744119f657cSakpm@osdl.org return 0; 2745119f657cSakpm@osdl.org return 1; 2746119f657cSakpm@osdl.org } 2747fa5dc22fSRoland McGrath 2748fa5dc22fSRoland McGrath 2749b1d0e4f5SNick Piggin static int special_mapping_fault(struct vm_area_struct *vma, 2750b1d0e4f5SNick Piggin struct vm_fault *vmf) 2751fa5dc22fSRoland McGrath { 2752b1d0e4f5SNick Piggin pgoff_t pgoff; 2753fa5dc22fSRoland McGrath struct page **pages; 2754fa5dc22fSRoland McGrath 2755b1d0e4f5SNick Piggin /* 2756b1d0e4f5SNick Piggin * special mappings have no vm_file, and in that case, the mm 2757b1d0e4f5SNick Piggin * uses vm_pgoff internally. So we have to subtract it from here. 2758b1d0e4f5SNick Piggin * We are allowed to do this because we are the mm; do not copy 2759b1d0e4f5SNick Piggin * this code into drivers! 2760b1d0e4f5SNick Piggin */ 2761b1d0e4f5SNick Piggin pgoff = vmf->pgoff - vma->vm_pgoff; 2762fa5dc22fSRoland McGrath 2763b1d0e4f5SNick Piggin for (pages = vma->vm_private_data; pgoff && *pages; ++pages) 2764b1d0e4f5SNick Piggin pgoff--; 2765fa5dc22fSRoland McGrath 2766fa5dc22fSRoland McGrath if (*pages) { 2767fa5dc22fSRoland McGrath struct page *page = *pages; 2768fa5dc22fSRoland McGrath get_page(page); 2769b1d0e4f5SNick Piggin vmf->page = page; 2770b1d0e4f5SNick Piggin return 0; 2771fa5dc22fSRoland McGrath } 2772fa5dc22fSRoland McGrath 2773b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 2774fa5dc22fSRoland McGrath } 2775fa5dc22fSRoland McGrath 2776fa5dc22fSRoland McGrath /* 2777fa5dc22fSRoland McGrath * Having a close hook prevents vma merging regardless of flags. 2778fa5dc22fSRoland McGrath */ 2779fa5dc22fSRoland McGrath static void special_mapping_close(struct vm_area_struct *vma) 2780fa5dc22fSRoland McGrath { 2781fa5dc22fSRoland McGrath } 2782fa5dc22fSRoland McGrath 2783f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct special_mapping_vmops = { 2784fa5dc22fSRoland McGrath .close = special_mapping_close, 2785b1d0e4f5SNick Piggin .fault = special_mapping_fault, 2786fa5dc22fSRoland McGrath }; 2787fa5dc22fSRoland McGrath 2788fa5dc22fSRoland McGrath /* 2789fa5dc22fSRoland McGrath * Called with mm->mmap_sem held for writing. 2790fa5dc22fSRoland McGrath * Insert a new vma covering the given region, with the given flags. 2791fa5dc22fSRoland McGrath * Its pages are supplied by the given array of struct page *. 2792fa5dc22fSRoland McGrath * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 2793fa5dc22fSRoland McGrath * The region past the last page supplied will always produce SIGBUS. 2794fa5dc22fSRoland McGrath * The array pointer and the pages it points to are assumed to stay alive 2795fa5dc22fSRoland McGrath * for as long as this mapping might exist. 2796fa5dc22fSRoland McGrath */ 2797fa5dc22fSRoland McGrath int install_special_mapping(struct mm_struct *mm, 2798fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 2799fa5dc22fSRoland McGrath unsigned long vm_flags, struct page **pages) 2800fa5dc22fSRoland McGrath { 2801462e635eSTavis Ormandy int ret; 2802fa5dc22fSRoland McGrath struct vm_area_struct *vma; 2803fa5dc22fSRoland McGrath 2804fa5dc22fSRoland McGrath vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 2805fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 2806fa5dc22fSRoland McGrath return -ENOMEM; 2807fa5dc22fSRoland McGrath 28085beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 2809fa5dc22fSRoland McGrath vma->vm_mm = mm; 2810fa5dc22fSRoland McGrath vma->vm_start = addr; 2811fa5dc22fSRoland McGrath vma->vm_end = addr + len; 2812fa5dc22fSRoland McGrath 28132f98735cSNick Piggin vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND; 28143ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 2815fa5dc22fSRoland McGrath 2816fa5dc22fSRoland McGrath vma->vm_ops = &special_mapping_vmops; 2817fa5dc22fSRoland McGrath vma->vm_private_data = pages; 2818fa5dc22fSRoland McGrath 2819462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 2820462e635eSTavis Ormandy if (ret) 2821462e635eSTavis Ormandy goto out; 2822fa5dc22fSRoland McGrath 2823fa5dc22fSRoland McGrath mm->total_vm += len >> PAGE_SHIFT; 2824fa5dc22fSRoland McGrath 2825cdd6c482SIngo Molnar perf_event_mmap(vma); 2826089dd79dSPeter Zijlstra 2827fa5dc22fSRoland McGrath return 0; 2828462e635eSTavis Ormandy 2829462e635eSTavis Ormandy out: 2830462e635eSTavis Ormandy kmem_cache_free(vm_area_cachep, vma); 2831462e635eSTavis Ormandy return ret; 2832fa5dc22fSRoland McGrath } 28337906d00cSAndrea Arcangeli 28347906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 28357906d00cSAndrea Arcangeli 2836454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 28377906d00cSAndrea Arcangeli { 2838bf181b9fSMichel Lespinasse if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) { 28397906d00cSAndrea Arcangeli /* 28407906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 28417906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 28427906d00cSAndrea Arcangeli */ 28432b575eb6SPeter Zijlstra mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem); 28447906d00cSAndrea Arcangeli /* 28457906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 28462b575eb6SPeter Zijlstra * anon_vma->root->mutex. If some other vma in this mm shares 28477906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 28487906d00cSAndrea Arcangeli * 28497906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 28507906d00cSAndrea Arcangeli * can't change from under us thanks to the 28512b575eb6SPeter Zijlstra * anon_vma->root->mutex. 28527906d00cSAndrea Arcangeli */ 28537906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 2854bf181b9fSMichel Lespinasse &anon_vma->root->rb_root.rb_node)) 28557906d00cSAndrea Arcangeli BUG(); 28567906d00cSAndrea Arcangeli } 28577906d00cSAndrea Arcangeli } 28587906d00cSAndrea Arcangeli 2859454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 28607906d00cSAndrea Arcangeli { 28617906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 28627906d00cSAndrea Arcangeli /* 28637906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 28647906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 28657906d00cSAndrea Arcangeli * 28667906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 28677906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 28687906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 28697906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 28707906d00cSAndrea Arcangeli */ 28717906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 28727906d00cSAndrea Arcangeli BUG(); 28733d48ae45SPeter Zijlstra mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem); 28747906d00cSAndrea Arcangeli } 28757906d00cSAndrea Arcangeli } 28767906d00cSAndrea Arcangeli 28777906d00cSAndrea Arcangeli /* 28787906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 28797906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 28807906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 28817906d00cSAndrea Arcangeli * 28827906d00cSAndrea Arcangeli * The caller must take the mmap_sem in write mode before calling 28837906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 28847906d00cSAndrea Arcangeli * mmap_sem until mm_drop_all_locks() returns. 28857906d00cSAndrea Arcangeli * 28867906d00cSAndrea Arcangeli * mmap_sem in write mode is required in order to block all operations 28877906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 28887906d00cSAndrea Arcangeli * altering the vma layout (for example populate_range() with 28897906d00cSAndrea Arcangeli * nonlinear vmas). It's also needed in write mode to avoid new 28907906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 28917906d00cSAndrea Arcangeli * 28927906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 28937906d00cSAndrea Arcangeli * or it would deadlock. 28947906d00cSAndrea Arcangeli * 2895bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 28967906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 28977906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 28987906d00cSAndrea Arcangeli * 28997906d00cSAndrea Arcangeli * We can take all the locks in random order because the VM code 29002b575eb6SPeter Zijlstra * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never 29017906d00cSAndrea Arcangeli * takes more than one of them in a row. Secondly we're protected 29027906d00cSAndrea Arcangeli * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex. 29037906d00cSAndrea Arcangeli * 29047906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 29057906d00cSAndrea Arcangeli * that may have to take thousand of locks. 29067906d00cSAndrea Arcangeli * 29077906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 29087906d00cSAndrea Arcangeli */ 29097906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 29107906d00cSAndrea Arcangeli { 29117906d00cSAndrea Arcangeli struct vm_area_struct *vma; 29125beb4930SRik van Riel struct anon_vma_chain *avc; 29137906d00cSAndrea Arcangeli 29147906d00cSAndrea Arcangeli BUG_ON(down_read_trylock(&mm->mmap_sem)); 29157906d00cSAndrea Arcangeli 29167906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 29177906d00cSAndrea Arcangeli 29187906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 29197906d00cSAndrea Arcangeli if (signal_pending(current)) 29207906d00cSAndrea Arcangeli goto out_unlock; 29217906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 2922454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 29237906d00cSAndrea Arcangeli } 29247cd5a02fSPeter Zijlstra 29257cd5a02fSPeter Zijlstra for (vma = mm->mmap; vma; vma = vma->vm_next) { 29267cd5a02fSPeter Zijlstra if (signal_pending(current)) 29277cd5a02fSPeter Zijlstra goto out_unlock; 29287cd5a02fSPeter Zijlstra if (vma->anon_vma) 29295beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 29305beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 29317cd5a02fSPeter Zijlstra } 29327cd5a02fSPeter Zijlstra 2933584cff54SKautuk Consul return 0; 29347906d00cSAndrea Arcangeli 29357906d00cSAndrea Arcangeli out_unlock: 29367906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 2937584cff54SKautuk Consul return -EINTR; 29387906d00cSAndrea Arcangeli } 29397906d00cSAndrea Arcangeli 29407906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 29417906d00cSAndrea Arcangeli { 2942bf181b9fSMichel Lespinasse if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) { 29437906d00cSAndrea Arcangeli /* 29447906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 29457906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 29467906d00cSAndrea Arcangeli * 29477906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 2948bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 29497906d00cSAndrea Arcangeli * never see our bitflag. 29507906d00cSAndrea Arcangeli * 29517906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 29527906d00cSAndrea Arcangeli * can't change from under us until we release the 29532b575eb6SPeter Zijlstra * anon_vma->root->mutex. 29547906d00cSAndrea Arcangeli */ 29557906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 2956bf181b9fSMichel Lespinasse &anon_vma->root->rb_root.rb_node)) 29577906d00cSAndrea Arcangeli BUG(); 2958cba48b98SRik van Riel anon_vma_unlock(anon_vma); 29597906d00cSAndrea Arcangeli } 29607906d00cSAndrea Arcangeli } 29617906d00cSAndrea Arcangeli 29627906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 29637906d00cSAndrea Arcangeli { 29647906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 29657906d00cSAndrea Arcangeli /* 29667906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 29677906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 29687906d00cSAndrea Arcangeli */ 29693d48ae45SPeter Zijlstra mutex_unlock(&mapping->i_mmap_mutex); 29707906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 29717906d00cSAndrea Arcangeli &mapping->flags)) 29727906d00cSAndrea Arcangeli BUG(); 29737906d00cSAndrea Arcangeli } 29747906d00cSAndrea Arcangeli } 29757906d00cSAndrea Arcangeli 29767906d00cSAndrea Arcangeli /* 29777906d00cSAndrea Arcangeli * The mmap_sem cannot be released by the caller until 29787906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 29797906d00cSAndrea Arcangeli */ 29807906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 29817906d00cSAndrea Arcangeli { 29827906d00cSAndrea Arcangeli struct vm_area_struct *vma; 29835beb4930SRik van Riel struct anon_vma_chain *avc; 29847906d00cSAndrea Arcangeli 29857906d00cSAndrea Arcangeli BUG_ON(down_read_trylock(&mm->mmap_sem)); 29867906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 29877906d00cSAndrea Arcangeli 29887906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 29897906d00cSAndrea Arcangeli if (vma->anon_vma) 29905beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 29915beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 29927906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 29937906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 29947906d00cSAndrea Arcangeli } 29957906d00cSAndrea Arcangeli 29967906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 29977906d00cSAndrea Arcangeli } 29988feae131SDavid Howells 29998feae131SDavid Howells /* 30008feae131SDavid Howells * initialise the VMA slab 30018feae131SDavid Howells */ 30028feae131SDavid Howells void __init mmap_init(void) 30038feae131SDavid Howells { 300400a62ce9SKOSAKI Motohiro int ret; 300500a62ce9SKOSAKI Motohiro 300600a62ce9SKOSAKI Motohiro ret = percpu_counter_init(&vm_committed_as, 0); 300700a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 30088feae131SDavid Howells } 3009