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 78763ecb03SLiam 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, 80763ecb03SLiam R. Howlett struct vm_area_struct *next, unsigned long start, 8168f48381SSuren Baghdasaryan unsigned long end, bool mm_wr_locked); 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 /* 134763ecb03SLiam R. Howlett * Close a vm structure and free it. 135a8fb5618SHugh Dickins */ 136*0d2ebf9cSSuren Baghdasaryan static void remove_vma(struct vm_area_struct *vma, bool unreachable) 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)); 144*0d2ebf9cSSuren Baghdasaryan if (unreachable) 145*0d2ebf9cSSuren Baghdasaryan __vm_area_free(vma); 146*0d2ebf9cSSuren Baghdasaryan else 1473928d4f5SLinus Torvalds vm_area_free(vma); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 150b62b633eSLiam R. Howlett static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, 151b62b633eSLiam R. Howlett unsigned long min) 152b62b633eSLiam R. Howlett { 153b62b633eSLiam R. Howlett return mas_prev(&vmi->mas, min); 154b62b633eSLiam R. Howlett } 155b62b633eSLiam R. Howlett 156b62b633eSLiam R. Howlett static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, 157b62b633eSLiam R. Howlett unsigned long start, unsigned long end, gfp_t gfp) 158b62b633eSLiam R. Howlett { 159b62b633eSLiam R. Howlett vmi->mas.index = start; 160b62b633eSLiam R. Howlett vmi->mas.last = end - 1; 161b62b633eSLiam R. Howlett mas_store_gfp(&vmi->mas, NULL, gfp); 162b62b633eSLiam R. Howlett if (unlikely(mas_is_err(&vmi->mas))) 163b62b633eSLiam R. Howlett return -ENOMEM; 164b62b633eSLiam R. Howlett 165b62b633eSLiam R. Howlett return 0; 166b62b633eSLiam R. Howlett } 167b62b633eSLiam R. Howlett 1682e7ce7d3SLiam R. Howlett /* 1692e7ce7d3SLiam R. Howlett * check_brk_limits() - Use platform specific check of range & verify mlock 1702e7ce7d3SLiam R. Howlett * limits. 1712e7ce7d3SLiam R. Howlett * @addr: The address to check 1722e7ce7d3SLiam R. Howlett * @len: The size of increase. 1732e7ce7d3SLiam R. Howlett * 1742e7ce7d3SLiam R. Howlett * Return: 0 on success. 1752e7ce7d3SLiam R. Howlett */ 1762e7ce7d3SLiam R. Howlett static int check_brk_limits(unsigned long addr, unsigned long len) 1772e7ce7d3SLiam R. Howlett { 1782e7ce7d3SLiam R. Howlett unsigned long mapped_addr; 1792e7ce7d3SLiam R. Howlett 1802e7ce7d3SLiam R. Howlett mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 1812e7ce7d3SLiam R. Howlett if (IS_ERR_VALUE(mapped_addr)) 1822e7ce7d3SLiam R. Howlett return mapped_addr; 1832e7ce7d3SLiam R. Howlett 1842e7ce7d3SLiam R. Howlett return mlock_future_check(current->mm, current->mm->def_flags, len); 1852e7ce7d3SLiam R. Howlett } 18692fed820SLiam R. Howlett static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, 187763ecb03SLiam R. Howlett unsigned long addr, unsigned long request, unsigned long flags); 1886a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 1891da177e4SLinus Torvalds { 1909bc8039eSYang Shi unsigned long newbrk, oldbrk, origbrk; 1911da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1922e7ce7d3SLiam R. Howlett struct vm_area_struct *brkvma, *next = NULL; 193a5b4592cSJiri Kosina unsigned long min_brk; 194128557ffSMichel Lespinasse bool populate; 1959bc8039eSYang Shi bool downgraded = false; 196897ab3e0SMike Rapoport LIST_HEAD(uf); 19792fed820SLiam R. Howlett struct vma_iterator vmi; 1981da177e4SLinus Torvalds 199d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 200dc0ef0dfSMichal Hocko return -EINTR; 2011da177e4SLinus Torvalds 2029bc8039eSYang Shi origbrk = mm->brk; 2039bc8039eSYang Shi 204a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 2055520e894SJiri Kosina /* 2065520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 2075520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 2085520e894SJiri Kosina * to be arbitrarily shifted 2095520e894SJiri Kosina */ 2104471a675SJiri Kosina if (current->brk_randomized) 2115520e894SJiri Kosina min_brk = mm->start_brk; 2125520e894SJiri Kosina else 2135520e894SJiri Kosina min_brk = mm->end_data; 214a5b4592cSJiri Kosina #else 215a5b4592cSJiri Kosina min_brk = mm->start_brk; 216a5b4592cSJiri Kosina #endif 217a5b4592cSJiri Kosina if (brk < min_brk) 2181da177e4SLinus Torvalds goto out; 2191e624196SRam Gupta 2201e624196SRam Gupta /* 2211e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2221e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2231e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2241e624196SRam Gupta * not page aligned -Ram Gupta 2251e624196SRam Gupta */ 2268764b338SCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, 2278764b338SCyrill Gorcunov mm->end_data, mm->start_data)) 2281e624196SRam Gupta goto out; 2291e624196SRam Gupta 2301da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2311da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2329bc8039eSYang Shi if (oldbrk == newbrk) { 2339bc8039eSYang Shi mm->brk = brk; 2349bc8039eSYang Shi goto success; 2359bc8039eSYang Shi } 2361da177e4SLinus Torvalds 2379bc8039eSYang Shi /* 2389bc8039eSYang Shi * Always allow shrinking brk. 23927b26701SLiam R. Howlett * do_vma_munmap() may downgrade mmap_lock to read. 2409bc8039eSYang Shi */ 2411da177e4SLinus Torvalds if (brk <= mm->brk) { 2429bc8039eSYang Shi int ret; 2439bc8039eSYang Shi 2442e7ce7d3SLiam R. Howlett /* Search one past newbrk */ 24592fed820SLiam R. Howlett vma_iter_init(&vmi, mm, newbrk); 24692fed820SLiam R. Howlett brkvma = vma_find(&vmi, oldbrk); 247f5ad5083SJason A. Donenfeld if (!brkvma || brkvma->vm_start >= oldbrk) 2482e7ce7d3SLiam R. Howlett goto out; /* mapping intersects with an existing non-brk vma. */ 2499bc8039eSYang Shi /* 2502e7ce7d3SLiam R. Howlett * mm->brk must be protected by write mmap_lock. 25127b26701SLiam R. Howlett * do_vma_munmap() may downgrade the lock, so update it 25227b26701SLiam R. Howlett * before calling do_vma_munmap(). 2539bc8039eSYang Shi */ 2549bc8039eSYang Shi mm->brk = brk; 25527b26701SLiam R. Howlett ret = do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true); 2562e7ce7d3SLiam R. Howlett if (ret == 1) { 2572e7ce7d3SLiam R. Howlett downgraded = true; 2582e7ce7d3SLiam R. Howlett goto success; 2592e7ce7d3SLiam R. Howlett } else if (!ret) 2602e7ce7d3SLiam R. Howlett goto success; 2612e7ce7d3SLiam R. Howlett 2629bc8039eSYang Shi mm->brk = origbrk; 2631da177e4SLinus Torvalds goto out; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2662e7ce7d3SLiam R. Howlett if (check_brk_limits(oldbrk, newbrk - oldbrk)) 2672e7ce7d3SLiam R. Howlett goto out; 2682e7ce7d3SLiam R. Howlett 2692e7ce7d3SLiam R. Howlett /* 2702e7ce7d3SLiam R. Howlett * Only check if the next VMA is within the stack_guard_gap of the 2712e7ce7d3SLiam R. Howlett * expansion area 2722e7ce7d3SLiam R. Howlett */ 27392fed820SLiam R. Howlett vma_iter_init(&vmi, mm, oldbrk); 27492fed820SLiam R. Howlett next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap); 2751be7107fSHugh Dickins if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) 2761da177e4SLinus Torvalds goto out; 2771da177e4SLinus Torvalds 27892fed820SLiam R. Howlett brkvma = vma_prev_limit(&vmi, mm->start_brk); 2791da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 28092fed820SLiam R. Howlett if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) 2811da177e4SLinus Torvalds goto out; 2822e7ce7d3SLiam R. Howlett 2831da177e4SLinus Torvalds mm->brk = brk; 2849bc8039eSYang Shi 2859bc8039eSYang Shi success: 286128557ffSMichel Lespinasse populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; 2879bc8039eSYang Shi if (downgraded) 288d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2899bc8039eSYang Shi else 290d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 291897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 292128557ffSMichel Lespinasse if (populate) 293128557ffSMichel Lespinasse mm_populate(oldbrk, newbrk - oldbrk); 294128557ffSMichel Lespinasse return brk; 295128557ffSMichel Lespinasse 2961da177e4SLinus Torvalds out: 297d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 298b7204006SAdrian Huang return origbrk; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 301d4af56c5SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 302d4af56c5SLiam R. Howlett extern void mt_validate(struct maple_tree *mt); 303d4af56c5SLiam R. Howlett extern void mt_dump(const struct maple_tree *mt); 3041da177e4SLinus Torvalds 305d4af56c5SLiam R. Howlett /* Validate the maple tree */ 306d4af56c5SLiam R. Howlett static void validate_mm_mt(struct mm_struct *mm) 307d4af56c5SLiam R. Howlett { 308d4af56c5SLiam R. Howlett struct maple_tree *mt = &mm->mm_mt; 309763ecb03SLiam R. Howlett struct vm_area_struct *vma_mt; 310d4af56c5SLiam R. Howlett 311d4af56c5SLiam R. Howlett MA_STATE(mas, mt, 0, 0); 312d4af56c5SLiam R. Howlett 313d4af56c5SLiam R. Howlett mt_validate(&mm->mm_mt); 314d4af56c5SLiam R. Howlett mas_for_each(&mas, vma_mt, ULONG_MAX) { 315763ecb03SLiam R. Howlett if ((vma_mt->vm_start != mas.index) || 316763ecb03SLiam R. Howlett (vma_mt->vm_end - 1 != mas.last)) { 317d4af56c5SLiam R. Howlett pr_emerg("issue in %s\n", current->comm); 318d4af56c5SLiam R. Howlett dump_stack(); 319d4af56c5SLiam R. Howlett dump_vma(vma_mt); 320d4af56c5SLiam R. Howlett pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, 321d4af56c5SLiam R. Howlett mas.index, mas.last); 322d4af56c5SLiam R. Howlett pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, 323d4af56c5SLiam R. Howlett vma_mt->vm_start, vma_mt->vm_end); 324d4af56c5SLiam R. Howlett 325d4af56c5SLiam R. Howlett mt_dump(mas.tree); 326d4af56c5SLiam R. Howlett if (vma_mt->vm_end != mas.last + 1) { 327d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", 328d4af56c5SLiam R. Howlett mm, vma_mt->vm_start, vma_mt->vm_end, 329d4af56c5SLiam R. Howlett mas.index, mas.last); 330d4af56c5SLiam R. Howlett mt_dump(mas.tree); 331d4af56c5SLiam R. Howlett } 332d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); 333d4af56c5SLiam R. Howlett if (vma_mt->vm_start != mas.index) { 334d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", 335d4af56c5SLiam R. Howlett mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); 336d4af56c5SLiam R. Howlett mt_dump(mas.tree); 337d4af56c5SLiam R. Howlett } 338d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); 339d4af56c5SLiam R. Howlett } 340d4af56c5SLiam R. Howlett } 341d4af56c5SLiam R. Howlett } 3421da177e4SLinus Torvalds 343eafd4dc4SRashika Kheria static void validate_mm(struct mm_struct *mm) 3441da177e4SLinus Torvalds { 3451da177e4SLinus Torvalds int bug = 0; 3461da177e4SLinus Torvalds int i = 0; 347763ecb03SLiam R. Howlett struct vm_area_struct *vma; 348763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 349ff26f70fSAndrew Morton 350524e00b3SLiam R. Howlett validate_mm_mt(mm); 351524e00b3SLiam R. Howlett 352763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 353524e00b3SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 35412352d3cSKonstantin Khlebnikov struct anon_vma *anon_vma = vma->anon_vma; 355ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 356ff26f70fSAndrew Morton 35712352d3cSKonstantin Khlebnikov if (anon_vma) { 35812352d3cSKonstantin Khlebnikov anon_vma_lock_read(anon_vma); 359ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 360ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 36112352d3cSKonstantin Khlebnikov anon_vma_unlock_read(anon_vma); 36212352d3cSKonstantin Khlebnikov } 363524e00b3SLiam R. Howlett #endif 3641da177e4SLinus Torvalds i++; 3651da177e4SLinus Torvalds } 3665a0768f6SMichel Lespinasse if (i != mm->map_count) { 367763ecb03SLiam R. Howlett pr_emerg("map_count %d mas_for_each %d\n", mm->map_count, i); 3685a0768f6SMichel Lespinasse bug = 1; 3695a0768f6SMichel Lespinasse } 37096dad67fSSasha Levin VM_BUG_ON_MM(bug, mm); 3711da177e4SLinus Torvalds } 372524e00b3SLiam R. Howlett 373524e00b3SLiam R. Howlett #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ 374d4af56c5SLiam R. Howlett #define validate_mm_mt(root) do { } while (0) 3751da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 376524e00b3SLiam R. Howlett #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 377d3737187SMichel Lespinasse 378bf181b9fSMichel Lespinasse /* 379bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 380bf181b9fSMichel Lespinasse * anon_vma's interval trees. 381bf181b9fSMichel Lespinasse * 382bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 383bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 384bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 385bf181b9fSMichel Lespinasse * 386bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 387bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 388bf181b9fSMichel Lespinasse * 389c1e8d7c6SMichel Lespinasse * The entire update must be protected by exclusive mmap_lock and by 390bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 391bf181b9fSMichel Lespinasse */ 392bf181b9fSMichel Lespinasse static inline void 393bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 394bf181b9fSMichel Lespinasse { 395bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 396bf181b9fSMichel Lespinasse 397bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 398bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 399bf181b9fSMichel Lespinasse } 400bf181b9fSMichel Lespinasse 401bf181b9fSMichel Lespinasse static inline void 402bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 403bf181b9fSMichel Lespinasse { 404bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 405bf181b9fSMichel Lespinasse 406bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 407bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 408bf181b9fSMichel Lespinasse } 409bf181b9fSMichel Lespinasse 410e8420a8eSCyril Hrubis static unsigned long count_vma_pages_range(struct mm_struct *mm, 411e8420a8eSCyril Hrubis unsigned long addr, unsigned long end) 412e8420a8eSCyril Hrubis { 4132e3af1dbSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, addr); 414e8420a8eSCyril Hrubis struct vm_area_struct *vma; 4152e3af1dbSMatthew Wilcox (Oracle) unsigned long nr_pages = 0; 416e8420a8eSCyril Hrubis 4172e3af1dbSMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) { 4182e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_start = max(addr, vma->vm_start); 4192e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_end = min(end, vma->vm_end); 420e8420a8eSCyril Hrubis 4212e3af1dbSMatthew Wilcox (Oracle) nr_pages += PHYS_PFN(vm_end - vm_start); 422e8420a8eSCyril Hrubis } 423e8420a8eSCyril Hrubis 424e8420a8eSCyril Hrubis return nr_pages; 425e8420a8eSCyril Hrubis } 426e8420a8eSCyril Hrubis 427c154124fSLiam R. Howlett static void __vma_link_file(struct vm_area_struct *vma, 428c154124fSLiam R. Howlett struct address_space *mapping) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 431cf508b58SMiaohe Lin mapping_allow_writable(mapping); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 4346b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 4351da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 438763ecb03SLiam R. Howlett static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) 4391da177e4SLinus Torvalds { 44079e4f2caSLiam R. Howlett VMA_ITERATOR(vmi, mm, 0); 4411da177e4SLinus Torvalds struct address_space *mapping = NULL; 4421da177e4SLinus Torvalds 44379e4f2caSLiam R. Howlett if (vma_iter_prealloc(&vmi)) 444d4af56c5SLiam R. Howlett return -ENOMEM; 445d4af56c5SLiam R. Howlett 44664ac4940SHuang Shijie if (vma->vm_file) { 4471da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 44883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 44964ac4940SHuang Shijie } 4501da177e4SLinus Torvalds 45179e4f2caSLiam R. Howlett vma_iter_store(&vmi, vma); 4521da177e4SLinus Torvalds 453c154124fSLiam R. Howlett if (mapping) { 454c154124fSLiam R. Howlett __vma_link_file(vma, mapping); 45583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 456c154124fSLiam R. Howlett } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds mm->map_count++; 4591da177e4SLinus Torvalds validate_mm(mm); 460d4af56c5SLiam R. Howlett return 0; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* 46468cefec5SLiam R. Howlett * init_multi_vma_prep() - Initializer for struct vma_prepare 46568cefec5SLiam R. Howlett * @vp: The vma_prepare struct 46668cefec5SLiam R. Howlett * @vma: The vma that will be altered once locked 46768cefec5SLiam R. Howlett * @next: The next vma if it is to be adjusted 46868cefec5SLiam R. Howlett * @remove: The first vma to be removed 46968cefec5SLiam R. Howlett * @remove2: The second vma to be removed 47068cefec5SLiam R. Howlett */ 47168cefec5SLiam R. Howlett static inline void init_multi_vma_prep(struct vma_prepare *vp, 47268cefec5SLiam R. Howlett struct vm_area_struct *vma, struct vm_area_struct *next, 47368cefec5SLiam R. Howlett struct vm_area_struct *remove, struct vm_area_struct *remove2) 47468cefec5SLiam R. Howlett { 47568cefec5SLiam R. Howlett memset(vp, 0, sizeof(struct vma_prepare)); 47668cefec5SLiam R. Howlett vp->vma = vma; 47768cefec5SLiam R. Howlett vp->anon_vma = vma->anon_vma; 47868cefec5SLiam R. Howlett vp->remove = remove; 47968cefec5SLiam R. Howlett vp->remove2 = remove2; 48068cefec5SLiam R. Howlett vp->adj_next = next; 48168cefec5SLiam R. Howlett if (!vp->anon_vma && next) 48268cefec5SLiam R. Howlett vp->anon_vma = next->anon_vma; 48368cefec5SLiam R. Howlett 48468cefec5SLiam R. Howlett vp->file = vma->vm_file; 48568cefec5SLiam R. Howlett if (vp->file) 48668cefec5SLiam R. Howlett vp->mapping = vma->vm_file->f_mapping; 48768cefec5SLiam R. Howlett 48868cefec5SLiam R. Howlett } 48968cefec5SLiam R. Howlett 49068cefec5SLiam R. Howlett /* 49168cefec5SLiam R. Howlett * init_vma_prep() - Initializer wrapper for vma_prepare struct 49268cefec5SLiam R. Howlett * @vp: The vma_prepare struct 49368cefec5SLiam R. Howlett * @vma: The vma that will be altered once locked 49468cefec5SLiam R. Howlett */ 49568cefec5SLiam R. Howlett static inline void init_vma_prep(struct vma_prepare *vp, 49668cefec5SLiam R. Howlett struct vm_area_struct *vma) 49768cefec5SLiam R. Howlett { 49868cefec5SLiam R. Howlett init_multi_vma_prep(vp, vma, NULL, NULL, NULL); 49968cefec5SLiam R. Howlett } 50068cefec5SLiam R. Howlett 50168cefec5SLiam R. Howlett 50268cefec5SLiam R. Howlett /* 503440703e0SLiam R. Howlett * vma_prepare() - Helper function for handling locking VMAs prior to altering 504440703e0SLiam R. Howlett * @vp: The initialized vma_prepare struct 505440703e0SLiam R. Howlett */ 506440703e0SLiam R. Howlett static inline void vma_prepare(struct vma_prepare *vp) 507440703e0SLiam R. Howlett { 50810fca64aSSuren Baghdasaryan vma_start_write(vp->vma); 50910fca64aSSuren Baghdasaryan if (vp->adj_next) 51010fca64aSSuren Baghdasaryan vma_start_write(vp->adj_next); 51110fca64aSSuren Baghdasaryan /* vp->insert is always a newly created VMA, no need for locking */ 51210fca64aSSuren Baghdasaryan if (vp->remove) 51310fca64aSSuren Baghdasaryan vma_start_write(vp->remove); 51410fca64aSSuren Baghdasaryan if (vp->remove2) 51510fca64aSSuren Baghdasaryan vma_start_write(vp->remove2); 51610fca64aSSuren Baghdasaryan 517440703e0SLiam R. Howlett if (vp->file) { 518440703e0SLiam R. Howlett uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); 519440703e0SLiam R. Howlett 520440703e0SLiam R. Howlett if (vp->adj_next) 521440703e0SLiam R. Howlett uprobe_munmap(vp->adj_next, vp->adj_next->vm_start, 522440703e0SLiam R. Howlett vp->adj_next->vm_end); 523440703e0SLiam R. Howlett 524440703e0SLiam R. Howlett i_mmap_lock_write(vp->mapping); 525440703e0SLiam R. Howlett if (vp->insert && vp->insert->vm_file) { 526440703e0SLiam R. Howlett /* 527440703e0SLiam R. Howlett * Put into interval tree now, so instantiated pages 528440703e0SLiam R. Howlett * are visible to arm/parisc __flush_dcache_page 529440703e0SLiam R. Howlett * throughout; but we cannot insert into address 530440703e0SLiam R. Howlett * space until vma start or end is updated. 531440703e0SLiam R. Howlett */ 532440703e0SLiam R. Howlett __vma_link_file(vp->insert, 533440703e0SLiam R. Howlett vp->insert->vm_file->f_mapping); 534440703e0SLiam R. Howlett } 535440703e0SLiam R. Howlett } 536440703e0SLiam R. Howlett 537440703e0SLiam R. Howlett if (vp->anon_vma) { 538440703e0SLiam R. Howlett anon_vma_lock_write(vp->anon_vma); 539440703e0SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vp->vma); 540440703e0SLiam R. Howlett if (vp->adj_next) 541440703e0SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vp->adj_next); 542440703e0SLiam R. Howlett } 543440703e0SLiam R. Howlett 544440703e0SLiam R. Howlett if (vp->file) { 545440703e0SLiam R. Howlett flush_dcache_mmap_lock(vp->mapping); 546440703e0SLiam R. Howlett vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap); 547440703e0SLiam R. Howlett if (vp->adj_next) 548440703e0SLiam R. Howlett vma_interval_tree_remove(vp->adj_next, 549440703e0SLiam R. Howlett &vp->mapping->i_mmap); 550440703e0SLiam R. Howlett } 551440703e0SLiam R. Howlett 552440703e0SLiam R. Howlett } 553440703e0SLiam R. Howlett 554440703e0SLiam R. Howlett /* 555440703e0SLiam R. Howlett * vma_complete- Helper function for handling the unlocking after altering VMAs, 556440703e0SLiam R. Howlett * or for inserting a VMA. 557440703e0SLiam R. Howlett * 558440703e0SLiam R. Howlett * @vp: The vma_prepare struct 559440703e0SLiam R. Howlett * @vmi: The vma iterator 560440703e0SLiam R. Howlett * @mm: The mm_struct 561440703e0SLiam R. Howlett */ 562440703e0SLiam R. Howlett static inline void vma_complete(struct vma_prepare *vp, 563440703e0SLiam R. Howlett struct vma_iterator *vmi, struct mm_struct *mm) 564440703e0SLiam R. Howlett { 565440703e0SLiam R. Howlett if (vp->file) { 566440703e0SLiam R. Howlett if (vp->adj_next) 567440703e0SLiam R. Howlett vma_interval_tree_insert(vp->adj_next, 568440703e0SLiam R. Howlett &vp->mapping->i_mmap); 569440703e0SLiam R. Howlett vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap); 570440703e0SLiam R. Howlett flush_dcache_mmap_unlock(vp->mapping); 571440703e0SLiam R. Howlett } 572440703e0SLiam R. Howlett 573440703e0SLiam R. Howlett if (vp->remove && vp->file) { 574440703e0SLiam R. Howlett __remove_shared_vm_struct(vp->remove, vp->file, vp->mapping); 575440703e0SLiam R. Howlett if (vp->remove2) 576440703e0SLiam R. Howlett __remove_shared_vm_struct(vp->remove2, vp->file, 577440703e0SLiam R. Howlett vp->mapping); 578440703e0SLiam R. Howlett } else if (vp->insert) { 579440703e0SLiam R. Howlett /* 580440703e0SLiam R. Howlett * split_vma has split insert from vma, and needs 581440703e0SLiam R. Howlett * us to insert it before dropping the locks 582440703e0SLiam R. Howlett * (it may either follow vma or precede it). 583440703e0SLiam R. Howlett */ 584440703e0SLiam R. Howlett vma_iter_store(vmi, vp->insert); 585440703e0SLiam R. Howlett mm->map_count++; 586440703e0SLiam R. Howlett } 587440703e0SLiam R. Howlett 588440703e0SLiam R. Howlett if (vp->anon_vma) { 589440703e0SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vp->vma); 590440703e0SLiam R. Howlett if (vp->adj_next) 591440703e0SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vp->adj_next); 592440703e0SLiam R. Howlett anon_vma_unlock_write(vp->anon_vma); 593440703e0SLiam R. Howlett } 594440703e0SLiam R. Howlett 595440703e0SLiam R. Howlett if (vp->file) { 596440703e0SLiam R. Howlett i_mmap_unlock_write(vp->mapping); 597440703e0SLiam R. Howlett uprobe_mmap(vp->vma); 598440703e0SLiam R. Howlett 599440703e0SLiam R. Howlett if (vp->adj_next) 600440703e0SLiam R. Howlett uprobe_mmap(vp->adj_next); 601440703e0SLiam R. Howlett } 602440703e0SLiam R. Howlett 603440703e0SLiam R. Howlett if (vp->remove) { 604440703e0SLiam R. Howlett again: 605457f67beSSuren Baghdasaryan vma_mark_detached(vp->remove, true); 606440703e0SLiam R. Howlett if (vp->file) { 607440703e0SLiam R. Howlett uprobe_munmap(vp->remove, vp->remove->vm_start, 608440703e0SLiam R. Howlett vp->remove->vm_end); 609440703e0SLiam R. Howlett fput(vp->file); 610440703e0SLiam R. Howlett } 611440703e0SLiam R. Howlett if (vp->remove->anon_vma) 612440703e0SLiam R. Howlett anon_vma_merge(vp->vma, vp->remove); 613440703e0SLiam R. Howlett mm->map_count--; 614440703e0SLiam R. Howlett mpol_put(vma_policy(vp->remove)); 615440703e0SLiam R. Howlett if (!vp->remove2) 616440703e0SLiam R. Howlett WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end); 617440703e0SLiam R. Howlett vm_area_free(vp->remove); 618440703e0SLiam R. Howlett 619440703e0SLiam R. Howlett /* 620440703e0SLiam R. Howlett * In mprotect's case 6 (see comments on vma_merge), 6215ff783f1SVlastimil Babka * we are removing both mid and next vmas 622440703e0SLiam R. Howlett */ 623440703e0SLiam R. Howlett if (vp->remove2) { 624440703e0SLiam R. Howlett vp->remove = vp->remove2; 625440703e0SLiam R. Howlett vp->remove2 = NULL; 626440703e0SLiam R. Howlett goto again; 627440703e0SLiam R. Howlett } 628440703e0SLiam R. Howlett } 629440703e0SLiam R. Howlett if (vp->insert && vp->file) 630440703e0SLiam R. Howlett uprobe_mmap(vp->insert); 631440703e0SLiam R. Howlett } 632440703e0SLiam R. Howlett 633440703e0SLiam R. Howlett /* 63404241ffeSLiam R. Howlett * dup_anon_vma() - Helper function to duplicate anon_vma 63504241ffeSLiam R. Howlett * @dst: The destination VMA 63604241ffeSLiam R. Howlett * @src: The source VMA 63704241ffeSLiam R. Howlett * 63804241ffeSLiam R. Howlett * Returns: 0 on success. 63904241ffeSLiam R. Howlett */ 64004241ffeSLiam R. Howlett static inline int dup_anon_vma(struct vm_area_struct *dst, 64104241ffeSLiam R. Howlett struct vm_area_struct *src) 64204241ffeSLiam R. Howlett { 64304241ffeSLiam R. Howlett /* 64404241ffeSLiam R. Howlett * Easily overlooked: when mprotect shifts the boundary, make sure the 64504241ffeSLiam R. Howlett * expanding vma has anon_vma set if the shrinking vma had, to cover any 64604241ffeSLiam R. Howlett * anon pages imported. 64704241ffeSLiam R. Howlett */ 64804241ffeSLiam R. Howlett if (src->anon_vma && !dst->anon_vma) { 64904241ffeSLiam R. Howlett dst->anon_vma = src->anon_vma; 65004241ffeSLiam R. Howlett return anon_vma_clone(dst, src); 65104241ffeSLiam R. Howlett } 65204241ffeSLiam R. Howlett 65304241ffeSLiam R. Howlett return 0; 65404241ffeSLiam R. Howlett } 65504241ffeSLiam R. Howlett 65604241ffeSLiam R. Howlett /* 6579303d3e1SLiam R. Howlett * vma_expand - Expand an existing VMA 6589303d3e1SLiam R. Howlett * 6599303d3e1SLiam R. Howlett * @vmi: The vma iterator 6609303d3e1SLiam R. Howlett * @vma: The vma to expand 6619303d3e1SLiam R. Howlett * @start: The start of the vma 6629303d3e1SLiam R. Howlett * @end: The exclusive end of the vma 6639303d3e1SLiam R. Howlett * @pgoff: The page offset of vma 6649303d3e1SLiam R. Howlett * @next: The current of next vma. 6659303d3e1SLiam R. Howlett * 6669303d3e1SLiam R. Howlett * Expand @vma to @start and @end. Can expand off the start and end. Will 6679303d3e1SLiam R. Howlett * expand over @next if it's different from @vma and @end == @next->vm_end. 6689303d3e1SLiam R. Howlett * Checking if the @vma can expand and merge with @next needs to be handled by 6699303d3e1SLiam R. Howlett * the caller. 6709303d3e1SLiam R. Howlett * 6719303d3e1SLiam R. Howlett * Returns: 0 on success 6729303d3e1SLiam R. Howlett */ 6737c9813e8SLiam R. Howlett int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, 6749303d3e1SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 6759303d3e1SLiam R. Howlett struct vm_area_struct *next) 6769303d3e1SLiam R. Howlett { 67768cefec5SLiam R. Howlett bool remove_next = false; 6789303d3e1SLiam R. Howlett struct vma_prepare vp; 6799303d3e1SLiam R. Howlett 6809303d3e1SLiam R. Howlett if (next && (vma != next) && (end == next->vm_end)) { 68104241ffeSLiam R. Howlett int ret; 6829303d3e1SLiam R. Howlett 68304241ffeSLiam R. Howlett remove_next = true; 68404241ffeSLiam R. Howlett ret = dup_anon_vma(vma, next); 68504241ffeSLiam R. Howlett if (ret) 68604241ffeSLiam R. Howlett return ret; 6879303d3e1SLiam R. Howlett } 6889303d3e1SLiam R. Howlett 68968cefec5SLiam R. Howlett init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); 6909303d3e1SLiam R. Howlett /* Not merging but overwriting any part of next is not handled. */ 6919303d3e1SLiam R. Howlett VM_WARN_ON(next && !vp.remove && 6929303d3e1SLiam R. Howlett next != vma && end > next->vm_start); 6939303d3e1SLiam R. Howlett /* Only handles expanding */ 6949303d3e1SLiam R. Howlett VM_WARN_ON(vma->vm_start < start || vma->vm_end > end); 6959303d3e1SLiam R. Howlett 6969303d3e1SLiam R. Howlett if (vma_iter_prealloc(vmi)) 6979303d3e1SLiam R. Howlett goto nomem; 6989303d3e1SLiam R. Howlett 699ccf1d78dSSuren Baghdasaryan vma_prepare(&vp); 7009303d3e1SLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 7019303d3e1SLiam R. Howlett /* VMA iterator points to previous, so set to start if necessary */ 7029303d3e1SLiam R. Howlett if (vma_iter_addr(vmi) != start) 7039303d3e1SLiam R. Howlett vma_iter_set(vmi, start); 7049303d3e1SLiam R. Howlett 7059303d3e1SLiam R. Howlett vma->vm_start = start; 7069303d3e1SLiam R. Howlett vma->vm_end = end; 7079303d3e1SLiam R. Howlett vma->vm_pgoff = pgoff; 7089303d3e1SLiam R. Howlett /* Note: mas must be pointing to the expanding VMA */ 7099303d3e1SLiam R. Howlett vma_iter_store(vmi, vma); 7109303d3e1SLiam R. Howlett 7119303d3e1SLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 7129303d3e1SLiam R. Howlett validate_mm(vma->vm_mm); 7139303d3e1SLiam R. Howlett return 0; 7149303d3e1SLiam R. Howlett 7159303d3e1SLiam R. Howlett nomem: 7169303d3e1SLiam R. Howlett return -ENOMEM; 7179303d3e1SLiam R. Howlett } 718cf51e86dSLiam R. Howlett 719cf51e86dSLiam R. Howlett /* 720cf51e86dSLiam R. Howlett * vma_shrink() - Reduce an existing VMAs memory area 721cf51e86dSLiam R. Howlett * @vmi: The vma iterator 722cf51e86dSLiam R. Howlett * @vma: The VMA to modify 723cf51e86dSLiam R. Howlett * @start: The new start 724cf51e86dSLiam R. Howlett * @end: The new end 725cf51e86dSLiam R. Howlett * 726cf51e86dSLiam R. Howlett * Returns: 0 on success, -ENOMEM otherwise 727cf51e86dSLiam R. Howlett */ 728cf51e86dSLiam R. Howlett int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, 729cf51e86dSLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff) 730cf51e86dSLiam R. Howlett { 731cf51e86dSLiam R. Howlett struct vma_prepare vp; 732cf51e86dSLiam R. Howlett 733cf51e86dSLiam R. Howlett WARN_ON((vma->vm_start != start) && (vma->vm_end != end)); 734cf51e86dSLiam R. Howlett 735cf51e86dSLiam R. Howlett if (vma_iter_prealloc(vmi)) 736cf51e86dSLiam R. Howlett return -ENOMEM; 737cf51e86dSLiam R. Howlett 738cf51e86dSLiam R. Howlett init_vma_prep(&vp, vma); 739cf51e86dSLiam R. Howlett vma_prepare(&vp); 740ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, start, end, 0); 741cf51e86dSLiam R. Howlett 742cf51e86dSLiam R. Howlett if (vma->vm_start < start) 743cf51e86dSLiam R. Howlett vma_iter_clear(vmi, vma->vm_start, start); 744cf51e86dSLiam R. Howlett 745cf51e86dSLiam R. Howlett if (vma->vm_end > end) 746cf51e86dSLiam R. Howlett vma_iter_clear(vmi, end, vma->vm_end); 747cf51e86dSLiam R. Howlett 748cf51e86dSLiam R. Howlett vma->vm_start = start; 749cf51e86dSLiam R. Howlett vma->vm_end = end; 750cf51e86dSLiam R. Howlett vma->vm_pgoff = pgoff; 751cf51e86dSLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 752cf51e86dSLiam R. Howlett validate_mm(vma->vm_mm); 753cf51e86dSLiam R. Howlett return 0; 754cf51e86dSLiam R. Howlett } 755cf51e86dSLiam R. Howlett 7569303d3e1SLiam R. Howlett /* 7571da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 758714965caSVlastimil Babka * per-vma resources, so we don't attempt to merge those if the caller indicates 759714965caSVlastimil Babka * the current vma may be removed as part of the merge. 7601da177e4SLinus Torvalds */ 7612dbf4010SVlastimil Babka static inline bool is_mergeable_vma(struct vm_area_struct *vma, 76219a809afSAndrea Arcangeli struct file *file, unsigned long vm_flags, 7639a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 764714965caSVlastimil Babka struct anon_vma_name *anon_name, bool may_remove_vma) 7651da177e4SLinus Torvalds { 76634228d47SCyrill Gorcunov /* 76734228d47SCyrill Gorcunov * VM_SOFTDIRTY should not prevent from VMA merging, if we 76834228d47SCyrill Gorcunov * match the flags but dirty bit -- the caller should mark 76934228d47SCyrill Gorcunov * merged VMA as dirty. If dirty bit won't be excluded from 7708bb4e7a2SWei Yang * comparison, we increase pressure on the memory system forcing 77134228d47SCyrill Gorcunov * the kernel to generate new VMAs when old one could be 77234228d47SCyrill Gorcunov * extended instead. 77334228d47SCyrill Gorcunov */ 77434228d47SCyrill Gorcunov if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 7752dbf4010SVlastimil Babka return false; 7761da177e4SLinus Torvalds if (vma->vm_file != file) 7772dbf4010SVlastimil Babka return false; 778714965caSVlastimil Babka if (may_remove_vma && vma->vm_ops && vma->vm_ops->close) 7792dbf4010SVlastimil Babka return false; 78019a809afSAndrea Arcangeli if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 7812dbf4010SVlastimil Babka return false; 7825c26f6acSSuren Baghdasaryan if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 7832dbf4010SVlastimil Babka return false; 7842dbf4010SVlastimil Babka return true; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7872dbf4010SVlastimil Babka static inline bool is_mergeable_anon_vma(struct anon_vma *anon_vma1, 7882dbf4010SVlastimil Babka struct anon_vma *anon_vma2, struct vm_area_struct *vma) 7891da177e4SLinus Torvalds { 790965f55deSShaohua Li /* 791965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 792965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 793965f55deSShaohua Li */ 794965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 795965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 7962dbf4010SVlastimil Babka return true; 797965f55deSShaohua Li return anon_vma1 == anon_vma2; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds /* 8011da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 8021da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 8051da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 8061da177e4SLinus Torvalds * 8071da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 80845e55300SPeter Collingbourne * indices (16TB on ia32) because do_mmap() does not permit mmap's which 8091da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 810714965caSVlastimil Babka * 811714965caSVlastimil Babka * We assume the vma may be removed as part of the merge. 8121da177e4SLinus Torvalds */ 8132dbf4010SVlastimil Babka static bool 8141da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 81519a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 8162dbf4010SVlastimil Babka pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 8175c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 8181da177e4SLinus Torvalds { 819714965caSVlastimil Babka if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, true) && 820965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 8211da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 8222dbf4010SVlastimil Babka return true; 8231da177e4SLinus Torvalds } 8242dbf4010SVlastimil Babka return false; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 8291da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 8301da177e4SLinus Torvalds * 8311da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 8321da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 833714965caSVlastimil Babka * 834714965caSVlastimil Babka * We assume that vma is not removed as part of the merge. 8351da177e4SLinus Torvalds */ 8362dbf4010SVlastimil Babka static bool 8371da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 83819a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 8392dbf4010SVlastimil Babka pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 8405c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 8411da177e4SLinus Torvalds { 842714965caSVlastimil Babka if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, false) && 843965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 8441da177e4SLinus Torvalds pgoff_t vm_pglen; 845d6e93217SLibin vm_pglen = vma_pages(vma); 8461da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 8472dbf4010SVlastimil Babka return true; 8481da177e4SLinus Torvalds } 8492dbf4010SVlastimil Babka return false; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* 8539a10064fSColin Cross * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 8549a10064fSColin Cross * figure out whether that can be merged with its predecessor or its 8559a10064fSColin Cross * successor. Or both (it neatly fills a hole). 8561da177e4SLinus Torvalds * 8571da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 8581da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 8591da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 8601da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 8611da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 8621da177e4SLinus Torvalds * case has already been eliminated. 8631da177e4SLinus Torvalds * 864fcfccd91SLorenzo Stoakes * The following mprotect cases have to be considered, where **** is 8651da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 866fcfccd91SLorenzo Stoakes * vma, PPPP is the previous vma, CCCC is a concurrent vma that starts 867fcfccd91SLorenzo Stoakes * at the same address as **** and is of the same or larger span, and 868fcfccd91SLorenzo Stoakes * NNNN the next vma after ****: 8691da177e4SLinus Torvalds * 870fcfccd91SLorenzo Stoakes * **** **** **** 871fcfccd91SLorenzo Stoakes * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPCCCCCC 8725d42ab29SWei Yang * cannot merge might become might become 873fcfccd91SLorenzo Stoakes * PPNNNNNNNNNN PPPPPPPPPPCC 8745d42ab29SWei Yang * mmap, brk or case 4 below case 5 below 8755d42ab29SWei Yang * mremap move: 876fcfccd91SLorenzo Stoakes * **** **** 877fcfccd91SLorenzo Stoakes * PPPP NNNN PPPPCCCCNNNN 8785d42ab29SWei Yang * might become might become 8795d42ab29SWei Yang * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 880fcfccd91SLorenzo Stoakes * PPPPPPPPNNNN 2 or PPPPPPPPNNNN 7 or 881fcfccd91SLorenzo Stoakes * PPPPNNNNNNNN 3 PPPPNNNNNNNN 8 8821da177e4SLinus Torvalds * 883fcfccd91SLorenzo Stoakes * It is important for case 8 that the vma CCCC overlapping the 884fcfccd91SLorenzo Stoakes * region **** is never going to extended over NNNN. Instead NNNN must 885fcfccd91SLorenzo Stoakes * be extended in region **** and CCCC must be removed. This way in 8860503ea8fSLiam R. Howlett * all cases where vma_merge succeeds, the moment vma_merge drops the 887e86f15eeSAndrea Arcangeli * rmap_locks, the properties of the merged vma will be already 888e86f15eeSAndrea Arcangeli * correct for the whole merged range. Some of those properties like 889e86f15eeSAndrea Arcangeli * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 890e86f15eeSAndrea Arcangeli * be correct for the whole merged range immediately after the 891fcfccd91SLorenzo Stoakes * rmap_locks are released. Otherwise if NNNN would be removed and 892fcfccd91SLorenzo Stoakes * CCCC would be extended over the NNNN range, remove_migration_ptes 893e86f15eeSAndrea Arcangeli * or other rmap walkers (if working on addresses beyond the "end" 894fcfccd91SLorenzo Stoakes * parameter) may establish ptes with the wrong permissions of CCCC 895fcfccd91SLorenzo Stoakes * instead of the right permissions of NNNN. 8960503ea8fSLiam R. Howlett * 8970503ea8fSLiam R. Howlett * In the code below: 8980503ea8fSLiam R. Howlett * PPPP is represented by *prev 899fcfccd91SLorenzo Stoakes * CCCC is represented by *curr or not represented at all (NULL) 900fcfccd91SLorenzo Stoakes * NNNN is represented by *next or not represented at all (NULL) 901fcfccd91SLorenzo Stoakes * **** is not represented - it will be merged and the vma containing the 9029e8a39d2SVlastimil Babka * area is returned, or the function will return NULL 9031da177e4SLinus Torvalds */ 9049760ebffSLiam R. Howlett struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, 9051da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 9061da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 9071da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 90819a809afSAndrea Arcangeli pgoff_t pgoff, struct mempolicy *policy, 9099a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 9105c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 9111da177e4SLinus Torvalds { 912b0729ae0SLorenzo Stoakes struct vm_area_struct *curr, *next, *res; 9130503ea8fSLiam R. Howlett struct vm_area_struct *vma, *adjust, *remove, *remove2; 9140173db4fSLorenzo Stoakes struct vma_prepare vp; 9150173db4fSLorenzo Stoakes pgoff_t vma_pgoff; 9160173db4fSLorenzo Stoakes int err = 0; 917eef19944SJakub Matěna bool merge_prev = false; 918eef19944SJakub Matěna bool merge_next = false; 9190503ea8fSLiam R. Howlett bool vma_expanded = false; 9200503ea8fSLiam R. Howlett unsigned long vma_start = addr; 9210173db4fSLorenzo Stoakes unsigned long vma_end = end; 9220173db4fSLorenzo Stoakes pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 9230173db4fSLorenzo Stoakes long adj_start = 0; 9241da177e4SLinus Torvalds 9250503ea8fSLiam R. Howlett validate_mm(mm); 9261da177e4SLinus Torvalds /* 9271da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 9281da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 9291da177e4SLinus Torvalds */ 9301da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 9311da177e4SLinus Torvalds return NULL; 9321da177e4SLinus Torvalds 93300cd00a6SLorenzo Stoakes /* Does the input range span an existing VMA? (cases 5 - 8) */ 93400cd00a6SLorenzo Stoakes curr = find_vma_intersection(mm, prev ? prev->vm_end : 0, end); 9351da177e4SLinus Torvalds 93600cd00a6SLorenzo Stoakes if (!curr || /* cases 1 - 4 */ 93700cd00a6SLorenzo Stoakes end == curr->vm_end) /* cases 6 - 8, adjacent VMA */ 93800cd00a6SLorenzo Stoakes next = vma_lookup(mm, end); 93900cd00a6SLorenzo Stoakes else 94000cd00a6SLorenzo Stoakes next = NULL; /* case 5 */ 9419e8a39d2SVlastimil Babka 9420503ea8fSLiam R. Howlett if (prev) { 9430503ea8fSLiam R. Howlett vma_start = prev->vm_start; 9440503ea8fSLiam R. Howlett vma_pgoff = prev->vm_pgoff; 9450173db4fSLorenzo Stoakes 946eef19944SJakub Matěna /* Can we merge the predecessor? */ 9470173db4fSLorenzo Stoakes if (addr == prev->vm_end && mpol_equal(vma_policy(prev), policy) 9480503ea8fSLiam R. Howlett && can_vma_merge_after(prev, vm_flags, anon_vma, file, 9490503ea8fSLiam R. Howlett pgoff, vm_userfaultfd_ctx, anon_name)) { 950eef19944SJakub Matěna merge_prev = true; 95118b098afSLiam R. Howlett vma_prev(vmi); 9521da177e4SLinus Torvalds } 9530503ea8fSLiam R. Howlett } 954b0729ae0SLorenzo Stoakes 955eef19944SJakub Matěna /* Can we merge the successor? */ 95600cd00a6SLorenzo Stoakes if (next && mpol_equal(policy, vma_policy(next)) && 9570173db4fSLorenzo Stoakes can_vma_merge_before(next, vm_flags, anon_vma, file, pgoff+pglen, 9589a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 959eef19944SJakub Matěna merge_next = true; 960eef19944SJakub Matěna } 9610503ea8fSLiam R. Howlett 9620173db4fSLorenzo Stoakes if (!merge_prev && !merge_next) 9630173db4fSLorenzo Stoakes return NULL; /* Not mergeable. */ 9640173db4fSLorenzo Stoakes 9650173db4fSLorenzo Stoakes res = vma = prev; 9660503ea8fSLiam R. Howlett remove = remove2 = adjust = NULL; 9670173db4fSLorenzo Stoakes 9680173db4fSLorenzo Stoakes /* Verify some invariant that must be enforced by the caller. */ 9690173db4fSLorenzo Stoakes VM_WARN_ON(prev && addr <= prev->vm_start); 9700173db4fSLorenzo Stoakes VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); 9710173db4fSLorenzo Stoakes VM_WARN_ON(addr >= end); 9720173db4fSLorenzo Stoakes 973eef19944SJakub Matěna /* Can we merge both the predecessor and the successor? */ 974eef19944SJakub Matěna if (merge_prev && merge_next && 9750503ea8fSLiam R. Howlett is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) { 9765ff783f1SVlastimil Babka remove = next; /* case 1 */ 9770503ea8fSLiam R. Howlett vma_end = next->vm_end; 97850dac011SVlastimil Babka err = dup_anon_vma(prev, next); 979fcfccd91SLorenzo Stoakes if (curr) { /* case 6 */ 980fcfccd91SLorenzo Stoakes remove = curr; 9815ff783f1SVlastimil Babka remove2 = next; 9825ff783f1SVlastimil Babka if (!next->anon_vma) 983fcfccd91SLorenzo Stoakes err = dup_anon_vma(prev, curr); 9840503ea8fSLiam R. Howlett } 9850173db4fSLorenzo Stoakes } else if (merge_prev) { /* case 2 */ 986fcfccd91SLorenzo Stoakes if (curr) { 987fcfccd91SLorenzo Stoakes err = dup_anon_vma(prev, curr); 988fcfccd91SLorenzo Stoakes if (end == curr->vm_end) { /* case 7 */ 989fcfccd91SLorenzo Stoakes remove = curr; 9900503ea8fSLiam R. Howlett } else { /* case 5 */ 991fcfccd91SLorenzo Stoakes adjust = curr; 992fcfccd91SLorenzo Stoakes adj_start = (end - curr->vm_start); 9930503ea8fSLiam R. Howlett } 9940503ea8fSLiam R. Howlett } 9950173db4fSLorenzo Stoakes } else { /* merge_next */ 996eef19944SJakub Matěna res = next; 9970503ea8fSLiam R. Howlett if (prev && addr < prev->vm_end) { /* case 4 */ 9980503ea8fSLiam R. Howlett vma_end = addr; 999183b7a60SVlastimil Babka adjust = next; 10001e76454fSVlastimil Babka adj_start = -(prev->vm_end - addr); 1001183b7a60SVlastimil Babka err = dup_anon_vma(next, prev); 10020503ea8fSLiam R. Howlett } else { 1003b0729ae0SLorenzo Stoakes /* 1004b0729ae0SLorenzo Stoakes * Note that cases 3 and 8 are the ONLY ones where prev 1005b0729ae0SLorenzo Stoakes * is permitted to be (but is not necessarily) NULL. 1006b0729ae0SLorenzo Stoakes */ 10070503ea8fSLiam R. Howlett vma = next; /* case 3 */ 10080503ea8fSLiam R. Howlett vma_start = addr; 10090503ea8fSLiam R. Howlett vma_end = next->vm_end; 1010097d70c6SVlastimil Babka vma_pgoff = next->vm_pgoff; 1011fcfccd91SLorenzo Stoakes if (curr) { /* case 8 */ 1012fcfccd91SLorenzo Stoakes vma_pgoff = curr->vm_pgoff; 1013fcfccd91SLorenzo Stoakes remove = curr; 1014fcfccd91SLorenzo Stoakes err = dup_anon_vma(next, curr); 10150503ea8fSLiam R. Howlett } 10160503ea8fSLiam R. Howlett } 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10190173db4fSLorenzo Stoakes /* Error in anon_vma clone. */ 1020eef19944SJakub Matěna if (err) 10211da177e4SLinus Torvalds return NULL; 10220503ea8fSLiam R. Howlett 10230503ea8fSLiam R. Howlett if (vma_iter_prealloc(vmi)) 10240503ea8fSLiam R. Howlett return NULL; 10250503ea8fSLiam R. Howlett 10260503ea8fSLiam R. Howlett init_multi_vma_prep(&vp, vma, adjust, remove, remove2); 10270503ea8fSLiam R. Howlett VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma && 10280503ea8fSLiam R. Howlett vp.anon_vma != adjust->anon_vma); 10290503ea8fSLiam R. Howlett 10300503ea8fSLiam R. Howlett vma_prepare(&vp); 1031ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, vma_start, vma_end, adj_start); 10320503ea8fSLiam R. Howlett if (vma_start < vma->vm_start || vma_end > vma->vm_end) 10330503ea8fSLiam R. Howlett vma_expanded = true; 10340503ea8fSLiam R. Howlett 10350503ea8fSLiam R. Howlett vma->vm_start = vma_start; 10360503ea8fSLiam R. Howlett vma->vm_end = vma_end; 10370503ea8fSLiam R. Howlett vma->vm_pgoff = vma_pgoff; 10380503ea8fSLiam R. Howlett 10390503ea8fSLiam R. Howlett if (vma_expanded) 10400503ea8fSLiam R. Howlett vma_iter_store(vmi, vma); 10410503ea8fSLiam R. Howlett 10421e76454fSVlastimil Babka if (adj_start) { 10431e76454fSVlastimil Babka adjust->vm_start += adj_start; 10441e76454fSVlastimil Babka adjust->vm_pgoff += adj_start >> PAGE_SHIFT; 10451e76454fSVlastimil Babka if (adj_start < 0) { 10460503ea8fSLiam R. Howlett WARN_ON(vma_expanded); 10470503ea8fSLiam R. Howlett vma_iter_store(vmi, next); 10480503ea8fSLiam R. Howlett } 10490503ea8fSLiam R. Howlett } 10500503ea8fSLiam R. Howlett 10510503ea8fSLiam R. Howlett vma_complete(&vp, vmi, mm); 10520503ea8fSLiam R. Howlett vma_iter_free(vmi); 10530503ea8fSLiam R. Howlett validate_mm(mm); 1054eef19944SJakub Matěna khugepaged_enter_vma(res, vm_flags); 10551da177e4SLinus Torvalds 10569760ebffSLiam R. Howlett return res; 1057f2ebfe43SLiam R. Howlett } 1058f2ebfe43SLiam R. Howlett 10591da177e4SLinus Torvalds /* 1060b4f315b4SEthon Paul * Rough compatibility check to quickly see if it's even worth looking 1061d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1062d0e9fe17SLinus Torvalds * 1063d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1064d0e9fe17SLinus Torvalds * in things that mprotect may change. 1065d0e9fe17SLinus Torvalds * 1066d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1067d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1068d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1069d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1070d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1071d0e9fe17SLinus Torvalds */ 1072d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1073d0e9fe17SLinus Torvalds { 1074d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1075d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1076d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 10776cb4d9a2SAnshuman Khandual !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1078d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1079d0e9fe17SLinus Torvalds } 1080d0e9fe17SLinus Torvalds 1081d0e9fe17SLinus Torvalds /* 1082d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1083d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1084d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1085d0e9fe17SLinus Torvalds * to share the anon_vma. 1086d0e9fe17SLinus Torvalds * 10875b449489SFlorian Rommel * NOTE! This runs with mmap_lock held for reading, so it is possible that 1088d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1089d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1090d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1091d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 10924db0c3c2SJason Low * we do that READ_ONCE() to make sure that we never re-load the pointer. 1093d0e9fe17SLinus Torvalds * 1094d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1095d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1096d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1097d0e9fe17SLinus Torvalds * a fork). 1098d0e9fe17SLinus Torvalds * 1099d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1100d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 11015b449489SFlorian Rommel * a read lock on the mmap_lock. 1102d0e9fe17SLinus Torvalds */ 1103d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1104d0e9fe17SLinus Torvalds { 1105d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 11064db0c3c2SJason Low struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1107d0e9fe17SLinus Torvalds 1108d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1109d0e9fe17SLinus Torvalds return anon_vma; 1110d0e9fe17SLinus Torvalds } 1111d0e9fe17SLinus Torvalds return NULL; 1112d0e9fe17SLinus Torvalds } 1113d0e9fe17SLinus Torvalds 1114d0e9fe17SLinus Torvalds /* 11151da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 11161da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 11171da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 11181da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 11191da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 11201da177e4SLinus Torvalds * mprotect. 11211da177e4SLinus Torvalds */ 11221da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 11231da177e4SLinus Torvalds { 1124763ecb03SLiam R. Howlett MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); 1125a67c8caaSMiaohe Lin struct anon_vma *anon_vma = NULL; 1126763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next; 11271da177e4SLinus Torvalds 1128a67c8caaSMiaohe Lin /* Try next first. */ 1129763ecb03SLiam R. Howlett next = mas_walk(&mas); 1130763ecb03SLiam R. Howlett if (next) { 1131763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(next, vma, next); 1132d0e9fe17SLinus Torvalds if (anon_vma) 1133d0e9fe17SLinus Torvalds return anon_vma; 1134a67c8caaSMiaohe Lin } 11351da177e4SLinus Torvalds 1136763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1137763ecb03SLiam R. Howlett VM_BUG_ON_VMA(prev != vma, vma); 1138763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1139a67c8caaSMiaohe Lin /* Try prev next. */ 1140763ecb03SLiam R. Howlett if (prev) 1141763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(prev, prev, vma); 1142a67c8caaSMiaohe Lin 11431da177e4SLinus Torvalds /* 1144a67c8caaSMiaohe Lin * We might reach here with anon_vma == NULL if we can't find 1145a67c8caaSMiaohe Lin * any reusable anon_vma. 11461da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 11471da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 11481da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 11491da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 11501da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 11511da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 11521da177e4SLinus Torvalds */ 1153a67c8caaSMiaohe Lin return anon_vma; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* 115740401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 115840401530SAl Viro * low as possible but still greater than mmap_min_addr 115940401530SAl Viro */ 116040401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 116140401530SAl Viro { 116240401530SAl Viro hint &= PAGE_MASK; 116340401530SAl Viro if (((void *)hint != NULL) && 116440401530SAl Viro (hint < mmap_min_addr)) 116540401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 116640401530SAl Viro return hint; 116740401530SAl Viro } 116840401530SAl Viro 11696aeb2542SMike Rapoport int mlock_future_check(struct mm_struct *mm, unsigned long flags, 1170363ee17fSDavidlohr Bueso unsigned long len) 1171363ee17fSDavidlohr Bueso { 1172363ee17fSDavidlohr Bueso unsigned long locked, lock_limit; 1173363ee17fSDavidlohr Bueso 1174363ee17fSDavidlohr Bueso /* mlock MCL_FUTURE? */ 1175363ee17fSDavidlohr Bueso if (flags & VM_LOCKED) { 1176363ee17fSDavidlohr Bueso locked = len >> PAGE_SHIFT; 1177363ee17fSDavidlohr Bueso locked += mm->locked_vm; 1178363ee17fSDavidlohr Bueso lock_limit = rlimit(RLIMIT_MEMLOCK); 1179363ee17fSDavidlohr Bueso lock_limit >>= PAGE_SHIFT; 1180363ee17fSDavidlohr Bueso if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 1181363ee17fSDavidlohr Bueso return -EAGAIN; 1182363ee17fSDavidlohr Bueso } 1183363ee17fSDavidlohr Bueso return 0; 1184363ee17fSDavidlohr Bueso } 1185363ee17fSDavidlohr Bueso 1186be83bbf8SLinus Torvalds static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) 1187be83bbf8SLinus Torvalds { 1188be83bbf8SLinus Torvalds if (S_ISREG(inode->i_mode)) 1189423913adSLinus Torvalds return MAX_LFS_FILESIZE; 1190be83bbf8SLinus Torvalds 1191be83bbf8SLinus Torvalds if (S_ISBLK(inode->i_mode)) 1192be83bbf8SLinus Torvalds return MAX_LFS_FILESIZE; 1193be83bbf8SLinus Torvalds 119476f34950SIvan Khoronzhuk if (S_ISSOCK(inode->i_mode)) 119576f34950SIvan Khoronzhuk return MAX_LFS_FILESIZE; 119676f34950SIvan Khoronzhuk 1197be83bbf8SLinus Torvalds /* Special "we do even unsigned file positions" case */ 1198be83bbf8SLinus Torvalds if (file->f_mode & FMODE_UNSIGNED_OFFSET) 1199be83bbf8SLinus Torvalds return 0; 1200be83bbf8SLinus Torvalds 1201be83bbf8SLinus Torvalds /* Yes, random drivers might want more. But I'm tired of buggy drivers */ 1202be83bbf8SLinus Torvalds return ULONG_MAX; 1203be83bbf8SLinus Torvalds } 1204be83bbf8SLinus Torvalds 1205be83bbf8SLinus Torvalds static inline bool file_mmap_ok(struct file *file, struct inode *inode, 1206be83bbf8SLinus Torvalds unsigned long pgoff, unsigned long len) 1207be83bbf8SLinus Torvalds { 1208be83bbf8SLinus Torvalds u64 maxsize = file_mmap_size_max(file, inode); 1209be83bbf8SLinus Torvalds 1210be83bbf8SLinus Torvalds if (maxsize && len > maxsize) 1211be83bbf8SLinus Torvalds return false; 1212be83bbf8SLinus Torvalds maxsize -= len; 1213be83bbf8SLinus Torvalds if (pgoff > maxsize >> PAGE_SHIFT) 1214be83bbf8SLinus Torvalds return false; 1215be83bbf8SLinus Torvalds return true; 1216be83bbf8SLinus Torvalds } 1217be83bbf8SLinus Torvalds 121840401530SAl Viro /* 12193e4e28c5SMichel Lespinasse * The caller must write-lock current->mm->mmap_lock. 12201da177e4SLinus Torvalds */ 12211fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, unsigned long addr, 12221da177e4SLinus Torvalds unsigned long len, unsigned long prot, 122345e55300SPeter Collingbourne unsigned long flags, unsigned long pgoff, 122445e55300SPeter Collingbourne unsigned long *populate, struct list_head *uf) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 122745e55300SPeter Collingbourne vm_flags_t vm_flags; 122862b5f7d0SDave Hansen int pkey = 0; 12291da177e4SLinus Torvalds 1230524e00b3SLiam R. Howlett validate_mm(mm); 123141badc15SMichel Lespinasse *populate = 0; 1232bebeb3d6SMichel Lespinasse 1233e37609bbSPiotr Kwapulinski if (!len) 1234e37609bbSPiotr Kwapulinski return -EINVAL; 1235e37609bbSPiotr Kwapulinski 12361da177e4SLinus Torvalds /* 12371da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 12381da177e4SLinus Torvalds * 12391da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 12401da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 12411da177e4SLinus Torvalds */ 12421da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 124390f8572bSEric W. Biederman if (!(file && path_noexec(&file->f_path))) 12441da177e4SLinus Torvalds prot |= PROT_EXEC; 12451da177e4SLinus Torvalds 1246a4ff8e86SMichal Hocko /* force arch specific MAP_FIXED handling in get_unmapped_area */ 1247a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) 1248a4ff8e86SMichal Hocko flags |= MAP_FIXED; 1249a4ff8e86SMichal Hocko 12507cd94146SEric Paris if (!(flags & MAP_FIXED)) 12517cd94146SEric Paris addr = round_hint_to_min(addr); 12527cd94146SEric Paris 12531da177e4SLinus Torvalds /* Careful about overflows.. */ 12541da177e4SLinus Torvalds len = PAGE_ALIGN(len); 12559206de95SAl Viro if (!len) 12561da177e4SLinus Torvalds return -ENOMEM; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds /* offset overflow? */ 12591da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 12601da177e4SLinus Torvalds return -EOVERFLOW; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* Too many mappings? */ 12631da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 12641da177e4SLinus Torvalds return -ENOMEM; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 12671da177e4SLinus Torvalds * that it represents a valid section of the address space. 12681da177e4SLinus Torvalds */ 12691da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 1270ff68dac6SGaowei Pu if (IS_ERR_VALUE(addr)) 12711da177e4SLinus Torvalds return addr; 12721da177e4SLinus Torvalds 1273a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) { 127435e43c5fSLiam Howlett if (find_vma_intersection(mm, addr, addr + len)) 1275a4ff8e86SMichal Hocko return -EEXIST; 1276a4ff8e86SMichal Hocko } 1277a4ff8e86SMichal Hocko 127862b5f7d0SDave Hansen if (prot == PROT_EXEC) { 127962b5f7d0SDave Hansen pkey = execute_only_pkey(mm); 128062b5f7d0SDave Hansen if (pkey < 0) 128162b5f7d0SDave Hansen pkey = 0; 128262b5f7d0SDave Hansen } 128362b5f7d0SDave Hansen 12841da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 12851da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 12861da177e4SLinus Torvalds * of the memory object, so we don't do any here. 12871da177e4SLinus Torvalds */ 128845e55300SPeter Collingbourne vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | 12891da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 12901da177e4SLinus Torvalds 1291cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 12921da177e4SLinus Torvalds if (!can_do_mlock()) 12931da177e4SLinus Torvalds return -EPERM; 1294ba470de4SRik van Riel 1295363ee17fSDavidlohr Bueso if (mlock_future_check(mm, vm_flags, len)) 12961da177e4SLinus Torvalds return -EAGAIN; 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds if (file) { 1299077bf22bSOleg Nesterov struct inode *inode = file_inode(file); 13001c972597SDan Williams unsigned long flags_mask; 13011c972597SDan Williams 1302be83bbf8SLinus Torvalds if (!file_mmap_ok(file, inode, pgoff, len)) 1303be83bbf8SLinus Torvalds return -EOVERFLOW; 1304be83bbf8SLinus Torvalds 13051c972597SDan Williams flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; 1306077bf22bSOleg Nesterov 13071da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 13081da177e4SLinus Torvalds case MAP_SHARED: 13091c972597SDan Williams /* 13101c972597SDan Williams * Force use of MAP_SHARED_VALIDATE with non-legacy 13111c972597SDan Williams * flags. E.g. MAP_SYNC is dangerous to use with 13121c972597SDan Williams * MAP_SHARED as you don't know which consistency model 13131c972597SDan Williams * you will get. We silently ignore unsupported flags 13141c972597SDan Williams * with MAP_SHARED to preserve backward compatibility. 13151c972597SDan Williams */ 13161c972597SDan Williams flags &= LEGACY_MAP_MASK; 1317e4a9bc58SJoe Perches fallthrough; 13181c972597SDan Williams case MAP_SHARED_VALIDATE: 13191c972597SDan Williams if (flags & ~flags_mask) 13201c972597SDan Williams return -EOPNOTSUPP; 1321dc617f29SDarrick J. Wong if (prot & PROT_WRITE) { 1322dc617f29SDarrick J. Wong if (!(file->f_mode & FMODE_WRITE)) 13231da177e4SLinus Torvalds return -EACCES; 1324dc617f29SDarrick J. Wong if (IS_SWAPFILE(file->f_mapping->host)) 1325dc617f29SDarrick J. Wong return -ETXTBSY; 1326dc617f29SDarrick J. Wong } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* 13291da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 13301da177e4SLinus Torvalds * file.. 13311da177e4SLinus Torvalds */ 13321da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 13331da177e4SLinus Torvalds return -EACCES; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13361da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 13371da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 1338e4a9bc58SJoe Perches fallthrough; 13391da177e4SLinus Torvalds case MAP_PRIVATE: 13401da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 13411da177e4SLinus Torvalds return -EACCES; 134290f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 134380c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 134480c5606cSLinus Torvalds return -EPERM; 134580c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 134680c5606cSLinus Torvalds } 134780c5606cSLinus Torvalds 134872c2d531SAl Viro if (!file->f_op->mmap) 134980c5606cSLinus Torvalds return -ENODEV; 1350b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1351b2c56e4fSOleg Nesterov return -EINVAL; 13521da177e4SLinus Torvalds break; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds default: 13551da177e4SLinus Torvalds return -EINVAL; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds } else { 13581da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 13591da177e4SLinus Torvalds case MAP_SHARED: 1360b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1361b2c56e4fSOleg Nesterov return -EINVAL; 1362ce363942STejun Heo /* 1363ce363942STejun Heo * Ignore pgoff. 1364ce363942STejun Heo */ 1365ce363942STejun Heo pgoff = 0; 13661da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13671da177e4SLinus Torvalds break; 13681da177e4SLinus Torvalds case MAP_PRIVATE: 13691da177e4SLinus Torvalds /* 13701da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 13711da177e4SLinus Torvalds */ 13721da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 13731da177e4SLinus Torvalds break; 13741da177e4SLinus Torvalds default: 13751da177e4SLinus Torvalds return -EINVAL; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds } 13781da177e4SLinus Torvalds 1379c22c0d63SMichel Lespinasse /* 1380c22c0d63SMichel Lespinasse * Set 'VM_NORESERVE' if we should not account for the 1381c22c0d63SMichel Lespinasse * memory use of this mapping. 1382c22c0d63SMichel Lespinasse */ 1383c22c0d63SMichel Lespinasse if (flags & MAP_NORESERVE) { 1384c22c0d63SMichel Lespinasse /* We honor MAP_NORESERVE if allowed to overcommit */ 1385c22c0d63SMichel Lespinasse if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1386c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1387c22c0d63SMichel Lespinasse 1388c22c0d63SMichel Lespinasse /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 1389c22c0d63SMichel Lespinasse if (file && is_file_hugepages(file)) 1390c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1391c22c0d63SMichel Lespinasse } 1392c22c0d63SMichel Lespinasse 1393897ab3e0SMike Rapoport addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); 139409a9f1d2SMichel Lespinasse if (!IS_ERR_VALUE(addr) && 139509a9f1d2SMichel Lespinasse ((vm_flags & VM_LOCKED) || 139609a9f1d2SMichel Lespinasse (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) 139741badc15SMichel Lespinasse *populate = len; 1398bebeb3d6SMichel Lespinasse return addr; 13990165ab44SMiklos Szeredi } 14006be5ceb0SLinus Torvalds 1401a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1402a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1403a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 140466f0dc48SHugh Dickins { 140566f0dc48SHugh Dickins struct file *file = NULL; 14061e3ee14bSChen Gang unsigned long retval; 140766f0dc48SHugh Dickins 140866f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1409120a795dSAl Viro audit_mmap_fd(fd, flags); 141066f0dc48SHugh Dickins file = fget(fd); 141166f0dc48SHugh Dickins if (!file) 14121e3ee14bSChen Gang return -EBADF; 14137bba8f0eSZhen Lei if (is_file_hugepages(file)) { 1414af73e4d9SNaoya Horiguchi len = ALIGN(len, huge_page_size(hstate_file(file))); 14157bba8f0eSZhen Lei } else if (unlikely(flags & MAP_HUGETLB)) { 1416493af578SJörn Engel retval = -EINVAL; 1417493af578SJörn Engel goto out_fput; 14187bba8f0eSZhen Lei } 141966f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 1420c103a4dcSAndrew Morton struct hstate *hs; 1421af73e4d9SNaoya Horiguchi 142220ac2893SAnshuman Khandual hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 1423091d0d55SLi Zefan if (!hs) 1424091d0d55SLi Zefan return -EINVAL; 1425091d0d55SLi Zefan 1426091d0d55SLi Zefan len = ALIGN(len, huge_page_size(hs)); 142766f0dc48SHugh Dickins /* 142866f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 142966f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 143066f0dc48SHugh Dickins */ 1431af73e4d9SNaoya Horiguchi file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, 143242d7395fSAndi Kleen VM_NORESERVE, 143383c1fd76Szhangyiru HUGETLB_ANONHUGE_INODE, 143442d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 143566f0dc48SHugh Dickins if (IS_ERR(file)) 143666f0dc48SHugh Dickins return PTR_ERR(file); 143766f0dc48SHugh Dickins } 143866f0dc48SHugh Dickins 14399fbeb5abSMichal Hocko retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1440493af578SJörn Engel out_fput: 144166f0dc48SHugh Dickins if (file) 144266f0dc48SHugh Dickins fput(file); 144366f0dc48SHugh Dickins return retval; 144466f0dc48SHugh Dickins } 144566f0dc48SHugh Dickins 1446a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1447a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1448a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1449a90f590aSDominik Brodowski { 1450a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1451a90f590aSDominik Brodowski } 1452a90f590aSDominik Brodowski 1453a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1454a4679373SChristoph Hellwig struct mmap_arg_struct { 1455a4679373SChristoph Hellwig unsigned long addr; 1456a4679373SChristoph Hellwig unsigned long len; 1457a4679373SChristoph Hellwig unsigned long prot; 1458a4679373SChristoph Hellwig unsigned long flags; 1459a4679373SChristoph Hellwig unsigned long fd; 1460a4679373SChristoph Hellwig unsigned long offset; 1461a4679373SChristoph Hellwig }; 1462a4679373SChristoph Hellwig 1463a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1464a4679373SChristoph Hellwig { 1465a4679373SChristoph Hellwig struct mmap_arg_struct a; 1466a4679373SChristoph Hellwig 1467a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1468a4679373SChristoph Hellwig return -EFAULT; 1469de1741a1SAlexander Kuleshov if (offset_in_page(a.offset)) 1470a4679373SChristoph Hellwig return -EINVAL; 1471a4679373SChristoph Hellwig 1472a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1473a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1474a4679373SChristoph Hellwig } 1475a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1476a4679373SChristoph Hellwig 14774e950f6fSAlexey Dobriyan /* 14788bb4e7a2SWei Yang * Some shared mappings will want the pages marked read-only 14794e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 14804e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 14814e950f6fSAlexey Dobriyan * VM_SHARED bit). 14824e950f6fSAlexey Dobriyan */ 14836d2329f8SAndrea Arcangeli int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 14844e950f6fSAlexey Dobriyan { 1485ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 14868a04446aSKirill A. Shutemov const struct vm_operations_struct *vm_ops = vma->vm_ops; 14874e950f6fSAlexey Dobriyan 14884e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 14894e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 14904e950f6fSAlexey Dobriyan return 0; 14914e950f6fSAlexey Dobriyan 14924e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 14938a04446aSKirill A. Shutemov if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) 14944e950f6fSAlexey Dobriyan return 1; 14954e950f6fSAlexey Dobriyan 149664e45507SPeter Feiner /* The open routine did something to the protections that pgprot_modify 149764e45507SPeter Feiner * won't preserve? */ 14986d2329f8SAndrea Arcangeli if (pgprot_val(vm_page_prot) != 14996d2329f8SAndrea Arcangeli pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) 15004e950f6fSAlexey Dobriyan return 0; 15014e950f6fSAlexey Dobriyan 1502f96f7a40SDavid Hildenbrand /* 1503f96f7a40SDavid Hildenbrand * Do we need to track softdirty? hugetlb does not support softdirty 1504f96f7a40SDavid Hildenbrand * tracking yet. 1505f96f7a40SDavid Hildenbrand */ 1506f96f7a40SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 150764e45507SPeter Feiner return 1; 150864e45507SPeter Feiner 150951d3d5ebSDavid Hildenbrand /* Do we need write faults for uffd-wp tracking? */ 151051d3d5ebSDavid Hildenbrand if (userfaultfd_wp(vma)) 151151d3d5ebSDavid Hildenbrand return 1; 151251d3d5ebSDavid Hildenbrand 15134e950f6fSAlexey Dobriyan /* Specialty mapping? */ 15144b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 15154e950f6fSAlexey Dobriyan return 0; 15164e950f6fSAlexey Dobriyan 15174e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 15184e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 1519f56753acSChristoph Hellwig mapping_can_writeback(vma->vm_file->f_mapping); 15204e950f6fSAlexey Dobriyan } 15214e950f6fSAlexey Dobriyan 1522fc8744adSLinus Torvalds /* 1523fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 15245a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1525fc8744adSLinus Torvalds */ 1526ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1527fc8744adSLinus Torvalds { 15285a6fe125SMel Gorman /* 15295a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 15305a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 15315a6fe125SMel Gorman */ 15325a6fe125SMel Gorman if (file && is_file_hugepages(file)) 15335a6fe125SMel Gorman return 0; 15345a6fe125SMel Gorman 1535fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1536fc8744adSLinus Torvalds } 1537fc8744adSLinus Torvalds 15383499a131SLiam R. Howlett /** 15393499a131SLiam R. Howlett * unmapped_area() - Find an area between the low_limit and the high_limit with 15403499a131SLiam R. Howlett * the correct alignment and offset, all from @info. Note: current->mm is used 15413499a131SLiam R. Howlett * for the search. 15423499a131SLiam R. Howlett * 154382b24936SVernon Yang * @info: The unmapped area information including the range [low_limit - 154482b24936SVernon Yang * high_limit), the alignment offset and mask. 15453499a131SLiam R. Howlett * 15463499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 15473499a131SLiam R. Howlett */ 1548baceaf1cSJaewon Kim static unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1549db4fbfb9SMichel Lespinasse { 15503499a131SLiam R. Howlett unsigned long length, gap; 1551db4fbfb9SMichel Lespinasse 15523499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1553db4fbfb9SMichel Lespinasse 1554db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1555db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1556db4fbfb9SMichel Lespinasse if (length < info->length) 1557db4fbfb9SMichel Lespinasse return -ENOMEM; 1558db4fbfb9SMichel Lespinasse 15593499a131SLiam R. Howlett if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, 15603499a131SLiam R. Howlett length)) 15613499a131SLiam R. Howlett return -ENOMEM; 15623499a131SLiam R. Howlett 1563d4af56c5SLiam R. Howlett gap = mas.index; 1564d4af56c5SLiam R. Howlett gap += (info->align_offset - gap) & info->align_mask; 15653499a131SLiam R. Howlett return gap; 1566db4fbfb9SMichel Lespinasse } 1567db4fbfb9SMichel Lespinasse 15683499a131SLiam R. Howlett /** 15693499a131SLiam R. Howlett * unmapped_area_topdown() - Find an area between the low_limit and the 157082b24936SVernon Yang * high_limit with the correct alignment and offset at the highest available 15713499a131SLiam R. Howlett * address, all from @info. Note: current->mm is used for the search. 15723499a131SLiam R. Howlett * 157382b24936SVernon Yang * @info: The unmapped area information including the range [low_limit - 157482b24936SVernon Yang * high_limit), the alignment offset and mask. 15753499a131SLiam R. Howlett * 15763499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 15773499a131SLiam R. Howlett */ 1578baceaf1cSJaewon Kim static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 1579db4fbfb9SMichel Lespinasse { 15803499a131SLiam R. Howlett unsigned long length, gap; 1581d4af56c5SLiam R. Howlett 15823499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1583db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1584db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1585db4fbfb9SMichel Lespinasse if (length < info->length) 1586db4fbfb9SMichel Lespinasse return -ENOMEM; 1587db4fbfb9SMichel Lespinasse 15883499a131SLiam R. Howlett if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, 15893499a131SLiam R. Howlett length)) 15903499a131SLiam R. Howlett return -ENOMEM; 15913499a131SLiam R. Howlett 1592d4af56c5SLiam R. Howlett gap = mas.last + 1 - info->length; 1593d4af56c5SLiam R. Howlett gap -= (gap - info->align_offset) & info->align_mask; 15943499a131SLiam R. Howlett return gap; 1595db4fbfb9SMichel Lespinasse } 1596db4fbfb9SMichel Lespinasse 1597baceaf1cSJaewon Kim /* 1598baceaf1cSJaewon Kim * Search for an unmapped address range. 1599baceaf1cSJaewon Kim * 1600baceaf1cSJaewon Kim * We are looking for a range that: 1601baceaf1cSJaewon Kim * - does not intersect with any VMA; 1602baceaf1cSJaewon Kim * - is contained within the [low_limit, high_limit) interval; 1603baceaf1cSJaewon Kim * - is at least the desired size. 1604baceaf1cSJaewon Kim * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) 1605baceaf1cSJaewon Kim */ 1606baceaf1cSJaewon Kim unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) 1607baceaf1cSJaewon Kim { 1608df529cabSJaewon Kim unsigned long addr; 1609df529cabSJaewon Kim 1610baceaf1cSJaewon Kim if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) 1611df529cabSJaewon Kim addr = unmapped_area_topdown(info); 1612baceaf1cSJaewon Kim else 1613df529cabSJaewon Kim addr = unmapped_area(info); 1614df529cabSJaewon Kim 1615df529cabSJaewon Kim trace_vm_unmapped_area(addr, info); 1616df529cabSJaewon Kim return addr; 1617baceaf1cSJaewon Kim } 1618f6795053SSteve Capper 16191da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 16201da177e4SLinus Torvalds * For shmat() with addr=0. 16211da177e4SLinus Torvalds * 16221da177e4SLinus Torvalds * Ugly calling convention alert: 16231da177e4SLinus Torvalds * Return value with the low bits set means error value, 16241da177e4SLinus Torvalds * ie 16251da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 16261da177e4SLinus Torvalds * error = ret; 16271da177e4SLinus Torvalds * 16281da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 16291da177e4SLinus Torvalds */ 16301da177e4SLinus Torvalds unsigned long 16314b439e25SChristophe Leroy generic_get_unmapped_area(struct file *filp, unsigned long addr, 16324b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16334b439e25SChristophe Leroy unsigned long flags) 16341da177e4SLinus Torvalds { 16351da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 16361be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 1637db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 16382cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 16391da177e4SLinus Torvalds 1640f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 16411da177e4SLinus Torvalds return -ENOMEM; 16421da177e4SLinus Torvalds 164306abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 164406abdfb4SBenjamin Herrenschmidt return addr; 164506abdfb4SBenjamin Herrenschmidt 16461da177e4SLinus Torvalds if (addr) { 16471da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 16481be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1649f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 16501be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 16511be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 16521da177e4SLinus Torvalds return addr; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds 1655db4fbfb9SMichel Lespinasse info.flags = 0; 1656db4fbfb9SMichel Lespinasse info.length = len; 16574e99b021SHeiko Carstens info.low_limit = mm->mmap_base; 1658f6795053SSteve Capper info.high_limit = mmap_end; 1659db4fbfb9SMichel Lespinasse info.align_mask = 0; 166009ef5283SJaewon Kim info.align_offset = 0; 1661db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 16621da177e4SLinus Torvalds } 16634b439e25SChristophe Leroy 16644b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA 16654b439e25SChristophe Leroy unsigned long 16664b439e25SChristophe Leroy arch_get_unmapped_area(struct file *filp, unsigned long addr, 16674b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16684b439e25SChristophe Leroy unsigned long flags) 16694b439e25SChristophe Leroy { 16704b439e25SChristophe Leroy return generic_get_unmapped_area(filp, addr, len, pgoff, flags); 16714b439e25SChristophe Leroy } 16721da177e4SLinus Torvalds #endif 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* 16751da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 16761da177e4SLinus Torvalds * stack's low limit (the base): 16771da177e4SLinus Torvalds */ 16781da177e4SLinus Torvalds unsigned long 16794b439e25SChristophe Leroy generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 168043cca0b1SYang Fan unsigned long len, unsigned long pgoff, 168143cca0b1SYang Fan unsigned long flags) 16821da177e4SLinus Torvalds { 16831be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 16841da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1685db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 16862cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds /* requested length too big for entire address space */ 1689f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 16901da177e4SLinus Torvalds return -ENOMEM; 16911da177e4SLinus Torvalds 169206abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 169306abdfb4SBenjamin Herrenschmidt return addr; 169406abdfb4SBenjamin Herrenschmidt 16951da177e4SLinus Torvalds /* requesting a specific address */ 16961da177e4SLinus Torvalds if (addr) { 16971da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 16981be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1699f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 17001be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 17011be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 17021da177e4SLinus Torvalds return addr; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 1705db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 1706db4fbfb9SMichel Lespinasse info.length = len; 17072afc745fSAkira Takeuchi info.low_limit = max(PAGE_SIZE, mmap_min_addr); 1708f6795053SSteve Capper info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); 1709db4fbfb9SMichel Lespinasse info.align_mask = 0; 171009ef5283SJaewon Kim info.align_offset = 0; 1711db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1712b716ad95SXiao Guangrong 17131da177e4SLinus Torvalds /* 17141da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 17151da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 17161da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 17171da177e4SLinus Torvalds * allocations. 17181da177e4SLinus Torvalds */ 1719de1741a1SAlexander Kuleshov if (offset_in_page(addr)) { 1720db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 1721db4fbfb9SMichel Lespinasse info.flags = 0; 1722db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1723f6795053SSteve Capper info.high_limit = mmap_end; 1724db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1725db4fbfb9SMichel Lespinasse } 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds return addr; 17281da177e4SLinus Torvalds } 17294b439e25SChristophe Leroy 17304b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 17314b439e25SChristophe Leroy unsigned long 17324b439e25SChristophe Leroy arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 17334b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 17344b439e25SChristophe Leroy unsigned long flags) 17354b439e25SChristophe Leroy { 17364b439e25SChristophe Leroy return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); 17374b439e25SChristophe Leroy } 17381da177e4SLinus Torvalds #endif 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds unsigned long 17411da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 17421da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 17431da177e4SLinus Torvalds { 174406abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 174506abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 174607ab67c8SLinus Torvalds 17479206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 17489206de95SAl Viro if (error) 17499206de95SAl Viro return error; 17509206de95SAl Viro 17519206de95SAl Viro /* Careful about overflows.. */ 17529206de95SAl Viro if (len > TASK_SIZE) 17539206de95SAl Viro return -ENOMEM; 17549206de95SAl Viro 175507ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 1756c01d5b30SHugh Dickins if (file) { 1757c01d5b30SHugh Dickins if (file->f_op->get_unmapped_area) 175807ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 1759c01d5b30SHugh Dickins } else if (flags & MAP_SHARED) { 1760c01d5b30SHugh Dickins /* 1761c01d5b30SHugh Dickins * mmap_region() will call shmem_zero_setup() to create a file, 1762c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge. 176345e55300SPeter Collingbourne * do_mmap() will clear pgoff, so match alignment. 1764c01d5b30SHugh Dickins */ 1765c01d5b30SHugh Dickins pgoff = 0; 1766c01d5b30SHugh Dickins get_area = shmem_get_unmapped_area; 1767c01d5b30SHugh Dickins } 1768c01d5b30SHugh Dickins 176907ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 177007ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 177107ab67c8SLinus Torvalds return addr; 177207ab67c8SLinus Torvalds 17731da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 17741da177e4SLinus Torvalds return -ENOMEM; 1775de1741a1SAlexander Kuleshov if (offset_in_page(addr)) 17761da177e4SLinus Torvalds return -EINVAL; 177706abdfb4SBenjamin Herrenschmidt 17789ac4ed4bSAl Viro error = security_mmap_addr(addr); 17799ac4ed4bSAl Viro return error ? error : addr; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 17831da177e4SLinus Torvalds 1784be8432e7SLiam R. Howlett /** 1785abdba2ddSLiam R. Howlett * find_vma_intersection() - Look up the first VMA which intersects the interval 1786abdba2ddSLiam R. Howlett * @mm: The process address space. 1787abdba2ddSLiam R. Howlett * @start_addr: The inclusive start user address. 1788abdba2ddSLiam R. Howlett * @end_addr: The exclusive end user address. 1789abdba2ddSLiam R. Howlett * 1790abdba2ddSLiam R. Howlett * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 1791abdba2ddSLiam R. Howlett * start_addr < end_addr. 1792abdba2ddSLiam R. Howlett */ 1793abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 1794abdba2ddSLiam R. Howlett unsigned long start_addr, 1795abdba2ddSLiam R. Howlett unsigned long end_addr) 1796abdba2ddSLiam R. Howlett { 1797abdba2ddSLiam R. Howlett unsigned long index = start_addr; 1798abdba2ddSLiam R. Howlett 1799abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 18007964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 1801abdba2ddSLiam R. Howlett } 1802abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 1803abdba2ddSLiam R. Howlett 1804abdba2ddSLiam R. Howlett /** 1805be8432e7SLiam R. Howlett * find_vma() - Find the VMA for a given address, or the next VMA. 1806be8432e7SLiam R. Howlett * @mm: The mm_struct to check 1807be8432e7SLiam R. Howlett * @addr: The address 1808be8432e7SLiam R. Howlett * 1809be8432e7SLiam R. Howlett * Returns: The VMA associated with addr, or the next VMA. 1810be8432e7SLiam R. Howlett * May return %NULL in the case of no VMA at addr or above. 1811be8432e7SLiam R. Howlett */ 18121da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 18131da177e4SLinus Torvalds { 1814be8432e7SLiam R. Howlett unsigned long index = addr; 18151da177e4SLinus Torvalds 18165b78ed24SLuigi Rizzo mmap_assert_locked(mm); 18177964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, ULONG_MAX); 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 18201da177e4SLinus Torvalds 18217fdbd37dSLiam R. Howlett /** 18227fdbd37dSLiam R. Howlett * find_vma_prev() - Find the VMA for a given address, or the next vma and 18237fdbd37dSLiam R. Howlett * set %pprev to the previous VMA, if any. 18247fdbd37dSLiam R. Howlett * @mm: The mm_struct to check 18257fdbd37dSLiam R. Howlett * @addr: The address 18267fdbd37dSLiam R. Howlett * @pprev: The pointer to set to the previous VMA 18277fdbd37dSLiam R. Howlett * 18287fdbd37dSLiam R. Howlett * Note that RCU lock is missing here since the external mmap_lock() is used 18297fdbd37dSLiam R. Howlett * instead. 18307fdbd37dSLiam R. Howlett * 18317fdbd37dSLiam R. Howlett * Returns: The VMA associated with @addr, or the next vma. 18327fdbd37dSLiam R. Howlett * May return %NULL in the case of no vma at addr or above. 18336bd4837dSKOSAKI Motohiro */ 18341da177e4SLinus Torvalds struct vm_area_struct * 18351da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 18361da177e4SLinus Torvalds struct vm_area_struct **pprev) 18371da177e4SLinus Torvalds { 18386bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 18397fdbd37dSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 18401da177e4SLinus Torvalds 18417fdbd37dSLiam R. Howlett vma = mas_walk(&mas); 18427fdbd37dSLiam R. Howlett *pprev = mas_prev(&mas, 0); 18437fdbd37dSLiam R. Howlett if (!vma) 18447fdbd37dSLiam R. Howlett vma = mas_next(&mas, ULONG_MAX); 18456bd4837dSKOSAKI Motohiro return vma; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds /* 18491da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 18501da177e4SLinus Torvalds * update accounting. This is shared with both the 18511da177e4SLinus Torvalds * grow-up and grow-down cases. 18521da177e4SLinus Torvalds */ 18531be7107fSHugh Dickins static int acct_stack_growth(struct vm_area_struct *vma, 18541be7107fSHugh Dickins unsigned long size, unsigned long grow) 18551da177e4SLinus Torvalds { 18561da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 18571be7107fSHugh Dickins unsigned long new_start; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds /* address space limit tests */ 186084638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, grow)) 18611da177e4SLinus Torvalds return -ENOMEM; 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds /* Stack limit test */ 186424c79d8eSKrzysztof Opasiak if (size > rlimit(RLIMIT_STACK)) 18651da177e4SLinus Torvalds return -ENOMEM; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* mlock limit tests */ 1868c5d8a364SMiaohe Lin if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) 18691da177e4SLinus Torvalds return -ENOMEM; 18701da177e4SLinus Torvalds 18710d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 18720d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 18730d59a01bSAdam Litke vma->vm_end - size; 18740d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 18750d59a01bSAdam Litke return -EFAULT; 18760d59a01bSAdam Litke 18771da177e4SLinus Torvalds /* 18781da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 18791da177e4SLinus Torvalds * update security statistics. 18801da177e4SLinus Torvalds */ 188105fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 18821da177e4SLinus Torvalds return -ENOMEM; 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds return 0; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 188746dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 18881da177e4SLinus Torvalds /* 188946dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 189046dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 18911da177e4SLinus Torvalds */ 189246dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 18931da177e4SLinus Torvalds { 189409357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 18951be7107fSHugh Dickins struct vm_area_struct *next; 18961be7107fSHugh Dickins unsigned long gap_addr; 189712352d3cSKonstantin Khlebnikov int error = 0; 1898d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 19011da177e4SLinus Torvalds return -EFAULT; 19021da177e4SLinus Torvalds 1903bd726c90SHelge Deller /* Guard against exceeding limits of the address space. */ 19041be7107fSHugh Dickins address &= PAGE_MASK; 190537511fb5SHelge Deller if (address >= (TASK_SIZE & PAGE_MASK)) 190612352d3cSKonstantin Khlebnikov return -ENOMEM; 1907bd726c90SHelge Deller address += PAGE_SIZE; 190812352d3cSKonstantin Khlebnikov 19091be7107fSHugh Dickins /* Enforce stack_guard_gap */ 19101be7107fSHugh Dickins gap_addr = address + stack_guard_gap; 1911bd726c90SHelge Deller 1912bd726c90SHelge Deller /* Guard against overflow */ 1913bd726c90SHelge Deller if (gap_addr < address || gap_addr > TASK_SIZE) 1914bd726c90SHelge Deller gap_addr = TASK_SIZE; 1915bd726c90SHelge Deller 1916763ecb03SLiam R. Howlett next = find_vma_intersection(mm, vma->vm_end, gap_addr); 1917763ecb03SLiam R. Howlett if (next && vma_is_accessible(next)) { 19181be7107fSHugh Dickins if (!(next->vm_flags & VM_GROWSUP)) 19191be7107fSHugh Dickins return -ENOMEM; 19201be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 19211be7107fSHugh Dickins } 19221be7107fSHugh Dickins 1923c5d5546eSVernon Yang if (mas_preallocate(&mas, GFP_KERNEL)) 19241da177e4SLinus Torvalds return -ENOMEM; 19251da177e4SLinus Torvalds 1926d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 1927d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 1928d4af56c5SLiam R. Howlett mas_destroy(&mas); 1929d4af56c5SLiam R. Howlett return -ENOMEM; 1930d4af56c5SLiam R. Howlett } 1931d4af56c5SLiam R. Howlett 19321da177e4SLinus Torvalds /* 19331da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 1934c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 19351da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 19361da177e4SLinus Torvalds */ 193712352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 19401da177e4SLinus Torvalds if (address > vma->vm_end) { 19411da177e4SLinus Torvalds unsigned long size, grow; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds size = address - vma->vm_start; 19441da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 19451da177e4SLinus Torvalds 194642c36f63SHugh Dickins error = -ENOMEM; 194742c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 19481da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 19493af9e859SEric B Munson if (!error) { 19504128997bSMichel Lespinasse /* 1951524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 1952524e00b3SLiam R. Howlett * we need to protect against concurrent vma 1953524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 1954524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 1955524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 1956524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 1957524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 19584128997bSMichel Lespinasse */ 195909357814SOleg Nesterov spin_lock(&mm->page_table_lock); 196087e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 196109357814SOleg Nesterov mm->locked_vm += grow; 196284638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 1963bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 19641da177e4SLinus Torvalds vma->vm_end = address; 1965d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 1966fbcc3104SLiam R. Howlett mas_set_range(&mas, vma->vm_start, address - 1); 1967fbcc3104SLiam R. Howlett mas_store_prealloc(&mas, vma); 1968bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 196909357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 19704128997bSMichel Lespinasse 19713af9e859SEric B Munson perf_event_mmap(vma); 19723af9e859SEric B Munson } 19731da177e4SLinus Torvalds } 197442c36f63SHugh Dickins } 197512352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 1976c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 1977d4af56c5SLiam R. Howlett mas_destroy(&mas); 19781da177e4SLinus Torvalds return error; 19791da177e4SLinus Torvalds } 198046dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 198146dea3d0SHugh Dickins 19821da177e4SLinus Torvalds /* 19831da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 19841da177e4SLinus Torvalds */ 1985524e00b3SLiam R. Howlett int expand_downwards(struct vm_area_struct *vma, unsigned long address) 19861da177e4SLinus Torvalds { 198709357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 1988763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); 19891be7107fSHugh Dickins struct vm_area_struct *prev; 19900a1d5299SJann Horn int error = 0; 19911da177e4SLinus Torvalds 19928869477aSEric Paris address &= PAGE_MASK; 19930a1d5299SJann Horn if (address < mmap_min_addr) 19940a1d5299SJann Horn return -EPERM; 19958869477aSEric Paris 19961be7107fSHugh Dickins /* Enforce stack_guard_gap */ 1997763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 19981be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 199932e4e6d5SOleg Nesterov if (prev && !(prev->vm_flags & VM_GROWSDOWN) && 20003122e80eSAnshuman Khandual vma_is_accessible(prev)) { 200132e4e6d5SOleg Nesterov if (address - prev->vm_end < stack_guard_gap) 200232e4e6d5SOleg Nesterov return -ENOMEM; 20031be7107fSHugh Dickins } 20041be7107fSHugh Dickins 2005c5d5546eSVernon Yang if (mas_preallocate(&mas, GFP_KERNEL)) 200612352d3cSKonstantin Khlebnikov return -ENOMEM; 20071da177e4SLinus Torvalds 2008d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2009d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2010d4af56c5SLiam R. Howlett mas_destroy(&mas); 2011d4af56c5SLiam R. Howlett return -ENOMEM; 2012d4af56c5SLiam R. Howlett } 2013d4af56c5SLiam R. Howlett 20141da177e4SLinus Torvalds /* 20151da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2016c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 20171da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 20181da177e4SLinus Torvalds */ 201912352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 20221da177e4SLinus Torvalds if (address < vma->vm_start) { 20231da177e4SLinus Torvalds unsigned long size, grow; 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds size = vma->vm_end - address; 20261da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 20271da177e4SLinus Torvalds 2028a626ca6aSLinus Torvalds error = -ENOMEM; 2029a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 20301da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 20311da177e4SLinus Torvalds if (!error) { 20324128997bSMichel Lespinasse /* 2033524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2034524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2035524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2036524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2037524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2038524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2039524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 20404128997bSMichel Lespinasse */ 204109357814SOleg Nesterov spin_lock(&mm->page_table_lock); 204287e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 204309357814SOleg Nesterov mm->locked_vm += grow; 204484638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2045bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 20461da177e4SLinus Torvalds vma->vm_start = address; 20471da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2048d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2049fbcc3104SLiam R. Howlett mas_set_range(&mas, address, vma->vm_end - 1); 2050fbcc3104SLiam R. Howlett mas_store_prealloc(&mas, vma); 2051bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 205209357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 20534128997bSMichel Lespinasse 20543af9e859SEric B Munson perf_event_mmap(vma); 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds } 2057a626ca6aSLinus Torvalds } 205812352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2059c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2060d4af56c5SLiam R. Howlett mas_destroy(&mas); 20611da177e4SLinus Torvalds return error; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641be7107fSHugh Dickins /* enforced gap between the expanding stack and other mappings. */ 20651be7107fSHugh Dickins unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; 20661be7107fSHugh Dickins 20671be7107fSHugh Dickins static int __init cmdline_parse_stack_guard_gap(char *p) 20681be7107fSHugh Dickins { 20691be7107fSHugh Dickins unsigned long val; 20701be7107fSHugh Dickins char *endptr; 20711be7107fSHugh Dickins 20721be7107fSHugh Dickins val = simple_strtoul(p, &endptr, 10); 20731be7107fSHugh Dickins if (!*endptr) 20741be7107fSHugh Dickins stack_guard_gap = val << PAGE_SHIFT; 20751be7107fSHugh Dickins 2076e6d09493SRandy Dunlap return 1; 20771be7107fSHugh Dickins } 20781be7107fSHugh Dickins __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); 20791be7107fSHugh Dickins 2080b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2081b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2082b6a2fea3SOllie Wild { 2083b6a2fea3SOllie Wild return expand_upwards(vma, address); 2084b6a2fea3SOllie Wild } 2085b6a2fea3SOllie Wild 2086b6a2fea3SOllie Wild struct vm_area_struct * 2087b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2088b6a2fea3SOllie Wild { 2089b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2090b6a2fea3SOllie Wild 2091b6a2fea3SOllie Wild addr &= PAGE_MASK; 2092b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2093b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2094b6a2fea3SOllie Wild return vma; 20954d45e75aSJann Horn if (!prev || expand_stack(prev, addr)) 2096b6a2fea3SOllie Wild return NULL; 2097cea10a19SMichel Lespinasse if (prev->vm_flags & VM_LOCKED) 2098fc05f566SKirill A. Shutemov populate_vma_page_range(prev, addr, prev->vm_end, NULL); 2099b6a2fea3SOllie Wild return prev; 2100b6a2fea3SOllie Wild } 2101b6a2fea3SOllie Wild #else 2102b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2103b6a2fea3SOllie Wild { 2104b6a2fea3SOllie Wild return expand_downwards(vma, address); 2105b6a2fea3SOllie Wild } 2106b6a2fea3SOllie Wild 21071da177e4SLinus Torvalds struct vm_area_struct * 21081da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 21091da177e4SLinus Torvalds { 21101da177e4SLinus Torvalds struct vm_area_struct *vma; 21111da177e4SLinus Torvalds unsigned long start; 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds addr &= PAGE_MASK; 21141da177e4SLinus Torvalds vma = find_vma(mm, addr); 21151da177e4SLinus Torvalds if (!vma) 21161da177e4SLinus Torvalds return NULL; 21171da177e4SLinus Torvalds if (vma->vm_start <= addr) 21181da177e4SLinus Torvalds return vma; 21191da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 21201da177e4SLinus Torvalds return NULL; 21211da177e4SLinus Torvalds start = vma->vm_start; 21221da177e4SLinus Torvalds if (expand_stack(vma, addr)) 21231da177e4SLinus Torvalds return NULL; 2124cea10a19SMichel Lespinasse if (vma->vm_flags & VM_LOCKED) 2125fc05f566SKirill A. Shutemov populate_vma_page_range(vma, addr, start, NULL); 21261da177e4SLinus Torvalds return vma; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds #endif 21291da177e4SLinus Torvalds 2130e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(find_extend_vma); 2131e1d6d01aSJesse Barnes 21322c0b3814SHugh Dickins /* 2133763ecb03SLiam R. Howlett * Ok - we have the memory areas we should free on a maple tree so release them, 2134763ecb03SLiam R. Howlett * and do the vma updates. 21351da177e4SLinus Torvalds * 21362c0b3814SHugh Dickins * Called with the mm semaphore held. 21371da177e4SLinus Torvalds */ 2138763ecb03SLiam R. Howlett static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) 21391da177e4SLinus Torvalds { 21404f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 2141763ecb03SLiam R. Howlett struct vm_area_struct *vma; 21424f74d2c8SLinus Torvalds 2143365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2144365e9c87SHugh Dickins update_hiwater_vm(mm); 2145763ecb03SLiam R. Howlett mas_for_each(mas, vma, ULONG_MAX) { 2146ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 21471da177e4SLinus Torvalds 21484f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 21494f74d2c8SLinus Torvalds nr_accounted += nrpages; 215084638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, -nrpages); 2151*0d2ebf9cSSuren Baghdasaryan remove_vma(vma, false); 2152763ecb03SLiam R. Howlett } 21534f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 21541da177e4SLinus Torvalds validate_mm(mm); 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds /* 21581da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 21591da177e4SLinus Torvalds * 2160f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 21611da177e4SLinus Torvalds */ 2162763ecb03SLiam R. Howlett static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, 2163e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2164763ecb03SLiam R. Howlett struct vm_area_struct *next, 216568f48381SSuren Baghdasaryan unsigned long start, unsigned long end, bool mm_wr_locked) 21661da177e4SLinus Torvalds { 2167d16dfc55SPeter Zijlstra struct mmu_gather tlb; 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds lru_add_drain(); 2170a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm); 2171365e9c87SHugh Dickins update_hiwater_rss(mm); 217268f48381SSuren Baghdasaryan unmap_vmas(&tlb, mt, vma, start, end, mm_wr_locked); 2173763ecb03SLiam R. Howlett free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 217498e51a22SSuren Baghdasaryan next ? next->vm_start : USER_PGTABLES_CEILING, 217598e51a22SSuren Baghdasaryan mm_wr_locked); 2176ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds /* 2180def5efe0SDavid Rientjes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 2181def5efe0SDavid Rientjes * has already been checked or doesn't make sense to fail. 21820fd5a9e2SLiam R. Howlett * VMA Iterator will point to the end VMA. 21831da177e4SLinus Torvalds */ 21849760ebffSLiam R. Howlett int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 21851da177e4SLinus Torvalds unsigned long addr, int new_below) 21861da177e4SLinus Torvalds { 2187b2b3b886SLiam R. Howlett struct vma_prepare vp; 21881da177e4SLinus Torvalds struct vm_area_struct *new; 2189e3975891SChen Gang int err; 21909760ebffSLiam R. Howlett 21919760ebffSLiam R. Howlett validate_mm_mt(vma->vm_mm); 21921da177e4SLinus Torvalds 2193b2b3b886SLiam R. Howlett WARN_ON(vma->vm_start >= addr); 2194b2b3b886SLiam R. Howlett WARN_ON(vma->vm_end <= addr); 2195b2b3b886SLiam R. Howlett 2196dd3b614fSDmitry Safonov if (vma->vm_ops && vma->vm_ops->may_split) { 2197dd3b614fSDmitry Safonov err = vma->vm_ops->may_split(vma, addr); 219831383c68SDan Williams if (err) 219931383c68SDan Williams return err; 220031383c68SDan Williams } 22011da177e4SLinus Torvalds 22023928d4f5SLinus Torvalds new = vm_area_dup(vma); 22031da177e4SLinus Torvalds if (!new) 2204e3975891SChen Gang return -ENOMEM; 22051da177e4SLinus Torvalds 2206b2b3b886SLiam R. Howlett err = -ENOMEM; 2207b2b3b886SLiam R. Howlett if (vma_iter_prealloc(vmi)) 2208b2b3b886SLiam R. Howlett goto out_free_vma; 2209b2b3b886SLiam R. Howlett 2210b2b3b886SLiam R. Howlett if (new_below) { 22111da177e4SLinus Torvalds new->vm_end = addr; 2212b2b3b886SLiam R. Howlett } else { 22131da177e4SLinus Torvalds new->vm_start = addr; 22141da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 2217ef0855d3SOleg Nesterov err = vma_dup_policy(vma, new); 2218ef0855d3SOleg Nesterov if (err) 2219b2b3b886SLiam R. Howlett goto out_free_vmi; 22201da177e4SLinus Torvalds 2221c4ea95d7SDaniel Forrest err = anon_vma_clone(new, vma); 2222c4ea95d7SDaniel Forrest if (err) 22235beb4930SRik van Riel goto out_free_mpol; 22245beb4930SRik van Riel 2225e9714acfSKonstantin Khlebnikov if (new->vm_file) 22261da177e4SLinus Torvalds get_file(new->vm_file); 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 22291da177e4SLinus Torvalds new->vm_ops->open(new); 22301da177e4SLinus Torvalds 2231b2b3b886SLiam R. Howlett init_vma_prep(&vp, vma); 2232b2b3b886SLiam R. Howlett vp.insert = new; 2233b2b3b886SLiam R. Howlett vma_prepare(&vp); 2234ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, vma->vm_start, addr, 0); 22351da177e4SLinus Torvalds 2236b2b3b886SLiam R. Howlett if (new_below) { 2237b2b3b886SLiam R. Howlett vma->vm_start = addr; 2238b2b3b886SLiam R. Howlett vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT; 2239b2b3b886SLiam R. Howlett } else { 2240b2b3b886SLiam R. Howlett vma->vm_end = addr; 22419760ebffSLiam R. Howlett } 22425beb4930SRik van Riel 2243b2b3b886SLiam R. Howlett /* vma_complete stores the new vma */ 2244b2b3b886SLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 2245b2b3b886SLiam R. Howlett 2246b2b3b886SLiam R. Howlett /* Success. */ 2247b2b3b886SLiam R. Howlett if (new_below) 2248b2b3b886SLiam R. Howlett vma_next(vmi); 2249b2b3b886SLiam R. Howlett validate_mm_mt(vma->vm_mm); 2250b2b3b886SLiam R. Howlett return 0; 2251b2b3b886SLiam R. Howlett 22525beb4930SRik van Riel out_free_mpol: 2253ef0855d3SOleg Nesterov mpol_put(vma_policy(new)); 2254b2b3b886SLiam R. Howlett out_free_vmi: 2255b2b3b886SLiam R. Howlett vma_iter_free(vmi); 22565beb4930SRik van Riel out_free_vma: 22573928d4f5SLinus Torvalds vm_area_free(new); 22589760ebffSLiam R. Howlett validate_mm_mt(vma->vm_mm); 22595beb4930SRik van Riel return err; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 2262659ace58SKOSAKI Motohiro /* 2263659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2264659ace58SKOSAKI Motohiro * either for the first part or the tail. 2265659ace58SKOSAKI Motohiro */ 22669760ebffSLiam R. Howlett int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 2267659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2268659ace58SKOSAKI Motohiro { 22699760ebffSLiam R. Howlett if (vma->vm_mm->map_count >= sysctl_max_map_count) 2270659ace58SKOSAKI Motohiro return -ENOMEM; 2271659ace58SKOSAKI Motohiro 22729760ebffSLiam R. Howlett return __split_vma(vmi, vma, addr, new_below); 2273f2ebfe43SLiam R. Howlett } 2274f2ebfe43SLiam R. Howlett 2275763ecb03SLiam R. Howlett static inline int munmap_sidetree(struct vm_area_struct *vma, 2276763ecb03SLiam R. Howlett struct ma_state *mas_detach) 22771da177e4SLinus Torvalds { 227873046fd0SSuren Baghdasaryan vma_start_write(vma); 2279763ecb03SLiam R. Howlett mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); 2280763ecb03SLiam R. Howlett if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) 2281763ecb03SLiam R. Howlett return -ENOMEM; 228211f9a21aSLiam R. Howlett 2283457f67beSSuren Baghdasaryan vma_mark_detached(vma, true); 2284763ecb03SLiam R. Howlett if (vma->vm_flags & VM_LOCKED) 2285763ecb03SLiam R. Howlett vma->vm_mm->locked_vm -= vma_pages(vma); 228611f9a21aSLiam R. Howlett 2287763ecb03SLiam R. Howlett return 0; 228811f9a21aSLiam R. Howlett } 228911f9a21aSLiam R. Howlett 229011f9a21aSLiam R. Howlett /* 2291183654ceSLiam R. Howlett * do_vmi_align_munmap() - munmap the aligned region from @start to @end. 2292183654ceSLiam R. Howlett * @vmi: The vma iterator 229311f9a21aSLiam R. Howlett * @vma: The starting vm_area_struct 229411f9a21aSLiam R. Howlett * @mm: The mm_struct 229511f9a21aSLiam R. Howlett * @start: The aligned start address to munmap. 229611f9a21aSLiam R. Howlett * @end: The aligned end address to munmap. 229711f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 22988651a137SLorenzo Stoakes * @downgrade: Set to true to attempt a write downgrade of the mmap_lock 229911f9a21aSLiam R. Howlett * 230011f9a21aSLiam R. Howlett * If @downgrade is true, check return code for potential release of the lock. 230111f9a21aSLiam R. Howlett */ 230211f9a21aSLiam R. Howlett static int 2303183654ceSLiam R. Howlett do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 230411f9a21aSLiam R. Howlett struct mm_struct *mm, unsigned long start, 230511f9a21aSLiam R. Howlett unsigned long end, struct list_head *uf, bool downgrade) 230611f9a21aSLiam R. Howlett { 2307763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next = NULL; 2308763ecb03SLiam R. Howlett struct maple_tree mt_detach; 2309763ecb03SLiam R. Howlett int count = 0; 2310d4af56c5SLiam R. Howlett int error = -ENOMEM; 2311763ecb03SLiam R. Howlett MA_STATE(mas_detach, &mt_detach, 0, 0); 2312763ecb03SLiam R. Howlett mt_init_flags(&mt_detach, MT_FLAGS_LOCK_EXTERN); 2313763ecb03SLiam R. Howlett mt_set_external_lock(&mt_detach, &mm->mmap_lock); 2314524e00b3SLiam R. Howlett 23151da177e4SLinus Torvalds /* 23161da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 23171da177e4SLinus Torvalds * 23181da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 23191da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 23201da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 23211da177e4SLinus Torvalds */ 2322763ecb03SLiam R. Howlett 2323763ecb03SLiam R. Howlett /* Does it split the first one? */ 2324146425a3SHugh Dickins if (start > vma->vm_start) { 2325659ace58SKOSAKI Motohiro 2326659ace58SKOSAKI Motohiro /* 2327659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2328659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2329659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2330659ace58SKOSAKI Motohiro */ 2331659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2332d4af56c5SLiam R. Howlett goto map_count_exceeded; 2333659ace58SKOSAKI Motohiro 23349760ebffSLiam R. Howlett error = __split_vma(vmi, vma, start, 0); 23351da177e4SLinus Torvalds if (error) 2336763ecb03SLiam R. Howlett goto start_split_failed; 233711f9a21aSLiam R. Howlett 23380fd5a9e2SLiam R. Howlett vma = vma_iter_load(vmi); 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 2341183654ceSLiam R. Howlett prev = vma_prev(vmi); 2342763ecb03SLiam R. Howlett if (unlikely((!prev))) 2343183654ceSLiam R. Howlett vma_iter_set(vmi, start); 234411f9a21aSLiam R. Howlett 2345763ecb03SLiam R. Howlett /* 2346763ecb03SLiam R. Howlett * Detach a range of VMAs from the mm. Using next as a temp variable as 2347763ecb03SLiam R. Howlett * it is always overwritten. 2348763ecb03SLiam R. Howlett */ 2349183654ceSLiam R. Howlett for_each_vma_range(*vmi, next, end) { 2350763ecb03SLiam R. Howlett /* Does it split the end? */ 2351763ecb03SLiam R. Howlett if (next->vm_end > end) { 23526b73cff2SLiam R. Howlett error = __split_vma(vmi, next, end, 0); 23531da177e4SLinus Torvalds if (error) 2354763ecb03SLiam R. Howlett goto end_split_failed; 235511f9a21aSLiam R. Howlett } 2356763ecb03SLiam R. Howlett error = munmap_sidetree(next, &mas_detach); 2357763ecb03SLiam R. Howlett if (error) 2358763ecb03SLiam R. Howlett goto munmap_sidetree_failed; 2359763ecb03SLiam R. Howlett 2360763ecb03SLiam R. Howlett count++; 2361763ecb03SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 2362763ecb03SLiam R. Howlett BUG_ON(next->vm_start < start); 2363763ecb03SLiam R. Howlett BUG_ON(next->vm_start > end); 2364763ecb03SLiam R. Howlett #endif 2365763ecb03SLiam R. Howlett } 2366763ecb03SLiam R. Howlett 2367183654ceSLiam R. Howlett next = vma_next(vmi); 23682376dd7cSAndrea Arcangeli if (unlikely(uf)) { 23692376dd7cSAndrea Arcangeli /* 23702376dd7cSAndrea Arcangeli * If userfaultfd_unmap_prep returns an error the vmas 2371f0953a1bSIngo Molnar * will remain split, but userland will get a 23722376dd7cSAndrea Arcangeli * highly unexpected error anyway. This is no 23732376dd7cSAndrea Arcangeli * different than the case where the first of the two 23742376dd7cSAndrea Arcangeli * __split_vma fails, but we don't undo the first 23752376dd7cSAndrea Arcangeli * split, despite we could. This is unlikely enough 23762376dd7cSAndrea Arcangeli * failure that it's not worth optimizing it for. 23772376dd7cSAndrea Arcangeli */ 237869dbe6daSLiam R. Howlett error = userfaultfd_unmap_prep(mm, start, end, uf); 237911f9a21aSLiam R. Howlett 23802376dd7cSAndrea Arcangeli if (error) 2381d4af56c5SLiam R. Howlett goto userfaultfd_error; 23822376dd7cSAndrea Arcangeli } 23832376dd7cSAndrea Arcangeli 2384763ecb03SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 2385763ecb03SLiam R. Howlett /* Make sure no VMAs are about to be lost. */ 2386763ecb03SLiam R. Howlett { 2387763ecb03SLiam R. Howlett MA_STATE(test, &mt_detach, start, end - 1); 2388763ecb03SLiam R. Howlett struct vm_area_struct *vma_mas, *vma_test; 2389763ecb03SLiam R. Howlett int test_count = 0; 2390763ecb03SLiam R. Howlett 2391183654ceSLiam R. Howlett vma_iter_set(vmi, start); 2392763ecb03SLiam R. Howlett rcu_read_lock(); 2393763ecb03SLiam R. Howlett vma_test = mas_find(&test, end - 1); 2394183654ceSLiam R. Howlett for_each_vma_range(*vmi, vma_mas, end) { 2395763ecb03SLiam R. Howlett BUG_ON(vma_mas != vma_test); 2396763ecb03SLiam R. Howlett test_count++; 2397763ecb03SLiam R. Howlett vma_test = mas_next(&test, end - 1); 2398763ecb03SLiam R. Howlett } 2399763ecb03SLiam R. Howlett rcu_read_unlock(); 2400763ecb03SLiam R. Howlett BUG_ON(count != test_count); 2401763ecb03SLiam R. Howlett } 2402763ecb03SLiam R. Howlett #endif 24030378c0a0SLiam R. Howlett /* Point of no return */ 2404183654ceSLiam R. Howlett vma_iter_set(vmi, start); 2405183654ceSLiam R. Howlett if (vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL)) 24060378c0a0SLiam R. Howlett return -ENOMEM; 24070378c0a0SLiam R. Howlett 2408763ecb03SLiam R. Howlett mm->map_count -= count; 240911f9a21aSLiam R. Howlett /* 241011f9a21aSLiam R. Howlett * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or 241111f9a21aSLiam R. Howlett * VM_GROWSUP VMA. Such VMAs can change their size under 241211f9a21aSLiam R. Howlett * down_read(mmap_lock) and collide with the VMA we are about to unmap. 241311f9a21aSLiam R. Howlett */ 241411f9a21aSLiam R. Howlett if (downgrade) { 2415763ecb03SLiam R. Howlett if (next && (next->vm_flags & VM_GROWSDOWN)) 241611f9a21aSLiam R. Howlett downgrade = false; 241711f9a21aSLiam R. Howlett else if (prev && (prev->vm_flags & VM_GROWSUP)) 241811f9a21aSLiam R. Howlett downgrade = false; 241911f9a21aSLiam R. Howlett else 2420d8ed45c5SMichel Lespinasse mmap_write_downgrade(mm); 242111f9a21aSLiam R. Howlett } 2422dd2283f2SYang Shi 242368f48381SSuren Baghdasaryan /* 242468f48381SSuren Baghdasaryan * We can free page tables without write-locking mmap_lock because VMAs 242568f48381SSuren Baghdasaryan * were isolated before we downgraded mmap_lock. 242668f48381SSuren Baghdasaryan */ 242768f48381SSuren Baghdasaryan unmap_region(mm, &mt_detach, vma, prev, next, start, end, !downgrade); 2428763ecb03SLiam R. Howlett /* Statistics and freeing VMAs */ 2429763ecb03SLiam R. Howlett mas_set(&mas_detach, start); 2430763ecb03SLiam R. Howlett remove_mt(mm, &mas_detach); 2431763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 24321da177e4SLinus Torvalds 2433524e00b3SLiam R. Howlett 2434524e00b3SLiam R. Howlett validate_mm(mm); 2435dd2283f2SYang Shi return downgrade ? 1 : 0; 2436d4af56c5SLiam R. Howlett 2437d4af56c5SLiam R. Howlett userfaultfd_error: 2438763ecb03SLiam R. Howlett munmap_sidetree_failed: 2439763ecb03SLiam R. Howlett end_split_failed: 2440763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 2441763ecb03SLiam R. Howlett start_split_failed: 2442763ecb03SLiam R. Howlett map_count_exceeded: 2443d4af56c5SLiam R. Howlett return error; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 244611f9a21aSLiam R. Howlett /* 2447183654ceSLiam R. Howlett * do_vmi_munmap() - munmap a given range. 2448183654ceSLiam R. Howlett * @vmi: The vma iterator 244911f9a21aSLiam R. Howlett * @mm: The mm_struct 245011f9a21aSLiam R. Howlett * @start: The start address to munmap 245111f9a21aSLiam R. Howlett * @len: The length of the range to munmap 245211f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 245311f9a21aSLiam R. Howlett * @downgrade: set to true if the user wants to attempt to write_downgrade the 24548651a137SLorenzo Stoakes * mmap_lock 245511f9a21aSLiam R. Howlett * 245611f9a21aSLiam R. Howlett * This function takes a @mas that is either pointing to the previous VMA or set 245711f9a21aSLiam R. Howlett * to MA_START and sets it up to remove the mapping(s). The @len will be 245811f9a21aSLiam R. Howlett * aligned and any arch_unmap work will be preformed. 245911f9a21aSLiam R. Howlett * 246011f9a21aSLiam R. Howlett * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. 246111f9a21aSLiam R. Howlett */ 2462183654ceSLiam R. Howlett int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 246311f9a21aSLiam R. Howlett unsigned long start, size_t len, struct list_head *uf, 246411f9a21aSLiam R. Howlett bool downgrade) 246511f9a21aSLiam R. Howlett { 246611f9a21aSLiam R. Howlett unsigned long end; 246711f9a21aSLiam R. Howlett struct vm_area_struct *vma; 246811f9a21aSLiam R. Howlett 246911f9a21aSLiam R. Howlett if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 247011f9a21aSLiam R. Howlett return -EINVAL; 247111f9a21aSLiam R. Howlett 247211f9a21aSLiam R. Howlett end = start + PAGE_ALIGN(len); 247311f9a21aSLiam R. Howlett if (end == start) 247411f9a21aSLiam R. Howlett return -EINVAL; 247511f9a21aSLiam R. Howlett 247611f9a21aSLiam R. Howlett /* arch_unmap() might do unmaps itself. */ 247711f9a21aSLiam R. Howlett arch_unmap(mm, start, end); 247811f9a21aSLiam R. Howlett 247911f9a21aSLiam R. Howlett /* Find the first overlapping VMA */ 2480183654ceSLiam R. Howlett vma = vma_find(vmi, end); 248111f9a21aSLiam R. Howlett if (!vma) 248211f9a21aSLiam R. Howlett return 0; 248311f9a21aSLiam R. Howlett 2484183654ceSLiam R. Howlett return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); 248511f9a21aSLiam R. Howlett } 248611f9a21aSLiam R. Howlett 248711f9a21aSLiam R. Howlett /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. 248811f9a21aSLiam R. Howlett * @mm: The mm_struct 248911f9a21aSLiam R. Howlett * @start: The start address to munmap 249011f9a21aSLiam R. Howlett * @len: The length to be munmapped. 249111f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 249211f9a21aSLiam R. Howlett */ 2493dd2283f2SYang Shi int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2494dd2283f2SYang Shi struct list_head *uf) 2495dd2283f2SYang Shi { 2496183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 249711f9a21aSLiam R. Howlett 2498183654ceSLiam R. Howlett return do_vmi_munmap(&vmi, mm, start, len, uf, false); 2499dd2283f2SYang Shi } 2500dd2283f2SYang Shi 2501e99668a5SLiam R. Howlett unsigned long mmap_region(struct file *file, unsigned long addr, 2502e99668a5SLiam R. Howlett unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, 2503e99668a5SLiam R. Howlett struct list_head *uf) 2504e99668a5SLiam R. Howlett { 2505e99668a5SLiam R. Howlett struct mm_struct *mm = current->mm; 2506e99668a5SLiam R. Howlett struct vm_area_struct *vma = NULL; 2507e99668a5SLiam R. Howlett struct vm_area_struct *next, *prev, *merge; 2508e99668a5SLiam R. Howlett pgoff_t pglen = len >> PAGE_SHIFT; 2509e99668a5SLiam R. Howlett unsigned long charged = 0; 2510e99668a5SLiam R. Howlett unsigned long end = addr + len; 2511e99668a5SLiam R. Howlett unsigned long merge_start = addr, merge_end = end; 2512e99668a5SLiam R. Howlett pgoff_t vm_pgoff; 2513e99668a5SLiam R. Howlett int error; 2514183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 2515e99668a5SLiam R. Howlett 2516e99668a5SLiam R. Howlett /* Check against address space limit. */ 2517e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { 2518e99668a5SLiam R. Howlett unsigned long nr_pages; 2519e99668a5SLiam R. Howlett 2520e99668a5SLiam R. Howlett /* 2521e99668a5SLiam R. Howlett * MAP_FIXED may remove pages of mappings that intersects with 2522e99668a5SLiam R. Howlett * requested mapping. Account for the pages it would unmap. 2523e99668a5SLiam R. Howlett */ 2524e99668a5SLiam R. Howlett nr_pages = count_vma_pages_range(mm, addr, end); 2525e99668a5SLiam R. Howlett 2526e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, 2527e99668a5SLiam R. Howlett (len >> PAGE_SHIFT) - nr_pages)) 2528e99668a5SLiam R. Howlett return -ENOMEM; 2529e99668a5SLiam R. Howlett } 2530e99668a5SLiam R. Howlett 2531e99668a5SLiam R. Howlett /* Unmap any existing mapping in the area */ 2532183654ceSLiam R. Howlett if (do_vmi_munmap(&vmi, mm, addr, len, uf, false)) 2533e99668a5SLiam R. Howlett return -ENOMEM; 2534e99668a5SLiam R. Howlett 2535e99668a5SLiam R. Howlett /* 2536e99668a5SLiam R. Howlett * Private writable mapping: check memory availability 2537e99668a5SLiam R. Howlett */ 2538e99668a5SLiam R. Howlett if (accountable_mapping(file, vm_flags)) { 2539e99668a5SLiam R. Howlett charged = len >> PAGE_SHIFT; 2540e99668a5SLiam R. Howlett if (security_vm_enough_memory_mm(mm, charged)) 2541e99668a5SLiam R. Howlett return -ENOMEM; 2542e99668a5SLiam R. Howlett vm_flags |= VM_ACCOUNT; 2543e99668a5SLiam R. Howlett } 2544e99668a5SLiam R. Howlett 2545183654ceSLiam R. Howlett next = vma_next(&vmi); 2546183654ceSLiam R. Howlett prev = vma_prev(&vmi); 2547e99668a5SLiam R. Howlett if (vm_flags & VM_SPECIAL) 2548e99668a5SLiam R. Howlett goto cannot_expand; 2549e99668a5SLiam R. Howlett 2550e99668a5SLiam R. Howlett /* Attempt to expand an old mapping */ 2551e99668a5SLiam R. Howlett /* Check next */ 2552e99668a5SLiam R. Howlett if (next && next->vm_start == end && !vma_policy(next) && 2553e99668a5SLiam R. Howlett can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, 2554e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL)) { 2555e99668a5SLiam R. Howlett merge_end = next->vm_end; 2556e99668a5SLiam R. Howlett vma = next; 2557e99668a5SLiam R. Howlett vm_pgoff = next->vm_pgoff - pglen; 2558e99668a5SLiam R. Howlett } 2559e99668a5SLiam R. Howlett 2560e99668a5SLiam R. Howlett /* Check prev */ 2561e99668a5SLiam R. Howlett if (prev && prev->vm_end == addr && !vma_policy(prev) && 2562e99668a5SLiam R. Howlett (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, 2563e99668a5SLiam R. Howlett pgoff, vma->vm_userfaultfd_ctx, NULL) : 2564e99668a5SLiam R. Howlett can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, 2565e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL))) { 2566e99668a5SLiam R. Howlett merge_start = prev->vm_start; 2567e99668a5SLiam R. Howlett vma = prev; 2568e99668a5SLiam R. Howlett vm_pgoff = prev->vm_pgoff; 2569e99668a5SLiam R. Howlett } 2570e99668a5SLiam R. Howlett 2571e99668a5SLiam R. Howlett 2572e99668a5SLiam R. Howlett /* Actually expand, if possible */ 2573e99668a5SLiam R. Howlett if (vma && 25743c441ab7SLiam R. Howlett !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { 2575e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vm_flags); 2576e99668a5SLiam R. Howlett goto expanded; 2577e99668a5SLiam R. Howlett } 2578e99668a5SLiam R. Howlett 2579e99668a5SLiam R. Howlett cannot_expand: 2580e99668a5SLiam R. Howlett /* 2581e99668a5SLiam R. Howlett * Determine the object being mapped and call the appropriate 2582e99668a5SLiam R. Howlett * specific mapper. the address has already been validated, but 2583e99668a5SLiam R. Howlett * not unmapped, but the maps are removed from the list. 2584e99668a5SLiam R. Howlett */ 2585e99668a5SLiam R. Howlett vma = vm_area_alloc(mm); 2586e99668a5SLiam R. Howlett if (!vma) { 2587e99668a5SLiam R. Howlett error = -ENOMEM; 2588e99668a5SLiam R. Howlett goto unacct_error; 2589e99668a5SLiam R. Howlett } 2590e99668a5SLiam R. Howlett 25910fd5a9e2SLiam R. Howlett vma_iter_set(&vmi, addr); 2592e99668a5SLiam R. Howlett vma->vm_start = addr; 2593e99668a5SLiam R. Howlett vma->vm_end = end; 25941c71222eSSuren Baghdasaryan vm_flags_init(vma, vm_flags); 2595e99668a5SLiam R. Howlett vma->vm_page_prot = vm_get_page_prot(vm_flags); 2596e99668a5SLiam R. Howlett vma->vm_pgoff = pgoff; 2597e99668a5SLiam R. Howlett 2598e99668a5SLiam R. Howlett if (file) { 2599e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) { 2600e99668a5SLiam R. Howlett error = mapping_map_writable(file->f_mapping); 2601e99668a5SLiam R. Howlett if (error) 2602e99668a5SLiam R. Howlett goto free_vma; 2603e99668a5SLiam R. Howlett } 2604e99668a5SLiam R. Howlett 2605e99668a5SLiam R. Howlett vma->vm_file = get_file(file); 2606e99668a5SLiam R. Howlett error = call_mmap(file, vma); 2607e99668a5SLiam R. Howlett if (error) 2608e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2609e99668a5SLiam R. Howlett 2610a57b7051SLiam Howlett /* 2611a57b7051SLiam Howlett * Expansion is handled above, merging is handled below. 2612a57b7051SLiam Howlett * Drivers should not alter the address of the VMA. 2613e99668a5SLiam R. Howlett */ 2614a57b7051SLiam Howlett error = -EINVAL; 2615cc8d1b09SLiam R. Howlett if (WARN_ON((addr != vma->vm_start))) 2616a57b7051SLiam Howlett goto close_and_free_vma; 2617e99668a5SLiam R. Howlett 2618cc8d1b09SLiam R. Howlett vma_iter_set(&vmi, addr); 2619e99668a5SLiam R. Howlett /* 2620e99668a5SLiam R. Howlett * If vm_flags changed after call_mmap(), we should try merge 2621e99668a5SLiam R. Howlett * vma again as we may succeed this time. 2622e99668a5SLiam R. Howlett */ 2623e99668a5SLiam R. Howlett if (unlikely(vm_flags != vma->vm_flags && prev)) { 26249760ebffSLiam R. Howlett merge = vma_merge(&vmi, mm, prev, vma->vm_start, 26259760ebffSLiam R. Howlett vma->vm_end, vma->vm_flags, NULL, 26269760ebffSLiam R. Howlett vma->vm_file, vma->vm_pgoff, NULL, 26279760ebffSLiam R. Howlett NULL_VM_UFFD_CTX, NULL); 2628e99668a5SLiam R. Howlett if (merge) { 2629e99668a5SLiam R. Howlett /* 2630e99668a5SLiam R. Howlett * ->mmap() can change vma->vm_file and fput 2631e99668a5SLiam R. Howlett * the original file. So fput the vma->vm_file 2632e99668a5SLiam R. Howlett * here or we would add an extra fput for file 2633e99668a5SLiam R. Howlett * and cause general protection fault 2634e99668a5SLiam R. Howlett * ultimately. 2635e99668a5SLiam R. Howlett */ 2636e99668a5SLiam R. Howlett fput(vma->vm_file); 2637e99668a5SLiam R. Howlett vm_area_free(vma); 2638e99668a5SLiam R. Howlett vma = merge; 2639e99668a5SLiam R. Howlett /* Update vm_flags to pick up the change. */ 2640e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2641e99668a5SLiam R. Howlett goto unmap_writable; 2642e99668a5SLiam R. Howlett } 2643e99668a5SLiam R. Howlett } 2644e99668a5SLiam R. Howlett 2645e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2646e99668a5SLiam R. Howlett } else if (vm_flags & VM_SHARED) { 2647e99668a5SLiam R. Howlett error = shmem_zero_setup(vma); 2648e99668a5SLiam R. Howlett if (error) 2649e99668a5SLiam R. Howlett goto free_vma; 2650e99668a5SLiam R. Howlett } else { 2651e99668a5SLiam R. Howlett vma_set_anonymous(vma); 2652e99668a5SLiam R. Howlett } 2653e99668a5SLiam R. Howlett 2654b507808eSJoey Gouly if (map_deny_write_exec(vma, vma->vm_flags)) { 2655b507808eSJoey Gouly error = -EACCES; 2656b507808eSJoey Gouly goto close_and_free_vma; 2657b507808eSJoey Gouly } 2658b507808eSJoey Gouly 2659e99668a5SLiam R. Howlett /* Allow architectures to sanity-check the vm_flags */ 2660e99668a5SLiam R. Howlett error = -EINVAL; 2661cc8d1b09SLiam R. Howlett if (!arch_validate_flags(vma->vm_flags)) 2662deb0f656SCarlos Llamas goto close_and_free_vma; 2663e99668a5SLiam R. Howlett 2664e99668a5SLiam R. Howlett error = -ENOMEM; 2665cc8d1b09SLiam R. Howlett if (vma_iter_prealloc(&vmi)) 26665789151eSMike Kravetz goto close_and_free_vma; 2667e99668a5SLiam R. Howlett 2668e99668a5SLiam R. Howlett if (vma->vm_file) 2669e99668a5SLiam R. Howlett i_mmap_lock_write(vma->vm_file->f_mapping); 2670e99668a5SLiam R. Howlett 2671183654ceSLiam R. Howlett vma_iter_store(&vmi, vma); 2672e99668a5SLiam R. Howlett mm->map_count++; 2673e99668a5SLiam R. Howlett if (vma->vm_file) { 2674e99668a5SLiam R. Howlett if (vma->vm_flags & VM_SHARED) 2675e99668a5SLiam R. Howlett mapping_allow_writable(vma->vm_file->f_mapping); 2676e99668a5SLiam R. Howlett 2677e99668a5SLiam R. Howlett flush_dcache_mmap_lock(vma->vm_file->f_mapping); 2678e99668a5SLiam R. Howlett vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); 2679e99668a5SLiam R. Howlett flush_dcache_mmap_unlock(vma->vm_file->f_mapping); 2680e99668a5SLiam R. Howlett i_mmap_unlock_write(vma->vm_file->f_mapping); 2681e99668a5SLiam R. Howlett } 2682e99668a5SLiam R. Howlett 2683e99668a5SLiam R. Howlett /* 2684e99668a5SLiam R. Howlett * vma_merge() calls khugepaged_enter_vma() either, the below 2685e99668a5SLiam R. Howlett * call covers the non-merge case. 2686e99668a5SLiam R. Howlett */ 2687e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vma->vm_flags); 2688e99668a5SLiam R. Howlett 2689e99668a5SLiam R. Howlett /* Once vma denies write, undo our temporary denial count */ 2690e99668a5SLiam R. Howlett unmap_writable: 2691e99668a5SLiam R. Howlett if (file && vm_flags & VM_SHARED) 2692e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2693e99668a5SLiam R. Howlett file = vma->vm_file; 2694e99668a5SLiam R. Howlett expanded: 2695e99668a5SLiam R. Howlett perf_event_mmap(vma); 2696e99668a5SLiam R. Howlett 2697e99668a5SLiam R. Howlett vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); 2698e99668a5SLiam R. Howlett if (vm_flags & VM_LOCKED) { 2699e99668a5SLiam R. Howlett if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || 2700e99668a5SLiam R. Howlett is_vm_hugetlb_page(vma) || 2701e99668a5SLiam R. Howlett vma == get_gate_vma(current->mm)) 2702e430a95aSSuren Baghdasaryan vm_flags_clear(vma, VM_LOCKED_MASK); 2703e99668a5SLiam R. Howlett else 2704e99668a5SLiam R. Howlett mm->locked_vm += (len >> PAGE_SHIFT); 2705e99668a5SLiam R. Howlett } 2706e99668a5SLiam R. Howlett 2707e99668a5SLiam R. Howlett if (file) 2708e99668a5SLiam R. Howlett uprobe_mmap(vma); 2709e99668a5SLiam R. Howlett 2710e99668a5SLiam R. Howlett /* 2711e99668a5SLiam R. Howlett * New (or expanded) vma always get soft dirty status. 2712e99668a5SLiam R. Howlett * Otherwise user-space soft-dirty page tracker won't 2713e99668a5SLiam R. Howlett * be able to distinguish situation when vma area unmapped, 2714e99668a5SLiam R. Howlett * then new mapped in-place (which must be aimed as 2715e99668a5SLiam R. Howlett * a completely new data area). 2716e99668a5SLiam R. Howlett */ 27171c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_SOFTDIRTY); 2718e99668a5SLiam R. Howlett 2719e99668a5SLiam R. Howlett vma_set_page_prot(vma); 2720e99668a5SLiam R. Howlett 2721e99668a5SLiam R. Howlett validate_mm(mm); 2722e99668a5SLiam R. Howlett return addr; 2723e99668a5SLiam R. Howlett 2724deb0f656SCarlos Llamas close_and_free_vma: 2725cc8d1b09SLiam R. Howlett if (file && vma->vm_ops && vma->vm_ops->close) 2726deb0f656SCarlos Llamas vma->vm_ops->close(vma); 2727cc8d1b09SLiam R. Howlett 2728cc8d1b09SLiam R. Howlett if (file || vma->vm_file) { 2729e99668a5SLiam R. Howlett unmap_and_free_vma: 2730e99668a5SLiam R. Howlett fput(vma->vm_file); 2731e99668a5SLiam R. Howlett vma->vm_file = NULL; 2732e99668a5SLiam R. Howlett 2733e99668a5SLiam R. Howlett /* Undo any partial mapping done by a device driver. */ 2734cc8d1b09SLiam R. Howlett unmap_region(mm, &mm->mm_mt, vma, prev, next, vma->vm_start, 273568f48381SSuren Baghdasaryan vma->vm_end, true); 2736cc8d1b09SLiam R. Howlett } 2737cc674ab3SLi Zetao if (file && (vm_flags & VM_SHARED)) 2738e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2739e99668a5SLiam R. Howlett free_vma: 2740e99668a5SLiam R. Howlett vm_area_free(vma); 2741e99668a5SLiam R. Howlett unacct_error: 2742e99668a5SLiam R. Howlett if (charged) 2743e99668a5SLiam R. Howlett vm_unacct_memory(charged); 2744e99668a5SLiam R. Howlett validate_mm(mm); 2745e99668a5SLiam R. Howlett return error; 2746e99668a5SLiam R. Howlett } 2747e99668a5SLiam R. Howlett 2748dd2283f2SYang Shi static int __vm_munmap(unsigned long start, size_t len, bool downgrade) 2749a46ef99dSLinus Torvalds { 2750a46ef99dSLinus Torvalds int ret; 2751bfce281cSAl Viro struct mm_struct *mm = current->mm; 2752897ab3e0SMike Rapoport LIST_HEAD(uf); 2753183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 2754a46ef99dSLinus Torvalds 2755d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2756ae798783SMichal Hocko return -EINTR; 2757ae798783SMichal Hocko 2758183654ceSLiam R. Howlett ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade); 2759dd2283f2SYang Shi /* 2760c1e8d7c6SMichel Lespinasse * Returning 1 indicates mmap_lock is downgraded. 2761dd2283f2SYang Shi * But 1 is not legal return value of vm_munmap() and munmap(), reset 2762dd2283f2SYang Shi * it to 0 before return. 2763dd2283f2SYang Shi */ 2764dd2283f2SYang Shi if (ret == 1) { 2765d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2766dd2283f2SYang Shi ret = 0; 2767dd2283f2SYang Shi } else 2768d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2769dd2283f2SYang Shi 2770897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 2771a46ef99dSLinus Torvalds return ret; 2772a46ef99dSLinus Torvalds } 2773dd2283f2SYang Shi 2774dd2283f2SYang Shi int vm_munmap(unsigned long start, size_t len) 2775dd2283f2SYang Shi { 2776dd2283f2SYang Shi return __vm_munmap(start, len, false); 2777dd2283f2SYang Shi } 2778a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2779a46ef99dSLinus Torvalds 27806a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 27811da177e4SLinus Torvalds { 2782ce18d171SCatalin Marinas addr = untagged_addr(addr); 2783dd2283f2SYang Shi return __vm_munmap(addr, len, true); 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds 2786c8d78c18SKirill A. Shutemov 2787c8d78c18SKirill A. Shutemov /* 2788c8d78c18SKirill A. Shutemov * Emulation of deprecated remap_file_pages() syscall. 2789c8d78c18SKirill A. Shutemov */ 2790c8d78c18SKirill A. Shutemov SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, 2791c8d78c18SKirill A. Shutemov unsigned long, prot, unsigned long, pgoff, unsigned long, flags) 2792c8d78c18SKirill A. Shutemov { 2793c8d78c18SKirill A. Shutemov 2794c8d78c18SKirill A. Shutemov struct mm_struct *mm = current->mm; 2795c8d78c18SKirill A. Shutemov struct vm_area_struct *vma; 2796c8d78c18SKirill A. Shutemov unsigned long populate = 0; 2797c8d78c18SKirill A. Shutemov unsigned long ret = -EINVAL; 2798c8d78c18SKirill A. Shutemov struct file *file; 2799c8d78c18SKirill A. Shutemov 2800ee65728eSMike Rapoport pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", 2801c8d78c18SKirill A. Shutemov current->comm, current->pid); 2802c8d78c18SKirill A. Shutemov 2803c8d78c18SKirill A. Shutemov if (prot) 2804c8d78c18SKirill A. Shutemov return ret; 2805c8d78c18SKirill A. Shutemov start = start & PAGE_MASK; 2806c8d78c18SKirill A. Shutemov size = size & PAGE_MASK; 2807c8d78c18SKirill A. Shutemov 2808c8d78c18SKirill A. Shutemov if (start + size <= start) 2809c8d78c18SKirill A. Shutemov return ret; 2810c8d78c18SKirill A. Shutemov 2811c8d78c18SKirill A. Shutemov /* Does pgoff wrap? */ 2812c8d78c18SKirill A. Shutemov if (pgoff + (size >> PAGE_SHIFT) < pgoff) 2813c8d78c18SKirill A. Shutemov return ret; 2814c8d78c18SKirill A. Shutemov 2815d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2816dc0ef0dfSMichal Hocko return -EINTR; 2817dc0ef0dfSMichal Hocko 28189b593cb2SLiam R. Howlett vma = vma_lookup(mm, start); 2819c8d78c18SKirill A. Shutemov 2820c8d78c18SKirill A. Shutemov if (!vma || !(vma->vm_flags & VM_SHARED)) 2821c8d78c18SKirill A. Shutemov goto out; 2822c8d78c18SKirill A. Shutemov 282348f7df32SKirill A. Shutemov if (start + size > vma->vm_end) { 2824763ecb03SLiam R. Howlett VMA_ITERATOR(vmi, mm, vma->vm_end); 2825763ecb03SLiam R. Howlett struct vm_area_struct *next, *prev = vma; 282648f7df32SKirill A. Shutemov 2827763ecb03SLiam R. Howlett for_each_vma_range(vmi, next, start + size) { 282848f7df32SKirill A. Shutemov /* hole between vmas ? */ 2829763ecb03SLiam R. Howlett if (next->vm_start != prev->vm_end) 283048f7df32SKirill A. Shutemov goto out; 283148f7df32SKirill A. Shutemov 283248f7df32SKirill A. Shutemov if (next->vm_file != vma->vm_file) 283348f7df32SKirill A. Shutemov goto out; 283448f7df32SKirill A. Shutemov 283548f7df32SKirill A. Shutemov if (next->vm_flags != vma->vm_flags) 283648f7df32SKirill A. Shutemov goto out; 283748f7df32SKirill A. Shutemov 28381db43d3fSLiam Howlett if (start + size <= next->vm_end) 28391db43d3fSLiam Howlett break; 28401db43d3fSLiam Howlett 2841763ecb03SLiam R. Howlett prev = next; 284248f7df32SKirill A. Shutemov } 284348f7df32SKirill A. Shutemov 284448f7df32SKirill A. Shutemov if (!next) 2845c8d78c18SKirill A. Shutemov goto out; 2846c8d78c18SKirill A. Shutemov } 2847c8d78c18SKirill A. Shutemov 2848c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; 2849c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; 2850c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; 2851c8d78c18SKirill A. Shutemov 2852c8d78c18SKirill A. Shutemov flags &= MAP_NONBLOCK; 2853c8d78c18SKirill A. Shutemov flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; 2854fce000b1SLiam Howlett if (vma->vm_flags & VM_LOCKED) 2855c8d78c18SKirill A. Shutemov flags |= MAP_LOCKED; 285648f7df32SKirill A. Shutemov 2857c8d78c18SKirill A. Shutemov file = get_file(vma->vm_file); 285845e55300SPeter Collingbourne ret = do_mmap(vma->vm_file, start, size, 2859897ab3e0SMike Rapoport prot, flags, pgoff, &populate, NULL); 2860c8d78c18SKirill A. Shutemov fput(file); 2861c8d78c18SKirill A. Shutemov out: 2862d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2863c8d78c18SKirill A. Shutemov if (populate) 2864c8d78c18SKirill A. Shutemov mm_populate(ret, populate); 2865c8d78c18SKirill A. Shutemov if (!IS_ERR_VALUE(ret)) 2866c8d78c18SKirill A. Shutemov ret = 0; 2867c8d78c18SKirill A. Shutemov return ret; 2868c8d78c18SKirill A. Shutemov } 2869c8d78c18SKirill A. Shutemov 28701da177e4SLinus Torvalds /* 287127b26701SLiam R. Howlett * do_vma_munmap() - Unmap a full or partial vma. 287227b26701SLiam R. Howlett * @vmi: The vma iterator pointing at the vma 287327b26701SLiam R. Howlett * @vma: The first vma to be munmapped 287427b26701SLiam R. Howlett * @start: the start of the address to unmap 287527b26701SLiam R. Howlett * @end: The end of the address to unmap 28762e7ce7d3SLiam R. Howlett * @uf: The userfaultfd list_head 287727b26701SLiam R. Howlett * @downgrade: Attempt to downgrade or not 28782e7ce7d3SLiam R. Howlett * 287927b26701SLiam R. Howlett * Returns: 0 on success and not downgraded, 1 on success and downgraded. 288027b26701SLiam R. Howlett * unmaps a VMA mapping when the vma iterator is already in position. 288127b26701SLiam R. Howlett * Does not handle alignment. 28821da177e4SLinus Torvalds */ 288327b26701SLiam R. Howlett int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 288427b26701SLiam R. Howlett unsigned long start, unsigned long end, 288527b26701SLiam R. Howlett struct list_head *uf, bool downgrade) 28862e7ce7d3SLiam R. Howlett { 28872e7ce7d3SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 28882e7ce7d3SLiam R. Howlett int ret; 28892e7ce7d3SLiam R. Howlett 289027b26701SLiam R. Howlett arch_unmap(mm, start, end); 289127b26701SLiam R. Howlett ret = do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); 28922e7ce7d3SLiam R. Howlett validate_mm_mt(mm); 28932e7ce7d3SLiam R. Howlett return ret; 28942e7ce7d3SLiam R. Howlett } 28952e7ce7d3SLiam R. Howlett 28962e7ce7d3SLiam R. Howlett /* 28972e7ce7d3SLiam R. Howlett * do_brk_flags() - Increase the brk vma if the flags match. 289892fed820SLiam R. Howlett * @vmi: The vma iterator 28992e7ce7d3SLiam R. Howlett * @addr: The start address 29002e7ce7d3SLiam R. Howlett * @len: The length of the increase 29012e7ce7d3SLiam R. Howlett * @vma: The vma, 29022e7ce7d3SLiam R. Howlett * @flags: The VMA Flags 29032e7ce7d3SLiam R. Howlett * 29042e7ce7d3SLiam R. Howlett * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags 29052e7ce7d3SLiam R. Howlett * do not match then create a new anonymous VMA. Eventually we may be able to 29062e7ce7d3SLiam R. Howlett * do some brk-specific accounting here. 29072e7ce7d3SLiam R. Howlett */ 290892fed820SLiam R. Howlett static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, 2909763ecb03SLiam R. Howlett unsigned long addr, unsigned long len, unsigned long flags) 29101da177e4SLinus Torvalds { 29111da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 2912287051b1SLiam R. Howlett struct vma_prepare vp; 29132e7ce7d3SLiam R. Howlett 2914d4af56c5SLiam R. Howlett validate_mm_mt(mm); 29152e7ce7d3SLiam R. Howlett /* 29162e7ce7d3SLiam R. Howlett * Check against address space limits by the changed size 29172e7ce7d3SLiam R. Howlett * Note: This happens *after* clearing old mappings in some code paths. 29182e7ce7d3SLiam R. Howlett */ 291916e72e9bSDenys Vlasenko flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 292084638335SKonstantin Khlebnikov if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) 29211da177e4SLinus Torvalds return -ENOMEM; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 29241da177e4SLinus Torvalds return -ENOMEM; 29251da177e4SLinus Torvalds 2926191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 29271da177e4SLinus Torvalds return -ENOMEM; 29281da177e4SLinus Torvalds 29291da177e4SLinus Torvalds /* 29302e7ce7d3SLiam R. Howlett * Expand the existing vma if possible; Note that singular lists do not 29312e7ce7d3SLiam R. Howlett * occur after forking, so the expand will only happen on new VMAs. 29321da177e4SLinus Torvalds */ 29336c28ca64SLiam Howlett if (vma && vma->vm_end == addr && !vma_policy(vma) && 29346c28ca64SLiam Howlett can_vma_merge_after(vma, flags, NULL, NULL, 29356c28ca64SLiam Howlett addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) { 293692fed820SLiam R. Howlett if (vma_iter_prealloc(vmi)) 2937675eaca1SAlistair Popple goto unacct_fail; 293828c5609fSLiam Howlett 2939287051b1SLiam R. Howlett init_vma_prep(&vp, vma); 2940287051b1SLiam R. Howlett vma_prepare(&vp); 2941ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0); 29422e7ce7d3SLiam R. Howlett vma->vm_end = addr + len; 29431c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_SOFTDIRTY); 294492fed820SLiam R. Howlett vma_iter_store(vmi, vma); 29452e7ce7d3SLiam R. Howlett 2946287051b1SLiam R. Howlett vma_complete(&vp, vmi, mm); 29472e7ce7d3SLiam R. Howlett khugepaged_enter_vma(vma, flags); 29482e7ce7d3SLiam R. Howlett goto out; 29492e7ce7d3SLiam R. Howlett } 29502e7ce7d3SLiam R. Howlett 29512e7ce7d3SLiam R. Howlett /* create a vma struct for an anonymous mapping */ 29522e7ce7d3SLiam R. Howlett vma = vm_area_alloc(mm); 29532e7ce7d3SLiam R. Howlett if (!vma) 2954675eaca1SAlistair Popple goto unacct_fail; 29551da177e4SLinus Torvalds 2956bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 29571da177e4SLinus Torvalds vma->vm_start = addr; 29581da177e4SLinus Torvalds vma->vm_end = addr + len; 29592e7ce7d3SLiam R. Howlett vma->vm_pgoff = addr >> PAGE_SHIFT; 29601c71222eSSuren Baghdasaryan vm_flags_init(vma, flags); 29613ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 296292fed820SLiam R. Howlett if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) 29632e7ce7d3SLiam R. Howlett goto mas_store_fail; 2964d4af56c5SLiam R. Howlett 29652e7ce7d3SLiam R. Howlett mm->map_count++; 29661da177e4SLinus Torvalds out: 29673af9e859SEric B Munson perf_event_mmap(vma); 29681da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 296984638335SKonstantin Khlebnikov mm->data_vm += len >> PAGE_SHIFT; 2970128557ffSMichel Lespinasse if (flags & VM_LOCKED) 2971ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 29721c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_SOFTDIRTY); 2973763ecb03SLiam R. Howlett validate_mm(mm); 29745d22fc25SLinus Torvalds return 0; 2975d4af56c5SLiam R. Howlett 29762e7ce7d3SLiam R. Howlett mas_store_fail: 2977d4af56c5SLiam R. Howlett vm_area_free(vma); 2978675eaca1SAlistair Popple unacct_fail: 29792e7ce7d3SLiam R. Howlett vm_unacct_memory(len >> PAGE_SHIFT); 29802e7ce7d3SLiam R. Howlett return -ENOMEM; 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 2983bb177a73SMichal Hocko int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) 2984e4eb1ff6SLinus Torvalds { 2985e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 29862e7ce7d3SLiam R. Howlett struct vm_area_struct *vma = NULL; 2987bb177a73SMichal Hocko unsigned long len; 29885d22fc25SLinus Torvalds int ret; 2989128557ffSMichel Lespinasse bool populate; 2990897ab3e0SMike Rapoport LIST_HEAD(uf); 299192fed820SLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 2992e4eb1ff6SLinus Torvalds 2993bb177a73SMichal Hocko len = PAGE_ALIGN(request); 2994bb177a73SMichal Hocko if (len < request) 2995bb177a73SMichal Hocko return -ENOMEM; 2996bb177a73SMichal Hocko if (!len) 2997bb177a73SMichal Hocko return 0; 2998bb177a73SMichal Hocko 2999d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 30002d6c9282SMichal Hocko return -EINTR; 30012d6c9282SMichal Hocko 30022e7ce7d3SLiam R. Howlett /* Until we need other flags, refuse anything except VM_EXEC. */ 30032e7ce7d3SLiam R. Howlett if ((flags & (~VM_EXEC)) != 0) 30042e7ce7d3SLiam R. Howlett return -EINVAL; 30052e7ce7d3SLiam R. Howlett 30062e7ce7d3SLiam R. Howlett ret = check_brk_limits(addr, len); 30072e7ce7d3SLiam R. Howlett if (ret) 30082e7ce7d3SLiam R. Howlett goto limits_failed; 30092e7ce7d3SLiam R. Howlett 3010183654ceSLiam R. Howlett ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0); 30112e7ce7d3SLiam R. Howlett if (ret) 30122e7ce7d3SLiam R. Howlett goto munmap_failed; 30132e7ce7d3SLiam R. Howlett 301492fed820SLiam R. Howlett vma = vma_prev(&vmi); 301592fed820SLiam R. Howlett ret = do_brk_flags(&vmi, vma, addr, len, flags); 3016128557ffSMichel Lespinasse populate = ((mm->def_flags & VM_LOCKED) != 0); 3017d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3018897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 30195d22fc25SLinus Torvalds if (populate && !ret) 3020128557ffSMichel Lespinasse mm_populate(addr, len); 3021e4eb1ff6SLinus Torvalds return ret; 30222e7ce7d3SLiam R. Howlett 30232e7ce7d3SLiam R. Howlett munmap_failed: 30242e7ce7d3SLiam R. Howlett limits_failed: 30252e7ce7d3SLiam R. Howlett mmap_write_unlock(mm); 30262e7ce7d3SLiam R. Howlett return ret; 3027e4eb1ff6SLinus Torvalds } 302816e72e9bSDenys Vlasenko EXPORT_SYMBOL(vm_brk_flags); 302916e72e9bSDenys Vlasenko 303016e72e9bSDenys Vlasenko int vm_brk(unsigned long addr, unsigned long len) 303116e72e9bSDenys Vlasenko { 303216e72e9bSDenys Vlasenko return vm_brk_flags(addr, len, 0); 303316e72e9bSDenys Vlasenko } 3034e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds /* Release all mmaps. */ 30371da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 30381da177e4SLinus Torvalds { 3039d16dfc55SPeter Zijlstra struct mmu_gather tlb; 3040ba470de4SRik van Riel struct vm_area_struct *vma; 30411da177e4SLinus Torvalds unsigned long nr_accounted = 0; 3042763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 3043763ecb03SLiam R. Howlett int count = 0; 30441da177e4SLinus Torvalds 3045d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 3046cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 3047d6dd61c8SJeremy Fitzhardinge 3048bf3980c8SSuren Baghdasaryan mmap_read_lock(mm); 30499480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 30509480c53eSJeremy Fitzhardinge 3051763ecb03SLiam R. Howlett vma = mas_find(&mas, ULONG_MAX); 305264591e86SSuren Baghdasaryan if (!vma) { 305364591e86SSuren Baghdasaryan /* Can happen if dup_mmap() received an OOM */ 3054bf3980c8SSuren Baghdasaryan mmap_read_unlock(mm); 30559480c53eSJeremy Fitzhardinge return; 305664591e86SSuren Baghdasaryan } 30579480c53eSJeremy Fitzhardinge 30581da177e4SLinus Torvalds lru_add_drain(); 30591da177e4SLinus Torvalds flush_cache_mm(mm); 3060d8b45053SWill Deacon tlb_gather_mmu_fullmm(&tlb, mm); 3061901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 3062763ecb03SLiam R. Howlett /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ 306368f48381SSuren Baghdasaryan unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX, false); 3064bf3980c8SSuren Baghdasaryan mmap_read_unlock(mm); 3065bf3980c8SSuren Baghdasaryan 3066bf3980c8SSuren Baghdasaryan /* 3067bf3980c8SSuren Baghdasaryan * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper 3068b3541d91SSuren Baghdasaryan * because the memory has been already freed. 3069bf3980c8SSuren Baghdasaryan */ 3070bf3980c8SSuren Baghdasaryan set_bit(MMF_OOM_SKIP, &mm->flags); 3071bf3980c8SSuren Baghdasaryan mmap_write_lock(mm); 3072763ecb03SLiam R. Howlett free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, 307398e51a22SSuren Baghdasaryan USER_PGTABLES_CEILING, true); 3074ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 30751da177e4SLinus Torvalds 3076763ecb03SLiam R. Howlett /* 3077763ecb03SLiam R. Howlett * Walk the list again, actually closing and freeing it, with preemption 3078763ecb03SLiam R. Howlett * enabled, without holding any MM locks besides the unreachable 3079763ecb03SLiam R. Howlett * mmap_write_lock. 3080763ecb03SLiam R. Howlett */ 3081763ecb03SLiam R. Howlett do { 30824f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 30834f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 3084*0d2ebf9cSSuren Baghdasaryan remove_vma(vma, true); 3085763ecb03SLiam R. Howlett count++; 30860a3b3c25SPaul E. McKenney cond_resched(); 3087763ecb03SLiam R. Howlett } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); 3088763ecb03SLiam R. Howlett 3089763ecb03SLiam R. Howlett BUG_ON(count != mm->map_count); 3090d4af56c5SLiam R. Howlett 3091d4af56c5SLiam R. Howlett trace_exit_mmap(mm); 3092d4af56c5SLiam R. Howlett __mt_destroy(&mm->mm_mt); 309364591e86SSuren Baghdasaryan mmap_write_unlock(mm); 30944f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 30951da177e4SLinus Torvalds } 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 30981da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 3099c8c06efaSDavidlohr Bueso * then i_mmap_rwsem is taken here. 31001da177e4SLinus Torvalds */ 31011da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 31021da177e4SLinus Torvalds { 3103d4af56c5SLiam R. Howlett unsigned long charged = vma_pages(vma); 31041da177e4SLinus Torvalds 3105d4af56c5SLiam R. Howlett 3106d0601a50SLiam R. Howlett if (find_vma_intersection(mm, vma->vm_start, vma->vm_end)) 3107c9d13f5fSChen Gang return -ENOMEM; 3108d4af56c5SLiam R. Howlett 3109c9d13f5fSChen Gang if ((vma->vm_flags & VM_ACCOUNT) && 3110d4af56c5SLiam R. Howlett security_vm_enough_memory_mm(mm, charged)) 3111c9d13f5fSChen Gang return -ENOMEM; 3112c9d13f5fSChen Gang 31131da177e4SLinus Torvalds /* 31141da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 31151da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 31161da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 31171da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 31181da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 31191da177e4SLinus Torvalds * 31201da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 31211da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 31221da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 31238332326eSLiao Pingfang * Similarly in do_mmap and in do_brk_flags. 31241da177e4SLinus Torvalds */ 31258a9cc3b5SOleg Nesterov if (vma_is_anonymous(vma)) { 31261da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 31271da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 31281da177e4SLinus Torvalds } 31292b144498SSrikar Dronamraju 3130763ecb03SLiam R. Howlett if (vma_link(mm, vma)) { 3131d4af56c5SLiam R. Howlett vm_unacct_memory(charged); 3132d4af56c5SLiam R. Howlett return -ENOMEM; 3133d4af56c5SLiam R. Howlett } 3134d4af56c5SLiam R. Howlett 31351da177e4SLinus Torvalds return 0; 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds /* 31391da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 31401da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 31411da177e4SLinus Torvalds */ 31421da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 314338a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 314438a76013SMichel Lespinasse bool *need_rmap_locks) 31451da177e4SLinus Torvalds { 31461da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 31471da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 31481da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 31491da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 3150948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 3151076f16bfSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 31521da177e4SLinus Torvalds 3153d4af56c5SLiam R. Howlett validate_mm_mt(mm); 31541da177e4SLinus Torvalds /* 31551da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 31561da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 31571da177e4SLinus Torvalds */ 3158ce75799bSOleg Nesterov if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 31591da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 3160948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 3161948f017bSAndrea Arcangeli } 31621da177e4SLinus Torvalds 3163763ecb03SLiam R. Howlett new_vma = find_vma_prev(mm, addr, &prev); 3164763ecb03SLiam R. Howlett if (new_vma && new_vma->vm_start < addr + len) 31656597d783SHugh Dickins return NULL; /* should never get here */ 3166524e00b3SLiam R. Howlett 31679760ebffSLiam R. Howlett new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags, 316819a809afSAndrea Arcangeli vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), 31695c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 31701da177e4SLinus Torvalds if (new_vma) { 31711da177e4SLinus Torvalds /* 31721da177e4SLinus Torvalds * Source vma may have been merged into new_vma 31731da177e4SLinus Torvalds */ 3174948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 3175948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 3176948f017bSAndrea Arcangeli /* 3177948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 3178948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 3179948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 3180948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 3181948f017bSAndrea Arcangeli * destination address of the mremap to allow 3182948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 3183948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 3184948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 3185948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 3186948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 3187948f017bSAndrea Arcangeli */ 318881d1b09cSSasha Levin VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 318938a76013SMichel Lespinasse *vmap = vma = new_vma; 3190108d6642SMichel Lespinasse } 319138a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 31921da177e4SLinus Torvalds } else { 31933928d4f5SLinus Torvalds new_vma = vm_area_dup(vma); 3194e3975891SChen Gang if (!new_vma) 3195e3975891SChen Gang goto out; 31961da177e4SLinus Torvalds new_vma->vm_start = addr; 31971da177e4SLinus Torvalds new_vma->vm_end = addr + len; 31981da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 3199ef0855d3SOleg Nesterov if (vma_dup_policy(vma, new_vma)) 3200523d4e20SMichel Lespinasse goto out_free_vma; 3201523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 3202523d4e20SMichel Lespinasse goto out_free_mempol; 3203e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 32041da177e4SLinus Torvalds get_file(new_vma->vm_file); 32051da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 32061da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 3207d6ac235dSSuren Baghdasaryan vma_start_write(new_vma); 3208763ecb03SLiam R. Howlett if (vma_link(mm, new_vma)) 3209524e00b3SLiam R. Howlett goto out_vma_link; 321038a76013SMichel Lespinasse *need_rmap_locks = false; 32111da177e4SLinus Torvalds } 3212d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32131da177e4SLinus Torvalds return new_vma; 32145beb4930SRik van Riel 3215524e00b3SLiam R. Howlett out_vma_link: 3216524e00b3SLiam R. Howlett if (new_vma->vm_ops && new_vma->vm_ops->close) 3217524e00b3SLiam R. Howlett new_vma->vm_ops->close(new_vma); 321892b73996SLiam Howlett 321992b73996SLiam Howlett if (new_vma->vm_file) 322092b73996SLiam Howlett fput(new_vma->vm_file); 322192b73996SLiam Howlett 322292b73996SLiam Howlett unlink_anon_vmas(new_vma); 32235beb4930SRik van Riel out_free_mempol: 3224ef0855d3SOleg Nesterov mpol_put(vma_policy(new_vma)); 32255beb4930SRik van Riel out_free_vma: 32263928d4f5SLinus Torvalds vm_area_free(new_vma); 3227e3975891SChen Gang out: 3228d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32295beb4930SRik van Riel return NULL; 32301da177e4SLinus Torvalds } 3231119f657cSakpm@osdl.org 3232119f657cSakpm@osdl.org /* 3233119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 3234119f657cSakpm@osdl.org * number of pages 3235119f657cSakpm@osdl.org */ 323684638335SKonstantin Khlebnikov bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) 3237119f657cSakpm@osdl.org { 323884638335SKonstantin Khlebnikov if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) 323984638335SKonstantin Khlebnikov return false; 3240119f657cSakpm@osdl.org 3241d977d56cSKonstantin Khlebnikov if (is_data_mapping(flags) && 3242d977d56cSKonstantin Khlebnikov mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { 3243f4fcd558SKonstantin Khlebnikov /* Workaround for Valgrind */ 3244f4fcd558SKonstantin Khlebnikov if (rlimit(RLIMIT_DATA) == 0 && 3245f4fcd558SKonstantin Khlebnikov mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) 3246f4fcd558SKonstantin Khlebnikov return true; 324757a7702bSDavid Woodhouse 324857a7702bSDavid Woodhouse pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", 3249d977d56cSKonstantin Khlebnikov current->comm, current->pid, 3250d977d56cSKonstantin Khlebnikov (mm->data_vm + npages) << PAGE_SHIFT, 325157a7702bSDavid Woodhouse rlimit(RLIMIT_DATA), 325257a7702bSDavid Woodhouse ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); 325357a7702bSDavid Woodhouse 325457a7702bSDavid Woodhouse if (!ignore_rlimit_data) 3255d977d56cSKonstantin Khlebnikov return false; 3256d977d56cSKonstantin Khlebnikov } 3257119f657cSakpm@osdl.org 325884638335SKonstantin Khlebnikov return true; 325984638335SKonstantin Khlebnikov } 326084638335SKonstantin Khlebnikov 326184638335SKonstantin Khlebnikov void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) 326284638335SKonstantin Khlebnikov { 32637866076bSPeng Liu WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); 326484638335SKonstantin Khlebnikov 3265d977d56cSKonstantin Khlebnikov if (is_exec_mapping(flags)) 326684638335SKonstantin Khlebnikov mm->exec_vm += npages; 3267d977d56cSKonstantin Khlebnikov else if (is_stack_mapping(flags)) 326884638335SKonstantin Khlebnikov mm->stack_vm += npages; 3269d977d56cSKonstantin Khlebnikov else if (is_data_mapping(flags)) 327084638335SKonstantin Khlebnikov mm->data_vm += npages; 3271119f657cSakpm@osdl.org } 3272fa5dc22fSRoland McGrath 3273b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf); 3274a62c34bdSAndy Lutomirski 3275a62c34bdSAndy Lutomirski /* 3276a62c34bdSAndy Lutomirski * Having a close hook prevents vma merging regardless of flags. 3277a62c34bdSAndy Lutomirski */ 3278a62c34bdSAndy Lutomirski static void special_mapping_close(struct vm_area_struct *vma) 3279a62c34bdSAndy Lutomirski { 3280a62c34bdSAndy Lutomirski } 3281a62c34bdSAndy Lutomirski 3282a62c34bdSAndy Lutomirski static const char *special_mapping_name(struct vm_area_struct *vma) 3283a62c34bdSAndy Lutomirski { 3284a62c34bdSAndy Lutomirski return ((struct vm_special_mapping *)vma->vm_private_data)->name; 3285a62c34bdSAndy Lutomirski } 3286a62c34bdSAndy Lutomirski 328714d07113SBrian Geffon static int special_mapping_mremap(struct vm_area_struct *new_vma) 3288b059a453SDmitry Safonov { 3289b059a453SDmitry Safonov struct vm_special_mapping *sm = new_vma->vm_private_data; 3290b059a453SDmitry Safonov 3291280e87e9SDmitry Safonov if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) 3292280e87e9SDmitry Safonov return -EFAULT; 3293280e87e9SDmitry Safonov 3294b059a453SDmitry Safonov if (sm->mremap) 3295b059a453SDmitry Safonov return sm->mremap(sm, new_vma); 3296280e87e9SDmitry Safonov 3297b059a453SDmitry Safonov return 0; 3298b059a453SDmitry Safonov } 3299b059a453SDmitry Safonov 3300871402e0SDmitry Safonov static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) 3301871402e0SDmitry Safonov { 3302871402e0SDmitry Safonov /* 3303871402e0SDmitry Safonov * Forbid splitting special mappings - kernel has expectations over 3304871402e0SDmitry Safonov * the number of pages in mapping. Together with VM_DONTEXPAND 3305871402e0SDmitry Safonov * the size of vma should stay the same over the special mapping's 3306871402e0SDmitry Safonov * lifetime. 3307871402e0SDmitry Safonov */ 3308871402e0SDmitry Safonov return -EINVAL; 3309871402e0SDmitry Safonov } 3310871402e0SDmitry Safonov 3311a62c34bdSAndy Lutomirski static const struct vm_operations_struct special_mapping_vmops = { 3312a62c34bdSAndy Lutomirski .close = special_mapping_close, 3313a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3314b059a453SDmitry Safonov .mremap = special_mapping_mremap, 3315a62c34bdSAndy Lutomirski .name = special_mapping_name, 3316af34ebebSDmitry Safonov /* vDSO code relies that VVAR can't be accessed remotely */ 3317af34ebebSDmitry Safonov .access = NULL, 3318871402e0SDmitry Safonov .may_split = special_mapping_split, 3319a62c34bdSAndy Lutomirski }; 3320a62c34bdSAndy Lutomirski 3321a62c34bdSAndy Lutomirski static const struct vm_operations_struct legacy_special_mapping_vmops = { 3322a62c34bdSAndy Lutomirski .close = special_mapping_close, 3323a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3324a62c34bdSAndy Lutomirski }; 3325fa5dc22fSRoland McGrath 3326b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf) 3327fa5dc22fSRoland McGrath { 332811bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 3329b1d0e4f5SNick Piggin pgoff_t pgoff; 3330fa5dc22fSRoland McGrath struct page **pages; 3331fa5dc22fSRoland McGrath 3332f872f540SAndy Lutomirski if (vma->vm_ops == &legacy_special_mapping_vmops) { 3333a62c34bdSAndy Lutomirski pages = vma->vm_private_data; 3334f872f540SAndy Lutomirski } else { 3335f872f540SAndy Lutomirski struct vm_special_mapping *sm = vma->vm_private_data; 3336f872f540SAndy Lutomirski 3337f872f540SAndy Lutomirski if (sm->fault) 333811bac800SDave Jiang return sm->fault(sm, vmf->vma, vmf); 3339f872f540SAndy Lutomirski 3340f872f540SAndy Lutomirski pages = sm->pages; 3341f872f540SAndy Lutomirski } 3342a62c34bdSAndy Lutomirski 33438a9cc3b5SOleg Nesterov for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) 3344b1d0e4f5SNick Piggin pgoff--; 3345fa5dc22fSRoland McGrath 3346fa5dc22fSRoland McGrath if (*pages) { 3347fa5dc22fSRoland McGrath struct page *page = *pages; 3348fa5dc22fSRoland McGrath get_page(page); 3349b1d0e4f5SNick Piggin vmf->page = page; 3350b1d0e4f5SNick Piggin return 0; 3351fa5dc22fSRoland McGrath } 3352fa5dc22fSRoland McGrath 3353b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 3354fa5dc22fSRoland McGrath } 3355fa5dc22fSRoland McGrath 3356a62c34bdSAndy Lutomirski static struct vm_area_struct *__install_special_mapping( 3357a62c34bdSAndy Lutomirski struct mm_struct *mm, 3358fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 335927f28b97SChen Gang unsigned long vm_flags, void *priv, 336027f28b97SChen Gang const struct vm_operations_struct *ops) 3361fa5dc22fSRoland McGrath { 3362462e635eSTavis Ormandy int ret; 3363fa5dc22fSRoland McGrath struct vm_area_struct *vma; 3364fa5dc22fSRoland McGrath 3365d4af56c5SLiam R. Howlett validate_mm_mt(mm); 3366490fc053SLinus Torvalds vma = vm_area_alloc(mm); 3367fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 33683935ed6aSStefani Seibold return ERR_PTR(-ENOMEM); 3369fa5dc22fSRoland McGrath 3370fa5dc22fSRoland McGrath vma->vm_start = addr; 3371fa5dc22fSRoland McGrath vma->vm_end = addr + len; 3372fa5dc22fSRoland McGrath 3373e430a95aSSuren Baghdasaryan vm_flags_init(vma, (vm_flags | mm->def_flags | 3374e430a95aSSuren Baghdasaryan VM_DONTEXPAND | VM_SOFTDIRTY) & ~VM_LOCKED_MASK); 33753ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 3376fa5dc22fSRoland McGrath 3377a62c34bdSAndy Lutomirski vma->vm_ops = ops; 3378a62c34bdSAndy Lutomirski vma->vm_private_data = priv; 3379fa5dc22fSRoland McGrath 3380462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 3381462e635eSTavis Ormandy if (ret) 3382462e635eSTavis Ormandy goto out; 3383fa5dc22fSRoland McGrath 338484638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); 3385fa5dc22fSRoland McGrath 3386cdd6c482SIngo Molnar perf_event_mmap(vma); 3387089dd79dSPeter Zijlstra 3388d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33893935ed6aSStefani Seibold return vma; 3390462e635eSTavis Ormandy 3391462e635eSTavis Ormandy out: 33923928d4f5SLinus Torvalds vm_area_free(vma); 3393d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33943935ed6aSStefani Seibold return ERR_PTR(ret); 33953935ed6aSStefani Seibold } 33963935ed6aSStefani Seibold 33972eefd878SDmitry Safonov bool vma_is_special_mapping(const struct vm_area_struct *vma, 33982eefd878SDmitry Safonov const struct vm_special_mapping *sm) 33992eefd878SDmitry Safonov { 34002eefd878SDmitry Safonov return vma->vm_private_data == sm && 34012eefd878SDmitry Safonov (vma->vm_ops == &special_mapping_vmops || 34022eefd878SDmitry Safonov vma->vm_ops == &legacy_special_mapping_vmops); 34032eefd878SDmitry Safonov } 34042eefd878SDmitry Safonov 3405a62c34bdSAndy Lutomirski /* 3406c1e8d7c6SMichel Lespinasse * Called with mm->mmap_lock held for writing. 3407a62c34bdSAndy Lutomirski * Insert a new vma covering the given region, with the given flags. 3408a62c34bdSAndy Lutomirski * Its pages are supplied by the given array of struct page *. 3409a62c34bdSAndy Lutomirski * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 3410a62c34bdSAndy Lutomirski * The region past the last page supplied will always produce SIGBUS. 3411a62c34bdSAndy Lutomirski * The array pointer and the pages it points to are assumed to stay alive 3412a62c34bdSAndy Lutomirski * for as long as this mapping might exist. 3413a62c34bdSAndy Lutomirski */ 3414a62c34bdSAndy Lutomirski struct vm_area_struct *_install_special_mapping( 3415a62c34bdSAndy Lutomirski struct mm_struct *mm, 3416a62c34bdSAndy Lutomirski unsigned long addr, unsigned long len, 3417a62c34bdSAndy Lutomirski unsigned long vm_flags, const struct vm_special_mapping *spec) 3418a62c34bdSAndy Lutomirski { 341927f28b97SChen Gang return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, 342027f28b97SChen Gang &special_mapping_vmops); 3421a62c34bdSAndy Lutomirski } 3422a62c34bdSAndy Lutomirski 34233935ed6aSStefani Seibold int install_special_mapping(struct mm_struct *mm, 34243935ed6aSStefani Seibold unsigned long addr, unsigned long len, 34253935ed6aSStefani Seibold unsigned long vm_flags, struct page **pages) 34263935ed6aSStefani Seibold { 3427a62c34bdSAndy Lutomirski struct vm_area_struct *vma = __install_special_mapping( 342827f28b97SChen Gang mm, addr, len, vm_flags, (void *)pages, 342927f28b97SChen Gang &legacy_special_mapping_vmops); 34303935ed6aSStefani Seibold 343114bd5b45SDuan Jiong return PTR_ERR_OR_ZERO(vma); 3432fa5dc22fSRoland McGrath } 34337906d00cSAndrea Arcangeli 34347906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 34357906d00cSAndrea Arcangeli 3436454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 34377906d00cSAndrea Arcangeli { 3438f808c13fSDavidlohr Bueso if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 34397906d00cSAndrea Arcangeli /* 34407906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 34417906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 34427906d00cSAndrea Arcangeli */ 3443da1c55f1SMichel Lespinasse down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 34447906d00cSAndrea Arcangeli /* 34457906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 34465a505085SIngo Molnar * anon_vma->root->rwsem. If some other vma in this mm shares 34477906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 34487906d00cSAndrea Arcangeli * 34497906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 34507906d00cSAndrea Arcangeli * can't change from under us thanks to the 34515a505085SIngo Molnar * anon_vma->root->rwsem. 34527906d00cSAndrea Arcangeli */ 34537906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 3454f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 34557906d00cSAndrea Arcangeli BUG(); 34567906d00cSAndrea Arcangeli } 34577906d00cSAndrea Arcangeli } 34587906d00cSAndrea Arcangeli 3459454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 34607906d00cSAndrea Arcangeli { 34617906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 34627906d00cSAndrea Arcangeli /* 34637906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 34647906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 34657906d00cSAndrea Arcangeli * 34667906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 34677906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 34687906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 34697906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 34707906d00cSAndrea Arcangeli */ 34717906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 34727906d00cSAndrea Arcangeli BUG(); 3473da1c55f1SMichel Lespinasse down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 34747906d00cSAndrea Arcangeli } 34757906d00cSAndrea Arcangeli } 34767906d00cSAndrea Arcangeli 34777906d00cSAndrea Arcangeli /* 34787906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 34797906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 34807906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 34817906d00cSAndrea Arcangeli * 3482c1e8d7c6SMichel Lespinasse * The caller must take the mmap_lock in write mode before calling 34837906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 3484c1e8d7c6SMichel Lespinasse * mmap_lock until mm_drop_all_locks() returns. 34857906d00cSAndrea Arcangeli * 3486c1e8d7c6SMichel Lespinasse * mmap_lock in write mode is required in order to block all operations 34877906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 348827ba0644SKirill A. Shutemov * altering the vma layout. It's also needed in write mode to avoid new 34897906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 34907906d00cSAndrea Arcangeli * 34917906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 34927906d00cSAndrea Arcangeli * or it would deadlock. 34937906d00cSAndrea Arcangeli * 3494bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 34957906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 34967906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 34977906d00cSAndrea Arcangeli * 349888f306b6SKirill A. Shutemov * We take locks in following order, accordingly to comment at beginning 349988f306b6SKirill A. Shutemov * of mm/rmap.c: 350088f306b6SKirill A. Shutemov * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 350188f306b6SKirill A. Shutemov * hugetlb mapping); 3502eeff9a5dSSuren Baghdasaryan * - all vmas marked locked 350388f306b6SKirill A. Shutemov * - all i_mmap_rwsem locks; 350488f306b6SKirill A. Shutemov * - all anon_vma->rwseml 350588f306b6SKirill A. Shutemov * 350688f306b6SKirill A. Shutemov * We can take all locks within these types randomly because the VM code 350788f306b6SKirill A. Shutemov * doesn't nest them and we protected from parallel mm_take_all_locks() by 350888f306b6SKirill A. Shutemov * mm_all_locks_mutex. 35097906d00cSAndrea Arcangeli * 35107906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 35117906d00cSAndrea Arcangeli * that may have to take thousand of locks. 35127906d00cSAndrea Arcangeli * 35137906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 35147906d00cSAndrea Arcangeli */ 35157906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 35167906d00cSAndrea Arcangeli { 35177906d00cSAndrea Arcangeli struct vm_area_struct *vma; 35185beb4930SRik van Riel struct anon_vma_chain *avc; 3519763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 35207906d00cSAndrea Arcangeli 3521325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 35227906d00cSAndrea Arcangeli 35237906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 35247906d00cSAndrea Arcangeli 3525763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35267906d00cSAndrea Arcangeli if (signal_pending(current)) 35277906d00cSAndrea Arcangeli goto out_unlock; 3528eeff9a5dSSuren Baghdasaryan vma_start_write(vma); 3529eeff9a5dSSuren Baghdasaryan } 3530eeff9a5dSSuren Baghdasaryan 3531eeff9a5dSSuren Baghdasaryan mas_set(&mas, 0); 3532eeff9a5dSSuren Baghdasaryan mas_for_each(&mas, vma, ULONG_MAX) { 3533eeff9a5dSSuren Baghdasaryan if (signal_pending(current)) 3534eeff9a5dSSuren Baghdasaryan goto out_unlock; 353588f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 353688f306b6SKirill A. Shutemov is_vm_hugetlb_page(vma)) 353788f306b6SKirill A. Shutemov vm_lock_mapping(mm, vma->vm_file->f_mapping); 353888f306b6SKirill A. Shutemov } 353988f306b6SKirill A. Shutemov 3540763ecb03SLiam R. Howlett mas_set(&mas, 0); 3541763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 354288f306b6SKirill A. Shutemov if (signal_pending(current)) 354388f306b6SKirill A. Shutemov goto out_unlock; 354488f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 354588f306b6SKirill A. Shutemov !is_vm_hugetlb_page(vma)) 3546454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 35477906d00cSAndrea Arcangeli } 35487cd5a02fSPeter Zijlstra 3549763ecb03SLiam R. Howlett mas_set(&mas, 0); 3550763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35517cd5a02fSPeter Zijlstra if (signal_pending(current)) 35527cd5a02fSPeter Zijlstra goto out_unlock; 35537cd5a02fSPeter Zijlstra if (vma->anon_vma) 35545beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 35555beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 35567cd5a02fSPeter Zijlstra } 35577cd5a02fSPeter Zijlstra 3558584cff54SKautuk Consul return 0; 35597906d00cSAndrea Arcangeli 35607906d00cSAndrea Arcangeli out_unlock: 35617906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 3562584cff54SKautuk Consul return -EINTR; 35637906d00cSAndrea Arcangeli } 35647906d00cSAndrea Arcangeli 35657906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 35667906d00cSAndrea Arcangeli { 3567f808c13fSDavidlohr Bueso if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 35687906d00cSAndrea Arcangeli /* 35697906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 35707906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 35717906d00cSAndrea Arcangeli * 35727906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 3573bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 35747906d00cSAndrea Arcangeli * never see our bitflag. 35757906d00cSAndrea Arcangeli * 35767906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 35777906d00cSAndrea Arcangeli * can't change from under us until we release the 35785a505085SIngo Molnar * anon_vma->root->rwsem. 35797906d00cSAndrea Arcangeli */ 35807906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 3581f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 35827906d00cSAndrea Arcangeli BUG(); 358308b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 35847906d00cSAndrea Arcangeli } 35857906d00cSAndrea Arcangeli } 35867906d00cSAndrea Arcangeli 35877906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 35887906d00cSAndrea Arcangeli { 35897906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 35907906d00cSAndrea Arcangeli /* 35917906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 35927906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 35937906d00cSAndrea Arcangeli */ 359483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 35957906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 35967906d00cSAndrea Arcangeli &mapping->flags)) 35977906d00cSAndrea Arcangeli BUG(); 35987906d00cSAndrea Arcangeli } 35997906d00cSAndrea Arcangeli } 36007906d00cSAndrea Arcangeli 36017906d00cSAndrea Arcangeli /* 3602c1e8d7c6SMichel Lespinasse * The mmap_lock cannot be released by the caller until 36037906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 36047906d00cSAndrea Arcangeli */ 36057906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 36067906d00cSAndrea Arcangeli { 36077906d00cSAndrea Arcangeli struct vm_area_struct *vma; 36085beb4930SRik van Riel struct anon_vma_chain *avc; 3609763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 36107906d00cSAndrea Arcangeli 3611325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 36127906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 36137906d00cSAndrea Arcangeli 3614763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 36157906d00cSAndrea Arcangeli if (vma->anon_vma) 36165beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 36175beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 36187906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 36197906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 36207906d00cSAndrea Arcangeli } 3621eeff9a5dSSuren Baghdasaryan vma_end_write_all(mm); 36227906d00cSAndrea Arcangeli 36237906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 36247906d00cSAndrea Arcangeli } 36258feae131SDavid Howells 36268feae131SDavid Howells /* 36273edf41d8Sseokhoon.yoon * initialise the percpu counter for VM 36288feae131SDavid Howells */ 36298feae131SDavid Howells void __init mmap_init(void) 36308feae131SDavid Howells { 363100a62ce9SKOSAKI Motohiro int ret; 363200a62ce9SKOSAKI Motohiro 3633908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 363400a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 36358feae131SDavid Howells } 3636c9b1d098SAndrew Shewmaker 3637c9b1d098SAndrew Shewmaker /* 3638c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 3639c9b1d098SAndrew Shewmaker * 3640c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 3641c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 3642c9b1d098SAndrew Shewmaker * mode. 3643c9b1d098SAndrew Shewmaker * 3644c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 3645c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 3646c9b1d098SAndrew Shewmaker */ 36471640879aSAndrew Shewmaker static int init_user_reserve(void) 3648c9b1d098SAndrew Shewmaker { 3649c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 3650c9b1d098SAndrew Shewmaker 3651c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 3652c9b1d098SAndrew Shewmaker 3653c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 3654c9b1d098SAndrew Shewmaker return 0; 3655c9b1d098SAndrew Shewmaker } 3656a64fb3cdSPaul Gortmaker subsys_initcall(init_user_reserve); 36574eeab4f5SAndrew Shewmaker 36584eeab4f5SAndrew Shewmaker /* 36594eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 36604eeab4f5SAndrew Shewmaker * 36614eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 36624eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 36634eeab4f5SAndrew Shewmaker * 36644eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 36654eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 36664eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 36674eeab4f5SAndrew Shewmaker */ 36681640879aSAndrew Shewmaker static int init_admin_reserve(void) 36694eeab4f5SAndrew Shewmaker { 36704eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 36714eeab4f5SAndrew Shewmaker 3672c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 36734eeab4f5SAndrew Shewmaker 36744eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 36754eeab4f5SAndrew Shewmaker return 0; 36764eeab4f5SAndrew Shewmaker } 3677a64fb3cdSPaul Gortmaker subsys_initcall(init_admin_reserve); 36781640879aSAndrew Shewmaker 36791640879aSAndrew Shewmaker /* 36801640879aSAndrew Shewmaker * Reinititalise user and admin reserves if memory is added or removed. 36811640879aSAndrew Shewmaker * 36821640879aSAndrew Shewmaker * The default user reserve max is 128MB, and the default max for the 36831640879aSAndrew Shewmaker * admin reserve is 8MB. These are usually, but not always, enough to 36841640879aSAndrew Shewmaker * enable recovery from a memory hogging process using login/sshd, a shell, 36851640879aSAndrew Shewmaker * and tools like top. It may make sense to increase or even disable the 36861640879aSAndrew Shewmaker * reserve depending on the existence of swap or variations in the recovery 36871640879aSAndrew Shewmaker * tools. So, the admin may have changed them. 36881640879aSAndrew Shewmaker * 36891640879aSAndrew Shewmaker * If memory is added and the reserves have been eliminated or increased above 36901640879aSAndrew Shewmaker * the default max, then we'll trust the admin. 36911640879aSAndrew Shewmaker * 36921640879aSAndrew Shewmaker * If memory is removed and there isn't enough free memory, then we 36931640879aSAndrew Shewmaker * need to reset the reserves. 36941640879aSAndrew Shewmaker * 36951640879aSAndrew Shewmaker * Otherwise keep the reserve set by the admin. 36961640879aSAndrew Shewmaker */ 36971640879aSAndrew Shewmaker static int reserve_mem_notifier(struct notifier_block *nb, 36981640879aSAndrew Shewmaker unsigned long action, void *data) 36991640879aSAndrew Shewmaker { 37001640879aSAndrew Shewmaker unsigned long tmp, free_kbytes; 37011640879aSAndrew Shewmaker 37021640879aSAndrew Shewmaker switch (action) { 37031640879aSAndrew Shewmaker case MEM_ONLINE: 37041640879aSAndrew Shewmaker /* Default max is 128MB. Leave alone if modified by operator. */ 37051640879aSAndrew Shewmaker tmp = sysctl_user_reserve_kbytes; 37061640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 17)) 37071640879aSAndrew Shewmaker init_user_reserve(); 37081640879aSAndrew Shewmaker 37091640879aSAndrew Shewmaker /* Default max is 8MB. Leave alone if modified by operator. */ 37101640879aSAndrew Shewmaker tmp = sysctl_admin_reserve_kbytes; 37111640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 13)) 37121640879aSAndrew Shewmaker init_admin_reserve(); 37131640879aSAndrew Shewmaker 37141640879aSAndrew Shewmaker break; 37151640879aSAndrew Shewmaker case MEM_OFFLINE: 3716c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37171640879aSAndrew Shewmaker 37181640879aSAndrew Shewmaker if (sysctl_user_reserve_kbytes > free_kbytes) { 37191640879aSAndrew Shewmaker init_user_reserve(); 37201640879aSAndrew Shewmaker pr_info("vm.user_reserve_kbytes reset to %lu\n", 37211640879aSAndrew Shewmaker sysctl_user_reserve_kbytes); 37221640879aSAndrew Shewmaker } 37231640879aSAndrew Shewmaker 37241640879aSAndrew Shewmaker if (sysctl_admin_reserve_kbytes > free_kbytes) { 37251640879aSAndrew Shewmaker init_admin_reserve(); 37261640879aSAndrew Shewmaker pr_info("vm.admin_reserve_kbytes reset to %lu\n", 37271640879aSAndrew Shewmaker sysctl_admin_reserve_kbytes); 37281640879aSAndrew Shewmaker } 37291640879aSAndrew Shewmaker break; 37301640879aSAndrew Shewmaker default: 37311640879aSAndrew Shewmaker break; 37321640879aSAndrew Shewmaker } 37331640879aSAndrew Shewmaker return NOTIFY_OK; 37341640879aSAndrew Shewmaker } 37351640879aSAndrew Shewmaker 37361640879aSAndrew Shewmaker static int __meminit init_reserve_notifier(void) 37371640879aSAndrew Shewmaker { 37381eeaa4fdSLiu Shixin if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI)) 3739b1de0d13SMitchel Humpherys pr_err("Failed registering memory add/remove notifier for admin reserve\n"); 37401640879aSAndrew Shewmaker 37411640879aSAndrew Shewmaker return 0; 37421640879aSAndrew Shewmaker } 3743a64fb3cdSPaul Gortmaker subsys_initcall(init_reserve_notifier); 3744