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, 81763ecb03SLiam R. Howlett unsigned long end); 82e0da382cSHugh Dickins 8364e45507SPeter Feiner static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) 8464e45507SPeter Feiner { 8564e45507SPeter Feiner return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); 8664e45507SPeter Feiner } 8764e45507SPeter Feiner 8864e45507SPeter Feiner /* Update vma->vm_page_prot to reflect vma->vm_flags. */ 8964e45507SPeter Feiner void vma_set_page_prot(struct vm_area_struct *vma) 9064e45507SPeter Feiner { 9164e45507SPeter Feiner unsigned long vm_flags = vma->vm_flags; 926d2329f8SAndrea Arcangeli pgprot_t vm_page_prot; 9364e45507SPeter Feiner 946d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); 956d2329f8SAndrea Arcangeli if (vma_wants_writenotify(vma, vm_page_prot)) { 9664e45507SPeter Feiner vm_flags &= ~VM_SHARED; 976d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); 9864e45507SPeter Feiner } 99c1e8d7c6SMichel Lespinasse /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ 1006d2329f8SAndrea Arcangeli WRITE_ONCE(vma->vm_page_prot, vm_page_prot); 10164e45507SPeter Feiner } 10264e45507SPeter Feiner 1031da177e4SLinus Torvalds /* 104c8c06efaSDavidlohr Bueso * Requires inode->i_mapping->i_mmap_rwsem 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1071da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1104bb5f5d9SDavid Herrmann mapping_unmap_writable(mapping); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 1136b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 1141da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* 1186b2dbba8SMichel Lespinasse * Unlink a file-based vm structure from its interval tree, to hide 119a8fb5618SHugh Dickins * vma from rmap and vmtruncate before freeing its page tables. 1201da177e4SLinus Torvalds */ 121a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds struct file *file = vma->vm_file; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds if (file) { 1261da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 12783cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 1281da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 12983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 1301da177e4SLinus Torvalds } 131a8fb5618SHugh Dickins } 132a8fb5618SHugh Dickins 133a8fb5618SHugh Dickins /* 134763ecb03SLiam R. Howlett * Close a vm structure and free it. 135a8fb5618SHugh Dickins */ 136763ecb03SLiam R. Howlett static void remove_vma(struct vm_area_struct *vma) 137a8fb5618SHugh Dickins { 138a8fb5618SHugh Dickins might_sleep(); 1391da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 1401da177e4SLinus Torvalds vma->vm_ops->close(vma); 141e9714acfSKonstantin Khlebnikov if (vma->vm_file) 142a8fb5618SHugh Dickins fput(vma->vm_file); 143f0be3d32SLee Schermerhorn mpol_put(vma_policy(vma)); 1443928d4f5SLinus Torvalds vm_area_free(vma); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 147b62b633eSLiam R. Howlett static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, 148b62b633eSLiam R. Howlett unsigned long min) 149b62b633eSLiam R. Howlett { 150b62b633eSLiam R. Howlett return mas_prev(&vmi->mas, min); 151b62b633eSLiam R. Howlett } 152b62b633eSLiam R. Howlett 153b62b633eSLiam R. Howlett static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, 154b62b633eSLiam R. Howlett unsigned long start, unsigned long end, gfp_t gfp) 155b62b633eSLiam R. Howlett { 156b62b633eSLiam R. Howlett vmi->mas.index = start; 157b62b633eSLiam R. Howlett vmi->mas.last = end - 1; 158b62b633eSLiam R. Howlett mas_store_gfp(&vmi->mas, NULL, gfp); 159b62b633eSLiam R. Howlett if (unlikely(mas_is_err(&vmi->mas))) 160b62b633eSLiam R. Howlett return -ENOMEM; 161b62b633eSLiam R. Howlett 162b62b633eSLiam R. Howlett return 0; 163b62b633eSLiam R. Howlett } 164b62b633eSLiam R. Howlett 1652e7ce7d3SLiam R. Howlett /* 1662e7ce7d3SLiam R. Howlett * check_brk_limits() - Use platform specific check of range & verify mlock 1672e7ce7d3SLiam R. Howlett * limits. 1682e7ce7d3SLiam R. Howlett * @addr: The address to check 1692e7ce7d3SLiam R. Howlett * @len: The size of increase. 1702e7ce7d3SLiam R. Howlett * 1712e7ce7d3SLiam R. Howlett * Return: 0 on success. 1722e7ce7d3SLiam R. Howlett */ 1732e7ce7d3SLiam R. Howlett static int check_brk_limits(unsigned long addr, unsigned long len) 1742e7ce7d3SLiam R. Howlett { 1752e7ce7d3SLiam R. Howlett unsigned long mapped_addr; 1762e7ce7d3SLiam R. Howlett 1772e7ce7d3SLiam R. Howlett mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 1782e7ce7d3SLiam R. Howlett if (IS_ERR_VALUE(mapped_addr)) 1792e7ce7d3SLiam R. Howlett return mapped_addr; 1802e7ce7d3SLiam R. Howlett 1812e7ce7d3SLiam R. Howlett return mlock_future_check(current->mm, current->mm->def_flags, len); 1822e7ce7d3SLiam R. Howlett } 18392fed820SLiam R. Howlett static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, 184763ecb03SLiam R. Howlett unsigned long addr, unsigned long request, unsigned long flags); 1856a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 1861da177e4SLinus Torvalds { 1879bc8039eSYang Shi unsigned long newbrk, oldbrk, origbrk; 1881da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1892e7ce7d3SLiam R. Howlett struct vm_area_struct *brkvma, *next = NULL; 190a5b4592cSJiri Kosina unsigned long min_brk; 191128557ffSMichel Lespinasse bool populate; 1929bc8039eSYang Shi bool downgraded = false; 193897ab3e0SMike Rapoport LIST_HEAD(uf); 19492fed820SLiam R. Howlett struct vma_iterator vmi; 1951da177e4SLinus Torvalds 196d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 197dc0ef0dfSMichal Hocko return -EINTR; 1981da177e4SLinus Torvalds 1999bc8039eSYang Shi origbrk = mm->brk; 2009bc8039eSYang Shi 201a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 2025520e894SJiri Kosina /* 2035520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 2045520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 2055520e894SJiri Kosina * to be arbitrarily shifted 2065520e894SJiri Kosina */ 2074471a675SJiri Kosina if (current->brk_randomized) 2085520e894SJiri Kosina min_brk = mm->start_brk; 2095520e894SJiri Kosina else 2105520e894SJiri Kosina min_brk = mm->end_data; 211a5b4592cSJiri Kosina #else 212a5b4592cSJiri Kosina min_brk = mm->start_brk; 213a5b4592cSJiri Kosina #endif 214a5b4592cSJiri Kosina if (brk < min_brk) 2151da177e4SLinus Torvalds goto out; 2161e624196SRam Gupta 2171e624196SRam Gupta /* 2181e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2191e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2201e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2211e624196SRam Gupta * not page aligned -Ram Gupta 2221e624196SRam Gupta */ 2238764b338SCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, 2248764b338SCyrill Gorcunov mm->end_data, mm->start_data)) 2251e624196SRam Gupta goto out; 2261e624196SRam Gupta 2271da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2281da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2299bc8039eSYang Shi if (oldbrk == newbrk) { 2309bc8039eSYang Shi mm->brk = brk; 2319bc8039eSYang Shi goto success; 2329bc8039eSYang Shi } 2331da177e4SLinus Torvalds 2349bc8039eSYang Shi /* 2359bc8039eSYang Shi * Always allow shrinking brk. 23627b26701SLiam R. Howlett * do_vma_munmap() may downgrade mmap_lock to read. 2379bc8039eSYang Shi */ 2381da177e4SLinus Torvalds if (brk <= mm->brk) { 2399bc8039eSYang Shi int ret; 2409bc8039eSYang Shi 2412e7ce7d3SLiam R. Howlett /* Search one past newbrk */ 24292fed820SLiam R. Howlett vma_iter_init(&vmi, mm, newbrk); 24392fed820SLiam R. Howlett brkvma = vma_find(&vmi, oldbrk); 244f5ad5083SJason A. Donenfeld if (!brkvma || brkvma->vm_start >= oldbrk) 2452e7ce7d3SLiam R. Howlett goto out; /* mapping intersects with an existing non-brk vma. */ 2469bc8039eSYang Shi /* 2472e7ce7d3SLiam R. Howlett * mm->brk must be protected by write mmap_lock. 24827b26701SLiam R. Howlett * do_vma_munmap() may downgrade the lock, so update it 24927b26701SLiam R. Howlett * before calling do_vma_munmap(). 2509bc8039eSYang Shi */ 2519bc8039eSYang Shi mm->brk = brk; 25227b26701SLiam R. Howlett ret = do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true); 2532e7ce7d3SLiam R. Howlett if (ret == 1) { 2542e7ce7d3SLiam R. Howlett downgraded = true; 2552e7ce7d3SLiam R. Howlett goto success; 2562e7ce7d3SLiam R. Howlett } else if (!ret) 2572e7ce7d3SLiam R. Howlett goto success; 2582e7ce7d3SLiam R. Howlett 2599bc8039eSYang Shi mm->brk = origbrk; 2601da177e4SLinus Torvalds goto out; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2632e7ce7d3SLiam R. Howlett if (check_brk_limits(oldbrk, newbrk - oldbrk)) 2642e7ce7d3SLiam R. Howlett goto out; 2652e7ce7d3SLiam R. Howlett 2662e7ce7d3SLiam R. Howlett /* 2672e7ce7d3SLiam R. Howlett * Only check if the next VMA is within the stack_guard_gap of the 2682e7ce7d3SLiam R. Howlett * expansion area 2692e7ce7d3SLiam R. Howlett */ 27092fed820SLiam R. Howlett vma_iter_init(&vmi, mm, oldbrk); 27192fed820SLiam R. Howlett next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap); 2721be7107fSHugh Dickins if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) 2731da177e4SLinus Torvalds goto out; 2741da177e4SLinus Torvalds 27592fed820SLiam R. Howlett brkvma = vma_prev_limit(&vmi, mm->start_brk); 2761da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 27792fed820SLiam R. Howlett if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) 2781da177e4SLinus Torvalds goto out; 2792e7ce7d3SLiam R. Howlett 2801da177e4SLinus Torvalds mm->brk = brk; 2819bc8039eSYang Shi 2829bc8039eSYang Shi success: 283128557ffSMichel Lespinasse populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; 2849bc8039eSYang Shi if (downgraded) 285d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2869bc8039eSYang Shi else 287d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 288897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 289128557ffSMichel Lespinasse if (populate) 290128557ffSMichel Lespinasse mm_populate(oldbrk, newbrk - oldbrk); 291128557ffSMichel Lespinasse return brk; 292128557ffSMichel Lespinasse 2931da177e4SLinus Torvalds out: 294d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 295b7204006SAdrian Huang return origbrk; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 298d4af56c5SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 299d4af56c5SLiam R. Howlett extern void mt_validate(struct maple_tree *mt); 300d4af56c5SLiam R. Howlett extern void mt_dump(const struct maple_tree *mt); 3011da177e4SLinus Torvalds 302d4af56c5SLiam R. Howlett /* Validate the maple tree */ 303d4af56c5SLiam R. Howlett static void validate_mm_mt(struct mm_struct *mm) 304d4af56c5SLiam R. Howlett { 305d4af56c5SLiam R. Howlett struct maple_tree *mt = &mm->mm_mt; 306763ecb03SLiam R. Howlett struct vm_area_struct *vma_mt; 307d4af56c5SLiam R. Howlett 308d4af56c5SLiam R. Howlett MA_STATE(mas, mt, 0, 0); 309d4af56c5SLiam R. Howlett 310d4af56c5SLiam R. Howlett mt_validate(&mm->mm_mt); 311d4af56c5SLiam R. Howlett mas_for_each(&mas, vma_mt, ULONG_MAX) { 312763ecb03SLiam R. Howlett if ((vma_mt->vm_start != mas.index) || 313763ecb03SLiam R. Howlett (vma_mt->vm_end - 1 != mas.last)) { 314d4af56c5SLiam R. Howlett pr_emerg("issue in %s\n", current->comm); 315d4af56c5SLiam R. Howlett dump_stack(); 316d4af56c5SLiam R. Howlett dump_vma(vma_mt); 317d4af56c5SLiam R. Howlett pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, 318d4af56c5SLiam R. Howlett mas.index, mas.last); 319d4af56c5SLiam R. Howlett pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, 320d4af56c5SLiam R. Howlett vma_mt->vm_start, vma_mt->vm_end); 321d4af56c5SLiam R. Howlett 322d4af56c5SLiam R. Howlett mt_dump(mas.tree); 323d4af56c5SLiam R. Howlett if (vma_mt->vm_end != mas.last + 1) { 324d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", 325d4af56c5SLiam R. Howlett mm, vma_mt->vm_start, vma_mt->vm_end, 326d4af56c5SLiam R. Howlett mas.index, mas.last); 327d4af56c5SLiam R. Howlett mt_dump(mas.tree); 328d4af56c5SLiam R. Howlett } 329d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); 330d4af56c5SLiam R. Howlett if (vma_mt->vm_start != mas.index) { 331d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", 332d4af56c5SLiam R. Howlett mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); 333d4af56c5SLiam R. Howlett mt_dump(mas.tree); 334d4af56c5SLiam R. Howlett } 335d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); 336d4af56c5SLiam R. Howlett } 337d4af56c5SLiam R. Howlett } 338d4af56c5SLiam R. Howlett } 3391da177e4SLinus Torvalds 340eafd4dc4SRashika Kheria static void validate_mm(struct mm_struct *mm) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds int bug = 0; 3431da177e4SLinus Torvalds int i = 0; 344763ecb03SLiam R. Howlett struct vm_area_struct *vma; 345763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 346ff26f70fSAndrew Morton 347524e00b3SLiam R. Howlett validate_mm_mt(mm); 348524e00b3SLiam R. Howlett 349763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 350524e00b3SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 35112352d3cSKonstantin Khlebnikov struct anon_vma *anon_vma = vma->anon_vma; 352ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 353ff26f70fSAndrew Morton 35412352d3cSKonstantin Khlebnikov if (anon_vma) { 35512352d3cSKonstantin Khlebnikov anon_vma_lock_read(anon_vma); 356ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 357ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 35812352d3cSKonstantin Khlebnikov anon_vma_unlock_read(anon_vma); 35912352d3cSKonstantin Khlebnikov } 360524e00b3SLiam R. Howlett #endif 3611da177e4SLinus Torvalds i++; 3621da177e4SLinus Torvalds } 3635a0768f6SMichel Lespinasse if (i != mm->map_count) { 364763ecb03SLiam R. Howlett pr_emerg("map_count %d mas_for_each %d\n", mm->map_count, i); 3655a0768f6SMichel Lespinasse bug = 1; 3665a0768f6SMichel Lespinasse } 36796dad67fSSasha Levin VM_BUG_ON_MM(bug, mm); 3681da177e4SLinus Torvalds } 369524e00b3SLiam R. Howlett 370524e00b3SLiam R. Howlett #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ 371d4af56c5SLiam R. Howlett #define validate_mm_mt(root) do { } while (0) 3721da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 373524e00b3SLiam R. Howlett #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 374d3737187SMichel Lespinasse 375bf181b9fSMichel Lespinasse /* 376bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 377bf181b9fSMichel Lespinasse * anon_vma's interval trees. 378bf181b9fSMichel Lespinasse * 379bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 380bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 381bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 382bf181b9fSMichel Lespinasse * 383bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 384bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 385bf181b9fSMichel Lespinasse * 386c1e8d7c6SMichel Lespinasse * The entire update must be protected by exclusive mmap_lock and by 387bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 388bf181b9fSMichel Lespinasse */ 389bf181b9fSMichel Lespinasse static inline void 390bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 391bf181b9fSMichel Lespinasse { 392bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 393bf181b9fSMichel Lespinasse 394bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 395bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 396bf181b9fSMichel Lespinasse } 397bf181b9fSMichel Lespinasse 398bf181b9fSMichel Lespinasse static inline void 399bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 400bf181b9fSMichel Lespinasse { 401bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 402bf181b9fSMichel Lespinasse 403bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 404bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 405bf181b9fSMichel Lespinasse } 406bf181b9fSMichel Lespinasse 407e8420a8eSCyril Hrubis static unsigned long count_vma_pages_range(struct mm_struct *mm, 408e8420a8eSCyril Hrubis unsigned long addr, unsigned long end) 409e8420a8eSCyril Hrubis { 4102e3af1dbSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, addr); 411e8420a8eSCyril Hrubis struct vm_area_struct *vma; 4122e3af1dbSMatthew Wilcox (Oracle) unsigned long nr_pages = 0; 413e8420a8eSCyril Hrubis 4142e3af1dbSMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) { 4152e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_start = max(addr, vma->vm_start); 4162e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_end = min(end, vma->vm_end); 417e8420a8eSCyril Hrubis 4182e3af1dbSMatthew Wilcox (Oracle) nr_pages += PHYS_PFN(vm_end - vm_start); 419e8420a8eSCyril Hrubis } 420e8420a8eSCyril Hrubis 421e8420a8eSCyril Hrubis return nr_pages; 422e8420a8eSCyril Hrubis } 423e8420a8eSCyril Hrubis 424c154124fSLiam R. Howlett static void __vma_link_file(struct vm_area_struct *vma, 425c154124fSLiam R. Howlett struct address_space *mapping) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 428cf508b58SMiaohe Lin mapping_allow_writable(mapping); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 4316b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 4321da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 435763ecb03SLiam R. Howlett static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) 4361da177e4SLinus Torvalds { 43779e4f2caSLiam R. Howlett VMA_ITERATOR(vmi, mm, 0); 4381da177e4SLinus Torvalds struct address_space *mapping = NULL; 4391da177e4SLinus Torvalds 44079e4f2caSLiam R. Howlett if (vma_iter_prealloc(&vmi)) 441d4af56c5SLiam R. Howlett return -ENOMEM; 442d4af56c5SLiam R. Howlett 44364ac4940SHuang Shijie if (vma->vm_file) { 4441da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 44583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 44664ac4940SHuang Shijie } 4471da177e4SLinus Torvalds 44879e4f2caSLiam R. Howlett vma_iter_store(&vmi, vma); 4491da177e4SLinus Torvalds 450c154124fSLiam R. Howlett if (mapping) { 451c154124fSLiam R. Howlett __vma_link_file(vma, mapping); 45283cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 453c154124fSLiam R. Howlett } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds mm->map_count++; 4561da177e4SLinus Torvalds validate_mm(mm); 457d4af56c5SLiam R. Howlett return 0; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /* 46168cefec5SLiam R. Howlett * init_multi_vma_prep() - Initializer for struct vma_prepare 46268cefec5SLiam R. Howlett * @vp: The vma_prepare struct 46368cefec5SLiam R. Howlett * @vma: The vma that will be altered once locked 46468cefec5SLiam R. Howlett * @next: The next vma if it is to be adjusted 46568cefec5SLiam R. Howlett * @remove: The first vma to be removed 46668cefec5SLiam R. Howlett * @remove2: The second vma to be removed 46768cefec5SLiam R. Howlett */ 46868cefec5SLiam R. Howlett static inline void init_multi_vma_prep(struct vma_prepare *vp, 46968cefec5SLiam R. Howlett struct vm_area_struct *vma, struct vm_area_struct *next, 47068cefec5SLiam R. Howlett struct vm_area_struct *remove, struct vm_area_struct *remove2) 47168cefec5SLiam R. Howlett { 47268cefec5SLiam R. Howlett memset(vp, 0, sizeof(struct vma_prepare)); 47368cefec5SLiam R. Howlett vp->vma = vma; 47468cefec5SLiam R. Howlett vp->anon_vma = vma->anon_vma; 47568cefec5SLiam R. Howlett vp->remove = remove; 47668cefec5SLiam R. Howlett vp->remove2 = remove2; 47768cefec5SLiam R. Howlett vp->adj_next = next; 47868cefec5SLiam R. Howlett if (!vp->anon_vma && next) 47968cefec5SLiam R. Howlett vp->anon_vma = next->anon_vma; 48068cefec5SLiam R. Howlett 48168cefec5SLiam R. Howlett vp->file = vma->vm_file; 48268cefec5SLiam R. Howlett if (vp->file) 48368cefec5SLiam R. Howlett vp->mapping = vma->vm_file->f_mapping; 48468cefec5SLiam R. Howlett 48568cefec5SLiam R. Howlett } 48668cefec5SLiam R. Howlett 48768cefec5SLiam R. Howlett /* 48868cefec5SLiam R. Howlett * init_vma_prep() - Initializer wrapper for vma_prepare struct 48968cefec5SLiam R. Howlett * @vp: The vma_prepare struct 49068cefec5SLiam R. Howlett * @vma: The vma that will be altered once locked 49168cefec5SLiam R. Howlett */ 49268cefec5SLiam R. Howlett static inline void init_vma_prep(struct vma_prepare *vp, 49368cefec5SLiam R. Howlett struct vm_area_struct *vma) 49468cefec5SLiam R. Howlett { 49568cefec5SLiam R. Howlett init_multi_vma_prep(vp, vma, NULL, NULL, NULL); 49668cefec5SLiam R. Howlett } 49768cefec5SLiam R. Howlett 49868cefec5SLiam R. Howlett 49968cefec5SLiam R. Howlett /* 500440703e0SLiam R. Howlett * vma_prepare() - Helper function for handling locking VMAs prior to altering 501440703e0SLiam R. Howlett * @vp: The initialized vma_prepare struct 502440703e0SLiam R. Howlett */ 503440703e0SLiam R. Howlett static inline void vma_prepare(struct vma_prepare *vp) 504440703e0SLiam R. Howlett { 505440703e0SLiam R. Howlett if (vp->file) { 506440703e0SLiam R. Howlett uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); 507440703e0SLiam R. Howlett 508440703e0SLiam R. Howlett if (vp->adj_next) 509440703e0SLiam R. Howlett uprobe_munmap(vp->adj_next, vp->adj_next->vm_start, 510440703e0SLiam R. Howlett vp->adj_next->vm_end); 511440703e0SLiam R. Howlett 512440703e0SLiam R. Howlett i_mmap_lock_write(vp->mapping); 513440703e0SLiam R. Howlett if (vp->insert && vp->insert->vm_file) { 514440703e0SLiam R. Howlett /* 515440703e0SLiam R. Howlett * Put into interval tree now, so instantiated pages 516440703e0SLiam R. Howlett * are visible to arm/parisc __flush_dcache_page 517440703e0SLiam R. Howlett * throughout; but we cannot insert into address 518440703e0SLiam R. Howlett * space until vma start or end is updated. 519440703e0SLiam R. Howlett */ 520440703e0SLiam R. Howlett __vma_link_file(vp->insert, 521440703e0SLiam R. Howlett vp->insert->vm_file->f_mapping); 522440703e0SLiam R. Howlett } 523440703e0SLiam R. Howlett } 524440703e0SLiam R. Howlett 525440703e0SLiam R. Howlett if (vp->anon_vma) { 526440703e0SLiam R. Howlett anon_vma_lock_write(vp->anon_vma); 527440703e0SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vp->vma); 528440703e0SLiam R. Howlett if (vp->adj_next) 529440703e0SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vp->adj_next); 530440703e0SLiam R. Howlett } 531440703e0SLiam R. Howlett 532440703e0SLiam R. Howlett if (vp->file) { 533440703e0SLiam R. Howlett flush_dcache_mmap_lock(vp->mapping); 534440703e0SLiam R. Howlett vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap); 535440703e0SLiam R. Howlett if (vp->adj_next) 536440703e0SLiam R. Howlett vma_interval_tree_remove(vp->adj_next, 537440703e0SLiam R. Howlett &vp->mapping->i_mmap); 538440703e0SLiam R. Howlett } 539440703e0SLiam R. Howlett 540440703e0SLiam R. Howlett } 541440703e0SLiam R. Howlett 542440703e0SLiam R. Howlett /* 543440703e0SLiam R. Howlett * vma_complete- Helper function for handling the unlocking after altering VMAs, 544440703e0SLiam R. Howlett * or for inserting a VMA. 545440703e0SLiam R. Howlett * 546440703e0SLiam R. Howlett * @vp: The vma_prepare struct 547440703e0SLiam R. Howlett * @vmi: The vma iterator 548440703e0SLiam R. Howlett * @mm: The mm_struct 549440703e0SLiam R. Howlett */ 550440703e0SLiam R. Howlett static inline void vma_complete(struct vma_prepare *vp, 551440703e0SLiam R. Howlett struct vma_iterator *vmi, struct mm_struct *mm) 552440703e0SLiam R. Howlett { 553440703e0SLiam R. Howlett if (vp->file) { 554440703e0SLiam R. Howlett if (vp->adj_next) 555440703e0SLiam R. Howlett vma_interval_tree_insert(vp->adj_next, 556440703e0SLiam R. Howlett &vp->mapping->i_mmap); 557440703e0SLiam R. Howlett vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap); 558440703e0SLiam R. Howlett flush_dcache_mmap_unlock(vp->mapping); 559440703e0SLiam R. Howlett } 560440703e0SLiam R. Howlett 561440703e0SLiam R. Howlett if (vp->remove && vp->file) { 562440703e0SLiam R. Howlett __remove_shared_vm_struct(vp->remove, vp->file, vp->mapping); 563440703e0SLiam R. Howlett if (vp->remove2) 564440703e0SLiam R. Howlett __remove_shared_vm_struct(vp->remove2, vp->file, 565440703e0SLiam R. Howlett vp->mapping); 566440703e0SLiam R. Howlett } else if (vp->insert) { 567440703e0SLiam R. Howlett /* 568440703e0SLiam R. Howlett * split_vma has split insert from vma, and needs 569440703e0SLiam R. Howlett * us to insert it before dropping the locks 570440703e0SLiam R. Howlett * (it may either follow vma or precede it). 571440703e0SLiam R. Howlett */ 572440703e0SLiam R. Howlett vma_iter_store(vmi, vp->insert); 573440703e0SLiam R. Howlett mm->map_count++; 574440703e0SLiam R. Howlett } 575440703e0SLiam R. Howlett 576440703e0SLiam R. Howlett if (vp->anon_vma) { 577440703e0SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vp->vma); 578440703e0SLiam R. Howlett if (vp->adj_next) 579440703e0SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vp->adj_next); 580440703e0SLiam R. Howlett anon_vma_unlock_write(vp->anon_vma); 581440703e0SLiam R. Howlett } 582440703e0SLiam R. Howlett 583440703e0SLiam R. Howlett if (vp->file) { 584440703e0SLiam R. Howlett i_mmap_unlock_write(vp->mapping); 585440703e0SLiam R. Howlett uprobe_mmap(vp->vma); 586440703e0SLiam R. Howlett 587440703e0SLiam R. Howlett if (vp->adj_next) 588440703e0SLiam R. Howlett uprobe_mmap(vp->adj_next); 589440703e0SLiam R. Howlett } 590440703e0SLiam R. Howlett 591440703e0SLiam R. Howlett if (vp->remove) { 592440703e0SLiam R. Howlett again: 593440703e0SLiam R. Howlett if (vp->file) { 594440703e0SLiam R. Howlett uprobe_munmap(vp->remove, vp->remove->vm_start, 595440703e0SLiam R. Howlett vp->remove->vm_end); 596440703e0SLiam R. Howlett fput(vp->file); 597440703e0SLiam R. Howlett } 598440703e0SLiam R. Howlett if (vp->remove->anon_vma) 599440703e0SLiam R. Howlett anon_vma_merge(vp->vma, vp->remove); 600440703e0SLiam R. Howlett mm->map_count--; 601440703e0SLiam R. Howlett mpol_put(vma_policy(vp->remove)); 602440703e0SLiam R. Howlett if (!vp->remove2) 603440703e0SLiam R. Howlett WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end); 604440703e0SLiam R. Howlett vm_area_free(vp->remove); 605440703e0SLiam R. Howlett 606440703e0SLiam R. Howlett /* 607440703e0SLiam R. Howlett * In mprotect's case 6 (see comments on vma_merge), 60868cefec5SLiam R. Howlett * we must remove the one after next as well. 609440703e0SLiam R. Howlett */ 610440703e0SLiam R. Howlett if (vp->remove2) { 611440703e0SLiam R. Howlett vp->remove = vp->remove2; 612440703e0SLiam R. Howlett vp->remove2 = NULL; 613440703e0SLiam R. Howlett goto again; 614440703e0SLiam R. Howlett } 615440703e0SLiam R. Howlett } 616440703e0SLiam R. Howlett if (vp->insert && vp->file) 617440703e0SLiam R. Howlett uprobe_mmap(vp->insert); 618440703e0SLiam R. Howlett } 619440703e0SLiam R. Howlett 620440703e0SLiam R. Howlett /* 6219303d3e1SLiam R. Howlett * vma_expand - Expand an existing VMA 6229303d3e1SLiam R. Howlett * 6239303d3e1SLiam R. Howlett * @vmi: The vma iterator 6249303d3e1SLiam R. Howlett * @vma: The vma to expand 6259303d3e1SLiam R. Howlett * @start: The start of the vma 6269303d3e1SLiam R. Howlett * @end: The exclusive end of the vma 6279303d3e1SLiam R. Howlett * @pgoff: The page offset of vma 6289303d3e1SLiam R. Howlett * @next: The current of next vma. 6299303d3e1SLiam R. Howlett * 6309303d3e1SLiam R. Howlett * Expand @vma to @start and @end. Can expand off the start and end. Will 6319303d3e1SLiam R. Howlett * expand over @next if it's different from @vma and @end == @next->vm_end. 6329303d3e1SLiam R. Howlett * Checking if the @vma can expand and merge with @next needs to be handled by 6339303d3e1SLiam R. Howlett * the caller. 6349303d3e1SLiam R. Howlett * 6359303d3e1SLiam R. Howlett * Returns: 0 on success 6369303d3e1SLiam R. Howlett */ 6377c9813e8SLiam R. Howlett int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, 6389303d3e1SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 6399303d3e1SLiam R. Howlett struct vm_area_struct *next) 6409303d3e1SLiam R. Howlett { 64168cefec5SLiam R. Howlett bool remove_next = false; 6429303d3e1SLiam R. Howlett struct vma_prepare vp; 6439303d3e1SLiam R. Howlett 6449303d3e1SLiam R. Howlett if (next && (vma != next) && (end == next->vm_end)) { 64568cefec5SLiam R. Howlett remove_next = true; 6469303d3e1SLiam R. Howlett if (next->anon_vma && !vma->anon_vma) { 6479303d3e1SLiam R. Howlett int error; 6489303d3e1SLiam R. Howlett 6499303d3e1SLiam R. Howlett vma->anon_vma = next->anon_vma; 6509303d3e1SLiam R. Howlett error = anon_vma_clone(vma, next); 6519303d3e1SLiam R. Howlett if (error) 6529303d3e1SLiam R. Howlett return error; 6539303d3e1SLiam R. Howlett } 6549303d3e1SLiam R. Howlett } 6559303d3e1SLiam R. Howlett 65668cefec5SLiam R. Howlett init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); 6579303d3e1SLiam R. Howlett /* Not merging but overwriting any part of next is not handled. */ 6589303d3e1SLiam R. Howlett VM_WARN_ON(next && !vp.remove && 6599303d3e1SLiam R. Howlett next != vma && end > next->vm_start); 6609303d3e1SLiam R. Howlett /* Only handles expanding */ 6619303d3e1SLiam R. Howlett VM_WARN_ON(vma->vm_start < start || vma->vm_end > end); 6629303d3e1SLiam R. Howlett 6639303d3e1SLiam R. Howlett if (vma_iter_prealloc(vmi)) 6649303d3e1SLiam R. Howlett goto nomem; 6659303d3e1SLiam R. Howlett 6669303d3e1SLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 6679303d3e1SLiam R. Howlett /* VMA iterator points to previous, so set to start if necessary */ 6689303d3e1SLiam R. Howlett if (vma_iter_addr(vmi) != start) 6699303d3e1SLiam R. Howlett vma_iter_set(vmi, start); 6709303d3e1SLiam R. Howlett 6719303d3e1SLiam R. Howlett vma_prepare(&vp); 6729303d3e1SLiam R. Howlett vma->vm_start = start; 6739303d3e1SLiam R. Howlett vma->vm_end = end; 6749303d3e1SLiam R. Howlett vma->vm_pgoff = pgoff; 6759303d3e1SLiam R. Howlett /* Note: mas must be pointing to the expanding VMA */ 6769303d3e1SLiam R. Howlett vma_iter_store(vmi, vma); 6779303d3e1SLiam R. Howlett 6789303d3e1SLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 6799303d3e1SLiam R. Howlett validate_mm(vma->vm_mm); 6809303d3e1SLiam R. Howlett return 0; 6819303d3e1SLiam R. Howlett 6829303d3e1SLiam R. Howlett nomem: 6839303d3e1SLiam R. Howlett return -ENOMEM; 6849303d3e1SLiam R. Howlett } 685*cf51e86dSLiam R. Howlett 686*cf51e86dSLiam R. Howlett /* 687*cf51e86dSLiam R. Howlett * vma_shrink() - Reduce an existing VMAs memory area 688*cf51e86dSLiam R. Howlett * @vmi: The vma iterator 689*cf51e86dSLiam R. Howlett * @vma: The VMA to modify 690*cf51e86dSLiam R. Howlett * @start: The new start 691*cf51e86dSLiam R. Howlett * @end: The new end 692*cf51e86dSLiam R. Howlett * 693*cf51e86dSLiam R. Howlett * Returns: 0 on success, -ENOMEM otherwise 694*cf51e86dSLiam R. Howlett */ 695*cf51e86dSLiam R. Howlett int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, 696*cf51e86dSLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff) 697*cf51e86dSLiam R. Howlett { 698*cf51e86dSLiam R. Howlett struct vma_prepare vp; 699*cf51e86dSLiam R. Howlett 700*cf51e86dSLiam R. Howlett WARN_ON((vma->vm_start != start) && (vma->vm_end != end)); 701*cf51e86dSLiam R. Howlett 702*cf51e86dSLiam R. Howlett if (vma_iter_prealloc(vmi)) 703*cf51e86dSLiam R. Howlett return -ENOMEM; 704*cf51e86dSLiam R. Howlett 705*cf51e86dSLiam R. Howlett init_vma_prep(&vp, vma); 706*cf51e86dSLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 707*cf51e86dSLiam R. Howlett vma_prepare(&vp); 708*cf51e86dSLiam R. Howlett 709*cf51e86dSLiam R. Howlett if (vma->vm_start < start) 710*cf51e86dSLiam R. Howlett vma_iter_clear(vmi, vma->vm_start, start); 711*cf51e86dSLiam R. Howlett 712*cf51e86dSLiam R. Howlett if (vma->vm_end > end) 713*cf51e86dSLiam R. Howlett vma_iter_clear(vmi, end, vma->vm_end); 714*cf51e86dSLiam R. Howlett 715*cf51e86dSLiam R. Howlett vma->vm_start = start; 716*cf51e86dSLiam R. Howlett vma->vm_end = end; 717*cf51e86dSLiam R. Howlett vma->vm_pgoff = pgoff; 718*cf51e86dSLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 719*cf51e86dSLiam R. Howlett validate_mm(vma->vm_mm); 720*cf51e86dSLiam R. Howlett return 0; 721*cf51e86dSLiam R. Howlett } 722*cf51e86dSLiam R. Howlett 7239303d3e1SLiam R. Howlett /* 7241da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 7251da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 7261da177e4SLinus Torvalds * The following helper function should be used when such adjustments 7271da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 7281da177e4SLinus Torvalds * before we drop the necessary locks. 7291da177e4SLinus Torvalds */ 7309e560446SLiam R. Howlett int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, 7319e560446SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 732b2b3b886SLiam R. Howlett struct vm_area_struct *expand) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 73568cefec5SLiam R. Howlett struct vm_area_struct *remove2 = NULL; 73668cefec5SLiam R. Howlett struct vm_area_struct *remove = NULL; 7371cd916d0SAndrew Morton struct vm_area_struct *next = find_vma(mm, vma->vm_end); 738524e00b3SLiam R. Howlett struct vm_area_struct *orig_vma = vma; 7391da177e4SLinus Torvalds struct file *file = vma->vm_file; 740524e00b3SLiam R. Howlett bool vma_changed = false; 7411da177e4SLinus Torvalds long adjust_next = 0; 742734537c9SKirill A. Shutemov struct vm_area_struct *exporter = NULL, *importer = NULL; 743440703e0SLiam R. Howlett struct vma_prepare vma_prep; 744287d97acSLinus Torvalds 745b2b3b886SLiam R. Howlett if (next) { 7461da177e4SLinus Torvalds if (end >= next->vm_end) { 7471da177e4SLinus Torvalds /* 7481da177e4SLinus Torvalds * vma expands, overlapping all the next, and 7491da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 75086d12e47SAndrea Arcangeli * The only other cases that gets here are 751e86f15eeSAndrea Arcangeli * case 1, case 7 and case 8. 752e86f15eeSAndrea Arcangeli */ 753e86f15eeSAndrea Arcangeli if (next == expand) { 754e86f15eeSAndrea Arcangeli /* 755e86f15eeSAndrea Arcangeli * The only case where we don't expand "vma" 756e86f15eeSAndrea Arcangeli * and we expand "next" instead is case 8. 757e86f15eeSAndrea Arcangeli */ 758e86f15eeSAndrea Arcangeli VM_WARN_ON(end != next->vm_end); 759e86f15eeSAndrea Arcangeli /* 76068cefec5SLiam R. Howlett * we're removing "vma" and that to do so we 761e86f15eeSAndrea Arcangeli * swapped "vma" and "next". 762e86f15eeSAndrea Arcangeli */ 763e86f15eeSAndrea Arcangeli VM_WARN_ON(file != next->vm_file); 764e86f15eeSAndrea Arcangeli swap(vma, next); 76568cefec5SLiam R. Howlett remove = next; 766e86f15eeSAndrea Arcangeli } else { 767e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != vma); 768e86f15eeSAndrea Arcangeli /* 76968cefec5SLiam R. Howlett * case 1, 6, 7, remove next. 77068cefec5SLiam R. Howlett * case 6 also removes the one beyond next 7711da177e4SLinus Torvalds */ 77268cefec5SLiam R. Howlett remove = next; 77368cefec5SLiam R. Howlett if (end > next->vm_end) 77468cefec5SLiam R. Howlett remove2 = find_vma(mm, next->vm_end); 775d4af56c5SLiam R. Howlett 77668cefec5SLiam R. Howlett VM_WARN_ON(remove2 != NULL && 77768cefec5SLiam R. Howlett end != remove2->vm_end); 778e86f15eeSAndrea Arcangeli } 779e86f15eeSAndrea Arcangeli 780287d97acSLinus Torvalds exporter = next; 7811da177e4SLinus Torvalds importer = vma; 782734537c9SKirill A. Shutemov 783734537c9SKirill A. Shutemov /* 784734537c9SKirill A. Shutemov * If next doesn't have anon_vma, import from vma after 785734537c9SKirill A. Shutemov * next, if the vma overlaps with it. 786734537c9SKirill A. Shutemov */ 78768cefec5SLiam R. Howlett if (remove2 != NULL && !next->anon_vma) 78868cefec5SLiam R. Howlett exporter = remove2; 789734537c9SKirill A. Shutemov 7901da177e4SLinus Torvalds } else if (end > next->vm_start) { 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * vma expands, overlapping part of the next: 7931da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 7941da177e4SLinus Torvalds */ 795f9d86a60SWei Yang adjust_next = (end - next->vm_start); 796287d97acSLinus Torvalds exporter = next; 7971da177e4SLinus Torvalds importer = vma; 798e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 7991da177e4SLinus Torvalds } else if (end < vma->vm_end) { 8001da177e4SLinus Torvalds /* 8011da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 8021da177e4SLinus Torvalds * split_vma inserting another: so it must be 8031da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 8041da177e4SLinus Torvalds */ 805f9d86a60SWei Yang adjust_next = -(vma->vm_end - end); 806287d97acSLinus Torvalds exporter = vma; 8071da177e4SLinus Torvalds importer = next; 808e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8115beb4930SRik van Riel /* 8125beb4930SRik van Riel * Easily overlooked: when mprotect shifts the boundary, 8135beb4930SRik van Riel * make sure the expanding vma has anon_vma set if the 8145beb4930SRik van Riel * shrinking vma had, to cover any anon pages imported. 8155beb4930SRik van Riel */ 816287d97acSLinus Torvalds if (exporter && exporter->anon_vma && !importer->anon_vma) { 817c4ea95d7SDaniel Forrest int error; 818c4ea95d7SDaniel Forrest 819287d97acSLinus Torvalds importer->anon_vma = exporter->anon_vma; 820b800c91aSKonstantin Khlebnikov error = anon_vma_clone(importer, exporter); 8213fe89b3eSLeon Yu if (error) 822b800c91aSKonstantin Khlebnikov return error; 823b800c91aSKonstantin Khlebnikov } 8245beb4930SRik van Riel } 82537f9f559SKirill A. Shutemov 8269e560446SLiam R. Howlett if (vma_iter_prealloc(vmi)) 827d4af56c5SLiam R. Howlett return -ENOMEM; 828d4af56c5SLiam R. Howlett 829d4af56c5SLiam R. Howlett vma_adjust_trans_huge(orig_vma, start, end, adjust_next); 830682968e0SSrikar Dronamraju 83168cefec5SLiam R. Howlett init_multi_vma_prep(&vma_prep, vma, adjust_next ? next : NULL, remove, 83268cefec5SLiam R. Howlett remove2); 83368cefec5SLiam R. Howlett VM_WARN_ON(vma_prep.anon_vma && adjust_next && next->anon_vma && 83468cefec5SLiam R. Howlett vma_prep.anon_vma != next->anon_vma); 8351da177e4SLinus Torvalds 836440703e0SLiam R. Howlett vma_prepare(&vma_prep); 8371da177e4SLinus Torvalds 838*cf51e86dSLiam R. Howlett if (start < vma->vm_start || end > vma->vm_end) 839524e00b3SLiam R. Howlett vma_changed = true; 840b2b3b886SLiam R. Howlett 841b2b3b886SLiam R. Howlett vma->vm_start = start; 8421da177e4SLinus Torvalds vma->vm_end = end; 843b2b3b886SLiam R. Howlett vma->vm_pgoff = pgoff; 844d4af56c5SLiam R. Howlett 845524e00b3SLiam R. Howlett if (vma_changed) 8469e560446SLiam R. Howlett vma_iter_store(vmi, vma); 847d4af56c5SLiam R. Howlett 8481da177e4SLinus Torvalds if (adjust_next) { 849f9d86a60SWei Yang next->vm_start += adjust_next; 850f9d86a60SWei Yang next->vm_pgoff += adjust_next >> PAGE_SHIFT; 851*cf51e86dSLiam R. Howlett if (adjust_next < 0) { 852*cf51e86dSLiam R. Howlett WARN_ON_ONCE(vma_changed); 8539e560446SLiam R. Howlett vma_iter_store(vmi, next); 8541da177e4SLinus Torvalds } 855*cf51e86dSLiam R. Howlett } 8561da177e4SLinus Torvalds 857440703e0SLiam R. Howlett vma_complete(&vma_prep, vmi, mm); 8589e560446SLiam R. Howlett vma_iter_free(vmi); 8591da177e4SLinus Torvalds validate_mm(mm); 860763ecb03SLiam R. Howlett 8615beb4930SRik van Riel return 0; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* 8651da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 8661da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 8671da177e4SLinus Torvalds */ 8681da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 86919a809afSAndrea Arcangeli struct file *file, unsigned long vm_flags, 8709a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 8715c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 8721da177e4SLinus Torvalds { 87334228d47SCyrill Gorcunov /* 87434228d47SCyrill Gorcunov * VM_SOFTDIRTY should not prevent from VMA merging, if we 87534228d47SCyrill Gorcunov * match the flags but dirty bit -- the caller should mark 87634228d47SCyrill Gorcunov * merged VMA as dirty. If dirty bit won't be excluded from 8778bb4e7a2SWei Yang * comparison, we increase pressure on the memory system forcing 87834228d47SCyrill Gorcunov * the kernel to generate new VMAs when old one could be 87934228d47SCyrill Gorcunov * extended instead. 88034228d47SCyrill Gorcunov */ 88134228d47SCyrill Gorcunov if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 8821da177e4SLinus Torvalds return 0; 8831da177e4SLinus Torvalds if (vma->vm_file != file) 8841da177e4SLinus Torvalds return 0; 8851da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 8861da177e4SLinus Torvalds return 0; 88719a809afSAndrea Arcangeli if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 88819a809afSAndrea Arcangeli return 0; 8895c26f6acSSuren Baghdasaryan if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 8909a10064fSColin Cross return 0; 8911da177e4SLinus Torvalds return 1; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 895965f55deSShaohua Li struct anon_vma *anon_vma2, 896965f55deSShaohua Li struct vm_area_struct *vma) 8971da177e4SLinus Torvalds { 898965f55deSShaohua Li /* 899965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 900965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 901965f55deSShaohua Li */ 902965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 903965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 904965f55deSShaohua Li return 1; 905965f55deSShaohua Li return anon_vma1 == anon_vma2; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds /* 9091da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 9101da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 9111da177e4SLinus Torvalds * 9121da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 9131da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 9141da177e4SLinus Torvalds * 9151da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 91645e55300SPeter Collingbourne * indices (16TB on ia32) because do_mmap() does not permit mmap's which 9171da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 9181da177e4SLinus Torvalds */ 9191da177e4SLinus Torvalds static int 9201da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 92119a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 92219a809afSAndrea Arcangeli pgoff_t vm_pgoff, 9239a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 9245c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 9251da177e4SLinus Torvalds { 9269a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 927965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 9281da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 9291da177e4SLinus Torvalds return 1; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds return 0; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 9361da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 9371da177e4SLinus Torvalds * 9381da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 9391da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds static int 9421da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 94319a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 94419a809afSAndrea Arcangeli pgoff_t vm_pgoff, 9459a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 9465c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 9471da177e4SLinus Torvalds { 9489a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 949965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 9501da177e4SLinus Torvalds pgoff_t vm_pglen; 951d6e93217SLibin vm_pglen = vma_pages(vma); 9521da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 9531da177e4SLinus Torvalds return 1; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds return 0; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds /* 9599a10064fSColin Cross * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 9609a10064fSColin Cross * figure out whether that can be merged with its predecessor or its 9619a10064fSColin Cross * successor. Or both (it neatly fills a hole). 9621da177e4SLinus Torvalds * 9631da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 9641da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 9651da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 9661da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 9671da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 9681da177e4SLinus Torvalds * case has already been eliminated. 9691da177e4SLinus Torvalds * 9701da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 9711da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 9721da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 9731da177e4SLinus Torvalds * 9745d42ab29SWei Yang * AAAA AAAA AAAA 9755d42ab29SWei Yang * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN 9765d42ab29SWei Yang * cannot merge might become might become 9775d42ab29SWei Yang * PPNNNNNNNNNN PPPPPPPPPPNN 9785d42ab29SWei Yang * mmap, brk or case 4 below case 5 below 9795d42ab29SWei Yang * mremap move: 9805d42ab29SWei Yang * AAAA AAAA 9815d42ab29SWei Yang * PPPP NNNN PPPPNNNNXXXX 9825d42ab29SWei Yang * might become might become 9835d42ab29SWei Yang * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 9845d42ab29SWei Yang * PPPPPPPPNNNN 2 or PPPPPPPPXXXX 7 or 9855d42ab29SWei Yang * PPPPNNNNNNNN 3 PPPPXXXXXXXX 8 9861da177e4SLinus Torvalds * 9878bb4e7a2SWei Yang * It is important for case 8 that the vma NNNN overlapping the 988e86f15eeSAndrea Arcangeli * region AAAA is never going to extended over XXXX. Instead XXXX must 989e86f15eeSAndrea Arcangeli * be extended in region AAAA and NNNN must be removed. This way in 990e86f15eeSAndrea Arcangeli * all cases where vma_merge succeeds, the moment vma_adjust drops the 991e86f15eeSAndrea Arcangeli * rmap_locks, the properties of the merged vma will be already 992e86f15eeSAndrea Arcangeli * correct for the whole merged range. Some of those properties like 993e86f15eeSAndrea Arcangeli * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 994e86f15eeSAndrea Arcangeli * be correct for the whole merged range immediately after the 995e86f15eeSAndrea Arcangeli * rmap_locks are released. Otherwise if XXXX would be removed and 996e86f15eeSAndrea Arcangeli * NNNN would be extended over the XXXX range, remove_migration_ptes 997e86f15eeSAndrea Arcangeli * or other rmap walkers (if working on addresses beyond the "end" 998e86f15eeSAndrea Arcangeli * parameter) may establish ptes with the wrong permissions of NNNN 999e86f15eeSAndrea Arcangeli * instead of the right permissions of XXXX. 10001da177e4SLinus Torvalds */ 10019760ebffSLiam R. Howlett struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, 10021da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 10031da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 10041da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 100519a809afSAndrea Arcangeli pgoff_t pgoff, struct mempolicy *policy, 10069a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 10075c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 10109760ebffSLiam R. Howlett struct vm_area_struct *mid, *next, *res = NULL; 1011eef19944SJakub Matěna int err = -1; 1012eef19944SJakub Matěna bool merge_prev = false; 1013eef19944SJakub Matěna bool merge_next = false; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds /* 10161da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 10171da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 10181da177e4SLinus Torvalds */ 10191da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 10201da177e4SLinus Torvalds return NULL; 10211da177e4SLinus Torvalds 1022763ecb03SLiam R. Howlett next = find_vma(mm, prev ? prev->vm_end : 0); 1023eef19944SJakub Matěna mid = next; 1024eef19944SJakub Matěna if (next && next->vm_end == end) /* cases 6, 7, 8 */ 1025763ecb03SLiam R. Howlett next = find_vma(mm, next->vm_end); 10261da177e4SLinus Torvalds 1027e86f15eeSAndrea Arcangeli /* verify some invariant that must be enforced by the caller */ 1028e86f15eeSAndrea Arcangeli VM_WARN_ON(prev && addr <= prev->vm_start); 1029eef19944SJakub Matěna VM_WARN_ON(mid && end > mid->vm_end); 1030e86f15eeSAndrea Arcangeli VM_WARN_ON(addr >= end); 1031e86f15eeSAndrea Arcangeli 1032eef19944SJakub Matěna /* Can we merge the predecessor? */ 10331da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 10341da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 10351da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 103619a809afSAndrea Arcangeli anon_vma, file, pgoff, 10379a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 1038eef19944SJakub Matěna merge_prev = true; 10391da177e4SLinus Torvalds } 1040eef19944SJakub Matěna /* Can we merge the successor? */ 10411da177e4SLinus Torvalds if (next && end == next->vm_start && 10421da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 10431da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 104419a809afSAndrea Arcangeli anon_vma, file, pgoff+pglen, 10459a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 1046eef19944SJakub Matěna merge_next = true; 1047eef19944SJakub Matěna } 1048eef19944SJakub Matěna /* Can we merge both the predecessor and the successor? */ 1049eef19944SJakub Matěna if (merge_prev && merge_next && 1050eef19944SJakub Matěna is_mergeable_anon_vma(prev->anon_vma, 1051eef19944SJakub Matěna next->anon_vma, NULL)) { /* cases 1, 6 */ 10529e560446SLiam R. Howlett err = __vma_adjust(vmi, prev, prev->vm_start, 1053b2b3b886SLiam R. Howlett next->vm_end, prev->vm_pgoff, prev); 1054eef19944SJakub Matěna res = prev; 1055eef19944SJakub Matěna } else if (merge_prev) { /* cases 2, 5, 7 */ 10569e560446SLiam R. Howlett err = __vma_adjust(vmi, prev, prev->vm_start, 1057b2b3b886SLiam R. Howlett end, prev->vm_pgoff, prev); 1058eef19944SJakub Matěna res = prev; 1059eef19944SJakub Matěna } else if (merge_next) { 10601da177e4SLinus Torvalds if (prev && addr < prev->vm_end) /* case 4 */ 10619e560446SLiam R. Howlett err = __vma_adjust(vmi, prev, prev->vm_start, 1062b2b3b886SLiam R. Howlett addr, prev->vm_pgoff, next); 1063eef19944SJakub Matěna else /* cases 3, 8 */ 10649e560446SLiam R. Howlett err = __vma_adjust(vmi, mid, addr, next->vm_end, 1065b2b3b886SLiam R. Howlett next->vm_pgoff - pglen, next); 1066eef19944SJakub Matěna res = next; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 1069eef19944SJakub Matěna /* 1070eef19944SJakub Matěna * Cannot merge with predecessor or successor or error in __vma_adjust? 1071eef19944SJakub Matěna */ 1072eef19944SJakub Matěna if (err) 10731da177e4SLinus Torvalds return NULL; 1074eef19944SJakub Matěna khugepaged_enter_vma(res, vm_flags); 10751da177e4SLinus Torvalds 10769760ebffSLiam R. Howlett if (res) 1077f2ebfe43SLiam R. Howlett vma_iter_set(vmi, end); 1078f2ebfe43SLiam R. Howlett 10799760ebffSLiam R. Howlett return res; 1080f2ebfe43SLiam R. Howlett } 1081f2ebfe43SLiam R. Howlett 10821da177e4SLinus Torvalds /* 1083b4f315b4SEthon Paul * Rough compatibility check to quickly see if it's even worth looking 1084d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1085d0e9fe17SLinus Torvalds * 1086d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1087d0e9fe17SLinus Torvalds * in things that mprotect may change. 1088d0e9fe17SLinus Torvalds * 1089d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1090d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1091d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1092d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1093d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1094d0e9fe17SLinus Torvalds */ 1095d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1096d0e9fe17SLinus Torvalds { 1097d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1098d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1099d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 11006cb4d9a2SAnshuman Khandual !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1101d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1102d0e9fe17SLinus Torvalds } 1103d0e9fe17SLinus Torvalds 1104d0e9fe17SLinus Torvalds /* 1105d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1106d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1107d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1108d0e9fe17SLinus Torvalds * to share the anon_vma. 1109d0e9fe17SLinus Torvalds * 11105b449489SFlorian Rommel * NOTE! This runs with mmap_lock held for reading, so it is possible that 1111d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1112d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1113d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1114d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 11154db0c3c2SJason Low * we do that READ_ONCE() to make sure that we never re-load the pointer. 1116d0e9fe17SLinus Torvalds * 1117d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1118d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1119d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1120d0e9fe17SLinus Torvalds * a fork). 1121d0e9fe17SLinus Torvalds * 1122d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1123d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 11245b449489SFlorian Rommel * a read lock on the mmap_lock. 1125d0e9fe17SLinus Torvalds */ 1126d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1127d0e9fe17SLinus Torvalds { 1128d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 11294db0c3c2SJason Low struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1130d0e9fe17SLinus Torvalds 1131d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1132d0e9fe17SLinus Torvalds return anon_vma; 1133d0e9fe17SLinus Torvalds } 1134d0e9fe17SLinus Torvalds return NULL; 1135d0e9fe17SLinus Torvalds } 1136d0e9fe17SLinus Torvalds 1137d0e9fe17SLinus Torvalds /* 11381da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 11391da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 11401da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 11411da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 11421da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 11431da177e4SLinus Torvalds * mprotect. 11441da177e4SLinus Torvalds */ 11451da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 11461da177e4SLinus Torvalds { 1147763ecb03SLiam R. Howlett MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); 1148a67c8caaSMiaohe Lin struct anon_vma *anon_vma = NULL; 1149763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next; 11501da177e4SLinus Torvalds 1151a67c8caaSMiaohe Lin /* Try next first. */ 1152763ecb03SLiam R. Howlett next = mas_walk(&mas); 1153763ecb03SLiam R. Howlett if (next) { 1154763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(next, vma, next); 1155d0e9fe17SLinus Torvalds if (anon_vma) 1156d0e9fe17SLinus Torvalds return anon_vma; 1157a67c8caaSMiaohe Lin } 11581da177e4SLinus Torvalds 1159763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1160763ecb03SLiam R. Howlett VM_BUG_ON_VMA(prev != vma, vma); 1161763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1162a67c8caaSMiaohe Lin /* Try prev next. */ 1163763ecb03SLiam R. Howlett if (prev) 1164763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(prev, prev, vma); 1165a67c8caaSMiaohe Lin 11661da177e4SLinus Torvalds /* 1167a67c8caaSMiaohe Lin * We might reach here with anon_vma == NULL if we can't find 1168a67c8caaSMiaohe Lin * any reusable anon_vma. 11691da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 11701da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 11711da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 11721da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 11731da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 11741da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 11751da177e4SLinus Torvalds */ 1176a67c8caaSMiaohe Lin return anon_vma; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds /* 118040401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 118140401530SAl Viro * low as possible but still greater than mmap_min_addr 118240401530SAl Viro */ 118340401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 118440401530SAl Viro { 118540401530SAl Viro hint &= PAGE_MASK; 118640401530SAl Viro if (((void *)hint != NULL) && 118740401530SAl Viro (hint < mmap_min_addr)) 118840401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 118940401530SAl Viro return hint; 119040401530SAl Viro } 119140401530SAl Viro 11926aeb2542SMike Rapoport int mlock_future_check(struct mm_struct *mm, unsigned long flags, 1193363ee17fSDavidlohr Bueso unsigned long len) 1194363ee17fSDavidlohr Bueso { 1195363ee17fSDavidlohr Bueso unsigned long locked, lock_limit; 1196363ee17fSDavidlohr Bueso 1197363ee17fSDavidlohr Bueso /* mlock MCL_FUTURE? */ 1198363ee17fSDavidlohr Bueso if (flags & VM_LOCKED) { 1199363ee17fSDavidlohr Bueso locked = len >> PAGE_SHIFT; 1200363ee17fSDavidlohr Bueso locked += mm->locked_vm; 1201363ee17fSDavidlohr Bueso lock_limit = rlimit(RLIMIT_MEMLOCK); 1202363ee17fSDavidlohr Bueso lock_limit >>= PAGE_SHIFT; 1203363ee17fSDavidlohr Bueso if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 1204363ee17fSDavidlohr Bueso return -EAGAIN; 1205363ee17fSDavidlohr Bueso } 1206363ee17fSDavidlohr Bueso return 0; 1207363ee17fSDavidlohr Bueso } 1208363ee17fSDavidlohr Bueso 1209be83bbf8SLinus Torvalds static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) 1210be83bbf8SLinus Torvalds { 1211be83bbf8SLinus Torvalds if (S_ISREG(inode->i_mode)) 1212423913adSLinus Torvalds return MAX_LFS_FILESIZE; 1213be83bbf8SLinus Torvalds 1214be83bbf8SLinus Torvalds if (S_ISBLK(inode->i_mode)) 1215be83bbf8SLinus Torvalds return MAX_LFS_FILESIZE; 1216be83bbf8SLinus Torvalds 121776f34950SIvan Khoronzhuk if (S_ISSOCK(inode->i_mode)) 121876f34950SIvan Khoronzhuk return MAX_LFS_FILESIZE; 121976f34950SIvan Khoronzhuk 1220be83bbf8SLinus Torvalds /* Special "we do even unsigned file positions" case */ 1221be83bbf8SLinus Torvalds if (file->f_mode & FMODE_UNSIGNED_OFFSET) 1222be83bbf8SLinus Torvalds return 0; 1223be83bbf8SLinus Torvalds 1224be83bbf8SLinus Torvalds /* Yes, random drivers might want more. But I'm tired of buggy drivers */ 1225be83bbf8SLinus Torvalds return ULONG_MAX; 1226be83bbf8SLinus Torvalds } 1227be83bbf8SLinus Torvalds 1228be83bbf8SLinus Torvalds static inline bool file_mmap_ok(struct file *file, struct inode *inode, 1229be83bbf8SLinus Torvalds unsigned long pgoff, unsigned long len) 1230be83bbf8SLinus Torvalds { 1231be83bbf8SLinus Torvalds u64 maxsize = file_mmap_size_max(file, inode); 1232be83bbf8SLinus Torvalds 1233be83bbf8SLinus Torvalds if (maxsize && len > maxsize) 1234be83bbf8SLinus Torvalds return false; 1235be83bbf8SLinus Torvalds maxsize -= len; 1236be83bbf8SLinus Torvalds if (pgoff > maxsize >> PAGE_SHIFT) 1237be83bbf8SLinus Torvalds return false; 1238be83bbf8SLinus Torvalds return true; 1239be83bbf8SLinus Torvalds } 1240be83bbf8SLinus Torvalds 124140401530SAl Viro /* 12423e4e28c5SMichel Lespinasse * The caller must write-lock current->mm->mmap_lock. 12431da177e4SLinus Torvalds */ 12441fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, unsigned long addr, 12451da177e4SLinus Torvalds unsigned long len, unsigned long prot, 124645e55300SPeter Collingbourne unsigned long flags, unsigned long pgoff, 124745e55300SPeter Collingbourne unsigned long *populate, struct list_head *uf) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 125045e55300SPeter Collingbourne vm_flags_t vm_flags; 125162b5f7d0SDave Hansen int pkey = 0; 12521da177e4SLinus Torvalds 1253524e00b3SLiam R. Howlett validate_mm(mm); 125441badc15SMichel Lespinasse *populate = 0; 1255bebeb3d6SMichel Lespinasse 1256e37609bbSPiotr Kwapulinski if (!len) 1257e37609bbSPiotr Kwapulinski return -EINVAL; 1258e37609bbSPiotr Kwapulinski 12591da177e4SLinus Torvalds /* 12601da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 12611da177e4SLinus Torvalds * 12621da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 12631da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 12641da177e4SLinus Torvalds */ 12651da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 126690f8572bSEric W. Biederman if (!(file && path_noexec(&file->f_path))) 12671da177e4SLinus Torvalds prot |= PROT_EXEC; 12681da177e4SLinus Torvalds 1269a4ff8e86SMichal Hocko /* force arch specific MAP_FIXED handling in get_unmapped_area */ 1270a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) 1271a4ff8e86SMichal Hocko flags |= MAP_FIXED; 1272a4ff8e86SMichal Hocko 12737cd94146SEric Paris if (!(flags & MAP_FIXED)) 12747cd94146SEric Paris addr = round_hint_to_min(addr); 12757cd94146SEric Paris 12761da177e4SLinus Torvalds /* Careful about overflows.. */ 12771da177e4SLinus Torvalds len = PAGE_ALIGN(len); 12789206de95SAl Viro if (!len) 12791da177e4SLinus Torvalds return -ENOMEM; 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds /* offset overflow? */ 12821da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 12831da177e4SLinus Torvalds return -EOVERFLOW; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* Too many mappings? */ 12861da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 12871da177e4SLinus Torvalds return -ENOMEM; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 12901da177e4SLinus Torvalds * that it represents a valid section of the address space. 12911da177e4SLinus Torvalds */ 12921da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 1293ff68dac6SGaowei Pu if (IS_ERR_VALUE(addr)) 12941da177e4SLinus Torvalds return addr; 12951da177e4SLinus Torvalds 1296a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) { 129735e43c5fSLiam Howlett if (find_vma_intersection(mm, addr, addr + len)) 1298a4ff8e86SMichal Hocko return -EEXIST; 1299a4ff8e86SMichal Hocko } 1300a4ff8e86SMichal Hocko 130162b5f7d0SDave Hansen if (prot == PROT_EXEC) { 130262b5f7d0SDave Hansen pkey = execute_only_pkey(mm); 130362b5f7d0SDave Hansen if (pkey < 0) 130462b5f7d0SDave Hansen pkey = 0; 130562b5f7d0SDave Hansen } 130662b5f7d0SDave Hansen 13071da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 13081da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 13091da177e4SLinus Torvalds * of the memory object, so we don't do any here. 13101da177e4SLinus Torvalds */ 131145e55300SPeter Collingbourne vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | 13121da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 13131da177e4SLinus Torvalds 1314cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 13151da177e4SLinus Torvalds if (!can_do_mlock()) 13161da177e4SLinus Torvalds return -EPERM; 1317ba470de4SRik van Riel 1318363ee17fSDavidlohr Bueso if (mlock_future_check(mm, vm_flags, len)) 13191da177e4SLinus Torvalds return -EAGAIN; 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds if (file) { 1322077bf22bSOleg Nesterov struct inode *inode = file_inode(file); 13231c972597SDan Williams unsigned long flags_mask; 13241c972597SDan Williams 1325be83bbf8SLinus Torvalds if (!file_mmap_ok(file, inode, pgoff, len)) 1326be83bbf8SLinus Torvalds return -EOVERFLOW; 1327be83bbf8SLinus Torvalds 13281c972597SDan Williams flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; 1329077bf22bSOleg Nesterov 13301da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 13311da177e4SLinus Torvalds case MAP_SHARED: 13321c972597SDan Williams /* 13331c972597SDan Williams * Force use of MAP_SHARED_VALIDATE with non-legacy 13341c972597SDan Williams * flags. E.g. MAP_SYNC is dangerous to use with 13351c972597SDan Williams * MAP_SHARED as you don't know which consistency model 13361c972597SDan Williams * you will get. We silently ignore unsupported flags 13371c972597SDan Williams * with MAP_SHARED to preserve backward compatibility. 13381c972597SDan Williams */ 13391c972597SDan Williams flags &= LEGACY_MAP_MASK; 1340e4a9bc58SJoe Perches fallthrough; 13411c972597SDan Williams case MAP_SHARED_VALIDATE: 13421c972597SDan Williams if (flags & ~flags_mask) 13431c972597SDan Williams return -EOPNOTSUPP; 1344dc617f29SDarrick J. Wong if (prot & PROT_WRITE) { 1345dc617f29SDarrick J. Wong if (!(file->f_mode & FMODE_WRITE)) 13461da177e4SLinus Torvalds return -EACCES; 1347dc617f29SDarrick J. Wong if (IS_SWAPFILE(file->f_mapping->host)) 1348dc617f29SDarrick J. Wong return -ETXTBSY; 1349dc617f29SDarrick J. Wong } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /* 13521da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 13531da177e4SLinus Torvalds * file.. 13541da177e4SLinus Torvalds */ 13551da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 13561da177e4SLinus Torvalds return -EACCES; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13591da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 13601da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 1361e4a9bc58SJoe Perches fallthrough; 13621da177e4SLinus Torvalds case MAP_PRIVATE: 13631da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 13641da177e4SLinus Torvalds return -EACCES; 136590f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 136680c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 136780c5606cSLinus Torvalds return -EPERM; 136880c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 136980c5606cSLinus Torvalds } 137080c5606cSLinus Torvalds 137172c2d531SAl Viro if (!file->f_op->mmap) 137280c5606cSLinus Torvalds return -ENODEV; 1373b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1374b2c56e4fSOleg Nesterov return -EINVAL; 13751da177e4SLinus Torvalds break; 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds default: 13781da177e4SLinus Torvalds return -EINVAL; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds } else { 13811da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 13821da177e4SLinus Torvalds case MAP_SHARED: 1383b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1384b2c56e4fSOleg Nesterov return -EINVAL; 1385ce363942STejun Heo /* 1386ce363942STejun Heo * Ignore pgoff. 1387ce363942STejun Heo */ 1388ce363942STejun Heo pgoff = 0; 13891da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13901da177e4SLinus Torvalds break; 13911da177e4SLinus Torvalds case MAP_PRIVATE: 13921da177e4SLinus Torvalds /* 13931da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 13941da177e4SLinus Torvalds */ 13951da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 13961da177e4SLinus Torvalds break; 13971da177e4SLinus Torvalds default: 13981da177e4SLinus Torvalds return -EINVAL; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds 1402c22c0d63SMichel Lespinasse /* 1403c22c0d63SMichel Lespinasse * Set 'VM_NORESERVE' if we should not account for the 1404c22c0d63SMichel Lespinasse * memory use of this mapping. 1405c22c0d63SMichel Lespinasse */ 1406c22c0d63SMichel Lespinasse if (flags & MAP_NORESERVE) { 1407c22c0d63SMichel Lespinasse /* We honor MAP_NORESERVE if allowed to overcommit */ 1408c22c0d63SMichel Lespinasse if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1409c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1410c22c0d63SMichel Lespinasse 1411c22c0d63SMichel Lespinasse /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 1412c22c0d63SMichel Lespinasse if (file && is_file_hugepages(file)) 1413c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1414c22c0d63SMichel Lespinasse } 1415c22c0d63SMichel Lespinasse 1416897ab3e0SMike Rapoport addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); 141709a9f1d2SMichel Lespinasse if (!IS_ERR_VALUE(addr) && 141809a9f1d2SMichel Lespinasse ((vm_flags & VM_LOCKED) || 141909a9f1d2SMichel Lespinasse (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) 142041badc15SMichel Lespinasse *populate = len; 1421bebeb3d6SMichel Lespinasse return addr; 14220165ab44SMiklos Szeredi } 14236be5ceb0SLinus Torvalds 1424a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1425a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1426a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 142766f0dc48SHugh Dickins { 142866f0dc48SHugh Dickins struct file *file = NULL; 14291e3ee14bSChen Gang unsigned long retval; 143066f0dc48SHugh Dickins 143166f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1432120a795dSAl Viro audit_mmap_fd(fd, flags); 143366f0dc48SHugh Dickins file = fget(fd); 143466f0dc48SHugh Dickins if (!file) 14351e3ee14bSChen Gang return -EBADF; 14367bba8f0eSZhen Lei if (is_file_hugepages(file)) { 1437af73e4d9SNaoya Horiguchi len = ALIGN(len, huge_page_size(hstate_file(file))); 14387bba8f0eSZhen Lei } else if (unlikely(flags & MAP_HUGETLB)) { 1439493af578SJörn Engel retval = -EINVAL; 1440493af578SJörn Engel goto out_fput; 14417bba8f0eSZhen Lei } 144266f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 1443c103a4dcSAndrew Morton struct hstate *hs; 1444af73e4d9SNaoya Horiguchi 144520ac2893SAnshuman Khandual hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 1446091d0d55SLi Zefan if (!hs) 1447091d0d55SLi Zefan return -EINVAL; 1448091d0d55SLi Zefan 1449091d0d55SLi Zefan len = ALIGN(len, huge_page_size(hs)); 145066f0dc48SHugh Dickins /* 145166f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 145266f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 145366f0dc48SHugh Dickins */ 1454af73e4d9SNaoya Horiguchi file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, 145542d7395fSAndi Kleen VM_NORESERVE, 145683c1fd76Szhangyiru HUGETLB_ANONHUGE_INODE, 145742d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 145866f0dc48SHugh Dickins if (IS_ERR(file)) 145966f0dc48SHugh Dickins return PTR_ERR(file); 146066f0dc48SHugh Dickins } 146166f0dc48SHugh Dickins 14629fbeb5abSMichal Hocko retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1463493af578SJörn Engel out_fput: 146466f0dc48SHugh Dickins if (file) 146566f0dc48SHugh Dickins fput(file); 146666f0dc48SHugh Dickins return retval; 146766f0dc48SHugh Dickins } 146866f0dc48SHugh Dickins 1469a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1470a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1471a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1472a90f590aSDominik Brodowski { 1473a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1474a90f590aSDominik Brodowski } 1475a90f590aSDominik Brodowski 1476a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1477a4679373SChristoph Hellwig struct mmap_arg_struct { 1478a4679373SChristoph Hellwig unsigned long addr; 1479a4679373SChristoph Hellwig unsigned long len; 1480a4679373SChristoph Hellwig unsigned long prot; 1481a4679373SChristoph Hellwig unsigned long flags; 1482a4679373SChristoph Hellwig unsigned long fd; 1483a4679373SChristoph Hellwig unsigned long offset; 1484a4679373SChristoph Hellwig }; 1485a4679373SChristoph Hellwig 1486a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1487a4679373SChristoph Hellwig { 1488a4679373SChristoph Hellwig struct mmap_arg_struct a; 1489a4679373SChristoph Hellwig 1490a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1491a4679373SChristoph Hellwig return -EFAULT; 1492de1741a1SAlexander Kuleshov if (offset_in_page(a.offset)) 1493a4679373SChristoph Hellwig return -EINVAL; 1494a4679373SChristoph Hellwig 1495a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1496a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1497a4679373SChristoph Hellwig } 1498a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1499a4679373SChristoph Hellwig 15004e950f6fSAlexey Dobriyan /* 15018bb4e7a2SWei Yang * Some shared mappings will want the pages marked read-only 15024e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 15034e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 15044e950f6fSAlexey Dobriyan * VM_SHARED bit). 15054e950f6fSAlexey Dobriyan */ 15066d2329f8SAndrea Arcangeli int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 15074e950f6fSAlexey Dobriyan { 1508ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 15098a04446aSKirill A. Shutemov const struct vm_operations_struct *vm_ops = vma->vm_ops; 15104e950f6fSAlexey Dobriyan 15114e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 15124e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 15134e950f6fSAlexey Dobriyan return 0; 15144e950f6fSAlexey Dobriyan 15154e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 15168a04446aSKirill A. Shutemov if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) 15174e950f6fSAlexey Dobriyan return 1; 15184e950f6fSAlexey Dobriyan 151964e45507SPeter Feiner /* The open routine did something to the protections that pgprot_modify 152064e45507SPeter Feiner * won't preserve? */ 15216d2329f8SAndrea Arcangeli if (pgprot_val(vm_page_prot) != 15226d2329f8SAndrea Arcangeli pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) 15234e950f6fSAlexey Dobriyan return 0; 15244e950f6fSAlexey Dobriyan 1525f96f7a40SDavid Hildenbrand /* 1526f96f7a40SDavid Hildenbrand * Do we need to track softdirty? hugetlb does not support softdirty 1527f96f7a40SDavid Hildenbrand * tracking yet. 1528f96f7a40SDavid Hildenbrand */ 1529f96f7a40SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 153064e45507SPeter Feiner return 1; 153164e45507SPeter Feiner 153251d3d5ebSDavid Hildenbrand /* Do we need write faults for uffd-wp tracking? */ 153351d3d5ebSDavid Hildenbrand if (userfaultfd_wp(vma)) 153451d3d5ebSDavid Hildenbrand return 1; 153551d3d5ebSDavid Hildenbrand 15364e950f6fSAlexey Dobriyan /* Specialty mapping? */ 15374b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 15384e950f6fSAlexey Dobriyan return 0; 15394e950f6fSAlexey Dobriyan 15404e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 15414e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 1542f56753acSChristoph Hellwig mapping_can_writeback(vma->vm_file->f_mapping); 15434e950f6fSAlexey Dobriyan } 15444e950f6fSAlexey Dobriyan 1545fc8744adSLinus Torvalds /* 1546fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 15475a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1548fc8744adSLinus Torvalds */ 1549ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1550fc8744adSLinus Torvalds { 15515a6fe125SMel Gorman /* 15525a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 15535a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 15545a6fe125SMel Gorman */ 15555a6fe125SMel Gorman if (file && is_file_hugepages(file)) 15565a6fe125SMel Gorman return 0; 15575a6fe125SMel Gorman 1558fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1559fc8744adSLinus Torvalds } 1560fc8744adSLinus Torvalds 15613499a131SLiam R. Howlett /** 15623499a131SLiam R. Howlett * unmapped_area() - Find an area between the low_limit and the high_limit with 15633499a131SLiam R. Howlett * the correct alignment and offset, all from @info. Note: current->mm is used 15643499a131SLiam R. Howlett * for the search. 15653499a131SLiam R. Howlett * 156682b24936SVernon Yang * @info: The unmapped area information including the range [low_limit - 156782b24936SVernon Yang * high_limit), the alignment offset and mask. 15683499a131SLiam R. Howlett * 15693499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 15703499a131SLiam R. Howlett */ 1571baceaf1cSJaewon Kim static unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1572db4fbfb9SMichel Lespinasse { 15733499a131SLiam R. Howlett unsigned long length, gap; 1574db4fbfb9SMichel Lespinasse 15753499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1576db4fbfb9SMichel Lespinasse 1577db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1578db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1579db4fbfb9SMichel Lespinasse if (length < info->length) 1580db4fbfb9SMichel Lespinasse return -ENOMEM; 1581db4fbfb9SMichel Lespinasse 15823499a131SLiam R. Howlett if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, 15833499a131SLiam R. Howlett length)) 15843499a131SLiam R. Howlett return -ENOMEM; 15853499a131SLiam R. Howlett 1586d4af56c5SLiam R. Howlett gap = mas.index; 1587d4af56c5SLiam R. Howlett gap += (info->align_offset - gap) & info->align_mask; 15883499a131SLiam R. Howlett return gap; 1589db4fbfb9SMichel Lespinasse } 1590db4fbfb9SMichel Lespinasse 15913499a131SLiam R. Howlett /** 15923499a131SLiam R. Howlett * unmapped_area_topdown() - Find an area between the low_limit and the 159382b24936SVernon Yang * high_limit with the correct alignment and offset at the highest available 15943499a131SLiam R. Howlett * address, all from @info. Note: current->mm is used for the search. 15953499a131SLiam R. Howlett * 159682b24936SVernon Yang * @info: The unmapped area information including the range [low_limit - 159782b24936SVernon Yang * high_limit), the alignment offset and mask. 15983499a131SLiam R. Howlett * 15993499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 16003499a131SLiam R. Howlett */ 1601baceaf1cSJaewon Kim static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 1602db4fbfb9SMichel Lespinasse { 16033499a131SLiam R. Howlett unsigned long length, gap; 1604d4af56c5SLiam R. Howlett 16053499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1606db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1607db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1608db4fbfb9SMichel Lespinasse if (length < info->length) 1609db4fbfb9SMichel Lespinasse return -ENOMEM; 1610db4fbfb9SMichel Lespinasse 16113499a131SLiam R. Howlett if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, 16123499a131SLiam R. Howlett length)) 16133499a131SLiam R. Howlett return -ENOMEM; 16143499a131SLiam R. Howlett 1615d4af56c5SLiam R. Howlett gap = mas.last + 1 - info->length; 1616d4af56c5SLiam R. Howlett gap -= (gap - info->align_offset) & info->align_mask; 16173499a131SLiam R. Howlett return gap; 1618db4fbfb9SMichel Lespinasse } 1619db4fbfb9SMichel Lespinasse 1620baceaf1cSJaewon Kim /* 1621baceaf1cSJaewon Kim * Search for an unmapped address range. 1622baceaf1cSJaewon Kim * 1623baceaf1cSJaewon Kim * We are looking for a range that: 1624baceaf1cSJaewon Kim * - does not intersect with any VMA; 1625baceaf1cSJaewon Kim * - is contained within the [low_limit, high_limit) interval; 1626baceaf1cSJaewon Kim * - is at least the desired size. 1627baceaf1cSJaewon Kim * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) 1628baceaf1cSJaewon Kim */ 1629baceaf1cSJaewon Kim unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) 1630baceaf1cSJaewon Kim { 1631df529cabSJaewon Kim unsigned long addr; 1632df529cabSJaewon Kim 1633baceaf1cSJaewon Kim if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) 1634df529cabSJaewon Kim addr = unmapped_area_topdown(info); 1635baceaf1cSJaewon Kim else 1636df529cabSJaewon Kim addr = unmapped_area(info); 1637df529cabSJaewon Kim 1638df529cabSJaewon Kim trace_vm_unmapped_area(addr, info); 1639df529cabSJaewon Kim return addr; 1640baceaf1cSJaewon Kim } 1641f6795053SSteve Capper 16421da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 16431da177e4SLinus Torvalds * For shmat() with addr=0. 16441da177e4SLinus Torvalds * 16451da177e4SLinus Torvalds * Ugly calling convention alert: 16461da177e4SLinus Torvalds * Return value with the low bits set means error value, 16471da177e4SLinus Torvalds * ie 16481da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 16491da177e4SLinus Torvalds * error = ret; 16501da177e4SLinus Torvalds * 16511da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 16521da177e4SLinus Torvalds */ 16531da177e4SLinus Torvalds unsigned long 16544b439e25SChristophe Leroy generic_get_unmapped_area(struct file *filp, unsigned long addr, 16554b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16564b439e25SChristophe Leroy unsigned long flags) 16571da177e4SLinus Torvalds { 16581da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 16591be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 1660db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 16612cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 16621da177e4SLinus Torvalds 1663f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 16641da177e4SLinus Torvalds return -ENOMEM; 16651da177e4SLinus Torvalds 166606abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 166706abdfb4SBenjamin Herrenschmidt return addr; 166806abdfb4SBenjamin Herrenschmidt 16691da177e4SLinus Torvalds if (addr) { 16701da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 16711be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1672f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 16731be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 16741be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 16751da177e4SLinus Torvalds return addr; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 1678db4fbfb9SMichel Lespinasse info.flags = 0; 1679db4fbfb9SMichel Lespinasse info.length = len; 16804e99b021SHeiko Carstens info.low_limit = mm->mmap_base; 1681f6795053SSteve Capper info.high_limit = mmap_end; 1682db4fbfb9SMichel Lespinasse info.align_mask = 0; 168309ef5283SJaewon Kim info.align_offset = 0; 1684db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 16851da177e4SLinus Torvalds } 16864b439e25SChristophe Leroy 16874b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA 16884b439e25SChristophe Leroy unsigned long 16894b439e25SChristophe Leroy arch_get_unmapped_area(struct file *filp, unsigned long addr, 16904b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16914b439e25SChristophe Leroy unsigned long flags) 16924b439e25SChristophe Leroy { 16934b439e25SChristophe Leroy return generic_get_unmapped_area(filp, addr, len, pgoff, flags); 16944b439e25SChristophe Leroy } 16951da177e4SLinus Torvalds #endif 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds /* 16981da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 16991da177e4SLinus Torvalds * stack's low limit (the base): 17001da177e4SLinus Torvalds */ 17011da177e4SLinus Torvalds unsigned long 17024b439e25SChristophe Leroy generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 170343cca0b1SYang Fan unsigned long len, unsigned long pgoff, 170443cca0b1SYang Fan unsigned long flags) 17051da177e4SLinus Torvalds { 17061be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 17071da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1708db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 17092cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds /* requested length too big for entire address space */ 1712f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 17131da177e4SLinus Torvalds return -ENOMEM; 17141da177e4SLinus Torvalds 171506abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 171606abdfb4SBenjamin Herrenschmidt return addr; 171706abdfb4SBenjamin Herrenschmidt 17181da177e4SLinus Torvalds /* requesting a specific address */ 17191da177e4SLinus Torvalds if (addr) { 17201da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 17211be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1722f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 17231be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 17241be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 17251da177e4SLinus Torvalds return addr; 17261da177e4SLinus Torvalds } 17271da177e4SLinus Torvalds 1728db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 1729db4fbfb9SMichel Lespinasse info.length = len; 17302afc745fSAkira Takeuchi info.low_limit = max(PAGE_SIZE, mmap_min_addr); 1731f6795053SSteve Capper info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); 1732db4fbfb9SMichel Lespinasse info.align_mask = 0; 173309ef5283SJaewon Kim info.align_offset = 0; 1734db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1735b716ad95SXiao Guangrong 17361da177e4SLinus Torvalds /* 17371da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 17381da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 17391da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 17401da177e4SLinus Torvalds * allocations. 17411da177e4SLinus Torvalds */ 1742de1741a1SAlexander Kuleshov if (offset_in_page(addr)) { 1743db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 1744db4fbfb9SMichel Lespinasse info.flags = 0; 1745db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1746f6795053SSteve Capper info.high_limit = mmap_end; 1747db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1748db4fbfb9SMichel Lespinasse } 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds return addr; 17511da177e4SLinus Torvalds } 17524b439e25SChristophe Leroy 17534b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 17544b439e25SChristophe Leroy unsigned long 17554b439e25SChristophe Leroy arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 17564b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 17574b439e25SChristophe Leroy unsigned long flags) 17584b439e25SChristophe Leroy { 17594b439e25SChristophe Leroy return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); 17604b439e25SChristophe Leroy } 17611da177e4SLinus Torvalds #endif 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds unsigned long 17641da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 17651da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 17661da177e4SLinus Torvalds { 176706abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 176806abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 176907ab67c8SLinus Torvalds 17709206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 17719206de95SAl Viro if (error) 17729206de95SAl Viro return error; 17739206de95SAl Viro 17749206de95SAl Viro /* Careful about overflows.. */ 17759206de95SAl Viro if (len > TASK_SIZE) 17769206de95SAl Viro return -ENOMEM; 17779206de95SAl Viro 177807ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 1779c01d5b30SHugh Dickins if (file) { 1780c01d5b30SHugh Dickins if (file->f_op->get_unmapped_area) 178107ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 1782c01d5b30SHugh Dickins } else if (flags & MAP_SHARED) { 1783c01d5b30SHugh Dickins /* 1784c01d5b30SHugh Dickins * mmap_region() will call shmem_zero_setup() to create a file, 1785c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge. 178645e55300SPeter Collingbourne * do_mmap() will clear pgoff, so match alignment. 1787c01d5b30SHugh Dickins */ 1788c01d5b30SHugh Dickins pgoff = 0; 1789c01d5b30SHugh Dickins get_area = shmem_get_unmapped_area; 1790c01d5b30SHugh Dickins } 1791c01d5b30SHugh Dickins 179207ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 179307ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 179407ab67c8SLinus Torvalds return addr; 179507ab67c8SLinus Torvalds 17961da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 17971da177e4SLinus Torvalds return -ENOMEM; 1798de1741a1SAlexander Kuleshov if (offset_in_page(addr)) 17991da177e4SLinus Torvalds return -EINVAL; 180006abdfb4SBenjamin Herrenschmidt 18019ac4ed4bSAl Viro error = security_mmap_addr(addr); 18029ac4ed4bSAl Viro return error ? error : addr; 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 18061da177e4SLinus Torvalds 1807be8432e7SLiam R. Howlett /** 1808abdba2ddSLiam R. Howlett * find_vma_intersection() - Look up the first VMA which intersects the interval 1809abdba2ddSLiam R. Howlett * @mm: The process address space. 1810abdba2ddSLiam R. Howlett * @start_addr: The inclusive start user address. 1811abdba2ddSLiam R. Howlett * @end_addr: The exclusive end user address. 1812abdba2ddSLiam R. Howlett * 1813abdba2ddSLiam R. Howlett * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 1814abdba2ddSLiam R. Howlett * start_addr < end_addr. 1815abdba2ddSLiam R. Howlett */ 1816abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 1817abdba2ddSLiam R. Howlett unsigned long start_addr, 1818abdba2ddSLiam R. Howlett unsigned long end_addr) 1819abdba2ddSLiam R. Howlett { 1820abdba2ddSLiam R. Howlett unsigned long index = start_addr; 1821abdba2ddSLiam R. Howlett 1822abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 18237964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 1824abdba2ddSLiam R. Howlett } 1825abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 1826abdba2ddSLiam R. Howlett 1827abdba2ddSLiam R. Howlett /** 1828be8432e7SLiam R. Howlett * find_vma() - Find the VMA for a given address, or the next VMA. 1829be8432e7SLiam R. Howlett * @mm: The mm_struct to check 1830be8432e7SLiam R. Howlett * @addr: The address 1831be8432e7SLiam R. Howlett * 1832be8432e7SLiam R. Howlett * Returns: The VMA associated with addr, or the next VMA. 1833be8432e7SLiam R. Howlett * May return %NULL in the case of no VMA at addr or above. 1834be8432e7SLiam R. Howlett */ 18351da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 18361da177e4SLinus Torvalds { 1837be8432e7SLiam R. Howlett unsigned long index = addr; 18381da177e4SLinus Torvalds 18395b78ed24SLuigi Rizzo mmap_assert_locked(mm); 18407964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, ULONG_MAX); 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 18431da177e4SLinus Torvalds 18447fdbd37dSLiam R. Howlett /** 18457fdbd37dSLiam R. Howlett * find_vma_prev() - Find the VMA for a given address, or the next vma and 18467fdbd37dSLiam R. Howlett * set %pprev to the previous VMA, if any. 18477fdbd37dSLiam R. Howlett * @mm: The mm_struct to check 18487fdbd37dSLiam R. Howlett * @addr: The address 18497fdbd37dSLiam R. Howlett * @pprev: The pointer to set to the previous VMA 18507fdbd37dSLiam R. Howlett * 18517fdbd37dSLiam R. Howlett * Note that RCU lock is missing here since the external mmap_lock() is used 18527fdbd37dSLiam R. Howlett * instead. 18537fdbd37dSLiam R. Howlett * 18547fdbd37dSLiam R. Howlett * Returns: The VMA associated with @addr, or the next vma. 18557fdbd37dSLiam R. Howlett * May return %NULL in the case of no vma at addr or above. 18566bd4837dSKOSAKI Motohiro */ 18571da177e4SLinus Torvalds struct vm_area_struct * 18581da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 18591da177e4SLinus Torvalds struct vm_area_struct **pprev) 18601da177e4SLinus Torvalds { 18616bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 18627fdbd37dSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 18631da177e4SLinus Torvalds 18647fdbd37dSLiam R. Howlett vma = mas_walk(&mas); 18657fdbd37dSLiam R. Howlett *pprev = mas_prev(&mas, 0); 18667fdbd37dSLiam R. Howlett if (!vma) 18677fdbd37dSLiam R. Howlett vma = mas_next(&mas, ULONG_MAX); 18686bd4837dSKOSAKI Motohiro return vma; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds /* 18721da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 18731da177e4SLinus Torvalds * update accounting. This is shared with both the 18741da177e4SLinus Torvalds * grow-up and grow-down cases. 18751da177e4SLinus Torvalds */ 18761be7107fSHugh Dickins static int acct_stack_growth(struct vm_area_struct *vma, 18771be7107fSHugh Dickins unsigned long size, unsigned long grow) 18781da177e4SLinus Torvalds { 18791da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 18801be7107fSHugh Dickins unsigned long new_start; 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* address space limit tests */ 188384638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, grow)) 18841da177e4SLinus Torvalds return -ENOMEM; 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds /* Stack limit test */ 188724c79d8eSKrzysztof Opasiak if (size > rlimit(RLIMIT_STACK)) 18881da177e4SLinus Torvalds return -ENOMEM; 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds /* mlock limit tests */ 1891c5d8a364SMiaohe Lin if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) 18921da177e4SLinus Torvalds return -ENOMEM; 18931da177e4SLinus Torvalds 18940d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 18950d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 18960d59a01bSAdam Litke vma->vm_end - size; 18970d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 18980d59a01bSAdam Litke return -EFAULT; 18990d59a01bSAdam Litke 19001da177e4SLinus Torvalds /* 19011da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 19021da177e4SLinus Torvalds * update security statistics. 19031da177e4SLinus Torvalds */ 190405fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 19051da177e4SLinus Torvalds return -ENOMEM; 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds return 0; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 191046dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 19111da177e4SLinus Torvalds /* 191246dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 191346dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 19141da177e4SLinus Torvalds */ 191546dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 19161da177e4SLinus Torvalds { 191709357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 19181be7107fSHugh Dickins struct vm_area_struct *next; 19191be7107fSHugh Dickins unsigned long gap_addr; 192012352d3cSKonstantin Khlebnikov int error = 0; 1921d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 19241da177e4SLinus Torvalds return -EFAULT; 19251da177e4SLinus Torvalds 1926bd726c90SHelge Deller /* Guard against exceeding limits of the address space. */ 19271be7107fSHugh Dickins address &= PAGE_MASK; 192837511fb5SHelge Deller if (address >= (TASK_SIZE & PAGE_MASK)) 192912352d3cSKonstantin Khlebnikov return -ENOMEM; 1930bd726c90SHelge Deller address += PAGE_SIZE; 193112352d3cSKonstantin Khlebnikov 19321be7107fSHugh Dickins /* Enforce stack_guard_gap */ 19331be7107fSHugh Dickins gap_addr = address + stack_guard_gap; 1934bd726c90SHelge Deller 1935bd726c90SHelge Deller /* Guard against overflow */ 1936bd726c90SHelge Deller if (gap_addr < address || gap_addr > TASK_SIZE) 1937bd726c90SHelge Deller gap_addr = TASK_SIZE; 1938bd726c90SHelge Deller 1939763ecb03SLiam R. Howlett next = find_vma_intersection(mm, vma->vm_end, gap_addr); 1940763ecb03SLiam R. Howlett if (next && vma_is_accessible(next)) { 19411be7107fSHugh Dickins if (!(next->vm_flags & VM_GROWSUP)) 19421be7107fSHugh Dickins return -ENOMEM; 19431be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 19441be7107fSHugh Dickins } 19451be7107fSHugh Dickins 1946c5d5546eSVernon Yang if (mas_preallocate(&mas, GFP_KERNEL)) 19471da177e4SLinus Torvalds return -ENOMEM; 19481da177e4SLinus Torvalds 1949d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 1950d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 1951d4af56c5SLiam R. Howlett mas_destroy(&mas); 1952d4af56c5SLiam R. Howlett return -ENOMEM; 1953d4af56c5SLiam R. Howlett } 1954d4af56c5SLiam R. Howlett 19551da177e4SLinus Torvalds /* 19561da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 1957c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 19581da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 19591da177e4SLinus Torvalds */ 196012352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 19611da177e4SLinus Torvalds 19621da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 19631da177e4SLinus Torvalds if (address > vma->vm_end) { 19641da177e4SLinus Torvalds unsigned long size, grow; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds size = address - vma->vm_start; 19671da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 19681da177e4SLinus Torvalds 196942c36f63SHugh Dickins error = -ENOMEM; 197042c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 19711da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 19723af9e859SEric B Munson if (!error) { 19734128997bSMichel Lespinasse /* 1974524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 1975524e00b3SLiam R. Howlett * we need to protect against concurrent vma 1976524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 1977524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 1978524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 1979524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 1980524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 19814128997bSMichel Lespinasse */ 198209357814SOleg Nesterov spin_lock(&mm->page_table_lock); 198387e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 198409357814SOleg Nesterov mm->locked_vm += grow; 198584638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 1986bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 19871da177e4SLinus Torvalds vma->vm_end = address; 1988d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 1989fbcc3104SLiam R. Howlett mas_set_range(&mas, vma->vm_start, address - 1); 1990fbcc3104SLiam R. Howlett mas_store_prealloc(&mas, vma); 1991bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 199209357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 19934128997bSMichel Lespinasse 19943af9e859SEric B Munson perf_event_mmap(vma); 19953af9e859SEric B Munson } 19961da177e4SLinus Torvalds } 199742c36f63SHugh Dickins } 199812352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 1999c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2000d4af56c5SLiam R. Howlett mas_destroy(&mas); 20011da177e4SLinus Torvalds return error; 20021da177e4SLinus Torvalds } 200346dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 200446dea3d0SHugh Dickins 20051da177e4SLinus Torvalds /* 20061da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 20071da177e4SLinus Torvalds */ 2008524e00b3SLiam R. Howlett int expand_downwards(struct vm_area_struct *vma, unsigned long address) 20091da177e4SLinus Torvalds { 201009357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 2011763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); 20121be7107fSHugh Dickins struct vm_area_struct *prev; 20130a1d5299SJann Horn int error = 0; 20141da177e4SLinus Torvalds 20158869477aSEric Paris address &= PAGE_MASK; 20160a1d5299SJann Horn if (address < mmap_min_addr) 20170a1d5299SJann Horn return -EPERM; 20188869477aSEric Paris 20191be7107fSHugh Dickins /* Enforce stack_guard_gap */ 2020763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 20211be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 202232e4e6d5SOleg Nesterov if (prev && !(prev->vm_flags & VM_GROWSDOWN) && 20233122e80eSAnshuman Khandual vma_is_accessible(prev)) { 202432e4e6d5SOleg Nesterov if (address - prev->vm_end < stack_guard_gap) 202532e4e6d5SOleg Nesterov return -ENOMEM; 20261be7107fSHugh Dickins } 20271be7107fSHugh Dickins 2028c5d5546eSVernon Yang if (mas_preallocate(&mas, GFP_KERNEL)) 202912352d3cSKonstantin Khlebnikov return -ENOMEM; 20301da177e4SLinus Torvalds 2031d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2032d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2033d4af56c5SLiam R. Howlett mas_destroy(&mas); 2034d4af56c5SLiam R. Howlett return -ENOMEM; 2035d4af56c5SLiam R. Howlett } 2036d4af56c5SLiam R. Howlett 20371da177e4SLinus Torvalds /* 20381da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2039c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 20401da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 20411da177e4SLinus Torvalds */ 204212352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 20451da177e4SLinus Torvalds if (address < vma->vm_start) { 20461da177e4SLinus Torvalds unsigned long size, grow; 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds size = vma->vm_end - address; 20491da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 20501da177e4SLinus Torvalds 2051a626ca6aSLinus Torvalds error = -ENOMEM; 2052a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 20531da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 20541da177e4SLinus Torvalds if (!error) { 20554128997bSMichel Lespinasse /* 2056524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2057524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2058524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2059524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2060524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2061524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2062524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 20634128997bSMichel Lespinasse */ 206409357814SOleg Nesterov spin_lock(&mm->page_table_lock); 206587e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 206609357814SOleg Nesterov mm->locked_vm += grow; 206784638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2068bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 20691da177e4SLinus Torvalds vma->vm_start = address; 20701da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2071d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2072fbcc3104SLiam R. Howlett mas_set_range(&mas, address, vma->vm_end - 1); 2073fbcc3104SLiam R. Howlett mas_store_prealloc(&mas, vma); 2074bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 207509357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 20764128997bSMichel Lespinasse 20773af9e859SEric B Munson perf_event_mmap(vma); 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds } 2080a626ca6aSLinus Torvalds } 208112352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2082c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2083d4af56c5SLiam R. Howlett mas_destroy(&mas); 20841da177e4SLinus Torvalds return error; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871be7107fSHugh Dickins /* enforced gap between the expanding stack and other mappings. */ 20881be7107fSHugh Dickins unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; 20891be7107fSHugh Dickins 20901be7107fSHugh Dickins static int __init cmdline_parse_stack_guard_gap(char *p) 20911be7107fSHugh Dickins { 20921be7107fSHugh Dickins unsigned long val; 20931be7107fSHugh Dickins char *endptr; 20941be7107fSHugh Dickins 20951be7107fSHugh Dickins val = simple_strtoul(p, &endptr, 10); 20961be7107fSHugh Dickins if (!*endptr) 20971be7107fSHugh Dickins stack_guard_gap = val << PAGE_SHIFT; 20981be7107fSHugh Dickins 2099e6d09493SRandy Dunlap return 1; 21001be7107fSHugh Dickins } 21011be7107fSHugh Dickins __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); 21021be7107fSHugh Dickins 2103b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2104b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2105b6a2fea3SOllie Wild { 2106b6a2fea3SOllie Wild return expand_upwards(vma, address); 2107b6a2fea3SOllie Wild } 2108b6a2fea3SOllie Wild 2109b6a2fea3SOllie Wild struct vm_area_struct * 2110b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2111b6a2fea3SOllie Wild { 2112b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2113b6a2fea3SOllie Wild 2114b6a2fea3SOllie Wild addr &= PAGE_MASK; 2115b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2116b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2117b6a2fea3SOllie Wild return vma; 21184d45e75aSJann Horn if (!prev || expand_stack(prev, addr)) 2119b6a2fea3SOllie Wild return NULL; 2120cea10a19SMichel Lespinasse if (prev->vm_flags & VM_LOCKED) 2121fc05f566SKirill A. Shutemov populate_vma_page_range(prev, addr, prev->vm_end, NULL); 2122b6a2fea3SOllie Wild return prev; 2123b6a2fea3SOllie Wild } 2124b6a2fea3SOllie Wild #else 2125b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2126b6a2fea3SOllie Wild { 2127b6a2fea3SOllie Wild return expand_downwards(vma, address); 2128b6a2fea3SOllie Wild } 2129b6a2fea3SOllie Wild 21301da177e4SLinus Torvalds struct vm_area_struct * 21311da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 21321da177e4SLinus Torvalds { 21331da177e4SLinus Torvalds struct vm_area_struct *vma; 21341da177e4SLinus Torvalds unsigned long start; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds addr &= PAGE_MASK; 21371da177e4SLinus Torvalds vma = find_vma(mm, addr); 21381da177e4SLinus Torvalds if (!vma) 21391da177e4SLinus Torvalds return NULL; 21401da177e4SLinus Torvalds if (vma->vm_start <= addr) 21411da177e4SLinus Torvalds return vma; 21421da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 21431da177e4SLinus Torvalds return NULL; 21441da177e4SLinus Torvalds start = vma->vm_start; 21451da177e4SLinus Torvalds if (expand_stack(vma, addr)) 21461da177e4SLinus Torvalds return NULL; 2147cea10a19SMichel Lespinasse if (vma->vm_flags & VM_LOCKED) 2148fc05f566SKirill A. Shutemov populate_vma_page_range(vma, addr, start, NULL); 21491da177e4SLinus Torvalds return vma; 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds #endif 21521da177e4SLinus Torvalds 2153e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(find_extend_vma); 2154e1d6d01aSJesse Barnes 21552c0b3814SHugh Dickins /* 2156763ecb03SLiam R. Howlett * Ok - we have the memory areas we should free on a maple tree so release them, 2157763ecb03SLiam R. Howlett * and do the vma updates. 21581da177e4SLinus Torvalds * 21592c0b3814SHugh Dickins * Called with the mm semaphore held. 21601da177e4SLinus Torvalds */ 2161763ecb03SLiam R. Howlett static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) 21621da177e4SLinus Torvalds { 21634f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 2164763ecb03SLiam R. Howlett struct vm_area_struct *vma; 21654f74d2c8SLinus Torvalds 2166365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2167365e9c87SHugh Dickins update_hiwater_vm(mm); 2168763ecb03SLiam R. Howlett mas_for_each(mas, vma, ULONG_MAX) { 2169ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 21701da177e4SLinus Torvalds 21714f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 21724f74d2c8SLinus Torvalds nr_accounted += nrpages; 217384638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, -nrpages); 2174763ecb03SLiam R. Howlett remove_vma(vma); 2175763ecb03SLiam R. Howlett } 21764f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 21771da177e4SLinus Torvalds validate_mm(mm); 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds /* 21811da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 21821da177e4SLinus Torvalds * 2183f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 21841da177e4SLinus Torvalds */ 2185763ecb03SLiam R. Howlett static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, 2186e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2187763ecb03SLiam R. Howlett struct vm_area_struct *next, 2188e0da382cSHugh Dickins unsigned long start, unsigned long end) 21891da177e4SLinus Torvalds { 2190d16dfc55SPeter Zijlstra struct mmu_gather tlb; 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds lru_add_drain(); 2193a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm); 2194365e9c87SHugh Dickins update_hiwater_rss(mm); 2195763ecb03SLiam R. Howlett unmap_vmas(&tlb, mt, vma, start, end); 2196763ecb03SLiam R. Howlett free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 21976ee8630eSHugh Dickins next ? next->vm_start : USER_PGTABLES_CEILING); 2198ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds /* 2202def5efe0SDavid Rientjes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 2203def5efe0SDavid Rientjes * has already been checked or doesn't make sense to fail. 22040fd5a9e2SLiam R. Howlett * VMA Iterator will point to the end VMA. 22051da177e4SLinus Torvalds */ 22069760ebffSLiam R. Howlett int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 22071da177e4SLinus Torvalds unsigned long addr, int new_below) 22081da177e4SLinus Torvalds { 2209b2b3b886SLiam R. Howlett struct vma_prepare vp; 22101da177e4SLinus Torvalds struct vm_area_struct *new; 2211e3975891SChen Gang int err; 22129760ebffSLiam R. Howlett 22139760ebffSLiam R. Howlett validate_mm_mt(vma->vm_mm); 22141da177e4SLinus Torvalds 2215b2b3b886SLiam R. Howlett WARN_ON(vma->vm_start >= addr); 2216b2b3b886SLiam R. Howlett WARN_ON(vma->vm_end <= addr); 2217b2b3b886SLiam R. Howlett 2218dd3b614fSDmitry Safonov if (vma->vm_ops && vma->vm_ops->may_split) { 2219dd3b614fSDmitry Safonov err = vma->vm_ops->may_split(vma, addr); 222031383c68SDan Williams if (err) 222131383c68SDan Williams return err; 222231383c68SDan Williams } 22231da177e4SLinus Torvalds 22243928d4f5SLinus Torvalds new = vm_area_dup(vma); 22251da177e4SLinus Torvalds if (!new) 2226e3975891SChen Gang return -ENOMEM; 22271da177e4SLinus Torvalds 2228b2b3b886SLiam R. Howlett err = -ENOMEM; 2229b2b3b886SLiam R. Howlett if (vma_iter_prealloc(vmi)) 2230b2b3b886SLiam R. Howlett goto out_free_vma; 2231b2b3b886SLiam R. Howlett 2232b2b3b886SLiam R. Howlett if (new_below) { 22331da177e4SLinus Torvalds new->vm_end = addr; 2234b2b3b886SLiam R. Howlett } else { 22351da177e4SLinus Torvalds new->vm_start = addr; 22361da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 2239ef0855d3SOleg Nesterov err = vma_dup_policy(vma, new); 2240ef0855d3SOleg Nesterov if (err) 2241b2b3b886SLiam R. Howlett goto out_free_vmi; 22421da177e4SLinus Torvalds 2243c4ea95d7SDaniel Forrest err = anon_vma_clone(new, vma); 2244c4ea95d7SDaniel Forrest if (err) 22455beb4930SRik van Riel goto out_free_mpol; 22465beb4930SRik van Riel 2247e9714acfSKonstantin Khlebnikov if (new->vm_file) 22481da177e4SLinus Torvalds get_file(new->vm_file); 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 22511da177e4SLinus Torvalds new->vm_ops->open(new); 22521da177e4SLinus Torvalds 2253b2b3b886SLiam R. Howlett vma_adjust_trans_huge(vma, vma->vm_start, addr, 0); 2254b2b3b886SLiam R. Howlett init_vma_prep(&vp, vma); 2255b2b3b886SLiam R. Howlett vp.insert = new; 2256b2b3b886SLiam R. Howlett vma_prepare(&vp); 22571da177e4SLinus Torvalds 2258b2b3b886SLiam R. Howlett if (new_below) { 2259b2b3b886SLiam R. Howlett vma->vm_start = addr; 2260b2b3b886SLiam R. Howlett vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT; 2261b2b3b886SLiam R. Howlett } else { 2262b2b3b886SLiam R. Howlett vma->vm_end = addr; 22639760ebffSLiam R. Howlett } 22645beb4930SRik van Riel 2265b2b3b886SLiam R. Howlett /* vma_complete stores the new vma */ 2266b2b3b886SLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 2267b2b3b886SLiam R. Howlett 2268b2b3b886SLiam R. Howlett /* Success. */ 2269b2b3b886SLiam R. Howlett if (new_below) 2270b2b3b886SLiam R. Howlett vma_next(vmi); 2271b2b3b886SLiam R. Howlett validate_mm_mt(vma->vm_mm); 2272b2b3b886SLiam R. Howlett return 0; 2273b2b3b886SLiam R. Howlett 22745beb4930SRik van Riel out_free_mpol: 2275ef0855d3SOleg Nesterov mpol_put(vma_policy(new)); 2276b2b3b886SLiam R. Howlett out_free_vmi: 2277b2b3b886SLiam R. Howlett vma_iter_free(vmi); 22785beb4930SRik van Riel out_free_vma: 22793928d4f5SLinus Torvalds vm_area_free(new); 22809760ebffSLiam R. Howlett validate_mm_mt(vma->vm_mm); 22815beb4930SRik van Riel return err; 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds 2284659ace58SKOSAKI Motohiro /* 2285659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2286659ace58SKOSAKI Motohiro * either for the first part or the tail. 2287659ace58SKOSAKI Motohiro */ 22889760ebffSLiam R. Howlett int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 2289659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2290659ace58SKOSAKI Motohiro { 22919760ebffSLiam R. Howlett if (vma->vm_mm->map_count >= sysctl_max_map_count) 2292659ace58SKOSAKI Motohiro return -ENOMEM; 2293659ace58SKOSAKI Motohiro 22949760ebffSLiam R. Howlett return __split_vma(vmi, vma, addr, new_below); 2295f2ebfe43SLiam R. Howlett } 2296f2ebfe43SLiam R. Howlett 2297763ecb03SLiam R. Howlett static inline int munmap_sidetree(struct vm_area_struct *vma, 2298763ecb03SLiam R. Howlett struct ma_state *mas_detach) 22991da177e4SLinus Torvalds { 2300763ecb03SLiam R. Howlett mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); 2301763ecb03SLiam R. Howlett if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) 2302763ecb03SLiam R. Howlett return -ENOMEM; 230311f9a21aSLiam R. Howlett 2304763ecb03SLiam R. Howlett if (vma->vm_flags & VM_LOCKED) 2305763ecb03SLiam R. Howlett vma->vm_mm->locked_vm -= vma_pages(vma); 230611f9a21aSLiam R. Howlett 2307763ecb03SLiam R. Howlett return 0; 230811f9a21aSLiam R. Howlett } 230911f9a21aSLiam R. Howlett 231011f9a21aSLiam R. Howlett /* 2311183654ceSLiam R. Howlett * do_vmi_align_munmap() - munmap the aligned region from @start to @end. 2312183654ceSLiam R. Howlett * @vmi: The vma iterator 231311f9a21aSLiam R. Howlett * @vma: The starting vm_area_struct 231411f9a21aSLiam R. Howlett * @mm: The mm_struct 231511f9a21aSLiam R. Howlett * @start: The aligned start address to munmap. 231611f9a21aSLiam R. Howlett * @end: The aligned end address to munmap. 231711f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 23188651a137SLorenzo Stoakes * @downgrade: Set to true to attempt a write downgrade of the mmap_lock 231911f9a21aSLiam R. Howlett * 232011f9a21aSLiam R. Howlett * If @downgrade is true, check return code for potential release of the lock. 232111f9a21aSLiam R. Howlett */ 232211f9a21aSLiam R. Howlett static int 2323183654ceSLiam R. Howlett do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 232411f9a21aSLiam R. Howlett struct mm_struct *mm, unsigned long start, 232511f9a21aSLiam R. Howlett unsigned long end, struct list_head *uf, bool downgrade) 232611f9a21aSLiam R. Howlett { 2327763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next = NULL; 2328763ecb03SLiam R. Howlett struct maple_tree mt_detach; 2329763ecb03SLiam R. Howlett int count = 0; 2330d4af56c5SLiam R. Howlett int error = -ENOMEM; 2331763ecb03SLiam R. Howlett MA_STATE(mas_detach, &mt_detach, 0, 0); 2332763ecb03SLiam R. Howlett mt_init_flags(&mt_detach, MT_FLAGS_LOCK_EXTERN); 2333763ecb03SLiam R. Howlett mt_set_external_lock(&mt_detach, &mm->mmap_lock); 2334524e00b3SLiam R. Howlett 23351da177e4SLinus Torvalds /* 23361da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 23371da177e4SLinus Torvalds * 23381da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 23391da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 23401da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 23411da177e4SLinus Torvalds */ 2342763ecb03SLiam R. Howlett 2343763ecb03SLiam R. Howlett /* Does it split the first one? */ 2344146425a3SHugh Dickins if (start > vma->vm_start) { 2345659ace58SKOSAKI Motohiro 2346659ace58SKOSAKI Motohiro /* 2347659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2348659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2349659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2350659ace58SKOSAKI Motohiro */ 2351659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2352d4af56c5SLiam R. Howlett goto map_count_exceeded; 2353659ace58SKOSAKI Motohiro 23549760ebffSLiam R. Howlett error = __split_vma(vmi, vma, start, 0); 23551da177e4SLinus Torvalds if (error) 2356763ecb03SLiam R. Howlett goto start_split_failed; 235711f9a21aSLiam R. Howlett 23580fd5a9e2SLiam R. Howlett vma = vma_iter_load(vmi); 23591da177e4SLinus Torvalds } 23601da177e4SLinus Torvalds 2361183654ceSLiam R. Howlett prev = vma_prev(vmi); 2362763ecb03SLiam R. Howlett if (unlikely((!prev))) 2363183654ceSLiam R. Howlett vma_iter_set(vmi, start); 236411f9a21aSLiam R. Howlett 2365763ecb03SLiam R. Howlett /* 2366763ecb03SLiam R. Howlett * Detach a range of VMAs from the mm. Using next as a temp variable as 2367763ecb03SLiam R. Howlett * it is always overwritten. 2368763ecb03SLiam R. Howlett */ 2369183654ceSLiam R. Howlett for_each_vma_range(*vmi, next, end) { 2370763ecb03SLiam R. Howlett /* Does it split the end? */ 2371763ecb03SLiam R. Howlett if (next->vm_end > end) { 23726b73cff2SLiam R. Howlett error = __split_vma(vmi, next, end, 0); 23731da177e4SLinus Torvalds if (error) 2374763ecb03SLiam R. Howlett goto end_split_failed; 237511f9a21aSLiam R. Howlett } 2376763ecb03SLiam R. Howlett error = munmap_sidetree(next, &mas_detach); 2377763ecb03SLiam R. Howlett if (error) 2378763ecb03SLiam R. Howlett goto munmap_sidetree_failed; 2379763ecb03SLiam R. Howlett 2380763ecb03SLiam R. Howlett count++; 2381763ecb03SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 2382763ecb03SLiam R. Howlett BUG_ON(next->vm_start < start); 2383763ecb03SLiam R. Howlett BUG_ON(next->vm_start > end); 2384763ecb03SLiam R. Howlett #endif 2385763ecb03SLiam R. Howlett } 2386763ecb03SLiam R. Howlett 2387183654ceSLiam R. Howlett next = vma_next(vmi); 23882376dd7cSAndrea Arcangeli if (unlikely(uf)) { 23892376dd7cSAndrea Arcangeli /* 23902376dd7cSAndrea Arcangeli * If userfaultfd_unmap_prep returns an error the vmas 2391f0953a1bSIngo Molnar * will remain split, but userland will get a 23922376dd7cSAndrea Arcangeli * highly unexpected error anyway. This is no 23932376dd7cSAndrea Arcangeli * different than the case where the first of the two 23942376dd7cSAndrea Arcangeli * __split_vma fails, but we don't undo the first 23952376dd7cSAndrea Arcangeli * split, despite we could. This is unlikely enough 23962376dd7cSAndrea Arcangeli * failure that it's not worth optimizing it for. 23972376dd7cSAndrea Arcangeli */ 239869dbe6daSLiam R. Howlett error = userfaultfd_unmap_prep(mm, start, end, uf); 239911f9a21aSLiam R. Howlett 24002376dd7cSAndrea Arcangeli if (error) 2401d4af56c5SLiam R. Howlett goto userfaultfd_error; 24022376dd7cSAndrea Arcangeli } 24032376dd7cSAndrea Arcangeli 2404763ecb03SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 2405763ecb03SLiam R. Howlett /* Make sure no VMAs are about to be lost. */ 2406763ecb03SLiam R. Howlett { 2407763ecb03SLiam R. Howlett MA_STATE(test, &mt_detach, start, end - 1); 2408763ecb03SLiam R. Howlett struct vm_area_struct *vma_mas, *vma_test; 2409763ecb03SLiam R. Howlett int test_count = 0; 2410763ecb03SLiam R. Howlett 2411183654ceSLiam R. Howlett vma_iter_set(vmi, start); 2412763ecb03SLiam R. Howlett rcu_read_lock(); 2413763ecb03SLiam R. Howlett vma_test = mas_find(&test, end - 1); 2414183654ceSLiam R. Howlett for_each_vma_range(*vmi, vma_mas, end) { 2415763ecb03SLiam R. Howlett BUG_ON(vma_mas != vma_test); 2416763ecb03SLiam R. Howlett test_count++; 2417763ecb03SLiam R. Howlett vma_test = mas_next(&test, end - 1); 2418763ecb03SLiam R. Howlett } 2419763ecb03SLiam R. Howlett rcu_read_unlock(); 2420763ecb03SLiam R. Howlett BUG_ON(count != test_count); 2421763ecb03SLiam R. Howlett } 2422763ecb03SLiam R. Howlett #endif 24230378c0a0SLiam R. Howlett /* Point of no return */ 2424183654ceSLiam R. Howlett vma_iter_set(vmi, start); 2425183654ceSLiam R. Howlett if (vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL)) 24260378c0a0SLiam R. Howlett return -ENOMEM; 24270378c0a0SLiam R. Howlett 2428763ecb03SLiam R. Howlett mm->map_count -= count; 242911f9a21aSLiam R. Howlett /* 243011f9a21aSLiam R. Howlett * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or 243111f9a21aSLiam R. Howlett * VM_GROWSUP VMA. Such VMAs can change their size under 243211f9a21aSLiam R. Howlett * down_read(mmap_lock) and collide with the VMA we are about to unmap. 243311f9a21aSLiam R. Howlett */ 243411f9a21aSLiam R. Howlett if (downgrade) { 2435763ecb03SLiam R. Howlett if (next && (next->vm_flags & VM_GROWSDOWN)) 243611f9a21aSLiam R. Howlett downgrade = false; 243711f9a21aSLiam R. Howlett else if (prev && (prev->vm_flags & VM_GROWSUP)) 243811f9a21aSLiam R. Howlett downgrade = false; 243911f9a21aSLiam R. Howlett else 2440d8ed45c5SMichel Lespinasse mmap_write_downgrade(mm); 244111f9a21aSLiam R. Howlett } 2442dd2283f2SYang Shi 2443763ecb03SLiam R. Howlett unmap_region(mm, &mt_detach, vma, prev, next, start, end); 2444763ecb03SLiam R. Howlett /* Statistics and freeing VMAs */ 2445763ecb03SLiam R. Howlett mas_set(&mas_detach, start); 2446763ecb03SLiam R. Howlett remove_mt(mm, &mas_detach); 2447763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 24481da177e4SLinus Torvalds 2449524e00b3SLiam R. Howlett 2450524e00b3SLiam R. Howlett validate_mm(mm); 2451dd2283f2SYang Shi return downgrade ? 1 : 0; 2452d4af56c5SLiam R. Howlett 2453d4af56c5SLiam R. Howlett userfaultfd_error: 2454763ecb03SLiam R. Howlett munmap_sidetree_failed: 2455763ecb03SLiam R. Howlett end_split_failed: 2456763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 2457763ecb03SLiam R. Howlett start_split_failed: 2458763ecb03SLiam R. Howlett map_count_exceeded: 2459d4af56c5SLiam R. Howlett return error; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 246211f9a21aSLiam R. Howlett /* 2463183654ceSLiam R. Howlett * do_vmi_munmap() - munmap a given range. 2464183654ceSLiam R. Howlett * @vmi: The vma iterator 246511f9a21aSLiam R. Howlett * @mm: The mm_struct 246611f9a21aSLiam R. Howlett * @start: The start address to munmap 246711f9a21aSLiam R. Howlett * @len: The length of the range to munmap 246811f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 246911f9a21aSLiam R. Howlett * @downgrade: set to true if the user wants to attempt to write_downgrade the 24708651a137SLorenzo Stoakes * mmap_lock 247111f9a21aSLiam R. Howlett * 247211f9a21aSLiam R. Howlett * This function takes a @mas that is either pointing to the previous VMA or set 247311f9a21aSLiam R. Howlett * to MA_START and sets it up to remove the mapping(s). The @len will be 247411f9a21aSLiam R. Howlett * aligned and any arch_unmap work will be preformed. 247511f9a21aSLiam R. Howlett * 247611f9a21aSLiam R. Howlett * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. 247711f9a21aSLiam R. Howlett */ 2478183654ceSLiam R. Howlett int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 247911f9a21aSLiam R. Howlett unsigned long start, size_t len, struct list_head *uf, 248011f9a21aSLiam R. Howlett bool downgrade) 248111f9a21aSLiam R. Howlett { 248211f9a21aSLiam R. Howlett unsigned long end; 248311f9a21aSLiam R. Howlett struct vm_area_struct *vma; 248411f9a21aSLiam R. Howlett 248511f9a21aSLiam R. Howlett if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 248611f9a21aSLiam R. Howlett return -EINVAL; 248711f9a21aSLiam R. Howlett 248811f9a21aSLiam R. Howlett end = start + PAGE_ALIGN(len); 248911f9a21aSLiam R. Howlett if (end == start) 249011f9a21aSLiam R. Howlett return -EINVAL; 249111f9a21aSLiam R. Howlett 249211f9a21aSLiam R. Howlett /* arch_unmap() might do unmaps itself. */ 249311f9a21aSLiam R. Howlett arch_unmap(mm, start, end); 249411f9a21aSLiam R. Howlett 249511f9a21aSLiam R. Howlett /* Find the first overlapping VMA */ 2496183654ceSLiam R. Howlett vma = vma_find(vmi, end); 249711f9a21aSLiam R. Howlett if (!vma) 249811f9a21aSLiam R. Howlett return 0; 249911f9a21aSLiam R. Howlett 2500183654ceSLiam R. Howlett return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); 250111f9a21aSLiam R. Howlett } 250211f9a21aSLiam R. Howlett 250311f9a21aSLiam R. Howlett /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. 250411f9a21aSLiam R. Howlett * @mm: The mm_struct 250511f9a21aSLiam R. Howlett * @start: The start address to munmap 250611f9a21aSLiam R. Howlett * @len: The length to be munmapped. 250711f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 250811f9a21aSLiam R. Howlett */ 2509dd2283f2SYang Shi int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2510dd2283f2SYang Shi struct list_head *uf) 2511dd2283f2SYang Shi { 2512183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 251311f9a21aSLiam R. Howlett 2514183654ceSLiam R. Howlett return do_vmi_munmap(&vmi, mm, start, len, uf, false); 2515dd2283f2SYang Shi } 2516dd2283f2SYang Shi 2517e99668a5SLiam R. Howlett unsigned long mmap_region(struct file *file, unsigned long addr, 2518e99668a5SLiam R. Howlett unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, 2519e99668a5SLiam R. Howlett struct list_head *uf) 2520e99668a5SLiam R. Howlett { 2521e99668a5SLiam R. Howlett struct mm_struct *mm = current->mm; 2522e99668a5SLiam R. Howlett struct vm_area_struct *vma = NULL; 2523e99668a5SLiam R. Howlett struct vm_area_struct *next, *prev, *merge; 2524e99668a5SLiam R. Howlett pgoff_t pglen = len >> PAGE_SHIFT; 2525e99668a5SLiam R. Howlett unsigned long charged = 0; 2526e99668a5SLiam R. Howlett unsigned long end = addr + len; 2527e99668a5SLiam R. Howlett unsigned long merge_start = addr, merge_end = end; 2528e99668a5SLiam R. Howlett pgoff_t vm_pgoff; 2529e99668a5SLiam R. Howlett int error; 2530183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 2531e99668a5SLiam R. Howlett 2532e99668a5SLiam R. Howlett /* Check against address space limit. */ 2533e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { 2534e99668a5SLiam R. Howlett unsigned long nr_pages; 2535e99668a5SLiam R. Howlett 2536e99668a5SLiam R. Howlett /* 2537e99668a5SLiam R. Howlett * MAP_FIXED may remove pages of mappings that intersects with 2538e99668a5SLiam R. Howlett * requested mapping. Account for the pages it would unmap. 2539e99668a5SLiam R. Howlett */ 2540e99668a5SLiam R. Howlett nr_pages = count_vma_pages_range(mm, addr, end); 2541e99668a5SLiam R. Howlett 2542e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, 2543e99668a5SLiam R. Howlett (len >> PAGE_SHIFT) - nr_pages)) 2544e99668a5SLiam R. Howlett return -ENOMEM; 2545e99668a5SLiam R. Howlett } 2546e99668a5SLiam R. Howlett 2547e99668a5SLiam R. Howlett /* Unmap any existing mapping in the area */ 2548183654ceSLiam R. Howlett if (do_vmi_munmap(&vmi, mm, addr, len, uf, false)) 2549e99668a5SLiam R. Howlett return -ENOMEM; 2550e99668a5SLiam R. Howlett 2551e99668a5SLiam R. Howlett /* 2552e99668a5SLiam R. Howlett * Private writable mapping: check memory availability 2553e99668a5SLiam R. Howlett */ 2554e99668a5SLiam R. Howlett if (accountable_mapping(file, vm_flags)) { 2555e99668a5SLiam R. Howlett charged = len >> PAGE_SHIFT; 2556e99668a5SLiam R. Howlett if (security_vm_enough_memory_mm(mm, charged)) 2557e99668a5SLiam R. Howlett return -ENOMEM; 2558e99668a5SLiam R. Howlett vm_flags |= VM_ACCOUNT; 2559e99668a5SLiam R. Howlett } 2560e99668a5SLiam R. Howlett 2561183654ceSLiam R. Howlett next = vma_next(&vmi); 2562183654ceSLiam R. Howlett prev = vma_prev(&vmi); 2563e99668a5SLiam R. Howlett if (vm_flags & VM_SPECIAL) 2564e99668a5SLiam R. Howlett goto cannot_expand; 2565e99668a5SLiam R. Howlett 2566e99668a5SLiam R. Howlett /* Attempt to expand an old mapping */ 2567e99668a5SLiam R. Howlett /* Check next */ 2568e99668a5SLiam R. Howlett if (next && next->vm_start == end && !vma_policy(next) && 2569e99668a5SLiam R. Howlett can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, 2570e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL)) { 2571e99668a5SLiam R. Howlett merge_end = next->vm_end; 2572e99668a5SLiam R. Howlett vma = next; 2573e99668a5SLiam R. Howlett vm_pgoff = next->vm_pgoff - pglen; 2574e99668a5SLiam R. Howlett } 2575e99668a5SLiam R. Howlett 2576e99668a5SLiam R. Howlett /* Check prev */ 2577e99668a5SLiam R. Howlett if (prev && prev->vm_end == addr && !vma_policy(prev) && 2578e99668a5SLiam R. Howlett (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, 2579e99668a5SLiam R. Howlett pgoff, vma->vm_userfaultfd_ctx, NULL) : 2580e99668a5SLiam R. Howlett can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, 2581e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL))) { 2582e99668a5SLiam R. Howlett merge_start = prev->vm_start; 2583e99668a5SLiam R. Howlett vma = prev; 2584e99668a5SLiam R. Howlett vm_pgoff = prev->vm_pgoff; 2585e99668a5SLiam R. Howlett } 2586e99668a5SLiam R. Howlett 2587e99668a5SLiam R. Howlett 2588e99668a5SLiam R. Howlett /* Actually expand, if possible */ 2589e99668a5SLiam R. Howlett if (vma && 25903c441ab7SLiam R. Howlett !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { 2591e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vm_flags); 2592e99668a5SLiam R. Howlett goto expanded; 2593e99668a5SLiam R. Howlett } 2594e99668a5SLiam R. Howlett 2595e99668a5SLiam R. Howlett cannot_expand: 2596e99668a5SLiam R. Howlett /* 2597e99668a5SLiam R. Howlett * Determine the object being mapped and call the appropriate 2598e99668a5SLiam R. Howlett * specific mapper. the address has already been validated, but 2599e99668a5SLiam R. Howlett * not unmapped, but the maps are removed from the list. 2600e99668a5SLiam R. Howlett */ 2601e99668a5SLiam R. Howlett vma = vm_area_alloc(mm); 2602e99668a5SLiam R. Howlett if (!vma) { 2603e99668a5SLiam R. Howlett error = -ENOMEM; 2604e99668a5SLiam R. Howlett goto unacct_error; 2605e99668a5SLiam R. Howlett } 2606e99668a5SLiam R. Howlett 26070fd5a9e2SLiam R. Howlett vma_iter_set(&vmi, addr); 2608e99668a5SLiam R. Howlett vma->vm_start = addr; 2609e99668a5SLiam R. Howlett vma->vm_end = end; 2610e99668a5SLiam R. Howlett vma->vm_flags = vm_flags; 2611e99668a5SLiam R. Howlett vma->vm_page_prot = vm_get_page_prot(vm_flags); 2612e99668a5SLiam R. Howlett vma->vm_pgoff = pgoff; 2613e99668a5SLiam R. Howlett 2614e99668a5SLiam R. Howlett if (file) { 2615e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) { 2616e99668a5SLiam R. Howlett error = mapping_map_writable(file->f_mapping); 2617e99668a5SLiam R. Howlett if (error) 2618e99668a5SLiam R. Howlett goto free_vma; 2619e99668a5SLiam R. Howlett } 2620e99668a5SLiam R. Howlett 2621e99668a5SLiam R. Howlett vma->vm_file = get_file(file); 2622e99668a5SLiam R. Howlett error = call_mmap(file, vma); 2623e99668a5SLiam R. Howlett if (error) 2624e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2625e99668a5SLiam R. Howlett 2626a57b7051SLiam Howlett /* 2627a57b7051SLiam Howlett * Expansion is handled above, merging is handled below. 2628a57b7051SLiam Howlett * Drivers should not alter the address of the VMA. 2629e99668a5SLiam R. Howlett */ 2630a57b7051SLiam Howlett error = -EINVAL; 2631cc8d1b09SLiam R. Howlett if (WARN_ON((addr != vma->vm_start))) 2632a57b7051SLiam Howlett goto close_and_free_vma; 2633e99668a5SLiam R. Howlett 2634cc8d1b09SLiam R. Howlett vma_iter_set(&vmi, addr); 2635e99668a5SLiam R. Howlett /* 2636e99668a5SLiam R. Howlett * If vm_flags changed after call_mmap(), we should try merge 2637e99668a5SLiam R. Howlett * vma again as we may succeed this time. 2638e99668a5SLiam R. Howlett */ 2639e99668a5SLiam R. Howlett if (unlikely(vm_flags != vma->vm_flags && prev)) { 26409760ebffSLiam R. Howlett merge = vma_merge(&vmi, mm, prev, vma->vm_start, 26419760ebffSLiam R. Howlett vma->vm_end, vma->vm_flags, NULL, 26429760ebffSLiam R. Howlett vma->vm_file, vma->vm_pgoff, NULL, 26439760ebffSLiam R. Howlett NULL_VM_UFFD_CTX, NULL); 2644e99668a5SLiam R. Howlett if (merge) { 2645e99668a5SLiam R. Howlett /* 2646e99668a5SLiam R. Howlett * ->mmap() can change vma->vm_file and fput 2647e99668a5SLiam R. Howlett * the original file. So fput the vma->vm_file 2648e99668a5SLiam R. Howlett * here or we would add an extra fput for file 2649e99668a5SLiam R. Howlett * and cause general protection fault 2650e99668a5SLiam R. Howlett * ultimately. 2651e99668a5SLiam R. Howlett */ 2652e99668a5SLiam R. Howlett fput(vma->vm_file); 2653e99668a5SLiam R. Howlett vm_area_free(vma); 2654e99668a5SLiam R. Howlett vma = merge; 2655e99668a5SLiam R. Howlett /* Update vm_flags to pick up the change. */ 2656e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2657e99668a5SLiam R. Howlett goto unmap_writable; 2658e99668a5SLiam R. Howlett } 2659e99668a5SLiam R. Howlett } 2660e99668a5SLiam R. Howlett 2661e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2662e99668a5SLiam R. Howlett } else if (vm_flags & VM_SHARED) { 2663e99668a5SLiam R. Howlett error = shmem_zero_setup(vma); 2664e99668a5SLiam R. Howlett if (error) 2665e99668a5SLiam R. Howlett goto free_vma; 2666e99668a5SLiam R. Howlett } else { 2667e99668a5SLiam R. Howlett vma_set_anonymous(vma); 2668e99668a5SLiam R. Howlett } 2669e99668a5SLiam R. Howlett 2670b507808eSJoey Gouly if (map_deny_write_exec(vma, vma->vm_flags)) { 2671b507808eSJoey Gouly error = -EACCES; 2672b507808eSJoey Gouly if (file) 2673b507808eSJoey Gouly goto close_and_free_vma; 2674b507808eSJoey Gouly else if (vma->vm_file) 2675b507808eSJoey Gouly goto unmap_and_free_vma; 2676b507808eSJoey Gouly else 2677b507808eSJoey Gouly goto free_vma; 2678b507808eSJoey Gouly } 2679b507808eSJoey Gouly 2680e99668a5SLiam R. Howlett /* Allow architectures to sanity-check the vm_flags */ 2681e99668a5SLiam R. Howlett error = -EINVAL; 2682cc8d1b09SLiam R. Howlett if (!arch_validate_flags(vma->vm_flags)) 2683deb0f656SCarlos Llamas goto close_and_free_vma; 2684e99668a5SLiam R. Howlett 2685e99668a5SLiam R. Howlett error = -ENOMEM; 2686cc8d1b09SLiam R. Howlett if (vma_iter_prealloc(&vmi)) 26875789151eSMike Kravetz goto close_and_free_vma; 2688e99668a5SLiam R. Howlett 2689e99668a5SLiam R. Howlett if (vma->vm_file) 2690e99668a5SLiam R. Howlett i_mmap_lock_write(vma->vm_file->f_mapping); 2691e99668a5SLiam R. Howlett 2692183654ceSLiam R. Howlett vma_iter_store(&vmi, vma); 2693e99668a5SLiam R. Howlett mm->map_count++; 2694e99668a5SLiam R. Howlett if (vma->vm_file) { 2695e99668a5SLiam R. Howlett if (vma->vm_flags & VM_SHARED) 2696e99668a5SLiam R. Howlett mapping_allow_writable(vma->vm_file->f_mapping); 2697e99668a5SLiam R. Howlett 2698e99668a5SLiam R. Howlett flush_dcache_mmap_lock(vma->vm_file->f_mapping); 2699e99668a5SLiam R. Howlett vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); 2700e99668a5SLiam R. Howlett flush_dcache_mmap_unlock(vma->vm_file->f_mapping); 2701e99668a5SLiam R. Howlett i_mmap_unlock_write(vma->vm_file->f_mapping); 2702e99668a5SLiam R. Howlett } 2703e99668a5SLiam R. Howlett 2704e99668a5SLiam R. Howlett /* 2705e99668a5SLiam R. Howlett * vma_merge() calls khugepaged_enter_vma() either, the below 2706e99668a5SLiam R. Howlett * call covers the non-merge case. 2707e99668a5SLiam R. Howlett */ 2708e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vma->vm_flags); 2709e99668a5SLiam R. Howlett 2710e99668a5SLiam R. Howlett /* Once vma denies write, undo our temporary denial count */ 2711e99668a5SLiam R. Howlett unmap_writable: 2712e99668a5SLiam R. Howlett if (file && vm_flags & VM_SHARED) 2713e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2714e99668a5SLiam R. Howlett file = vma->vm_file; 2715e99668a5SLiam R. Howlett expanded: 2716e99668a5SLiam R. Howlett perf_event_mmap(vma); 2717e99668a5SLiam R. Howlett 2718e99668a5SLiam R. Howlett vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); 2719e99668a5SLiam R. Howlett if (vm_flags & VM_LOCKED) { 2720e99668a5SLiam R. Howlett if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || 2721e99668a5SLiam R. Howlett is_vm_hugetlb_page(vma) || 2722e99668a5SLiam R. Howlett vma == get_gate_vma(current->mm)) 2723e99668a5SLiam R. Howlett vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 2724e99668a5SLiam R. Howlett else 2725e99668a5SLiam R. Howlett mm->locked_vm += (len >> PAGE_SHIFT); 2726e99668a5SLiam R. Howlett } 2727e99668a5SLiam R. Howlett 2728e99668a5SLiam R. Howlett if (file) 2729e99668a5SLiam R. Howlett uprobe_mmap(vma); 2730e99668a5SLiam R. Howlett 2731e99668a5SLiam R. Howlett /* 2732e99668a5SLiam R. Howlett * New (or expanded) vma always get soft dirty status. 2733e99668a5SLiam R. Howlett * Otherwise user-space soft-dirty page tracker won't 2734e99668a5SLiam R. Howlett * be able to distinguish situation when vma area unmapped, 2735e99668a5SLiam R. Howlett * then new mapped in-place (which must be aimed as 2736e99668a5SLiam R. Howlett * a completely new data area). 2737e99668a5SLiam R. Howlett */ 2738e99668a5SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 2739e99668a5SLiam R. Howlett 2740e99668a5SLiam R. Howlett vma_set_page_prot(vma); 2741e99668a5SLiam R. Howlett 2742e99668a5SLiam R. Howlett validate_mm(mm); 2743e99668a5SLiam R. Howlett return addr; 2744e99668a5SLiam R. Howlett 2745deb0f656SCarlos Llamas close_and_free_vma: 2746cc8d1b09SLiam R. Howlett if (file && vma->vm_ops && vma->vm_ops->close) 2747deb0f656SCarlos Llamas vma->vm_ops->close(vma); 2748cc8d1b09SLiam R. Howlett 2749cc8d1b09SLiam R. Howlett if (file || vma->vm_file) { 2750e99668a5SLiam R. Howlett unmap_and_free_vma: 2751e99668a5SLiam R. Howlett fput(vma->vm_file); 2752e99668a5SLiam R. Howlett vma->vm_file = NULL; 2753e99668a5SLiam R. Howlett 2754e99668a5SLiam R. Howlett /* Undo any partial mapping done by a device driver. */ 2755cc8d1b09SLiam R. Howlett unmap_region(mm, &mm->mm_mt, vma, prev, next, vma->vm_start, 2756cc8d1b09SLiam R. Howlett vma->vm_end); 2757cc8d1b09SLiam R. Howlett } 2758cc674ab3SLi Zetao if (file && (vm_flags & VM_SHARED)) 2759e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2760e99668a5SLiam R. Howlett free_vma: 2761e99668a5SLiam R. Howlett vm_area_free(vma); 2762e99668a5SLiam R. Howlett unacct_error: 2763e99668a5SLiam R. Howlett if (charged) 2764e99668a5SLiam R. Howlett vm_unacct_memory(charged); 2765e99668a5SLiam R. Howlett validate_mm(mm); 2766e99668a5SLiam R. Howlett return error; 2767e99668a5SLiam R. Howlett } 2768e99668a5SLiam R. Howlett 2769dd2283f2SYang Shi static int __vm_munmap(unsigned long start, size_t len, bool downgrade) 2770a46ef99dSLinus Torvalds { 2771a46ef99dSLinus Torvalds int ret; 2772bfce281cSAl Viro struct mm_struct *mm = current->mm; 2773897ab3e0SMike Rapoport LIST_HEAD(uf); 2774183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 2775a46ef99dSLinus Torvalds 2776d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2777ae798783SMichal Hocko return -EINTR; 2778ae798783SMichal Hocko 2779183654ceSLiam R. Howlett ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade); 2780dd2283f2SYang Shi /* 2781c1e8d7c6SMichel Lespinasse * Returning 1 indicates mmap_lock is downgraded. 2782dd2283f2SYang Shi * But 1 is not legal return value of vm_munmap() and munmap(), reset 2783dd2283f2SYang Shi * it to 0 before return. 2784dd2283f2SYang Shi */ 2785dd2283f2SYang Shi if (ret == 1) { 2786d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2787dd2283f2SYang Shi ret = 0; 2788dd2283f2SYang Shi } else 2789d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2790dd2283f2SYang Shi 2791897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 2792a46ef99dSLinus Torvalds return ret; 2793a46ef99dSLinus Torvalds } 2794dd2283f2SYang Shi 2795dd2283f2SYang Shi int vm_munmap(unsigned long start, size_t len) 2796dd2283f2SYang Shi { 2797dd2283f2SYang Shi return __vm_munmap(start, len, false); 2798dd2283f2SYang Shi } 2799a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2800a46ef99dSLinus Torvalds 28016a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 28021da177e4SLinus Torvalds { 2803ce18d171SCatalin Marinas addr = untagged_addr(addr); 2804dd2283f2SYang Shi return __vm_munmap(addr, len, true); 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 2807c8d78c18SKirill A. Shutemov 2808c8d78c18SKirill A. Shutemov /* 2809c8d78c18SKirill A. Shutemov * Emulation of deprecated remap_file_pages() syscall. 2810c8d78c18SKirill A. Shutemov */ 2811c8d78c18SKirill A. Shutemov SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, 2812c8d78c18SKirill A. Shutemov unsigned long, prot, unsigned long, pgoff, unsigned long, flags) 2813c8d78c18SKirill A. Shutemov { 2814c8d78c18SKirill A. Shutemov 2815c8d78c18SKirill A. Shutemov struct mm_struct *mm = current->mm; 2816c8d78c18SKirill A. Shutemov struct vm_area_struct *vma; 2817c8d78c18SKirill A. Shutemov unsigned long populate = 0; 2818c8d78c18SKirill A. Shutemov unsigned long ret = -EINVAL; 2819c8d78c18SKirill A. Shutemov struct file *file; 2820c8d78c18SKirill A. Shutemov 2821ee65728eSMike Rapoport pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", 2822c8d78c18SKirill A. Shutemov current->comm, current->pid); 2823c8d78c18SKirill A. Shutemov 2824c8d78c18SKirill A. Shutemov if (prot) 2825c8d78c18SKirill A. Shutemov return ret; 2826c8d78c18SKirill A. Shutemov start = start & PAGE_MASK; 2827c8d78c18SKirill A. Shutemov size = size & PAGE_MASK; 2828c8d78c18SKirill A. Shutemov 2829c8d78c18SKirill A. Shutemov if (start + size <= start) 2830c8d78c18SKirill A. Shutemov return ret; 2831c8d78c18SKirill A. Shutemov 2832c8d78c18SKirill A. Shutemov /* Does pgoff wrap? */ 2833c8d78c18SKirill A. Shutemov if (pgoff + (size >> PAGE_SHIFT) < pgoff) 2834c8d78c18SKirill A. Shutemov return ret; 2835c8d78c18SKirill A. Shutemov 2836d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2837dc0ef0dfSMichal Hocko return -EINTR; 2838dc0ef0dfSMichal Hocko 28399b593cb2SLiam R. Howlett vma = vma_lookup(mm, start); 2840c8d78c18SKirill A. Shutemov 2841c8d78c18SKirill A. Shutemov if (!vma || !(vma->vm_flags & VM_SHARED)) 2842c8d78c18SKirill A. Shutemov goto out; 2843c8d78c18SKirill A. Shutemov 284448f7df32SKirill A. Shutemov if (start + size > vma->vm_end) { 2845763ecb03SLiam R. Howlett VMA_ITERATOR(vmi, mm, vma->vm_end); 2846763ecb03SLiam R. Howlett struct vm_area_struct *next, *prev = vma; 284748f7df32SKirill A. Shutemov 2848763ecb03SLiam R. Howlett for_each_vma_range(vmi, next, start + size) { 284948f7df32SKirill A. Shutemov /* hole between vmas ? */ 2850763ecb03SLiam R. Howlett if (next->vm_start != prev->vm_end) 285148f7df32SKirill A. Shutemov goto out; 285248f7df32SKirill A. Shutemov 285348f7df32SKirill A. Shutemov if (next->vm_file != vma->vm_file) 285448f7df32SKirill A. Shutemov goto out; 285548f7df32SKirill A. Shutemov 285648f7df32SKirill A. Shutemov if (next->vm_flags != vma->vm_flags) 285748f7df32SKirill A. Shutemov goto out; 285848f7df32SKirill A. Shutemov 28591db43d3fSLiam Howlett if (start + size <= next->vm_end) 28601db43d3fSLiam Howlett break; 28611db43d3fSLiam Howlett 2862763ecb03SLiam R. Howlett prev = next; 286348f7df32SKirill A. Shutemov } 286448f7df32SKirill A. Shutemov 286548f7df32SKirill A. Shutemov if (!next) 2866c8d78c18SKirill A. Shutemov goto out; 2867c8d78c18SKirill A. Shutemov } 2868c8d78c18SKirill A. Shutemov 2869c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; 2870c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; 2871c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; 2872c8d78c18SKirill A. Shutemov 2873c8d78c18SKirill A. Shutemov flags &= MAP_NONBLOCK; 2874c8d78c18SKirill A. Shutemov flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; 2875fce000b1SLiam Howlett if (vma->vm_flags & VM_LOCKED) 2876c8d78c18SKirill A. Shutemov flags |= MAP_LOCKED; 287748f7df32SKirill A. Shutemov 2878c8d78c18SKirill A. Shutemov file = get_file(vma->vm_file); 287945e55300SPeter Collingbourne ret = do_mmap(vma->vm_file, start, size, 2880897ab3e0SMike Rapoport prot, flags, pgoff, &populate, NULL); 2881c8d78c18SKirill A. Shutemov fput(file); 2882c8d78c18SKirill A. Shutemov out: 2883d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2884c8d78c18SKirill A. Shutemov if (populate) 2885c8d78c18SKirill A. Shutemov mm_populate(ret, populate); 2886c8d78c18SKirill A. Shutemov if (!IS_ERR_VALUE(ret)) 2887c8d78c18SKirill A. Shutemov ret = 0; 2888c8d78c18SKirill A. Shutemov return ret; 2889c8d78c18SKirill A. Shutemov } 2890c8d78c18SKirill A. Shutemov 28911da177e4SLinus Torvalds /* 289227b26701SLiam R. Howlett * do_vma_munmap() - Unmap a full or partial vma. 289327b26701SLiam R. Howlett * @vmi: The vma iterator pointing at the vma 289427b26701SLiam R. Howlett * @vma: The first vma to be munmapped 289527b26701SLiam R. Howlett * @start: the start of the address to unmap 289627b26701SLiam R. Howlett * @end: The end of the address to unmap 28972e7ce7d3SLiam R. Howlett * @uf: The userfaultfd list_head 289827b26701SLiam R. Howlett * @downgrade: Attempt to downgrade or not 28992e7ce7d3SLiam R. Howlett * 290027b26701SLiam R. Howlett * Returns: 0 on success and not downgraded, 1 on success and downgraded. 290127b26701SLiam R. Howlett * unmaps a VMA mapping when the vma iterator is already in position. 290227b26701SLiam R. Howlett * Does not handle alignment. 29031da177e4SLinus Torvalds */ 290427b26701SLiam R. Howlett int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 290527b26701SLiam R. Howlett unsigned long start, unsigned long end, 290627b26701SLiam R. Howlett struct list_head *uf, bool downgrade) 29072e7ce7d3SLiam R. Howlett { 29082e7ce7d3SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 29092e7ce7d3SLiam R. Howlett int ret; 29102e7ce7d3SLiam R. Howlett 291127b26701SLiam R. Howlett arch_unmap(mm, start, end); 291227b26701SLiam R. Howlett ret = do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); 29132e7ce7d3SLiam R. Howlett validate_mm_mt(mm); 29142e7ce7d3SLiam R. Howlett return ret; 29152e7ce7d3SLiam R. Howlett } 29162e7ce7d3SLiam R. Howlett 29172e7ce7d3SLiam R. Howlett /* 29182e7ce7d3SLiam R. Howlett * do_brk_flags() - Increase the brk vma if the flags match. 291992fed820SLiam R. Howlett * @vmi: The vma iterator 29202e7ce7d3SLiam R. Howlett * @addr: The start address 29212e7ce7d3SLiam R. Howlett * @len: The length of the increase 29222e7ce7d3SLiam R. Howlett * @vma: The vma, 29232e7ce7d3SLiam R. Howlett * @flags: The VMA Flags 29242e7ce7d3SLiam R. Howlett * 29252e7ce7d3SLiam R. Howlett * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags 29262e7ce7d3SLiam R. Howlett * do not match then create a new anonymous VMA. Eventually we may be able to 29272e7ce7d3SLiam R. Howlett * do some brk-specific accounting here. 29282e7ce7d3SLiam R. Howlett */ 292992fed820SLiam R. Howlett static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, 2930763ecb03SLiam R. Howlett unsigned long addr, unsigned long len, unsigned long flags) 29311da177e4SLinus Torvalds { 29321da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 29332e7ce7d3SLiam R. Howlett 2934d4af56c5SLiam R. Howlett validate_mm_mt(mm); 29352e7ce7d3SLiam R. Howlett /* 29362e7ce7d3SLiam R. Howlett * Check against address space limits by the changed size 29372e7ce7d3SLiam R. Howlett * Note: This happens *after* clearing old mappings in some code paths. 29382e7ce7d3SLiam R. Howlett */ 293916e72e9bSDenys Vlasenko flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 294084638335SKonstantin Khlebnikov if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) 29411da177e4SLinus Torvalds return -ENOMEM; 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 29441da177e4SLinus Torvalds return -ENOMEM; 29451da177e4SLinus Torvalds 2946191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 29471da177e4SLinus Torvalds return -ENOMEM; 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds /* 29502e7ce7d3SLiam R. Howlett * Expand the existing vma if possible; Note that singular lists do not 29512e7ce7d3SLiam R. Howlett * occur after forking, so the expand will only happen on new VMAs. 29521da177e4SLinus Torvalds */ 29536c28ca64SLiam Howlett if (vma && vma->vm_end == addr && !vma_policy(vma) && 29546c28ca64SLiam Howlett can_vma_merge_after(vma, flags, NULL, NULL, 29556c28ca64SLiam Howlett addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) { 295692fed820SLiam R. Howlett if (vma_iter_prealloc(vmi)) 2957675eaca1SAlistair Popple goto unacct_fail; 295828c5609fSLiam Howlett 295928c5609fSLiam Howlett vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0); 29602e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 29612e7ce7d3SLiam R. Howlett anon_vma_lock_write(vma->anon_vma); 29622e7ce7d3SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 29631da177e4SLinus Torvalds } 29642e7ce7d3SLiam R. Howlett vma->vm_end = addr + len; 29652e7ce7d3SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 296692fed820SLiam R. Howlett vma_iter_store(vmi, vma); 29672e7ce7d3SLiam R. Howlett 29682e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 29692e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 29702e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 29712e7ce7d3SLiam R. Howlett } 29722e7ce7d3SLiam R. Howlett khugepaged_enter_vma(vma, flags); 29732e7ce7d3SLiam R. Howlett goto out; 29742e7ce7d3SLiam R. Howlett } 29752e7ce7d3SLiam R. Howlett 29762e7ce7d3SLiam R. Howlett /* create a vma struct for an anonymous mapping */ 29772e7ce7d3SLiam R. Howlett vma = vm_area_alloc(mm); 29782e7ce7d3SLiam R. Howlett if (!vma) 2979675eaca1SAlistair Popple goto unacct_fail; 29801da177e4SLinus Torvalds 2981bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 29821da177e4SLinus Torvalds vma->vm_start = addr; 29831da177e4SLinus Torvalds vma->vm_end = addr + len; 29842e7ce7d3SLiam R. Howlett vma->vm_pgoff = addr >> PAGE_SHIFT; 29851da177e4SLinus Torvalds vma->vm_flags = flags; 29863ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 298792fed820SLiam R. Howlett if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) 29882e7ce7d3SLiam R. Howlett goto mas_store_fail; 2989d4af56c5SLiam R. Howlett 29902e7ce7d3SLiam R. Howlett mm->map_count++; 29911da177e4SLinus Torvalds out: 29923af9e859SEric B Munson perf_event_mmap(vma); 29931da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 299484638335SKonstantin Khlebnikov mm->data_vm += len >> PAGE_SHIFT; 2995128557ffSMichel Lespinasse if (flags & VM_LOCKED) 2996ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 2997d9104d1cSCyrill Gorcunov vma->vm_flags |= VM_SOFTDIRTY; 2998763ecb03SLiam R. Howlett validate_mm(mm); 29995d22fc25SLinus Torvalds return 0; 3000d4af56c5SLiam R. Howlett 30012e7ce7d3SLiam R. Howlett mas_store_fail: 3002d4af56c5SLiam R. Howlett vm_area_free(vma); 3003675eaca1SAlistair Popple unacct_fail: 30042e7ce7d3SLiam R. Howlett vm_unacct_memory(len >> PAGE_SHIFT); 30052e7ce7d3SLiam R. Howlett return -ENOMEM; 30061da177e4SLinus Torvalds } 30071da177e4SLinus Torvalds 3008bb177a73SMichal Hocko int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) 3009e4eb1ff6SLinus Torvalds { 3010e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 30112e7ce7d3SLiam R. Howlett struct vm_area_struct *vma = NULL; 3012bb177a73SMichal Hocko unsigned long len; 30135d22fc25SLinus Torvalds int ret; 3014128557ffSMichel Lespinasse bool populate; 3015897ab3e0SMike Rapoport LIST_HEAD(uf); 301692fed820SLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 3017e4eb1ff6SLinus Torvalds 3018bb177a73SMichal Hocko len = PAGE_ALIGN(request); 3019bb177a73SMichal Hocko if (len < request) 3020bb177a73SMichal Hocko return -ENOMEM; 3021bb177a73SMichal Hocko if (!len) 3022bb177a73SMichal Hocko return 0; 3023bb177a73SMichal Hocko 3024d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 30252d6c9282SMichal Hocko return -EINTR; 30262d6c9282SMichal Hocko 30272e7ce7d3SLiam R. Howlett /* Until we need other flags, refuse anything except VM_EXEC. */ 30282e7ce7d3SLiam R. Howlett if ((flags & (~VM_EXEC)) != 0) 30292e7ce7d3SLiam R. Howlett return -EINVAL; 30302e7ce7d3SLiam R. Howlett 30312e7ce7d3SLiam R. Howlett ret = check_brk_limits(addr, len); 30322e7ce7d3SLiam R. Howlett if (ret) 30332e7ce7d3SLiam R. Howlett goto limits_failed; 30342e7ce7d3SLiam R. Howlett 3035183654ceSLiam R. Howlett ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0); 30362e7ce7d3SLiam R. Howlett if (ret) 30372e7ce7d3SLiam R. Howlett goto munmap_failed; 30382e7ce7d3SLiam R. Howlett 303992fed820SLiam R. Howlett vma = vma_prev(&vmi); 304092fed820SLiam R. Howlett ret = do_brk_flags(&vmi, vma, addr, len, flags); 3041128557ffSMichel Lespinasse populate = ((mm->def_flags & VM_LOCKED) != 0); 3042d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3043897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 30445d22fc25SLinus Torvalds if (populate && !ret) 3045128557ffSMichel Lespinasse mm_populate(addr, len); 3046e4eb1ff6SLinus Torvalds return ret; 30472e7ce7d3SLiam R. Howlett 30482e7ce7d3SLiam R. Howlett munmap_failed: 30492e7ce7d3SLiam R. Howlett limits_failed: 30502e7ce7d3SLiam R. Howlett mmap_write_unlock(mm); 30512e7ce7d3SLiam R. Howlett return ret; 3052e4eb1ff6SLinus Torvalds } 305316e72e9bSDenys Vlasenko EXPORT_SYMBOL(vm_brk_flags); 305416e72e9bSDenys Vlasenko 305516e72e9bSDenys Vlasenko int vm_brk(unsigned long addr, unsigned long len) 305616e72e9bSDenys Vlasenko { 305716e72e9bSDenys Vlasenko return vm_brk_flags(addr, len, 0); 305816e72e9bSDenys Vlasenko } 3059e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds /* Release all mmaps. */ 30621da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 30631da177e4SLinus Torvalds { 3064d16dfc55SPeter Zijlstra struct mmu_gather tlb; 3065ba470de4SRik van Riel struct vm_area_struct *vma; 30661da177e4SLinus Torvalds unsigned long nr_accounted = 0; 3067763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 3068763ecb03SLiam R. Howlett int count = 0; 30691da177e4SLinus Torvalds 3070d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 3071cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 3072d6dd61c8SJeremy Fitzhardinge 3073bf3980c8SSuren Baghdasaryan mmap_read_lock(mm); 30749480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 30759480c53eSJeremy Fitzhardinge 3076763ecb03SLiam R. Howlett vma = mas_find(&mas, ULONG_MAX); 307764591e86SSuren Baghdasaryan if (!vma) { 307864591e86SSuren Baghdasaryan /* Can happen if dup_mmap() received an OOM */ 3079bf3980c8SSuren Baghdasaryan mmap_read_unlock(mm); 30809480c53eSJeremy Fitzhardinge return; 308164591e86SSuren Baghdasaryan } 30829480c53eSJeremy Fitzhardinge 30831da177e4SLinus Torvalds lru_add_drain(); 30841da177e4SLinus Torvalds flush_cache_mm(mm); 3085d8b45053SWill Deacon tlb_gather_mmu_fullmm(&tlb, mm); 3086901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 3087763ecb03SLiam R. Howlett /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ 3088763ecb03SLiam R. Howlett unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX); 3089bf3980c8SSuren Baghdasaryan mmap_read_unlock(mm); 3090bf3980c8SSuren Baghdasaryan 3091bf3980c8SSuren Baghdasaryan /* 3092bf3980c8SSuren Baghdasaryan * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper 3093b3541d91SSuren Baghdasaryan * because the memory has been already freed. 3094bf3980c8SSuren Baghdasaryan */ 3095bf3980c8SSuren Baghdasaryan set_bit(MMF_OOM_SKIP, &mm->flags); 3096bf3980c8SSuren Baghdasaryan mmap_write_lock(mm); 3097763ecb03SLiam R. Howlett free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, 3098763ecb03SLiam R. Howlett USER_PGTABLES_CEILING); 3099ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 31001da177e4SLinus Torvalds 3101763ecb03SLiam R. Howlett /* 3102763ecb03SLiam R. Howlett * Walk the list again, actually closing and freeing it, with preemption 3103763ecb03SLiam R. Howlett * enabled, without holding any MM locks besides the unreachable 3104763ecb03SLiam R. Howlett * mmap_write_lock. 3105763ecb03SLiam R. Howlett */ 3106763ecb03SLiam R. Howlett do { 31074f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 31084f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 3109763ecb03SLiam R. Howlett remove_vma(vma); 3110763ecb03SLiam R. Howlett count++; 31110a3b3c25SPaul E. McKenney cond_resched(); 3112763ecb03SLiam R. Howlett } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); 3113763ecb03SLiam R. Howlett 3114763ecb03SLiam R. Howlett BUG_ON(count != mm->map_count); 3115d4af56c5SLiam R. Howlett 3116d4af56c5SLiam R. Howlett trace_exit_mmap(mm); 3117d4af56c5SLiam R. Howlett __mt_destroy(&mm->mm_mt); 311864591e86SSuren Baghdasaryan mmap_write_unlock(mm); 31194f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 31201da177e4SLinus Torvalds } 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 31231da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 3124c8c06efaSDavidlohr Bueso * then i_mmap_rwsem is taken here. 31251da177e4SLinus Torvalds */ 31261da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 31271da177e4SLinus Torvalds { 3128d4af56c5SLiam R. Howlett unsigned long charged = vma_pages(vma); 31291da177e4SLinus Torvalds 3130d4af56c5SLiam R. Howlett 3131d0601a50SLiam R. Howlett if (find_vma_intersection(mm, vma->vm_start, vma->vm_end)) 3132c9d13f5fSChen Gang return -ENOMEM; 3133d4af56c5SLiam R. Howlett 3134c9d13f5fSChen Gang if ((vma->vm_flags & VM_ACCOUNT) && 3135d4af56c5SLiam R. Howlett security_vm_enough_memory_mm(mm, charged)) 3136c9d13f5fSChen Gang return -ENOMEM; 3137c9d13f5fSChen Gang 31381da177e4SLinus Torvalds /* 31391da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 31401da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 31411da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 31421da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 31431da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 31441da177e4SLinus Torvalds * 31451da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 31461da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 31471da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 31488332326eSLiao Pingfang * Similarly in do_mmap and in do_brk_flags. 31491da177e4SLinus Torvalds */ 31508a9cc3b5SOleg Nesterov if (vma_is_anonymous(vma)) { 31511da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 31521da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 31531da177e4SLinus Torvalds } 31542b144498SSrikar Dronamraju 3155763ecb03SLiam R. Howlett if (vma_link(mm, vma)) { 3156d4af56c5SLiam R. Howlett vm_unacct_memory(charged); 3157d4af56c5SLiam R. Howlett return -ENOMEM; 3158d4af56c5SLiam R. Howlett } 3159d4af56c5SLiam R. Howlett 31601da177e4SLinus Torvalds return 0; 31611da177e4SLinus Torvalds } 31621da177e4SLinus Torvalds 31631da177e4SLinus Torvalds /* 31641da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 31651da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 31661da177e4SLinus Torvalds */ 31671da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 316838a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 316938a76013SMichel Lespinasse bool *need_rmap_locks) 31701da177e4SLinus Torvalds { 31711da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 31721da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 31731da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 31741da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 3175948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 3176076f16bfSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 31771da177e4SLinus Torvalds 3178d4af56c5SLiam R. Howlett validate_mm_mt(mm); 31791da177e4SLinus Torvalds /* 31801da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 31811da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 31821da177e4SLinus Torvalds */ 3183ce75799bSOleg Nesterov if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 31841da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 3185948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 3186948f017bSAndrea Arcangeli } 31871da177e4SLinus Torvalds 3188763ecb03SLiam R. Howlett new_vma = find_vma_prev(mm, addr, &prev); 3189763ecb03SLiam R. Howlett if (new_vma && new_vma->vm_start < addr + len) 31906597d783SHugh Dickins return NULL; /* should never get here */ 3191524e00b3SLiam R. Howlett 31929760ebffSLiam R. Howlett new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags, 319319a809afSAndrea Arcangeli vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), 31945c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 31951da177e4SLinus Torvalds if (new_vma) { 31961da177e4SLinus Torvalds /* 31971da177e4SLinus Torvalds * Source vma may have been merged into new_vma 31981da177e4SLinus Torvalds */ 3199948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 3200948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 3201948f017bSAndrea Arcangeli /* 3202948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 3203948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 3204948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 3205948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 3206948f017bSAndrea Arcangeli * destination address of the mremap to allow 3207948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 3208948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 3209948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 3210948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 3211948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 3212948f017bSAndrea Arcangeli */ 321381d1b09cSSasha Levin VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 321438a76013SMichel Lespinasse *vmap = vma = new_vma; 3215108d6642SMichel Lespinasse } 321638a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 32171da177e4SLinus Torvalds } else { 32183928d4f5SLinus Torvalds new_vma = vm_area_dup(vma); 3219e3975891SChen Gang if (!new_vma) 3220e3975891SChen Gang goto out; 32211da177e4SLinus Torvalds new_vma->vm_start = addr; 32221da177e4SLinus Torvalds new_vma->vm_end = addr + len; 32231da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 3224ef0855d3SOleg Nesterov if (vma_dup_policy(vma, new_vma)) 3225523d4e20SMichel Lespinasse goto out_free_vma; 3226523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 3227523d4e20SMichel Lespinasse goto out_free_mempol; 3228e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 32291da177e4SLinus Torvalds get_file(new_vma->vm_file); 32301da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 32311da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 3232763ecb03SLiam R. Howlett if (vma_link(mm, new_vma)) 3233524e00b3SLiam R. Howlett goto out_vma_link; 323438a76013SMichel Lespinasse *need_rmap_locks = false; 32351da177e4SLinus Torvalds } 3236d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32371da177e4SLinus Torvalds return new_vma; 32385beb4930SRik van Riel 3239524e00b3SLiam R. Howlett out_vma_link: 3240524e00b3SLiam R. Howlett if (new_vma->vm_ops && new_vma->vm_ops->close) 3241524e00b3SLiam R. Howlett new_vma->vm_ops->close(new_vma); 324292b73996SLiam Howlett 324392b73996SLiam Howlett if (new_vma->vm_file) 324492b73996SLiam Howlett fput(new_vma->vm_file); 324592b73996SLiam Howlett 324692b73996SLiam Howlett unlink_anon_vmas(new_vma); 32475beb4930SRik van Riel out_free_mempol: 3248ef0855d3SOleg Nesterov mpol_put(vma_policy(new_vma)); 32495beb4930SRik van Riel out_free_vma: 32503928d4f5SLinus Torvalds vm_area_free(new_vma); 3251e3975891SChen Gang out: 3252d4af56c5SLiam R. Howlett validate_mm_mt(mm); 32535beb4930SRik van Riel return NULL; 32541da177e4SLinus Torvalds } 3255119f657cSakpm@osdl.org 3256119f657cSakpm@osdl.org /* 3257119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 3258119f657cSakpm@osdl.org * number of pages 3259119f657cSakpm@osdl.org */ 326084638335SKonstantin Khlebnikov bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) 3261119f657cSakpm@osdl.org { 326284638335SKonstantin Khlebnikov if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) 326384638335SKonstantin Khlebnikov return false; 3264119f657cSakpm@osdl.org 3265d977d56cSKonstantin Khlebnikov if (is_data_mapping(flags) && 3266d977d56cSKonstantin Khlebnikov mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { 3267f4fcd558SKonstantin Khlebnikov /* Workaround for Valgrind */ 3268f4fcd558SKonstantin Khlebnikov if (rlimit(RLIMIT_DATA) == 0 && 3269f4fcd558SKonstantin Khlebnikov mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) 3270f4fcd558SKonstantin Khlebnikov return true; 327157a7702bSDavid Woodhouse 327257a7702bSDavid Woodhouse pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", 3273d977d56cSKonstantin Khlebnikov current->comm, current->pid, 3274d977d56cSKonstantin Khlebnikov (mm->data_vm + npages) << PAGE_SHIFT, 327557a7702bSDavid Woodhouse rlimit(RLIMIT_DATA), 327657a7702bSDavid Woodhouse ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); 327757a7702bSDavid Woodhouse 327857a7702bSDavid Woodhouse if (!ignore_rlimit_data) 3279d977d56cSKonstantin Khlebnikov return false; 3280d977d56cSKonstantin Khlebnikov } 3281119f657cSakpm@osdl.org 328284638335SKonstantin Khlebnikov return true; 328384638335SKonstantin Khlebnikov } 328484638335SKonstantin Khlebnikov 328584638335SKonstantin Khlebnikov void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) 328684638335SKonstantin Khlebnikov { 32877866076bSPeng Liu WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); 328884638335SKonstantin Khlebnikov 3289d977d56cSKonstantin Khlebnikov if (is_exec_mapping(flags)) 329084638335SKonstantin Khlebnikov mm->exec_vm += npages; 3291d977d56cSKonstantin Khlebnikov else if (is_stack_mapping(flags)) 329284638335SKonstantin Khlebnikov mm->stack_vm += npages; 3293d977d56cSKonstantin Khlebnikov else if (is_data_mapping(flags)) 329484638335SKonstantin Khlebnikov mm->data_vm += npages; 3295119f657cSakpm@osdl.org } 3296fa5dc22fSRoland McGrath 3297b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf); 3298a62c34bdSAndy Lutomirski 3299a62c34bdSAndy Lutomirski /* 3300a62c34bdSAndy Lutomirski * Having a close hook prevents vma merging regardless of flags. 3301a62c34bdSAndy Lutomirski */ 3302a62c34bdSAndy Lutomirski static void special_mapping_close(struct vm_area_struct *vma) 3303a62c34bdSAndy Lutomirski { 3304a62c34bdSAndy Lutomirski } 3305a62c34bdSAndy Lutomirski 3306a62c34bdSAndy Lutomirski static const char *special_mapping_name(struct vm_area_struct *vma) 3307a62c34bdSAndy Lutomirski { 3308a62c34bdSAndy Lutomirski return ((struct vm_special_mapping *)vma->vm_private_data)->name; 3309a62c34bdSAndy Lutomirski } 3310a62c34bdSAndy Lutomirski 331114d07113SBrian Geffon static int special_mapping_mremap(struct vm_area_struct *new_vma) 3312b059a453SDmitry Safonov { 3313b059a453SDmitry Safonov struct vm_special_mapping *sm = new_vma->vm_private_data; 3314b059a453SDmitry Safonov 3315280e87e9SDmitry Safonov if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) 3316280e87e9SDmitry Safonov return -EFAULT; 3317280e87e9SDmitry Safonov 3318b059a453SDmitry Safonov if (sm->mremap) 3319b059a453SDmitry Safonov return sm->mremap(sm, new_vma); 3320280e87e9SDmitry Safonov 3321b059a453SDmitry Safonov return 0; 3322b059a453SDmitry Safonov } 3323b059a453SDmitry Safonov 3324871402e0SDmitry Safonov static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) 3325871402e0SDmitry Safonov { 3326871402e0SDmitry Safonov /* 3327871402e0SDmitry Safonov * Forbid splitting special mappings - kernel has expectations over 3328871402e0SDmitry Safonov * the number of pages in mapping. Together with VM_DONTEXPAND 3329871402e0SDmitry Safonov * the size of vma should stay the same over the special mapping's 3330871402e0SDmitry Safonov * lifetime. 3331871402e0SDmitry Safonov */ 3332871402e0SDmitry Safonov return -EINVAL; 3333871402e0SDmitry Safonov } 3334871402e0SDmitry Safonov 3335a62c34bdSAndy Lutomirski static const struct vm_operations_struct special_mapping_vmops = { 3336a62c34bdSAndy Lutomirski .close = special_mapping_close, 3337a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3338b059a453SDmitry Safonov .mremap = special_mapping_mremap, 3339a62c34bdSAndy Lutomirski .name = special_mapping_name, 3340af34ebebSDmitry Safonov /* vDSO code relies that VVAR can't be accessed remotely */ 3341af34ebebSDmitry Safonov .access = NULL, 3342871402e0SDmitry Safonov .may_split = special_mapping_split, 3343a62c34bdSAndy Lutomirski }; 3344a62c34bdSAndy Lutomirski 3345a62c34bdSAndy Lutomirski static const struct vm_operations_struct legacy_special_mapping_vmops = { 3346a62c34bdSAndy Lutomirski .close = special_mapping_close, 3347a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3348a62c34bdSAndy Lutomirski }; 3349fa5dc22fSRoland McGrath 3350b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf) 3351fa5dc22fSRoland McGrath { 335211bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 3353b1d0e4f5SNick Piggin pgoff_t pgoff; 3354fa5dc22fSRoland McGrath struct page **pages; 3355fa5dc22fSRoland McGrath 3356f872f540SAndy Lutomirski if (vma->vm_ops == &legacy_special_mapping_vmops) { 3357a62c34bdSAndy Lutomirski pages = vma->vm_private_data; 3358f872f540SAndy Lutomirski } else { 3359f872f540SAndy Lutomirski struct vm_special_mapping *sm = vma->vm_private_data; 3360f872f540SAndy Lutomirski 3361f872f540SAndy Lutomirski if (sm->fault) 336211bac800SDave Jiang return sm->fault(sm, vmf->vma, vmf); 3363f872f540SAndy Lutomirski 3364f872f540SAndy Lutomirski pages = sm->pages; 3365f872f540SAndy Lutomirski } 3366a62c34bdSAndy Lutomirski 33678a9cc3b5SOleg Nesterov for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) 3368b1d0e4f5SNick Piggin pgoff--; 3369fa5dc22fSRoland McGrath 3370fa5dc22fSRoland McGrath if (*pages) { 3371fa5dc22fSRoland McGrath struct page *page = *pages; 3372fa5dc22fSRoland McGrath get_page(page); 3373b1d0e4f5SNick Piggin vmf->page = page; 3374b1d0e4f5SNick Piggin return 0; 3375fa5dc22fSRoland McGrath } 3376fa5dc22fSRoland McGrath 3377b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 3378fa5dc22fSRoland McGrath } 3379fa5dc22fSRoland McGrath 3380a62c34bdSAndy Lutomirski static struct vm_area_struct *__install_special_mapping( 3381a62c34bdSAndy Lutomirski struct mm_struct *mm, 3382fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 338327f28b97SChen Gang unsigned long vm_flags, void *priv, 338427f28b97SChen Gang const struct vm_operations_struct *ops) 3385fa5dc22fSRoland McGrath { 3386462e635eSTavis Ormandy int ret; 3387fa5dc22fSRoland McGrath struct vm_area_struct *vma; 3388fa5dc22fSRoland McGrath 3389d4af56c5SLiam R. Howlett validate_mm_mt(mm); 3390490fc053SLinus Torvalds vma = vm_area_alloc(mm); 3391fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 33923935ed6aSStefani Seibold return ERR_PTR(-ENOMEM); 3393fa5dc22fSRoland McGrath 3394fa5dc22fSRoland McGrath vma->vm_start = addr; 3395fa5dc22fSRoland McGrath vma->vm_end = addr + len; 3396fa5dc22fSRoland McGrath 3397d9104d1cSCyrill Gorcunov vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY; 33981fc09228SHugh Dickins vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 33993ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 3400fa5dc22fSRoland McGrath 3401a62c34bdSAndy Lutomirski vma->vm_ops = ops; 3402a62c34bdSAndy Lutomirski vma->vm_private_data = priv; 3403fa5dc22fSRoland McGrath 3404462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 3405462e635eSTavis Ormandy if (ret) 3406462e635eSTavis Ormandy goto out; 3407fa5dc22fSRoland McGrath 340884638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); 3409fa5dc22fSRoland McGrath 3410cdd6c482SIngo Molnar perf_event_mmap(vma); 3411089dd79dSPeter Zijlstra 3412d4af56c5SLiam R. Howlett validate_mm_mt(mm); 34133935ed6aSStefani Seibold return vma; 3414462e635eSTavis Ormandy 3415462e635eSTavis Ormandy out: 34163928d4f5SLinus Torvalds vm_area_free(vma); 3417d4af56c5SLiam R. Howlett validate_mm_mt(mm); 34183935ed6aSStefani Seibold return ERR_PTR(ret); 34193935ed6aSStefani Seibold } 34203935ed6aSStefani Seibold 34212eefd878SDmitry Safonov bool vma_is_special_mapping(const struct vm_area_struct *vma, 34222eefd878SDmitry Safonov const struct vm_special_mapping *sm) 34232eefd878SDmitry Safonov { 34242eefd878SDmitry Safonov return vma->vm_private_data == sm && 34252eefd878SDmitry Safonov (vma->vm_ops == &special_mapping_vmops || 34262eefd878SDmitry Safonov vma->vm_ops == &legacy_special_mapping_vmops); 34272eefd878SDmitry Safonov } 34282eefd878SDmitry Safonov 3429a62c34bdSAndy Lutomirski /* 3430c1e8d7c6SMichel Lespinasse * Called with mm->mmap_lock held for writing. 3431a62c34bdSAndy Lutomirski * Insert a new vma covering the given region, with the given flags. 3432a62c34bdSAndy Lutomirski * Its pages are supplied by the given array of struct page *. 3433a62c34bdSAndy Lutomirski * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 3434a62c34bdSAndy Lutomirski * The region past the last page supplied will always produce SIGBUS. 3435a62c34bdSAndy Lutomirski * The array pointer and the pages it points to are assumed to stay alive 3436a62c34bdSAndy Lutomirski * for as long as this mapping might exist. 3437a62c34bdSAndy Lutomirski */ 3438a62c34bdSAndy Lutomirski struct vm_area_struct *_install_special_mapping( 3439a62c34bdSAndy Lutomirski struct mm_struct *mm, 3440a62c34bdSAndy Lutomirski unsigned long addr, unsigned long len, 3441a62c34bdSAndy Lutomirski unsigned long vm_flags, const struct vm_special_mapping *spec) 3442a62c34bdSAndy Lutomirski { 344327f28b97SChen Gang return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, 344427f28b97SChen Gang &special_mapping_vmops); 3445a62c34bdSAndy Lutomirski } 3446a62c34bdSAndy Lutomirski 34473935ed6aSStefani Seibold int install_special_mapping(struct mm_struct *mm, 34483935ed6aSStefani Seibold unsigned long addr, unsigned long len, 34493935ed6aSStefani Seibold unsigned long vm_flags, struct page **pages) 34503935ed6aSStefani Seibold { 3451a62c34bdSAndy Lutomirski struct vm_area_struct *vma = __install_special_mapping( 345227f28b97SChen Gang mm, addr, len, vm_flags, (void *)pages, 345327f28b97SChen Gang &legacy_special_mapping_vmops); 34543935ed6aSStefani Seibold 345514bd5b45SDuan Jiong return PTR_ERR_OR_ZERO(vma); 3456fa5dc22fSRoland McGrath } 34577906d00cSAndrea Arcangeli 34587906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 34597906d00cSAndrea Arcangeli 3460454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 34617906d00cSAndrea Arcangeli { 3462f808c13fSDavidlohr Bueso if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 34637906d00cSAndrea Arcangeli /* 34647906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 34657906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 34667906d00cSAndrea Arcangeli */ 3467da1c55f1SMichel Lespinasse down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 34687906d00cSAndrea Arcangeli /* 34697906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 34705a505085SIngo Molnar * anon_vma->root->rwsem. If some other vma in this mm shares 34717906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 34727906d00cSAndrea Arcangeli * 34737906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 34747906d00cSAndrea Arcangeli * can't change from under us thanks to the 34755a505085SIngo Molnar * anon_vma->root->rwsem. 34767906d00cSAndrea Arcangeli */ 34777906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 3478f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 34797906d00cSAndrea Arcangeli BUG(); 34807906d00cSAndrea Arcangeli } 34817906d00cSAndrea Arcangeli } 34827906d00cSAndrea Arcangeli 3483454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 34847906d00cSAndrea Arcangeli { 34857906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 34867906d00cSAndrea Arcangeli /* 34877906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 34887906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 34897906d00cSAndrea Arcangeli * 34907906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 34917906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 34927906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 34937906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 34947906d00cSAndrea Arcangeli */ 34957906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 34967906d00cSAndrea Arcangeli BUG(); 3497da1c55f1SMichel Lespinasse down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 34987906d00cSAndrea Arcangeli } 34997906d00cSAndrea Arcangeli } 35007906d00cSAndrea Arcangeli 35017906d00cSAndrea Arcangeli /* 35027906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 35037906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 35047906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 35057906d00cSAndrea Arcangeli * 3506c1e8d7c6SMichel Lespinasse * The caller must take the mmap_lock in write mode before calling 35077906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 3508c1e8d7c6SMichel Lespinasse * mmap_lock until mm_drop_all_locks() returns. 35097906d00cSAndrea Arcangeli * 3510c1e8d7c6SMichel Lespinasse * mmap_lock in write mode is required in order to block all operations 35117906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 351227ba0644SKirill A. Shutemov * altering the vma layout. It's also needed in write mode to avoid new 35137906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 35147906d00cSAndrea Arcangeli * 35157906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 35167906d00cSAndrea Arcangeli * or it would deadlock. 35177906d00cSAndrea Arcangeli * 3518bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 35197906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 35207906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 35217906d00cSAndrea Arcangeli * 352288f306b6SKirill A. Shutemov * We take locks in following order, accordingly to comment at beginning 352388f306b6SKirill A. Shutemov * of mm/rmap.c: 352488f306b6SKirill A. Shutemov * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 352588f306b6SKirill A. Shutemov * hugetlb mapping); 352688f306b6SKirill A. Shutemov * - all i_mmap_rwsem locks; 352788f306b6SKirill A. Shutemov * - all anon_vma->rwseml 352888f306b6SKirill A. Shutemov * 352988f306b6SKirill A. Shutemov * We can take all locks within these types randomly because the VM code 353088f306b6SKirill A. Shutemov * doesn't nest them and we protected from parallel mm_take_all_locks() by 353188f306b6SKirill A. Shutemov * mm_all_locks_mutex. 35327906d00cSAndrea Arcangeli * 35337906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 35347906d00cSAndrea Arcangeli * that may have to take thousand of locks. 35357906d00cSAndrea Arcangeli * 35367906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 35377906d00cSAndrea Arcangeli */ 35387906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 35397906d00cSAndrea Arcangeli { 35407906d00cSAndrea Arcangeli struct vm_area_struct *vma; 35415beb4930SRik van Riel struct anon_vma_chain *avc; 3542763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 35437906d00cSAndrea Arcangeli 3544325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 35457906d00cSAndrea Arcangeli 35467906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 35477906d00cSAndrea Arcangeli 3548763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35497906d00cSAndrea Arcangeli if (signal_pending(current)) 35507906d00cSAndrea Arcangeli goto out_unlock; 355188f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 355288f306b6SKirill A. Shutemov is_vm_hugetlb_page(vma)) 355388f306b6SKirill A. Shutemov vm_lock_mapping(mm, vma->vm_file->f_mapping); 355488f306b6SKirill A. Shutemov } 355588f306b6SKirill A. Shutemov 3556763ecb03SLiam R. Howlett mas_set(&mas, 0); 3557763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 355888f306b6SKirill A. Shutemov if (signal_pending(current)) 355988f306b6SKirill A. Shutemov goto out_unlock; 356088f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 356188f306b6SKirill A. Shutemov !is_vm_hugetlb_page(vma)) 3562454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 35637906d00cSAndrea Arcangeli } 35647cd5a02fSPeter Zijlstra 3565763ecb03SLiam R. Howlett mas_set(&mas, 0); 3566763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35677cd5a02fSPeter Zijlstra if (signal_pending(current)) 35687cd5a02fSPeter Zijlstra goto out_unlock; 35697cd5a02fSPeter Zijlstra if (vma->anon_vma) 35705beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 35715beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 35727cd5a02fSPeter Zijlstra } 35737cd5a02fSPeter Zijlstra 3574584cff54SKautuk Consul return 0; 35757906d00cSAndrea Arcangeli 35767906d00cSAndrea Arcangeli out_unlock: 35777906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 3578584cff54SKautuk Consul return -EINTR; 35797906d00cSAndrea Arcangeli } 35807906d00cSAndrea Arcangeli 35817906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 35827906d00cSAndrea Arcangeli { 3583f808c13fSDavidlohr Bueso if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 35847906d00cSAndrea Arcangeli /* 35857906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 35867906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 35877906d00cSAndrea Arcangeli * 35887906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 3589bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 35907906d00cSAndrea Arcangeli * never see our bitflag. 35917906d00cSAndrea Arcangeli * 35927906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 35937906d00cSAndrea Arcangeli * can't change from under us until we release the 35945a505085SIngo Molnar * anon_vma->root->rwsem. 35957906d00cSAndrea Arcangeli */ 35967906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 3597f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 35987906d00cSAndrea Arcangeli BUG(); 359908b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 36007906d00cSAndrea Arcangeli } 36017906d00cSAndrea Arcangeli } 36027906d00cSAndrea Arcangeli 36037906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 36047906d00cSAndrea Arcangeli { 36057906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 36067906d00cSAndrea Arcangeli /* 36077906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 36087906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 36097906d00cSAndrea Arcangeli */ 361083cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 36117906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 36127906d00cSAndrea Arcangeli &mapping->flags)) 36137906d00cSAndrea Arcangeli BUG(); 36147906d00cSAndrea Arcangeli } 36157906d00cSAndrea Arcangeli } 36167906d00cSAndrea Arcangeli 36177906d00cSAndrea Arcangeli /* 3618c1e8d7c6SMichel Lespinasse * The mmap_lock cannot be released by the caller until 36197906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 36207906d00cSAndrea Arcangeli */ 36217906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 36227906d00cSAndrea Arcangeli { 36237906d00cSAndrea Arcangeli struct vm_area_struct *vma; 36245beb4930SRik van Riel struct anon_vma_chain *avc; 3625763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 36267906d00cSAndrea Arcangeli 3627325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 36287906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 36297906d00cSAndrea Arcangeli 3630763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 36317906d00cSAndrea Arcangeli if (vma->anon_vma) 36325beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 36335beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 36347906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 36357906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 36367906d00cSAndrea Arcangeli } 36377906d00cSAndrea Arcangeli 36387906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 36397906d00cSAndrea Arcangeli } 36408feae131SDavid Howells 36418feae131SDavid Howells /* 36423edf41d8Sseokhoon.yoon * initialise the percpu counter for VM 36438feae131SDavid Howells */ 36448feae131SDavid Howells void __init mmap_init(void) 36458feae131SDavid Howells { 364600a62ce9SKOSAKI Motohiro int ret; 364700a62ce9SKOSAKI Motohiro 3648908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 364900a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 36508feae131SDavid Howells } 3651c9b1d098SAndrew Shewmaker 3652c9b1d098SAndrew Shewmaker /* 3653c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 3654c9b1d098SAndrew Shewmaker * 3655c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 3656c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 3657c9b1d098SAndrew Shewmaker * mode. 3658c9b1d098SAndrew Shewmaker * 3659c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 3660c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 3661c9b1d098SAndrew Shewmaker */ 36621640879aSAndrew Shewmaker static int init_user_reserve(void) 3663c9b1d098SAndrew Shewmaker { 3664c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 3665c9b1d098SAndrew Shewmaker 3666c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 3667c9b1d098SAndrew Shewmaker 3668c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 3669c9b1d098SAndrew Shewmaker return 0; 3670c9b1d098SAndrew Shewmaker } 3671a64fb3cdSPaul Gortmaker subsys_initcall(init_user_reserve); 36724eeab4f5SAndrew Shewmaker 36734eeab4f5SAndrew Shewmaker /* 36744eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 36754eeab4f5SAndrew Shewmaker * 36764eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 36774eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 36784eeab4f5SAndrew Shewmaker * 36794eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 36804eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 36814eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 36824eeab4f5SAndrew Shewmaker */ 36831640879aSAndrew Shewmaker static int init_admin_reserve(void) 36844eeab4f5SAndrew Shewmaker { 36854eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 36864eeab4f5SAndrew Shewmaker 3687c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 36884eeab4f5SAndrew Shewmaker 36894eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 36904eeab4f5SAndrew Shewmaker return 0; 36914eeab4f5SAndrew Shewmaker } 3692a64fb3cdSPaul Gortmaker subsys_initcall(init_admin_reserve); 36931640879aSAndrew Shewmaker 36941640879aSAndrew Shewmaker /* 36951640879aSAndrew Shewmaker * Reinititalise user and admin reserves if memory is added or removed. 36961640879aSAndrew Shewmaker * 36971640879aSAndrew Shewmaker * The default user reserve max is 128MB, and the default max for the 36981640879aSAndrew Shewmaker * admin reserve is 8MB. These are usually, but not always, enough to 36991640879aSAndrew Shewmaker * enable recovery from a memory hogging process using login/sshd, a shell, 37001640879aSAndrew Shewmaker * and tools like top. It may make sense to increase or even disable the 37011640879aSAndrew Shewmaker * reserve depending on the existence of swap or variations in the recovery 37021640879aSAndrew Shewmaker * tools. So, the admin may have changed them. 37031640879aSAndrew Shewmaker * 37041640879aSAndrew Shewmaker * If memory is added and the reserves have been eliminated or increased above 37051640879aSAndrew Shewmaker * the default max, then we'll trust the admin. 37061640879aSAndrew Shewmaker * 37071640879aSAndrew Shewmaker * If memory is removed and there isn't enough free memory, then we 37081640879aSAndrew Shewmaker * need to reset the reserves. 37091640879aSAndrew Shewmaker * 37101640879aSAndrew Shewmaker * Otherwise keep the reserve set by the admin. 37111640879aSAndrew Shewmaker */ 37121640879aSAndrew Shewmaker static int reserve_mem_notifier(struct notifier_block *nb, 37131640879aSAndrew Shewmaker unsigned long action, void *data) 37141640879aSAndrew Shewmaker { 37151640879aSAndrew Shewmaker unsigned long tmp, free_kbytes; 37161640879aSAndrew Shewmaker 37171640879aSAndrew Shewmaker switch (action) { 37181640879aSAndrew Shewmaker case MEM_ONLINE: 37191640879aSAndrew Shewmaker /* Default max is 128MB. Leave alone if modified by operator. */ 37201640879aSAndrew Shewmaker tmp = sysctl_user_reserve_kbytes; 37211640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 17)) 37221640879aSAndrew Shewmaker init_user_reserve(); 37231640879aSAndrew Shewmaker 37241640879aSAndrew Shewmaker /* Default max is 8MB. Leave alone if modified by operator. */ 37251640879aSAndrew Shewmaker tmp = sysctl_admin_reserve_kbytes; 37261640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 13)) 37271640879aSAndrew Shewmaker init_admin_reserve(); 37281640879aSAndrew Shewmaker 37291640879aSAndrew Shewmaker break; 37301640879aSAndrew Shewmaker case MEM_OFFLINE: 3731c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37321640879aSAndrew Shewmaker 37331640879aSAndrew Shewmaker if (sysctl_user_reserve_kbytes > free_kbytes) { 37341640879aSAndrew Shewmaker init_user_reserve(); 37351640879aSAndrew Shewmaker pr_info("vm.user_reserve_kbytes reset to %lu\n", 37361640879aSAndrew Shewmaker sysctl_user_reserve_kbytes); 37371640879aSAndrew Shewmaker } 37381640879aSAndrew Shewmaker 37391640879aSAndrew Shewmaker if (sysctl_admin_reserve_kbytes > free_kbytes) { 37401640879aSAndrew Shewmaker init_admin_reserve(); 37411640879aSAndrew Shewmaker pr_info("vm.admin_reserve_kbytes reset to %lu\n", 37421640879aSAndrew Shewmaker sysctl_admin_reserve_kbytes); 37431640879aSAndrew Shewmaker } 37441640879aSAndrew Shewmaker break; 37451640879aSAndrew Shewmaker default: 37461640879aSAndrew Shewmaker break; 37471640879aSAndrew Shewmaker } 37481640879aSAndrew Shewmaker return NOTIFY_OK; 37491640879aSAndrew Shewmaker } 37501640879aSAndrew Shewmaker 37511640879aSAndrew Shewmaker static int __meminit init_reserve_notifier(void) 37521640879aSAndrew Shewmaker { 37531eeaa4fdSLiu Shixin if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI)) 3754b1de0d13SMitchel Humpherys pr_err("Failed registering memory add/remove notifier for admin reserve\n"); 37551640879aSAndrew Shewmaker 37561640879aSAndrew Shewmaker return 0; 37571640879aSAndrew Shewmaker } 3758a64fb3cdSPaul Gortmaker subsys_initcall(init_reserve_notifier); 3759