1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * mm/mmap.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Written by obz. 61da177e4SLinus Torvalds * 7046c6884SAlan Cox * Address space accounting code <alan@lxorguk.ukuu.org.uk> 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 10b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11b1de0d13SMitchel Humpherys 12e8420a8eSCyril Hrubis #include <linux/kernel.h> 131da177e4SLinus Torvalds #include <linux/slab.h> 144af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 1617fca131SArnd Bergmann #include <linux/mm_inline.h> 17615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 181da177e4SLinus Torvalds #include <linux/shm.h> 191da177e4SLinus Torvalds #include <linux/mman.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/swap.h> 221da177e4SLinus Torvalds #include <linux/syscalls.h> 23c59ede7bSRandy.Dunlap #include <linux/capability.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/file.h> 261da177e4SLinus Torvalds #include <linux/fs.h> 271da177e4SLinus Torvalds #include <linux/personality.h> 281da177e4SLinus Torvalds #include <linux/security.h> 291da177e4SLinus Torvalds #include <linux/hugetlb.h> 30c01d5b30SHugh Dickins #include <linux/shmem_fs.h> 311da177e4SLinus Torvalds #include <linux/profile.h> 32b95f1b31SPaul Gortmaker #include <linux/export.h> 331da177e4SLinus Torvalds #include <linux/mount.h> 341da177e4SLinus Torvalds #include <linux/mempolicy.h> 351da177e4SLinus Torvalds #include <linux/rmap.h> 36cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 3782f71ae4SKonstantin Khlebnikov #include <linux/mmdebug.h> 38cdd6c482SIngo Molnar #include <linux/perf_event.h> 39120a795dSAl Viro #include <linux/audit.h> 40b15d00b6SAndrea Arcangeli #include <linux/khugepaged.h> 412b144498SSrikar Dronamraju #include <linux/uprobes.h> 421640879aSAndrew Shewmaker #include <linux/notifier.h> 431640879aSAndrew Shewmaker #include <linux/memory.h> 44b1de0d13SMitchel Humpherys #include <linux/printk.h> 4519a809afSAndrea Arcangeli #include <linux/userfaultfd_k.h> 46d977d56cSKonstantin Khlebnikov #include <linux/moduleparam.h> 4762b5f7d0SDave Hansen #include <linux/pkeys.h> 4821292580SAndrea Arcangeli #include <linux/oom.h> 4904f5866eSAndrea Arcangeli #include <linux/sched/mm.h> 501da177e4SLinus Torvalds 517c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 521da177e4SLinus Torvalds #include <asm/cacheflush.h> 531da177e4SLinus Torvalds #include <asm/tlb.h> 54d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h> 551da177e4SLinus Torvalds 56df529cabSJaewon Kim #define CREATE_TRACE_POINTS 57df529cabSJaewon Kim #include <trace/events/mmap.h> 58df529cabSJaewon Kim 5942b77728SJan Beulich #include "internal.h" 6042b77728SJan Beulich 613a459756SKirill Korotaev #ifndef arch_mmap_check 623a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags) (0) 633a459756SKirill Korotaev #endif 643a459756SKirill Korotaev 65d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS 66d07e2259SDaniel Cashman const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; 67d07e2259SDaniel Cashman const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; 68d07e2259SDaniel Cashman int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; 69d07e2259SDaniel Cashman #endif 70d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS 71d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; 72d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; 73d07e2259SDaniel Cashman int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; 74d07e2259SDaniel Cashman #endif 75d07e2259SDaniel Cashman 76f4fcd558SKonstantin Khlebnikov static bool ignore_rlimit_data; 77d977d56cSKonstantin Khlebnikov core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); 78d07e2259SDaniel Cashman 79e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm, 80e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 81e0da382cSHugh Dickins unsigned long start, unsigned long end); 82e0da382cSHugh Dickins 8364e45507SPeter Feiner static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) 8464e45507SPeter Feiner { 8564e45507SPeter Feiner return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); 8664e45507SPeter Feiner } 8764e45507SPeter Feiner 8864e45507SPeter Feiner /* Update vma->vm_page_prot to reflect vma->vm_flags. */ 8964e45507SPeter Feiner void vma_set_page_prot(struct vm_area_struct *vma) 9064e45507SPeter Feiner { 9164e45507SPeter Feiner unsigned long vm_flags = vma->vm_flags; 926d2329f8SAndrea Arcangeli pgprot_t vm_page_prot; 9364e45507SPeter Feiner 946d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); 956d2329f8SAndrea Arcangeli if (vma_wants_writenotify(vma, vm_page_prot)) { 9664e45507SPeter Feiner vm_flags &= ~VM_SHARED; 976d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); 9864e45507SPeter Feiner } 99c1e8d7c6SMichel Lespinasse /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ 1006d2329f8SAndrea Arcangeli WRITE_ONCE(vma->vm_page_prot, vm_page_prot); 10164e45507SPeter Feiner } 10264e45507SPeter Feiner 1031da177e4SLinus Torvalds /* 104c8c06efaSDavidlohr Bueso * Requires inode->i_mapping->i_mmap_rwsem 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1071da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1104bb5f5d9SDavid Herrmann mapping_unmap_writable(mapping); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 1136b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 1141da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* 1186b2dbba8SMichel Lespinasse * Unlink a file-based vm structure from its interval tree, to hide 119a8fb5618SHugh Dickins * vma from rmap and vmtruncate before freeing its page tables. 1201da177e4SLinus Torvalds */ 121a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds struct file *file = vma->vm_file; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds if (file) { 1261da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 12783cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 1281da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 12983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 1301da177e4SLinus Torvalds } 131a8fb5618SHugh Dickins } 132a8fb5618SHugh Dickins 133a8fb5618SHugh Dickins /* 134a8fb5618SHugh Dickins * Close a vm structure and free it, returning the next. 135a8fb5618SHugh Dickins */ 136a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) 137a8fb5618SHugh Dickins { 138a8fb5618SHugh Dickins struct vm_area_struct *next = vma->vm_next; 139a8fb5618SHugh Dickins 140a8fb5618SHugh Dickins might_sleep(); 1411da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 1421da177e4SLinus Torvalds vma->vm_ops->close(vma); 143e9714acfSKonstantin Khlebnikov if (vma->vm_file) 144a8fb5618SHugh Dickins fput(vma->vm_file); 145f0be3d32SLee Schermerhorn mpol_put(vma_policy(vma)); 1463928d4f5SLinus Torvalds vm_area_free(vma); 147a8fb5618SHugh Dickins return next; 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1502e7ce7d3SLiam R. Howlett /* 1512e7ce7d3SLiam R. Howlett * check_brk_limits() - Use platform specific check of range & verify mlock 1522e7ce7d3SLiam R. Howlett * limits. 1532e7ce7d3SLiam R. Howlett * @addr: The address to check 1542e7ce7d3SLiam R. Howlett * @len: The size of increase. 1552e7ce7d3SLiam R. Howlett * 1562e7ce7d3SLiam R. Howlett * Return: 0 on success. 1572e7ce7d3SLiam R. Howlett */ 1582e7ce7d3SLiam R. Howlett static int check_brk_limits(unsigned long addr, unsigned long len) 1592e7ce7d3SLiam R. Howlett { 1602e7ce7d3SLiam R. Howlett unsigned long mapped_addr; 1612e7ce7d3SLiam R. Howlett 1622e7ce7d3SLiam R. Howlett mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 1632e7ce7d3SLiam R. Howlett if (IS_ERR_VALUE(mapped_addr)) 1642e7ce7d3SLiam R. Howlett return mapped_addr; 1652e7ce7d3SLiam R. Howlett 1662e7ce7d3SLiam R. Howlett return mlock_future_check(current->mm, current->mm->def_flags, len); 1672e7ce7d3SLiam R. Howlett } 1682e7ce7d3SLiam R. Howlett static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, 1692e7ce7d3SLiam R. Howlett unsigned long newbrk, unsigned long oldbrk, 170bb177a73SMichal Hocko struct list_head *uf); 1712e7ce7d3SLiam R. Howlett static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *brkvma, 1722e7ce7d3SLiam R. Howlett unsigned long addr, unsigned long request, 1732e7ce7d3SLiam R. Howlett unsigned long flags); 1746a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 1751da177e4SLinus Torvalds { 1769bc8039eSYang Shi unsigned long newbrk, oldbrk, origbrk; 1771da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1782e7ce7d3SLiam R. Howlett struct vm_area_struct *brkvma, *next = NULL; 179a5b4592cSJiri Kosina unsigned long min_brk; 180128557ffSMichel Lespinasse bool populate; 1819bc8039eSYang Shi bool downgraded = false; 182897ab3e0SMike Rapoport LIST_HEAD(uf); 1832e7ce7d3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 1841da177e4SLinus Torvalds 185d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 186dc0ef0dfSMichal Hocko return -EINTR; 1871da177e4SLinus Torvalds 1889bc8039eSYang Shi origbrk = mm->brk; 1899bc8039eSYang Shi 190a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 1915520e894SJiri Kosina /* 1925520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 1935520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 1945520e894SJiri Kosina * to be arbitrarily shifted 1955520e894SJiri Kosina */ 1964471a675SJiri Kosina if (current->brk_randomized) 1975520e894SJiri Kosina min_brk = mm->start_brk; 1985520e894SJiri Kosina else 1995520e894SJiri Kosina min_brk = mm->end_data; 200a5b4592cSJiri Kosina #else 201a5b4592cSJiri Kosina min_brk = mm->start_brk; 202a5b4592cSJiri Kosina #endif 203a5b4592cSJiri Kosina if (brk < min_brk) 2041da177e4SLinus Torvalds goto out; 2051e624196SRam Gupta 2061e624196SRam Gupta /* 2071e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2081e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2091e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2101e624196SRam Gupta * not page aligned -Ram Gupta 2111e624196SRam Gupta */ 2128764b338SCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, 2138764b338SCyrill Gorcunov mm->end_data, mm->start_data)) 2141e624196SRam Gupta goto out; 2151e624196SRam Gupta 2161da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2171da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2189bc8039eSYang Shi if (oldbrk == newbrk) { 2199bc8039eSYang Shi mm->brk = brk; 2209bc8039eSYang Shi goto success; 2219bc8039eSYang Shi } 2221da177e4SLinus Torvalds 2239bc8039eSYang Shi /* 2249bc8039eSYang Shi * Always allow shrinking brk. 2252e7ce7d3SLiam R. Howlett * do_brk_munmap() may downgrade mmap_lock to read. 2269bc8039eSYang Shi */ 2271da177e4SLinus Torvalds if (brk <= mm->brk) { 2289bc8039eSYang Shi int ret; 2299bc8039eSYang Shi 2302e7ce7d3SLiam R. Howlett /* Search one past newbrk */ 2312e7ce7d3SLiam R. Howlett mas_set(&mas, newbrk); 2322e7ce7d3SLiam R. Howlett brkvma = mas_find(&mas, oldbrk); 2332e7ce7d3SLiam R. Howlett BUG_ON(brkvma == NULL); 2342e7ce7d3SLiam R. Howlett if (brkvma->vm_start >= oldbrk) 2352e7ce7d3SLiam R. Howlett goto out; /* mapping intersects with an existing non-brk vma. */ 2369bc8039eSYang Shi /* 2372e7ce7d3SLiam R. Howlett * mm->brk must be protected by write mmap_lock. 2382e7ce7d3SLiam R. Howlett * do_brk_munmap() may downgrade the lock, so update it 2392e7ce7d3SLiam R. Howlett * before calling do_brk_munmap(). 2409bc8039eSYang Shi */ 2419bc8039eSYang Shi mm->brk = brk; 2422e7ce7d3SLiam R. Howlett mas.last = oldbrk - 1; 2432e7ce7d3SLiam R. Howlett ret = do_brk_munmap(&mas, brkvma, newbrk, oldbrk, &uf); 2442e7ce7d3SLiam R. Howlett if (ret == 1) { 2452e7ce7d3SLiam R. Howlett downgraded = true; 2462e7ce7d3SLiam R. Howlett goto success; 2472e7ce7d3SLiam R. Howlett } else if (!ret) 2482e7ce7d3SLiam R. Howlett goto success; 2492e7ce7d3SLiam R. Howlett 2509bc8039eSYang Shi mm->brk = origbrk; 2511da177e4SLinus Torvalds goto out; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2542e7ce7d3SLiam R. Howlett if (check_brk_limits(oldbrk, newbrk - oldbrk)) 2552e7ce7d3SLiam R. Howlett goto out; 2562e7ce7d3SLiam R. Howlett 2572e7ce7d3SLiam R. Howlett /* 2582e7ce7d3SLiam R. Howlett * Only check if the next VMA is within the stack_guard_gap of the 2592e7ce7d3SLiam R. Howlett * expansion area 2602e7ce7d3SLiam R. Howlett */ 2612e7ce7d3SLiam R. Howlett mas_set(&mas, oldbrk); 2622e7ce7d3SLiam R. Howlett next = mas_find(&mas, newbrk - 1 + PAGE_SIZE + stack_guard_gap); 2631be7107fSHugh Dickins if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) 2641da177e4SLinus Torvalds goto out; 2651da177e4SLinus Torvalds 2662e7ce7d3SLiam R. Howlett brkvma = mas_prev(&mas, mm->start_brk); 2671da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 2682e7ce7d3SLiam R. Howlett if (do_brk_flags(&mas, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) 2691da177e4SLinus Torvalds goto out; 2702e7ce7d3SLiam R. Howlett 2711da177e4SLinus Torvalds mm->brk = brk; 2729bc8039eSYang Shi 2739bc8039eSYang Shi success: 274128557ffSMichel Lespinasse populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; 2759bc8039eSYang Shi if (downgraded) 276d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2779bc8039eSYang Shi else 278d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 279897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 280128557ffSMichel Lespinasse if (populate) 281128557ffSMichel Lespinasse mm_populate(oldbrk, newbrk - oldbrk); 282128557ffSMichel Lespinasse return brk; 283128557ffSMichel Lespinasse 2841da177e4SLinus Torvalds out: 285d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 286b7204006SAdrian Huang return origbrk; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 289d4af56c5SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 290d4af56c5SLiam R. Howlett extern void mt_validate(struct maple_tree *mt); 291d4af56c5SLiam R. Howlett extern void mt_dump(const struct maple_tree *mt); 2921da177e4SLinus Torvalds 293d4af56c5SLiam R. Howlett /* Validate the maple tree */ 294d4af56c5SLiam R. Howlett static void validate_mm_mt(struct mm_struct *mm) 295d4af56c5SLiam R. Howlett { 296d4af56c5SLiam R. Howlett struct maple_tree *mt = &mm->mm_mt; 297d4af56c5SLiam R. Howlett struct vm_area_struct *vma_mt, *vma = mm->mmap; 298d4af56c5SLiam R. Howlett 299d4af56c5SLiam R. Howlett MA_STATE(mas, mt, 0, 0); 300d4af56c5SLiam R. Howlett 301d4af56c5SLiam R. Howlett mt_validate(&mm->mm_mt); 302d4af56c5SLiam R. Howlett mas_for_each(&mas, vma_mt, ULONG_MAX) { 303d4af56c5SLiam R. Howlett if (xa_is_zero(vma_mt)) 304d4af56c5SLiam R. Howlett continue; 305d4af56c5SLiam R. Howlett 306d4af56c5SLiam R. Howlett if (!vma) 307d4af56c5SLiam R. Howlett break; 308d4af56c5SLiam R. Howlett 309d4af56c5SLiam R. Howlett if ((vma != vma_mt) || 310d4af56c5SLiam R. Howlett (vma->vm_start != vma_mt->vm_start) || 311d4af56c5SLiam R. Howlett (vma->vm_end != vma_mt->vm_end) || 312d4af56c5SLiam R. Howlett (vma->vm_start != mas.index) || 313d4af56c5SLiam R. Howlett (vma->vm_end - 1 != mas.last)) { 314d4af56c5SLiam R. Howlett pr_emerg("issue in %s\n", current->comm); 315d4af56c5SLiam R. Howlett dump_stack(); 316d4af56c5SLiam R. Howlett dump_vma(vma_mt); 317524e00b3SLiam R. Howlett pr_emerg("and vm_next\n"); 318d4af56c5SLiam R. Howlett dump_vma(vma->vm_next); 319d4af56c5SLiam R. Howlett pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, 320d4af56c5SLiam R. Howlett mas.index, mas.last); 321d4af56c5SLiam R. Howlett pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, 322d4af56c5SLiam R. Howlett vma_mt->vm_start, vma_mt->vm_end); 323524e00b3SLiam R. Howlett if (vma->vm_prev) { 324524e00b3SLiam R. Howlett pr_emerg("ll prev: %p %lu - %lu\n", 325524e00b3SLiam R. Howlett vma->vm_prev, vma->vm_prev->vm_start, 326524e00b3SLiam R. Howlett vma->vm_prev->vm_end); 327524e00b3SLiam R. Howlett } 328524e00b3SLiam R. Howlett pr_emerg("ll vma: %p %lu - %lu\n", vma, 329d4af56c5SLiam R. Howlett vma->vm_start, vma->vm_end); 330524e00b3SLiam R. Howlett if (vma->vm_next) { 331524e00b3SLiam R. Howlett pr_emerg("ll next: %p %lu - %lu\n", 332524e00b3SLiam R. Howlett vma->vm_next, vma->vm_next->vm_start, 333524e00b3SLiam R. Howlett vma->vm_next->vm_end); 334524e00b3SLiam R. Howlett } 335d4af56c5SLiam R. Howlett 336d4af56c5SLiam R. Howlett mt_dump(mas.tree); 337d4af56c5SLiam R. Howlett if (vma_mt->vm_end != mas.last + 1) { 338d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", 339d4af56c5SLiam R. Howlett mm, vma_mt->vm_start, vma_mt->vm_end, 340d4af56c5SLiam R. Howlett mas.index, mas.last); 341d4af56c5SLiam R. Howlett mt_dump(mas.tree); 342d4af56c5SLiam R. Howlett } 343d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); 344d4af56c5SLiam R. Howlett if (vma_mt->vm_start != mas.index) { 345d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", 346d4af56c5SLiam R. Howlett mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); 347d4af56c5SLiam R. Howlett mt_dump(mas.tree); 348d4af56c5SLiam R. Howlett } 349d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); 350d4af56c5SLiam R. Howlett } 351d4af56c5SLiam R. Howlett VM_BUG_ON(vma != vma_mt); 352d4af56c5SLiam R. Howlett vma = vma->vm_next; 353d4af56c5SLiam R. Howlett 354d4af56c5SLiam R. Howlett } 355d4af56c5SLiam R. Howlett VM_BUG_ON(vma); 356d4af56c5SLiam R. Howlett } 3571da177e4SLinus Torvalds 358eafd4dc4SRashika Kheria static void validate_mm(struct mm_struct *mm) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds int bug = 0; 3611da177e4SLinus Torvalds int i = 0; 3625a0768f6SMichel Lespinasse unsigned long highest_address = 0; 363ed8ea815SMichel Lespinasse struct vm_area_struct *vma = mm->mmap; 364ff26f70fSAndrew Morton 365524e00b3SLiam R. Howlett validate_mm_mt(mm); 366524e00b3SLiam R. Howlett 367ed8ea815SMichel Lespinasse while (vma) { 368524e00b3SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 36912352d3cSKonstantin Khlebnikov struct anon_vma *anon_vma = vma->anon_vma; 370ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 371ff26f70fSAndrew Morton 37212352d3cSKonstantin Khlebnikov if (anon_vma) { 37312352d3cSKonstantin Khlebnikov anon_vma_lock_read(anon_vma); 374ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 375ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 37612352d3cSKonstantin Khlebnikov anon_vma_unlock_read(anon_vma); 37712352d3cSKonstantin Khlebnikov } 378524e00b3SLiam R. Howlett #endif 37912352d3cSKonstantin Khlebnikov 3801be7107fSHugh Dickins highest_address = vm_end_gap(vma); 381ed8ea815SMichel Lespinasse vma = vma->vm_next; 3821da177e4SLinus Torvalds i++; 3831da177e4SLinus Torvalds } 3845a0768f6SMichel Lespinasse if (i != mm->map_count) { 3858542bdfcSSasha Levin pr_emerg("map_count %d vm_next %d\n", mm->map_count, i); 3865a0768f6SMichel Lespinasse bug = 1; 3875a0768f6SMichel Lespinasse } 3885a0768f6SMichel Lespinasse if (highest_address != mm->highest_vm_end) { 3898542bdfcSSasha Levin pr_emerg("mm->highest_vm_end %lx, found %lx\n", 3905a0768f6SMichel Lespinasse mm->highest_vm_end, highest_address); 3915a0768f6SMichel Lespinasse bug = 1; 3925a0768f6SMichel Lespinasse } 39396dad67fSSasha Levin VM_BUG_ON_MM(bug, mm); 3941da177e4SLinus Torvalds } 395524e00b3SLiam R. Howlett 396524e00b3SLiam R. Howlett #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ 397d4af56c5SLiam R. Howlett #define validate_mm_mt(root) do { } while (0) 3981da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 399524e00b3SLiam R. Howlett #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 400d3737187SMichel Lespinasse 401bf181b9fSMichel Lespinasse /* 402bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 403bf181b9fSMichel Lespinasse * anon_vma's interval trees. 404bf181b9fSMichel Lespinasse * 405bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 406bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 407bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 408bf181b9fSMichel Lespinasse * 409bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 410bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 411bf181b9fSMichel Lespinasse * 412c1e8d7c6SMichel Lespinasse * The entire update must be protected by exclusive mmap_lock and by 413bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 414bf181b9fSMichel Lespinasse */ 415bf181b9fSMichel Lespinasse static inline void 416bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 417bf181b9fSMichel Lespinasse { 418bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 419bf181b9fSMichel Lespinasse 420bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 421bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 422bf181b9fSMichel Lespinasse } 423bf181b9fSMichel Lespinasse 424bf181b9fSMichel Lespinasse static inline void 425bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 426bf181b9fSMichel Lespinasse { 427bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 428bf181b9fSMichel Lespinasse 429bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 430bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 431bf181b9fSMichel Lespinasse } 432bf181b9fSMichel Lespinasse 433524e00b3SLiam R. Howlett /* 434524e00b3SLiam R. Howlett * range_has_overlap() - Check the @start - @end range for overlapping VMAs and 435524e00b3SLiam R. Howlett * sets up a pointer to the previous VMA 436524e00b3SLiam R. Howlett * @mm: the mm struct 437524e00b3SLiam R. Howlett * @start: the start address of the range 438524e00b3SLiam R. Howlett * @end: the end address of the range 439524e00b3SLiam R. Howlett * @pprev: the pointer to the pointer of the previous VMA 440524e00b3SLiam R. Howlett * 441524e00b3SLiam R. Howlett * Returns: True if there is an overlapping VMA, false otherwise 442524e00b3SLiam R. Howlett */ 443524e00b3SLiam R. Howlett static inline 444524e00b3SLiam R. Howlett bool range_has_overlap(struct mm_struct *mm, unsigned long start, 445524e00b3SLiam R. Howlett unsigned long end, struct vm_area_struct **pprev) 4461da177e4SLinus Torvalds { 447524e00b3SLiam R. Howlett struct vm_area_struct *existing; 4481da177e4SLinus Torvalds 449524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 450524e00b3SLiam R. Howlett existing = mas_find(&mas, end - 1); 451524e00b3SLiam R. Howlett *pprev = mas_prev(&mas, 0); 452524e00b3SLiam R. Howlett return existing ? true : false; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4553903b55aSLiam R. Howlett /* 456f39af059SMatthew Wilcox (Oracle) * __vma_next() - Get the next VMA. 4573903b55aSLiam R. Howlett * @mm: The mm_struct. 4583903b55aSLiam R. Howlett * @vma: The current vma. 4593903b55aSLiam R. Howlett * 4603903b55aSLiam R. Howlett * If @vma is NULL, return the first vma in the mm. 4613903b55aSLiam R. Howlett * 4623903b55aSLiam R. Howlett * Returns: The next VMA after @vma. 4633903b55aSLiam R. Howlett */ 464f39af059SMatthew Wilcox (Oracle) static inline struct vm_area_struct *__vma_next(struct mm_struct *mm, 4653903b55aSLiam R. Howlett struct vm_area_struct *vma) 4663903b55aSLiam R. Howlett { 4673903b55aSLiam R. Howlett if (!vma) 4683903b55aSLiam R. Howlett return mm->mmap; 4693903b55aSLiam R. Howlett 4703903b55aSLiam R. Howlett return vma->vm_next; 4713903b55aSLiam R. Howlett } 472fb8090b6SLiam R. Howlett 473e8420a8eSCyril Hrubis static unsigned long count_vma_pages_range(struct mm_struct *mm, 474e8420a8eSCyril Hrubis unsigned long addr, unsigned long end) 475e8420a8eSCyril Hrubis { 4762e3af1dbSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, addr); 477e8420a8eSCyril Hrubis struct vm_area_struct *vma; 4782e3af1dbSMatthew Wilcox (Oracle) unsigned long nr_pages = 0; 479e8420a8eSCyril Hrubis 4802e3af1dbSMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) { 4812e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_start = max(addr, vma->vm_start); 4822e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_end = min(end, vma->vm_end); 483e8420a8eSCyril Hrubis 4842e3af1dbSMatthew Wilcox (Oracle) nr_pages += PHYS_PFN(vm_end - vm_start); 485e8420a8eSCyril Hrubis } 486e8420a8eSCyril Hrubis 487e8420a8eSCyril Hrubis return nr_pages; 488e8420a8eSCyril Hrubis } 489e8420a8eSCyril Hrubis 490cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds struct file *file; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds file = vma->vm_file; 4951da177e4SLinus Torvalds if (file) { 4961da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 499cf508b58SMiaohe Lin mapping_allow_writable(mapping); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 5026b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5031da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 507d4af56c5SLiam R. Howlett /* 508d4af56c5SLiam R. Howlett * vma_mas_store() - Store a VMA in the maple tree. 509d4af56c5SLiam R. Howlett * @vma: The vm_area_struct 510d4af56c5SLiam R. Howlett * @mas: The maple state 511d4af56c5SLiam R. Howlett * 512d4af56c5SLiam R. Howlett * Efficient way to store a VMA in the maple tree when the @mas has already 513d4af56c5SLiam R. Howlett * walked to the correct location. 514d4af56c5SLiam R. Howlett * 515d4af56c5SLiam R. Howlett * Note: the end address is inclusive in the maple tree. 516d4af56c5SLiam R. Howlett */ 517d4af56c5SLiam R. Howlett void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas) 518d4af56c5SLiam R. Howlett { 519d4af56c5SLiam R. Howlett trace_vma_store(mas->tree, vma); 520d4af56c5SLiam R. Howlett mas_set_range(mas, vma->vm_start, vma->vm_end - 1); 521d4af56c5SLiam R. Howlett mas_store_prealloc(mas, vma); 522d4af56c5SLiam R. Howlett } 523d4af56c5SLiam R. Howlett 524d4af56c5SLiam R. Howlett /* 525d4af56c5SLiam R. Howlett * vma_mas_remove() - Remove a VMA from the maple tree. 526d4af56c5SLiam R. Howlett * @vma: The vm_area_struct 527d4af56c5SLiam R. Howlett * @mas: The maple state 528d4af56c5SLiam R. Howlett * 529d4af56c5SLiam R. Howlett * Efficient way to remove a VMA from the maple tree when the @mas has already 530d4af56c5SLiam R. Howlett * been established and points to the correct location. 531d4af56c5SLiam R. Howlett * Note: the end address is inclusive in the maple tree. 532d4af56c5SLiam R. Howlett */ 533d4af56c5SLiam R. Howlett void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas) 534d4af56c5SLiam R. Howlett { 535d4af56c5SLiam R. Howlett trace_vma_mas_szero(mas->tree, vma->vm_start, vma->vm_end - 1); 536d4af56c5SLiam R. Howlett mas->index = vma->vm_start; 537d4af56c5SLiam R. Howlett mas->last = vma->vm_end - 1; 538d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 539d4af56c5SLiam R. Howlett } 540d4af56c5SLiam R. Howlett 541d4af56c5SLiam R. Howlett /* 542d4af56c5SLiam R. Howlett * vma_mas_szero() - Set a given range to zero. Used when modifying a 543d4af56c5SLiam R. Howlett * vm_area_struct start or end. 544d4af56c5SLiam R. Howlett * 545d4af56c5SLiam R. Howlett * @mm: The struct_mm 546d4af56c5SLiam R. Howlett * @start: The start address to zero 547d4af56c5SLiam R. Howlett * @end: The end address to zero. 548d4af56c5SLiam R. Howlett */ 549d4af56c5SLiam R. Howlett static inline void vma_mas_szero(struct ma_state *mas, unsigned long start, 550d4af56c5SLiam R. Howlett unsigned long end) 551d4af56c5SLiam R. Howlett { 552d4af56c5SLiam R. Howlett trace_vma_mas_szero(mas->tree, start, end - 1); 553d4af56c5SLiam R. Howlett mas_set_range(mas, start, end - 1); 554d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 555d4af56c5SLiam R. Howlett } 556d4af56c5SLiam R. Howlett 557d4af56c5SLiam R. Howlett static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 558524e00b3SLiam R. Howlett struct vm_area_struct *prev) 5591da177e4SLinus Torvalds { 560d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 5611da177e4SLinus Torvalds struct address_space *mapping = NULL; 5621da177e4SLinus Torvalds 563d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 564d4af56c5SLiam R. Howlett return -ENOMEM; 565d4af56c5SLiam R. Howlett 56664ac4940SHuang Shijie if (vma->vm_file) { 5671da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 56883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 56964ac4940SHuang Shijie } 5701da177e4SLinus Torvalds 571d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 572524e00b3SLiam R. Howlett __vma_link_list(mm, vma, prev); 5731da177e4SLinus Torvalds __vma_link_file(vma); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds if (mapping) 57683cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds mm->map_count++; 5791da177e4SLinus Torvalds validate_mm(mm); 580d4af56c5SLiam R. Howlett return 0; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* 58488f6b4c3SKautuk Consul * Helper for vma_adjust() in the split_vma insert case: insert a vma into the 585524e00b3SLiam R. Howlett * mm's list and the mm tree. It has already been inserted into the interval tree. 5861da177e4SLinus Torvalds */ 587d4af56c5SLiam R. Howlett static void __insert_vm_struct(struct mm_struct *mm, struct ma_state *mas, 5883b0e81a1SLiam R. Howlett struct vm_area_struct *vma, unsigned long location) 5891da177e4SLinus Torvalds { 5906597d783SHugh Dickins struct vm_area_struct *prev; 5911da177e4SLinus Torvalds 5923b0e81a1SLiam R. Howlett mas_set(mas, location); 593524e00b3SLiam R. Howlett prev = mas_prev(mas, 0); 594d4af56c5SLiam R. Howlett vma_mas_store(vma, mas); 595d4af56c5SLiam R. Howlett __vma_link_list(mm, vma, prev); 5961da177e4SLinus Torvalds mm->map_count++; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* 600*4dd1b841SLiam R. Howlett * vma_expand - Expand an existing VMA 601*4dd1b841SLiam R. Howlett * 602*4dd1b841SLiam R. Howlett * @mas: The maple state 603*4dd1b841SLiam R. Howlett * @vma: The vma to expand 604*4dd1b841SLiam R. Howlett * @start: The start of the vma 605*4dd1b841SLiam R. Howlett * @end: The exclusive end of the vma 606*4dd1b841SLiam R. Howlett * @pgoff: The page offset of vma 607*4dd1b841SLiam R. Howlett * @next: The current of next vma. 608*4dd1b841SLiam R. Howlett * 609*4dd1b841SLiam R. Howlett * Expand @vma to @start and @end. Can expand off the start and end. Will 610*4dd1b841SLiam R. Howlett * expand over @next if it's different from @vma and @end == @next->vm_end. 611*4dd1b841SLiam R. Howlett * Checking if the @vma can expand and merge with @next needs to be handled by 612*4dd1b841SLiam R. Howlett * the caller. 613*4dd1b841SLiam R. Howlett * 614*4dd1b841SLiam R. Howlett * Returns: 0 on success 615*4dd1b841SLiam R. Howlett */ 616*4dd1b841SLiam R. Howlett inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma, 617*4dd1b841SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 618*4dd1b841SLiam R. Howlett struct vm_area_struct *next) 619*4dd1b841SLiam R. Howlett { 620*4dd1b841SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 621*4dd1b841SLiam R. Howlett struct address_space *mapping = NULL; 622*4dd1b841SLiam R. Howlett struct rb_root_cached *root = NULL; 623*4dd1b841SLiam R. Howlett struct anon_vma *anon_vma = vma->anon_vma; 624*4dd1b841SLiam R. Howlett struct file *file = vma->vm_file; 625*4dd1b841SLiam R. Howlett bool remove_next = false; 626*4dd1b841SLiam R. Howlett 627*4dd1b841SLiam R. Howlett if (next && (vma != next) && (end == next->vm_end)) { 628*4dd1b841SLiam R. Howlett remove_next = true; 629*4dd1b841SLiam R. Howlett if (next->anon_vma && !vma->anon_vma) { 630*4dd1b841SLiam R. Howlett int error; 631*4dd1b841SLiam R. Howlett 632*4dd1b841SLiam R. Howlett anon_vma = next->anon_vma; 633*4dd1b841SLiam R. Howlett vma->anon_vma = anon_vma; 634*4dd1b841SLiam R. Howlett error = anon_vma_clone(vma, next); 635*4dd1b841SLiam R. Howlett if (error) 636*4dd1b841SLiam R. Howlett return error; 637*4dd1b841SLiam R. Howlett } 638*4dd1b841SLiam R. Howlett } 639*4dd1b841SLiam R. Howlett 640*4dd1b841SLiam R. Howlett /* Not merging but overwriting any part of next is not handled. */ 641*4dd1b841SLiam R. Howlett VM_BUG_ON(next && !remove_next && next != vma && end > next->vm_start); 642*4dd1b841SLiam R. Howlett /* Only handles expanding */ 643*4dd1b841SLiam R. Howlett VM_BUG_ON(vma->vm_start < start || vma->vm_end > end); 644*4dd1b841SLiam R. Howlett 645*4dd1b841SLiam R. Howlett if (mas_preallocate(mas, vma, GFP_KERNEL)) 646*4dd1b841SLiam R. Howlett goto nomem; 647*4dd1b841SLiam R. Howlett 648*4dd1b841SLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 649*4dd1b841SLiam R. Howlett 650*4dd1b841SLiam R. Howlett if (file) { 651*4dd1b841SLiam R. Howlett mapping = file->f_mapping; 652*4dd1b841SLiam R. Howlett root = &mapping->i_mmap; 653*4dd1b841SLiam R. Howlett uprobe_munmap(vma, vma->vm_start, vma->vm_end); 654*4dd1b841SLiam R. Howlett i_mmap_lock_write(mapping); 655*4dd1b841SLiam R. Howlett } 656*4dd1b841SLiam R. Howlett 657*4dd1b841SLiam R. Howlett if (anon_vma) { 658*4dd1b841SLiam R. Howlett anon_vma_lock_write(anon_vma); 659*4dd1b841SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 660*4dd1b841SLiam R. Howlett } 661*4dd1b841SLiam R. Howlett 662*4dd1b841SLiam R. Howlett if (file) { 663*4dd1b841SLiam R. Howlett flush_dcache_mmap_lock(mapping); 664*4dd1b841SLiam R. Howlett vma_interval_tree_remove(vma, root); 665*4dd1b841SLiam R. Howlett } 666*4dd1b841SLiam R. Howlett 667*4dd1b841SLiam R. Howlett vma->vm_start = start; 668*4dd1b841SLiam R. Howlett vma->vm_end = end; 669*4dd1b841SLiam R. Howlett vma->vm_pgoff = pgoff; 670*4dd1b841SLiam R. Howlett /* Note: mas must be pointing to the expanding VMA */ 671*4dd1b841SLiam R. Howlett vma_mas_store(vma, mas); 672*4dd1b841SLiam R. Howlett 673*4dd1b841SLiam R. Howlett if (file) { 674*4dd1b841SLiam R. Howlett vma_interval_tree_insert(vma, root); 675*4dd1b841SLiam R. Howlett flush_dcache_mmap_unlock(mapping); 676*4dd1b841SLiam R. Howlett } 677*4dd1b841SLiam R. Howlett 678*4dd1b841SLiam R. Howlett /* Expanding over the next vma */ 679*4dd1b841SLiam R. Howlett if (remove_next) { 680*4dd1b841SLiam R. Howlett /* Remove from mm linked list - also updates highest_vm_end */ 681*4dd1b841SLiam R. Howlett __vma_unlink_list(mm, next); 682*4dd1b841SLiam R. Howlett 683*4dd1b841SLiam R. Howlett /* Kill the cache */ 684*4dd1b841SLiam R. Howlett vmacache_invalidate(mm); 685*4dd1b841SLiam R. Howlett 686*4dd1b841SLiam R. Howlett if (file) 687*4dd1b841SLiam R. Howlett __remove_shared_vm_struct(next, file, mapping); 688*4dd1b841SLiam R. Howlett 689*4dd1b841SLiam R. Howlett } else if (!next) { 690*4dd1b841SLiam R. Howlett mm->highest_vm_end = vm_end_gap(vma); 691*4dd1b841SLiam R. Howlett } 692*4dd1b841SLiam R. Howlett 693*4dd1b841SLiam R. Howlett if (anon_vma) { 694*4dd1b841SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 695*4dd1b841SLiam R. Howlett anon_vma_unlock_write(anon_vma); 696*4dd1b841SLiam R. Howlett } 697*4dd1b841SLiam R. Howlett 698*4dd1b841SLiam R. Howlett if (file) { 699*4dd1b841SLiam R. Howlett i_mmap_unlock_write(mapping); 700*4dd1b841SLiam R. Howlett uprobe_mmap(vma); 701*4dd1b841SLiam R. Howlett } 702*4dd1b841SLiam R. Howlett 703*4dd1b841SLiam R. Howlett if (remove_next) { 704*4dd1b841SLiam R. Howlett if (file) { 705*4dd1b841SLiam R. Howlett uprobe_munmap(next, next->vm_start, next->vm_end); 706*4dd1b841SLiam R. Howlett fput(file); 707*4dd1b841SLiam R. Howlett } 708*4dd1b841SLiam R. Howlett if (next->anon_vma) 709*4dd1b841SLiam R. Howlett anon_vma_merge(vma, next); 710*4dd1b841SLiam R. Howlett mm->map_count--; 711*4dd1b841SLiam R. Howlett mpol_put(vma_policy(next)); 712*4dd1b841SLiam R. Howlett vm_area_free(next); 713*4dd1b841SLiam R. Howlett } 714*4dd1b841SLiam R. Howlett 715*4dd1b841SLiam R. Howlett validate_mm(mm); 716*4dd1b841SLiam R. Howlett return 0; 717*4dd1b841SLiam R. Howlett 718*4dd1b841SLiam R. Howlett nomem: 719*4dd1b841SLiam R. Howlett return -ENOMEM; 720*4dd1b841SLiam R. Howlett } 721*4dd1b841SLiam R. Howlett 722*4dd1b841SLiam R. Howlett /* 7231da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 7241da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 7251da177e4SLinus Torvalds * The following helper function should be used when such adjustments 7261da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 7271da177e4SLinus Torvalds * before we drop the necessary locks. 7281da177e4SLinus Torvalds */ 729e86f15eeSAndrea Arcangeli int __vma_adjust(struct vm_area_struct *vma, unsigned long start, 730e86f15eeSAndrea Arcangeli unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert, 731e86f15eeSAndrea Arcangeli struct vm_area_struct *expand) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 734524e00b3SLiam R. Howlett struct vm_area_struct *next_next, *next = find_vma(mm, vma->vm_end); 735524e00b3SLiam R. Howlett struct vm_area_struct *orig_vma = vma; 7361da177e4SLinus Torvalds struct address_space *mapping = NULL; 737f808c13fSDavidlohr Bueso struct rb_root_cached *root = NULL; 738012f1800SRik van Riel struct anon_vma *anon_vma = NULL; 7391da177e4SLinus Torvalds struct file *file = vma->vm_file; 740524e00b3SLiam R. Howlett bool vma_changed = false; 7411da177e4SLinus Torvalds long adjust_next = 0; 7421da177e4SLinus Torvalds int remove_next = 0; 743d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 744734537c9SKirill A. Shutemov struct vm_area_struct *exporter = NULL, *importer = NULL; 7453b0e81a1SLiam R. Howlett unsigned long ll_prev = vma->vm_start; /* linked list prev. */ 746287d97acSLinus Torvalds 747d4af56c5SLiam R. Howlett if (next && !insert) { 7481da177e4SLinus Torvalds if (end >= next->vm_end) { 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * vma expands, overlapping all the next, and 7511da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 75286d12e47SAndrea Arcangeli * The only other cases that gets here are 753e86f15eeSAndrea Arcangeli * case 1, case 7 and case 8. 754e86f15eeSAndrea Arcangeli */ 755e86f15eeSAndrea Arcangeli if (next == expand) { 756e86f15eeSAndrea Arcangeli /* 757e86f15eeSAndrea Arcangeli * The only case where we don't expand "vma" 758e86f15eeSAndrea Arcangeli * and we expand "next" instead is case 8. 759e86f15eeSAndrea Arcangeli */ 760e86f15eeSAndrea Arcangeli VM_WARN_ON(end != next->vm_end); 761e86f15eeSAndrea Arcangeli /* 762e86f15eeSAndrea Arcangeli * remove_next == 3 means we're 763e86f15eeSAndrea Arcangeli * removing "vma" and that to do so we 764e86f15eeSAndrea Arcangeli * swapped "vma" and "next". 765e86f15eeSAndrea Arcangeli */ 766e86f15eeSAndrea Arcangeli remove_next = 3; 767e86f15eeSAndrea Arcangeli VM_WARN_ON(file != next->vm_file); 768e86f15eeSAndrea Arcangeli swap(vma, next); 769e86f15eeSAndrea Arcangeli } else { 770e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != vma); 771e86f15eeSAndrea Arcangeli /* 772e86f15eeSAndrea Arcangeli * case 1, 6, 7, remove_next == 2 is case 6, 773e86f15eeSAndrea Arcangeli * remove_next == 1 is case 1 or 7. 7741da177e4SLinus Torvalds */ 775734537c9SKirill A. Shutemov remove_next = 1 + (end > next->vm_end); 776d4af56c5SLiam R. Howlett if (remove_next == 2) 777d4af56c5SLiam R. Howlett next_next = find_vma(mm, next->vm_end); 778d4af56c5SLiam R. Howlett 779e86f15eeSAndrea Arcangeli VM_WARN_ON(remove_next == 2 && 780e86f15eeSAndrea Arcangeli end != next->vm_next->vm_end); 781e86f15eeSAndrea Arcangeli } 782e86f15eeSAndrea Arcangeli 783287d97acSLinus Torvalds exporter = next; 7841da177e4SLinus Torvalds importer = vma; 785734537c9SKirill A. Shutemov 786734537c9SKirill A. Shutemov /* 787734537c9SKirill A. Shutemov * If next doesn't have anon_vma, import from vma after 788734537c9SKirill A. Shutemov * next, if the vma overlaps with it. 789734537c9SKirill A. Shutemov */ 79097a42cd4SAndrea Arcangeli if (remove_next == 2 && !next->anon_vma) 791734537c9SKirill A. Shutemov exporter = next->vm_next; 792734537c9SKirill A. Shutemov 7931da177e4SLinus Torvalds } else if (end > next->vm_start) { 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * vma expands, overlapping part of the next: 7961da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 7971da177e4SLinus Torvalds */ 798f9d86a60SWei Yang adjust_next = (end - next->vm_start); 799287d97acSLinus Torvalds exporter = next; 8001da177e4SLinus Torvalds importer = vma; 801e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 8021da177e4SLinus Torvalds } else if (end < vma->vm_end) { 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 8051da177e4SLinus Torvalds * split_vma inserting another: so it must be 8061da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 8071da177e4SLinus Torvalds */ 808f9d86a60SWei Yang adjust_next = -(vma->vm_end - end); 809287d97acSLinus Torvalds exporter = vma; 8101da177e4SLinus Torvalds importer = next; 811e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 8145beb4930SRik van Riel /* 8155beb4930SRik van Riel * Easily overlooked: when mprotect shifts the boundary, 8165beb4930SRik van Riel * make sure the expanding vma has anon_vma set if the 8175beb4930SRik van Riel * shrinking vma had, to cover any anon pages imported. 8185beb4930SRik van Riel */ 819287d97acSLinus Torvalds if (exporter && exporter->anon_vma && !importer->anon_vma) { 820c4ea95d7SDaniel Forrest int error; 821c4ea95d7SDaniel Forrest 822287d97acSLinus Torvalds importer->anon_vma = exporter->anon_vma; 823b800c91aSKonstantin Khlebnikov error = anon_vma_clone(importer, exporter); 8243fe89b3eSLeon Yu if (error) 825b800c91aSKonstantin Khlebnikov return error; 826b800c91aSKonstantin Khlebnikov } 8275beb4930SRik van Riel } 82837f9f559SKirill A. Shutemov 829d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 830d4af56c5SLiam R. Howlett return -ENOMEM; 831d4af56c5SLiam R. Howlett 832d4af56c5SLiam R. Howlett vma_adjust_trans_huge(orig_vma, start, end, adjust_next); 8331da177e4SLinus Torvalds if (file) { 8341da177e4SLinus Torvalds mapping = file->f_mapping; 8351da177e4SLinus Torvalds root = &mapping->i_mmap; 836cbc91f71SSrikar Dronamraju uprobe_munmap(vma, vma->vm_start, vma->vm_end); 837682968e0SSrikar Dronamraju 838682968e0SSrikar Dronamraju if (adjust_next) 83927ba0644SKirill A. Shutemov uprobe_munmap(next, next->vm_start, next->vm_end); 840682968e0SSrikar Dronamraju 84183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 8421da177e4SLinus Torvalds if (insert) { 8431da177e4SLinus Torvalds /* 8446b2dbba8SMichel Lespinasse * Put into interval tree now, so instantiated pages 8451da177e4SLinus Torvalds * are visible to arm/parisc __flush_dcache_page 8461da177e4SLinus Torvalds * throughout; but we cannot insert into address 8471da177e4SLinus Torvalds * space until vma start or end is updated. 8481da177e4SLinus Torvalds */ 8491da177e4SLinus Torvalds __vma_link_file(insert); 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 853012f1800SRik van Riel anon_vma = vma->anon_vma; 854bf181b9fSMichel Lespinasse if (!anon_vma && adjust_next) 855bf181b9fSMichel Lespinasse anon_vma = next->anon_vma; 856bf181b9fSMichel Lespinasse if (anon_vma) { 857e86f15eeSAndrea Arcangeli VM_WARN_ON(adjust_next && next->anon_vma && 858e86f15eeSAndrea Arcangeli anon_vma != next->anon_vma); 8594fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 860bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 861bf181b9fSMichel Lespinasse if (adjust_next) 862bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(next); 863bf181b9fSMichel Lespinasse } 864012f1800SRik van Riel 8650fc48a6eSWei Yang if (file) { 8661da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 8676b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, root); 8681da177e4SLinus Torvalds if (adjust_next) 8696b2dbba8SMichel Lespinasse vma_interval_tree_remove(next, root); 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 872d3737187SMichel Lespinasse if (start != vma->vm_start) { 8733b0e81a1SLiam R. Howlett if ((vma->vm_start < start) && 8743b0e81a1SLiam R. Howlett (!insert || (insert->vm_end != start))) { 875524e00b3SLiam R. Howlett vma_mas_szero(&mas, vma->vm_start, start); 8763b0e81a1SLiam R. Howlett VM_WARN_ON(insert && insert->vm_start > vma->vm_start); 8773b0e81a1SLiam R. Howlett } else { 878524e00b3SLiam R. Howlett vma_changed = true; 8793b0e81a1SLiam R. Howlett } 8801da177e4SLinus Torvalds vma->vm_start = start; 881d3737187SMichel Lespinasse } 882d3737187SMichel Lespinasse if (end != vma->vm_end) { 8833b0e81a1SLiam R. Howlett if (vma->vm_end > end) { 8843b0e81a1SLiam R. Howlett if (!insert || (insert->vm_start != end)) { 885524e00b3SLiam R. Howlett vma_mas_szero(&mas, end, vma->vm_end); 8863b0e81a1SLiam R. Howlett VM_WARN_ON(insert && 8873b0e81a1SLiam R. Howlett insert->vm_end < vma->vm_end); 8883b0e81a1SLiam R. Howlett } else if (insert->vm_start == end) { 8893b0e81a1SLiam R. Howlett ll_prev = vma->vm_end; 8903b0e81a1SLiam R. Howlett } 8913b0e81a1SLiam R. Howlett } else { 892524e00b3SLiam R. Howlett vma_changed = true; 8933b0e81a1SLiam R. Howlett } 8941da177e4SLinus Torvalds vma->vm_end = end; 895524e00b3SLiam R. Howlett if (!next) 896524e00b3SLiam R. Howlett mm->highest_vm_end = vm_end_gap(vma); 897d3737187SMichel Lespinasse } 898d4af56c5SLiam R. Howlett 899524e00b3SLiam R. Howlett if (vma_changed) 900d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 901d4af56c5SLiam R. Howlett 9021da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 9031da177e4SLinus Torvalds if (adjust_next) { 904f9d86a60SWei Yang next->vm_start += adjust_next; 905f9d86a60SWei Yang next->vm_pgoff += adjust_next >> PAGE_SHIFT; 906d4af56c5SLiam R. Howlett vma_mas_store(next, &mas); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 9090fc48a6eSWei Yang if (file) { 9101da177e4SLinus Torvalds if (adjust_next) 9116b2dbba8SMichel Lespinasse vma_interval_tree_insert(next, root); 9126b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, root); 9131da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds if (remove_next) { 9171da177e4SLinus Torvalds /* 9181da177e4SLinus Torvalds * vma_merge has merged next into vma, and needs 9191da177e4SLinus Torvalds * us to remove next before dropping the locks. 920d4af56c5SLiam R. Howlett * Since we have expanded over this vma, the maple tree will 921d4af56c5SLiam R. Howlett * have overwritten by storing the value 9221da177e4SLinus Torvalds */ 923524e00b3SLiam R. Howlett __vma_unlink_list(mm, next); 924d4af56c5SLiam R. Howlett if (remove_next == 2) 925524e00b3SLiam R. Howlett __vma_unlink_list(mm, next_next); 926524e00b3SLiam R. Howlett /* Kill the cache */ 927524e00b3SLiam R. Howlett vmacache_invalidate(mm); 928524e00b3SLiam R. Howlett 929d4af56c5SLiam R. Howlett if (file) { 9301da177e4SLinus Torvalds __remove_shared_vm_struct(next, file, mapping); 931d4af56c5SLiam R. Howlett if (remove_next == 2) 932d4af56c5SLiam R. Howlett __remove_shared_vm_struct(next_next, file, mapping); 933d4af56c5SLiam R. Howlett } 9341da177e4SLinus Torvalds } else if (insert) { 9351da177e4SLinus Torvalds /* 9361da177e4SLinus Torvalds * split_vma has split insert from vma, and needs 9371da177e4SLinus Torvalds * us to insert it before dropping the locks 9381da177e4SLinus Torvalds * (it may either follow vma or precede it). 9391da177e4SLinus Torvalds */ 9403b0e81a1SLiam R. Howlett __insert_vm_struct(mm, &mas, insert, ll_prev); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 943bf181b9fSMichel Lespinasse if (anon_vma) { 944bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 945bf181b9fSMichel Lespinasse if (adjust_next) 946bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(next); 94708b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 948bf181b9fSMichel Lespinasse } 9491da177e4SLinus Torvalds 9500fc48a6eSWei Yang if (file) { 951808fbdbeSWei Yang i_mmap_unlock_write(mapping); 9527b2d81d4SIngo Molnar uprobe_mmap(vma); 9532b144498SSrikar Dronamraju 9542b144498SSrikar Dronamraju if (adjust_next) 9557b2d81d4SIngo Molnar uprobe_mmap(next); 9562b144498SSrikar Dronamraju } 9572b144498SSrikar Dronamraju 9581da177e4SLinus Torvalds if (remove_next) { 959d4af56c5SLiam R. Howlett again: 960925d1c40SMatt Helsley if (file) { 961cbc91f71SSrikar Dronamraju uprobe_munmap(next, next->vm_start, next->vm_end); 9621da177e4SLinus Torvalds fput(file); 963925d1c40SMatt Helsley } 9645beb4930SRik van Riel if (next->anon_vma) 9655beb4930SRik van Riel anon_vma_merge(vma, next); 9661da177e4SLinus Torvalds mm->map_count--; 9673964acd0SOleg Nesterov mpol_put(vma_policy(next)); 968524e00b3SLiam R. Howlett if (remove_next != 2) 969524e00b3SLiam R. Howlett BUG_ON(vma->vm_end < next->vm_end); 9703928d4f5SLinus Torvalds vm_area_free(next); 971524e00b3SLiam R. Howlett 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * In mprotect's case 6 (see comments on vma_merge), 9741da177e4SLinus Torvalds * we must remove another next too. It would clutter 9751da177e4SLinus Torvalds * up the code too much to do both in one go. 9761da177e4SLinus Torvalds */ 977e86f15eeSAndrea Arcangeli if (remove_next != 3) { 978e86f15eeSAndrea Arcangeli /* 979e86f15eeSAndrea Arcangeli * If "next" was removed and vma->vm_end was 980e86f15eeSAndrea Arcangeli * expanded (up) over it, in turn 981e86f15eeSAndrea Arcangeli * "next->vm_prev->vm_end" changed and the 982e86f15eeSAndrea Arcangeli * "vma->vm_next" gap must be updated. 983e86f15eeSAndrea Arcangeli */ 984d4af56c5SLiam R. Howlett next = next_next; 985e86f15eeSAndrea Arcangeli } else { 986e86f15eeSAndrea Arcangeli /* 987e86f15eeSAndrea Arcangeli * For the scope of the comment "next" and 988e86f15eeSAndrea Arcangeli * "vma" considered pre-swap(): if "vma" was 989e86f15eeSAndrea Arcangeli * removed, next->vm_start was expanded (down) 990e86f15eeSAndrea Arcangeli * over it and the "next" gap must be updated. 991e86f15eeSAndrea Arcangeli * Because of the swap() the post-swap() "vma" 992e86f15eeSAndrea Arcangeli * actually points to pre-swap() "next" 993e86f15eeSAndrea Arcangeli * (post-swap() "next" as opposed is now a 994e86f15eeSAndrea Arcangeli * dangling pointer). 995e86f15eeSAndrea Arcangeli */ 996e86f15eeSAndrea Arcangeli next = vma; 997e86f15eeSAndrea Arcangeli } 998734537c9SKirill A. Shutemov if (remove_next == 2) { 999734537c9SKirill A. Shutemov remove_next = 1; 10001da177e4SLinus Torvalds goto again; 1001524e00b3SLiam R. Howlett } else if (!next) { 1002fb8c41e9SAndrea Arcangeli /* 1003fb8c41e9SAndrea Arcangeli * If remove_next == 2 we obviously can't 1004fb8c41e9SAndrea Arcangeli * reach this path. 1005fb8c41e9SAndrea Arcangeli * 1006fb8c41e9SAndrea Arcangeli * If remove_next == 3 we can't reach this 1007fb8c41e9SAndrea Arcangeli * path because pre-swap() next is always not 1008fb8c41e9SAndrea Arcangeli * NULL. pre-swap() "next" is not being 1009fb8c41e9SAndrea Arcangeli * removed and its next->vm_end is not altered 1010fb8c41e9SAndrea Arcangeli * (and furthermore "end" already matches 1011fb8c41e9SAndrea Arcangeli * next->vm_end in remove_next == 3). 1012fb8c41e9SAndrea Arcangeli * 1013fb8c41e9SAndrea Arcangeli * We reach this only in the remove_next == 1 1014fb8c41e9SAndrea Arcangeli * case if the "next" vma that was removed was 1015fb8c41e9SAndrea Arcangeli * the highest vma of the mm. However in such 1016fb8c41e9SAndrea Arcangeli * case next->vm_end == "end" and the extended 1017fb8c41e9SAndrea Arcangeli * "vma" has vma->vm_end == next->vm_end so 1018fb8c41e9SAndrea Arcangeli * mm->highest_vm_end doesn't need any update 1019fb8c41e9SAndrea Arcangeli * in remove_next == 1 case. 1020fb8c41e9SAndrea Arcangeli */ 10211be7107fSHugh Dickins VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma)); 1022fb8c41e9SAndrea Arcangeli } 10231da177e4SLinus Torvalds } 10242b144498SSrikar Dronamraju if (insert && file) 10257b2d81d4SIngo Molnar uprobe_mmap(insert); 10261da177e4SLinus Torvalds 10273b0e81a1SLiam R. Howlett mas_destroy(&mas); 10281da177e4SLinus Torvalds validate_mm(mm); 10295beb4930SRik van Riel return 0; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* 10331da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 10341da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 103719a809afSAndrea Arcangeli struct file *file, unsigned long vm_flags, 10389a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 10395c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 10401da177e4SLinus Torvalds { 104134228d47SCyrill Gorcunov /* 104234228d47SCyrill Gorcunov * VM_SOFTDIRTY should not prevent from VMA merging, if we 104334228d47SCyrill Gorcunov * match the flags but dirty bit -- the caller should mark 104434228d47SCyrill Gorcunov * merged VMA as dirty. If dirty bit won't be excluded from 10458bb4e7a2SWei Yang * comparison, we increase pressure on the memory system forcing 104634228d47SCyrill Gorcunov * the kernel to generate new VMAs when old one could be 104734228d47SCyrill Gorcunov * extended instead. 104834228d47SCyrill Gorcunov */ 104934228d47SCyrill Gorcunov if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 10501da177e4SLinus Torvalds return 0; 10511da177e4SLinus Torvalds if (vma->vm_file != file) 10521da177e4SLinus Torvalds return 0; 10531da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 10541da177e4SLinus Torvalds return 0; 105519a809afSAndrea Arcangeli if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 105619a809afSAndrea Arcangeli return 0; 10575c26f6acSSuren Baghdasaryan if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 10589a10064fSColin Cross return 0; 10591da177e4SLinus Torvalds return 1; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 1063965f55deSShaohua Li struct anon_vma *anon_vma2, 1064965f55deSShaohua Li struct vm_area_struct *vma) 10651da177e4SLinus Torvalds { 1066965f55deSShaohua Li /* 1067965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 1068965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 1069965f55deSShaohua Li */ 1070965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 1071965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 1072965f55deSShaohua Li return 1; 1073965f55deSShaohua Li return anon_vma1 == anon_vma2; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* 10771da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 10781da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 10791da177e4SLinus Torvalds * 10801da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 10811da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 10821da177e4SLinus Torvalds * 10831da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 108445e55300SPeter Collingbourne * indices (16TB on ia32) because do_mmap() does not permit mmap's which 10851da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 10861da177e4SLinus Torvalds */ 10871da177e4SLinus Torvalds static int 10881da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 108919a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 109019a809afSAndrea Arcangeli pgoff_t vm_pgoff, 10919a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 10925c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 10931da177e4SLinus Torvalds { 10949a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 1095965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 10961da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 10971da177e4SLinus Torvalds return 1; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds return 0; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds /* 11031da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 11041da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 11051da177e4SLinus Torvalds * 11061da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 11071da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 11081da177e4SLinus Torvalds */ 11091da177e4SLinus Torvalds static int 11101da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 111119a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 111219a809afSAndrea Arcangeli pgoff_t vm_pgoff, 11139a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 11145c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 11151da177e4SLinus Torvalds { 11169a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 1117965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 11181da177e4SLinus Torvalds pgoff_t vm_pglen; 1119d6e93217SLibin vm_pglen = vma_pages(vma); 11201da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 11211da177e4SLinus Torvalds return 1; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds return 0; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /* 11279a10064fSColin Cross * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 11289a10064fSColin Cross * figure out whether that can be merged with its predecessor or its 11299a10064fSColin Cross * successor. Or both (it neatly fills a hole). 11301da177e4SLinus Torvalds * 11311da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 11321da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 11331da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 11341da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 11351da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 11361da177e4SLinus Torvalds * case has already been eliminated. 11371da177e4SLinus Torvalds * 11381da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 11391da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 11401da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 11411da177e4SLinus Torvalds * 11425d42ab29SWei Yang * AAAA AAAA AAAA 11435d42ab29SWei Yang * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN 11445d42ab29SWei Yang * cannot merge might become might become 11455d42ab29SWei Yang * PPNNNNNNNNNN PPPPPPPPPPNN 11465d42ab29SWei Yang * mmap, brk or case 4 below case 5 below 11475d42ab29SWei Yang * mremap move: 11485d42ab29SWei Yang * AAAA AAAA 11495d42ab29SWei Yang * PPPP NNNN PPPPNNNNXXXX 11505d42ab29SWei Yang * might become might become 11515d42ab29SWei Yang * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 11525d42ab29SWei Yang * PPPPPPPPNNNN 2 or PPPPPPPPXXXX 7 or 11535d42ab29SWei Yang * PPPPNNNNNNNN 3 PPPPXXXXXXXX 8 11541da177e4SLinus Torvalds * 11558bb4e7a2SWei Yang * It is important for case 8 that the vma NNNN overlapping the 1156e86f15eeSAndrea Arcangeli * region AAAA is never going to extended over XXXX. Instead XXXX must 1157e86f15eeSAndrea Arcangeli * be extended in region AAAA and NNNN must be removed. This way in 1158e86f15eeSAndrea Arcangeli * all cases where vma_merge succeeds, the moment vma_adjust drops the 1159e86f15eeSAndrea Arcangeli * rmap_locks, the properties of the merged vma will be already 1160e86f15eeSAndrea Arcangeli * correct for the whole merged range. Some of those properties like 1161e86f15eeSAndrea Arcangeli * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 1162e86f15eeSAndrea Arcangeli * be correct for the whole merged range immediately after the 1163e86f15eeSAndrea Arcangeli * rmap_locks are released. Otherwise if XXXX would be removed and 1164e86f15eeSAndrea Arcangeli * NNNN would be extended over the XXXX range, remove_migration_ptes 1165e86f15eeSAndrea Arcangeli * or other rmap walkers (if working on addresses beyond the "end" 1166e86f15eeSAndrea Arcangeli * parameter) may establish ptes with the wrong permissions of NNNN 1167e86f15eeSAndrea Arcangeli * instead of the right permissions of XXXX. 11681da177e4SLinus Torvalds */ 11691da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm, 11701da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 11711da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 11721da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 117319a809afSAndrea Arcangeli pgoff_t pgoff, struct mempolicy *policy, 11749a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 11755c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 11761da177e4SLinus Torvalds { 11771da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 11781da177e4SLinus Torvalds struct vm_area_struct *area, *next; 11795beb4930SRik van Riel int err; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* 11821da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 11831da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 11841da177e4SLinus Torvalds */ 11851da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 11861da177e4SLinus Torvalds return NULL; 11871da177e4SLinus Torvalds 1188f39af059SMatthew Wilcox (Oracle) next = __vma_next(mm, prev); 11891da177e4SLinus Torvalds area = next; 1190e86f15eeSAndrea Arcangeli if (area && area->vm_end == end) /* cases 6, 7, 8 */ 11911da177e4SLinus Torvalds next = next->vm_next; 11921da177e4SLinus Torvalds 1193e86f15eeSAndrea Arcangeli /* verify some invariant that must be enforced by the caller */ 1194e86f15eeSAndrea Arcangeli VM_WARN_ON(prev && addr <= prev->vm_start); 1195e86f15eeSAndrea Arcangeli VM_WARN_ON(area && end > area->vm_end); 1196e86f15eeSAndrea Arcangeli VM_WARN_ON(addr >= end); 1197e86f15eeSAndrea Arcangeli 11981da177e4SLinus Torvalds /* 11991da177e4SLinus Torvalds * Can it merge with the predecessor? 12001da177e4SLinus Torvalds */ 12011da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 12021da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 12031da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 120419a809afSAndrea Arcangeli anon_vma, file, pgoff, 12059a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 12061da177e4SLinus Torvalds /* 12071da177e4SLinus Torvalds * OK, it can. Can we now merge in the successor as well? 12081da177e4SLinus Torvalds */ 12091da177e4SLinus Torvalds if (next && end == next->vm_start && 12101da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 12111da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 121219a809afSAndrea Arcangeli anon_vma, file, 121319a809afSAndrea Arcangeli pgoff+pglen, 12149a10064fSColin Cross vm_userfaultfd_ctx, anon_name) && 12151da177e4SLinus Torvalds is_mergeable_anon_vma(prev->anon_vma, 1216965f55deSShaohua Li next->anon_vma, NULL)) { 12171da177e4SLinus Torvalds /* cases 1, 6 */ 1218e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1219e86f15eeSAndrea Arcangeli next->vm_end, prev->vm_pgoff, NULL, 1220e86f15eeSAndrea Arcangeli prev); 12211da177e4SLinus Torvalds } else /* cases 2, 5, 7 */ 1222e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1223e86f15eeSAndrea Arcangeli end, prev->vm_pgoff, NULL, prev); 12245beb4930SRik van Riel if (err) 12255beb4930SRik van Riel return NULL; 1226c791576cSYang Shi khugepaged_enter_vma(prev, vm_flags); 12271da177e4SLinus Torvalds return prev; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds /* 12311da177e4SLinus Torvalds * Can this new request be merged in front of next? 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds if (next && end == next->vm_start && 12341da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 12351da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 123619a809afSAndrea Arcangeli anon_vma, file, pgoff+pglen, 12379a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 12381da177e4SLinus Torvalds if (prev && addr < prev->vm_end) /* case 4 */ 1239e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1240e86f15eeSAndrea Arcangeli addr, prev->vm_pgoff, NULL, next); 1241e86f15eeSAndrea Arcangeli else { /* cases 3, 8 */ 1242e86f15eeSAndrea Arcangeli err = __vma_adjust(area, addr, next->vm_end, 1243e86f15eeSAndrea Arcangeli next->vm_pgoff - pglen, NULL, next); 1244e86f15eeSAndrea Arcangeli /* 1245e86f15eeSAndrea Arcangeli * In case 3 area is already equal to next and 1246e86f15eeSAndrea Arcangeli * this is a noop, but in case 8 "area" has 1247e86f15eeSAndrea Arcangeli * been removed and next was expanded over it. 1248e86f15eeSAndrea Arcangeli */ 1249e86f15eeSAndrea Arcangeli area = next; 1250e86f15eeSAndrea Arcangeli } 12515beb4930SRik van Riel if (err) 12525beb4930SRik van Riel return NULL; 1253c791576cSYang Shi khugepaged_enter_vma(area, vm_flags); 12541da177e4SLinus Torvalds return area; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds return NULL; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds /* 1261b4f315b4SEthon Paul * Rough compatibility check to quickly see if it's even worth looking 1262d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1263d0e9fe17SLinus Torvalds * 1264d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1265d0e9fe17SLinus Torvalds * in things that mprotect may change. 1266d0e9fe17SLinus Torvalds * 1267d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1268d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1269d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1270d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1271d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1272d0e9fe17SLinus Torvalds */ 1273d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1274d0e9fe17SLinus Torvalds { 1275d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1276d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1277d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 12786cb4d9a2SAnshuman Khandual !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1279d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1280d0e9fe17SLinus Torvalds } 1281d0e9fe17SLinus Torvalds 1282d0e9fe17SLinus Torvalds /* 1283d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1284d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1285d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1286d0e9fe17SLinus Torvalds * to share the anon_vma. 1287d0e9fe17SLinus Torvalds * 12885b449489SFlorian Rommel * NOTE! This runs with mmap_lock held for reading, so it is possible that 1289d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1290d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1291d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1292d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 12934db0c3c2SJason Low * we do that READ_ONCE() to make sure that we never re-load the pointer. 1294d0e9fe17SLinus Torvalds * 1295d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1296d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1297d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1298d0e9fe17SLinus Torvalds * a fork). 1299d0e9fe17SLinus Torvalds * 1300d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1301d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 13025b449489SFlorian Rommel * a read lock on the mmap_lock. 1303d0e9fe17SLinus Torvalds */ 1304d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1305d0e9fe17SLinus Torvalds { 1306d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 13074db0c3c2SJason Low struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1308d0e9fe17SLinus Torvalds 1309d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1310d0e9fe17SLinus Torvalds return anon_vma; 1311d0e9fe17SLinus Torvalds } 1312d0e9fe17SLinus Torvalds return NULL; 1313d0e9fe17SLinus Torvalds } 1314d0e9fe17SLinus Torvalds 1315d0e9fe17SLinus Torvalds /* 13161da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 13171da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 13181da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 13191da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 13201da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 13211da177e4SLinus Torvalds * mprotect. 13221da177e4SLinus Torvalds */ 13231da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 13241da177e4SLinus Torvalds { 1325a67c8caaSMiaohe Lin struct anon_vma *anon_vma = NULL; 13261da177e4SLinus Torvalds 1327a67c8caaSMiaohe Lin /* Try next first. */ 1328a67c8caaSMiaohe Lin if (vma->vm_next) { 1329a67c8caaSMiaohe Lin anon_vma = reusable_anon_vma(vma->vm_next, vma, vma->vm_next); 1330d0e9fe17SLinus Torvalds if (anon_vma) 1331d0e9fe17SLinus Torvalds return anon_vma; 1332a67c8caaSMiaohe Lin } 13331da177e4SLinus Torvalds 1334a67c8caaSMiaohe Lin /* Try prev next. */ 1335a67c8caaSMiaohe Lin if (vma->vm_prev) 1336a67c8caaSMiaohe Lin anon_vma = reusable_anon_vma(vma->vm_prev, vma->vm_prev, vma); 1337a67c8caaSMiaohe Lin 13381da177e4SLinus Torvalds /* 1339a67c8caaSMiaohe Lin * We might reach here with anon_vma == NULL if we can't find 1340a67c8caaSMiaohe Lin * any reusable anon_vma. 13411da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 13421da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 13431da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 13441da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 13451da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 13461da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 13471da177e4SLinus Torvalds */ 1348a67c8caaSMiaohe Lin return anon_vma; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /* 135240401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 135340401530SAl Viro * low as possible but still greater than mmap_min_addr 135440401530SAl Viro */ 135540401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 135640401530SAl Viro { 135740401530SAl Viro hint &= PAGE_MASK; 135840401530SAl Viro if (((void *)hint != NULL) && 135940401530SAl Viro (hint < mmap_min_addr)) 136040401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 136140401530SAl Viro return hint; 136240401530SAl Viro } 136340401530SAl Viro 13646aeb2542SMike Rapoport int mlock_future_check(struct mm_struct *mm, unsigned long flags, 1365363ee17fSDavidlohr Bueso unsigned long len) 1366363ee17fSDavidlohr Bueso { 1367363ee17fSDavidlohr Bueso unsigned long locked, lock_limit; 1368363ee17fSDavidlohr Bueso 1369363ee17fSDavidlohr Bueso /* mlock MCL_FUTURE? */ 1370363ee17fSDavidlohr Bueso if (flags & VM_LOCKED) { 1371363ee17fSDavidlohr Bueso locked = len >> PAGE_SHIFT; 1372363ee17fSDavidlohr Bueso locked += mm->locked_vm; 1373363ee17fSDavidlohr Bueso lock_limit = rlimit(RLIMIT_MEMLOCK); 1374363ee17fSDavidlohr Bueso lock_limit >>= PAGE_SHIFT; 1375363ee17fSDavidlohr Bueso if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 1376363ee17fSDavidlohr Bueso return -EAGAIN; 1377363ee17fSDavidlohr Bueso } 1378363ee17fSDavidlohr Bueso return 0; 1379363ee17fSDavidlohr Bueso } 1380363ee17fSDavidlohr Bueso 1381be83bbf8SLinus Torvalds static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) 1382be83bbf8SLinus Torvalds { 1383be83bbf8SLinus Torvalds if (S_ISREG(inode->i_mode)) 1384423913adSLinus Torvalds return MAX_LFS_FILESIZE; 1385be83bbf8SLinus Torvalds 1386be83bbf8SLinus Torvalds if (S_ISBLK(inode->i_mode)) 1387be83bbf8SLinus Torvalds return MAX_LFS_FILESIZE; 1388be83bbf8SLinus Torvalds 138976f34950SIvan Khoronzhuk if (S_ISSOCK(inode->i_mode)) 139076f34950SIvan Khoronzhuk return MAX_LFS_FILESIZE; 139176f34950SIvan Khoronzhuk 1392be83bbf8SLinus Torvalds /* Special "we do even unsigned file positions" case */ 1393be83bbf8SLinus Torvalds if (file->f_mode & FMODE_UNSIGNED_OFFSET) 1394be83bbf8SLinus Torvalds return 0; 1395be83bbf8SLinus Torvalds 1396be83bbf8SLinus Torvalds /* Yes, random drivers might want more. But I'm tired of buggy drivers */ 1397be83bbf8SLinus Torvalds return ULONG_MAX; 1398be83bbf8SLinus Torvalds } 1399be83bbf8SLinus Torvalds 1400be83bbf8SLinus Torvalds static inline bool file_mmap_ok(struct file *file, struct inode *inode, 1401be83bbf8SLinus Torvalds unsigned long pgoff, unsigned long len) 1402be83bbf8SLinus Torvalds { 1403be83bbf8SLinus Torvalds u64 maxsize = file_mmap_size_max(file, inode); 1404be83bbf8SLinus Torvalds 1405be83bbf8SLinus Torvalds if (maxsize && len > maxsize) 1406be83bbf8SLinus Torvalds return false; 1407be83bbf8SLinus Torvalds maxsize -= len; 1408be83bbf8SLinus Torvalds if (pgoff > maxsize >> PAGE_SHIFT) 1409be83bbf8SLinus Torvalds return false; 1410be83bbf8SLinus Torvalds return true; 1411be83bbf8SLinus Torvalds } 1412be83bbf8SLinus Torvalds 141340401530SAl Viro /* 14143e4e28c5SMichel Lespinasse * The caller must write-lock current->mm->mmap_lock. 14151da177e4SLinus Torvalds */ 14161fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, unsigned long addr, 14171da177e4SLinus Torvalds unsigned long len, unsigned long prot, 141845e55300SPeter Collingbourne unsigned long flags, unsigned long pgoff, 141945e55300SPeter Collingbourne unsigned long *populate, struct list_head *uf) 14201da177e4SLinus Torvalds { 14211da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 142245e55300SPeter Collingbourne vm_flags_t vm_flags; 142362b5f7d0SDave Hansen int pkey = 0; 14241da177e4SLinus Torvalds 1425524e00b3SLiam R. Howlett validate_mm(mm); 142641badc15SMichel Lespinasse *populate = 0; 1427bebeb3d6SMichel Lespinasse 1428e37609bbSPiotr Kwapulinski if (!len) 1429e37609bbSPiotr Kwapulinski return -EINVAL; 1430e37609bbSPiotr Kwapulinski 14311da177e4SLinus Torvalds /* 14321da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 14331da177e4SLinus Torvalds * 14341da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 14351da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 14361da177e4SLinus Torvalds */ 14371da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 143890f8572bSEric W. Biederman if (!(file && path_noexec(&file->f_path))) 14391da177e4SLinus Torvalds prot |= PROT_EXEC; 14401da177e4SLinus Torvalds 1441a4ff8e86SMichal Hocko /* force arch specific MAP_FIXED handling in get_unmapped_area */ 1442a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) 1443a4ff8e86SMichal Hocko flags |= MAP_FIXED; 1444a4ff8e86SMichal Hocko 14457cd94146SEric Paris if (!(flags & MAP_FIXED)) 14467cd94146SEric Paris addr = round_hint_to_min(addr); 14477cd94146SEric Paris 14481da177e4SLinus Torvalds /* Careful about overflows.. */ 14491da177e4SLinus Torvalds len = PAGE_ALIGN(len); 14509206de95SAl Viro if (!len) 14511da177e4SLinus Torvalds return -ENOMEM; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds /* offset overflow? */ 14541da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 14551da177e4SLinus Torvalds return -EOVERFLOW; 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds /* Too many mappings? */ 14581da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 14591da177e4SLinus Torvalds return -ENOMEM; 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 14621da177e4SLinus Torvalds * that it represents a valid section of the address space. 14631da177e4SLinus Torvalds */ 14641da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 1465ff68dac6SGaowei Pu if (IS_ERR_VALUE(addr)) 14661da177e4SLinus Torvalds return addr; 14671da177e4SLinus Torvalds 1468a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) { 146935e43c5fSLiam Howlett if (find_vma_intersection(mm, addr, addr + len)) 1470a4ff8e86SMichal Hocko return -EEXIST; 1471a4ff8e86SMichal Hocko } 1472a4ff8e86SMichal Hocko 147362b5f7d0SDave Hansen if (prot == PROT_EXEC) { 147462b5f7d0SDave Hansen pkey = execute_only_pkey(mm); 147562b5f7d0SDave Hansen if (pkey < 0) 147662b5f7d0SDave Hansen pkey = 0; 147762b5f7d0SDave Hansen } 147862b5f7d0SDave Hansen 14791da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 14801da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 14811da177e4SLinus Torvalds * of the memory object, so we don't do any here. 14821da177e4SLinus Torvalds */ 148345e55300SPeter Collingbourne vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | 14841da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 14851da177e4SLinus Torvalds 1486cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 14871da177e4SLinus Torvalds if (!can_do_mlock()) 14881da177e4SLinus Torvalds return -EPERM; 1489ba470de4SRik van Riel 1490363ee17fSDavidlohr Bueso if (mlock_future_check(mm, vm_flags, len)) 14911da177e4SLinus Torvalds return -EAGAIN; 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds if (file) { 1494077bf22bSOleg Nesterov struct inode *inode = file_inode(file); 14951c972597SDan Williams unsigned long flags_mask; 14961c972597SDan Williams 1497be83bbf8SLinus Torvalds if (!file_mmap_ok(file, inode, pgoff, len)) 1498be83bbf8SLinus Torvalds return -EOVERFLOW; 1499be83bbf8SLinus Torvalds 15001c972597SDan Williams flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; 1501077bf22bSOleg Nesterov 15021da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 15031da177e4SLinus Torvalds case MAP_SHARED: 15041c972597SDan Williams /* 15051c972597SDan Williams * Force use of MAP_SHARED_VALIDATE with non-legacy 15061c972597SDan Williams * flags. E.g. MAP_SYNC is dangerous to use with 15071c972597SDan Williams * MAP_SHARED as you don't know which consistency model 15081c972597SDan Williams * you will get. We silently ignore unsupported flags 15091c972597SDan Williams * with MAP_SHARED to preserve backward compatibility. 15101c972597SDan Williams */ 15111c972597SDan Williams flags &= LEGACY_MAP_MASK; 1512e4a9bc58SJoe Perches fallthrough; 15131c972597SDan Williams case MAP_SHARED_VALIDATE: 15141c972597SDan Williams if (flags & ~flags_mask) 15151c972597SDan Williams return -EOPNOTSUPP; 1516dc617f29SDarrick J. Wong if (prot & PROT_WRITE) { 1517dc617f29SDarrick J. Wong if (!(file->f_mode & FMODE_WRITE)) 15181da177e4SLinus Torvalds return -EACCES; 1519dc617f29SDarrick J. Wong if (IS_SWAPFILE(file->f_mapping->host)) 1520dc617f29SDarrick J. Wong return -ETXTBSY; 1521dc617f29SDarrick J. Wong } 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /* 15241da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 15251da177e4SLinus Torvalds * file.. 15261da177e4SLinus Torvalds */ 15271da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 15281da177e4SLinus Torvalds return -EACCES; 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 15311da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 15321da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 1533e4a9bc58SJoe Perches fallthrough; 15341da177e4SLinus Torvalds case MAP_PRIVATE: 15351da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 15361da177e4SLinus Torvalds return -EACCES; 153790f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 153880c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 153980c5606cSLinus Torvalds return -EPERM; 154080c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 154180c5606cSLinus Torvalds } 154280c5606cSLinus Torvalds 154372c2d531SAl Viro if (!file->f_op->mmap) 154480c5606cSLinus Torvalds return -ENODEV; 1545b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1546b2c56e4fSOleg Nesterov return -EINVAL; 15471da177e4SLinus Torvalds break; 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds default: 15501da177e4SLinus Torvalds return -EINVAL; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds } else { 15531da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 15541da177e4SLinus Torvalds case MAP_SHARED: 1555b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1556b2c56e4fSOleg Nesterov return -EINVAL; 1557ce363942STejun Heo /* 1558ce363942STejun Heo * Ignore pgoff. 1559ce363942STejun Heo */ 1560ce363942STejun Heo pgoff = 0; 15611da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 15621da177e4SLinus Torvalds break; 15631da177e4SLinus Torvalds case MAP_PRIVATE: 15641da177e4SLinus Torvalds /* 15651da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 15661da177e4SLinus Torvalds */ 15671da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 15681da177e4SLinus Torvalds break; 15691da177e4SLinus Torvalds default: 15701da177e4SLinus Torvalds return -EINVAL; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 1574c22c0d63SMichel Lespinasse /* 1575c22c0d63SMichel Lespinasse * Set 'VM_NORESERVE' if we should not account for the 1576c22c0d63SMichel Lespinasse * memory use of this mapping. 1577c22c0d63SMichel Lespinasse */ 1578c22c0d63SMichel Lespinasse if (flags & MAP_NORESERVE) { 1579c22c0d63SMichel Lespinasse /* We honor MAP_NORESERVE if allowed to overcommit */ 1580c22c0d63SMichel Lespinasse if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1581c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1582c22c0d63SMichel Lespinasse 1583c22c0d63SMichel Lespinasse /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 1584c22c0d63SMichel Lespinasse if (file && is_file_hugepages(file)) 1585c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1586c22c0d63SMichel Lespinasse } 1587c22c0d63SMichel Lespinasse 1588897ab3e0SMike Rapoport addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); 158909a9f1d2SMichel Lespinasse if (!IS_ERR_VALUE(addr) && 159009a9f1d2SMichel Lespinasse ((vm_flags & VM_LOCKED) || 159109a9f1d2SMichel Lespinasse (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) 159241badc15SMichel Lespinasse *populate = len; 1593bebeb3d6SMichel Lespinasse return addr; 15940165ab44SMiklos Szeredi } 15956be5ceb0SLinus Torvalds 1596a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1597a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1598a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 159966f0dc48SHugh Dickins { 160066f0dc48SHugh Dickins struct file *file = NULL; 16011e3ee14bSChen Gang unsigned long retval; 160266f0dc48SHugh Dickins 160366f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1604120a795dSAl Viro audit_mmap_fd(fd, flags); 160566f0dc48SHugh Dickins file = fget(fd); 160666f0dc48SHugh Dickins if (!file) 16071e3ee14bSChen Gang return -EBADF; 16087bba8f0eSZhen Lei if (is_file_hugepages(file)) { 1609af73e4d9SNaoya Horiguchi len = ALIGN(len, huge_page_size(hstate_file(file))); 16107bba8f0eSZhen Lei } else if (unlikely(flags & MAP_HUGETLB)) { 1611493af578SJörn Engel retval = -EINVAL; 1612493af578SJörn Engel goto out_fput; 16137bba8f0eSZhen Lei } 161466f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 1615c103a4dcSAndrew Morton struct hstate *hs; 1616af73e4d9SNaoya Horiguchi 161720ac2893SAnshuman Khandual hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 1618091d0d55SLi Zefan if (!hs) 1619091d0d55SLi Zefan return -EINVAL; 1620091d0d55SLi Zefan 1621091d0d55SLi Zefan len = ALIGN(len, huge_page_size(hs)); 162266f0dc48SHugh Dickins /* 162366f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 162466f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 162566f0dc48SHugh Dickins */ 1626af73e4d9SNaoya Horiguchi file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, 162742d7395fSAndi Kleen VM_NORESERVE, 162883c1fd76Szhangyiru HUGETLB_ANONHUGE_INODE, 162942d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 163066f0dc48SHugh Dickins if (IS_ERR(file)) 163166f0dc48SHugh Dickins return PTR_ERR(file); 163266f0dc48SHugh Dickins } 163366f0dc48SHugh Dickins 16349fbeb5abSMichal Hocko retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1635493af578SJörn Engel out_fput: 163666f0dc48SHugh Dickins if (file) 163766f0dc48SHugh Dickins fput(file); 163866f0dc48SHugh Dickins return retval; 163966f0dc48SHugh Dickins } 164066f0dc48SHugh Dickins 1641a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1642a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1643a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1644a90f590aSDominik Brodowski { 1645a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1646a90f590aSDominik Brodowski } 1647a90f590aSDominik Brodowski 1648a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1649a4679373SChristoph Hellwig struct mmap_arg_struct { 1650a4679373SChristoph Hellwig unsigned long addr; 1651a4679373SChristoph Hellwig unsigned long len; 1652a4679373SChristoph Hellwig unsigned long prot; 1653a4679373SChristoph Hellwig unsigned long flags; 1654a4679373SChristoph Hellwig unsigned long fd; 1655a4679373SChristoph Hellwig unsigned long offset; 1656a4679373SChristoph Hellwig }; 1657a4679373SChristoph Hellwig 1658a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1659a4679373SChristoph Hellwig { 1660a4679373SChristoph Hellwig struct mmap_arg_struct a; 1661a4679373SChristoph Hellwig 1662a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1663a4679373SChristoph Hellwig return -EFAULT; 1664de1741a1SAlexander Kuleshov if (offset_in_page(a.offset)) 1665a4679373SChristoph Hellwig return -EINVAL; 1666a4679373SChristoph Hellwig 1667a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1668a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1669a4679373SChristoph Hellwig } 1670a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1671a4679373SChristoph Hellwig 16724e950f6fSAlexey Dobriyan /* 16738bb4e7a2SWei Yang * Some shared mappings will want the pages marked read-only 16744e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 16754e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 16764e950f6fSAlexey Dobriyan * VM_SHARED bit). 16774e950f6fSAlexey Dobriyan */ 16786d2329f8SAndrea Arcangeli int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 16794e950f6fSAlexey Dobriyan { 1680ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 16818a04446aSKirill A. Shutemov const struct vm_operations_struct *vm_ops = vma->vm_ops; 16824e950f6fSAlexey Dobriyan 16834e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 16844e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 16854e950f6fSAlexey Dobriyan return 0; 16864e950f6fSAlexey Dobriyan 16874e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 16888a04446aSKirill A. Shutemov if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) 16894e950f6fSAlexey Dobriyan return 1; 16904e950f6fSAlexey Dobriyan 169164e45507SPeter Feiner /* The open routine did something to the protections that pgprot_modify 169264e45507SPeter Feiner * won't preserve? */ 16936d2329f8SAndrea Arcangeli if (pgprot_val(vm_page_prot) != 16946d2329f8SAndrea Arcangeli pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) 16954e950f6fSAlexey Dobriyan return 0; 16964e950f6fSAlexey Dobriyan 1697f96f7a40SDavid Hildenbrand /* 1698f96f7a40SDavid Hildenbrand * Do we need to track softdirty? hugetlb does not support softdirty 1699f96f7a40SDavid Hildenbrand * tracking yet. 1700f96f7a40SDavid Hildenbrand */ 1701f96f7a40SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 170264e45507SPeter Feiner return 1; 170364e45507SPeter Feiner 17044e950f6fSAlexey Dobriyan /* Specialty mapping? */ 17054b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 17064e950f6fSAlexey Dobriyan return 0; 17074e950f6fSAlexey Dobriyan 17084e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 17094e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 1710f56753acSChristoph Hellwig mapping_can_writeback(vma->vm_file->f_mapping); 17114e950f6fSAlexey Dobriyan } 17124e950f6fSAlexey Dobriyan 1713fc8744adSLinus Torvalds /* 1714fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 17155a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1716fc8744adSLinus Torvalds */ 1717ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1718fc8744adSLinus Torvalds { 17195a6fe125SMel Gorman /* 17205a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 17215a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 17225a6fe125SMel Gorman */ 17235a6fe125SMel Gorman if (file && is_file_hugepages(file)) 17245a6fe125SMel Gorman return 0; 17255a6fe125SMel Gorman 1726fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1727fc8744adSLinus Torvalds } 1728fc8744adSLinus Torvalds 17290165ab44SMiklos Szeredi unsigned long mmap_region(struct file *file, unsigned long addr, 1730897ab3e0SMike Rapoport unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, 1731897ab3e0SMike Rapoport struct list_head *uf) 17320165ab44SMiklos Szeredi { 17330165ab44SMiklos Szeredi struct mm_struct *mm = current->mm; 1734*4dd1b841SLiam R. Howlett struct vm_area_struct *vma = NULL; 1735*4dd1b841SLiam R. Howlett struct vm_area_struct *next, *prev, *merge; 1736*4dd1b841SLiam R. Howlett pgoff_t pglen = len >> PAGE_SHIFT; 17370165ab44SMiklos Szeredi unsigned long charged = 0; 1738*4dd1b841SLiam R. Howlett unsigned long end = addr + len; 1739*4dd1b841SLiam R. Howlett unsigned long merge_start = addr, merge_end = end; 1740*4dd1b841SLiam R. Howlett pgoff_t vm_pgoff; 1741*4dd1b841SLiam R. Howlett int error; 1742*4dd1b841SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, end - 1); 17430165ab44SMiklos Szeredi 1744e8420a8eSCyril Hrubis /* Check against address space limit. */ 174584638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { 1746e8420a8eSCyril Hrubis unsigned long nr_pages; 1747e8420a8eSCyril Hrubis 1748e8420a8eSCyril Hrubis /* 1749e8420a8eSCyril Hrubis * MAP_FIXED may remove pages of mappings that intersects with 1750e8420a8eSCyril Hrubis * requested mapping. Account for the pages it would unmap. 1751e8420a8eSCyril Hrubis */ 1752*4dd1b841SLiam R. Howlett nr_pages = count_vma_pages_range(mm, addr, end); 1753e8420a8eSCyril Hrubis 175484638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vm_flags, 175584638335SKonstantin Khlebnikov (len >> PAGE_SHIFT) - nr_pages)) 1756e8420a8eSCyril Hrubis return -ENOMEM; 1757e8420a8eSCyril Hrubis } 1758e8420a8eSCyril Hrubis 1759*4dd1b841SLiam R. Howlett /* Unmap any existing mapping in the area */ 1760*4dd1b841SLiam R. Howlett if (do_munmap(mm, addr, len, uf)) 17611da177e4SLinus Torvalds return -ENOMEM; 1762*4dd1b841SLiam R. Howlett 1763fc8744adSLinus Torvalds /* 17641da177e4SLinus Torvalds * Private writable mapping: check memory availability 17651da177e4SLinus Torvalds */ 17665a6fe125SMel Gorman if (accountable_mapping(file, vm_flags)) { 17671da177e4SLinus Torvalds charged = len >> PAGE_SHIFT; 1768191c5424SAl Viro if (security_vm_enough_memory_mm(mm, charged)) 17691da177e4SLinus Torvalds return -ENOMEM; 17701da177e4SLinus Torvalds vm_flags |= VM_ACCOUNT; 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 1773*4dd1b841SLiam R. Howlett next = mas_next(&mas, ULONG_MAX); 1774*4dd1b841SLiam R. Howlett prev = mas_prev(&mas, 0); 1775*4dd1b841SLiam R. Howlett if (vm_flags & VM_SPECIAL) 1776*4dd1b841SLiam R. Howlett goto cannot_expand; 17771da177e4SLinus Torvalds 1778*4dd1b841SLiam R. Howlett /* Attempt to expand an old mapping */ 1779*4dd1b841SLiam R. Howlett /* Check next */ 1780*4dd1b841SLiam R. Howlett if (next && next->vm_start == end && !vma_policy(next) && 1781*4dd1b841SLiam R. Howlett can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, 1782*4dd1b841SLiam R. Howlett NULL_VM_UFFD_CTX, NULL)) { 1783*4dd1b841SLiam R. Howlett merge_end = next->vm_end; 1784*4dd1b841SLiam R. Howlett vma = next; 1785*4dd1b841SLiam R. Howlett vm_pgoff = next->vm_pgoff - pglen; 1786*4dd1b841SLiam R. Howlett } 1787*4dd1b841SLiam R. Howlett 1788*4dd1b841SLiam R. Howlett /* Check prev */ 1789*4dd1b841SLiam R. Howlett if (prev && prev->vm_end == addr && !vma_policy(prev) && 1790*4dd1b841SLiam R. Howlett (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, 1791*4dd1b841SLiam R. Howlett pgoff, vma->vm_userfaultfd_ctx, NULL) : 1792*4dd1b841SLiam R. Howlett can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, 1793*4dd1b841SLiam R. Howlett NULL_VM_UFFD_CTX, NULL))) { 1794*4dd1b841SLiam R. Howlett merge_start = prev->vm_start; 1795*4dd1b841SLiam R. Howlett vma = prev; 1796*4dd1b841SLiam R. Howlett vm_pgoff = prev->vm_pgoff; 1797*4dd1b841SLiam R. Howlett } 1798*4dd1b841SLiam R. Howlett 1799*4dd1b841SLiam R. Howlett 1800*4dd1b841SLiam R. Howlett /* Actually expand, if possible */ 1801*4dd1b841SLiam R. Howlett if (vma && 1802*4dd1b841SLiam R. Howlett !vma_expand(&mas, vma, merge_start, merge_end, vm_pgoff, next)) { 1803*4dd1b841SLiam R. Howlett khugepaged_enter_vma(vma, vm_flags); 1804*4dd1b841SLiam R. Howlett goto expanded; 1805*4dd1b841SLiam R. Howlett } 1806*4dd1b841SLiam R. Howlett 1807*4dd1b841SLiam R. Howlett mas.index = addr; 1808*4dd1b841SLiam R. Howlett mas.last = end - 1; 1809*4dd1b841SLiam R. Howlett cannot_expand: 18101da177e4SLinus Torvalds /* 18111da177e4SLinus Torvalds * Determine the object being mapped and call the appropriate 18121da177e4SLinus Torvalds * specific mapper. the address has already been validated, but 18131da177e4SLinus Torvalds * not unmapped, but the maps are removed from the list. 18141da177e4SLinus Torvalds */ 1815490fc053SLinus Torvalds vma = vm_area_alloc(mm); 18161da177e4SLinus Torvalds if (!vma) { 18171da177e4SLinus Torvalds error = -ENOMEM; 18181da177e4SLinus Torvalds goto unacct_error; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds vma->vm_start = addr; 1822*4dd1b841SLiam R. Howlett vma->vm_end = end; 18231da177e4SLinus Torvalds vma->vm_flags = vm_flags; 18243ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vm_flags); 18251da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds if (file) { 18284bb5f5d9SDavid Herrmann if (vm_flags & VM_SHARED) { 18294bb5f5d9SDavid Herrmann error = mapping_map_writable(file->f_mapping); 18304bb5f5d9SDavid Herrmann if (error) 18318d0920bdSDavid Hildenbrand goto free_vma; 18324bb5f5d9SDavid Herrmann } 18334bb5f5d9SDavid Herrmann 1834cb0942b8SAl Viro vma->vm_file = get_file(file); 1835f74ac015SMiklos Szeredi error = call_mmap(file, vma); 18361da177e4SLinus Torvalds if (error) 18371da177e4SLinus Torvalds goto unmap_and_free_vma; 18381da177e4SLinus Torvalds 1839309d08d9SLiu Zixian /* Can addr have changed?? 1840309d08d9SLiu Zixian * 1841309d08d9SLiu Zixian * Answer: Yes, several device drivers can do it in their 1842309d08d9SLiu Zixian * f_op->mmap method. -DaveM 1843309d08d9SLiu Zixian */ 1844309d08d9SLiu Zixian WARN_ON_ONCE(addr != vma->vm_start); 1845309d08d9SLiu Zixian 1846309d08d9SLiu Zixian addr = vma->vm_start; 1847*4dd1b841SLiam R. Howlett mas_reset(&mas); 1848309d08d9SLiu Zixian 1849*4dd1b841SLiam R. Howlett /* 1850*4dd1b841SLiam R. Howlett * If vm_flags changed after call_mmap(), we should try merge 1851*4dd1b841SLiam R. Howlett * vma again as we may succeed this time. 1852d70cec89SMiaohe Lin */ 1853d70cec89SMiaohe Lin if (unlikely(vm_flags != vma->vm_flags && prev)) { 1854d70cec89SMiaohe Lin merge = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags, 18559a10064fSColin Cross NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL); 1856d70cec89SMiaohe Lin if (merge) { 1857*4dd1b841SLiam R. Howlett /* 1858*4dd1b841SLiam R. Howlett * ->mmap() can change vma->vm_file and fput 1859*4dd1b841SLiam R. Howlett * the original file. So fput the vma->vm_file 1860*4dd1b841SLiam R. Howlett * here or we would add an extra fput for file 1861*4dd1b841SLiam R. Howlett * and cause general protection fault 1862*4dd1b841SLiam R. Howlett * ultimately. 1863bc4fe4cdSMiaohe Lin */ 1864bc4fe4cdSMiaohe Lin fput(vma->vm_file); 1865d70cec89SMiaohe Lin vm_area_free(vma); 1866d70cec89SMiaohe Lin vma = merge; 1867309d08d9SLiu Zixian /* Update vm_flags to pick up the change. */ 1868*4dd1b841SLiam R. Howlett addr = vma->vm_start; 1869d70cec89SMiaohe Lin vm_flags = vma->vm_flags; 1870d70cec89SMiaohe Lin goto unmap_writable; 1871d70cec89SMiaohe Lin } 1872d70cec89SMiaohe Lin } 1873d70cec89SMiaohe Lin 18741da177e4SLinus Torvalds vm_flags = vma->vm_flags; 1875f8dbf0a7SHuang Shijie } else if (vm_flags & VM_SHARED) { 1876f8dbf0a7SHuang Shijie error = shmem_zero_setup(vma); 1877f8dbf0a7SHuang Shijie if (error) 1878f8dbf0a7SHuang Shijie goto free_vma; 1879bfd40eafSKirill A. Shutemov } else { 1880bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 1881f8dbf0a7SHuang Shijie } 18821da177e4SLinus Torvalds 1883c462ac28SCatalin Marinas /* Allow architectures to sanity-check the vm_flags */ 1884c462ac28SCatalin Marinas if (!arch_validate_flags(vma->vm_flags)) { 1885c462ac28SCatalin Marinas error = -EINVAL; 1886c462ac28SCatalin Marinas if (file) 1887c462ac28SCatalin Marinas goto unmap_and_free_vma; 1888c462ac28SCatalin Marinas else 1889c462ac28SCatalin Marinas goto free_vma; 1890c462ac28SCatalin Marinas } 1891c462ac28SCatalin Marinas 1892*4dd1b841SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) { 1893d4af56c5SLiam R. Howlett error = -ENOMEM; 1894d4af56c5SLiam R. Howlett if (file) 1895d4af56c5SLiam R. Howlett goto unmap_and_free_vma; 1896d4af56c5SLiam R. Howlett else 1897d4af56c5SLiam R. Howlett goto free_vma; 1898d4af56c5SLiam R. Howlett } 1899613bec09SYang Shi 1900*4dd1b841SLiam R. Howlett if (vma->vm_file) 1901*4dd1b841SLiam R. Howlett i_mmap_lock_write(vma->vm_file->f_mapping); 1902*4dd1b841SLiam R. Howlett 1903*4dd1b841SLiam R. Howlett vma_mas_store(vma, &mas); 1904*4dd1b841SLiam R. Howlett __vma_link_list(mm, vma, prev); 1905*4dd1b841SLiam R. Howlett mm->map_count++; 1906*4dd1b841SLiam R. Howlett if (vma->vm_file) { 1907*4dd1b841SLiam R. Howlett if (vma->vm_flags & VM_SHARED) 1908*4dd1b841SLiam R. Howlett mapping_allow_writable(vma->vm_file->f_mapping); 1909*4dd1b841SLiam R. Howlett 1910*4dd1b841SLiam R. Howlett flush_dcache_mmap_lock(vma->vm_file->f_mapping); 1911*4dd1b841SLiam R. Howlett vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); 1912*4dd1b841SLiam R. Howlett flush_dcache_mmap_unlock(vma->vm_file->f_mapping); 1913*4dd1b841SLiam R. Howlett i_mmap_unlock_write(vma->vm_file->f_mapping); 1914*4dd1b841SLiam R. Howlett } 1915*4dd1b841SLiam R. Howlett 1916613bec09SYang Shi /* 1917613bec09SYang Shi * vma_merge() calls khugepaged_enter_vma() either, the below 1918613bec09SYang Shi * call covers the non-merge case. 1919613bec09SYang Shi */ 1920613bec09SYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 1921613bec09SYang Shi 19224d3d5b41SOleg Nesterov /* Once vma denies write, undo our temporary denial count */ 1923d70cec89SMiaohe Lin unmap_writable: 19248d0920bdSDavid Hildenbrand if (file && vm_flags & VM_SHARED) 19254bb5f5d9SDavid Herrmann mapping_unmap_writable(file->f_mapping); 1926e8686772SOleg Nesterov file = vma->vm_file; 1927*4dd1b841SLiam R. Howlett expanded: 1928cdd6c482SIngo Molnar perf_event_mmap(vma); 19290a4a9391SPeter Zijlstra 193084638335SKonstantin Khlebnikov vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); 19311da177e4SLinus Torvalds if (vm_flags & VM_LOCKED) { 1932e1fb4a08SDave Jiang if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || 1933e1fb4a08SDave Jiang is_vm_hugetlb_page(vma) || 1934e1fb4a08SDave Jiang vma == get_gate_vma(current->mm)) 1935de60f5f1SEric B Munson vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 1936e1fb4a08SDave Jiang else 1937e1fb4a08SDave Jiang mm->locked_vm += (len >> PAGE_SHIFT); 1938bebeb3d6SMichel Lespinasse } 19392b144498SSrikar Dronamraju 1940c7a3a88cSOleg Nesterov if (file) 1941c7a3a88cSOleg Nesterov uprobe_mmap(vma); 19422b144498SSrikar Dronamraju 1943d9104d1cSCyrill Gorcunov /* 1944d9104d1cSCyrill Gorcunov * New (or expanded) vma always get soft dirty status. 1945d9104d1cSCyrill Gorcunov * Otherwise user-space soft-dirty page tracker won't 1946d9104d1cSCyrill Gorcunov * be able to distinguish situation when vma area unmapped, 1947d9104d1cSCyrill Gorcunov * then new mapped in-place (which must be aimed as 1948d9104d1cSCyrill Gorcunov * a completely new data area). 1949d9104d1cSCyrill Gorcunov */ 1950d9104d1cSCyrill Gorcunov vma->vm_flags |= VM_SOFTDIRTY; 1951d9104d1cSCyrill Gorcunov 195264e45507SPeter Feiner vma_set_page_prot(vma); 195364e45507SPeter Feiner 1954*4dd1b841SLiam R. Howlett validate_mm(mm); 19551da177e4SLinus Torvalds return addr; 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds unmap_and_free_vma: 19581527f926SChristian König fput(vma->vm_file); 19591da177e4SLinus Torvalds vma->vm_file = NULL; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds /* Undo any partial mapping done by a device driver. */ 1962e0da382cSHugh Dickins unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); 19634bb5f5d9SDavid Herrmann if (vm_flags & VM_SHARED) 19644bb5f5d9SDavid Herrmann mapping_unmap_writable(file->f_mapping); 19651da177e4SLinus Torvalds free_vma: 19663928d4f5SLinus Torvalds vm_area_free(vma); 19671da177e4SLinus Torvalds unacct_error: 19681da177e4SLinus Torvalds if (charged) 19691da177e4SLinus Torvalds vm_unacct_memory(charged); 1970*4dd1b841SLiam R. Howlett validate_mm(mm); 19711da177e4SLinus Torvalds return error; 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 19743499a131SLiam R. Howlett /** 19753499a131SLiam R. Howlett * unmapped_area() - Find an area between the low_limit and the high_limit with 19763499a131SLiam R. Howlett * the correct alignment and offset, all from @info. Note: current->mm is used 19773499a131SLiam R. Howlett * for the search. 19783499a131SLiam R. Howlett * 19793499a131SLiam R. Howlett * @info: The unmapped area information including the range (low_limit - 19803499a131SLiam R. Howlett * hight_limit), the alignment offset and mask. 19813499a131SLiam R. Howlett * 19823499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 19833499a131SLiam R. Howlett */ 1984baceaf1cSJaewon Kim static unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1985db4fbfb9SMichel Lespinasse { 19863499a131SLiam R. Howlett unsigned long length, gap; 1987db4fbfb9SMichel Lespinasse 19883499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1989db4fbfb9SMichel Lespinasse 1990db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1991db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1992db4fbfb9SMichel Lespinasse if (length < info->length) 1993db4fbfb9SMichel Lespinasse return -ENOMEM; 1994db4fbfb9SMichel Lespinasse 19953499a131SLiam R. Howlett if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, 19963499a131SLiam R. Howlett length)) 19973499a131SLiam R. Howlett return -ENOMEM; 19983499a131SLiam R. Howlett 1999d4af56c5SLiam R. Howlett gap = mas.index; 2000d4af56c5SLiam R. Howlett gap += (info->align_offset - gap) & info->align_mask; 20013499a131SLiam R. Howlett return gap; 2002db4fbfb9SMichel Lespinasse } 2003db4fbfb9SMichel Lespinasse 20043499a131SLiam R. Howlett /** 20053499a131SLiam R. Howlett * unmapped_area_topdown() - Find an area between the low_limit and the 20063499a131SLiam R. Howlett * high_limit with * the correct alignment and offset at the highest available 20073499a131SLiam R. Howlett * address, all from @info. Note: current->mm is used for the search. 20083499a131SLiam R. Howlett * 20093499a131SLiam R. Howlett * @info: The unmapped area information including the range (low_limit - 20103499a131SLiam R. Howlett * hight_limit), the alignment offset and mask. 20113499a131SLiam R. Howlett * 20123499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 20133499a131SLiam R. Howlett */ 2014baceaf1cSJaewon Kim static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 2015db4fbfb9SMichel Lespinasse { 20163499a131SLiam R. Howlett unsigned long length, gap; 2017d4af56c5SLiam R. Howlett 20183499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 2019db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 2020db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 2021db4fbfb9SMichel Lespinasse if (length < info->length) 2022db4fbfb9SMichel Lespinasse return -ENOMEM; 2023db4fbfb9SMichel Lespinasse 20243499a131SLiam R. Howlett if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, 20253499a131SLiam R. Howlett length)) 20263499a131SLiam R. Howlett return -ENOMEM; 20273499a131SLiam R. Howlett 2028d4af56c5SLiam R. Howlett gap = mas.last + 1 - info->length; 2029d4af56c5SLiam R. Howlett gap -= (gap - info->align_offset) & info->align_mask; 20303499a131SLiam R. Howlett return gap; 2031db4fbfb9SMichel Lespinasse } 2032db4fbfb9SMichel Lespinasse 2033baceaf1cSJaewon Kim /* 2034baceaf1cSJaewon Kim * Search for an unmapped address range. 2035baceaf1cSJaewon Kim * 2036baceaf1cSJaewon Kim * We are looking for a range that: 2037baceaf1cSJaewon Kim * - does not intersect with any VMA; 2038baceaf1cSJaewon Kim * - is contained within the [low_limit, high_limit) interval; 2039baceaf1cSJaewon Kim * - is at least the desired size. 2040baceaf1cSJaewon Kim * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) 2041baceaf1cSJaewon Kim */ 2042baceaf1cSJaewon Kim unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) 2043baceaf1cSJaewon Kim { 2044df529cabSJaewon Kim unsigned long addr; 2045df529cabSJaewon Kim 2046baceaf1cSJaewon Kim if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) 2047df529cabSJaewon Kim addr = unmapped_area_topdown(info); 2048baceaf1cSJaewon Kim else 2049df529cabSJaewon Kim addr = unmapped_area(info); 2050df529cabSJaewon Kim 2051df529cabSJaewon Kim trace_vm_unmapped_area(addr, info); 2052df529cabSJaewon Kim return addr; 2053baceaf1cSJaewon Kim } 2054f6795053SSteve Capper 20551da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 20561da177e4SLinus Torvalds * For shmat() with addr=0. 20571da177e4SLinus Torvalds * 20581da177e4SLinus Torvalds * Ugly calling convention alert: 20591da177e4SLinus Torvalds * Return value with the low bits set means error value, 20601da177e4SLinus Torvalds * ie 20611da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 20621da177e4SLinus Torvalds * error = ret; 20631da177e4SLinus Torvalds * 20641da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 20651da177e4SLinus Torvalds */ 20661da177e4SLinus Torvalds unsigned long 20674b439e25SChristophe Leroy generic_get_unmapped_area(struct file *filp, unsigned long addr, 20684b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 20694b439e25SChristophe Leroy unsigned long flags) 20701da177e4SLinus Torvalds { 20711da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 20721be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 2073db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 20742cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 20751da177e4SLinus Torvalds 2076f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 20771da177e4SLinus Torvalds return -ENOMEM; 20781da177e4SLinus Torvalds 207906abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 208006abdfb4SBenjamin Herrenschmidt return addr; 208106abdfb4SBenjamin Herrenschmidt 20821da177e4SLinus Torvalds if (addr) { 20831da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 20841be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 2085f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 20861be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 20871be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 20881da177e4SLinus Torvalds return addr; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 2091db4fbfb9SMichel Lespinasse info.flags = 0; 2092db4fbfb9SMichel Lespinasse info.length = len; 20934e99b021SHeiko Carstens info.low_limit = mm->mmap_base; 2094f6795053SSteve Capper info.high_limit = mmap_end; 2095db4fbfb9SMichel Lespinasse info.align_mask = 0; 209609ef5283SJaewon Kim info.align_offset = 0; 2097db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 20981da177e4SLinus Torvalds } 20994b439e25SChristophe Leroy 21004b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA 21014b439e25SChristophe Leroy unsigned long 21024b439e25SChristophe Leroy arch_get_unmapped_area(struct file *filp, unsigned long addr, 21034b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 21044b439e25SChristophe Leroy unsigned long flags) 21054b439e25SChristophe Leroy { 21064b439e25SChristophe Leroy return generic_get_unmapped_area(filp, addr, len, pgoff, flags); 21074b439e25SChristophe Leroy } 21081da177e4SLinus Torvalds #endif 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds /* 21111da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 21121da177e4SLinus Torvalds * stack's low limit (the base): 21131da177e4SLinus Torvalds */ 21141da177e4SLinus Torvalds unsigned long 21154b439e25SChristophe Leroy generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 211643cca0b1SYang Fan unsigned long len, unsigned long pgoff, 211743cca0b1SYang Fan unsigned long flags) 21181da177e4SLinus Torvalds { 21191be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 21201da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 2121db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 21222cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds /* requested length too big for entire address space */ 2125f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 21261da177e4SLinus Torvalds return -ENOMEM; 21271da177e4SLinus Torvalds 212806abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 212906abdfb4SBenjamin Herrenschmidt return addr; 213006abdfb4SBenjamin Herrenschmidt 21311da177e4SLinus Torvalds /* requesting a specific address */ 21321da177e4SLinus Torvalds if (addr) { 21331da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 21341be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 2135f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 21361be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 21371be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 21381da177e4SLinus Torvalds return addr; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 2141db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 2142db4fbfb9SMichel Lespinasse info.length = len; 21432afc745fSAkira Takeuchi info.low_limit = max(PAGE_SIZE, mmap_min_addr); 2144f6795053SSteve Capper info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); 2145db4fbfb9SMichel Lespinasse info.align_mask = 0; 214609ef5283SJaewon Kim info.align_offset = 0; 2147db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 2148b716ad95SXiao Guangrong 21491da177e4SLinus Torvalds /* 21501da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 21511da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 21521da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 21531da177e4SLinus Torvalds * allocations. 21541da177e4SLinus Torvalds */ 2155de1741a1SAlexander Kuleshov if (offset_in_page(addr)) { 2156db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 2157db4fbfb9SMichel Lespinasse info.flags = 0; 2158db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 2159f6795053SSteve Capper info.high_limit = mmap_end; 2160db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 2161db4fbfb9SMichel Lespinasse } 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds return addr; 21641da177e4SLinus Torvalds } 21654b439e25SChristophe Leroy 21664b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 21674b439e25SChristophe Leroy unsigned long 21684b439e25SChristophe Leroy arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 21694b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 21704b439e25SChristophe Leroy unsigned long flags) 21714b439e25SChristophe Leroy { 21724b439e25SChristophe Leroy return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); 21734b439e25SChristophe Leroy } 21741da177e4SLinus Torvalds #endif 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds unsigned long 21771da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 21781da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 21791da177e4SLinus Torvalds { 218006abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 218106abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 218207ab67c8SLinus Torvalds 21839206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 21849206de95SAl Viro if (error) 21859206de95SAl Viro return error; 21869206de95SAl Viro 21879206de95SAl Viro /* Careful about overflows.. */ 21889206de95SAl Viro if (len > TASK_SIZE) 21899206de95SAl Viro return -ENOMEM; 21909206de95SAl Viro 219107ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 2192c01d5b30SHugh Dickins if (file) { 2193c01d5b30SHugh Dickins if (file->f_op->get_unmapped_area) 219407ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 2195c01d5b30SHugh Dickins } else if (flags & MAP_SHARED) { 2196c01d5b30SHugh Dickins /* 2197c01d5b30SHugh Dickins * mmap_region() will call shmem_zero_setup() to create a file, 2198c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge. 219945e55300SPeter Collingbourne * do_mmap() will clear pgoff, so match alignment. 2200c01d5b30SHugh Dickins */ 2201c01d5b30SHugh Dickins pgoff = 0; 2202c01d5b30SHugh Dickins get_area = shmem_get_unmapped_area; 2203f35b5d7dSRik van Riel } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 2204f35b5d7dSRik van Riel /* Ensures that larger anonymous mappings are THP aligned. */ 2205f35b5d7dSRik van Riel get_area = thp_get_unmapped_area; 2206c01d5b30SHugh Dickins } 2207c01d5b30SHugh Dickins 220807ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 220907ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 221007ab67c8SLinus Torvalds return addr; 221107ab67c8SLinus Torvalds 22121da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 22131da177e4SLinus Torvalds return -ENOMEM; 2214de1741a1SAlexander Kuleshov if (offset_in_page(addr)) 22151da177e4SLinus Torvalds return -EINVAL; 221606abdfb4SBenjamin Herrenschmidt 22179ac4ed4bSAl Viro error = security_mmap_addr(addr); 22189ac4ed4bSAl Viro return error ? error : addr; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 22221da177e4SLinus Torvalds 2223be8432e7SLiam R. Howlett /** 2224abdba2ddSLiam R. Howlett * find_vma_intersection() - Look up the first VMA which intersects the interval 2225abdba2ddSLiam R. Howlett * @mm: The process address space. 2226abdba2ddSLiam R. Howlett * @start_addr: The inclusive start user address. 2227abdba2ddSLiam R. Howlett * @end_addr: The exclusive end user address. 2228abdba2ddSLiam R. Howlett * 2229abdba2ddSLiam R. Howlett * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 2230abdba2ddSLiam R. Howlett * start_addr < end_addr. 2231abdba2ddSLiam R. Howlett */ 2232abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 2233abdba2ddSLiam R. Howlett unsigned long start_addr, 2234abdba2ddSLiam R. Howlett unsigned long end_addr) 2235abdba2ddSLiam R. Howlett { 2236abdba2ddSLiam R. Howlett struct vm_area_struct *vma; 2237abdba2ddSLiam R. Howlett unsigned long index = start_addr; 2238abdba2ddSLiam R. Howlett 2239abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 2240abdba2ddSLiam R. Howlett /* Check the cache first. */ 2241abdba2ddSLiam R. Howlett vma = vmacache_find(mm, start_addr); 2242abdba2ddSLiam R. Howlett if (likely(vma)) 2243abdba2ddSLiam R. Howlett return vma; 2244abdba2ddSLiam R. Howlett 2245abdba2ddSLiam R. Howlett vma = mt_find(&mm->mm_mt, &index, end_addr - 1); 2246abdba2ddSLiam R. Howlett if (vma) 2247abdba2ddSLiam R. Howlett vmacache_update(start_addr, vma); 2248abdba2ddSLiam R. Howlett return vma; 2249abdba2ddSLiam R. Howlett } 2250abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 2251abdba2ddSLiam R. Howlett 2252abdba2ddSLiam R. Howlett /** 2253be8432e7SLiam R. Howlett * find_vma() - Find the VMA for a given address, or the next VMA. 2254be8432e7SLiam R. Howlett * @mm: The mm_struct to check 2255be8432e7SLiam R. Howlett * @addr: The address 2256be8432e7SLiam R. Howlett * 2257be8432e7SLiam R. Howlett * Returns: The VMA associated with addr, or the next VMA. 2258be8432e7SLiam R. Howlett * May return %NULL in the case of no VMA at addr or above. 2259be8432e7SLiam R. Howlett */ 22601da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 22611da177e4SLinus Torvalds { 2262615d6e87SDavidlohr Bueso struct vm_area_struct *vma; 2263be8432e7SLiam R. Howlett unsigned long index = addr; 22641da177e4SLinus Torvalds 22655b78ed24SLuigi Rizzo mmap_assert_locked(mm); 22661da177e4SLinus Torvalds /* Check the cache first. */ 2267615d6e87SDavidlohr Bueso vma = vmacache_find(mm, addr); 2268615d6e87SDavidlohr Bueso if (likely(vma)) 2269615d6e87SDavidlohr Bueso return vma; 22701da177e4SLinus Torvalds 2271be8432e7SLiam R. Howlett vma = mt_find(&mm->mm_mt, &index, ULONG_MAX); 22721da177e4SLinus Torvalds if (vma) 2273615d6e87SDavidlohr Bueso vmacache_update(addr, vma); 22741da177e4SLinus Torvalds return vma; 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 22771da177e4SLinus Torvalds 22787fdbd37dSLiam R. Howlett /** 22797fdbd37dSLiam R. Howlett * find_vma_prev() - Find the VMA for a given address, or the next vma and 22807fdbd37dSLiam R. Howlett * set %pprev to the previous VMA, if any. 22817fdbd37dSLiam R. Howlett * @mm: The mm_struct to check 22827fdbd37dSLiam R. Howlett * @addr: The address 22837fdbd37dSLiam R. Howlett * @pprev: The pointer to set to the previous VMA 22847fdbd37dSLiam R. Howlett * 22857fdbd37dSLiam R. Howlett * Note that RCU lock is missing here since the external mmap_lock() is used 22867fdbd37dSLiam R. Howlett * instead. 22877fdbd37dSLiam R. Howlett * 22887fdbd37dSLiam R. Howlett * Returns: The VMA associated with @addr, or the next vma. 22897fdbd37dSLiam R. Howlett * May return %NULL in the case of no vma at addr or above. 22906bd4837dSKOSAKI Motohiro */ 22911da177e4SLinus Torvalds struct vm_area_struct * 22921da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 22931da177e4SLinus Torvalds struct vm_area_struct **pprev) 22941da177e4SLinus Torvalds { 22956bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 22967fdbd37dSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 22971da177e4SLinus Torvalds 22987fdbd37dSLiam R. Howlett vma = mas_walk(&mas); 22997fdbd37dSLiam R. Howlett *pprev = mas_prev(&mas, 0); 23007fdbd37dSLiam R. Howlett if (!vma) 23017fdbd37dSLiam R. Howlett vma = mas_next(&mas, ULONG_MAX); 23026bd4837dSKOSAKI Motohiro return vma; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds /* 23061da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 23071da177e4SLinus Torvalds * update accounting. This is shared with both the 23081da177e4SLinus Torvalds * grow-up and grow-down cases. 23091da177e4SLinus Torvalds */ 23101be7107fSHugh Dickins static int acct_stack_growth(struct vm_area_struct *vma, 23111be7107fSHugh Dickins unsigned long size, unsigned long grow) 23121da177e4SLinus Torvalds { 23131da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 23141be7107fSHugh Dickins unsigned long new_start; 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds /* address space limit tests */ 231784638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, grow)) 23181da177e4SLinus Torvalds return -ENOMEM; 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds /* Stack limit test */ 232124c79d8eSKrzysztof Opasiak if (size > rlimit(RLIMIT_STACK)) 23221da177e4SLinus Torvalds return -ENOMEM; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds /* mlock limit tests */ 2325c5d8a364SMiaohe Lin if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) 23261da177e4SLinus Torvalds return -ENOMEM; 23271da177e4SLinus Torvalds 23280d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 23290d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 23300d59a01bSAdam Litke vma->vm_end - size; 23310d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 23320d59a01bSAdam Litke return -EFAULT; 23330d59a01bSAdam Litke 23341da177e4SLinus Torvalds /* 23351da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 23361da177e4SLinus Torvalds * update security statistics. 23371da177e4SLinus Torvalds */ 233805fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 23391da177e4SLinus Torvalds return -ENOMEM; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds return 0; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 234446dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 23451da177e4SLinus Torvalds /* 234646dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 234746dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 23481da177e4SLinus Torvalds */ 234946dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 23501da177e4SLinus Torvalds { 235109357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 23521be7107fSHugh Dickins struct vm_area_struct *next; 23531be7107fSHugh Dickins unsigned long gap_addr; 235412352d3cSKonstantin Khlebnikov int error = 0; 2355d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 23581da177e4SLinus Torvalds return -EFAULT; 23591da177e4SLinus Torvalds 2360bd726c90SHelge Deller /* Guard against exceeding limits of the address space. */ 23611be7107fSHugh Dickins address &= PAGE_MASK; 236237511fb5SHelge Deller if (address >= (TASK_SIZE & PAGE_MASK)) 236312352d3cSKonstantin Khlebnikov return -ENOMEM; 2364bd726c90SHelge Deller address += PAGE_SIZE; 236512352d3cSKonstantin Khlebnikov 23661be7107fSHugh Dickins /* Enforce stack_guard_gap */ 23671be7107fSHugh Dickins gap_addr = address + stack_guard_gap; 2368bd726c90SHelge Deller 2369bd726c90SHelge Deller /* Guard against overflow */ 2370bd726c90SHelge Deller if (gap_addr < address || gap_addr > TASK_SIZE) 2371bd726c90SHelge Deller gap_addr = TASK_SIZE; 2372bd726c90SHelge Deller 23731be7107fSHugh Dickins next = vma->vm_next; 23743122e80eSAnshuman Khandual if (next && next->vm_start < gap_addr && vma_is_accessible(next)) { 23751be7107fSHugh Dickins if (!(next->vm_flags & VM_GROWSUP)) 23761be7107fSHugh Dickins return -ENOMEM; 23771be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 23781be7107fSHugh Dickins } 23791be7107fSHugh Dickins 2380d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 23811da177e4SLinus Torvalds return -ENOMEM; 23821da177e4SLinus Torvalds 2383d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2384d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2385d4af56c5SLiam R. Howlett mas_destroy(&mas); 2386d4af56c5SLiam R. Howlett return -ENOMEM; 2387d4af56c5SLiam R. Howlett } 2388d4af56c5SLiam R. Howlett 23891da177e4SLinus Torvalds /* 23901da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2391c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 23921da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 23931da177e4SLinus Torvalds */ 239412352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 23971da177e4SLinus Torvalds if (address > vma->vm_end) { 23981da177e4SLinus Torvalds unsigned long size, grow; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds size = address - vma->vm_start; 24011da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 24021da177e4SLinus Torvalds 240342c36f63SHugh Dickins error = -ENOMEM; 240442c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 24051da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 24063af9e859SEric B Munson if (!error) { 24074128997bSMichel Lespinasse /* 2408524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2409524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2410524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2411524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2412524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2413524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2414524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 24154128997bSMichel Lespinasse */ 241609357814SOleg Nesterov spin_lock(&mm->page_table_lock); 241787e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 241809357814SOleg Nesterov mm->locked_vm += grow; 241984638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2420bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 24211da177e4SLinus Torvalds vma->vm_end = address; 2422d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2423d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 2424bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 2425524e00b3SLiam R. Howlett if (!vma->vm_next) 24261be7107fSHugh Dickins mm->highest_vm_end = vm_end_gap(vma); 242709357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 24284128997bSMichel Lespinasse 24293af9e859SEric B Munson perf_event_mmap(vma); 24303af9e859SEric B Munson } 24311da177e4SLinus Torvalds } 243242c36f63SHugh Dickins } 243312352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2434c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2435d4af56c5SLiam R. Howlett mas_destroy(&mas); 24361da177e4SLinus Torvalds return error; 24371da177e4SLinus Torvalds } 243846dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 243946dea3d0SHugh Dickins 24401da177e4SLinus Torvalds /* 24411da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 24421da177e4SLinus Torvalds */ 2443524e00b3SLiam R. Howlett int expand_downwards(struct vm_area_struct *vma, unsigned long address) 24441da177e4SLinus Torvalds { 244509357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 24461be7107fSHugh Dickins struct vm_area_struct *prev; 24470a1d5299SJann Horn int error = 0; 2448d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 24491da177e4SLinus Torvalds 24508869477aSEric Paris address &= PAGE_MASK; 24510a1d5299SJann Horn if (address < mmap_min_addr) 24520a1d5299SJann Horn return -EPERM; 24538869477aSEric Paris 24541be7107fSHugh Dickins /* Enforce stack_guard_gap */ 24551be7107fSHugh Dickins prev = vma->vm_prev; 24561be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 245732e4e6d5SOleg Nesterov if (prev && !(prev->vm_flags & VM_GROWSDOWN) && 24583122e80eSAnshuman Khandual vma_is_accessible(prev)) { 245932e4e6d5SOleg Nesterov if (address - prev->vm_end < stack_guard_gap) 246032e4e6d5SOleg Nesterov return -ENOMEM; 24611be7107fSHugh Dickins } 24621be7107fSHugh Dickins 2463d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 246412352d3cSKonstantin Khlebnikov return -ENOMEM; 24651da177e4SLinus Torvalds 2466d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2467d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2468d4af56c5SLiam R. Howlett mas_destroy(&mas); 2469d4af56c5SLiam R. Howlett return -ENOMEM; 2470d4af56c5SLiam R. Howlett } 2471d4af56c5SLiam R. Howlett 24721da177e4SLinus Torvalds /* 24731da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2474c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 24751da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 24761da177e4SLinus Torvalds */ 247712352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 24801da177e4SLinus Torvalds if (address < vma->vm_start) { 24811da177e4SLinus Torvalds unsigned long size, grow; 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds size = vma->vm_end - address; 24841da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 24851da177e4SLinus Torvalds 2486a626ca6aSLinus Torvalds error = -ENOMEM; 2487a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 24881da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 24891da177e4SLinus Torvalds if (!error) { 24904128997bSMichel Lespinasse /* 2491524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2492524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2493524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2494524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2495524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2496524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2497524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 24984128997bSMichel Lespinasse */ 249909357814SOleg Nesterov spin_lock(&mm->page_table_lock); 250087e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 250109357814SOleg Nesterov mm->locked_vm += grow; 250284638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2503bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 25041da177e4SLinus Torvalds vma->vm_start = address; 25051da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2506d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2507d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 2508bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 250909357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 25104128997bSMichel Lespinasse 25113af9e859SEric B Munson perf_event_mmap(vma); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds } 2514a626ca6aSLinus Torvalds } 251512352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2516c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2517d4af56c5SLiam R. Howlett mas_destroy(&mas); 25181da177e4SLinus Torvalds return error; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 25211be7107fSHugh Dickins /* enforced gap between the expanding stack and other mappings. */ 25221be7107fSHugh Dickins unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; 25231be7107fSHugh Dickins 25241be7107fSHugh Dickins static int __init cmdline_parse_stack_guard_gap(char *p) 25251be7107fSHugh Dickins { 25261be7107fSHugh Dickins unsigned long val; 25271be7107fSHugh Dickins char *endptr; 25281be7107fSHugh Dickins 25291be7107fSHugh Dickins val = simple_strtoul(p, &endptr, 10); 25301be7107fSHugh Dickins if (!*endptr) 25311be7107fSHugh Dickins stack_guard_gap = val << PAGE_SHIFT; 25321be7107fSHugh Dickins 2533e6d09493SRandy Dunlap return 1; 25341be7107fSHugh Dickins } 25351be7107fSHugh Dickins __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); 25361be7107fSHugh Dickins 2537b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2538b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2539b6a2fea3SOllie Wild { 2540b6a2fea3SOllie Wild return expand_upwards(vma, address); 2541b6a2fea3SOllie Wild } 2542b6a2fea3SOllie Wild 2543b6a2fea3SOllie Wild struct vm_area_struct * 2544b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2545b6a2fea3SOllie Wild { 2546b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2547b6a2fea3SOllie Wild 2548b6a2fea3SOllie Wild addr &= PAGE_MASK; 2549b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2550b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2551b6a2fea3SOllie Wild return vma; 25524d45e75aSJann Horn if (!prev || expand_stack(prev, addr)) 2553b6a2fea3SOllie Wild return NULL; 2554cea10a19SMichel Lespinasse if (prev->vm_flags & VM_LOCKED) 2555fc05f566SKirill A. Shutemov populate_vma_page_range(prev, addr, prev->vm_end, NULL); 2556b6a2fea3SOllie Wild return prev; 2557b6a2fea3SOllie Wild } 2558b6a2fea3SOllie Wild #else 2559b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2560b6a2fea3SOllie Wild { 2561b6a2fea3SOllie Wild return expand_downwards(vma, address); 2562b6a2fea3SOllie Wild } 2563b6a2fea3SOllie Wild 25641da177e4SLinus Torvalds struct vm_area_struct * 25651da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 25661da177e4SLinus Torvalds { 25671da177e4SLinus Torvalds struct vm_area_struct *vma; 25681da177e4SLinus Torvalds unsigned long start; 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds addr &= PAGE_MASK; 25711da177e4SLinus Torvalds vma = find_vma(mm, addr); 25721da177e4SLinus Torvalds if (!vma) 25731da177e4SLinus Torvalds return NULL; 25741da177e4SLinus Torvalds if (vma->vm_start <= addr) 25751da177e4SLinus Torvalds return vma; 25761da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 25771da177e4SLinus Torvalds return NULL; 25781da177e4SLinus Torvalds start = vma->vm_start; 25791da177e4SLinus Torvalds if (expand_stack(vma, addr)) 25801da177e4SLinus Torvalds return NULL; 2581cea10a19SMichel Lespinasse if (vma->vm_flags & VM_LOCKED) 2582fc05f566SKirill A. Shutemov populate_vma_page_range(vma, addr, start, NULL); 25831da177e4SLinus Torvalds return vma; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds #endif 25861da177e4SLinus Torvalds 2587e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(find_extend_vma); 2588e1d6d01aSJesse Barnes 25892c0b3814SHugh Dickins /* 25902c0b3814SHugh Dickins * Ok - we have the memory areas we should free on the vma list, 25912c0b3814SHugh Dickins * so release them, and do the vma updates. 25921da177e4SLinus Torvalds * 25932c0b3814SHugh Dickins * Called with the mm semaphore held. 25941da177e4SLinus Torvalds */ 25952c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) 25961da177e4SLinus Torvalds { 25974f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 25984f74d2c8SLinus Torvalds 2599365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2600365e9c87SHugh Dickins update_hiwater_vm(mm); 26012c0b3814SHugh Dickins do { 2602ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 26031da177e4SLinus Torvalds 26044f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 26054f74d2c8SLinus Torvalds nr_accounted += nrpages; 260684638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, -nrpages); 2607a8fb5618SHugh Dickins vma = remove_vma(vma); 2608146425a3SHugh Dickins } while (vma); 26094f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 26101da177e4SLinus Torvalds validate_mm(mm); 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds /* 26141da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 26151da177e4SLinus Torvalds * 2616f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 26171da177e4SLinus Torvalds */ 26181da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm, 2619e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2620e0da382cSHugh Dickins unsigned long start, unsigned long end) 26211da177e4SLinus Torvalds { 2622f39af059SMatthew Wilcox (Oracle) struct vm_area_struct *next = __vma_next(mm, prev); 2623d16dfc55SPeter Zijlstra struct mmu_gather tlb; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds lru_add_drain(); 2626a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm); 2627365e9c87SHugh Dickins update_hiwater_rss(mm); 26284f74d2c8SLinus Torvalds unmap_vmas(&tlb, vma, start, end); 2629d16dfc55SPeter Zijlstra free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 26306ee8630eSHugh Dickins next ? next->vm_start : USER_PGTABLES_CEILING); 2631ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 26321da177e4SLinus Torvalds } 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds /* 26351da177e4SLinus Torvalds * Create a list of vma's touched by the unmap, removing them from the mm's 26361da177e4SLinus Torvalds * vma list as we go.. 26371da177e4SLinus Torvalds */ 2638246c320aSKirill A. Shutemov static bool 2639d4af56c5SLiam R. Howlett detach_vmas_to_be_unmapped(struct mm_struct *mm, struct ma_state *mas, 2640d4af56c5SLiam R. Howlett struct vm_area_struct *vma, struct vm_area_struct *prev, 2641d4af56c5SLiam R. Howlett unsigned long end) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds struct vm_area_struct **insertion_point; 26441da177e4SLinus Torvalds struct vm_area_struct *tail_vma = NULL; 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds insertion_point = (prev ? &prev->vm_next : &mm->mmap); 2647297c5eeeSLinus Torvalds vma->vm_prev = NULL; 2648524e00b3SLiam R. Howlett vma_mas_szero(mas, vma->vm_start, end); 26491da177e4SLinus Torvalds do { 2650a213e5cfSHugh Dickins if (vma->vm_flags & VM_LOCKED) 2651a213e5cfSHugh Dickins mm->locked_vm -= vma_pages(vma); 26521da177e4SLinus Torvalds mm->map_count--; 26531da177e4SLinus Torvalds tail_vma = vma; 26541da177e4SLinus Torvalds vma = vma->vm_next; 26551da177e4SLinus Torvalds } while (vma && vma->vm_start < end); 26561da177e4SLinus Torvalds *insertion_point = vma; 2657524e00b3SLiam R. Howlett if (vma) 2658297c5eeeSLinus Torvalds vma->vm_prev = prev; 2659524e00b3SLiam R. Howlett else 26601be7107fSHugh Dickins mm->highest_vm_end = prev ? vm_end_gap(prev) : 0; 26611da177e4SLinus Torvalds tail_vma->vm_next = NULL; 2662615d6e87SDavidlohr Bueso 2663615d6e87SDavidlohr Bueso /* Kill the cache */ 2664615d6e87SDavidlohr Bueso vmacache_invalidate(mm); 2665246c320aSKirill A. Shutemov 2666246c320aSKirill A. Shutemov /* 2667246c320aSKirill A. Shutemov * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or 2668246c320aSKirill A. Shutemov * VM_GROWSUP VMA. Such VMAs can change their size under 2669246c320aSKirill A. Shutemov * down_read(mmap_lock) and collide with the VMA we are about to unmap. 2670246c320aSKirill A. Shutemov */ 2671246c320aSKirill A. Shutemov if (vma && (vma->vm_flags & VM_GROWSDOWN)) 2672246c320aSKirill A. Shutemov return false; 2673246c320aSKirill A. Shutemov if (prev && (prev->vm_flags & VM_GROWSUP)) 2674246c320aSKirill A. Shutemov return false; 2675246c320aSKirill A. Shutemov return true; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds /* 2679def5efe0SDavid Rientjes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 2680def5efe0SDavid Rientjes * has already been checked or doesn't make sense to fail. 26811da177e4SLinus Torvalds */ 2682def5efe0SDavid Rientjes int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 26831da177e4SLinus Torvalds unsigned long addr, int new_below) 26841da177e4SLinus Torvalds { 26851da177e4SLinus Torvalds struct vm_area_struct *new; 2686e3975891SChen Gang int err; 2687d4af56c5SLiam R. Howlett validate_mm_mt(mm); 26881da177e4SLinus Torvalds 2689dd3b614fSDmitry Safonov if (vma->vm_ops && vma->vm_ops->may_split) { 2690dd3b614fSDmitry Safonov err = vma->vm_ops->may_split(vma, addr); 269131383c68SDan Williams if (err) 269231383c68SDan Williams return err; 269331383c68SDan Williams } 26941da177e4SLinus Torvalds 26953928d4f5SLinus Torvalds new = vm_area_dup(vma); 26961da177e4SLinus Torvalds if (!new) 2697e3975891SChen Gang return -ENOMEM; 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds if (new_below) 27001da177e4SLinus Torvalds new->vm_end = addr; 27011da177e4SLinus Torvalds else { 27021da177e4SLinus Torvalds new->vm_start = addr; 27031da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 27041da177e4SLinus Torvalds } 27051da177e4SLinus Torvalds 2706ef0855d3SOleg Nesterov err = vma_dup_policy(vma, new); 2707ef0855d3SOleg Nesterov if (err) 27085beb4930SRik van Riel goto out_free_vma; 27091da177e4SLinus Torvalds 2710c4ea95d7SDaniel Forrest err = anon_vma_clone(new, vma); 2711c4ea95d7SDaniel Forrest if (err) 27125beb4930SRik van Riel goto out_free_mpol; 27135beb4930SRik van Riel 2714e9714acfSKonstantin Khlebnikov if (new->vm_file) 27151da177e4SLinus Torvalds get_file(new->vm_file); 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 27181da177e4SLinus Torvalds new->vm_ops->open(new); 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds if (new_below) 27215beb4930SRik van Riel err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + 27221da177e4SLinus Torvalds ((addr - new->vm_start) >> PAGE_SHIFT), new); 27231da177e4SLinus Torvalds else 27245beb4930SRik van Riel err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); 27251da177e4SLinus Torvalds 27265beb4930SRik van Riel /* Success. */ 27275beb4930SRik van Riel if (!err) 27281da177e4SLinus Torvalds return 0; 27295beb4930SRik van Riel 2730d4af56c5SLiam R. Howlett /* Avoid vm accounting in close() operation */ 2731d4af56c5SLiam R. Howlett new->vm_start = new->vm_end; 2732d4af56c5SLiam R. Howlett new->vm_pgoff = 0; 27335beb4930SRik van Riel /* Clean everything up if vma_adjust failed. */ 273458927533SRik van Riel if (new->vm_ops && new->vm_ops->close) 27355beb4930SRik van Riel new->vm_ops->close(new); 2736e9714acfSKonstantin Khlebnikov if (new->vm_file) 27375beb4930SRik van Riel fput(new->vm_file); 27382aeadc30SAndrea Arcangeli unlink_anon_vmas(new); 27395beb4930SRik van Riel out_free_mpol: 2740ef0855d3SOleg Nesterov mpol_put(vma_policy(new)); 27415beb4930SRik van Riel out_free_vma: 27423928d4f5SLinus Torvalds vm_area_free(new); 2743d4af56c5SLiam R. Howlett validate_mm_mt(mm); 27445beb4930SRik van Riel return err; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 2747659ace58SKOSAKI Motohiro /* 2748659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2749659ace58SKOSAKI Motohiro * either for the first part or the tail. 2750659ace58SKOSAKI Motohiro */ 2751659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 2752659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2753659ace58SKOSAKI Motohiro { 2754659ace58SKOSAKI Motohiro if (mm->map_count >= sysctl_max_map_count) 2755659ace58SKOSAKI Motohiro return -ENOMEM; 2756659ace58SKOSAKI Motohiro 2757659ace58SKOSAKI Motohiro return __split_vma(mm, vma, addr, new_below); 2758659ace58SKOSAKI Motohiro } 2759659ace58SKOSAKI Motohiro 27601da177e4SLinus Torvalds /* Munmap is split into 2 main parts -- this part which finds 27611da177e4SLinus Torvalds * what needs doing, and the areas themselves, which do the 27621da177e4SLinus Torvalds * work. This now handles partial unmappings. 27631da177e4SLinus Torvalds * Jeremy Fitzhardinge <jeremy@goop.org> 27641da177e4SLinus Torvalds */ 276585a06835SYang Shi int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2766dd2283f2SYang Shi struct list_head *uf, bool downgrade) 27671da177e4SLinus Torvalds { 27681da177e4SLinus Torvalds unsigned long end; 2769146425a3SHugh Dickins struct vm_area_struct *vma, *prev, *last; 2770d4af56c5SLiam R. Howlett int error = -ENOMEM; 2771d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 27721da177e4SLinus Torvalds 2773de1741a1SAlexander Kuleshov if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 27741da177e4SLinus Torvalds return -EINVAL; 27751da177e4SLinus Torvalds 2776cc71aba3Svishnu.ps len = PAGE_ALIGN(len); 27775a28fc94SDave Hansen end = start + len; 2778cc71aba3Svishnu.ps if (len == 0) 27791da177e4SLinus Torvalds return -EINVAL; 27801da177e4SLinus Torvalds 2781524e00b3SLiam R. Howlett /* arch_unmap() might do unmaps itself. */ 27825a28fc94SDave Hansen arch_unmap(mm, start, end); 27835a28fc94SDave Hansen 278478d9cf60SGonzalo Matias Juarez Tello /* Find the first overlapping VMA where start < vma->vm_end */ 278578d9cf60SGonzalo Matias Juarez Tello vma = find_vma_intersection(mm, start, end); 2786146425a3SHugh Dickins if (!vma) 27871da177e4SLinus Torvalds return 0; 2788d4af56c5SLiam R. Howlett 2789d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 2790d4af56c5SLiam R. Howlett return -ENOMEM; 27919be34c9dSLinus Torvalds prev = vma->vm_prev; 2792524e00b3SLiam R. Howlett /* we have start < vma->vm_end */ 2793524e00b3SLiam R. Howlett 27941da177e4SLinus Torvalds /* 27951da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 27961da177e4SLinus Torvalds * 27971da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 27981da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 27991da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 28001da177e4SLinus Torvalds */ 2801146425a3SHugh Dickins if (start > vma->vm_start) { 2802659ace58SKOSAKI Motohiro 2803659ace58SKOSAKI Motohiro /* 2804659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2805659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2806659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2807659ace58SKOSAKI Motohiro */ 2808659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2809d4af56c5SLiam R. Howlett goto map_count_exceeded; 2810659ace58SKOSAKI Motohiro 2811659ace58SKOSAKI Motohiro error = __split_vma(mm, vma, start, 0); 28121da177e4SLinus Torvalds if (error) 2813d4af56c5SLiam R. Howlett goto split_failed; 2814146425a3SHugh Dickins prev = vma; 28151da177e4SLinus Torvalds } 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds /* Does it split the last one? */ 28181da177e4SLinus Torvalds last = find_vma(mm, end); 28191da177e4SLinus Torvalds if (last && end > last->vm_start) { 2820d4af56c5SLiam R. Howlett error = __split_vma(mm, last, end, 1); 28211da177e4SLinus Torvalds if (error) 2822d4af56c5SLiam R. Howlett goto split_failed; 28231da177e4SLinus Torvalds } 2824f39af059SMatthew Wilcox (Oracle) vma = __vma_next(mm, prev); 28251da177e4SLinus Torvalds 28262376dd7cSAndrea Arcangeli if (unlikely(uf)) { 28272376dd7cSAndrea Arcangeli /* 28282376dd7cSAndrea Arcangeli * If userfaultfd_unmap_prep returns an error the vmas 2829f0953a1bSIngo Molnar * will remain split, but userland will get a 28302376dd7cSAndrea Arcangeli * highly unexpected error anyway. This is no 28312376dd7cSAndrea Arcangeli * different than the case where the first of the two 28322376dd7cSAndrea Arcangeli * __split_vma fails, but we don't undo the first 28332376dd7cSAndrea Arcangeli * split, despite we could. This is unlikely enough 28342376dd7cSAndrea Arcangeli * failure that it's not worth optimizing it for. 28352376dd7cSAndrea Arcangeli */ 2836d4af56c5SLiam R. Howlett error = userfaultfd_unmap_prep(vma, start, end, uf); 28372376dd7cSAndrea Arcangeli if (error) 2838d4af56c5SLiam R. Howlett goto userfaultfd_error; 28392376dd7cSAndrea Arcangeli } 28402376dd7cSAndrea Arcangeli 2841dd2283f2SYang Shi /* Detach vmas from rbtree */ 2842d4af56c5SLiam R. Howlett if (!detach_vmas_to_be_unmapped(mm, &mas, vma, prev, end)) 2843246c320aSKirill A. Shutemov downgrade = false; 28441da177e4SLinus Torvalds 2845dd2283f2SYang Shi if (downgrade) 2846d8ed45c5SMichel Lespinasse mmap_write_downgrade(mm); 2847dd2283f2SYang Shi 2848dd2283f2SYang Shi unmap_region(mm, vma, prev, start, end); 2849dd2283f2SYang Shi 28501da177e4SLinus Torvalds /* Fix up all other VM information */ 28512c0b3814SHugh Dickins remove_vma_list(mm, vma); 28521da177e4SLinus Torvalds 2853524e00b3SLiam R. Howlett 2854524e00b3SLiam R. Howlett validate_mm(mm); 2855dd2283f2SYang Shi return downgrade ? 1 : 0; 2856d4af56c5SLiam R. Howlett 2857d4af56c5SLiam R. Howlett map_count_exceeded: 2858d4af56c5SLiam R. Howlett split_failed: 2859d4af56c5SLiam R. Howlett userfaultfd_error: 2860d4af56c5SLiam R. Howlett mas_destroy(&mas); 2861d4af56c5SLiam R. Howlett return error; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 2864dd2283f2SYang Shi int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2865dd2283f2SYang Shi struct list_head *uf) 2866dd2283f2SYang Shi { 2867dd2283f2SYang Shi return __do_munmap(mm, start, len, uf, false); 2868dd2283f2SYang Shi } 2869dd2283f2SYang Shi 2870dd2283f2SYang Shi static int __vm_munmap(unsigned long start, size_t len, bool downgrade) 2871a46ef99dSLinus Torvalds { 2872a46ef99dSLinus Torvalds int ret; 2873bfce281cSAl Viro struct mm_struct *mm = current->mm; 2874897ab3e0SMike Rapoport LIST_HEAD(uf); 2875a46ef99dSLinus Torvalds 2876d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2877ae798783SMichal Hocko return -EINTR; 2878ae798783SMichal Hocko 2879dd2283f2SYang Shi ret = __do_munmap(mm, start, len, &uf, downgrade); 2880dd2283f2SYang Shi /* 2881c1e8d7c6SMichel Lespinasse * Returning 1 indicates mmap_lock is downgraded. 2882dd2283f2SYang Shi * But 1 is not legal return value of vm_munmap() and munmap(), reset 2883dd2283f2SYang Shi * it to 0 before return. 2884dd2283f2SYang Shi */ 2885dd2283f2SYang Shi if (ret == 1) { 2886d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2887dd2283f2SYang Shi ret = 0; 2888dd2283f2SYang Shi } else 2889d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2890dd2283f2SYang Shi 2891897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 2892a46ef99dSLinus Torvalds return ret; 2893a46ef99dSLinus Torvalds } 2894dd2283f2SYang Shi 2895dd2283f2SYang Shi int vm_munmap(unsigned long start, size_t len) 2896dd2283f2SYang Shi { 2897dd2283f2SYang Shi return __vm_munmap(start, len, false); 2898dd2283f2SYang Shi } 2899a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2900a46ef99dSLinus Torvalds 29016a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 29021da177e4SLinus Torvalds { 2903ce18d171SCatalin Marinas addr = untagged_addr(addr); 2904dd2283f2SYang Shi return __vm_munmap(addr, len, true); 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds 2907c8d78c18SKirill A. Shutemov 2908c8d78c18SKirill A. Shutemov /* 2909c8d78c18SKirill A. Shutemov * Emulation of deprecated remap_file_pages() syscall. 2910c8d78c18SKirill A. Shutemov */ 2911c8d78c18SKirill A. Shutemov SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, 2912c8d78c18SKirill A. Shutemov unsigned long, prot, unsigned long, pgoff, unsigned long, flags) 2913c8d78c18SKirill A. Shutemov { 2914c8d78c18SKirill A. Shutemov 2915c8d78c18SKirill A. Shutemov struct mm_struct *mm = current->mm; 2916c8d78c18SKirill A. Shutemov struct vm_area_struct *vma; 2917c8d78c18SKirill A. Shutemov unsigned long populate = 0; 2918c8d78c18SKirill A. Shutemov unsigned long ret = -EINVAL; 2919c8d78c18SKirill A. Shutemov struct file *file; 2920c8d78c18SKirill A. Shutemov 2921ee65728eSMike Rapoport pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", 2922c8d78c18SKirill A. Shutemov current->comm, current->pid); 2923c8d78c18SKirill A. Shutemov 2924c8d78c18SKirill A. Shutemov if (prot) 2925c8d78c18SKirill A. Shutemov return ret; 2926c8d78c18SKirill A. Shutemov start = start & PAGE_MASK; 2927c8d78c18SKirill A. Shutemov size = size & PAGE_MASK; 2928c8d78c18SKirill A. Shutemov 2929c8d78c18SKirill A. Shutemov if (start + size <= start) 2930c8d78c18SKirill A. Shutemov return ret; 2931c8d78c18SKirill A. Shutemov 2932c8d78c18SKirill A. Shutemov /* Does pgoff wrap? */ 2933c8d78c18SKirill A. Shutemov if (pgoff + (size >> PAGE_SHIFT) < pgoff) 2934c8d78c18SKirill A. Shutemov return ret; 2935c8d78c18SKirill A. Shutemov 2936d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2937dc0ef0dfSMichal Hocko return -EINTR; 2938dc0ef0dfSMichal Hocko 29399b593cb2SLiam R. Howlett vma = vma_lookup(mm, start); 2940c8d78c18SKirill A. Shutemov 2941c8d78c18SKirill A. Shutemov if (!vma || !(vma->vm_flags & VM_SHARED)) 2942c8d78c18SKirill A. Shutemov goto out; 2943c8d78c18SKirill A. Shutemov 294448f7df32SKirill A. Shutemov if (start + size > vma->vm_end) { 294548f7df32SKirill A. Shutemov struct vm_area_struct *next; 294648f7df32SKirill A. Shutemov 294748f7df32SKirill A. Shutemov for (next = vma->vm_next; next; next = next->vm_next) { 294848f7df32SKirill A. Shutemov /* hole between vmas ? */ 294948f7df32SKirill A. Shutemov if (next->vm_start != next->vm_prev->vm_end) 295048f7df32SKirill A. Shutemov goto out; 295148f7df32SKirill A. Shutemov 295248f7df32SKirill A. Shutemov if (next->vm_file != vma->vm_file) 295348f7df32SKirill A. Shutemov goto out; 295448f7df32SKirill A. Shutemov 295548f7df32SKirill A. Shutemov if (next->vm_flags != vma->vm_flags) 295648f7df32SKirill A. Shutemov goto out; 295748f7df32SKirill A. Shutemov 295848f7df32SKirill A. Shutemov if (start + size <= next->vm_end) 295948f7df32SKirill A. Shutemov break; 296048f7df32SKirill A. Shutemov } 296148f7df32SKirill A. Shutemov 296248f7df32SKirill A. Shutemov if (!next) 2963c8d78c18SKirill A. Shutemov goto out; 2964c8d78c18SKirill A. Shutemov } 2965c8d78c18SKirill A. Shutemov 2966c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; 2967c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; 2968c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; 2969c8d78c18SKirill A. Shutemov 2970c8d78c18SKirill A. Shutemov flags &= MAP_NONBLOCK; 2971c8d78c18SKirill A. Shutemov flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; 2972fce000b1SLiam Howlett if (vma->vm_flags & VM_LOCKED) 2973c8d78c18SKirill A. Shutemov flags |= MAP_LOCKED; 297448f7df32SKirill A. Shutemov 2975c8d78c18SKirill A. Shutemov file = get_file(vma->vm_file); 297645e55300SPeter Collingbourne ret = do_mmap(vma->vm_file, start, size, 2977897ab3e0SMike Rapoport prot, flags, pgoff, &populate, NULL); 2978c8d78c18SKirill A. Shutemov fput(file); 2979c8d78c18SKirill A. Shutemov out: 2980d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2981c8d78c18SKirill A. Shutemov if (populate) 2982c8d78c18SKirill A. Shutemov mm_populate(ret, populate); 2983c8d78c18SKirill A. Shutemov if (!IS_ERR_VALUE(ret)) 2984c8d78c18SKirill A. Shutemov ret = 0; 2985c8d78c18SKirill A. Shutemov return ret; 2986c8d78c18SKirill A. Shutemov } 2987c8d78c18SKirill A. Shutemov 29881da177e4SLinus Torvalds /* 29892e7ce7d3SLiam R. Howlett * brk_munmap() - Unmap a parital vma. 29902e7ce7d3SLiam R. Howlett * @mas: The maple tree state. 29912e7ce7d3SLiam R. Howlett * @vma: The vma to be modified 29922e7ce7d3SLiam R. Howlett * @newbrk: the start of the address to unmap 29932e7ce7d3SLiam R. Howlett * @oldbrk: The end of the address to unmap 29942e7ce7d3SLiam R. Howlett * @uf: The userfaultfd list_head 29952e7ce7d3SLiam R. Howlett * 29962e7ce7d3SLiam R. Howlett * Returns: 1 on success. 29972e7ce7d3SLiam R. Howlett * unmaps a partial VMA mapping. Does not handle alignment, downgrades lock if 29982e7ce7d3SLiam R. Howlett * possible. 29991da177e4SLinus Torvalds */ 30002e7ce7d3SLiam R. Howlett static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, 30012e7ce7d3SLiam R. Howlett unsigned long newbrk, unsigned long oldbrk, 30022e7ce7d3SLiam R. Howlett struct list_head *uf) 30032e7ce7d3SLiam R. Howlett { 30042e7ce7d3SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 30052e7ce7d3SLiam R. Howlett int ret; 30062e7ce7d3SLiam R. Howlett 30072e7ce7d3SLiam R. Howlett arch_unmap(mm, newbrk, oldbrk); 30082e7ce7d3SLiam R. Howlett ret = __do_munmap(mm, newbrk, oldbrk - newbrk, uf, true); 30092e7ce7d3SLiam R. Howlett validate_mm_mt(mm); 30102e7ce7d3SLiam R. Howlett return ret; 30112e7ce7d3SLiam R. Howlett } 30122e7ce7d3SLiam R. Howlett 30132e7ce7d3SLiam R. Howlett /* 30142e7ce7d3SLiam R. Howlett * do_brk_flags() - Increase the brk vma if the flags match. 30152e7ce7d3SLiam R. Howlett * @mas: The maple tree state. 30162e7ce7d3SLiam R. Howlett * @addr: The start address 30172e7ce7d3SLiam R. Howlett * @len: The length of the increase 30182e7ce7d3SLiam R. Howlett * @vma: The vma, 30192e7ce7d3SLiam R. Howlett * @flags: The VMA Flags 30202e7ce7d3SLiam R. Howlett * 30212e7ce7d3SLiam R. Howlett * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags 30222e7ce7d3SLiam R. Howlett * do not match then create a new anonymous VMA. Eventually we may be able to 30232e7ce7d3SLiam R. Howlett * do some brk-specific accounting here. 30242e7ce7d3SLiam R. Howlett */ 30252e7ce7d3SLiam R. Howlett static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, 30262e7ce7d3SLiam R. Howlett unsigned long addr, unsigned long len, 30272e7ce7d3SLiam R. Howlett unsigned long flags) 30281da177e4SLinus Torvalds { 30291da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 30302e7ce7d3SLiam R. Howlett struct vm_area_struct *prev = NULL; 30312e7ce7d3SLiam R. Howlett 3032d4af56c5SLiam R. Howlett validate_mm_mt(mm); 30332e7ce7d3SLiam R. Howlett /* 30342e7ce7d3SLiam R. Howlett * Check against address space limits by the changed size 30352e7ce7d3SLiam R. Howlett * Note: This happens *after* clearing old mappings in some code paths. 30362e7ce7d3SLiam R. Howlett */ 303716e72e9bSDenys Vlasenko flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 303884638335SKonstantin Khlebnikov if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) 30391da177e4SLinus Torvalds return -ENOMEM; 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 30421da177e4SLinus Torvalds return -ENOMEM; 30431da177e4SLinus Torvalds 3044191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 30451da177e4SLinus Torvalds return -ENOMEM; 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds /* 30482e7ce7d3SLiam R. Howlett * Expand the existing vma if possible; Note that singular lists do not 30492e7ce7d3SLiam R. Howlett * occur after forking, so the expand will only happen on new VMAs. 30501da177e4SLinus Torvalds */ 30512e7ce7d3SLiam R. Howlett if (vma && 30522e7ce7d3SLiam R. Howlett (!vma->anon_vma || list_is_singular(&vma->anon_vma_chain)) && 30532e7ce7d3SLiam R. Howlett ((vma->vm_flags & ~VM_SOFTDIRTY) == flags)) { 30542e7ce7d3SLiam R. Howlett mas->index = vma->vm_start; 30552e7ce7d3SLiam R. Howlett mas->last = addr + len - 1; 30562e7ce7d3SLiam R. Howlett vma_adjust_trans_huge(vma, addr, addr + len, 0); 30572e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 30582e7ce7d3SLiam R. Howlett anon_vma_lock_write(vma->anon_vma); 30592e7ce7d3SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 30601da177e4SLinus Torvalds } 30612e7ce7d3SLiam R. Howlett vma->vm_end = addr + len; 30622e7ce7d3SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 30632e7ce7d3SLiam R. Howlett if (mas_store_gfp(mas, vma, GFP_KERNEL)) 30642e7ce7d3SLiam R. Howlett goto mas_expand_failed; 30652e7ce7d3SLiam R. Howlett 30662e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 30672e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 30682e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 30692e7ce7d3SLiam R. Howlett } 30702e7ce7d3SLiam R. Howlett khugepaged_enter_vma(vma, flags); 30712e7ce7d3SLiam R. Howlett goto out; 30722e7ce7d3SLiam R. Howlett } 30732e7ce7d3SLiam R. Howlett prev = vma; 30742e7ce7d3SLiam R. Howlett 30752e7ce7d3SLiam R. Howlett /* create a vma struct for an anonymous mapping */ 30762e7ce7d3SLiam R. Howlett vma = vm_area_alloc(mm); 30772e7ce7d3SLiam R. Howlett if (!vma) 30782e7ce7d3SLiam R. Howlett goto vma_alloc_fail; 30791da177e4SLinus Torvalds 3080bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 30811da177e4SLinus Torvalds vma->vm_start = addr; 30821da177e4SLinus Torvalds vma->vm_end = addr + len; 30832e7ce7d3SLiam R. Howlett vma->vm_pgoff = addr >> PAGE_SHIFT; 30841da177e4SLinus Torvalds vma->vm_flags = flags; 30853ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 30862e7ce7d3SLiam R. Howlett mas_set_range(mas, vma->vm_start, addr + len - 1); 30872e7ce7d3SLiam R. Howlett if (mas_store_gfp(mas, vma, GFP_KERNEL)) 30882e7ce7d3SLiam R. Howlett goto mas_store_fail; 3089d4af56c5SLiam R. Howlett 30902e7ce7d3SLiam R. Howlett if (!prev) 30912e7ce7d3SLiam R. Howlett prev = mas_prev(mas, 0); 30922e7ce7d3SLiam R. Howlett 30932e7ce7d3SLiam R. Howlett __vma_link_list(mm, vma, prev); 30942e7ce7d3SLiam R. Howlett mm->map_count++; 30951da177e4SLinus Torvalds out: 30963af9e859SEric B Munson perf_event_mmap(vma); 30971da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 309884638335SKonstantin Khlebnikov mm->data_vm += len >> PAGE_SHIFT; 3099128557ffSMichel Lespinasse if (flags & VM_LOCKED) 3100ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 3101d9104d1cSCyrill Gorcunov vma->vm_flags |= VM_SOFTDIRTY; 3102d4af56c5SLiam R. Howlett validate_mm_mt(mm); 31035d22fc25SLinus Torvalds return 0; 3104d4af56c5SLiam R. Howlett 31052e7ce7d3SLiam R. Howlett mas_store_fail: 3106d4af56c5SLiam R. Howlett vm_area_free(vma); 31072e7ce7d3SLiam R. Howlett vma_alloc_fail: 31082e7ce7d3SLiam R. Howlett vm_unacct_memory(len >> PAGE_SHIFT); 31092e7ce7d3SLiam R. Howlett return -ENOMEM; 31102e7ce7d3SLiam R. Howlett 31112e7ce7d3SLiam R. Howlett mas_expand_failed: 31122e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 31132e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 31142e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 31152e7ce7d3SLiam R. Howlett } 3116d4af56c5SLiam R. Howlett return -ENOMEM; 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds 3119bb177a73SMichal Hocko int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) 3120e4eb1ff6SLinus Torvalds { 3121e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 31222e7ce7d3SLiam R. Howlett struct vm_area_struct *vma = NULL; 3123bb177a73SMichal Hocko unsigned long len; 31245d22fc25SLinus Torvalds int ret; 3125128557ffSMichel Lespinasse bool populate; 3126897ab3e0SMike Rapoport LIST_HEAD(uf); 31272e7ce7d3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 3128e4eb1ff6SLinus Torvalds 3129bb177a73SMichal Hocko len = PAGE_ALIGN(request); 3130bb177a73SMichal Hocko if (len < request) 3131bb177a73SMichal Hocko return -ENOMEM; 3132bb177a73SMichal Hocko if (!len) 3133bb177a73SMichal Hocko return 0; 3134bb177a73SMichal Hocko 3135d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 31362d6c9282SMichal Hocko return -EINTR; 31372d6c9282SMichal Hocko 31382e7ce7d3SLiam R. Howlett /* Until we need other flags, refuse anything except VM_EXEC. */ 31392e7ce7d3SLiam R. Howlett if ((flags & (~VM_EXEC)) != 0) 31402e7ce7d3SLiam R. Howlett return -EINVAL; 31412e7ce7d3SLiam R. Howlett 31422e7ce7d3SLiam R. Howlett ret = check_brk_limits(addr, len); 31432e7ce7d3SLiam R. Howlett if (ret) 31442e7ce7d3SLiam R. Howlett goto limits_failed; 31452e7ce7d3SLiam R. Howlett 31462e7ce7d3SLiam R. Howlett if (find_vma_intersection(mm, addr, addr + len)) 31472e7ce7d3SLiam R. Howlett ret = do_munmap(mm, addr, len, &uf); 31482e7ce7d3SLiam R. Howlett 31492e7ce7d3SLiam R. Howlett if (ret) 31502e7ce7d3SLiam R. Howlett goto munmap_failed; 31512e7ce7d3SLiam R. Howlett 31522e7ce7d3SLiam R. Howlett vma = mas_prev(&mas, 0); 31532e7ce7d3SLiam R. Howlett if (!vma || vma->vm_end != addr || vma_policy(vma) || 31542e7ce7d3SLiam R. Howlett !can_vma_merge_after(vma, flags, NULL, NULL, 31552e7ce7d3SLiam R. Howlett addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) 31562e7ce7d3SLiam R. Howlett vma = NULL; 31572e7ce7d3SLiam R. Howlett 31582e7ce7d3SLiam R. Howlett ret = do_brk_flags(&mas, vma, addr, len, flags); 3159128557ffSMichel Lespinasse populate = ((mm->def_flags & VM_LOCKED) != 0); 3160d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3161897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 31625d22fc25SLinus Torvalds if (populate && !ret) 3163128557ffSMichel Lespinasse mm_populate(addr, len); 3164e4eb1ff6SLinus Torvalds return ret; 31652e7ce7d3SLiam R. Howlett 31662e7ce7d3SLiam R. Howlett munmap_failed: 31672e7ce7d3SLiam R. Howlett limits_failed: 31682e7ce7d3SLiam R. Howlett mmap_write_unlock(mm); 31692e7ce7d3SLiam R. Howlett return ret; 3170e4eb1ff6SLinus Torvalds } 317116e72e9bSDenys Vlasenko EXPORT_SYMBOL(vm_brk_flags); 317216e72e9bSDenys Vlasenko 317316e72e9bSDenys Vlasenko int vm_brk(unsigned long addr, unsigned long len) 317416e72e9bSDenys Vlasenko { 317516e72e9bSDenys Vlasenko return vm_brk_flags(addr, len, 0); 317616e72e9bSDenys Vlasenko } 3177e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 31781da177e4SLinus Torvalds 31791da177e4SLinus Torvalds /* Release all mmaps. */ 31801da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 31811da177e4SLinus Torvalds { 3182d16dfc55SPeter Zijlstra struct mmu_gather tlb; 3183ba470de4SRik van Riel struct vm_area_struct *vma; 31841da177e4SLinus Torvalds unsigned long nr_accounted = 0; 31851da177e4SLinus Torvalds 3186d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 3187cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 3188d6dd61c8SJeremy Fitzhardinge 318927ae357fSDavid Rientjes if (unlikely(mm_is_oom_victim(mm))) { 319027ae357fSDavid Rientjes /* 319127ae357fSDavid Rientjes * Manually reap the mm to free as much memory as possible. 319227ae357fSDavid Rientjes * Then, as the oom reaper does, set MMF_OOM_SKIP to disregard 3193c1e8d7c6SMichel Lespinasse * this mm from further consideration. Taking mm->mmap_lock for 319427ae357fSDavid Rientjes * write after setting MMF_OOM_SKIP will guarantee that the oom 3195c1e8d7c6SMichel Lespinasse * reaper will not run on this mm again after mmap_lock is 319627ae357fSDavid Rientjes * dropped. 319727ae357fSDavid Rientjes * 3198c1e8d7c6SMichel Lespinasse * Nothing can be holding mm->mmap_lock here and the above call 319927ae357fSDavid Rientjes * to mmu_notifier_release(mm) ensures mmu notifier callbacks in 320027ae357fSDavid Rientjes * __oom_reap_task_mm() will not block. 320127ae357fSDavid Rientjes */ 320293065ac7SMichal Hocko (void)__oom_reap_task_mm(mm); 320327ae357fSDavid Rientjes set_bit(MMF_OOM_SKIP, &mm->flags); 320427ae357fSDavid Rientjes } 320527ae357fSDavid Rientjes 320664591e86SSuren Baghdasaryan mmap_write_lock(mm); 32079480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 32089480c53eSJeremy Fitzhardinge 3209ba470de4SRik van Riel vma = mm->mmap; 321064591e86SSuren Baghdasaryan if (!vma) { 321164591e86SSuren Baghdasaryan /* Can happen if dup_mmap() received an OOM */ 321264591e86SSuren Baghdasaryan mmap_write_unlock(mm); 32139480c53eSJeremy Fitzhardinge return; 321464591e86SSuren Baghdasaryan } 32159480c53eSJeremy Fitzhardinge 32161da177e4SLinus Torvalds lru_add_drain(); 32171da177e4SLinus Torvalds flush_cache_mm(mm); 3218d8b45053SWill Deacon tlb_gather_mmu_fullmm(&tlb, mm); 3219901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 3220e0da382cSHugh Dickins /* Use -1 here to ensure all VMAs in the mm are unmapped */ 32214f74d2c8SLinus Torvalds unmap_vmas(&tlb, vma, 0, -1); 32226ee8630eSHugh Dickins free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); 3223ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 32241da177e4SLinus Torvalds 322564591e86SSuren Baghdasaryan /* Walk the list again, actually closing and freeing it. */ 32264f74d2c8SLinus Torvalds while (vma) { 32274f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 32284f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 3229a8fb5618SHugh Dickins vma = remove_vma(vma); 32300a3b3c25SPaul E. McKenney cond_resched(); 32314f74d2c8SLinus Torvalds } 3232d4af56c5SLiam R. Howlett 3233d4af56c5SLiam R. Howlett trace_exit_mmap(mm); 3234d4af56c5SLiam R. Howlett __mt_destroy(&mm->mm_mt); 3235f798a1d4SSuren Baghdasaryan mm->mmap = NULL; 323664591e86SSuren Baghdasaryan mmap_write_unlock(mm); 32374f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 32381da177e4SLinus Torvalds } 32391da177e4SLinus Torvalds 32401da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 32411da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 3242c8c06efaSDavidlohr Bueso * then i_mmap_rwsem is taken here. 32431da177e4SLinus Torvalds */ 32441da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 32451da177e4SLinus Torvalds { 32466597d783SHugh Dickins struct vm_area_struct *prev; 3247d4af56c5SLiam R. Howlett unsigned long charged = vma_pages(vma); 32481da177e4SLinus Torvalds 3249d4af56c5SLiam R. Howlett 3250524e00b3SLiam R. Howlett if (range_has_overlap(mm, vma->vm_start, vma->vm_end, &prev)) 3251c9d13f5fSChen Gang return -ENOMEM; 3252d4af56c5SLiam R. Howlett 3253c9d13f5fSChen Gang if ((vma->vm_flags & VM_ACCOUNT) && 3254d4af56c5SLiam R. Howlett security_vm_enough_memory_mm(mm, charged)) 3255c9d13f5fSChen Gang return -ENOMEM; 3256c9d13f5fSChen Gang 32571da177e4SLinus Torvalds /* 32581da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 32591da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 32601da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 32611da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 32621da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 32631da177e4SLinus Torvalds * 32641da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 32651da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 32661da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 32678332326eSLiao Pingfang * Similarly in do_mmap and in do_brk_flags. 32681da177e4SLinus Torvalds */ 32698a9cc3b5SOleg Nesterov if (vma_is_anonymous(vma)) { 32701da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 32711da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 32721da177e4SLinus Torvalds } 32732b144498SSrikar Dronamraju 3274524e00b3SLiam R. Howlett if (vma_link(mm, vma, prev)) { 3275d4af56c5SLiam R. Howlett vm_unacct_memory(charged); 3276d4af56c5SLiam R. Howlett return -ENOMEM; 3277d4af56c5SLiam R. Howlett } 3278d4af56c5SLiam R. Howlett 32791da177e4SLinus Torvalds return 0; 32801da177e4SLinus Torvalds } 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds /* 32831da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 32841da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 32851da177e4SLinus Torvalds */ 32861da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 328738a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 328838a76013SMichel Lespinasse bool *need_rmap_locks) 32891da177e4SLinus Torvalds { 32901da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 32911da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 32921da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 32931da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 3294948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 32951da177e4SLinus Torvalds 3296d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32971da177e4SLinus Torvalds /* 32981da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 32991da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 33001da177e4SLinus Torvalds */ 3301ce75799bSOleg Nesterov if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 33021da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 3303948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 3304948f017bSAndrea Arcangeli } 33051da177e4SLinus Torvalds 3306524e00b3SLiam R. Howlett if (range_has_overlap(mm, addr, addr + len, &prev)) 33076597d783SHugh Dickins return NULL; /* should never get here */ 3308524e00b3SLiam R. Howlett 33091da177e4SLinus Torvalds new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, 331019a809afSAndrea Arcangeli vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), 33115c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 33121da177e4SLinus Torvalds if (new_vma) { 33131da177e4SLinus Torvalds /* 33141da177e4SLinus Torvalds * Source vma may have been merged into new_vma 33151da177e4SLinus Torvalds */ 3316948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 3317948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 3318948f017bSAndrea Arcangeli /* 3319948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 3320948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 3321948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 3322948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 3323948f017bSAndrea Arcangeli * destination address of the mremap to allow 3324948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 3325948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 3326948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 3327948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 3328948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 3329948f017bSAndrea Arcangeli */ 333081d1b09cSSasha Levin VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 333138a76013SMichel Lespinasse *vmap = vma = new_vma; 3332108d6642SMichel Lespinasse } 333338a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 33341da177e4SLinus Torvalds } else { 33353928d4f5SLinus Torvalds new_vma = vm_area_dup(vma); 3336e3975891SChen Gang if (!new_vma) 3337e3975891SChen Gang goto out; 33381da177e4SLinus Torvalds new_vma->vm_start = addr; 33391da177e4SLinus Torvalds new_vma->vm_end = addr + len; 33401da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 3341ef0855d3SOleg Nesterov if (vma_dup_policy(vma, new_vma)) 3342523d4e20SMichel Lespinasse goto out_free_vma; 3343523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 3344523d4e20SMichel Lespinasse goto out_free_mempol; 3345e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 33461da177e4SLinus Torvalds get_file(new_vma->vm_file); 33471da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 33481da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 3349524e00b3SLiam R. Howlett if (vma_link(mm, new_vma, prev)) 3350524e00b3SLiam R. Howlett goto out_vma_link; 335138a76013SMichel Lespinasse *need_rmap_locks = false; 33521da177e4SLinus Torvalds } 3353d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33541da177e4SLinus Torvalds return new_vma; 33555beb4930SRik van Riel 3356524e00b3SLiam R. Howlett out_vma_link: 3357524e00b3SLiam R. Howlett if (new_vma->vm_ops && new_vma->vm_ops->close) 3358524e00b3SLiam R. Howlett new_vma->vm_ops->close(new_vma); 33595beb4930SRik van Riel out_free_mempol: 3360ef0855d3SOleg Nesterov mpol_put(vma_policy(new_vma)); 33615beb4930SRik van Riel out_free_vma: 33623928d4f5SLinus Torvalds vm_area_free(new_vma); 3363e3975891SChen Gang out: 3364d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33655beb4930SRik van Riel return NULL; 33661da177e4SLinus Torvalds } 3367119f657cSakpm@osdl.org 3368119f657cSakpm@osdl.org /* 3369119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 3370119f657cSakpm@osdl.org * number of pages 3371119f657cSakpm@osdl.org */ 337284638335SKonstantin Khlebnikov bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) 3373119f657cSakpm@osdl.org { 337484638335SKonstantin Khlebnikov if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) 337584638335SKonstantin Khlebnikov return false; 3376119f657cSakpm@osdl.org 3377d977d56cSKonstantin Khlebnikov if (is_data_mapping(flags) && 3378d977d56cSKonstantin Khlebnikov mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { 3379f4fcd558SKonstantin Khlebnikov /* Workaround for Valgrind */ 3380f4fcd558SKonstantin Khlebnikov if (rlimit(RLIMIT_DATA) == 0 && 3381f4fcd558SKonstantin Khlebnikov mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) 3382f4fcd558SKonstantin Khlebnikov return true; 338357a7702bSDavid Woodhouse 338457a7702bSDavid Woodhouse pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", 3385d977d56cSKonstantin Khlebnikov current->comm, current->pid, 3386d977d56cSKonstantin Khlebnikov (mm->data_vm + npages) << PAGE_SHIFT, 338757a7702bSDavid Woodhouse rlimit(RLIMIT_DATA), 338857a7702bSDavid Woodhouse ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); 338957a7702bSDavid Woodhouse 339057a7702bSDavid Woodhouse if (!ignore_rlimit_data) 3391d977d56cSKonstantin Khlebnikov return false; 3392d977d56cSKonstantin Khlebnikov } 3393119f657cSakpm@osdl.org 339484638335SKonstantin Khlebnikov return true; 339584638335SKonstantin Khlebnikov } 339684638335SKonstantin Khlebnikov 339784638335SKonstantin Khlebnikov void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) 339884638335SKonstantin Khlebnikov { 33997866076bSPeng Liu WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); 340084638335SKonstantin Khlebnikov 3401d977d56cSKonstantin Khlebnikov if (is_exec_mapping(flags)) 340284638335SKonstantin Khlebnikov mm->exec_vm += npages; 3403d977d56cSKonstantin Khlebnikov else if (is_stack_mapping(flags)) 340484638335SKonstantin Khlebnikov mm->stack_vm += npages; 3405d977d56cSKonstantin Khlebnikov else if (is_data_mapping(flags)) 340684638335SKonstantin Khlebnikov mm->data_vm += npages; 3407119f657cSakpm@osdl.org } 3408fa5dc22fSRoland McGrath 3409b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf); 3410a62c34bdSAndy Lutomirski 3411a62c34bdSAndy Lutomirski /* 3412a62c34bdSAndy Lutomirski * Having a close hook prevents vma merging regardless of flags. 3413a62c34bdSAndy Lutomirski */ 3414a62c34bdSAndy Lutomirski static void special_mapping_close(struct vm_area_struct *vma) 3415a62c34bdSAndy Lutomirski { 3416a62c34bdSAndy Lutomirski } 3417a62c34bdSAndy Lutomirski 3418a62c34bdSAndy Lutomirski static const char *special_mapping_name(struct vm_area_struct *vma) 3419a62c34bdSAndy Lutomirski { 3420a62c34bdSAndy Lutomirski return ((struct vm_special_mapping *)vma->vm_private_data)->name; 3421a62c34bdSAndy Lutomirski } 3422a62c34bdSAndy Lutomirski 342314d07113SBrian Geffon static int special_mapping_mremap(struct vm_area_struct *new_vma) 3424b059a453SDmitry Safonov { 3425b059a453SDmitry Safonov struct vm_special_mapping *sm = new_vma->vm_private_data; 3426b059a453SDmitry Safonov 3427280e87e9SDmitry Safonov if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) 3428280e87e9SDmitry Safonov return -EFAULT; 3429280e87e9SDmitry Safonov 3430b059a453SDmitry Safonov if (sm->mremap) 3431b059a453SDmitry Safonov return sm->mremap(sm, new_vma); 3432280e87e9SDmitry Safonov 3433b059a453SDmitry Safonov return 0; 3434b059a453SDmitry Safonov } 3435b059a453SDmitry Safonov 3436871402e0SDmitry Safonov static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) 3437871402e0SDmitry Safonov { 3438871402e0SDmitry Safonov /* 3439871402e0SDmitry Safonov * Forbid splitting special mappings - kernel has expectations over 3440871402e0SDmitry Safonov * the number of pages in mapping. Together with VM_DONTEXPAND 3441871402e0SDmitry Safonov * the size of vma should stay the same over the special mapping's 3442871402e0SDmitry Safonov * lifetime. 3443871402e0SDmitry Safonov */ 3444871402e0SDmitry Safonov return -EINVAL; 3445871402e0SDmitry Safonov } 3446871402e0SDmitry Safonov 3447a62c34bdSAndy Lutomirski static const struct vm_operations_struct special_mapping_vmops = { 3448a62c34bdSAndy Lutomirski .close = special_mapping_close, 3449a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3450b059a453SDmitry Safonov .mremap = special_mapping_mremap, 3451a62c34bdSAndy Lutomirski .name = special_mapping_name, 3452af34ebebSDmitry Safonov /* vDSO code relies that VVAR can't be accessed remotely */ 3453af34ebebSDmitry Safonov .access = NULL, 3454871402e0SDmitry Safonov .may_split = special_mapping_split, 3455a62c34bdSAndy Lutomirski }; 3456a62c34bdSAndy Lutomirski 3457a62c34bdSAndy Lutomirski static const struct vm_operations_struct legacy_special_mapping_vmops = { 3458a62c34bdSAndy Lutomirski .close = special_mapping_close, 3459a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3460a62c34bdSAndy Lutomirski }; 3461fa5dc22fSRoland McGrath 3462b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf) 3463fa5dc22fSRoland McGrath { 346411bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 3465b1d0e4f5SNick Piggin pgoff_t pgoff; 3466fa5dc22fSRoland McGrath struct page **pages; 3467fa5dc22fSRoland McGrath 3468f872f540SAndy Lutomirski if (vma->vm_ops == &legacy_special_mapping_vmops) { 3469a62c34bdSAndy Lutomirski pages = vma->vm_private_data; 3470f872f540SAndy Lutomirski } else { 3471f872f540SAndy Lutomirski struct vm_special_mapping *sm = vma->vm_private_data; 3472f872f540SAndy Lutomirski 3473f872f540SAndy Lutomirski if (sm->fault) 347411bac800SDave Jiang return sm->fault(sm, vmf->vma, vmf); 3475f872f540SAndy Lutomirski 3476f872f540SAndy Lutomirski pages = sm->pages; 3477f872f540SAndy Lutomirski } 3478a62c34bdSAndy Lutomirski 34798a9cc3b5SOleg Nesterov for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) 3480b1d0e4f5SNick Piggin pgoff--; 3481fa5dc22fSRoland McGrath 3482fa5dc22fSRoland McGrath if (*pages) { 3483fa5dc22fSRoland McGrath struct page *page = *pages; 3484fa5dc22fSRoland McGrath get_page(page); 3485b1d0e4f5SNick Piggin vmf->page = page; 3486b1d0e4f5SNick Piggin return 0; 3487fa5dc22fSRoland McGrath } 3488fa5dc22fSRoland McGrath 3489b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 3490fa5dc22fSRoland McGrath } 3491fa5dc22fSRoland McGrath 3492a62c34bdSAndy Lutomirski static struct vm_area_struct *__install_special_mapping( 3493a62c34bdSAndy Lutomirski struct mm_struct *mm, 3494fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 349527f28b97SChen Gang unsigned long vm_flags, void *priv, 349627f28b97SChen Gang const struct vm_operations_struct *ops) 3497fa5dc22fSRoland McGrath { 3498462e635eSTavis Ormandy int ret; 3499fa5dc22fSRoland McGrath struct vm_area_struct *vma; 3500fa5dc22fSRoland McGrath 3501d4af56c5SLiam R. Howlett validate_mm_mt(mm); 3502490fc053SLinus Torvalds vma = vm_area_alloc(mm); 3503fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 35043935ed6aSStefani Seibold return ERR_PTR(-ENOMEM); 3505fa5dc22fSRoland McGrath 3506fa5dc22fSRoland McGrath vma->vm_start = addr; 3507fa5dc22fSRoland McGrath vma->vm_end = addr + len; 3508fa5dc22fSRoland McGrath 3509d9104d1cSCyrill Gorcunov vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY; 35101fc09228SHugh Dickins vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 35113ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 3512fa5dc22fSRoland McGrath 3513a62c34bdSAndy Lutomirski vma->vm_ops = ops; 3514a62c34bdSAndy Lutomirski vma->vm_private_data = priv; 3515fa5dc22fSRoland McGrath 3516462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 3517462e635eSTavis Ormandy if (ret) 3518462e635eSTavis Ormandy goto out; 3519fa5dc22fSRoland McGrath 352084638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); 3521fa5dc22fSRoland McGrath 3522cdd6c482SIngo Molnar perf_event_mmap(vma); 3523089dd79dSPeter Zijlstra 3524d4af56c5SLiam R. Howlett validate_mm_mt(mm); 35253935ed6aSStefani Seibold return vma; 3526462e635eSTavis Ormandy 3527462e635eSTavis Ormandy out: 35283928d4f5SLinus Torvalds vm_area_free(vma); 3529d4af56c5SLiam R. Howlett validate_mm_mt(mm); 35303935ed6aSStefani Seibold return ERR_PTR(ret); 35313935ed6aSStefani Seibold } 35323935ed6aSStefani Seibold 35332eefd878SDmitry Safonov bool vma_is_special_mapping(const struct vm_area_struct *vma, 35342eefd878SDmitry Safonov const struct vm_special_mapping *sm) 35352eefd878SDmitry Safonov { 35362eefd878SDmitry Safonov return vma->vm_private_data == sm && 35372eefd878SDmitry Safonov (vma->vm_ops == &special_mapping_vmops || 35382eefd878SDmitry Safonov vma->vm_ops == &legacy_special_mapping_vmops); 35392eefd878SDmitry Safonov } 35402eefd878SDmitry Safonov 3541a62c34bdSAndy Lutomirski /* 3542c1e8d7c6SMichel Lespinasse * Called with mm->mmap_lock held for writing. 3543a62c34bdSAndy Lutomirski * Insert a new vma covering the given region, with the given flags. 3544a62c34bdSAndy Lutomirski * Its pages are supplied by the given array of struct page *. 3545a62c34bdSAndy Lutomirski * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 3546a62c34bdSAndy Lutomirski * The region past the last page supplied will always produce SIGBUS. 3547a62c34bdSAndy Lutomirski * The array pointer and the pages it points to are assumed to stay alive 3548a62c34bdSAndy Lutomirski * for as long as this mapping might exist. 3549a62c34bdSAndy Lutomirski */ 3550a62c34bdSAndy Lutomirski struct vm_area_struct *_install_special_mapping( 3551a62c34bdSAndy Lutomirski struct mm_struct *mm, 3552a62c34bdSAndy Lutomirski unsigned long addr, unsigned long len, 3553a62c34bdSAndy Lutomirski unsigned long vm_flags, const struct vm_special_mapping *spec) 3554a62c34bdSAndy Lutomirski { 355527f28b97SChen Gang return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, 355627f28b97SChen Gang &special_mapping_vmops); 3557a62c34bdSAndy Lutomirski } 3558a62c34bdSAndy Lutomirski 35593935ed6aSStefani Seibold int install_special_mapping(struct mm_struct *mm, 35603935ed6aSStefani Seibold unsigned long addr, unsigned long len, 35613935ed6aSStefani Seibold unsigned long vm_flags, struct page **pages) 35623935ed6aSStefani Seibold { 3563a62c34bdSAndy Lutomirski struct vm_area_struct *vma = __install_special_mapping( 356427f28b97SChen Gang mm, addr, len, vm_flags, (void *)pages, 356527f28b97SChen Gang &legacy_special_mapping_vmops); 35663935ed6aSStefani Seibold 356714bd5b45SDuan Jiong return PTR_ERR_OR_ZERO(vma); 3568fa5dc22fSRoland McGrath } 35697906d00cSAndrea Arcangeli 35707906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 35717906d00cSAndrea Arcangeli 3572454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 35737906d00cSAndrea Arcangeli { 3574f808c13fSDavidlohr Bueso if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 35757906d00cSAndrea Arcangeli /* 35767906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 35777906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 35787906d00cSAndrea Arcangeli */ 3579da1c55f1SMichel Lespinasse down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 35807906d00cSAndrea Arcangeli /* 35817906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 35825a505085SIngo Molnar * anon_vma->root->rwsem. If some other vma in this mm shares 35837906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 35847906d00cSAndrea Arcangeli * 35857906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 35867906d00cSAndrea Arcangeli * can't change from under us thanks to the 35875a505085SIngo Molnar * anon_vma->root->rwsem. 35887906d00cSAndrea Arcangeli */ 35897906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 3590f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 35917906d00cSAndrea Arcangeli BUG(); 35927906d00cSAndrea Arcangeli } 35937906d00cSAndrea Arcangeli } 35947906d00cSAndrea Arcangeli 3595454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 35967906d00cSAndrea Arcangeli { 35977906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 35987906d00cSAndrea Arcangeli /* 35997906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 36007906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 36017906d00cSAndrea Arcangeli * 36027906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 36037906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 36047906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 36057906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 36067906d00cSAndrea Arcangeli */ 36077906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 36087906d00cSAndrea Arcangeli BUG(); 3609da1c55f1SMichel Lespinasse down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 36107906d00cSAndrea Arcangeli } 36117906d00cSAndrea Arcangeli } 36127906d00cSAndrea Arcangeli 36137906d00cSAndrea Arcangeli /* 36147906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 36157906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 36167906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 36177906d00cSAndrea Arcangeli * 3618c1e8d7c6SMichel Lespinasse * The caller must take the mmap_lock in write mode before calling 36197906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 3620c1e8d7c6SMichel Lespinasse * mmap_lock until mm_drop_all_locks() returns. 36217906d00cSAndrea Arcangeli * 3622c1e8d7c6SMichel Lespinasse * mmap_lock in write mode is required in order to block all operations 36237906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 362427ba0644SKirill A. Shutemov * altering the vma layout. It's also needed in write mode to avoid new 36257906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 36267906d00cSAndrea Arcangeli * 36277906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 36287906d00cSAndrea Arcangeli * or it would deadlock. 36297906d00cSAndrea Arcangeli * 3630bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 36317906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 36327906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 36337906d00cSAndrea Arcangeli * 363488f306b6SKirill A. Shutemov * We take locks in following order, accordingly to comment at beginning 363588f306b6SKirill A. Shutemov * of mm/rmap.c: 363688f306b6SKirill A. Shutemov * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 363788f306b6SKirill A. Shutemov * hugetlb mapping); 363888f306b6SKirill A. Shutemov * - all i_mmap_rwsem locks; 363988f306b6SKirill A. Shutemov * - all anon_vma->rwseml 364088f306b6SKirill A. Shutemov * 364188f306b6SKirill A. Shutemov * We can take all locks within these types randomly because the VM code 364288f306b6SKirill A. Shutemov * doesn't nest them and we protected from parallel mm_take_all_locks() by 364388f306b6SKirill A. Shutemov * mm_all_locks_mutex. 36447906d00cSAndrea Arcangeli * 36457906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 36467906d00cSAndrea Arcangeli * that may have to take thousand of locks. 36477906d00cSAndrea Arcangeli * 36487906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 36497906d00cSAndrea Arcangeli */ 36507906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 36517906d00cSAndrea Arcangeli { 36527906d00cSAndrea Arcangeli struct vm_area_struct *vma; 36535beb4930SRik van Riel struct anon_vma_chain *avc; 36547906d00cSAndrea Arcangeli 3655325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 36567906d00cSAndrea Arcangeli 36577906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 36587906d00cSAndrea Arcangeli 36597906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 36607906d00cSAndrea Arcangeli if (signal_pending(current)) 36617906d00cSAndrea Arcangeli goto out_unlock; 366288f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 366388f306b6SKirill A. Shutemov is_vm_hugetlb_page(vma)) 366488f306b6SKirill A. Shutemov vm_lock_mapping(mm, vma->vm_file->f_mapping); 366588f306b6SKirill A. Shutemov } 366688f306b6SKirill A. Shutemov 366788f306b6SKirill A. Shutemov for (vma = mm->mmap; vma; vma = vma->vm_next) { 366888f306b6SKirill A. Shutemov if (signal_pending(current)) 366988f306b6SKirill A. Shutemov goto out_unlock; 367088f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 367188f306b6SKirill A. Shutemov !is_vm_hugetlb_page(vma)) 3672454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 36737906d00cSAndrea Arcangeli } 36747cd5a02fSPeter Zijlstra 36757cd5a02fSPeter Zijlstra for (vma = mm->mmap; vma; vma = vma->vm_next) { 36767cd5a02fSPeter Zijlstra if (signal_pending(current)) 36777cd5a02fSPeter Zijlstra goto out_unlock; 36787cd5a02fSPeter Zijlstra if (vma->anon_vma) 36795beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 36805beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 36817cd5a02fSPeter Zijlstra } 36827cd5a02fSPeter Zijlstra 3683584cff54SKautuk Consul return 0; 36847906d00cSAndrea Arcangeli 36857906d00cSAndrea Arcangeli out_unlock: 36867906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 3687584cff54SKautuk Consul return -EINTR; 36887906d00cSAndrea Arcangeli } 36897906d00cSAndrea Arcangeli 36907906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 36917906d00cSAndrea Arcangeli { 3692f808c13fSDavidlohr Bueso if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 36937906d00cSAndrea Arcangeli /* 36947906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 36957906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 36967906d00cSAndrea Arcangeli * 36977906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 3698bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 36997906d00cSAndrea Arcangeli * never see our bitflag. 37007906d00cSAndrea Arcangeli * 37017906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 37027906d00cSAndrea Arcangeli * can't change from under us until we release the 37035a505085SIngo Molnar * anon_vma->root->rwsem. 37047906d00cSAndrea Arcangeli */ 37057906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 3706f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 37077906d00cSAndrea Arcangeli BUG(); 370808b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 37097906d00cSAndrea Arcangeli } 37107906d00cSAndrea Arcangeli } 37117906d00cSAndrea Arcangeli 37127906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 37137906d00cSAndrea Arcangeli { 37147906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 37157906d00cSAndrea Arcangeli /* 37167906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 37177906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 37187906d00cSAndrea Arcangeli */ 371983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 37207906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 37217906d00cSAndrea Arcangeli &mapping->flags)) 37227906d00cSAndrea Arcangeli BUG(); 37237906d00cSAndrea Arcangeli } 37247906d00cSAndrea Arcangeli } 37257906d00cSAndrea Arcangeli 37267906d00cSAndrea Arcangeli /* 3727c1e8d7c6SMichel Lespinasse * The mmap_lock cannot be released by the caller until 37287906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 37297906d00cSAndrea Arcangeli */ 37307906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 37317906d00cSAndrea Arcangeli { 37327906d00cSAndrea Arcangeli struct vm_area_struct *vma; 37335beb4930SRik van Riel struct anon_vma_chain *avc; 37347906d00cSAndrea Arcangeli 3735325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 37367906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 37377906d00cSAndrea Arcangeli 37387906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 37397906d00cSAndrea Arcangeli if (vma->anon_vma) 37405beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 37415beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 37427906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 37437906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 37447906d00cSAndrea Arcangeli } 37457906d00cSAndrea Arcangeli 37467906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 37477906d00cSAndrea Arcangeli } 37488feae131SDavid Howells 37498feae131SDavid Howells /* 37503edf41d8Sseokhoon.yoon * initialise the percpu counter for VM 37518feae131SDavid Howells */ 37528feae131SDavid Howells void __init mmap_init(void) 37538feae131SDavid Howells { 375400a62ce9SKOSAKI Motohiro int ret; 375500a62ce9SKOSAKI Motohiro 3756908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 375700a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 37588feae131SDavid Howells } 3759c9b1d098SAndrew Shewmaker 3760c9b1d098SAndrew Shewmaker /* 3761c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 3762c9b1d098SAndrew Shewmaker * 3763c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 3764c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 3765c9b1d098SAndrew Shewmaker * mode. 3766c9b1d098SAndrew Shewmaker * 3767c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 3768c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 3769c9b1d098SAndrew Shewmaker */ 37701640879aSAndrew Shewmaker static int init_user_reserve(void) 3771c9b1d098SAndrew Shewmaker { 3772c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 3773c9b1d098SAndrew Shewmaker 3774c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 3775c9b1d098SAndrew Shewmaker 3776c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 3777c9b1d098SAndrew Shewmaker return 0; 3778c9b1d098SAndrew Shewmaker } 3779a64fb3cdSPaul Gortmaker subsys_initcall(init_user_reserve); 37804eeab4f5SAndrew Shewmaker 37814eeab4f5SAndrew Shewmaker /* 37824eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 37834eeab4f5SAndrew Shewmaker * 37844eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 37854eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 37864eeab4f5SAndrew Shewmaker * 37874eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 37884eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 37894eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 37904eeab4f5SAndrew Shewmaker */ 37911640879aSAndrew Shewmaker static int init_admin_reserve(void) 37924eeab4f5SAndrew Shewmaker { 37934eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 37944eeab4f5SAndrew Shewmaker 3795c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37964eeab4f5SAndrew Shewmaker 37974eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 37984eeab4f5SAndrew Shewmaker return 0; 37994eeab4f5SAndrew Shewmaker } 3800a64fb3cdSPaul Gortmaker subsys_initcall(init_admin_reserve); 38011640879aSAndrew Shewmaker 38021640879aSAndrew Shewmaker /* 38031640879aSAndrew Shewmaker * Reinititalise user and admin reserves if memory is added or removed. 38041640879aSAndrew Shewmaker * 38051640879aSAndrew Shewmaker * The default user reserve max is 128MB, and the default max for the 38061640879aSAndrew Shewmaker * admin reserve is 8MB. These are usually, but not always, enough to 38071640879aSAndrew Shewmaker * enable recovery from a memory hogging process using login/sshd, a shell, 38081640879aSAndrew Shewmaker * and tools like top. It may make sense to increase or even disable the 38091640879aSAndrew Shewmaker * reserve depending on the existence of swap or variations in the recovery 38101640879aSAndrew Shewmaker * tools. So, the admin may have changed them. 38111640879aSAndrew Shewmaker * 38121640879aSAndrew Shewmaker * If memory is added and the reserves have been eliminated or increased above 38131640879aSAndrew Shewmaker * the default max, then we'll trust the admin. 38141640879aSAndrew Shewmaker * 38151640879aSAndrew Shewmaker * If memory is removed and there isn't enough free memory, then we 38161640879aSAndrew Shewmaker * need to reset the reserves. 38171640879aSAndrew Shewmaker * 38181640879aSAndrew Shewmaker * Otherwise keep the reserve set by the admin. 38191640879aSAndrew Shewmaker */ 38201640879aSAndrew Shewmaker static int reserve_mem_notifier(struct notifier_block *nb, 38211640879aSAndrew Shewmaker unsigned long action, void *data) 38221640879aSAndrew Shewmaker { 38231640879aSAndrew Shewmaker unsigned long tmp, free_kbytes; 38241640879aSAndrew Shewmaker 38251640879aSAndrew Shewmaker switch (action) { 38261640879aSAndrew Shewmaker case MEM_ONLINE: 38271640879aSAndrew Shewmaker /* Default max is 128MB. Leave alone if modified by operator. */ 38281640879aSAndrew Shewmaker tmp = sysctl_user_reserve_kbytes; 38291640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 17)) 38301640879aSAndrew Shewmaker init_user_reserve(); 38311640879aSAndrew Shewmaker 38321640879aSAndrew Shewmaker /* Default max is 8MB. Leave alone if modified by operator. */ 38331640879aSAndrew Shewmaker tmp = sysctl_admin_reserve_kbytes; 38341640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 13)) 38351640879aSAndrew Shewmaker init_admin_reserve(); 38361640879aSAndrew Shewmaker 38371640879aSAndrew Shewmaker break; 38381640879aSAndrew Shewmaker case MEM_OFFLINE: 3839c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 38401640879aSAndrew Shewmaker 38411640879aSAndrew Shewmaker if (sysctl_user_reserve_kbytes > free_kbytes) { 38421640879aSAndrew Shewmaker init_user_reserve(); 38431640879aSAndrew Shewmaker pr_info("vm.user_reserve_kbytes reset to %lu\n", 38441640879aSAndrew Shewmaker sysctl_user_reserve_kbytes); 38451640879aSAndrew Shewmaker } 38461640879aSAndrew Shewmaker 38471640879aSAndrew Shewmaker if (sysctl_admin_reserve_kbytes > free_kbytes) { 38481640879aSAndrew Shewmaker init_admin_reserve(); 38491640879aSAndrew Shewmaker pr_info("vm.admin_reserve_kbytes reset to %lu\n", 38501640879aSAndrew Shewmaker sysctl_admin_reserve_kbytes); 38511640879aSAndrew Shewmaker } 38521640879aSAndrew Shewmaker break; 38531640879aSAndrew Shewmaker default: 38541640879aSAndrew Shewmaker break; 38551640879aSAndrew Shewmaker } 38561640879aSAndrew Shewmaker return NOTIFY_OK; 38571640879aSAndrew Shewmaker } 38581640879aSAndrew Shewmaker 38591640879aSAndrew Shewmaker static struct notifier_block reserve_mem_nb = { 38601640879aSAndrew Shewmaker .notifier_call = reserve_mem_notifier, 38611640879aSAndrew Shewmaker }; 38621640879aSAndrew Shewmaker 38631640879aSAndrew Shewmaker static int __meminit init_reserve_notifier(void) 38641640879aSAndrew Shewmaker { 38651640879aSAndrew Shewmaker if (register_hotmemory_notifier(&reserve_mem_nb)) 3866b1de0d13SMitchel Humpherys pr_err("Failed registering memory add/remove notifier for admin reserve\n"); 38671640879aSAndrew Shewmaker 38681640879aSAndrew Shewmaker return 0; 38691640879aSAndrew Shewmaker } 3870a64fb3cdSPaul Gortmaker subsys_initcall(init_reserve_notifier); 3871