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> 171da177e4SLinus Torvalds #include <linux/shm.h> 181da177e4SLinus Torvalds #include <linux/mman.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/swap.h> 211da177e4SLinus Torvalds #include <linux/syscalls.h> 22c59ede7bSRandy.Dunlap #include <linux/capability.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/file.h> 251da177e4SLinus Torvalds #include <linux/fs.h> 261da177e4SLinus Torvalds #include <linux/personality.h> 271da177e4SLinus Torvalds #include <linux/security.h> 281da177e4SLinus Torvalds #include <linux/hugetlb.h> 29c01d5b30SHugh Dickins #include <linux/shmem_fs.h> 301da177e4SLinus Torvalds #include <linux/profile.h> 31b95f1b31SPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/mount.h> 331da177e4SLinus Torvalds #include <linux/mempolicy.h> 341da177e4SLinus Torvalds #include <linux/rmap.h> 35cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 3682f71ae4SKonstantin Khlebnikov #include <linux/mmdebug.h> 37cdd6c482SIngo Molnar #include <linux/perf_event.h> 38120a795dSAl Viro #include <linux/audit.h> 39b15d00b6SAndrea Arcangeli #include <linux/khugepaged.h> 402b144498SSrikar Dronamraju #include <linux/uprobes.h> 411640879aSAndrew Shewmaker #include <linux/notifier.h> 421640879aSAndrew Shewmaker #include <linux/memory.h> 43b1de0d13SMitchel Humpherys #include <linux/printk.h> 4419a809afSAndrea Arcangeli #include <linux/userfaultfd_k.h> 45d977d56cSKonstantin Khlebnikov #include <linux/moduleparam.h> 4662b5f7d0SDave Hansen #include <linux/pkeys.h> 4721292580SAndrea Arcangeli #include <linux/oom.h> 4804f5866eSAndrea Arcangeli #include <linux/sched/mm.h> 491da177e4SLinus Torvalds 507c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 511da177e4SLinus Torvalds #include <asm/cacheflush.h> 521da177e4SLinus Torvalds #include <asm/tlb.h> 53d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h> 541da177e4SLinus Torvalds 55df529cabSJaewon Kim #define CREATE_TRACE_POINTS 56df529cabSJaewon Kim #include <trace/events/mmap.h> 57df529cabSJaewon Kim 5842b77728SJan Beulich #include "internal.h" 5942b77728SJan Beulich 603a459756SKirill Korotaev #ifndef arch_mmap_check 613a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags) (0) 623a459756SKirill Korotaev #endif 633a459756SKirill Korotaev 64d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS 65d07e2259SDaniel Cashman const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; 66d07e2259SDaniel Cashman const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; 67d07e2259SDaniel Cashman int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; 68d07e2259SDaniel Cashman #endif 69d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS 70d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; 71d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; 72d07e2259SDaniel Cashman int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; 73d07e2259SDaniel Cashman #endif 74d07e2259SDaniel Cashman 75f4fcd558SKonstantin Khlebnikov static bool ignore_rlimit_data; 76d977d56cSKonstantin Khlebnikov core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); 77d07e2259SDaniel Cashman 78*763ecb03SLiam R. Howlett static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, 79e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 80*763ecb03SLiam R. Howlett struct vm_area_struct *next, unsigned long start, 81*763ecb03SLiam R. Howlett 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 /* 134*763ecb03SLiam R. Howlett * Close a vm structure and free it. 135a8fb5618SHugh Dickins */ 136*763ecb03SLiam R. Howlett static void remove_vma(struct vm_area_struct *vma) 137a8fb5618SHugh Dickins { 138a8fb5618SHugh Dickins might_sleep(); 1391da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 1401da177e4SLinus Torvalds vma->vm_ops->close(vma); 141e9714acfSKonstantin Khlebnikov if (vma->vm_file) 142a8fb5618SHugh Dickins fput(vma->vm_file); 143f0be3d32SLee Schermerhorn mpol_put(vma_policy(vma)); 1443928d4f5SLinus Torvalds vm_area_free(vma); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1472e7ce7d3SLiam R. Howlett /* 1482e7ce7d3SLiam R. Howlett * check_brk_limits() - Use platform specific check of range & verify mlock 1492e7ce7d3SLiam R. Howlett * limits. 1502e7ce7d3SLiam R. Howlett * @addr: The address to check 1512e7ce7d3SLiam R. Howlett * @len: The size of increase. 1522e7ce7d3SLiam R. Howlett * 1532e7ce7d3SLiam R. Howlett * Return: 0 on success. 1542e7ce7d3SLiam R. Howlett */ 1552e7ce7d3SLiam R. Howlett static int check_brk_limits(unsigned long addr, unsigned long len) 1562e7ce7d3SLiam R. Howlett { 1572e7ce7d3SLiam R. Howlett unsigned long mapped_addr; 1582e7ce7d3SLiam R. Howlett 1592e7ce7d3SLiam R. Howlett mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 1602e7ce7d3SLiam R. Howlett if (IS_ERR_VALUE(mapped_addr)) 1612e7ce7d3SLiam R. Howlett return mapped_addr; 1622e7ce7d3SLiam R. Howlett 1632e7ce7d3SLiam R. Howlett return mlock_future_check(current->mm, current->mm->def_flags, len); 1642e7ce7d3SLiam R. Howlett } 1652e7ce7d3SLiam R. Howlett static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, 1662e7ce7d3SLiam R. Howlett unsigned long newbrk, unsigned long oldbrk, 167bb177a73SMichal Hocko struct list_head *uf); 1682e7ce7d3SLiam R. Howlett static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *brkvma, 169*763ecb03SLiam R. Howlett unsigned long addr, unsigned long request, unsigned long flags); 1706a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 1711da177e4SLinus Torvalds { 1729bc8039eSYang Shi unsigned long newbrk, oldbrk, origbrk; 1731da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1742e7ce7d3SLiam R. Howlett struct vm_area_struct *brkvma, *next = NULL; 175a5b4592cSJiri Kosina unsigned long min_brk; 176128557ffSMichel Lespinasse bool populate; 1779bc8039eSYang Shi bool downgraded = false; 178897ab3e0SMike Rapoport LIST_HEAD(uf); 1792e7ce7d3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 1801da177e4SLinus Torvalds 181d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 182dc0ef0dfSMichal Hocko return -EINTR; 1831da177e4SLinus Torvalds 1849bc8039eSYang Shi origbrk = mm->brk; 1859bc8039eSYang Shi 186a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 1875520e894SJiri Kosina /* 1885520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 1895520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 1905520e894SJiri Kosina * to be arbitrarily shifted 1915520e894SJiri Kosina */ 1924471a675SJiri Kosina if (current->brk_randomized) 1935520e894SJiri Kosina min_brk = mm->start_brk; 1945520e894SJiri Kosina else 1955520e894SJiri Kosina min_brk = mm->end_data; 196a5b4592cSJiri Kosina #else 197a5b4592cSJiri Kosina min_brk = mm->start_brk; 198a5b4592cSJiri Kosina #endif 199a5b4592cSJiri Kosina if (brk < min_brk) 2001da177e4SLinus Torvalds goto out; 2011e624196SRam Gupta 2021e624196SRam Gupta /* 2031e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2041e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2051e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2061e624196SRam Gupta * not page aligned -Ram Gupta 2071e624196SRam Gupta */ 2088764b338SCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, 2098764b338SCyrill Gorcunov mm->end_data, mm->start_data)) 2101e624196SRam Gupta goto out; 2111e624196SRam Gupta 2121da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2131da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2149bc8039eSYang Shi if (oldbrk == newbrk) { 2159bc8039eSYang Shi mm->brk = brk; 2169bc8039eSYang Shi goto success; 2179bc8039eSYang Shi } 2181da177e4SLinus Torvalds 2199bc8039eSYang Shi /* 2209bc8039eSYang Shi * Always allow shrinking brk. 2212e7ce7d3SLiam R. Howlett * do_brk_munmap() may downgrade mmap_lock to read. 2229bc8039eSYang Shi */ 2231da177e4SLinus Torvalds if (brk <= mm->brk) { 2249bc8039eSYang Shi int ret; 2259bc8039eSYang Shi 2262e7ce7d3SLiam R. Howlett /* Search one past newbrk */ 2272e7ce7d3SLiam R. Howlett mas_set(&mas, newbrk); 2282e7ce7d3SLiam R. Howlett brkvma = mas_find(&mas, oldbrk); 2292e7ce7d3SLiam R. Howlett BUG_ON(brkvma == NULL); 2302e7ce7d3SLiam R. Howlett if (brkvma->vm_start >= oldbrk) 2312e7ce7d3SLiam R. Howlett goto out; /* mapping intersects with an existing non-brk vma. */ 2329bc8039eSYang Shi /* 2332e7ce7d3SLiam R. Howlett * mm->brk must be protected by write mmap_lock. 2342e7ce7d3SLiam R. Howlett * do_brk_munmap() may downgrade the lock, so update it 2352e7ce7d3SLiam R. Howlett * before calling do_brk_munmap(). 2369bc8039eSYang Shi */ 2379bc8039eSYang Shi mm->brk = brk; 2382e7ce7d3SLiam R. Howlett ret = do_brk_munmap(&mas, brkvma, newbrk, oldbrk, &uf); 2392e7ce7d3SLiam R. Howlett if (ret == 1) { 2402e7ce7d3SLiam R. Howlett downgraded = true; 2412e7ce7d3SLiam R. Howlett goto success; 2422e7ce7d3SLiam R. Howlett } else if (!ret) 2432e7ce7d3SLiam R. Howlett goto success; 2442e7ce7d3SLiam R. Howlett 2459bc8039eSYang Shi mm->brk = origbrk; 2461da177e4SLinus Torvalds goto out; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2492e7ce7d3SLiam R. Howlett if (check_brk_limits(oldbrk, newbrk - oldbrk)) 2502e7ce7d3SLiam R. Howlett goto out; 2512e7ce7d3SLiam R. Howlett 2522e7ce7d3SLiam R. Howlett /* 2532e7ce7d3SLiam R. Howlett * Only check if the next VMA is within the stack_guard_gap of the 2542e7ce7d3SLiam R. Howlett * expansion area 2552e7ce7d3SLiam R. Howlett */ 2562e7ce7d3SLiam R. Howlett mas_set(&mas, oldbrk); 2572e7ce7d3SLiam R. Howlett next = mas_find(&mas, newbrk - 1 + PAGE_SIZE + stack_guard_gap); 2581be7107fSHugh Dickins if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) 2591da177e4SLinus Torvalds goto out; 2601da177e4SLinus Torvalds 2612e7ce7d3SLiam R. Howlett brkvma = mas_prev(&mas, mm->start_brk); 2621da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 2632e7ce7d3SLiam R. Howlett if (do_brk_flags(&mas, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) 2641da177e4SLinus Torvalds goto out; 2652e7ce7d3SLiam R. Howlett 2661da177e4SLinus Torvalds mm->brk = brk; 2679bc8039eSYang Shi 2689bc8039eSYang Shi success: 269128557ffSMichel Lespinasse populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; 2709bc8039eSYang Shi if (downgraded) 271d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2729bc8039eSYang Shi else 273d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 274897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 275128557ffSMichel Lespinasse if (populate) 276128557ffSMichel Lespinasse mm_populate(oldbrk, newbrk - oldbrk); 277128557ffSMichel Lespinasse return brk; 278128557ffSMichel Lespinasse 2791da177e4SLinus Torvalds out: 280d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 281b7204006SAdrian Huang return origbrk; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 284d4af56c5SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 285d4af56c5SLiam R. Howlett extern void mt_validate(struct maple_tree *mt); 286d4af56c5SLiam R. Howlett extern void mt_dump(const struct maple_tree *mt); 2871da177e4SLinus Torvalds 288d4af56c5SLiam R. Howlett /* Validate the maple tree */ 289d4af56c5SLiam R. Howlett static void validate_mm_mt(struct mm_struct *mm) 290d4af56c5SLiam R. Howlett { 291d4af56c5SLiam R. Howlett struct maple_tree *mt = &mm->mm_mt; 292*763ecb03SLiam R. Howlett struct vm_area_struct *vma_mt; 293d4af56c5SLiam R. Howlett 294d4af56c5SLiam R. Howlett MA_STATE(mas, mt, 0, 0); 295d4af56c5SLiam R. Howlett 296d4af56c5SLiam R. Howlett mt_validate(&mm->mm_mt); 297d4af56c5SLiam R. Howlett mas_for_each(&mas, vma_mt, ULONG_MAX) { 298*763ecb03SLiam R. Howlett if ((vma_mt->vm_start != mas.index) || 299*763ecb03SLiam R. Howlett (vma_mt->vm_end - 1 != mas.last)) { 300d4af56c5SLiam R. Howlett pr_emerg("issue in %s\n", current->comm); 301d4af56c5SLiam R. Howlett dump_stack(); 302d4af56c5SLiam R. Howlett dump_vma(vma_mt); 303d4af56c5SLiam R. Howlett pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, 304d4af56c5SLiam R. Howlett mas.index, mas.last); 305d4af56c5SLiam R. Howlett pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, 306d4af56c5SLiam R. Howlett vma_mt->vm_start, vma_mt->vm_end); 307d4af56c5SLiam R. Howlett 308d4af56c5SLiam R. Howlett mt_dump(mas.tree); 309d4af56c5SLiam R. Howlett if (vma_mt->vm_end != mas.last + 1) { 310d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", 311d4af56c5SLiam R. Howlett mm, vma_mt->vm_start, vma_mt->vm_end, 312d4af56c5SLiam R. Howlett mas.index, mas.last); 313d4af56c5SLiam R. Howlett mt_dump(mas.tree); 314d4af56c5SLiam R. Howlett } 315d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); 316d4af56c5SLiam R. Howlett if (vma_mt->vm_start != mas.index) { 317d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", 318d4af56c5SLiam R. Howlett mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); 319d4af56c5SLiam R. Howlett mt_dump(mas.tree); 320d4af56c5SLiam R. Howlett } 321d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); 322d4af56c5SLiam R. Howlett } 323d4af56c5SLiam R. Howlett } 324d4af56c5SLiam R. Howlett } 3251da177e4SLinus Torvalds 326eafd4dc4SRashika Kheria static void validate_mm(struct mm_struct *mm) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds int bug = 0; 3291da177e4SLinus Torvalds int i = 0; 330*763ecb03SLiam R. Howlett struct vm_area_struct *vma; 331*763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 332ff26f70fSAndrew Morton 333524e00b3SLiam R. Howlett validate_mm_mt(mm); 334524e00b3SLiam R. Howlett 335*763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 336524e00b3SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 33712352d3cSKonstantin Khlebnikov struct anon_vma *anon_vma = vma->anon_vma; 338ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 339ff26f70fSAndrew Morton 34012352d3cSKonstantin Khlebnikov if (anon_vma) { 34112352d3cSKonstantin Khlebnikov anon_vma_lock_read(anon_vma); 342ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 343ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 34412352d3cSKonstantin Khlebnikov anon_vma_unlock_read(anon_vma); 34512352d3cSKonstantin Khlebnikov } 346524e00b3SLiam R. Howlett #endif 3471da177e4SLinus Torvalds i++; 3481da177e4SLinus Torvalds } 3495a0768f6SMichel Lespinasse if (i != mm->map_count) { 350*763ecb03SLiam R. Howlett pr_emerg("map_count %d mas_for_each %d\n", mm->map_count, i); 3515a0768f6SMichel Lespinasse bug = 1; 3525a0768f6SMichel Lespinasse } 35396dad67fSSasha Levin VM_BUG_ON_MM(bug, mm); 3541da177e4SLinus Torvalds } 355524e00b3SLiam R. Howlett 356524e00b3SLiam R. Howlett #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ 357d4af56c5SLiam R. Howlett #define validate_mm_mt(root) do { } while (0) 3581da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 359524e00b3SLiam R. Howlett #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 360d3737187SMichel Lespinasse 361bf181b9fSMichel Lespinasse /* 362bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 363bf181b9fSMichel Lespinasse * anon_vma's interval trees. 364bf181b9fSMichel Lespinasse * 365bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 366bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 367bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 368bf181b9fSMichel Lespinasse * 369bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 370bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 371bf181b9fSMichel Lespinasse * 372c1e8d7c6SMichel Lespinasse * The entire update must be protected by exclusive mmap_lock and by 373bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 374bf181b9fSMichel Lespinasse */ 375bf181b9fSMichel Lespinasse static inline void 376bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 377bf181b9fSMichel Lespinasse { 378bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 379bf181b9fSMichel Lespinasse 380bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 381bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 382bf181b9fSMichel Lespinasse } 383bf181b9fSMichel Lespinasse 384bf181b9fSMichel Lespinasse static inline void 385bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 386bf181b9fSMichel Lespinasse { 387bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 388bf181b9fSMichel Lespinasse 389bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 390bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 391bf181b9fSMichel Lespinasse } 392bf181b9fSMichel Lespinasse 393524e00b3SLiam R. Howlett /* 394524e00b3SLiam R. Howlett * range_has_overlap() - Check the @start - @end range for overlapping VMAs and 395524e00b3SLiam R. Howlett * sets up a pointer to the previous VMA 396524e00b3SLiam R. Howlett * @mm: the mm struct 397524e00b3SLiam R. Howlett * @start: the start address of the range 398524e00b3SLiam R. Howlett * @end: the end address of the range 399524e00b3SLiam R. Howlett * @pprev: the pointer to the pointer of the previous VMA 400524e00b3SLiam R. Howlett * 401524e00b3SLiam R. Howlett * Returns: True if there is an overlapping VMA, false otherwise 402524e00b3SLiam R. Howlett */ 403524e00b3SLiam R. Howlett static inline 404524e00b3SLiam R. Howlett bool range_has_overlap(struct mm_struct *mm, unsigned long start, 405524e00b3SLiam R. Howlett unsigned long end, struct vm_area_struct **pprev) 4061da177e4SLinus Torvalds { 407524e00b3SLiam R. Howlett struct vm_area_struct *existing; 4081da177e4SLinus Torvalds 409524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 410*763ecb03SLiam R. Howlett rcu_read_lock(); 411524e00b3SLiam R. Howlett existing = mas_find(&mas, end - 1); 412524e00b3SLiam R. Howlett *pprev = mas_prev(&mas, 0); 413*763ecb03SLiam R. Howlett rcu_read_unlock(); 414524e00b3SLiam R. Howlett return existing ? true : false; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 417e8420a8eSCyril Hrubis static unsigned long count_vma_pages_range(struct mm_struct *mm, 418e8420a8eSCyril Hrubis unsigned long addr, unsigned long end) 419e8420a8eSCyril Hrubis { 4202e3af1dbSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, addr); 421e8420a8eSCyril Hrubis struct vm_area_struct *vma; 4222e3af1dbSMatthew Wilcox (Oracle) unsigned long nr_pages = 0; 423e8420a8eSCyril Hrubis 4242e3af1dbSMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) { 4252e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_start = max(addr, vma->vm_start); 4262e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_end = min(end, vma->vm_end); 427e8420a8eSCyril Hrubis 4282e3af1dbSMatthew Wilcox (Oracle) nr_pages += PHYS_PFN(vm_end - vm_start); 429e8420a8eSCyril Hrubis } 430e8420a8eSCyril Hrubis 431e8420a8eSCyril Hrubis return nr_pages; 432e8420a8eSCyril Hrubis } 433e8420a8eSCyril Hrubis 434cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds struct file *file; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds file = vma->vm_file; 4391da177e4SLinus Torvalds if (file) { 4401da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 443cf508b58SMiaohe Lin mapping_allow_writable(mapping); 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 4466b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 4471da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 451d4af56c5SLiam R. Howlett /* 452d4af56c5SLiam R. Howlett * vma_mas_store() - Store a VMA in the maple tree. 453d4af56c5SLiam R. Howlett * @vma: The vm_area_struct 454d4af56c5SLiam R. Howlett * @mas: The maple state 455d4af56c5SLiam R. Howlett * 456d4af56c5SLiam R. Howlett * Efficient way to store a VMA in the maple tree when the @mas has already 457d4af56c5SLiam R. Howlett * walked to the correct location. 458d4af56c5SLiam R. Howlett * 459d4af56c5SLiam R. Howlett * Note: the end address is inclusive in the maple tree. 460d4af56c5SLiam R. Howlett */ 461d4af56c5SLiam R. Howlett void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas) 462d4af56c5SLiam R. Howlett { 463d4af56c5SLiam R. Howlett trace_vma_store(mas->tree, vma); 464d4af56c5SLiam R. Howlett mas_set_range(mas, vma->vm_start, vma->vm_end - 1); 465d4af56c5SLiam R. Howlett mas_store_prealloc(mas, vma); 466d4af56c5SLiam R. Howlett } 467d4af56c5SLiam R. Howlett 468d4af56c5SLiam R. Howlett /* 469d4af56c5SLiam R. Howlett * vma_mas_remove() - Remove a VMA from the maple tree. 470d4af56c5SLiam R. Howlett * @vma: The vm_area_struct 471d4af56c5SLiam R. Howlett * @mas: The maple state 472d4af56c5SLiam R. Howlett * 473d4af56c5SLiam R. Howlett * Efficient way to remove a VMA from the maple tree when the @mas has already 474d4af56c5SLiam R. Howlett * been established and points to the correct location. 475d4af56c5SLiam R. Howlett * Note: the end address is inclusive in the maple tree. 476d4af56c5SLiam R. Howlett */ 477d4af56c5SLiam R. Howlett void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas) 478d4af56c5SLiam R. Howlett { 479d4af56c5SLiam R. Howlett trace_vma_mas_szero(mas->tree, vma->vm_start, vma->vm_end - 1); 480d4af56c5SLiam R. Howlett mas->index = vma->vm_start; 481d4af56c5SLiam R. Howlett mas->last = vma->vm_end - 1; 482d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 483d4af56c5SLiam R. Howlett } 484d4af56c5SLiam R. Howlett 485d4af56c5SLiam R. Howlett /* 486d4af56c5SLiam R. Howlett * vma_mas_szero() - Set a given range to zero. Used when modifying a 487d4af56c5SLiam R. Howlett * vm_area_struct start or end. 488d4af56c5SLiam R. Howlett * 489d4af56c5SLiam R. Howlett * @mm: The struct_mm 490d4af56c5SLiam R. Howlett * @start: The start address to zero 491d4af56c5SLiam R. Howlett * @end: The end address to zero. 492d4af56c5SLiam R. Howlett */ 493d4af56c5SLiam R. Howlett static inline void vma_mas_szero(struct ma_state *mas, unsigned long start, 494d4af56c5SLiam R. Howlett unsigned long end) 495d4af56c5SLiam R. Howlett { 496d4af56c5SLiam R. Howlett trace_vma_mas_szero(mas->tree, start, end - 1); 497d4af56c5SLiam R. Howlett mas_set_range(mas, start, end - 1); 498d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 499d4af56c5SLiam R. Howlett } 500d4af56c5SLiam R. Howlett 501*763ecb03SLiam R. Howlett static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) 5021da177e4SLinus Torvalds { 503d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 5041da177e4SLinus Torvalds struct address_space *mapping = NULL; 5051da177e4SLinus Torvalds 506d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 507d4af56c5SLiam R. Howlett return -ENOMEM; 508d4af56c5SLiam R. Howlett 50964ac4940SHuang Shijie if (vma->vm_file) { 5101da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 51183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 51264ac4940SHuang Shijie } 5131da177e4SLinus Torvalds 514d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 5151da177e4SLinus Torvalds __vma_link_file(vma); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds if (mapping) 51883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds mm->map_count++; 5211da177e4SLinus Torvalds validate_mm(mm); 522d4af56c5SLiam R. Howlett return 0; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds /* 5264dd1b841SLiam R. Howlett * vma_expand - Expand an existing VMA 5274dd1b841SLiam R. Howlett * 5284dd1b841SLiam R. Howlett * @mas: The maple state 5294dd1b841SLiam R. Howlett * @vma: The vma to expand 5304dd1b841SLiam R. Howlett * @start: The start of the vma 5314dd1b841SLiam R. Howlett * @end: The exclusive end of the vma 5324dd1b841SLiam R. Howlett * @pgoff: The page offset of vma 5334dd1b841SLiam R. Howlett * @next: The current of next vma. 5344dd1b841SLiam R. Howlett * 5354dd1b841SLiam R. Howlett * Expand @vma to @start and @end. Can expand off the start and end. Will 5364dd1b841SLiam R. Howlett * expand over @next if it's different from @vma and @end == @next->vm_end. 5374dd1b841SLiam R. Howlett * Checking if the @vma can expand and merge with @next needs to be handled by 5384dd1b841SLiam R. Howlett * the caller. 5394dd1b841SLiam R. Howlett * 5404dd1b841SLiam R. Howlett * Returns: 0 on success 5414dd1b841SLiam R. Howlett */ 5424dd1b841SLiam R. Howlett inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma, 5434dd1b841SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 5444dd1b841SLiam R. Howlett struct vm_area_struct *next) 5454dd1b841SLiam R. Howlett { 5464dd1b841SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 5474dd1b841SLiam R. Howlett struct address_space *mapping = NULL; 5484dd1b841SLiam R. Howlett struct rb_root_cached *root = NULL; 5494dd1b841SLiam R. Howlett struct anon_vma *anon_vma = vma->anon_vma; 5504dd1b841SLiam R. Howlett struct file *file = vma->vm_file; 5514dd1b841SLiam R. Howlett bool remove_next = false; 5524dd1b841SLiam R. Howlett 5534dd1b841SLiam R. Howlett if (next && (vma != next) && (end == next->vm_end)) { 5544dd1b841SLiam R. Howlett remove_next = true; 5554dd1b841SLiam R. Howlett if (next->anon_vma && !vma->anon_vma) { 5564dd1b841SLiam R. Howlett int error; 5574dd1b841SLiam R. Howlett 5584dd1b841SLiam R. Howlett anon_vma = next->anon_vma; 5594dd1b841SLiam R. Howlett vma->anon_vma = anon_vma; 5604dd1b841SLiam R. Howlett error = anon_vma_clone(vma, next); 5614dd1b841SLiam R. Howlett if (error) 5624dd1b841SLiam R. Howlett return error; 5634dd1b841SLiam R. Howlett } 5644dd1b841SLiam R. Howlett } 5654dd1b841SLiam R. Howlett 5664dd1b841SLiam R. Howlett /* Not merging but overwriting any part of next is not handled. */ 5674dd1b841SLiam R. Howlett VM_BUG_ON(next && !remove_next && next != vma && end > next->vm_start); 5684dd1b841SLiam R. Howlett /* Only handles expanding */ 5694dd1b841SLiam R. Howlett VM_BUG_ON(vma->vm_start < start || vma->vm_end > end); 5704dd1b841SLiam R. Howlett 5714dd1b841SLiam R. Howlett if (mas_preallocate(mas, vma, GFP_KERNEL)) 5724dd1b841SLiam R. Howlett goto nomem; 5734dd1b841SLiam R. Howlett 5744dd1b841SLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 5754dd1b841SLiam R. Howlett 5764dd1b841SLiam R. Howlett if (file) { 5774dd1b841SLiam R. Howlett mapping = file->f_mapping; 5784dd1b841SLiam R. Howlett root = &mapping->i_mmap; 5794dd1b841SLiam R. Howlett uprobe_munmap(vma, vma->vm_start, vma->vm_end); 5804dd1b841SLiam R. Howlett i_mmap_lock_write(mapping); 5814dd1b841SLiam R. Howlett } 5824dd1b841SLiam R. Howlett 5834dd1b841SLiam R. Howlett if (anon_vma) { 5844dd1b841SLiam R. Howlett anon_vma_lock_write(anon_vma); 5854dd1b841SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 5864dd1b841SLiam R. Howlett } 5874dd1b841SLiam R. Howlett 5884dd1b841SLiam R. Howlett if (file) { 5894dd1b841SLiam R. Howlett flush_dcache_mmap_lock(mapping); 5904dd1b841SLiam R. Howlett vma_interval_tree_remove(vma, root); 5914dd1b841SLiam R. Howlett } 5924dd1b841SLiam R. Howlett 5934dd1b841SLiam R. Howlett vma->vm_start = start; 5944dd1b841SLiam R. Howlett vma->vm_end = end; 5954dd1b841SLiam R. Howlett vma->vm_pgoff = pgoff; 5964dd1b841SLiam R. Howlett /* Note: mas must be pointing to the expanding VMA */ 5974dd1b841SLiam R. Howlett vma_mas_store(vma, mas); 5984dd1b841SLiam R. Howlett 5994dd1b841SLiam R. Howlett if (file) { 6004dd1b841SLiam R. Howlett vma_interval_tree_insert(vma, root); 6014dd1b841SLiam R. Howlett flush_dcache_mmap_unlock(mapping); 6024dd1b841SLiam R. Howlett } 6034dd1b841SLiam R. Howlett 6044dd1b841SLiam R. Howlett /* Expanding over the next vma */ 605*763ecb03SLiam R. Howlett if (remove_next && file) { 6064dd1b841SLiam R. Howlett __remove_shared_vm_struct(next, file, mapping); 6074dd1b841SLiam R. Howlett } 6084dd1b841SLiam R. Howlett 6094dd1b841SLiam R. Howlett if (anon_vma) { 6104dd1b841SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 6114dd1b841SLiam R. Howlett anon_vma_unlock_write(anon_vma); 6124dd1b841SLiam R. Howlett } 6134dd1b841SLiam R. Howlett 6144dd1b841SLiam R. Howlett if (file) { 6154dd1b841SLiam R. Howlett i_mmap_unlock_write(mapping); 6164dd1b841SLiam R. Howlett uprobe_mmap(vma); 6174dd1b841SLiam R. Howlett } 6184dd1b841SLiam R. Howlett 6194dd1b841SLiam R. Howlett if (remove_next) { 6204dd1b841SLiam R. Howlett if (file) { 6214dd1b841SLiam R. Howlett uprobe_munmap(next, next->vm_start, next->vm_end); 6224dd1b841SLiam R. Howlett fput(file); 6234dd1b841SLiam R. Howlett } 6244dd1b841SLiam R. Howlett if (next->anon_vma) 6254dd1b841SLiam R. Howlett anon_vma_merge(vma, next); 6264dd1b841SLiam R. Howlett mm->map_count--; 6274dd1b841SLiam R. Howlett mpol_put(vma_policy(next)); 6284dd1b841SLiam R. Howlett vm_area_free(next); 6294dd1b841SLiam R. Howlett } 6304dd1b841SLiam R. Howlett 6314dd1b841SLiam R. Howlett validate_mm(mm); 6324dd1b841SLiam R. Howlett return 0; 6334dd1b841SLiam R. Howlett 6344dd1b841SLiam R. Howlett nomem: 6354dd1b841SLiam R. Howlett return -ENOMEM; 6364dd1b841SLiam R. Howlett } 6374dd1b841SLiam R. Howlett 6384dd1b841SLiam R. Howlett /* 6391da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 6401da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 6411da177e4SLinus Torvalds * The following helper function should be used when such adjustments 6421da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 6431da177e4SLinus Torvalds * before we drop the necessary locks. 6441da177e4SLinus Torvalds */ 645e86f15eeSAndrea Arcangeli int __vma_adjust(struct vm_area_struct *vma, unsigned long start, 646e86f15eeSAndrea Arcangeli unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert, 647e86f15eeSAndrea Arcangeli struct vm_area_struct *expand) 6481da177e4SLinus Torvalds { 6491da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 650524e00b3SLiam R. Howlett struct vm_area_struct *next_next, *next = find_vma(mm, vma->vm_end); 651524e00b3SLiam R. Howlett struct vm_area_struct *orig_vma = vma; 6521da177e4SLinus Torvalds struct address_space *mapping = NULL; 653f808c13fSDavidlohr Bueso struct rb_root_cached *root = NULL; 654012f1800SRik van Riel struct anon_vma *anon_vma = NULL; 6551da177e4SLinus Torvalds struct file *file = vma->vm_file; 656524e00b3SLiam R. Howlett bool vma_changed = false; 6571da177e4SLinus Torvalds long adjust_next = 0; 6581da177e4SLinus Torvalds int remove_next = 0; 659d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 660734537c9SKirill A. Shutemov struct vm_area_struct *exporter = NULL, *importer = NULL; 661287d97acSLinus Torvalds 662d4af56c5SLiam R. Howlett if (next && !insert) { 6631da177e4SLinus Torvalds if (end >= next->vm_end) { 6641da177e4SLinus Torvalds /* 6651da177e4SLinus Torvalds * vma expands, overlapping all the next, and 6661da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 66786d12e47SAndrea Arcangeli * The only other cases that gets here are 668e86f15eeSAndrea Arcangeli * case 1, case 7 and case 8. 669e86f15eeSAndrea Arcangeli */ 670e86f15eeSAndrea Arcangeli if (next == expand) { 671e86f15eeSAndrea Arcangeli /* 672e86f15eeSAndrea Arcangeli * The only case where we don't expand "vma" 673e86f15eeSAndrea Arcangeli * and we expand "next" instead is case 8. 674e86f15eeSAndrea Arcangeli */ 675e86f15eeSAndrea Arcangeli VM_WARN_ON(end != next->vm_end); 676e86f15eeSAndrea Arcangeli /* 677e86f15eeSAndrea Arcangeli * remove_next == 3 means we're 678e86f15eeSAndrea Arcangeli * removing "vma" and that to do so we 679e86f15eeSAndrea Arcangeli * swapped "vma" and "next". 680e86f15eeSAndrea Arcangeli */ 681e86f15eeSAndrea Arcangeli remove_next = 3; 682e86f15eeSAndrea Arcangeli VM_WARN_ON(file != next->vm_file); 683e86f15eeSAndrea Arcangeli swap(vma, next); 684e86f15eeSAndrea Arcangeli } else { 685e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != vma); 686e86f15eeSAndrea Arcangeli /* 687e86f15eeSAndrea Arcangeli * case 1, 6, 7, remove_next == 2 is case 6, 688e86f15eeSAndrea Arcangeli * remove_next == 1 is case 1 or 7. 6891da177e4SLinus Torvalds */ 690734537c9SKirill A. Shutemov remove_next = 1 + (end > next->vm_end); 691d4af56c5SLiam R. Howlett if (remove_next == 2) 692d4af56c5SLiam R. Howlett next_next = find_vma(mm, next->vm_end); 693d4af56c5SLiam R. Howlett 694e86f15eeSAndrea Arcangeli VM_WARN_ON(remove_next == 2 && 695*763ecb03SLiam R. Howlett end != next_next->vm_end); 696e86f15eeSAndrea Arcangeli } 697e86f15eeSAndrea Arcangeli 698287d97acSLinus Torvalds exporter = next; 6991da177e4SLinus Torvalds importer = vma; 700734537c9SKirill A. Shutemov 701734537c9SKirill A. Shutemov /* 702734537c9SKirill A. Shutemov * If next doesn't have anon_vma, import from vma after 703734537c9SKirill A. Shutemov * next, if the vma overlaps with it. 704734537c9SKirill A. Shutemov */ 70597a42cd4SAndrea Arcangeli if (remove_next == 2 && !next->anon_vma) 706*763ecb03SLiam R. Howlett exporter = next_next; 707734537c9SKirill A. Shutemov 7081da177e4SLinus Torvalds } else if (end > next->vm_start) { 7091da177e4SLinus Torvalds /* 7101da177e4SLinus Torvalds * vma expands, overlapping part of the next: 7111da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 7121da177e4SLinus Torvalds */ 713f9d86a60SWei Yang adjust_next = (end - next->vm_start); 714287d97acSLinus Torvalds exporter = next; 7151da177e4SLinus Torvalds importer = vma; 716e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 7171da177e4SLinus Torvalds } else if (end < vma->vm_end) { 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 7201da177e4SLinus Torvalds * split_vma inserting another: so it must be 7211da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 7221da177e4SLinus Torvalds */ 723f9d86a60SWei Yang adjust_next = -(vma->vm_end - end); 724287d97acSLinus Torvalds exporter = vma; 7251da177e4SLinus Torvalds importer = next; 726e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7295beb4930SRik van Riel /* 7305beb4930SRik van Riel * Easily overlooked: when mprotect shifts the boundary, 7315beb4930SRik van Riel * make sure the expanding vma has anon_vma set if the 7325beb4930SRik van Riel * shrinking vma had, to cover any anon pages imported. 7335beb4930SRik van Riel */ 734287d97acSLinus Torvalds if (exporter && exporter->anon_vma && !importer->anon_vma) { 735c4ea95d7SDaniel Forrest int error; 736c4ea95d7SDaniel Forrest 737287d97acSLinus Torvalds importer->anon_vma = exporter->anon_vma; 738b800c91aSKonstantin Khlebnikov error = anon_vma_clone(importer, exporter); 7393fe89b3eSLeon Yu if (error) 740b800c91aSKonstantin Khlebnikov return error; 741b800c91aSKonstantin Khlebnikov } 7425beb4930SRik van Riel } 74337f9f559SKirill A. Shutemov 744d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 745d4af56c5SLiam R. Howlett return -ENOMEM; 746d4af56c5SLiam R. Howlett 747d4af56c5SLiam R. Howlett vma_adjust_trans_huge(orig_vma, start, end, adjust_next); 7481da177e4SLinus Torvalds if (file) { 7491da177e4SLinus Torvalds mapping = file->f_mapping; 7501da177e4SLinus Torvalds root = &mapping->i_mmap; 751cbc91f71SSrikar Dronamraju uprobe_munmap(vma, vma->vm_start, vma->vm_end); 752682968e0SSrikar Dronamraju 753682968e0SSrikar Dronamraju if (adjust_next) 75427ba0644SKirill A. Shutemov uprobe_munmap(next, next->vm_start, next->vm_end); 755682968e0SSrikar Dronamraju 75683cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 7571da177e4SLinus Torvalds if (insert) { 7581da177e4SLinus Torvalds /* 7596b2dbba8SMichel Lespinasse * Put into interval tree now, so instantiated pages 7601da177e4SLinus Torvalds * are visible to arm/parisc __flush_dcache_page 7611da177e4SLinus Torvalds * throughout; but we cannot insert into address 7621da177e4SLinus Torvalds * space until vma start or end is updated. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds __vma_link_file(insert); 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 768012f1800SRik van Riel anon_vma = vma->anon_vma; 769bf181b9fSMichel Lespinasse if (!anon_vma && adjust_next) 770bf181b9fSMichel Lespinasse anon_vma = next->anon_vma; 771bf181b9fSMichel Lespinasse if (anon_vma) { 772e86f15eeSAndrea Arcangeli VM_WARN_ON(adjust_next && next->anon_vma && 773e86f15eeSAndrea Arcangeli anon_vma != next->anon_vma); 7744fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 775bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 776bf181b9fSMichel Lespinasse if (adjust_next) 777bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(next); 778bf181b9fSMichel Lespinasse } 779012f1800SRik van Riel 7800fc48a6eSWei Yang if (file) { 7811da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 7826b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, root); 7831da177e4SLinus Torvalds if (adjust_next) 7846b2dbba8SMichel Lespinasse vma_interval_tree_remove(next, root); 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 787d3737187SMichel Lespinasse if (start != vma->vm_start) { 7883b0e81a1SLiam R. Howlett if ((vma->vm_start < start) && 7893b0e81a1SLiam R. Howlett (!insert || (insert->vm_end != start))) { 790524e00b3SLiam R. Howlett vma_mas_szero(&mas, vma->vm_start, start); 7913b0e81a1SLiam R. Howlett VM_WARN_ON(insert && insert->vm_start > vma->vm_start); 7923b0e81a1SLiam R. Howlett } else { 793524e00b3SLiam R. Howlett vma_changed = true; 7943b0e81a1SLiam R. Howlett } 7951da177e4SLinus Torvalds vma->vm_start = start; 796d3737187SMichel Lespinasse } 797d3737187SMichel Lespinasse if (end != vma->vm_end) { 7983b0e81a1SLiam R. Howlett if (vma->vm_end > end) { 7993b0e81a1SLiam R. Howlett if (!insert || (insert->vm_start != end)) { 800524e00b3SLiam R. Howlett vma_mas_szero(&mas, end, vma->vm_end); 801*763ecb03SLiam R. Howlett mas_reset(&mas); 8023b0e81a1SLiam R. Howlett VM_WARN_ON(insert && 8033b0e81a1SLiam R. Howlett insert->vm_end < vma->vm_end); 8043b0e81a1SLiam R. Howlett } 8053b0e81a1SLiam R. Howlett } else { 806524e00b3SLiam R. Howlett vma_changed = true; 8073b0e81a1SLiam R. Howlett } 8081da177e4SLinus Torvalds vma->vm_end = end; 809d3737187SMichel Lespinasse } 810d4af56c5SLiam R. Howlett 811524e00b3SLiam R. Howlett if (vma_changed) 812d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 813d4af56c5SLiam R. Howlett 8141da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 8151da177e4SLinus Torvalds if (adjust_next) { 816f9d86a60SWei Yang next->vm_start += adjust_next; 817f9d86a60SWei Yang next->vm_pgoff += adjust_next >> PAGE_SHIFT; 818d4af56c5SLiam R. Howlett vma_mas_store(next, &mas); 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8210fc48a6eSWei Yang if (file) { 8221da177e4SLinus Torvalds if (adjust_next) 8236b2dbba8SMichel Lespinasse vma_interval_tree_insert(next, root); 8246b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, root); 8251da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 828*763ecb03SLiam R. Howlett if (remove_next && file) { 8291da177e4SLinus Torvalds __remove_shared_vm_struct(next, file, mapping); 830d4af56c5SLiam R. Howlett if (remove_next == 2) 831d4af56c5SLiam R. Howlett __remove_shared_vm_struct(next_next, file, mapping); 8321da177e4SLinus Torvalds } else if (insert) { 8331da177e4SLinus Torvalds /* 8341da177e4SLinus Torvalds * split_vma has split insert from vma, and needs 8351da177e4SLinus Torvalds * us to insert it before dropping the locks 8361da177e4SLinus Torvalds * (it may either follow vma or precede it). 8371da177e4SLinus Torvalds */ 838*763ecb03SLiam R. Howlett mas_reset(&mas); 839*763ecb03SLiam R. Howlett vma_mas_store(insert, &mas); 840*763ecb03SLiam R. Howlett mm->map_count++; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 843bf181b9fSMichel Lespinasse if (anon_vma) { 844bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 845bf181b9fSMichel Lespinasse if (adjust_next) 846bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(next); 84708b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 848bf181b9fSMichel Lespinasse } 8491da177e4SLinus Torvalds 8500fc48a6eSWei Yang if (file) { 851808fbdbeSWei Yang i_mmap_unlock_write(mapping); 8527b2d81d4SIngo Molnar uprobe_mmap(vma); 8532b144498SSrikar Dronamraju 8542b144498SSrikar Dronamraju if (adjust_next) 8557b2d81d4SIngo Molnar uprobe_mmap(next); 8562b144498SSrikar Dronamraju } 8572b144498SSrikar Dronamraju 8581da177e4SLinus Torvalds if (remove_next) { 859d4af56c5SLiam R. Howlett again: 860925d1c40SMatt Helsley if (file) { 861cbc91f71SSrikar Dronamraju uprobe_munmap(next, next->vm_start, next->vm_end); 8621da177e4SLinus Torvalds fput(file); 863925d1c40SMatt Helsley } 8645beb4930SRik van Riel if (next->anon_vma) 8655beb4930SRik van Riel anon_vma_merge(vma, next); 8661da177e4SLinus Torvalds mm->map_count--; 8673964acd0SOleg Nesterov mpol_put(vma_policy(next)); 868524e00b3SLiam R. Howlett if (remove_next != 2) 869524e00b3SLiam R. Howlett BUG_ON(vma->vm_end < next->vm_end); 8703928d4f5SLinus Torvalds vm_area_free(next); 871524e00b3SLiam R. Howlett 8721da177e4SLinus Torvalds /* 8731da177e4SLinus Torvalds * In mprotect's case 6 (see comments on vma_merge), 874*763ecb03SLiam R. Howlett * we must remove next_next too. 8751da177e4SLinus Torvalds */ 876734537c9SKirill A. Shutemov if (remove_next == 2) { 877734537c9SKirill A. Shutemov remove_next = 1; 878*763ecb03SLiam R. Howlett next = next_next; 8791da177e4SLinus Torvalds goto again; 880fb8c41e9SAndrea Arcangeli } 8811da177e4SLinus Torvalds } 8822b144498SSrikar Dronamraju if (insert && file) 8837b2d81d4SIngo Molnar uprobe_mmap(insert); 8841da177e4SLinus Torvalds 8853b0e81a1SLiam R. Howlett mas_destroy(&mas); 8861da177e4SLinus Torvalds validate_mm(mm); 887*763ecb03SLiam R. Howlett 8885beb4930SRik van Riel return 0; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds /* 8921da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 8931da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 89619a809afSAndrea Arcangeli struct file *file, unsigned long vm_flags, 8979a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 8985c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 8991da177e4SLinus Torvalds { 90034228d47SCyrill Gorcunov /* 90134228d47SCyrill Gorcunov * VM_SOFTDIRTY should not prevent from VMA merging, if we 90234228d47SCyrill Gorcunov * match the flags but dirty bit -- the caller should mark 90334228d47SCyrill Gorcunov * merged VMA as dirty. If dirty bit won't be excluded from 9048bb4e7a2SWei Yang * comparison, we increase pressure on the memory system forcing 90534228d47SCyrill Gorcunov * the kernel to generate new VMAs when old one could be 90634228d47SCyrill Gorcunov * extended instead. 90734228d47SCyrill Gorcunov */ 90834228d47SCyrill Gorcunov if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 9091da177e4SLinus Torvalds return 0; 9101da177e4SLinus Torvalds if (vma->vm_file != file) 9111da177e4SLinus Torvalds return 0; 9121da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 9131da177e4SLinus Torvalds return 0; 91419a809afSAndrea Arcangeli if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 91519a809afSAndrea Arcangeli return 0; 9165c26f6acSSuren Baghdasaryan if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 9179a10064fSColin Cross return 0; 9181da177e4SLinus Torvalds return 1; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 922965f55deSShaohua Li struct anon_vma *anon_vma2, 923965f55deSShaohua Li struct vm_area_struct *vma) 9241da177e4SLinus Torvalds { 925965f55deSShaohua Li /* 926965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 927965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 928965f55deSShaohua Li */ 929965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 930965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 931965f55deSShaohua Li return 1; 932965f55deSShaohua Li return anon_vma1 == anon_vma2; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /* 9361da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 9371da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 9381da177e4SLinus Torvalds * 9391da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 9401da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 9411da177e4SLinus Torvalds * 9421da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 94345e55300SPeter Collingbourne * indices (16TB on ia32) because do_mmap() does not permit mmap's which 9441da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 9451da177e4SLinus Torvalds */ 9461da177e4SLinus Torvalds static int 9471da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 94819a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 94919a809afSAndrea Arcangeli pgoff_t vm_pgoff, 9509a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 9515c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 9521da177e4SLinus Torvalds { 9539a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 954965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 9551da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 9561da177e4SLinus Torvalds return 1; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds return 0; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 9631da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 9641da177e4SLinus Torvalds * 9651da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 9661da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 9671da177e4SLinus Torvalds */ 9681da177e4SLinus Torvalds static int 9691da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 97019a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 97119a809afSAndrea Arcangeli pgoff_t vm_pgoff, 9729a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 9735c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 9741da177e4SLinus Torvalds { 9759a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 976965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 9771da177e4SLinus Torvalds pgoff_t vm_pglen; 978d6e93217SLibin vm_pglen = vma_pages(vma); 9791da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 9801da177e4SLinus Torvalds return 1; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds return 0; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* 9869a10064fSColin Cross * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 9879a10064fSColin Cross * figure out whether that can be merged with its predecessor or its 9889a10064fSColin Cross * successor. Or both (it neatly fills a hole). 9891da177e4SLinus Torvalds * 9901da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 9911da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 9921da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 9931da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 9941da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 9951da177e4SLinus Torvalds * case has already been eliminated. 9961da177e4SLinus Torvalds * 9971da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 9981da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 9991da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 10001da177e4SLinus Torvalds * 10015d42ab29SWei Yang * AAAA AAAA AAAA 10025d42ab29SWei Yang * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN 10035d42ab29SWei Yang * cannot merge might become might become 10045d42ab29SWei Yang * PPNNNNNNNNNN PPPPPPPPPPNN 10055d42ab29SWei Yang * mmap, brk or case 4 below case 5 below 10065d42ab29SWei Yang * mremap move: 10075d42ab29SWei Yang * AAAA AAAA 10085d42ab29SWei Yang * PPPP NNNN PPPPNNNNXXXX 10095d42ab29SWei Yang * might become might become 10105d42ab29SWei Yang * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 10115d42ab29SWei Yang * PPPPPPPPNNNN 2 or PPPPPPPPXXXX 7 or 10125d42ab29SWei Yang * PPPPNNNNNNNN 3 PPPPXXXXXXXX 8 10131da177e4SLinus Torvalds * 10148bb4e7a2SWei Yang * It is important for case 8 that the vma NNNN overlapping the 1015e86f15eeSAndrea Arcangeli * region AAAA is never going to extended over XXXX. Instead XXXX must 1016e86f15eeSAndrea Arcangeli * be extended in region AAAA and NNNN must be removed. This way in 1017e86f15eeSAndrea Arcangeli * all cases where vma_merge succeeds, the moment vma_adjust drops the 1018e86f15eeSAndrea Arcangeli * rmap_locks, the properties of the merged vma will be already 1019e86f15eeSAndrea Arcangeli * correct for the whole merged range. Some of those properties like 1020e86f15eeSAndrea Arcangeli * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 1021e86f15eeSAndrea Arcangeli * be correct for the whole merged range immediately after the 1022e86f15eeSAndrea Arcangeli * rmap_locks are released. Otherwise if XXXX would be removed and 1023e86f15eeSAndrea Arcangeli * NNNN would be extended over the XXXX range, remove_migration_ptes 1024e86f15eeSAndrea Arcangeli * or other rmap walkers (if working on addresses beyond the "end" 1025e86f15eeSAndrea Arcangeli * parameter) may establish ptes with the wrong permissions of NNNN 1026e86f15eeSAndrea Arcangeli * instead of the right permissions of XXXX. 10271da177e4SLinus Torvalds */ 10281da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm, 10291da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 10301da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 10311da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 103219a809afSAndrea Arcangeli pgoff_t pgoff, struct mempolicy *policy, 10339a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 10345c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 10351da177e4SLinus Torvalds { 10361da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 10371da177e4SLinus Torvalds struct vm_area_struct *area, *next; 10385beb4930SRik van Riel int err; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* 10411da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 10421da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 10431da177e4SLinus Torvalds */ 10441da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 10451da177e4SLinus Torvalds return NULL; 10461da177e4SLinus Torvalds 1047*763ecb03SLiam R. Howlett next = find_vma(mm, prev ? prev->vm_end : 0); 10481da177e4SLinus Torvalds area = next; 1049e86f15eeSAndrea Arcangeli if (area && area->vm_end == end) /* cases 6, 7, 8 */ 1050*763ecb03SLiam R. Howlett next = find_vma(mm, next->vm_end); 10511da177e4SLinus Torvalds 1052e86f15eeSAndrea Arcangeli /* verify some invariant that must be enforced by the caller */ 1053e86f15eeSAndrea Arcangeli VM_WARN_ON(prev && addr <= prev->vm_start); 1054e86f15eeSAndrea Arcangeli VM_WARN_ON(area && end > area->vm_end); 1055e86f15eeSAndrea Arcangeli VM_WARN_ON(addr >= end); 1056e86f15eeSAndrea Arcangeli 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Can it merge with the predecessor? 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 10611da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 10621da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 106319a809afSAndrea Arcangeli anon_vma, file, pgoff, 10649a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 10651da177e4SLinus Torvalds /* 10661da177e4SLinus Torvalds * OK, it can. Can we now merge in the successor as well? 10671da177e4SLinus Torvalds */ 10681da177e4SLinus Torvalds if (next && end == next->vm_start && 10691da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 10701da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 107119a809afSAndrea Arcangeli anon_vma, file, 107219a809afSAndrea Arcangeli pgoff+pglen, 10739a10064fSColin Cross vm_userfaultfd_ctx, anon_name) && 10741da177e4SLinus Torvalds is_mergeable_anon_vma(prev->anon_vma, 1075965f55deSShaohua Li next->anon_vma, NULL)) { 10761da177e4SLinus Torvalds /* cases 1, 6 */ 1077e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1078e86f15eeSAndrea Arcangeli next->vm_end, prev->vm_pgoff, NULL, 1079e86f15eeSAndrea Arcangeli prev); 10801da177e4SLinus Torvalds } else /* cases 2, 5, 7 */ 1081e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1082e86f15eeSAndrea Arcangeli end, prev->vm_pgoff, NULL, prev); 10835beb4930SRik van Riel if (err) 10845beb4930SRik van Riel return NULL; 1085c791576cSYang Shi khugepaged_enter_vma(prev, vm_flags); 10861da177e4SLinus Torvalds return prev; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * Can this new request be merged in front of next? 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds if (next && end == next->vm_start && 10931da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 10941da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 109519a809afSAndrea Arcangeli anon_vma, file, pgoff+pglen, 10969a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 10971da177e4SLinus Torvalds if (prev && addr < prev->vm_end) /* case 4 */ 1098e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1099e86f15eeSAndrea Arcangeli addr, prev->vm_pgoff, NULL, next); 1100e86f15eeSAndrea Arcangeli else { /* cases 3, 8 */ 1101e86f15eeSAndrea Arcangeli err = __vma_adjust(area, addr, next->vm_end, 1102e86f15eeSAndrea Arcangeli next->vm_pgoff - pglen, NULL, next); 1103e86f15eeSAndrea Arcangeli /* 1104e86f15eeSAndrea Arcangeli * In case 3 area is already equal to next and 1105e86f15eeSAndrea Arcangeli * this is a noop, but in case 8 "area" has 1106e86f15eeSAndrea Arcangeli * been removed and next was expanded over it. 1107e86f15eeSAndrea Arcangeli */ 1108e86f15eeSAndrea Arcangeli area = next; 1109e86f15eeSAndrea Arcangeli } 11105beb4930SRik van Riel if (err) 11115beb4930SRik van Riel return NULL; 1112c791576cSYang Shi khugepaged_enter_vma(area, vm_flags); 11131da177e4SLinus Torvalds return area; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds return NULL; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds /* 1120b4f315b4SEthon Paul * Rough compatibility check to quickly see if it's even worth looking 1121d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1122d0e9fe17SLinus Torvalds * 1123d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1124d0e9fe17SLinus Torvalds * in things that mprotect may change. 1125d0e9fe17SLinus Torvalds * 1126d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1127d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1128d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1129d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1130d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1131d0e9fe17SLinus Torvalds */ 1132d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1133d0e9fe17SLinus Torvalds { 1134d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1135d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1136d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 11376cb4d9a2SAnshuman Khandual !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1138d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1139d0e9fe17SLinus Torvalds } 1140d0e9fe17SLinus Torvalds 1141d0e9fe17SLinus Torvalds /* 1142d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1143d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1144d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1145d0e9fe17SLinus Torvalds * to share the anon_vma. 1146d0e9fe17SLinus Torvalds * 11475b449489SFlorian Rommel * NOTE! This runs with mmap_lock held for reading, so it is possible that 1148d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1149d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1150d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1151d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 11524db0c3c2SJason Low * we do that READ_ONCE() to make sure that we never re-load the pointer. 1153d0e9fe17SLinus Torvalds * 1154d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1155d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1156d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1157d0e9fe17SLinus Torvalds * a fork). 1158d0e9fe17SLinus Torvalds * 1159d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1160d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 11615b449489SFlorian Rommel * a read lock on the mmap_lock. 1162d0e9fe17SLinus Torvalds */ 1163d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1164d0e9fe17SLinus Torvalds { 1165d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 11664db0c3c2SJason Low struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1167d0e9fe17SLinus Torvalds 1168d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1169d0e9fe17SLinus Torvalds return anon_vma; 1170d0e9fe17SLinus Torvalds } 1171d0e9fe17SLinus Torvalds return NULL; 1172d0e9fe17SLinus Torvalds } 1173d0e9fe17SLinus Torvalds 1174d0e9fe17SLinus Torvalds /* 11751da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 11761da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 11771da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 11781da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 11791da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 11801da177e4SLinus Torvalds * mprotect. 11811da177e4SLinus Torvalds */ 11821da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 11831da177e4SLinus Torvalds { 1184*763ecb03SLiam R. Howlett MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); 1185a67c8caaSMiaohe Lin struct anon_vma *anon_vma = NULL; 1186*763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next; 11871da177e4SLinus Torvalds 1188a67c8caaSMiaohe Lin /* Try next first. */ 1189*763ecb03SLiam R. Howlett next = mas_walk(&mas); 1190*763ecb03SLiam R. Howlett if (next) { 1191*763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(next, vma, next); 1192d0e9fe17SLinus Torvalds if (anon_vma) 1193d0e9fe17SLinus Torvalds return anon_vma; 1194a67c8caaSMiaohe Lin } 11951da177e4SLinus Torvalds 1196*763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1197*763ecb03SLiam R. Howlett VM_BUG_ON_VMA(prev != vma, vma); 1198*763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1199a67c8caaSMiaohe Lin /* Try prev next. */ 1200*763ecb03SLiam R. Howlett if (prev) 1201*763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(prev, prev, vma); 1202a67c8caaSMiaohe Lin 12031da177e4SLinus Torvalds /* 1204a67c8caaSMiaohe Lin * We might reach here with anon_vma == NULL if we can't find 1205a67c8caaSMiaohe Lin * any reusable anon_vma. 12061da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 12071da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 12081da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 12091da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 12101da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 12111da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 12121da177e4SLinus Torvalds */ 1213a67c8caaSMiaohe Lin return anon_vma; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds /* 121740401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 121840401530SAl Viro * low as possible but still greater than mmap_min_addr 121940401530SAl Viro */ 122040401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 122140401530SAl Viro { 122240401530SAl Viro hint &= PAGE_MASK; 122340401530SAl Viro if (((void *)hint != NULL) && 122440401530SAl Viro (hint < mmap_min_addr)) 122540401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 122640401530SAl Viro return hint; 122740401530SAl Viro } 122840401530SAl Viro 12296aeb2542SMike Rapoport int mlock_future_check(struct mm_struct *mm, unsigned long flags, 1230363ee17fSDavidlohr Bueso unsigned long len) 1231363ee17fSDavidlohr Bueso { 1232363ee17fSDavidlohr Bueso unsigned long locked, lock_limit; 1233363ee17fSDavidlohr Bueso 1234363ee17fSDavidlohr Bueso /* mlock MCL_FUTURE? */ 1235363ee17fSDavidlohr Bueso if (flags & VM_LOCKED) { 1236363ee17fSDavidlohr Bueso locked = len >> PAGE_SHIFT; 1237363ee17fSDavidlohr Bueso locked += mm->locked_vm; 1238363ee17fSDavidlohr Bueso lock_limit = rlimit(RLIMIT_MEMLOCK); 1239363ee17fSDavidlohr Bueso lock_limit >>= PAGE_SHIFT; 1240363ee17fSDavidlohr Bueso if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 1241363ee17fSDavidlohr Bueso return -EAGAIN; 1242363ee17fSDavidlohr Bueso } 1243363ee17fSDavidlohr Bueso return 0; 1244363ee17fSDavidlohr Bueso } 1245363ee17fSDavidlohr Bueso 1246be83bbf8SLinus Torvalds static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) 1247be83bbf8SLinus Torvalds { 1248be83bbf8SLinus Torvalds if (S_ISREG(inode->i_mode)) 1249423913adSLinus Torvalds return MAX_LFS_FILESIZE; 1250be83bbf8SLinus Torvalds 1251be83bbf8SLinus Torvalds if (S_ISBLK(inode->i_mode)) 1252be83bbf8SLinus Torvalds return MAX_LFS_FILESIZE; 1253be83bbf8SLinus Torvalds 125476f34950SIvan Khoronzhuk if (S_ISSOCK(inode->i_mode)) 125576f34950SIvan Khoronzhuk return MAX_LFS_FILESIZE; 125676f34950SIvan Khoronzhuk 1257be83bbf8SLinus Torvalds /* Special "we do even unsigned file positions" case */ 1258be83bbf8SLinus Torvalds if (file->f_mode & FMODE_UNSIGNED_OFFSET) 1259be83bbf8SLinus Torvalds return 0; 1260be83bbf8SLinus Torvalds 1261be83bbf8SLinus Torvalds /* Yes, random drivers might want more. But I'm tired of buggy drivers */ 1262be83bbf8SLinus Torvalds return ULONG_MAX; 1263be83bbf8SLinus Torvalds } 1264be83bbf8SLinus Torvalds 1265be83bbf8SLinus Torvalds static inline bool file_mmap_ok(struct file *file, struct inode *inode, 1266be83bbf8SLinus Torvalds unsigned long pgoff, unsigned long len) 1267be83bbf8SLinus Torvalds { 1268be83bbf8SLinus Torvalds u64 maxsize = file_mmap_size_max(file, inode); 1269be83bbf8SLinus Torvalds 1270be83bbf8SLinus Torvalds if (maxsize && len > maxsize) 1271be83bbf8SLinus Torvalds return false; 1272be83bbf8SLinus Torvalds maxsize -= len; 1273be83bbf8SLinus Torvalds if (pgoff > maxsize >> PAGE_SHIFT) 1274be83bbf8SLinus Torvalds return false; 1275be83bbf8SLinus Torvalds return true; 1276be83bbf8SLinus Torvalds } 1277be83bbf8SLinus Torvalds 127840401530SAl Viro /* 12793e4e28c5SMichel Lespinasse * The caller must write-lock current->mm->mmap_lock. 12801da177e4SLinus Torvalds */ 12811fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, unsigned long addr, 12821da177e4SLinus Torvalds unsigned long len, unsigned long prot, 128345e55300SPeter Collingbourne unsigned long flags, unsigned long pgoff, 128445e55300SPeter Collingbourne unsigned long *populate, struct list_head *uf) 12851da177e4SLinus Torvalds { 12861da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 128745e55300SPeter Collingbourne vm_flags_t vm_flags; 128862b5f7d0SDave Hansen int pkey = 0; 12891da177e4SLinus Torvalds 1290524e00b3SLiam R. Howlett validate_mm(mm); 129141badc15SMichel Lespinasse *populate = 0; 1292bebeb3d6SMichel Lespinasse 1293e37609bbSPiotr Kwapulinski if (!len) 1294e37609bbSPiotr Kwapulinski return -EINVAL; 1295e37609bbSPiotr Kwapulinski 12961da177e4SLinus Torvalds /* 12971da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 12981da177e4SLinus Torvalds * 12991da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 13001da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 13011da177e4SLinus Torvalds */ 13021da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 130390f8572bSEric W. Biederman if (!(file && path_noexec(&file->f_path))) 13041da177e4SLinus Torvalds prot |= PROT_EXEC; 13051da177e4SLinus Torvalds 1306a4ff8e86SMichal Hocko /* force arch specific MAP_FIXED handling in get_unmapped_area */ 1307a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) 1308a4ff8e86SMichal Hocko flags |= MAP_FIXED; 1309a4ff8e86SMichal Hocko 13107cd94146SEric Paris if (!(flags & MAP_FIXED)) 13117cd94146SEric Paris addr = round_hint_to_min(addr); 13127cd94146SEric Paris 13131da177e4SLinus Torvalds /* Careful about overflows.. */ 13141da177e4SLinus Torvalds len = PAGE_ALIGN(len); 13159206de95SAl Viro if (!len) 13161da177e4SLinus Torvalds return -ENOMEM; 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds /* offset overflow? */ 13191da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 13201da177e4SLinus Torvalds return -EOVERFLOW; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* Too many mappings? */ 13231da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 13241da177e4SLinus Torvalds return -ENOMEM; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 13271da177e4SLinus Torvalds * that it represents a valid section of the address space. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 1330ff68dac6SGaowei Pu if (IS_ERR_VALUE(addr)) 13311da177e4SLinus Torvalds return addr; 13321da177e4SLinus Torvalds 1333a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) { 133435e43c5fSLiam Howlett if (find_vma_intersection(mm, addr, addr + len)) 1335a4ff8e86SMichal Hocko return -EEXIST; 1336a4ff8e86SMichal Hocko } 1337a4ff8e86SMichal Hocko 133862b5f7d0SDave Hansen if (prot == PROT_EXEC) { 133962b5f7d0SDave Hansen pkey = execute_only_pkey(mm); 134062b5f7d0SDave Hansen if (pkey < 0) 134162b5f7d0SDave Hansen pkey = 0; 134262b5f7d0SDave Hansen } 134362b5f7d0SDave Hansen 13441da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 13451da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 13461da177e4SLinus Torvalds * of the memory object, so we don't do any here. 13471da177e4SLinus Torvalds */ 134845e55300SPeter Collingbourne vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | 13491da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 13501da177e4SLinus Torvalds 1351cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 13521da177e4SLinus Torvalds if (!can_do_mlock()) 13531da177e4SLinus Torvalds return -EPERM; 1354ba470de4SRik van Riel 1355363ee17fSDavidlohr Bueso if (mlock_future_check(mm, vm_flags, len)) 13561da177e4SLinus Torvalds return -EAGAIN; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (file) { 1359077bf22bSOleg Nesterov struct inode *inode = file_inode(file); 13601c972597SDan Williams unsigned long flags_mask; 13611c972597SDan Williams 1362be83bbf8SLinus Torvalds if (!file_mmap_ok(file, inode, pgoff, len)) 1363be83bbf8SLinus Torvalds return -EOVERFLOW; 1364be83bbf8SLinus Torvalds 13651c972597SDan Williams flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; 1366077bf22bSOleg Nesterov 13671da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 13681da177e4SLinus Torvalds case MAP_SHARED: 13691c972597SDan Williams /* 13701c972597SDan Williams * Force use of MAP_SHARED_VALIDATE with non-legacy 13711c972597SDan Williams * flags. E.g. MAP_SYNC is dangerous to use with 13721c972597SDan Williams * MAP_SHARED as you don't know which consistency model 13731c972597SDan Williams * you will get. We silently ignore unsupported flags 13741c972597SDan Williams * with MAP_SHARED to preserve backward compatibility. 13751c972597SDan Williams */ 13761c972597SDan Williams flags &= LEGACY_MAP_MASK; 1377e4a9bc58SJoe Perches fallthrough; 13781c972597SDan Williams case MAP_SHARED_VALIDATE: 13791c972597SDan Williams if (flags & ~flags_mask) 13801c972597SDan Williams return -EOPNOTSUPP; 1381dc617f29SDarrick J. Wong if (prot & PROT_WRITE) { 1382dc617f29SDarrick J. Wong if (!(file->f_mode & FMODE_WRITE)) 13831da177e4SLinus Torvalds return -EACCES; 1384dc617f29SDarrick J. Wong if (IS_SWAPFILE(file->f_mapping->host)) 1385dc617f29SDarrick J. Wong return -ETXTBSY; 1386dc617f29SDarrick J. Wong } 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds /* 13891da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 13901da177e4SLinus Torvalds * file.. 13911da177e4SLinus Torvalds */ 13921da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 13931da177e4SLinus Torvalds return -EACCES; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13961da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 13971da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 1398e4a9bc58SJoe Perches fallthrough; 13991da177e4SLinus Torvalds case MAP_PRIVATE: 14001da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 14011da177e4SLinus Torvalds return -EACCES; 140290f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 140380c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 140480c5606cSLinus Torvalds return -EPERM; 140580c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 140680c5606cSLinus Torvalds } 140780c5606cSLinus Torvalds 140872c2d531SAl Viro if (!file->f_op->mmap) 140980c5606cSLinus Torvalds return -ENODEV; 1410b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1411b2c56e4fSOleg Nesterov return -EINVAL; 14121da177e4SLinus Torvalds break; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds default: 14151da177e4SLinus Torvalds return -EINVAL; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds } else { 14181da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 14191da177e4SLinus Torvalds case MAP_SHARED: 1420b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1421b2c56e4fSOleg Nesterov return -EINVAL; 1422ce363942STejun Heo /* 1423ce363942STejun Heo * Ignore pgoff. 1424ce363942STejun Heo */ 1425ce363942STejun Heo pgoff = 0; 14261da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 14271da177e4SLinus Torvalds break; 14281da177e4SLinus Torvalds case MAP_PRIVATE: 14291da177e4SLinus Torvalds /* 14301da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 14311da177e4SLinus Torvalds */ 14321da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 14331da177e4SLinus Torvalds break; 14341da177e4SLinus Torvalds default: 14351da177e4SLinus Torvalds return -EINVAL; 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 1439c22c0d63SMichel Lespinasse /* 1440c22c0d63SMichel Lespinasse * Set 'VM_NORESERVE' if we should not account for the 1441c22c0d63SMichel Lespinasse * memory use of this mapping. 1442c22c0d63SMichel Lespinasse */ 1443c22c0d63SMichel Lespinasse if (flags & MAP_NORESERVE) { 1444c22c0d63SMichel Lespinasse /* We honor MAP_NORESERVE if allowed to overcommit */ 1445c22c0d63SMichel Lespinasse if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1446c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1447c22c0d63SMichel Lespinasse 1448c22c0d63SMichel Lespinasse /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 1449c22c0d63SMichel Lespinasse if (file && is_file_hugepages(file)) 1450c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1451c22c0d63SMichel Lespinasse } 1452c22c0d63SMichel Lespinasse 1453897ab3e0SMike Rapoport addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); 145409a9f1d2SMichel Lespinasse if (!IS_ERR_VALUE(addr) && 145509a9f1d2SMichel Lespinasse ((vm_flags & VM_LOCKED) || 145609a9f1d2SMichel Lespinasse (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) 145741badc15SMichel Lespinasse *populate = len; 1458bebeb3d6SMichel Lespinasse return addr; 14590165ab44SMiklos Szeredi } 14606be5ceb0SLinus Torvalds 1461a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1462a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1463a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 146466f0dc48SHugh Dickins { 146566f0dc48SHugh Dickins struct file *file = NULL; 14661e3ee14bSChen Gang unsigned long retval; 146766f0dc48SHugh Dickins 146866f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1469120a795dSAl Viro audit_mmap_fd(fd, flags); 147066f0dc48SHugh Dickins file = fget(fd); 147166f0dc48SHugh Dickins if (!file) 14721e3ee14bSChen Gang return -EBADF; 14737bba8f0eSZhen Lei if (is_file_hugepages(file)) { 1474af73e4d9SNaoya Horiguchi len = ALIGN(len, huge_page_size(hstate_file(file))); 14757bba8f0eSZhen Lei } else if (unlikely(flags & MAP_HUGETLB)) { 1476493af578SJörn Engel retval = -EINVAL; 1477493af578SJörn Engel goto out_fput; 14787bba8f0eSZhen Lei } 147966f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 1480c103a4dcSAndrew Morton struct hstate *hs; 1481af73e4d9SNaoya Horiguchi 148220ac2893SAnshuman Khandual hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 1483091d0d55SLi Zefan if (!hs) 1484091d0d55SLi Zefan return -EINVAL; 1485091d0d55SLi Zefan 1486091d0d55SLi Zefan len = ALIGN(len, huge_page_size(hs)); 148766f0dc48SHugh Dickins /* 148866f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 148966f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 149066f0dc48SHugh Dickins */ 1491af73e4d9SNaoya Horiguchi file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, 149242d7395fSAndi Kleen VM_NORESERVE, 149383c1fd76Szhangyiru HUGETLB_ANONHUGE_INODE, 149442d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 149566f0dc48SHugh Dickins if (IS_ERR(file)) 149666f0dc48SHugh Dickins return PTR_ERR(file); 149766f0dc48SHugh Dickins } 149866f0dc48SHugh Dickins 14999fbeb5abSMichal Hocko retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1500493af578SJörn Engel out_fput: 150166f0dc48SHugh Dickins if (file) 150266f0dc48SHugh Dickins fput(file); 150366f0dc48SHugh Dickins return retval; 150466f0dc48SHugh Dickins } 150566f0dc48SHugh Dickins 1506a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1507a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1508a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1509a90f590aSDominik Brodowski { 1510a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1511a90f590aSDominik Brodowski } 1512a90f590aSDominik Brodowski 1513a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1514a4679373SChristoph Hellwig struct mmap_arg_struct { 1515a4679373SChristoph Hellwig unsigned long addr; 1516a4679373SChristoph Hellwig unsigned long len; 1517a4679373SChristoph Hellwig unsigned long prot; 1518a4679373SChristoph Hellwig unsigned long flags; 1519a4679373SChristoph Hellwig unsigned long fd; 1520a4679373SChristoph Hellwig unsigned long offset; 1521a4679373SChristoph Hellwig }; 1522a4679373SChristoph Hellwig 1523a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1524a4679373SChristoph Hellwig { 1525a4679373SChristoph Hellwig struct mmap_arg_struct a; 1526a4679373SChristoph Hellwig 1527a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1528a4679373SChristoph Hellwig return -EFAULT; 1529de1741a1SAlexander Kuleshov if (offset_in_page(a.offset)) 1530a4679373SChristoph Hellwig return -EINVAL; 1531a4679373SChristoph Hellwig 1532a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1533a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1534a4679373SChristoph Hellwig } 1535a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1536a4679373SChristoph Hellwig 15374e950f6fSAlexey Dobriyan /* 15388bb4e7a2SWei Yang * Some shared mappings will want the pages marked read-only 15394e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 15404e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 15414e950f6fSAlexey Dobriyan * VM_SHARED bit). 15424e950f6fSAlexey Dobriyan */ 15436d2329f8SAndrea Arcangeli int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 15444e950f6fSAlexey Dobriyan { 1545ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 15468a04446aSKirill A. Shutemov const struct vm_operations_struct *vm_ops = vma->vm_ops; 15474e950f6fSAlexey Dobriyan 15484e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 15494e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 15504e950f6fSAlexey Dobriyan return 0; 15514e950f6fSAlexey Dobriyan 15524e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 15538a04446aSKirill A. Shutemov if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) 15544e950f6fSAlexey Dobriyan return 1; 15554e950f6fSAlexey Dobriyan 155664e45507SPeter Feiner /* The open routine did something to the protections that pgprot_modify 155764e45507SPeter Feiner * won't preserve? */ 15586d2329f8SAndrea Arcangeli if (pgprot_val(vm_page_prot) != 15596d2329f8SAndrea Arcangeli pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) 15604e950f6fSAlexey Dobriyan return 0; 15614e950f6fSAlexey Dobriyan 1562f96f7a40SDavid Hildenbrand /* 1563f96f7a40SDavid Hildenbrand * Do we need to track softdirty? hugetlb does not support softdirty 1564f96f7a40SDavid Hildenbrand * tracking yet. 1565f96f7a40SDavid Hildenbrand */ 1566f96f7a40SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 156764e45507SPeter Feiner return 1; 156864e45507SPeter Feiner 15694e950f6fSAlexey Dobriyan /* Specialty mapping? */ 15704b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 15714e950f6fSAlexey Dobriyan return 0; 15724e950f6fSAlexey Dobriyan 15734e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 15744e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 1575f56753acSChristoph Hellwig mapping_can_writeback(vma->vm_file->f_mapping); 15764e950f6fSAlexey Dobriyan } 15774e950f6fSAlexey Dobriyan 1578fc8744adSLinus Torvalds /* 1579fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 15805a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1581fc8744adSLinus Torvalds */ 1582ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1583fc8744adSLinus Torvalds { 15845a6fe125SMel Gorman /* 15855a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 15865a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 15875a6fe125SMel Gorman */ 15885a6fe125SMel Gorman if (file && is_file_hugepages(file)) 15895a6fe125SMel Gorman return 0; 15905a6fe125SMel Gorman 1591fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1592fc8744adSLinus Torvalds } 1593fc8744adSLinus Torvalds 15943499a131SLiam R. Howlett /** 15953499a131SLiam R. Howlett * unmapped_area() - Find an area between the low_limit and the high_limit with 15963499a131SLiam R. Howlett * the correct alignment and offset, all from @info. Note: current->mm is used 15973499a131SLiam R. Howlett * for the search. 15983499a131SLiam R. Howlett * 15993499a131SLiam R. Howlett * @info: The unmapped area information including the range (low_limit - 16003499a131SLiam R. Howlett * hight_limit), the alignment offset and mask. 16013499a131SLiam R. Howlett * 16023499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 16033499a131SLiam R. Howlett */ 1604baceaf1cSJaewon Kim static unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1605db4fbfb9SMichel Lespinasse { 16063499a131SLiam R. Howlett unsigned long length, gap; 1607db4fbfb9SMichel Lespinasse 16083499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1609db4fbfb9SMichel Lespinasse 1610db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1611db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1612db4fbfb9SMichel Lespinasse if (length < info->length) 1613db4fbfb9SMichel Lespinasse return -ENOMEM; 1614db4fbfb9SMichel Lespinasse 16153499a131SLiam R. Howlett if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, 16163499a131SLiam R. Howlett length)) 16173499a131SLiam R. Howlett return -ENOMEM; 16183499a131SLiam R. Howlett 1619d4af56c5SLiam R. Howlett gap = mas.index; 1620d4af56c5SLiam R. Howlett gap += (info->align_offset - gap) & info->align_mask; 16213499a131SLiam R. Howlett return gap; 1622db4fbfb9SMichel Lespinasse } 1623db4fbfb9SMichel Lespinasse 16243499a131SLiam R. Howlett /** 16253499a131SLiam R. Howlett * unmapped_area_topdown() - Find an area between the low_limit and the 16263499a131SLiam R. Howlett * high_limit with * the correct alignment and offset at the highest available 16273499a131SLiam R. Howlett * address, all from @info. Note: current->mm is used for the search. 16283499a131SLiam R. Howlett * 16293499a131SLiam R. Howlett * @info: The unmapped area information including the range (low_limit - 16303499a131SLiam R. Howlett * hight_limit), the alignment offset and mask. 16313499a131SLiam R. Howlett * 16323499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 16333499a131SLiam R. Howlett */ 1634baceaf1cSJaewon Kim static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 1635db4fbfb9SMichel Lespinasse { 16363499a131SLiam R. Howlett unsigned long length, gap; 1637d4af56c5SLiam R. Howlett 16383499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1639db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1640db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1641db4fbfb9SMichel Lespinasse if (length < info->length) 1642db4fbfb9SMichel Lespinasse return -ENOMEM; 1643db4fbfb9SMichel Lespinasse 16443499a131SLiam R. Howlett if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, 16453499a131SLiam R. Howlett length)) 16463499a131SLiam R. Howlett return -ENOMEM; 16473499a131SLiam R. Howlett 1648d4af56c5SLiam R. Howlett gap = mas.last + 1 - info->length; 1649d4af56c5SLiam R. Howlett gap -= (gap - info->align_offset) & info->align_mask; 16503499a131SLiam R. Howlett return gap; 1651db4fbfb9SMichel Lespinasse } 1652db4fbfb9SMichel Lespinasse 1653baceaf1cSJaewon Kim /* 1654baceaf1cSJaewon Kim * Search for an unmapped address range. 1655baceaf1cSJaewon Kim * 1656baceaf1cSJaewon Kim * We are looking for a range that: 1657baceaf1cSJaewon Kim * - does not intersect with any VMA; 1658baceaf1cSJaewon Kim * - is contained within the [low_limit, high_limit) interval; 1659baceaf1cSJaewon Kim * - is at least the desired size. 1660baceaf1cSJaewon Kim * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) 1661baceaf1cSJaewon Kim */ 1662baceaf1cSJaewon Kim unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) 1663baceaf1cSJaewon Kim { 1664df529cabSJaewon Kim unsigned long addr; 1665df529cabSJaewon Kim 1666baceaf1cSJaewon Kim if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) 1667df529cabSJaewon Kim addr = unmapped_area_topdown(info); 1668baceaf1cSJaewon Kim else 1669df529cabSJaewon Kim addr = unmapped_area(info); 1670df529cabSJaewon Kim 1671df529cabSJaewon Kim trace_vm_unmapped_area(addr, info); 1672df529cabSJaewon Kim return addr; 1673baceaf1cSJaewon Kim } 1674f6795053SSteve Capper 16751da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 16761da177e4SLinus Torvalds * For shmat() with addr=0. 16771da177e4SLinus Torvalds * 16781da177e4SLinus Torvalds * Ugly calling convention alert: 16791da177e4SLinus Torvalds * Return value with the low bits set means error value, 16801da177e4SLinus Torvalds * ie 16811da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 16821da177e4SLinus Torvalds * error = ret; 16831da177e4SLinus Torvalds * 16841da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 16851da177e4SLinus Torvalds */ 16861da177e4SLinus Torvalds unsigned long 16874b439e25SChristophe Leroy generic_get_unmapped_area(struct file *filp, unsigned long addr, 16884b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16894b439e25SChristophe Leroy unsigned long flags) 16901da177e4SLinus Torvalds { 16911da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 16921be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 1693db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 16942cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 16951da177e4SLinus Torvalds 1696f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 16971da177e4SLinus Torvalds return -ENOMEM; 16981da177e4SLinus Torvalds 169906abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 170006abdfb4SBenjamin Herrenschmidt return addr; 170106abdfb4SBenjamin Herrenschmidt 17021da177e4SLinus Torvalds if (addr) { 17031da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 17041be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1705f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 17061be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 17071be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 17081da177e4SLinus Torvalds return addr; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 1711db4fbfb9SMichel Lespinasse info.flags = 0; 1712db4fbfb9SMichel Lespinasse info.length = len; 17134e99b021SHeiko Carstens info.low_limit = mm->mmap_base; 1714f6795053SSteve Capper info.high_limit = mmap_end; 1715db4fbfb9SMichel Lespinasse info.align_mask = 0; 171609ef5283SJaewon Kim info.align_offset = 0; 1717db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 17181da177e4SLinus Torvalds } 17194b439e25SChristophe Leroy 17204b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA 17214b439e25SChristophe Leroy unsigned long 17224b439e25SChristophe Leroy arch_get_unmapped_area(struct file *filp, unsigned long addr, 17234b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 17244b439e25SChristophe Leroy unsigned long flags) 17254b439e25SChristophe Leroy { 17264b439e25SChristophe Leroy return generic_get_unmapped_area(filp, addr, len, pgoff, flags); 17274b439e25SChristophe Leroy } 17281da177e4SLinus Torvalds #endif 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds /* 17311da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 17321da177e4SLinus Torvalds * stack's low limit (the base): 17331da177e4SLinus Torvalds */ 17341da177e4SLinus Torvalds unsigned long 17354b439e25SChristophe Leroy generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 173643cca0b1SYang Fan unsigned long len, unsigned long pgoff, 173743cca0b1SYang Fan unsigned long flags) 17381da177e4SLinus Torvalds { 17391be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 17401da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1741db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 17422cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds /* requested length too big for entire address space */ 1745f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 17461da177e4SLinus Torvalds return -ENOMEM; 17471da177e4SLinus Torvalds 174806abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 174906abdfb4SBenjamin Herrenschmidt return addr; 175006abdfb4SBenjamin Herrenschmidt 17511da177e4SLinus Torvalds /* requesting a specific address */ 17521da177e4SLinus Torvalds if (addr) { 17531da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 17541be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1755f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 17561be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 17571be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 17581da177e4SLinus Torvalds return addr; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 1761db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 1762db4fbfb9SMichel Lespinasse info.length = len; 17632afc745fSAkira Takeuchi info.low_limit = max(PAGE_SIZE, mmap_min_addr); 1764f6795053SSteve Capper info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); 1765db4fbfb9SMichel Lespinasse info.align_mask = 0; 176609ef5283SJaewon Kim info.align_offset = 0; 1767db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1768b716ad95SXiao Guangrong 17691da177e4SLinus Torvalds /* 17701da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 17711da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 17721da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 17731da177e4SLinus Torvalds * allocations. 17741da177e4SLinus Torvalds */ 1775de1741a1SAlexander Kuleshov if (offset_in_page(addr)) { 1776db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 1777db4fbfb9SMichel Lespinasse info.flags = 0; 1778db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1779f6795053SSteve Capper info.high_limit = mmap_end; 1780db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1781db4fbfb9SMichel Lespinasse } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds return addr; 17841da177e4SLinus Torvalds } 17854b439e25SChristophe Leroy 17864b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 17874b439e25SChristophe Leroy unsigned long 17884b439e25SChristophe Leroy arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 17894b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 17904b439e25SChristophe Leroy unsigned long flags) 17914b439e25SChristophe Leroy { 17924b439e25SChristophe Leroy return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); 17934b439e25SChristophe Leroy } 17941da177e4SLinus Torvalds #endif 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds unsigned long 17971da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 17981da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 17991da177e4SLinus Torvalds { 180006abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 180106abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 180207ab67c8SLinus Torvalds 18039206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 18049206de95SAl Viro if (error) 18059206de95SAl Viro return error; 18069206de95SAl Viro 18079206de95SAl Viro /* Careful about overflows.. */ 18089206de95SAl Viro if (len > TASK_SIZE) 18099206de95SAl Viro return -ENOMEM; 18109206de95SAl Viro 181107ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 1812c01d5b30SHugh Dickins if (file) { 1813c01d5b30SHugh Dickins if (file->f_op->get_unmapped_area) 181407ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 1815c01d5b30SHugh Dickins } else if (flags & MAP_SHARED) { 1816c01d5b30SHugh Dickins /* 1817c01d5b30SHugh Dickins * mmap_region() will call shmem_zero_setup() to create a file, 1818c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge. 181945e55300SPeter Collingbourne * do_mmap() will clear pgoff, so match alignment. 1820c01d5b30SHugh Dickins */ 1821c01d5b30SHugh Dickins pgoff = 0; 1822c01d5b30SHugh Dickins get_area = shmem_get_unmapped_area; 1823f35b5d7dSRik van Riel } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 1824f35b5d7dSRik van Riel /* Ensures that larger anonymous mappings are THP aligned. */ 1825f35b5d7dSRik van Riel get_area = thp_get_unmapped_area; 1826c01d5b30SHugh Dickins } 1827c01d5b30SHugh Dickins 182807ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 182907ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 183007ab67c8SLinus Torvalds return addr; 183107ab67c8SLinus Torvalds 18321da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 18331da177e4SLinus Torvalds return -ENOMEM; 1834de1741a1SAlexander Kuleshov if (offset_in_page(addr)) 18351da177e4SLinus Torvalds return -EINVAL; 183606abdfb4SBenjamin Herrenschmidt 18379ac4ed4bSAl Viro error = security_mmap_addr(addr); 18389ac4ed4bSAl Viro return error ? error : addr; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 18421da177e4SLinus Torvalds 1843be8432e7SLiam R. Howlett /** 1844abdba2ddSLiam R. Howlett * find_vma_intersection() - Look up the first VMA which intersects the interval 1845abdba2ddSLiam R. Howlett * @mm: The process address space. 1846abdba2ddSLiam R. Howlett * @start_addr: The inclusive start user address. 1847abdba2ddSLiam R. Howlett * @end_addr: The exclusive end user address. 1848abdba2ddSLiam R. Howlett * 1849abdba2ddSLiam R. Howlett * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 1850abdba2ddSLiam R. Howlett * start_addr < end_addr. 1851abdba2ddSLiam R. Howlett */ 1852abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 1853abdba2ddSLiam R. Howlett unsigned long start_addr, 1854abdba2ddSLiam R. Howlett unsigned long end_addr) 1855abdba2ddSLiam R. Howlett { 1856abdba2ddSLiam R. Howlett unsigned long index = start_addr; 1857abdba2ddSLiam R. Howlett 1858abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 18597964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 1860abdba2ddSLiam R. Howlett } 1861abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 1862abdba2ddSLiam R. Howlett 1863abdba2ddSLiam R. Howlett /** 1864be8432e7SLiam R. Howlett * find_vma() - Find the VMA for a given address, or the next VMA. 1865be8432e7SLiam R. Howlett * @mm: The mm_struct to check 1866be8432e7SLiam R. Howlett * @addr: The address 1867be8432e7SLiam R. Howlett * 1868be8432e7SLiam R. Howlett * Returns: The VMA associated with addr, or the next VMA. 1869be8432e7SLiam R. Howlett * May return %NULL in the case of no VMA at addr or above. 1870be8432e7SLiam R. Howlett */ 18711da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 18721da177e4SLinus Torvalds { 1873be8432e7SLiam R. Howlett unsigned long index = addr; 18741da177e4SLinus Torvalds 18755b78ed24SLuigi Rizzo mmap_assert_locked(mm); 18767964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, ULONG_MAX); 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 18791da177e4SLinus Torvalds 18807fdbd37dSLiam R. Howlett /** 18817fdbd37dSLiam R. Howlett * find_vma_prev() - Find the VMA for a given address, or the next vma and 18827fdbd37dSLiam R. Howlett * set %pprev to the previous VMA, if any. 18837fdbd37dSLiam R. Howlett * @mm: The mm_struct to check 18847fdbd37dSLiam R. Howlett * @addr: The address 18857fdbd37dSLiam R. Howlett * @pprev: The pointer to set to the previous VMA 18867fdbd37dSLiam R. Howlett * 18877fdbd37dSLiam R. Howlett * Note that RCU lock is missing here since the external mmap_lock() is used 18887fdbd37dSLiam R. Howlett * instead. 18897fdbd37dSLiam R. Howlett * 18907fdbd37dSLiam R. Howlett * Returns: The VMA associated with @addr, or the next vma. 18917fdbd37dSLiam R. Howlett * May return %NULL in the case of no vma at addr or above. 18926bd4837dSKOSAKI Motohiro */ 18931da177e4SLinus Torvalds struct vm_area_struct * 18941da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 18951da177e4SLinus Torvalds struct vm_area_struct **pprev) 18961da177e4SLinus Torvalds { 18976bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 18987fdbd37dSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 18991da177e4SLinus Torvalds 19007fdbd37dSLiam R. Howlett vma = mas_walk(&mas); 19017fdbd37dSLiam R. Howlett *pprev = mas_prev(&mas, 0); 19027fdbd37dSLiam R. Howlett if (!vma) 19037fdbd37dSLiam R. Howlett vma = mas_next(&mas, ULONG_MAX); 19046bd4837dSKOSAKI Motohiro return vma; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds /* 19081da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 19091da177e4SLinus Torvalds * update accounting. This is shared with both the 19101da177e4SLinus Torvalds * grow-up and grow-down cases. 19111da177e4SLinus Torvalds */ 19121be7107fSHugh Dickins static int acct_stack_growth(struct vm_area_struct *vma, 19131be7107fSHugh Dickins unsigned long size, unsigned long grow) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 19161be7107fSHugh Dickins unsigned long new_start; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds /* address space limit tests */ 191984638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, grow)) 19201da177e4SLinus Torvalds return -ENOMEM; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* Stack limit test */ 192324c79d8eSKrzysztof Opasiak if (size > rlimit(RLIMIT_STACK)) 19241da177e4SLinus Torvalds return -ENOMEM; 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* mlock limit tests */ 1927c5d8a364SMiaohe Lin if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) 19281da177e4SLinus Torvalds return -ENOMEM; 19291da177e4SLinus Torvalds 19300d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 19310d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 19320d59a01bSAdam Litke vma->vm_end - size; 19330d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 19340d59a01bSAdam Litke return -EFAULT; 19350d59a01bSAdam Litke 19361da177e4SLinus Torvalds /* 19371da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 19381da177e4SLinus Torvalds * update security statistics. 19391da177e4SLinus Torvalds */ 194005fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 19411da177e4SLinus Torvalds return -ENOMEM; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds return 0; 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds 194646dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 19471da177e4SLinus Torvalds /* 194846dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 194946dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 19501da177e4SLinus Torvalds */ 195146dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 19521da177e4SLinus Torvalds { 195309357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 19541be7107fSHugh Dickins struct vm_area_struct *next; 19551be7107fSHugh Dickins unsigned long gap_addr; 195612352d3cSKonstantin Khlebnikov int error = 0; 1957d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 19601da177e4SLinus Torvalds return -EFAULT; 19611da177e4SLinus Torvalds 1962bd726c90SHelge Deller /* Guard against exceeding limits of the address space. */ 19631be7107fSHugh Dickins address &= PAGE_MASK; 196437511fb5SHelge Deller if (address >= (TASK_SIZE & PAGE_MASK)) 196512352d3cSKonstantin Khlebnikov return -ENOMEM; 1966bd726c90SHelge Deller address += PAGE_SIZE; 196712352d3cSKonstantin Khlebnikov 19681be7107fSHugh Dickins /* Enforce stack_guard_gap */ 19691be7107fSHugh Dickins gap_addr = address + stack_guard_gap; 1970bd726c90SHelge Deller 1971bd726c90SHelge Deller /* Guard against overflow */ 1972bd726c90SHelge Deller if (gap_addr < address || gap_addr > TASK_SIZE) 1973bd726c90SHelge Deller gap_addr = TASK_SIZE; 1974bd726c90SHelge Deller 1975*763ecb03SLiam R. Howlett next = find_vma_intersection(mm, vma->vm_end, gap_addr); 1976*763ecb03SLiam R. Howlett if (next && vma_is_accessible(next)) { 19771be7107fSHugh Dickins if (!(next->vm_flags & VM_GROWSUP)) 19781be7107fSHugh Dickins return -ENOMEM; 19791be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 19801be7107fSHugh Dickins } 19811be7107fSHugh Dickins 1982d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 19831da177e4SLinus Torvalds return -ENOMEM; 19841da177e4SLinus Torvalds 1985d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 1986d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 1987d4af56c5SLiam R. Howlett mas_destroy(&mas); 1988d4af56c5SLiam R. Howlett return -ENOMEM; 1989d4af56c5SLiam R. Howlett } 1990d4af56c5SLiam R. Howlett 19911da177e4SLinus Torvalds /* 19921da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 1993c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 19941da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 19951da177e4SLinus Torvalds */ 199612352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 19991da177e4SLinus Torvalds if (address > vma->vm_end) { 20001da177e4SLinus Torvalds unsigned long size, grow; 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds size = address - vma->vm_start; 20031da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 20041da177e4SLinus Torvalds 200542c36f63SHugh Dickins error = -ENOMEM; 200642c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 20071da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 20083af9e859SEric B Munson if (!error) { 20094128997bSMichel Lespinasse /* 2010524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2011524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2012524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2013524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2014524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2015524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2016524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 20174128997bSMichel Lespinasse */ 201809357814SOleg Nesterov spin_lock(&mm->page_table_lock); 201987e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 202009357814SOleg Nesterov mm->locked_vm += grow; 202184638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2022bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 20231da177e4SLinus Torvalds vma->vm_end = address; 2024d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2025d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 2026bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 202709357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 20284128997bSMichel Lespinasse 20293af9e859SEric B Munson perf_event_mmap(vma); 20303af9e859SEric B Munson } 20311da177e4SLinus Torvalds } 203242c36f63SHugh Dickins } 203312352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2034c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2035d4af56c5SLiam R. Howlett mas_destroy(&mas); 20361da177e4SLinus Torvalds return error; 20371da177e4SLinus Torvalds } 203846dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 203946dea3d0SHugh Dickins 20401da177e4SLinus Torvalds /* 20411da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 20421da177e4SLinus Torvalds */ 2043524e00b3SLiam R. Howlett int expand_downwards(struct vm_area_struct *vma, unsigned long address) 20441da177e4SLinus Torvalds { 204509357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 2046*763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); 20471be7107fSHugh Dickins struct vm_area_struct *prev; 20480a1d5299SJann Horn int error = 0; 20491da177e4SLinus Torvalds 20508869477aSEric Paris address &= PAGE_MASK; 20510a1d5299SJann Horn if (address < mmap_min_addr) 20520a1d5299SJann Horn return -EPERM; 20538869477aSEric Paris 20541be7107fSHugh Dickins /* Enforce stack_guard_gap */ 2055*763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 20561be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 205732e4e6d5SOleg Nesterov if (prev && !(prev->vm_flags & VM_GROWSDOWN) && 20583122e80eSAnshuman Khandual vma_is_accessible(prev)) { 205932e4e6d5SOleg Nesterov if (address - prev->vm_end < stack_guard_gap) 206032e4e6d5SOleg Nesterov return -ENOMEM; 20611be7107fSHugh Dickins } 20621be7107fSHugh Dickins 2063d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 206412352d3cSKonstantin Khlebnikov return -ENOMEM; 20651da177e4SLinus Torvalds 2066d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2067d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2068d4af56c5SLiam R. Howlett mas_destroy(&mas); 2069d4af56c5SLiam R. Howlett return -ENOMEM; 2070d4af56c5SLiam R. Howlett } 2071d4af56c5SLiam R. Howlett 20721da177e4SLinus Torvalds /* 20731da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2074c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 20751da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 20761da177e4SLinus Torvalds */ 207712352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 20801da177e4SLinus Torvalds if (address < vma->vm_start) { 20811da177e4SLinus Torvalds unsigned long size, grow; 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds size = vma->vm_end - address; 20841da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 20851da177e4SLinus Torvalds 2086a626ca6aSLinus Torvalds error = -ENOMEM; 2087a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 20881da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 20891da177e4SLinus Torvalds if (!error) { 20904128997bSMichel Lespinasse /* 2091524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2092524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2093524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2094524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2095524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2096524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2097524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 20984128997bSMichel Lespinasse */ 209909357814SOleg Nesterov spin_lock(&mm->page_table_lock); 210087e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 210109357814SOleg Nesterov mm->locked_vm += grow; 210284638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2103bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 21041da177e4SLinus Torvalds vma->vm_start = address; 21051da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2106d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2107d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 2108bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 210909357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 21104128997bSMichel Lespinasse 21113af9e859SEric B Munson perf_event_mmap(vma); 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds } 2114a626ca6aSLinus Torvalds } 211512352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2116c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2117d4af56c5SLiam R. Howlett mas_destroy(&mas); 21181da177e4SLinus Torvalds return error; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 21211be7107fSHugh Dickins /* enforced gap between the expanding stack and other mappings. */ 21221be7107fSHugh Dickins unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; 21231be7107fSHugh Dickins 21241be7107fSHugh Dickins static int __init cmdline_parse_stack_guard_gap(char *p) 21251be7107fSHugh Dickins { 21261be7107fSHugh Dickins unsigned long val; 21271be7107fSHugh Dickins char *endptr; 21281be7107fSHugh Dickins 21291be7107fSHugh Dickins val = simple_strtoul(p, &endptr, 10); 21301be7107fSHugh Dickins if (!*endptr) 21311be7107fSHugh Dickins stack_guard_gap = val << PAGE_SHIFT; 21321be7107fSHugh Dickins 2133e6d09493SRandy Dunlap return 1; 21341be7107fSHugh Dickins } 21351be7107fSHugh Dickins __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); 21361be7107fSHugh Dickins 2137b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2138b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2139b6a2fea3SOllie Wild { 2140b6a2fea3SOllie Wild return expand_upwards(vma, address); 2141b6a2fea3SOllie Wild } 2142b6a2fea3SOllie Wild 2143b6a2fea3SOllie Wild struct vm_area_struct * 2144b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2145b6a2fea3SOllie Wild { 2146b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2147b6a2fea3SOllie Wild 2148b6a2fea3SOllie Wild addr &= PAGE_MASK; 2149b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2150b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2151b6a2fea3SOllie Wild return vma; 21524d45e75aSJann Horn if (!prev || expand_stack(prev, addr)) 2153b6a2fea3SOllie Wild return NULL; 2154cea10a19SMichel Lespinasse if (prev->vm_flags & VM_LOCKED) 2155fc05f566SKirill A. Shutemov populate_vma_page_range(prev, addr, prev->vm_end, NULL); 2156b6a2fea3SOllie Wild return prev; 2157b6a2fea3SOllie Wild } 2158b6a2fea3SOllie Wild #else 2159b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2160b6a2fea3SOllie Wild { 2161b6a2fea3SOllie Wild return expand_downwards(vma, address); 2162b6a2fea3SOllie Wild } 2163b6a2fea3SOllie Wild 21641da177e4SLinus Torvalds struct vm_area_struct * 21651da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 21661da177e4SLinus Torvalds { 21671da177e4SLinus Torvalds struct vm_area_struct *vma; 21681da177e4SLinus Torvalds unsigned long start; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds addr &= PAGE_MASK; 21711da177e4SLinus Torvalds vma = find_vma(mm, addr); 21721da177e4SLinus Torvalds if (!vma) 21731da177e4SLinus Torvalds return NULL; 21741da177e4SLinus Torvalds if (vma->vm_start <= addr) 21751da177e4SLinus Torvalds return vma; 21761da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 21771da177e4SLinus Torvalds return NULL; 21781da177e4SLinus Torvalds start = vma->vm_start; 21791da177e4SLinus Torvalds if (expand_stack(vma, addr)) 21801da177e4SLinus Torvalds return NULL; 2181cea10a19SMichel Lespinasse if (vma->vm_flags & VM_LOCKED) 2182fc05f566SKirill A. Shutemov populate_vma_page_range(vma, addr, start, NULL); 21831da177e4SLinus Torvalds return vma; 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds #endif 21861da177e4SLinus Torvalds 2187e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(find_extend_vma); 2188e1d6d01aSJesse Barnes 21892c0b3814SHugh Dickins /* 2190*763ecb03SLiam R. Howlett * Ok - we have the memory areas we should free on a maple tree so release them, 2191*763ecb03SLiam R. Howlett * and do the vma updates. 21921da177e4SLinus Torvalds * 21932c0b3814SHugh Dickins * Called with the mm semaphore held. 21941da177e4SLinus Torvalds */ 2195*763ecb03SLiam R. Howlett static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) 21961da177e4SLinus Torvalds { 21974f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 2198*763ecb03SLiam R. Howlett struct vm_area_struct *vma; 21994f74d2c8SLinus Torvalds 2200365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2201365e9c87SHugh Dickins update_hiwater_vm(mm); 2202*763ecb03SLiam R. Howlett mas_for_each(mas, vma, ULONG_MAX) { 2203ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 22041da177e4SLinus Torvalds 22054f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 22064f74d2c8SLinus Torvalds nr_accounted += nrpages; 220784638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, -nrpages); 2208*763ecb03SLiam R. Howlett remove_vma(vma); 2209*763ecb03SLiam R. Howlett } 22104f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 22111da177e4SLinus Torvalds validate_mm(mm); 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds /* 22151da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 22161da177e4SLinus Torvalds * 2217f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 22181da177e4SLinus Torvalds */ 2219*763ecb03SLiam R. Howlett static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, 2220e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2221*763ecb03SLiam R. Howlett struct vm_area_struct *next, 2222e0da382cSHugh Dickins unsigned long start, unsigned long end) 22231da177e4SLinus Torvalds { 2224d16dfc55SPeter Zijlstra struct mmu_gather tlb; 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds lru_add_drain(); 2227a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm); 2228365e9c87SHugh Dickins update_hiwater_rss(mm); 2229*763ecb03SLiam R. Howlett unmap_vmas(&tlb, mt, vma, start, end); 2230*763ecb03SLiam R. Howlett free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 22316ee8630eSHugh Dickins next ? next->vm_start : USER_PGTABLES_CEILING); 2232ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds /* 2236def5efe0SDavid Rientjes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 2237def5efe0SDavid Rientjes * has already been checked or doesn't make sense to fail. 22381da177e4SLinus Torvalds */ 2239def5efe0SDavid Rientjes int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 22401da177e4SLinus Torvalds unsigned long addr, int new_below) 22411da177e4SLinus Torvalds { 22421da177e4SLinus Torvalds struct vm_area_struct *new; 2243e3975891SChen Gang int err; 2244d4af56c5SLiam R. Howlett validate_mm_mt(mm); 22451da177e4SLinus Torvalds 2246dd3b614fSDmitry Safonov if (vma->vm_ops && vma->vm_ops->may_split) { 2247dd3b614fSDmitry Safonov err = vma->vm_ops->may_split(vma, addr); 224831383c68SDan Williams if (err) 224931383c68SDan Williams return err; 225031383c68SDan Williams } 22511da177e4SLinus Torvalds 22523928d4f5SLinus Torvalds new = vm_area_dup(vma); 22531da177e4SLinus Torvalds if (!new) 2254e3975891SChen Gang return -ENOMEM; 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds if (new_below) 22571da177e4SLinus Torvalds new->vm_end = addr; 22581da177e4SLinus Torvalds else { 22591da177e4SLinus Torvalds new->vm_start = addr; 22601da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 2263ef0855d3SOleg Nesterov err = vma_dup_policy(vma, new); 2264ef0855d3SOleg Nesterov if (err) 22655beb4930SRik van Riel goto out_free_vma; 22661da177e4SLinus Torvalds 2267c4ea95d7SDaniel Forrest err = anon_vma_clone(new, vma); 2268c4ea95d7SDaniel Forrest if (err) 22695beb4930SRik van Riel goto out_free_mpol; 22705beb4930SRik van Riel 2271e9714acfSKonstantin Khlebnikov if (new->vm_file) 22721da177e4SLinus Torvalds get_file(new->vm_file); 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 22751da177e4SLinus Torvalds new->vm_ops->open(new); 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds if (new_below) 22785beb4930SRik van Riel err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + 22791da177e4SLinus Torvalds ((addr - new->vm_start) >> PAGE_SHIFT), new); 22801da177e4SLinus Torvalds else 22815beb4930SRik van Riel err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); 22821da177e4SLinus Torvalds 22835beb4930SRik van Riel /* Success. */ 22845beb4930SRik van Riel if (!err) 22851da177e4SLinus Torvalds return 0; 22865beb4930SRik van Riel 2287d4af56c5SLiam R. Howlett /* Avoid vm accounting in close() operation */ 2288d4af56c5SLiam R. Howlett new->vm_start = new->vm_end; 2289d4af56c5SLiam R. Howlett new->vm_pgoff = 0; 22905beb4930SRik van Riel /* Clean everything up if vma_adjust failed. */ 229158927533SRik van Riel if (new->vm_ops && new->vm_ops->close) 22925beb4930SRik van Riel new->vm_ops->close(new); 2293e9714acfSKonstantin Khlebnikov if (new->vm_file) 22945beb4930SRik van Riel fput(new->vm_file); 22952aeadc30SAndrea Arcangeli unlink_anon_vmas(new); 22965beb4930SRik van Riel out_free_mpol: 2297ef0855d3SOleg Nesterov mpol_put(vma_policy(new)); 22985beb4930SRik van Riel out_free_vma: 22993928d4f5SLinus Torvalds vm_area_free(new); 2300d4af56c5SLiam R. Howlett validate_mm_mt(mm); 23015beb4930SRik van Riel return err; 23021da177e4SLinus Torvalds } 23031da177e4SLinus Torvalds 2304659ace58SKOSAKI Motohiro /* 2305659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2306659ace58SKOSAKI Motohiro * either for the first part or the tail. 2307659ace58SKOSAKI Motohiro */ 2308659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 2309659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2310659ace58SKOSAKI Motohiro { 2311659ace58SKOSAKI Motohiro if (mm->map_count >= sysctl_max_map_count) 2312659ace58SKOSAKI Motohiro return -ENOMEM; 2313659ace58SKOSAKI Motohiro 2314659ace58SKOSAKI Motohiro return __split_vma(mm, vma, addr, new_below); 2315659ace58SKOSAKI Motohiro } 2316659ace58SKOSAKI Motohiro 2317*763ecb03SLiam R. Howlett static inline int munmap_sidetree(struct vm_area_struct *vma, 2318*763ecb03SLiam R. Howlett struct ma_state *mas_detach) 23191da177e4SLinus Torvalds { 2320*763ecb03SLiam R. Howlett mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); 2321*763ecb03SLiam R. Howlett if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) 2322*763ecb03SLiam R. Howlett return -ENOMEM; 232311f9a21aSLiam R. Howlett 2324*763ecb03SLiam R. Howlett if (vma->vm_flags & VM_LOCKED) 2325*763ecb03SLiam R. Howlett vma->vm_mm->locked_vm -= vma_pages(vma); 232611f9a21aSLiam R. Howlett 2327*763ecb03SLiam R. Howlett return 0; 232811f9a21aSLiam R. Howlett } 232911f9a21aSLiam R. Howlett 233011f9a21aSLiam R. Howlett /* 233111f9a21aSLiam R. Howlett * do_mas_align_munmap() - munmap the aligned region from @start to @end. 233211f9a21aSLiam R. Howlett * @mas: The maple_state, ideally set up to alter the correct tree location. 233311f9a21aSLiam R. Howlett * @vma: The starting vm_area_struct 233411f9a21aSLiam R. Howlett * @mm: The mm_struct 233511f9a21aSLiam R. Howlett * @start: The aligned start address to munmap. 233611f9a21aSLiam R. Howlett * @end: The aligned end address to munmap. 233711f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 233811f9a21aSLiam R. Howlett * @downgrade: Set to true to attempt a write downgrade of the mmap_sem 233911f9a21aSLiam R. Howlett * 234011f9a21aSLiam R. Howlett * If @downgrade is true, check return code for potential release of the lock. 234111f9a21aSLiam R. Howlett */ 234211f9a21aSLiam R. Howlett static int 234311f9a21aSLiam R. Howlett do_mas_align_munmap(struct ma_state *mas, struct vm_area_struct *vma, 234411f9a21aSLiam R. Howlett struct mm_struct *mm, unsigned long start, 234511f9a21aSLiam R. Howlett unsigned long end, struct list_head *uf, bool downgrade) 234611f9a21aSLiam R. Howlett { 2347*763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next = NULL; 2348*763ecb03SLiam R. Howlett struct maple_tree mt_detach; 2349*763ecb03SLiam R. Howlett int count = 0; 2350d4af56c5SLiam R. Howlett int error = -ENOMEM; 2351*763ecb03SLiam R. Howlett MA_STATE(mas_detach, &mt_detach, 0, 0); 2352*763ecb03SLiam R. Howlett mt_init_flags(&mt_detach, MT_FLAGS_LOCK_EXTERN); 2353*763ecb03SLiam R. Howlett mt_set_external_lock(&mt_detach, &mm->mmap_lock); 2354524e00b3SLiam R. Howlett 235511f9a21aSLiam R. Howlett if (mas_preallocate(mas, vma, GFP_KERNEL)) 235611f9a21aSLiam R. Howlett return -ENOMEM; 235711f9a21aSLiam R. Howlett 235811f9a21aSLiam R. Howlett mas->last = end - 1; 23591da177e4SLinus Torvalds /* 23601da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 23611da177e4SLinus Torvalds * 23621da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 23631da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 23641da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 23651da177e4SLinus Torvalds */ 2366*763ecb03SLiam R. Howlett 2367*763ecb03SLiam R. Howlett /* Does it split the first one? */ 2368146425a3SHugh Dickins if (start > vma->vm_start) { 2369659ace58SKOSAKI Motohiro 2370659ace58SKOSAKI Motohiro /* 2371659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2372659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2373659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2374659ace58SKOSAKI Motohiro */ 2375659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2376d4af56c5SLiam R. Howlett goto map_count_exceeded; 2377659ace58SKOSAKI Motohiro 2378*763ecb03SLiam R. Howlett /* 2379*763ecb03SLiam R. Howlett * mas_pause() is not needed since mas->index needs to be set 2380*763ecb03SLiam R. Howlett * differently than vma->vm_end anyways. 2381*763ecb03SLiam R. Howlett */ 2382659ace58SKOSAKI Motohiro error = __split_vma(mm, vma, start, 0); 23831da177e4SLinus Torvalds if (error) 2384*763ecb03SLiam R. Howlett goto start_split_failed; 238511f9a21aSLiam R. Howlett 2386*763ecb03SLiam R. Howlett mas_set(mas, start); 2387*763ecb03SLiam R. Howlett vma = mas_walk(mas); 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 2390*763ecb03SLiam R. Howlett prev = mas_prev(mas, 0); 2391*763ecb03SLiam R. Howlett if (unlikely((!prev))) 2392*763ecb03SLiam R. Howlett mas_set(mas, start); 239311f9a21aSLiam R. Howlett 2394*763ecb03SLiam R. Howlett /* 2395*763ecb03SLiam R. Howlett * Detach a range of VMAs from the mm. Using next as a temp variable as 2396*763ecb03SLiam R. Howlett * it is always overwritten. 2397*763ecb03SLiam R. Howlett */ 2398*763ecb03SLiam R. Howlett mas_for_each(mas, next, end - 1) { 2399*763ecb03SLiam R. Howlett /* Does it split the end? */ 2400*763ecb03SLiam R. Howlett if (next->vm_end > end) { 2401*763ecb03SLiam R. Howlett struct vm_area_struct *split; 240211f9a21aSLiam R. Howlett 2403*763ecb03SLiam R. Howlett error = __split_vma(mm, next, end, 1); 24041da177e4SLinus Torvalds if (error) 2405*763ecb03SLiam R. Howlett goto end_split_failed; 240611f9a21aSLiam R. Howlett 2407*763ecb03SLiam R. Howlett mas_set(mas, end); 2408*763ecb03SLiam R. Howlett split = mas_prev(mas, 0); 2409*763ecb03SLiam R. Howlett error = munmap_sidetree(split, &mas_detach); 2410*763ecb03SLiam R. Howlett if (error) 2411*763ecb03SLiam R. Howlett goto munmap_sidetree_failed; 2412*763ecb03SLiam R. Howlett 2413*763ecb03SLiam R. Howlett count++; 2414*763ecb03SLiam R. Howlett if (vma == next) 2415*763ecb03SLiam R. Howlett vma = split; 2416*763ecb03SLiam R. Howlett break; 241711f9a21aSLiam R. Howlett } 2418*763ecb03SLiam R. Howlett error = munmap_sidetree(next, &mas_detach); 2419*763ecb03SLiam R. Howlett if (error) 2420*763ecb03SLiam R. Howlett goto munmap_sidetree_failed; 2421*763ecb03SLiam R. Howlett 2422*763ecb03SLiam R. Howlett count++; 2423*763ecb03SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 2424*763ecb03SLiam R. Howlett BUG_ON(next->vm_start < start); 2425*763ecb03SLiam R. Howlett BUG_ON(next->vm_start > end); 2426*763ecb03SLiam R. Howlett #endif 2427*763ecb03SLiam R. Howlett } 2428*763ecb03SLiam R. Howlett 2429*763ecb03SLiam R. Howlett if (!next) 2430*763ecb03SLiam R. Howlett next = mas_next(mas, ULONG_MAX); 24311da177e4SLinus Torvalds 24322376dd7cSAndrea Arcangeli if (unlikely(uf)) { 24332376dd7cSAndrea Arcangeli /* 24342376dd7cSAndrea Arcangeli * If userfaultfd_unmap_prep returns an error the vmas 2435f0953a1bSIngo Molnar * will remain split, but userland will get a 24362376dd7cSAndrea Arcangeli * highly unexpected error anyway. This is no 24372376dd7cSAndrea Arcangeli * different than the case where the first of the two 24382376dd7cSAndrea Arcangeli * __split_vma fails, but we don't undo the first 24392376dd7cSAndrea Arcangeli * split, despite we could. This is unlikely enough 24402376dd7cSAndrea Arcangeli * failure that it's not worth optimizing it for. 24412376dd7cSAndrea Arcangeli */ 244269dbe6daSLiam R. Howlett error = userfaultfd_unmap_prep(mm, start, end, uf); 244311f9a21aSLiam R. Howlett 24442376dd7cSAndrea Arcangeli if (error) 2445d4af56c5SLiam R. Howlett goto userfaultfd_error; 24462376dd7cSAndrea Arcangeli } 24472376dd7cSAndrea Arcangeli 2448*763ecb03SLiam R. Howlett /* Point of no return */ 2449*763ecb03SLiam R. Howlett mas_set_range(mas, start, end - 1); 2450*763ecb03SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 2451*763ecb03SLiam R. Howlett /* Make sure no VMAs are about to be lost. */ 2452*763ecb03SLiam R. Howlett { 2453*763ecb03SLiam R. Howlett MA_STATE(test, &mt_detach, start, end - 1); 2454*763ecb03SLiam R. Howlett struct vm_area_struct *vma_mas, *vma_test; 2455*763ecb03SLiam R. Howlett int test_count = 0; 2456*763ecb03SLiam R. Howlett 2457*763ecb03SLiam R. Howlett rcu_read_lock(); 2458*763ecb03SLiam R. Howlett vma_test = mas_find(&test, end - 1); 2459*763ecb03SLiam R. Howlett mas_for_each(mas, vma_mas, end - 1) { 2460*763ecb03SLiam R. Howlett BUG_ON(vma_mas != vma_test); 2461*763ecb03SLiam R. Howlett test_count++; 2462*763ecb03SLiam R. Howlett vma_test = mas_next(&test, end - 1); 2463*763ecb03SLiam R. Howlett } 2464*763ecb03SLiam R. Howlett rcu_read_unlock(); 2465*763ecb03SLiam R. Howlett BUG_ON(count != test_count); 2466*763ecb03SLiam R. Howlett mas_set_range(mas, start, end - 1); 2467*763ecb03SLiam R. Howlett } 2468*763ecb03SLiam R. Howlett #endif 246911f9a21aSLiam R. Howlett mas_store_prealloc(mas, NULL); 2470*763ecb03SLiam R. Howlett mm->map_count -= count; 247111f9a21aSLiam R. Howlett /* 247211f9a21aSLiam R. Howlett * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or 247311f9a21aSLiam R. Howlett * VM_GROWSUP VMA. Such VMAs can change their size under 247411f9a21aSLiam R. Howlett * down_read(mmap_lock) and collide with the VMA we are about to unmap. 247511f9a21aSLiam R. Howlett */ 247611f9a21aSLiam R. Howlett if (downgrade) { 2477*763ecb03SLiam R. Howlett if (next && (next->vm_flags & VM_GROWSDOWN)) 247811f9a21aSLiam R. Howlett downgrade = false; 247911f9a21aSLiam R. Howlett else if (prev && (prev->vm_flags & VM_GROWSUP)) 248011f9a21aSLiam R. Howlett downgrade = false; 248111f9a21aSLiam R. Howlett else 2482d8ed45c5SMichel Lespinasse mmap_write_downgrade(mm); 248311f9a21aSLiam R. Howlett } 2484dd2283f2SYang Shi 2485*763ecb03SLiam R. Howlett unmap_region(mm, &mt_detach, vma, prev, next, start, end); 2486*763ecb03SLiam R. Howlett /* Statistics and freeing VMAs */ 2487*763ecb03SLiam R. Howlett mas_set(&mas_detach, start); 2488*763ecb03SLiam R. Howlett remove_mt(mm, &mas_detach); 2489*763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 24901da177e4SLinus Torvalds 2491524e00b3SLiam R. Howlett 2492524e00b3SLiam R. Howlett validate_mm(mm); 2493dd2283f2SYang Shi return downgrade ? 1 : 0; 2494d4af56c5SLiam R. Howlett 2495d4af56c5SLiam R. Howlett userfaultfd_error: 2496*763ecb03SLiam R. Howlett munmap_sidetree_failed: 2497*763ecb03SLiam R. Howlett end_split_failed: 2498*763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 2499*763ecb03SLiam R. Howlett start_split_failed: 2500*763ecb03SLiam R. Howlett map_count_exceeded: 250111f9a21aSLiam R. Howlett mas_destroy(mas); 2502d4af56c5SLiam R. Howlett return error; 25031da177e4SLinus Torvalds } 25041da177e4SLinus Torvalds 250511f9a21aSLiam R. Howlett /* 250611f9a21aSLiam R. Howlett * do_mas_munmap() - munmap a given range. 250711f9a21aSLiam R. Howlett * @mas: The maple state 250811f9a21aSLiam R. Howlett * @mm: The mm_struct 250911f9a21aSLiam R. Howlett * @start: The start address to munmap 251011f9a21aSLiam R. Howlett * @len: The length of the range to munmap 251111f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 251211f9a21aSLiam R. Howlett * @downgrade: set to true if the user wants to attempt to write_downgrade the 251311f9a21aSLiam R. Howlett * mmap_sem 251411f9a21aSLiam R. Howlett * 251511f9a21aSLiam R. Howlett * This function takes a @mas that is either pointing to the previous VMA or set 251611f9a21aSLiam R. Howlett * to MA_START and sets it up to remove the mapping(s). The @len will be 251711f9a21aSLiam R. Howlett * aligned and any arch_unmap work will be preformed. 251811f9a21aSLiam R. Howlett * 251911f9a21aSLiam R. Howlett * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. 252011f9a21aSLiam R. Howlett */ 252111f9a21aSLiam R. Howlett int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm, 252211f9a21aSLiam R. Howlett unsigned long start, size_t len, struct list_head *uf, 252311f9a21aSLiam R. Howlett bool downgrade) 252411f9a21aSLiam R. Howlett { 252511f9a21aSLiam R. Howlett unsigned long end; 252611f9a21aSLiam R. Howlett struct vm_area_struct *vma; 252711f9a21aSLiam R. Howlett 252811f9a21aSLiam R. Howlett if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 252911f9a21aSLiam R. Howlett return -EINVAL; 253011f9a21aSLiam R. Howlett 253111f9a21aSLiam R. Howlett end = start + PAGE_ALIGN(len); 253211f9a21aSLiam R. Howlett if (end == start) 253311f9a21aSLiam R. Howlett return -EINVAL; 253411f9a21aSLiam R. Howlett 253511f9a21aSLiam R. Howlett /* arch_unmap() might do unmaps itself. */ 253611f9a21aSLiam R. Howlett arch_unmap(mm, start, end); 253711f9a21aSLiam R. Howlett 253811f9a21aSLiam R. Howlett /* Find the first overlapping VMA */ 253911f9a21aSLiam R. Howlett vma = mas_find(mas, end - 1); 254011f9a21aSLiam R. Howlett if (!vma) 254111f9a21aSLiam R. Howlett return 0; 254211f9a21aSLiam R. Howlett 254311f9a21aSLiam R. Howlett return do_mas_align_munmap(mas, vma, mm, start, end, uf, downgrade); 254411f9a21aSLiam R. Howlett } 254511f9a21aSLiam R. Howlett 254611f9a21aSLiam R. Howlett /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. 254711f9a21aSLiam R. Howlett * @mm: The mm_struct 254811f9a21aSLiam R. Howlett * @start: The start address to munmap 254911f9a21aSLiam R. Howlett * @len: The length to be munmapped. 255011f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 255111f9a21aSLiam R. Howlett */ 2552dd2283f2SYang Shi int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2553dd2283f2SYang Shi struct list_head *uf) 2554dd2283f2SYang Shi { 255511f9a21aSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 255611f9a21aSLiam R. Howlett 255711f9a21aSLiam R. Howlett return do_mas_munmap(&mas, mm, start, len, uf, false); 2558dd2283f2SYang Shi } 2559dd2283f2SYang Shi 2560e99668a5SLiam R. Howlett unsigned long mmap_region(struct file *file, unsigned long addr, 2561e99668a5SLiam R. Howlett unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, 2562e99668a5SLiam R. Howlett struct list_head *uf) 2563e99668a5SLiam R. Howlett { 2564e99668a5SLiam R. Howlett struct mm_struct *mm = current->mm; 2565e99668a5SLiam R. Howlett struct vm_area_struct *vma = NULL; 2566e99668a5SLiam R. Howlett struct vm_area_struct *next, *prev, *merge; 2567e99668a5SLiam R. Howlett pgoff_t pglen = len >> PAGE_SHIFT; 2568e99668a5SLiam R. Howlett unsigned long charged = 0; 2569e99668a5SLiam R. Howlett unsigned long end = addr + len; 2570e99668a5SLiam R. Howlett unsigned long merge_start = addr, merge_end = end; 2571e99668a5SLiam R. Howlett pgoff_t vm_pgoff; 2572e99668a5SLiam R. Howlett int error; 2573e99668a5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, end - 1); 2574e99668a5SLiam R. Howlett 2575e99668a5SLiam R. Howlett /* Check against address space limit. */ 2576e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { 2577e99668a5SLiam R. Howlett unsigned long nr_pages; 2578e99668a5SLiam R. Howlett 2579e99668a5SLiam R. Howlett /* 2580e99668a5SLiam R. Howlett * MAP_FIXED may remove pages of mappings that intersects with 2581e99668a5SLiam R. Howlett * requested mapping. Account for the pages it would unmap. 2582e99668a5SLiam R. Howlett */ 2583e99668a5SLiam R. Howlett nr_pages = count_vma_pages_range(mm, addr, end); 2584e99668a5SLiam R. Howlett 2585e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, 2586e99668a5SLiam R. Howlett (len >> PAGE_SHIFT) - nr_pages)) 2587e99668a5SLiam R. Howlett return -ENOMEM; 2588e99668a5SLiam R. Howlett } 2589e99668a5SLiam R. Howlett 2590e99668a5SLiam R. Howlett /* Unmap any existing mapping in the area */ 259111f9a21aSLiam R. Howlett if (do_mas_munmap(&mas, mm, addr, len, uf, false)) 2592e99668a5SLiam R. Howlett return -ENOMEM; 2593e99668a5SLiam R. Howlett 2594e99668a5SLiam R. Howlett /* 2595e99668a5SLiam R. Howlett * Private writable mapping: check memory availability 2596e99668a5SLiam R. Howlett */ 2597e99668a5SLiam R. Howlett if (accountable_mapping(file, vm_flags)) { 2598e99668a5SLiam R. Howlett charged = len >> PAGE_SHIFT; 2599e99668a5SLiam R. Howlett if (security_vm_enough_memory_mm(mm, charged)) 2600e99668a5SLiam R. Howlett return -ENOMEM; 2601e99668a5SLiam R. Howlett vm_flags |= VM_ACCOUNT; 2602e99668a5SLiam R. Howlett } 2603e99668a5SLiam R. Howlett 2604e99668a5SLiam R. Howlett next = mas_next(&mas, ULONG_MAX); 2605e99668a5SLiam R. Howlett prev = mas_prev(&mas, 0); 2606e99668a5SLiam R. Howlett if (vm_flags & VM_SPECIAL) 2607e99668a5SLiam R. Howlett goto cannot_expand; 2608e99668a5SLiam R. Howlett 2609e99668a5SLiam R. Howlett /* Attempt to expand an old mapping */ 2610e99668a5SLiam R. Howlett /* Check next */ 2611e99668a5SLiam R. Howlett if (next && next->vm_start == end && !vma_policy(next) && 2612e99668a5SLiam R. Howlett can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, 2613e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL)) { 2614e99668a5SLiam R. Howlett merge_end = next->vm_end; 2615e99668a5SLiam R. Howlett vma = next; 2616e99668a5SLiam R. Howlett vm_pgoff = next->vm_pgoff - pglen; 2617e99668a5SLiam R. Howlett } 2618e99668a5SLiam R. Howlett 2619e99668a5SLiam R. Howlett /* Check prev */ 2620e99668a5SLiam R. Howlett if (prev && prev->vm_end == addr && !vma_policy(prev) && 2621e99668a5SLiam R. Howlett (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, 2622e99668a5SLiam R. Howlett pgoff, vma->vm_userfaultfd_ctx, NULL) : 2623e99668a5SLiam R. Howlett can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, 2624e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL))) { 2625e99668a5SLiam R. Howlett merge_start = prev->vm_start; 2626e99668a5SLiam R. Howlett vma = prev; 2627e99668a5SLiam R. Howlett vm_pgoff = prev->vm_pgoff; 2628e99668a5SLiam R. Howlett } 2629e99668a5SLiam R. Howlett 2630e99668a5SLiam R. Howlett 2631e99668a5SLiam R. Howlett /* Actually expand, if possible */ 2632e99668a5SLiam R. Howlett if (vma && 2633e99668a5SLiam R. Howlett !vma_expand(&mas, vma, merge_start, merge_end, vm_pgoff, next)) { 2634e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vm_flags); 2635e99668a5SLiam R. Howlett goto expanded; 2636e99668a5SLiam R. Howlett } 2637e99668a5SLiam R. Howlett 2638e99668a5SLiam R. Howlett mas.index = addr; 2639e99668a5SLiam R. Howlett mas.last = end - 1; 2640e99668a5SLiam R. Howlett cannot_expand: 2641e99668a5SLiam R. Howlett /* 2642e99668a5SLiam R. Howlett * Determine the object being mapped and call the appropriate 2643e99668a5SLiam R. Howlett * specific mapper. the address has already been validated, but 2644e99668a5SLiam R. Howlett * not unmapped, but the maps are removed from the list. 2645e99668a5SLiam R. Howlett */ 2646e99668a5SLiam R. Howlett vma = vm_area_alloc(mm); 2647e99668a5SLiam R. Howlett if (!vma) { 2648e99668a5SLiam R. Howlett error = -ENOMEM; 2649e99668a5SLiam R. Howlett goto unacct_error; 2650e99668a5SLiam R. Howlett } 2651e99668a5SLiam R. Howlett 2652e99668a5SLiam R. Howlett vma->vm_start = addr; 2653e99668a5SLiam R. Howlett vma->vm_end = end; 2654e99668a5SLiam R. Howlett vma->vm_flags = vm_flags; 2655e99668a5SLiam R. Howlett vma->vm_page_prot = vm_get_page_prot(vm_flags); 2656e99668a5SLiam R. Howlett vma->vm_pgoff = pgoff; 2657e99668a5SLiam R. Howlett 2658e99668a5SLiam R. Howlett if (file) { 2659e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) { 2660e99668a5SLiam R. Howlett error = mapping_map_writable(file->f_mapping); 2661e99668a5SLiam R. Howlett if (error) 2662e99668a5SLiam R. Howlett goto free_vma; 2663e99668a5SLiam R. Howlett } 2664e99668a5SLiam R. Howlett 2665e99668a5SLiam R. Howlett vma->vm_file = get_file(file); 2666e99668a5SLiam R. Howlett error = call_mmap(file, vma); 2667e99668a5SLiam R. Howlett if (error) 2668e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2669e99668a5SLiam R. Howlett 2670e99668a5SLiam R. Howlett /* Can addr have changed?? 2671e99668a5SLiam R. Howlett * 2672e99668a5SLiam R. Howlett * Answer: Yes, several device drivers can do it in their 2673e99668a5SLiam R. Howlett * f_op->mmap method. -DaveM 2674e99668a5SLiam R. Howlett */ 2675e99668a5SLiam R. Howlett WARN_ON_ONCE(addr != vma->vm_start); 2676e99668a5SLiam R. Howlett 2677e99668a5SLiam R. Howlett addr = vma->vm_start; 2678e99668a5SLiam R. Howlett mas_reset(&mas); 2679e99668a5SLiam R. Howlett 2680e99668a5SLiam R. Howlett /* 2681e99668a5SLiam R. Howlett * If vm_flags changed after call_mmap(), we should try merge 2682e99668a5SLiam R. Howlett * vma again as we may succeed this time. 2683e99668a5SLiam R. Howlett */ 2684e99668a5SLiam R. Howlett if (unlikely(vm_flags != vma->vm_flags && prev)) { 2685e99668a5SLiam R. Howlett merge = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags, 2686e99668a5SLiam R. Howlett NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL); 2687e99668a5SLiam R. Howlett if (merge) { 2688e99668a5SLiam R. Howlett /* 2689e99668a5SLiam R. Howlett * ->mmap() can change vma->vm_file and fput 2690e99668a5SLiam R. Howlett * the original file. So fput the vma->vm_file 2691e99668a5SLiam R. Howlett * here or we would add an extra fput for file 2692e99668a5SLiam R. Howlett * and cause general protection fault 2693e99668a5SLiam R. Howlett * ultimately. 2694e99668a5SLiam R. Howlett */ 2695e99668a5SLiam R. Howlett fput(vma->vm_file); 2696e99668a5SLiam R. Howlett vm_area_free(vma); 2697e99668a5SLiam R. Howlett vma = merge; 2698e99668a5SLiam R. Howlett /* Update vm_flags to pick up the change. */ 2699e99668a5SLiam R. Howlett addr = vma->vm_start; 2700e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2701e99668a5SLiam R. Howlett goto unmap_writable; 2702e99668a5SLiam R. Howlett } 2703e99668a5SLiam R. Howlett } 2704e99668a5SLiam R. Howlett 2705e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2706e99668a5SLiam R. Howlett } else if (vm_flags & VM_SHARED) { 2707e99668a5SLiam R. Howlett error = shmem_zero_setup(vma); 2708e99668a5SLiam R. Howlett if (error) 2709e99668a5SLiam R. Howlett goto free_vma; 2710e99668a5SLiam R. Howlett } else { 2711e99668a5SLiam R. Howlett vma_set_anonymous(vma); 2712e99668a5SLiam R. Howlett } 2713e99668a5SLiam R. Howlett 2714e99668a5SLiam R. Howlett /* Allow architectures to sanity-check the vm_flags */ 2715e99668a5SLiam R. Howlett if (!arch_validate_flags(vma->vm_flags)) { 2716e99668a5SLiam R. Howlett error = -EINVAL; 2717e99668a5SLiam R. Howlett if (file) 2718e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2719e99668a5SLiam R. Howlett else 2720e99668a5SLiam R. Howlett goto free_vma; 2721e99668a5SLiam R. Howlett } 2722e99668a5SLiam R. Howlett 2723e99668a5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) { 2724e99668a5SLiam R. Howlett error = -ENOMEM; 2725e99668a5SLiam R. Howlett if (file) 2726e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2727e99668a5SLiam R. Howlett else 2728e99668a5SLiam R. Howlett goto free_vma; 2729e99668a5SLiam R. Howlett } 2730e99668a5SLiam R. Howlett 2731e99668a5SLiam R. Howlett if (vma->vm_file) 2732e99668a5SLiam R. Howlett i_mmap_lock_write(vma->vm_file->f_mapping); 2733e99668a5SLiam R. Howlett 2734e99668a5SLiam R. Howlett vma_mas_store(vma, &mas); 2735e99668a5SLiam R. Howlett mm->map_count++; 2736e99668a5SLiam R. Howlett if (vma->vm_file) { 2737e99668a5SLiam R. Howlett if (vma->vm_flags & VM_SHARED) 2738e99668a5SLiam R. Howlett mapping_allow_writable(vma->vm_file->f_mapping); 2739e99668a5SLiam R. Howlett 2740e99668a5SLiam R. Howlett flush_dcache_mmap_lock(vma->vm_file->f_mapping); 2741e99668a5SLiam R. Howlett vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); 2742e99668a5SLiam R. Howlett flush_dcache_mmap_unlock(vma->vm_file->f_mapping); 2743e99668a5SLiam R. Howlett i_mmap_unlock_write(vma->vm_file->f_mapping); 2744e99668a5SLiam R. Howlett } 2745e99668a5SLiam R. Howlett 2746e99668a5SLiam R. Howlett /* 2747e99668a5SLiam R. Howlett * vma_merge() calls khugepaged_enter_vma() either, the below 2748e99668a5SLiam R. Howlett * call covers the non-merge case. 2749e99668a5SLiam R. Howlett */ 2750e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vma->vm_flags); 2751e99668a5SLiam R. Howlett 2752e99668a5SLiam R. Howlett /* Once vma denies write, undo our temporary denial count */ 2753e99668a5SLiam R. Howlett unmap_writable: 2754e99668a5SLiam R. Howlett if (file && vm_flags & VM_SHARED) 2755e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2756e99668a5SLiam R. Howlett file = vma->vm_file; 2757e99668a5SLiam R. Howlett expanded: 2758e99668a5SLiam R. Howlett perf_event_mmap(vma); 2759e99668a5SLiam R. Howlett 2760e99668a5SLiam R. Howlett vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); 2761e99668a5SLiam R. Howlett if (vm_flags & VM_LOCKED) { 2762e99668a5SLiam R. Howlett if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || 2763e99668a5SLiam R. Howlett is_vm_hugetlb_page(vma) || 2764e99668a5SLiam R. Howlett vma == get_gate_vma(current->mm)) 2765e99668a5SLiam R. Howlett vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 2766e99668a5SLiam R. Howlett else 2767e99668a5SLiam R. Howlett mm->locked_vm += (len >> PAGE_SHIFT); 2768e99668a5SLiam R. Howlett } 2769e99668a5SLiam R. Howlett 2770e99668a5SLiam R. Howlett if (file) 2771e99668a5SLiam R. Howlett uprobe_mmap(vma); 2772e99668a5SLiam R. Howlett 2773e99668a5SLiam R. Howlett /* 2774e99668a5SLiam R. Howlett * New (or expanded) vma always get soft dirty status. 2775e99668a5SLiam R. Howlett * Otherwise user-space soft-dirty page tracker won't 2776e99668a5SLiam R. Howlett * be able to distinguish situation when vma area unmapped, 2777e99668a5SLiam R. Howlett * then new mapped in-place (which must be aimed as 2778e99668a5SLiam R. Howlett * a completely new data area). 2779e99668a5SLiam R. Howlett */ 2780e99668a5SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 2781e99668a5SLiam R. Howlett 2782e99668a5SLiam R. Howlett vma_set_page_prot(vma); 2783e99668a5SLiam R. Howlett 2784e99668a5SLiam R. Howlett validate_mm(mm); 2785e99668a5SLiam R. Howlett return addr; 2786e99668a5SLiam R. Howlett 2787e99668a5SLiam R. Howlett unmap_and_free_vma: 2788e99668a5SLiam R. Howlett fput(vma->vm_file); 2789e99668a5SLiam R. Howlett vma->vm_file = NULL; 2790e99668a5SLiam R. Howlett 2791e99668a5SLiam R. Howlett /* Undo any partial mapping done by a device driver. */ 2792*763ecb03SLiam R. Howlett unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end); 2793e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) 2794e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2795e99668a5SLiam R. Howlett free_vma: 2796e99668a5SLiam R. Howlett vm_area_free(vma); 2797e99668a5SLiam R. Howlett unacct_error: 2798e99668a5SLiam R. Howlett if (charged) 2799e99668a5SLiam R. Howlett vm_unacct_memory(charged); 2800e99668a5SLiam R. Howlett validate_mm(mm); 2801e99668a5SLiam R. Howlett return error; 2802e99668a5SLiam R. Howlett } 2803e99668a5SLiam R. Howlett 2804dd2283f2SYang Shi static int __vm_munmap(unsigned long start, size_t len, bool downgrade) 2805a46ef99dSLinus Torvalds { 2806a46ef99dSLinus Torvalds int ret; 2807bfce281cSAl Viro struct mm_struct *mm = current->mm; 2808897ab3e0SMike Rapoport LIST_HEAD(uf); 280911f9a21aSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 2810a46ef99dSLinus Torvalds 2811d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2812ae798783SMichal Hocko return -EINTR; 2813ae798783SMichal Hocko 281411f9a21aSLiam R. Howlett ret = do_mas_munmap(&mas, mm, start, len, &uf, downgrade); 2815dd2283f2SYang Shi /* 2816c1e8d7c6SMichel Lespinasse * Returning 1 indicates mmap_lock is downgraded. 2817dd2283f2SYang Shi * But 1 is not legal return value of vm_munmap() and munmap(), reset 2818dd2283f2SYang Shi * it to 0 before return. 2819dd2283f2SYang Shi */ 2820dd2283f2SYang Shi if (ret == 1) { 2821d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2822dd2283f2SYang Shi ret = 0; 2823dd2283f2SYang Shi } else 2824d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2825dd2283f2SYang Shi 2826897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 2827a46ef99dSLinus Torvalds return ret; 2828a46ef99dSLinus Torvalds } 2829dd2283f2SYang Shi 2830dd2283f2SYang Shi int vm_munmap(unsigned long start, size_t len) 2831dd2283f2SYang Shi { 2832dd2283f2SYang Shi return __vm_munmap(start, len, false); 2833dd2283f2SYang Shi } 2834a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2835a46ef99dSLinus Torvalds 28366a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 28371da177e4SLinus Torvalds { 2838ce18d171SCatalin Marinas addr = untagged_addr(addr); 2839dd2283f2SYang Shi return __vm_munmap(addr, len, true); 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 2842c8d78c18SKirill A. Shutemov 2843c8d78c18SKirill A. Shutemov /* 2844c8d78c18SKirill A. Shutemov * Emulation of deprecated remap_file_pages() syscall. 2845c8d78c18SKirill A. Shutemov */ 2846c8d78c18SKirill A. Shutemov SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, 2847c8d78c18SKirill A. Shutemov unsigned long, prot, unsigned long, pgoff, unsigned long, flags) 2848c8d78c18SKirill A. Shutemov { 2849c8d78c18SKirill A. Shutemov 2850c8d78c18SKirill A. Shutemov struct mm_struct *mm = current->mm; 2851c8d78c18SKirill A. Shutemov struct vm_area_struct *vma; 2852c8d78c18SKirill A. Shutemov unsigned long populate = 0; 2853c8d78c18SKirill A. Shutemov unsigned long ret = -EINVAL; 2854c8d78c18SKirill A. Shutemov struct file *file; 2855c8d78c18SKirill A. Shutemov 2856ee65728eSMike Rapoport pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", 2857c8d78c18SKirill A. Shutemov current->comm, current->pid); 2858c8d78c18SKirill A. Shutemov 2859c8d78c18SKirill A. Shutemov if (prot) 2860c8d78c18SKirill A. Shutemov return ret; 2861c8d78c18SKirill A. Shutemov start = start & PAGE_MASK; 2862c8d78c18SKirill A. Shutemov size = size & PAGE_MASK; 2863c8d78c18SKirill A. Shutemov 2864c8d78c18SKirill A. Shutemov if (start + size <= start) 2865c8d78c18SKirill A. Shutemov return ret; 2866c8d78c18SKirill A. Shutemov 2867c8d78c18SKirill A. Shutemov /* Does pgoff wrap? */ 2868c8d78c18SKirill A. Shutemov if (pgoff + (size >> PAGE_SHIFT) < pgoff) 2869c8d78c18SKirill A. Shutemov return ret; 2870c8d78c18SKirill A. Shutemov 2871d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2872dc0ef0dfSMichal Hocko return -EINTR; 2873dc0ef0dfSMichal Hocko 28749b593cb2SLiam R. Howlett vma = vma_lookup(mm, start); 2875c8d78c18SKirill A. Shutemov 2876c8d78c18SKirill A. Shutemov if (!vma || !(vma->vm_flags & VM_SHARED)) 2877c8d78c18SKirill A. Shutemov goto out; 2878c8d78c18SKirill A. Shutemov 287948f7df32SKirill A. Shutemov if (start + size > vma->vm_end) { 2880*763ecb03SLiam R. Howlett VMA_ITERATOR(vmi, mm, vma->vm_end); 2881*763ecb03SLiam R. Howlett struct vm_area_struct *next, *prev = vma; 288248f7df32SKirill A. Shutemov 2883*763ecb03SLiam R. Howlett for_each_vma_range(vmi, next, start + size) { 288448f7df32SKirill A. Shutemov /* hole between vmas ? */ 2885*763ecb03SLiam R. Howlett if (next->vm_start != prev->vm_end) 288648f7df32SKirill A. Shutemov goto out; 288748f7df32SKirill A. Shutemov 288848f7df32SKirill A. Shutemov if (next->vm_file != vma->vm_file) 288948f7df32SKirill A. Shutemov goto out; 289048f7df32SKirill A. Shutemov 289148f7df32SKirill A. Shutemov if (next->vm_flags != vma->vm_flags) 289248f7df32SKirill A. Shutemov goto out; 289348f7df32SKirill A. Shutemov 2894*763ecb03SLiam R. Howlett prev = next; 289548f7df32SKirill A. Shutemov } 289648f7df32SKirill A. Shutemov 289748f7df32SKirill A. Shutemov if (!next) 2898c8d78c18SKirill A. Shutemov goto out; 2899c8d78c18SKirill A. Shutemov } 2900c8d78c18SKirill A. Shutemov 2901c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; 2902c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; 2903c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; 2904c8d78c18SKirill A. Shutemov 2905c8d78c18SKirill A. Shutemov flags &= MAP_NONBLOCK; 2906c8d78c18SKirill A. Shutemov flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; 2907fce000b1SLiam Howlett if (vma->vm_flags & VM_LOCKED) 2908c8d78c18SKirill A. Shutemov flags |= MAP_LOCKED; 290948f7df32SKirill A. Shutemov 2910c8d78c18SKirill A. Shutemov file = get_file(vma->vm_file); 291145e55300SPeter Collingbourne ret = do_mmap(vma->vm_file, start, size, 2912897ab3e0SMike Rapoport prot, flags, pgoff, &populate, NULL); 2913c8d78c18SKirill A. Shutemov fput(file); 2914c8d78c18SKirill A. Shutemov out: 2915d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2916c8d78c18SKirill A. Shutemov if (populate) 2917c8d78c18SKirill A. Shutemov mm_populate(ret, populate); 2918c8d78c18SKirill A. Shutemov if (!IS_ERR_VALUE(ret)) 2919c8d78c18SKirill A. Shutemov ret = 0; 2920c8d78c18SKirill A. Shutemov return ret; 2921c8d78c18SKirill A. Shutemov } 2922c8d78c18SKirill A. Shutemov 29231da177e4SLinus Torvalds /* 29242e7ce7d3SLiam R. Howlett * brk_munmap() - Unmap a parital vma. 29252e7ce7d3SLiam R. Howlett * @mas: The maple tree state. 29262e7ce7d3SLiam R. Howlett * @vma: The vma to be modified 29272e7ce7d3SLiam R. Howlett * @newbrk: the start of the address to unmap 29282e7ce7d3SLiam R. Howlett * @oldbrk: The end of the address to unmap 29292e7ce7d3SLiam R. Howlett * @uf: The userfaultfd list_head 29302e7ce7d3SLiam R. Howlett * 29312e7ce7d3SLiam R. Howlett * Returns: 1 on success. 29322e7ce7d3SLiam R. Howlett * unmaps a partial VMA mapping. Does not handle alignment, downgrades lock if 29332e7ce7d3SLiam R. Howlett * possible. 29341da177e4SLinus Torvalds */ 29352e7ce7d3SLiam R. Howlett static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, 29362e7ce7d3SLiam R. Howlett unsigned long newbrk, unsigned long oldbrk, 29372e7ce7d3SLiam R. Howlett struct list_head *uf) 29382e7ce7d3SLiam R. Howlett { 29392e7ce7d3SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 29402e7ce7d3SLiam R. Howlett int ret; 29412e7ce7d3SLiam R. Howlett 29422e7ce7d3SLiam R. Howlett arch_unmap(mm, newbrk, oldbrk); 294367e7c167SLiam R. Howlett ret = do_mas_align_munmap(mas, vma, mm, newbrk, oldbrk, uf, true); 29442e7ce7d3SLiam R. Howlett validate_mm_mt(mm); 29452e7ce7d3SLiam R. Howlett return ret; 29462e7ce7d3SLiam R. Howlett } 29472e7ce7d3SLiam R. Howlett 29482e7ce7d3SLiam R. Howlett /* 29492e7ce7d3SLiam R. Howlett * do_brk_flags() - Increase the brk vma if the flags match. 29502e7ce7d3SLiam R. Howlett * @mas: The maple tree state. 29512e7ce7d3SLiam R. Howlett * @addr: The start address 29522e7ce7d3SLiam R. Howlett * @len: The length of the increase 29532e7ce7d3SLiam R. Howlett * @vma: The vma, 29542e7ce7d3SLiam R. Howlett * @flags: The VMA Flags 29552e7ce7d3SLiam R. Howlett * 29562e7ce7d3SLiam R. Howlett * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags 29572e7ce7d3SLiam R. Howlett * do not match then create a new anonymous VMA. Eventually we may be able to 29582e7ce7d3SLiam R. Howlett * do some brk-specific accounting here. 29592e7ce7d3SLiam R. Howlett */ 29602e7ce7d3SLiam R. Howlett static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, 2961*763ecb03SLiam R. Howlett unsigned long addr, unsigned long len, unsigned long flags) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 29642e7ce7d3SLiam R. Howlett 2965d4af56c5SLiam R. Howlett validate_mm_mt(mm); 29662e7ce7d3SLiam R. Howlett /* 29672e7ce7d3SLiam R. Howlett * Check against address space limits by the changed size 29682e7ce7d3SLiam R. Howlett * Note: This happens *after* clearing old mappings in some code paths. 29692e7ce7d3SLiam R. Howlett */ 297016e72e9bSDenys Vlasenko flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 297184638335SKonstantin Khlebnikov if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) 29721da177e4SLinus Torvalds return -ENOMEM; 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 29751da177e4SLinus Torvalds return -ENOMEM; 29761da177e4SLinus Torvalds 2977191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 29781da177e4SLinus Torvalds return -ENOMEM; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds /* 29812e7ce7d3SLiam R. Howlett * Expand the existing vma if possible; Note that singular lists do not 29822e7ce7d3SLiam R. Howlett * occur after forking, so the expand will only happen on new VMAs. 29831da177e4SLinus Torvalds */ 29842e7ce7d3SLiam R. Howlett if (vma && 29852e7ce7d3SLiam R. Howlett (!vma->anon_vma || list_is_singular(&vma->anon_vma_chain)) && 29862e7ce7d3SLiam R. Howlett ((vma->vm_flags & ~VM_SOFTDIRTY) == flags)) { 29872e7ce7d3SLiam R. Howlett mas->index = vma->vm_start; 29882e7ce7d3SLiam R. Howlett mas->last = addr + len - 1; 29892e7ce7d3SLiam R. Howlett vma_adjust_trans_huge(vma, addr, addr + len, 0); 29902e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 29912e7ce7d3SLiam R. Howlett anon_vma_lock_write(vma->anon_vma); 29922e7ce7d3SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 29931da177e4SLinus Torvalds } 29942e7ce7d3SLiam R. Howlett vma->vm_end = addr + len; 29952e7ce7d3SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 29962e7ce7d3SLiam R. Howlett if (mas_store_gfp(mas, vma, GFP_KERNEL)) 29972e7ce7d3SLiam R. Howlett goto mas_expand_failed; 29982e7ce7d3SLiam R. Howlett 29992e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 30002e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 30012e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 30022e7ce7d3SLiam R. Howlett } 30032e7ce7d3SLiam R. Howlett khugepaged_enter_vma(vma, flags); 30042e7ce7d3SLiam R. Howlett goto out; 30052e7ce7d3SLiam R. Howlett } 30062e7ce7d3SLiam R. Howlett 30072e7ce7d3SLiam R. Howlett /* create a vma struct for an anonymous mapping */ 30082e7ce7d3SLiam R. Howlett vma = vm_area_alloc(mm); 30092e7ce7d3SLiam R. Howlett if (!vma) 30102e7ce7d3SLiam R. Howlett goto vma_alloc_fail; 30111da177e4SLinus Torvalds 3012bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 30131da177e4SLinus Torvalds vma->vm_start = addr; 30141da177e4SLinus Torvalds vma->vm_end = addr + len; 30152e7ce7d3SLiam R. Howlett vma->vm_pgoff = addr >> PAGE_SHIFT; 30161da177e4SLinus Torvalds vma->vm_flags = flags; 30173ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 30182e7ce7d3SLiam R. Howlett mas_set_range(mas, vma->vm_start, addr + len - 1); 30192e7ce7d3SLiam R. Howlett if (mas_store_gfp(mas, vma, GFP_KERNEL)) 30202e7ce7d3SLiam R. Howlett goto mas_store_fail; 3021d4af56c5SLiam R. Howlett 30222e7ce7d3SLiam R. Howlett mm->map_count++; 30231da177e4SLinus Torvalds out: 30243af9e859SEric B Munson perf_event_mmap(vma); 30251da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 302684638335SKonstantin Khlebnikov mm->data_vm += len >> PAGE_SHIFT; 3027128557ffSMichel Lespinasse if (flags & VM_LOCKED) 3028ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 3029d9104d1cSCyrill Gorcunov vma->vm_flags |= VM_SOFTDIRTY; 3030*763ecb03SLiam R. Howlett validate_mm(mm); 30315d22fc25SLinus Torvalds return 0; 3032d4af56c5SLiam R. Howlett 30332e7ce7d3SLiam R. Howlett mas_store_fail: 3034d4af56c5SLiam R. Howlett vm_area_free(vma); 30352e7ce7d3SLiam R. Howlett vma_alloc_fail: 30362e7ce7d3SLiam R. Howlett vm_unacct_memory(len >> PAGE_SHIFT); 30372e7ce7d3SLiam R. Howlett return -ENOMEM; 30382e7ce7d3SLiam R. Howlett 30392e7ce7d3SLiam R. Howlett mas_expand_failed: 30402e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 30412e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 30422e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 30432e7ce7d3SLiam R. Howlett } 3044d4af56c5SLiam R. Howlett return -ENOMEM; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 3047bb177a73SMichal Hocko int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) 3048e4eb1ff6SLinus Torvalds { 3049e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 30502e7ce7d3SLiam R. Howlett struct vm_area_struct *vma = NULL; 3051bb177a73SMichal Hocko unsigned long len; 30525d22fc25SLinus Torvalds int ret; 3053128557ffSMichel Lespinasse bool populate; 3054897ab3e0SMike Rapoport LIST_HEAD(uf); 30552e7ce7d3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 3056e4eb1ff6SLinus Torvalds 3057bb177a73SMichal Hocko len = PAGE_ALIGN(request); 3058bb177a73SMichal Hocko if (len < request) 3059bb177a73SMichal Hocko return -ENOMEM; 3060bb177a73SMichal Hocko if (!len) 3061bb177a73SMichal Hocko return 0; 3062bb177a73SMichal Hocko 3063d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 30642d6c9282SMichal Hocko return -EINTR; 30652d6c9282SMichal Hocko 30662e7ce7d3SLiam R. Howlett /* Until we need other flags, refuse anything except VM_EXEC. */ 30672e7ce7d3SLiam R. Howlett if ((flags & (~VM_EXEC)) != 0) 30682e7ce7d3SLiam R. Howlett return -EINVAL; 30692e7ce7d3SLiam R. Howlett 30702e7ce7d3SLiam R. Howlett ret = check_brk_limits(addr, len); 30712e7ce7d3SLiam R. Howlett if (ret) 30722e7ce7d3SLiam R. Howlett goto limits_failed; 30732e7ce7d3SLiam R. Howlett 307411f9a21aSLiam R. Howlett ret = do_mas_munmap(&mas, mm, addr, len, &uf, 0); 30752e7ce7d3SLiam R. Howlett if (ret) 30762e7ce7d3SLiam R. Howlett goto munmap_failed; 30772e7ce7d3SLiam R. Howlett 30782e7ce7d3SLiam R. Howlett vma = mas_prev(&mas, 0); 30792e7ce7d3SLiam R. Howlett if (!vma || vma->vm_end != addr || vma_policy(vma) || 30802e7ce7d3SLiam R. Howlett !can_vma_merge_after(vma, flags, NULL, NULL, 30812e7ce7d3SLiam R. Howlett addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) 30822e7ce7d3SLiam R. Howlett vma = NULL; 30832e7ce7d3SLiam R. Howlett 30842e7ce7d3SLiam R. Howlett ret = do_brk_flags(&mas, vma, addr, len, flags); 3085128557ffSMichel Lespinasse populate = ((mm->def_flags & VM_LOCKED) != 0); 3086d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3087897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 30885d22fc25SLinus Torvalds if (populate && !ret) 3089128557ffSMichel Lespinasse mm_populate(addr, len); 3090e4eb1ff6SLinus Torvalds return ret; 30912e7ce7d3SLiam R. Howlett 30922e7ce7d3SLiam R. Howlett munmap_failed: 30932e7ce7d3SLiam R. Howlett limits_failed: 30942e7ce7d3SLiam R. Howlett mmap_write_unlock(mm); 30952e7ce7d3SLiam R. Howlett return ret; 3096e4eb1ff6SLinus Torvalds } 309716e72e9bSDenys Vlasenko EXPORT_SYMBOL(vm_brk_flags); 309816e72e9bSDenys Vlasenko 309916e72e9bSDenys Vlasenko int vm_brk(unsigned long addr, unsigned long len) 310016e72e9bSDenys Vlasenko { 310116e72e9bSDenys Vlasenko return vm_brk_flags(addr, len, 0); 310216e72e9bSDenys Vlasenko } 3103e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds /* Release all mmaps. */ 31061da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 31071da177e4SLinus Torvalds { 3108d16dfc55SPeter Zijlstra struct mmu_gather tlb; 3109ba470de4SRik van Riel struct vm_area_struct *vma; 31101da177e4SLinus Torvalds unsigned long nr_accounted = 0; 3111*763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 3112*763ecb03SLiam R. Howlett int count = 0; 31131da177e4SLinus Torvalds 3114d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 3115cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 3116d6dd61c8SJeremy Fitzhardinge 311727ae357fSDavid Rientjes if (unlikely(mm_is_oom_victim(mm))) { 311827ae357fSDavid Rientjes /* 311927ae357fSDavid Rientjes * Manually reap the mm to free as much memory as possible. 312027ae357fSDavid Rientjes * Then, as the oom reaper does, set MMF_OOM_SKIP to disregard 3121c1e8d7c6SMichel Lespinasse * this mm from further consideration. Taking mm->mmap_lock for 312227ae357fSDavid Rientjes * write after setting MMF_OOM_SKIP will guarantee that the oom 3123c1e8d7c6SMichel Lespinasse * reaper will not run on this mm again after mmap_lock is 312427ae357fSDavid Rientjes * dropped. 312527ae357fSDavid Rientjes * 3126c1e8d7c6SMichel Lespinasse * Nothing can be holding mm->mmap_lock here and the above call 312727ae357fSDavid Rientjes * to mmu_notifier_release(mm) ensures mmu notifier callbacks in 312827ae357fSDavid Rientjes * __oom_reap_task_mm() will not block. 312927ae357fSDavid Rientjes */ 313093065ac7SMichal Hocko (void)__oom_reap_task_mm(mm); 313127ae357fSDavid Rientjes set_bit(MMF_OOM_SKIP, &mm->flags); 313227ae357fSDavid Rientjes } 313327ae357fSDavid Rientjes 313464591e86SSuren Baghdasaryan mmap_write_lock(mm); 31359480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 31369480c53eSJeremy Fitzhardinge 3137*763ecb03SLiam R. Howlett vma = mas_find(&mas, ULONG_MAX); 313864591e86SSuren Baghdasaryan if (!vma) { 313964591e86SSuren Baghdasaryan /* Can happen if dup_mmap() received an OOM */ 314064591e86SSuren Baghdasaryan mmap_write_unlock(mm); 31419480c53eSJeremy Fitzhardinge return; 314264591e86SSuren Baghdasaryan } 31439480c53eSJeremy Fitzhardinge 31441da177e4SLinus Torvalds lru_add_drain(); 31451da177e4SLinus Torvalds flush_cache_mm(mm); 3146d8b45053SWill Deacon tlb_gather_mmu_fullmm(&tlb, mm); 3147901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 3148*763ecb03SLiam R. Howlett /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ 3149*763ecb03SLiam R. Howlett unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX); 3150*763ecb03SLiam R. Howlett free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, 3151*763ecb03SLiam R. Howlett USER_PGTABLES_CEILING); 3152ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 31531da177e4SLinus Torvalds 3154*763ecb03SLiam R. Howlett /* 3155*763ecb03SLiam R. Howlett * Walk the list again, actually closing and freeing it, with preemption 3156*763ecb03SLiam R. Howlett * enabled, without holding any MM locks besides the unreachable 3157*763ecb03SLiam R. Howlett * mmap_write_lock. 3158*763ecb03SLiam R. Howlett */ 3159*763ecb03SLiam R. Howlett do { 31604f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 31614f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 3162*763ecb03SLiam R. Howlett remove_vma(vma); 3163*763ecb03SLiam R. Howlett count++; 31640a3b3c25SPaul E. McKenney cond_resched(); 3165*763ecb03SLiam R. Howlett } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); 3166*763ecb03SLiam R. Howlett 3167*763ecb03SLiam R. Howlett BUG_ON(count != mm->map_count); 3168d4af56c5SLiam R. Howlett 3169d4af56c5SLiam R. Howlett trace_exit_mmap(mm); 3170d4af56c5SLiam R. Howlett __mt_destroy(&mm->mm_mt); 317164591e86SSuren Baghdasaryan mmap_write_unlock(mm); 31724f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 31731da177e4SLinus Torvalds } 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 31761da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 3177c8c06efaSDavidlohr Bueso * then i_mmap_rwsem is taken here. 31781da177e4SLinus Torvalds */ 31791da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 31801da177e4SLinus Torvalds { 31816597d783SHugh Dickins struct vm_area_struct *prev; 3182d4af56c5SLiam R. Howlett unsigned long charged = vma_pages(vma); 31831da177e4SLinus Torvalds 3184d4af56c5SLiam R. Howlett 3185524e00b3SLiam R. Howlett if (range_has_overlap(mm, vma->vm_start, vma->vm_end, &prev)) 3186c9d13f5fSChen Gang return -ENOMEM; 3187d4af56c5SLiam R. Howlett 3188c9d13f5fSChen Gang if ((vma->vm_flags & VM_ACCOUNT) && 3189d4af56c5SLiam R. Howlett security_vm_enough_memory_mm(mm, charged)) 3190c9d13f5fSChen Gang return -ENOMEM; 3191c9d13f5fSChen Gang 31921da177e4SLinus Torvalds /* 31931da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 31941da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 31951da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 31961da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 31971da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 31981da177e4SLinus Torvalds * 31991da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 32001da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 32011da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 32028332326eSLiao Pingfang * Similarly in do_mmap and in do_brk_flags. 32031da177e4SLinus Torvalds */ 32048a9cc3b5SOleg Nesterov if (vma_is_anonymous(vma)) { 32051da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 32061da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 32071da177e4SLinus Torvalds } 32082b144498SSrikar Dronamraju 3209*763ecb03SLiam R. Howlett if (vma_link(mm, vma)) { 3210d4af56c5SLiam R. Howlett vm_unacct_memory(charged); 3211d4af56c5SLiam R. Howlett return -ENOMEM; 3212d4af56c5SLiam R. Howlett } 3213d4af56c5SLiam R. Howlett 32141da177e4SLinus Torvalds return 0; 32151da177e4SLinus Torvalds } 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds /* 32181da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 32191da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 32201da177e4SLinus Torvalds */ 32211da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 322238a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 322338a76013SMichel Lespinasse bool *need_rmap_locks) 32241da177e4SLinus Torvalds { 32251da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 32261da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 32271da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 32281da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 3229948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 32301da177e4SLinus Torvalds 3231d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32321da177e4SLinus Torvalds /* 32331da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 32341da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 32351da177e4SLinus Torvalds */ 3236ce75799bSOleg Nesterov if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 32371da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 3238948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 3239948f017bSAndrea Arcangeli } 32401da177e4SLinus Torvalds 3241*763ecb03SLiam R. Howlett new_vma = find_vma_prev(mm, addr, &prev); 3242*763ecb03SLiam R. Howlett if (new_vma && new_vma->vm_start < addr + len) 32436597d783SHugh Dickins return NULL; /* should never get here */ 3244524e00b3SLiam R. Howlett 32451da177e4SLinus Torvalds new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, 324619a809afSAndrea Arcangeli vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), 32475c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 32481da177e4SLinus Torvalds if (new_vma) { 32491da177e4SLinus Torvalds /* 32501da177e4SLinus Torvalds * Source vma may have been merged into new_vma 32511da177e4SLinus Torvalds */ 3252948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 3253948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 3254948f017bSAndrea Arcangeli /* 3255948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 3256948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 3257948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 3258948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 3259948f017bSAndrea Arcangeli * destination address of the mremap to allow 3260948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 3261948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 3262948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 3263948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 3264948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 3265948f017bSAndrea Arcangeli */ 326681d1b09cSSasha Levin VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 326738a76013SMichel Lespinasse *vmap = vma = new_vma; 3268108d6642SMichel Lespinasse } 326938a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 32701da177e4SLinus Torvalds } else { 32713928d4f5SLinus Torvalds new_vma = vm_area_dup(vma); 3272e3975891SChen Gang if (!new_vma) 3273e3975891SChen Gang goto out; 32741da177e4SLinus Torvalds new_vma->vm_start = addr; 32751da177e4SLinus Torvalds new_vma->vm_end = addr + len; 32761da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 3277ef0855d3SOleg Nesterov if (vma_dup_policy(vma, new_vma)) 3278523d4e20SMichel Lespinasse goto out_free_vma; 3279523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 3280523d4e20SMichel Lespinasse goto out_free_mempol; 3281e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 32821da177e4SLinus Torvalds get_file(new_vma->vm_file); 32831da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 32841da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 3285*763ecb03SLiam R. Howlett if (vma_link(mm, new_vma)) 3286524e00b3SLiam R. Howlett goto out_vma_link; 328738a76013SMichel Lespinasse *need_rmap_locks = false; 32881da177e4SLinus Torvalds } 3289d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32901da177e4SLinus Torvalds return new_vma; 32915beb4930SRik van Riel 3292524e00b3SLiam R. Howlett out_vma_link: 3293524e00b3SLiam R. Howlett if (new_vma->vm_ops && new_vma->vm_ops->close) 3294524e00b3SLiam R. Howlett new_vma->vm_ops->close(new_vma); 32955beb4930SRik van Riel out_free_mempol: 3296ef0855d3SOleg Nesterov mpol_put(vma_policy(new_vma)); 32975beb4930SRik van Riel out_free_vma: 32983928d4f5SLinus Torvalds vm_area_free(new_vma); 3299e3975891SChen Gang out: 3300d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33015beb4930SRik van Riel return NULL; 33021da177e4SLinus Torvalds } 3303119f657cSakpm@osdl.org 3304119f657cSakpm@osdl.org /* 3305119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 3306119f657cSakpm@osdl.org * number of pages 3307119f657cSakpm@osdl.org */ 330884638335SKonstantin Khlebnikov bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) 3309119f657cSakpm@osdl.org { 331084638335SKonstantin Khlebnikov if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) 331184638335SKonstantin Khlebnikov return false; 3312119f657cSakpm@osdl.org 3313d977d56cSKonstantin Khlebnikov if (is_data_mapping(flags) && 3314d977d56cSKonstantin Khlebnikov mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { 3315f4fcd558SKonstantin Khlebnikov /* Workaround for Valgrind */ 3316f4fcd558SKonstantin Khlebnikov if (rlimit(RLIMIT_DATA) == 0 && 3317f4fcd558SKonstantin Khlebnikov mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) 3318f4fcd558SKonstantin Khlebnikov return true; 331957a7702bSDavid Woodhouse 332057a7702bSDavid Woodhouse pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", 3321d977d56cSKonstantin Khlebnikov current->comm, current->pid, 3322d977d56cSKonstantin Khlebnikov (mm->data_vm + npages) << PAGE_SHIFT, 332357a7702bSDavid Woodhouse rlimit(RLIMIT_DATA), 332457a7702bSDavid Woodhouse ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); 332557a7702bSDavid Woodhouse 332657a7702bSDavid Woodhouse if (!ignore_rlimit_data) 3327d977d56cSKonstantin Khlebnikov return false; 3328d977d56cSKonstantin Khlebnikov } 3329119f657cSakpm@osdl.org 333084638335SKonstantin Khlebnikov return true; 333184638335SKonstantin Khlebnikov } 333284638335SKonstantin Khlebnikov 333384638335SKonstantin Khlebnikov void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) 333484638335SKonstantin Khlebnikov { 33357866076bSPeng Liu WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); 333684638335SKonstantin Khlebnikov 3337d977d56cSKonstantin Khlebnikov if (is_exec_mapping(flags)) 333884638335SKonstantin Khlebnikov mm->exec_vm += npages; 3339d977d56cSKonstantin Khlebnikov else if (is_stack_mapping(flags)) 334084638335SKonstantin Khlebnikov mm->stack_vm += npages; 3341d977d56cSKonstantin Khlebnikov else if (is_data_mapping(flags)) 334284638335SKonstantin Khlebnikov mm->data_vm += npages; 3343119f657cSakpm@osdl.org } 3344fa5dc22fSRoland McGrath 3345b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf); 3346a62c34bdSAndy Lutomirski 3347a62c34bdSAndy Lutomirski /* 3348a62c34bdSAndy Lutomirski * Having a close hook prevents vma merging regardless of flags. 3349a62c34bdSAndy Lutomirski */ 3350a62c34bdSAndy Lutomirski static void special_mapping_close(struct vm_area_struct *vma) 3351a62c34bdSAndy Lutomirski { 3352a62c34bdSAndy Lutomirski } 3353a62c34bdSAndy Lutomirski 3354a62c34bdSAndy Lutomirski static const char *special_mapping_name(struct vm_area_struct *vma) 3355a62c34bdSAndy Lutomirski { 3356a62c34bdSAndy Lutomirski return ((struct vm_special_mapping *)vma->vm_private_data)->name; 3357a62c34bdSAndy Lutomirski } 3358a62c34bdSAndy Lutomirski 335914d07113SBrian Geffon static int special_mapping_mremap(struct vm_area_struct *new_vma) 3360b059a453SDmitry Safonov { 3361b059a453SDmitry Safonov struct vm_special_mapping *sm = new_vma->vm_private_data; 3362b059a453SDmitry Safonov 3363280e87e9SDmitry Safonov if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) 3364280e87e9SDmitry Safonov return -EFAULT; 3365280e87e9SDmitry Safonov 3366b059a453SDmitry Safonov if (sm->mremap) 3367b059a453SDmitry Safonov return sm->mremap(sm, new_vma); 3368280e87e9SDmitry Safonov 3369b059a453SDmitry Safonov return 0; 3370b059a453SDmitry Safonov } 3371b059a453SDmitry Safonov 3372871402e0SDmitry Safonov static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) 3373871402e0SDmitry Safonov { 3374871402e0SDmitry Safonov /* 3375871402e0SDmitry Safonov * Forbid splitting special mappings - kernel has expectations over 3376871402e0SDmitry Safonov * the number of pages in mapping. Together with VM_DONTEXPAND 3377871402e0SDmitry Safonov * the size of vma should stay the same over the special mapping's 3378871402e0SDmitry Safonov * lifetime. 3379871402e0SDmitry Safonov */ 3380871402e0SDmitry Safonov return -EINVAL; 3381871402e0SDmitry Safonov } 3382871402e0SDmitry Safonov 3383a62c34bdSAndy Lutomirski static const struct vm_operations_struct special_mapping_vmops = { 3384a62c34bdSAndy Lutomirski .close = special_mapping_close, 3385a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3386b059a453SDmitry Safonov .mremap = special_mapping_mremap, 3387a62c34bdSAndy Lutomirski .name = special_mapping_name, 3388af34ebebSDmitry Safonov /* vDSO code relies that VVAR can't be accessed remotely */ 3389af34ebebSDmitry Safonov .access = NULL, 3390871402e0SDmitry Safonov .may_split = special_mapping_split, 3391a62c34bdSAndy Lutomirski }; 3392a62c34bdSAndy Lutomirski 3393a62c34bdSAndy Lutomirski static const struct vm_operations_struct legacy_special_mapping_vmops = { 3394a62c34bdSAndy Lutomirski .close = special_mapping_close, 3395a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3396a62c34bdSAndy Lutomirski }; 3397fa5dc22fSRoland McGrath 3398b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf) 3399fa5dc22fSRoland McGrath { 340011bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 3401b1d0e4f5SNick Piggin pgoff_t pgoff; 3402fa5dc22fSRoland McGrath struct page **pages; 3403fa5dc22fSRoland McGrath 3404f872f540SAndy Lutomirski if (vma->vm_ops == &legacy_special_mapping_vmops) { 3405a62c34bdSAndy Lutomirski pages = vma->vm_private_data; 3406f872f540SAndy Lutomirski } else { 3407f872f540SAndy Lutomirski struct vm_special_mapping *sm = vma->vm_private_data; 3408f872f540SAndy Lutomirski 3409f872f540SAndy Lutomirski if (sm->fault) 341011bac800SDave Jiang return sm->fault(sm, vmf->vma, vmf); 3411f872f540SAndy Lutomirski 3412f872f540SAndy Lutomirski pages = sm->pages; 3413f872f540SAndy Lutomirski } 3414a62c34bdSAndy Lutomirski 34158a9cc3b5SOleg Nesterov for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) 3416b1d0e4f5SNick Piggin pgoff--; 3417fa5dc22fSRoland McGrath 3418fa5dc22fSRoland McGrath if (*pages) { 3419fa5dc22fSRoland McGrath struct page *page = *pages; 3420fa5dc22fSRoland McGrath get_page(page); 3421b1d0e4f5SNick Piggin vmf->page = page; 3422b1d0e4f5SNick Piggin return 0; 3423fa5dc22fSRoland McGrath } 3424fa5dc22fSRoland McGrath 3425b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 3426fa5dc22fSRoland McGrath } 3427fa5dc22fSRoland McGrath 3428a62c34bdSAndy Lutomirski static struct vm_area_struct *__install_special_mapping( 3429a62c34bdSAndy Lutomirski struct mm_struct *mm, 3430fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 343127f28b97SChen Gang unsigned long vm_flags, void *priv, 343227f28b97SChen Gang const struct vm_operations_struct *ops) 3433fa5dc22fSRoland McGrath { 3434462e635eSTavis Ormandy int ret; 3435fa5dc22fSRoland McGrath struct vm_area_struct *vma; 3436fa5dc22fSRoland McGrath 3437d4af56c5SLiam R. Howlett validate_mm_mt(mm); 3438490fc053SLinus Torvalds vma = vm_area_alloc(mm); 3439fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 34403935ed6aSStefani Seibold return ERR_PTR(-ENOMEM); 3441fa5dc22fSRoland McGrath 3442fa5dc22fSRoland McGrath vma->vm_start = addr; 3443fa5dc22fSRoland McGrath vma->vm_end = addr + len; 3444fa5dc22fSRoland McGrath 3445d9104d1cSCyrill Gorcunov vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY; 34461fc09228SHugh Dickins vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 34473ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 3448fa5dc22fSRoland McGrath 3449a62c34bdSAndy Lutomirski vma->vm_ops = ops; 3450a62c34bdSAndy Lutomirski vma->vm_private_data = priv; 3451fa5dc22fSRoland McGrath 3452462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 3453462e635eSTavis Ormandy if (ret) 3454462e635eSTavis Ormandy goto out; 3455fa5dc22fSRoland McGrath 345684638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); 3457fa5dc22fSRoland McGrath 3458cdd6c482SIngo Molnar perf_event_mmap(vma); 3459089dd79dSPeter Zijlstra 3460d4af56c5SLiam R. Howlett validate_mm_mt(mm); 34613935ed6aSStefani Seibold return vma; 3462462e635eSTavis Ormandy 3463462e635eSTavis Ormandy out: 34643928d4f5SLinus Torvalds vm_area_free(vma); 3465d4af56c5SLiam R. Howlett validate_mm_mt(mm); 34663935ed6aSStefani Seibold return ERR_PTR(ret); 34673935ed6aSStefani Seibold } 34683935ed6aSStefani Seibold 34692eefd878SDmitry Safonov bool vma_is_special_mapping(const struct vm_area_struct *vma, 34702eefd878SDmitry Safonov const struct vm_special_mapping *sm) 34712eefd878SDmitry Safonov { 34722eefd878SDmitry Safonov return vma->vm_private_data == sm && 34732eefd878SDmitry Safonov (vma->vm_ops == &special_mapping_vmops || 34742eefd878SDmitry Safonov vma->vm_ops == &legacy_special_mapping_vmops); 34752eefd878SDmitry Safonov } 34762eefd878SDmitry Safonov 3477a62c34bdSAndy Lutomirski /* 3478c1e8d7c6SMichel Lespinasse * Called with mm->mmap_lock held for writing. 3479a62c34bdSAndy Lutomirski * Insert a new vma covering the given region, with the given flags. 3480a62c34bdSAndy Lutomirski * Its pages are supplied by the given array of struct page *. 3481a62c34bdSAndy Lutomirski * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 3482a62c34bdSAndy Lutomirski * The region past the last page supplied will always produce SIGBUS. 3483a62c34bdSAndy Lutomirski * The array pointer and the pages it points to are assumed to stay alive 3484a62c34bdSAndy Lutomirski * for as long as this mapping might exist. 3485a62c34bdSAndy Lutomirski */ 3486a62c34bdSAndy Lutomirski struct vm_area_struct *_install_special_mapping( 3487a62c34bdSAndy Lutomirski struct mm_struct *mm, 3488a62c34bdSAndy Lutomirski unsigned long addr, unsigned long len, 3489a62c34bdSAndy Lutomirski unsigned long vm_flags, const struct vm_special_mapping *spec) 3490a62c34bdSAndy Lutomirski { 349127f28b97SChen Gang return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, 349227f28b97SChen Gang &special_mapping_vmops); 3493a62c34bdSAndy Lutomirski } 3494a62c34bdSAndy Lutomirski 34953935ed6aSStefani Seibold int install_special_mapping(struct mm_struct *mm, 34963935ed6aSStefani Seibold unsigned long addr, unsigned long len, 34973935ed6aSStefani Seibold unsigned long vm_flags, struct page **pages) 34983935ed6aSStefani Seibold { 3499a62c34bdSAndy Lutomirski struct vm_area_struct *vma = __install_special_mapping( 350027f28b97SChen Gang mm, addr, len, vm_flags, (void *)pages, 350127f28b97SChen Gang &legacy_special_mapping_vmops); 35023935ed6aSStefani Seibold 350314bd5b45SDuan Jiong return PTR_ERR_OR_ZERO(vma); 3504fa5dc22fSRoland McGrath } 35057906d00cSAndrea Arcangeli 35067906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 35077906d00cSAndrea Arcangeli 3508454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 35097906d00cSAndrea Arcangeli { 3510f808c13fSDavidlohr Bueso if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 35117906d00cSAndrea Arcangeli /* 35127906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 35137906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 35147906d00cSAndrea Arcangeli */ 3515da1c55f1SMichel Lespinasse down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 35167906d00cSAndrea Arcangeli /* 35177906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 35185a505085SIngo Molnar * anon_vma->root->rwsem. If some other vma in this mm shares 35197906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 35207906d00cSAndrea Arcangeli * 35217906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 35227906d00cSAndrea Arcangeli * can't change from under us thanks to the 35235a505085SIngo Molnar * anon_vma->root->rwsem. 35247906d00cSAndrea Arcangeli */ 35257906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 3526f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 35277906d00cSAndrea Arcangeli BUG(); 35287906d00cSAndrea Arcangeli } 35297906d00cSAndrea Arcangeli } 35307906d00cSAndrea Arcangeli 3531454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 35327906d00cSAndrea Arcangeli { 35337906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 35347906d00cSAndrea Arcangeli /* 35357906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 35367906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 35377906d00cSAndrea Arcangeli * 35387906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 35397906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 35407906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 35417906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 35427906d00cSAndrea Arcangeli */ 35437906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 35447906d00cSAndrea Arcangeli BUG(); 3545da1c55f1SMichel Lespinasse down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 35467906d00cSAndrea Arcangeli } 35477906d00cSAndrea Arcangeli } 35487906d00cSAndrea Arcangeli 35497906d00cSAndrea Arcangeli /* 35507906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 35517906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 35527906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 35537906d00cSAndrea Arcangeli * 3554c1e8d7c6SMichel Lespinasse * The caller must take the mmap_lock in write mode before calling 35557906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 3556c1e8d7c6SMichel Lespinasse * mmap_lock until mm_drop_all_locks() returns. 35577906d00cSAndrea Arcangeli * 3558c1e8d7c6SMichel Lespinasse * mmap_lock in write mode is required in order to block all operations 35597906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 356027ba0644SKirill A. Shutemov * altering the vma layout. It's also needed in write mode to avoid new 35617906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 35627906d00cSAndrea Arcangeli * 35637906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 35647906d00cSAndrea Arcangeli * or it would deadlock. 35657906d00cSAndrea Arcangeli * 3566bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 35677906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 35687906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 35697906d00cSAndrea Arcangeli * 357088f306b6SKirill A. Shutemov * We take locks in following order, accordingly to comment at beginning 357188f306b6SKirill A. Shutemov * of mm/rmap.c: 357288f306b6SKirill A. Shutemov * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 357388f306b6SKirill A. Shutemov * hugetlb mapping); 357488f306b6SKirill A. Shutemov * - all i_mmap_rwsem locks; 357588f306b6SKirill A. Shutemov * - all anon_vma->rwseml 357688f306b6SKirill A. Shutemov * 357788f306b6SKirill A. Shutemov * We can take all locks within these types randomly because the VM code 357888f306b6SKirill A. Shutemov * doesn't nest them and we protected from parallel mm_take_all_locks() by 357988f306b6SKirill A. Shutemov * mm_all_locks_mutex. 35807906d00cSAndrea Arcangeli * 35817906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 35827906d00cSAndrea Arcangeli * that may have to take thousand of locks. 35837906d00cSAndrea Arcangeli * 35847906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 35857906d00cSAndrea Arcangeli */ 35867906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 35877906d00cSAndrea Arcangeli { 35887906d00cSAndrea Arcangeli struct vm_area_struct *vma; 35895beb4930SRik van Riel struct anon_vma_chain *avc; 3590*763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 35917906d00cSAndrea Arcangeli 3592325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 35937906d00cSAndrea Arcangeli 35947906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 35957906d00cSAndrea Arcangeli 3596*763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35977906d00cSAndrea Arcangeli if (signal_pending(current)) 35987906d00cSAndrea Arcangeli goto out_unlock; 359988f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 360088f306b6SKirill A. Shutemov is_vm_hugetlb_page(vma)) 360188f306b6SKirill A. Shutemov vm_lock_mapping(mm, vma->vm_file->f_mapping); 360288f306b6SKirill A. Shutemov } 360388f306b6SKirill A. Shutemov 3604*763ecb03SLiam R. Howlett mas_set(&mas, 0); 3605*763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 360688f306b6SKirill A. Shutemov if (signal_pending(current)) 360788f306b6SKirill A. Shutemov goto out_unlock; 360888f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 360988f306b6SKirill A. Shutemov !is_vm_hugetlb_page(vma)) 3610454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 36117906d00cSAndrea Arcangeli } 36127cd5a02fSPeter Zijlstra 3613*763ecb03SLiam R. Howlett mas_set(&mas, 0); 3614*763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 36157cd5a02fSPeter Zijlstra if (signal_pending(current)) 36167cd5a02fSPeter Zijlstra goto out_unlock; 36177cd5a02fSPeter Zijlstra if (vma->anon_vma) 36185beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 36195beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 36207cd5a02fSPeter Zijlstra } 36217cd5a02fSPeter Zijlstra 3622584cff54SKautuk Consul return 0; 36237906d00cSAndrea Arcangeli 36247906d00cSAndrea Arcangeli out_unlock: 36257906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 3626584cff54SKautuk Consul return -EINTR; 36277906d00cSAndrea Arcangeli } 36287906d00cSAndrea Arcangeli 36297906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 36307906d00cSAndrea Arcangeli { 3631f808c13fSDavidlohr Bueso if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 36327906d00cSAndrea Arcangeli /* 36337906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 36347906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 36357906d00cSAndrea Arcangeli * 36367906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 3637bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 36387906d00cSAndrea Arcangeli * never see our bitflag. 36397906d00cSAndrea Arcangeli * 36407906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 36417906d00cSAndrea Arcangeli * can't change from under us until we release the 36425a505085SIngo Molnar * anon_vma->root->rwsem. 36437906d00cSAndrea Arcangeli */ 36447906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 3645f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 36467906d00cSAndrea Arcangeli BUG(); 364708b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 36487906d00cSAndrea Arcangeli } 36497906d00cSAndrea Arcangeli } 36507906d00cSAndrea Arcangeli 36517906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 36527906d00cSAndrea Arcangeli { 36537906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 36547906d00cSAndrea Arcangeli /* 36557906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 36567906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 36577906d00cSAndrea Arcangeli */ 365883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 36597906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 36607906d00cSAndrea Arcangeli &mapping->flags)) 36617906d00cSAndrea Arcangeli BUG(); 36627906d00cSAndrea Arcangeli } 36637906d00cSAndrea Arcangeli } 36647906d00cSAndrea Arcangeli 36657906d00cSAndrea Arcangeli /* 3666c1e8d7c6SMichel Lespinasse * The mmap_lock cannot be released by the caller until 36677906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 36687906d00cSAndrea Arcangeli */ 36697906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 36707906d00cSAndrea Arcangeli { 36717906d00cSAndrea Arcangeli struct vm_area_struct *vma; 36725beb4930SRik van Riel struct anon_vma_chain *avc; 3673*763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 36747906d00cSAndrea Arcangeli 3675325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 36767906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 36777906d00cSAndrea Arcangeli 3678*763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 36797906d00cSAndrea Arcangeli if (vma->anon_vma) 36805beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 36815beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 36827906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 36837906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 36847906d00cSAndrea Arcangeli } 36857906d00cSAndrea Arcangeli 36867906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 36877906d00cSAndrea Arcangeli } 36888feae131SDavid Howells 36898feae131SDavid Howells /* 36903edf41d8Sseokhoon.yoon * initialise the percpu counter for VM 36918feae131SDavid Howells */ 36928feae131SDavid Howells void __init mmap_init(void) 36938feae131SDavid Howells { 369400a62ce9SKOSAKI Motohiro int ret; 369500a62ce9SKOSAKI Motohiro 3696908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 369700a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 36988feae131SDavid Howells } 3699c9b1d098SAndrew Shewmaker 3700c9b1d098SAndrew Shewmaker /* 3701c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 3702c9b1d098SAndrew Shewmaker * 3703c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 3704c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 3705c9b1d098SAndrew Shewmaker * mode. 3706c9b1d098SAndrew Shewmaker * 3707c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 3708c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 3709c9b1d098SAndrew Shewmaker */ 37101640879aSAndrew Shewmaker static int init_user_reserve(void) 3711c9b1d098SAndrew Shewmaker { 3712c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 3713c9b1d098SAndrew Shewmaker 3714c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 3715c9b1d098SAndrew Shewmaker 3716c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 3717c9b1d098SAndrew Shewmaker return 0; 3718c9b1d098SAndrew Shewmaker } 3719a64fb3cdSPaul Gortmaker subsys_initcall(init_user_reserve); 37204eeab4f5SAndrew Shewmaker 37214eeab4f5SAndrew Shewmaker /* 37224eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 37234eeab4f5SAndrew Shewmaker * 37244eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 37254eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 37264eeab4f5SAndrew Shewmaker * 37274eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 37284eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 37294eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 37304eeab4f5SAndrew Shewmaker */ 37311640879aSAndrew Shewmaker static int init_admin_reserve(void) 37324eeab4f5SAndrew Shewmaker { 37334eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 37344eeab4f5SAndrew Shewmaker 3735c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37364eeab4f5SAndrew Shewmaker 37374eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 37384eeab4f5SAndrew Shewmaker return 0; 37394eeab4f5SAndrew Shewmaker } 3740a64fb3cdSPaul Gortmaker subsys_initcall(init_admin_reserve); 37411640879aSAndrew Shewmaker 37421640879aSAndrew Shewmaker /* 37431640879aSAndrew Shewmaker * Reinititalise user and admin reserves if memory is added or removed. 37441640879aSAndrew Shewmaker * 37451640879aSAndrew Shewmaker * The default user reserve max is 128MB, and the default max for the 37461640879aSAndrew Shewmaker * admin reserve is 8MB. These are usually, but not always, enough to 37471640879aSAndrew Shewmaker * enable recovery from a memory hogging process using login/sshd, a shell, 37481640879aSAndrew Shewmaker * and tools like top. It may make sense to increase or even disable the 37491640879aSAndrew Shewmaker * reserve depending on the existence of swap or variations in the recovery 37501640879aSAndrew Shewmaker * tools. So, the admin may have changed them. 37511640879aSAndrew Shewmaker * 37521640879aSAndrew Shewmaker * If memory is added and the reserves have been eliminated or increased above 37531640879aSAndrew Shewmaker * the default max, then we'll trust the admin. 37541640879aSAndrew Shewmaker * 37551640879aSAndrew Shewmaker * If memory is removed and there isn't enough free memory, then we 37561640879aSAndrew Shewmaker * need to reset the reserves. 37571640879aSAndrew Shewmaker * 37581640879aSAndrew Shewmaker * Otherwise keep the reserve set by the admin. 37591640879aSAndrew Shewmaker */ 37601640879aSAndrew Shewmaker static int reserve_mem_notifier(struct notifier_block *nb, 37611640879aSAndrew Shewmaker unsigned long action, void *data) 37621640879aSAndrew Shewmaker { 37631640879aSAndrew Shewmaker unsigned long tmp, free_kbytes; 37641640879aSAndrew Shewmaker 37651640879aSAndrew Shewmaker switch (action) { 37661640879aSAndrew Shewmaker case MEM_ONLINE: 37671640879aSAndrew Shewmaker /* Default max is 128MB. Leave alone if modified by operator. */ 37681640879aSAndrew Shewmaker tmp = sysctl_user_reserve_kbytes; 37691640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 17)) 37701640879aSAndrew Shewmaker init_user_reserve(); 37711640879aSAndrew Shewmaker 37721640879aSAndrew Shewmaker /* Default max is 8MB. Leave alone if modified by operator. */ 37731640879aSAndrew Shewmaker tmp = sysctl_admin_reserve_kbytes; 37741640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 13)) 37751640879aSAndrew Shewmaker init_admin_reserve(); 37761640879aSAndrew Shewmaker 37771640879aSAndrew Shewmaker break; 37781640879aSAndrew Shewmaker case MEM_OFFLINE: 3779c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37801640879aSAndrew Shewmaker 37811640879aSAndrew Shewmaker if (sysctl_user_reserve_kbytes > free_kbytes) { 37821640879aSAndrew Shewmaker init_user_reserve(); 37831640879aSAndrew Shewmaker pr_info("vm.user_reserve_kbytes reset to %lu\n", 37841640879aSAndrew Shewmaker sysctl_user_reserve_kbytes); 37851640879aSAndrew Shewmaker } 37861640879aSAndrew Shewmaker 37871640879aSAndrew Shewmaker if (sysctl_admin_reserve_kbytes > free_kbytes) { 37881640879aSAndrew Shewmaker init_admin_reserve(); 37891640879aSAndrew Shewmaker pr_info("vm.admin_reserve_kbytes reset to %lu\n", 37901640879aSAndrew Shewmaker sysctl_admin_reserve_kbytes); 37911640879aSAndrew Shewmaker } 37921640879aSAndrew Shewmaker break; 37931640879aSAndrew Shewmaker default: 37941640879aSAndrew Shewmaker break; 37951640879aSAndrew Shewmaker } 37961640879aSAndrew Shewmaker return NOTIFY_OK; 37971640879aSAndrew Shewmaker } 37981640879aSAndrew Shewmaker 37991640879aSAndrew Shewmaker static struct notifier_block reserve_mem_nb = { 38001640879aSAndrew Shewmaker .notifier_call = reserve_mem_notifier, 38011640879aSAndrew Shewmaker }; 38021640879aSAndrew Shewmaker 38031640879aSAndrew Shewmaker static int __meminit init_reserve_notifier(void) 38041640879aSAndrew Shewmaker { 38051640879aSAndrew Shewmaker if (register_hotmemory_notifier(&reserve_mem_nb)) 3806b1de0d13SMitchel Humpherys pr_err("Failed registering memory add/remove notifier for admin reserve\n"); 38071640879aSAndrew Shewmaker 38081640879aSAndrew Shewmaker return 0; 38091640879aSAndrew Shewmaker } 3810a64fb3cdSPaul Gortmaker subsys_initcall(init_reserve_notifier); 3811