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> 49d7597f59SStefan Roesch #include <linux/ksm.h> 501da177e4SLinus Torvalds 517c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 521da177e4SLinus Torvalds #include <asm/cacheflush.h> 531da177e4SLinus Torvalds #include <asm/tlb.h> 54d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h> 551da177e4SLinus Torvalds 56df529cabSJaewon Kim #define CREATE_TRACE_POINTS 57df529cabSJaewon Kim #include <trace/events/mmap.h> 58df529cabSJaewon Kim 5942b77728SJan Beulich #include "internal.h" 6042b77728SJan Beulich 613a459756SKirill Korotaev #ifndef arch_mmap_check 623a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags) (0) 633a459756SKirill Korotaev #endif 643a459756SKirill Korotaev 65d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS 66d07e2259SDaniel Cashman const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; 67d07e2259SDaniel Cashman const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; 68d07e2259SDaniel Cashman int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; 69d07e2259SDaniel Cashman #endif 70d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS 71d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; 72d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; 73d07e2259SDaniel Cashman int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; 74d07e2259SDaniel Cashman #endif 75d07e2259SDaniel Cashman 76f4fcd558SKonstantin Khlebnikov static bool ignore_rlimit_data; 77d977d56cSKonstantin Khlebnikov core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); 78d07e2259SDaniel Cashman 79763ecb03SLiam R. Howlett static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, 80e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 81763ecb03SLiam R. Howlett struct vm_area_struct *next, unsigned long start, 8268f48381SSuren Baghdasaryan unsigned long end, bool mm_wr_locked); 83e0da382cSHugh Dickins 8464e45507SPeter Feiner static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) 8564e45507SPeter Feiner { 8664e45507SPeter Feiner return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); 8764e45507SPeter Feiner } 8864e45507SPeter Feiner 8964e45507SPeter Feiner /* Update vma->vm_page_prot to reflect vma->vm_flags. */ 9064e45507SPeter Feiner void vma_set_page_prot(struct vm_area_struct *vma) 9164e45507SPeter Feiner { 9264e45507SPeter Feiner unsigned long vm_flags = vma->vm_flags; 936d2329f8SAndrea Arcangeli pgprot_t vm_page_prot; 9464e45507SPeter Feiner 956d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); 966d2329f8SAndrea Arcangeli if (vma_wants_writenotify(vma, vm_page_prot)) { 9764e45507SPeter Feiner vm_flags &= ~VM_SHARED; 986d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); 9964e45507SPeter Feiner } 100c1e8d7c6SMichel Lespinasse /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ 1016d2329f8SAndrea Arcangeli WRITE_ONCE(vma->vm_page_prot, vm_page_prot); 10264e45507SPeter Feiner } 10364e45507SPeter Feiner 1041da177e4SLinus Torvalds /* 105c8c06efaSDavidlohr Bueso * Requires inode->i_mapping->i_mmap_rwsem 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1081da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1114bb5f5d9SDavid Herrmann mapping_unmap_writable(mapping); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 1146b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 1151da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 1196b2dbba8SMichel Lespinasse * Unlink a file-based vm structure from its interval tree, to hide 120a8fb5618SHugh Dickins * vma from rmap and vmtruncate before freeing its page tables. 1211da177e4SLinus Torvalds */ 122a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds struct file *file = vma->vm_file; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds if (file) { 1271da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 12883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 1291da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 13083cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 1311da177e4SLinus Torvalds } 132a8fb5618SHugh Dickins } 133a8fb5618SHugh Dickins 134a8fb5618SHugh Dickins /* 135763ecb03SLiam R. Howlett * Close a vm structure and free it. 136a8fb5618SHugh Dickins */ 1370d2ebf9cSSuren Baghdasaryan static void remove_vma(struct vm_area_struct *vma, bool unreachable) 138a8fb5618SHugh Dickins { 139a8fb5618SHugh Dickins might_sleep(); 1401da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 1411da177e4SLinus Torvalds vma->vm_ops->close(vma); 142e9714acfSKonstantin Khlebnikov if (vma->vm_file) 143a8fb5618SHugh Dickins fput(vma->vm_file); 144f0be3d32SLee Schermerhorn mpol_put(vma_policy(vma)); 1450d2ebf9cSSuren Baghdasaryan if (unreachable) 1460d2ebf9cSSuren Baghdasaryan __vm_area_free(vma); 1470d2ebf9cSSuren Baghdasaryan else 1483928d4f5SLinus Torvalds vm_area_free(vma); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 151b62b633eSLiam R. Howlett static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, 152b62b633eSLiam R. Howlett unsigned long min) 153b62b633eSLiam R. Howlett { 154b62b633eSLiam R. Howlett return mas_prev(&vmi->mas, min); 155b62b633eSLiam R. Howlett } 156b62b633eSLiam R. Howlett 157b62b633eSLiam R. Howlett static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, 158b62b633eSLiam R. Howlett unsigned long start, unsigned long end, gfp_t gfp) 159b62b633eSLiam R. Howlett { 160b62b633eSLiam R. Howlett vmi->mas.index = start; 161b62b633eSLiam R. Howlett vmi->mas.last = end - 1; 162b62b633eSLiam R. Howlett mas_store_gfp(&vmi->mas, NULL, gfp); 163b62b633eSLiam R. Howlett if (unlikely(mas_is_err(&vmi->mas))) 164b62b633eSLiam R. Howlett return -ENOMEM; 165b62b633eSLiam R. Howlett 166b62b633eSLiam R. Howlett return 0; 167b62b633eSLiam R. Howlett } 168b62b633eSLiam R. Howlett 1692e7ce7d3SLiam R. Howlett /* 1702e7ce7d3SLiam R. Howlett * check_brk_limits() - Use platform specific check of range & verify mlock 1712e7ce7d3SLiam R. Howlett * limits. 1722e7ce7d3SLiam R. Howlett * @addr: The address to check 1732e7ce7d3SLiam R. Howlett * @len: The size of increase. 1742e7ce7d3SLiam R. Howlett * 1752e7ce7d3SLiam R. Howlett * Return: 0 on success. 1762e7ce7d3SLiam R. Howlett */ 1772e7ce7d3SLiam R. Howlett static int check_brk_limits(unsigned long addr, unsigned long len) 1782e7ce7d3SLiam R. Howlett { 1792e7ce7d3SLiam R. Howlett unsigned long mapped_addr; 1802e7ce7d3SLiam R. Howlett 1812e7ce7d3SLiam R. Howlett mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 1822e7ce7d3SLiam R. Howlett if (IS_ERR_VALUE(mapped_addr)) 1832e7ce7d3SLiam R. Howlett return mapped_addr; 1842e7ce7d3SLiam R. Howlett 185*3c54a298SLorenzo Stoakes return mlock_future_check(current->mm, current->mm->def_flags, len) 186*3c54a298SLorenzo Stoakes ? 0 : -EAGAIN; 1872e7ce7d3SLiam R. Howlett } 18892fed820SLiam R. Howlett static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, 189763ecb03SLiam R. Howlett unsigned long addr, unsigned long request, unsigned long flags); 1906a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 1911da177e4SLinus Torvalds { 1929bc8039eSYang Shi unsigned long newbrk, oldbrk, origbrk; 1931da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1942e7ce7d3SLiam R. Howlett struct vm_area_struct *brkvma, *next = NULL; 195a5b4592cSJiri Kosina unsigned long min_brk; 196128557ffSMichel Lespinasse bool populate; 1979bc8039eSYang Shi bool downgraded = false; 198897ab3e0SMike Rapoport LIST_HEAD(uf); 19992fed820SLiam R. Howlett struct vma_iterator vmi; 2001da177e4SLinus Torvalds 201d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 202dc0ef0dfSMichal Hocko return -EINTR; 2031da177e4SLinus Torvalds 2049bc8039eSYang Shi origbrk = mm->brk; 2059bc8039eSYang Shi 206a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 2075520e894SJiri Kosina /* 2085520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 2095520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 2105520e894SJiri Kosina * to be arbitrarily shifted 2115520e894SJiri Kosina */ 2124471a675SJiri Kosina if (current->brk_randomized) 2135520e894SJiri Kosina min_brk = mm->start_brk; 2145520e894SJiri Kosina else 2155520e894SJiri Kosina min_brk = mm->end_data; 216a5b4592cSJiri Kosina #else 217a5b4592cSJiri Kosina min_brk = mm->start_brk; 218a5b4592cSJiri Kosina #endif 219a5b4592cSJiri Kosina if (brk < min_brk) 2201da177e4SLinus Torvalds goto out; 2211e624196SRam Gupta 2221e624196SRam Gupta /* 2231e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2241e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2251e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2261e624196SRam Gupta * not page aligned -Ram Gupta 2271e624196SRam Gupta */ 2288764b338SCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, 2298764b338SCyrill Gorcunov mm->end_data, mm->start_data)) 2301e624196SRam Gupta goto out; 2311e624196SRam Gupta 2321da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2331da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2349bc8039eSYang Shi if (oldbrk == newbrk) { 2359bc8039eSYang Shi mm->brk = brk; 2369bc8039eSYang Shi goto success; 2379bc8039eSYang Shi } 2381da177e4SLinus Torvalds 2399bc8039eSYang Shi /* 2409bc8039eSYang Shi * Always allow shrinking brk. 24127b26701SLiam R. Howlett * do_vma_munmap() may downgrade mmap_lock to read. 2429bc8039eSYang Shi */ 2431da177e4SLinus Torvalds if (brk <= mm->brk) { 2449bc8039eSYang Shi int ret; 2459bc8039eSYang Shi 2462e7ce7d3SLiam R. Howlett /* Search one past newbrk */ 24792fed820SLiam R. Howlett vma_iter_init(&vmi, mm, newbrk); 24892fed820SLiam R. Howlett brkvma = vma_find(&vmi, oldbrk); 249f5ad5083SJason A. Donenfeld if (!brkvma || brkvma->vm_start >= oldbrk) 2502e7ce7d3SLiam R. Howlett goto out; /* mapping intersects with an existing non-brk vma. */ 2519bc8039eSYang Shi /* 2522e7ce7d3SLiam R. Howlett * mm->brk must be protected by write mmap_lock. 25327b26701SLiam R. Howlett * do_vma_munmap() may downgrade the lock, so update it 25427b26701SLiam R. Howlett * before calling do_vma_munmap(). 2559bc8039eSYang Shi */ 2569bc8039eSYang Shi mm->brk = brk; 25727b26701SLiam R. Howlett ret = do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true); 2582e7ce7d3SLiam R. Howlett if (ret == 1) { 2592e7ce7d3SLiam R. Howlett downgraded = true; 2602e7ce7d3SLiam R. Howlett goto success; 2612e7ce7d3SLiam R. Howlett } else if (!ret) 2622e7ce7d3SLiam R. Howlett goto success; 2632e7ce7d3SLiam R. Howlett 2649bc8039eSYang Shi mm->brk = origbrk; 2651da177e4SLinus Torvalds goto out; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2682e7ce7d3SLiam R. Howlett if (check_brk_limits(oldbrk, newbrk - oldbrk)) 2692e7ce7d3SLiam R. Howlett goto out; 2702e7ce7d3SLiam R. Howlett 2712e7ce7d3SLiam R. Howlett /* 2722e7ce7d3SLiam R. Howlett * Only check if the next VMA is within the stack_guard_gap of the 2732e7ce7d3SLiam R. Howlett * expansion area 2742e7ce7d3SLiam R. Howlett */ 27592fed820SLiam R. Howlett vma_iter_init(&vmi, mm, oldbrk); 27692fed820SLiam R. Howlett next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap); 2771be7107fSHugh Dickins if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) 2781da177e4SLinus Torvalds goto out; 2791da177e4SLinus Torvalds 28092fed820SLiam R. Howlett brkvma = vma_prev_limit(&vmi, mm->start_brk); 2811da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 28292fed820SLiam R. Howlett if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) 2831da177e4SLinus Torvalds goto out; 2842e7ce7d3SLiam R. Howlett 2851da177e4SLinus Torvalds mm->brk = brk; 2869bc8039eSYang Shi 2879bc8039eSYang Shi success: 288128557ffSMichel Lespinasse populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; 2899bc8039eSYang Shi if (downgraded) 290d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2919bc8039eSYang Shi else 292d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 293897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 294128557ffSMichel Lespinasse if (populate) 295128557ffSMichel Lespinasse mm_populate(oldbrk, newbrk - oldbrk); 296128557ffSMichel Lespinasse return brk; 297128557ffSMichel Lespinasse 2981da177e4SLinus Torvalds out: 299d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 300b7204006SAdrian Huang return origbrk; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 303d4af56c5SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 304eafd4dc4SRashika Kheria static void validate_mm(struct mm_struct *mm) 3051da177e4SLinus Torvalds { 3061da177e4SLinus Torvalds int bug = 0; 3071da177e4SLinus Torvalds int i = 0; 308763ecb03SLiam R. Howlett struct vm_area_struct *vma; 309b50e195fSLiam R. Howlett VMA_ITERATOR(vmi, mm, 0); 310ff26f70fSAndrew Morton 311b50e195fSLiam R. Howlett mt_validate(&mm->mm_mt); 312b50e195fSLiam R. Howlett for_each_vma(vmi, vma) { 313524e00b3SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 31412352d3cSKonstantin Khlebnikov struct anon_vma *anon_vma = vma->anon_vma; 315ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 316b50e195fSLiam R. Howlett #endif 317b50e195fSLiam R. Howlett unsigned long vmi_start, vmi_end; 318b50e195fSLiam R. Howlett bool warn = 0; 319ff26f70fSAndrew Morton 320b50e195fSLiam R. Howlett vmi_start = vma_iter_addr(&vmi); 321b50e195fSLiam R. Howlett vmi_end = vma_iter_end(&vmi); 322b50e195fSLiam R. Howlett if (VM_WARN_ON_ONCE_MM(vma->vm_end != vmi_end, mm)) 323b50e195fSLiam R. Howlett warn = 1; 324b50e195fSLiam R. Howlett 325b50e195fSLiam R. Howlett if (VM_WARN_ON_ONCE_MM(vma->vm_start != vmi_start, mm)) 326b50e195fSLiam R. Howlett warn = 1; 327b50e195fSLiam R. Howlett 328b50e195fSLiam R. Howlett if (warn) { 329b50e195fSLiam R. Howlett pr_emerg("issue in %s\n", current->comm); 330b50e195fSLiam R. Howlett dump_stack(); 331b50e195fSLiam R. Howlett dump_vma(vma); 332b50e195fSLiam R. Howlett pr_emerg("tree range: %px start %lx end %lx\n", vma, 333b50e195fSLiam R. Howlett vmi_start, vmi_end - 1); 334b50e195fSLiam R. Howlett vma_iter_dump_tree(&vmi); 335b50e195fSLiam R. Howlett } 336b50e195fSLiam R. Howlett 337b50e195fSLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 33812352d3cSKonstantin Khlebnikov if (anon_vma) { 33912352d3cSKonstantin Khlebnikov anon_vma_lock_read(anon_vma); 340ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 341ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 34212352d3cSKonstantin Khlebnikov anon_vma_unlock_read(anon_vma); 34312352d3cSKonstantin Khlebnikov } 344524e00b3SLiam R. Howlett #endif 3451da177e4SLinus Torvalds i++; 3461da177e4SLinus Torvalds } 3475a0768f6SMichel Lespinasse if (i != mm->map_count) { 348b50e195fSLiam R. Howlett pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i); 3495a0768f6SMichel Lespinasse bug = 1; 3505a0768f6SMichel Lespinasse } 35196dad67fSSasha Levin VM_BUG_ON_MM(bug, mm); 3521da177e4SLinus Torvalds } 353524e00b3SLiam R. Howlett 354524e00b3SLiam R. Howlett #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ 3551da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 356524e00b3SLiam R. Howlett #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 357d3737187SMichel Lespinasse 358bf181b9fSMichel Lespinasse /* 359bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 360bf181b9fSMichel Lespinasse * anon_vma's interval trees. 361bf181b9fSMichel Lespinasse * 362bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 363bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 364bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 365bf181b9fSMichel Lespinasse * 366bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 367bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 368bf181b9fSMichel Lespinasse * 369c1e8d7c6SMichel Lespinasse * The entire update must be protected by exclusive mmap_lock and by 370bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 371bf181b9fSMichel Lespinasse */ 372bf181b9fSMichel Lespinasse static inline void 373bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 374bf181b9fSMichel Lespinasse { 375bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 376bf181b9fSMichel Lespinasse 377bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 378bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 379bf181b9fSMichel Lespinasse } 380bf181b9fSMichel Lespinasse 381bf181b9fSMichel Lespinasse static inline void 382bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 383bf181b9fSMichel Lespinasse { 384bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 385bf181b9fSMichel Lespinasse 386bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 387bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 388bf181b9fSMichel Lespinasse } 389bf181b9fSMichel Lespinasse 390e8420a8eSCyril Hrubis static unsigned long count_vma_pages_range(struct mm_struct *mm, 391e8420a8eSCyril Hrubis unsigned long addr, unsigned long end) 392e8420a8eSCyril Hrubis { 3932e3af1dbSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, addr); 394e8420a8eSCyril Hrubis struct vm_area_struct *vma; 3952e3af1dbSMatthew Wilcox (Oracle) unsigned long nr_pages = 0; 396e8420a8eSCyril Hrubis 3972e3af1dbSMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) { 3982e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_start = max(addr, vma->vm_start); 3992e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_end = min(end, vma->vm_end); 400e8420a8eSCyril Hrubis 4012e3af1dbSMatthew Wilcox (Oracle) nr_pages += PHYS_PFN(vm_end - vm_start); 402e8420a8eSCyril Hrubis } 403e8420a8eSCyril Hrubis 404e8420a8eSCyril Hrubis return nr_pages; 405e8420a8eSCyril Hrubis } 406e8420a8eSCyril Hrubis 407c154124fSLiam R. Howlett static void __vma_link_file(struct vm_area_struct *vma, 408c154124fSLiam R. Howlett struct address_space *mapping) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 411cf508b58SMiaohe Lin mapping_allow_writable(mapping); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 4146b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 4151da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 418763ecb03SLiam R. Howlett static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) 4191da177e4SLinus Torvalds { 42079e4f2caSLiam R. Howlett VMA_ITERATOR(vmi, mm, 0); 4211da177e4SLinus Torvalds struct address_space *mapping = NULL; 4221da177e4SLinus Torvalds 42379e4f2caSLiam R. Howlett if (vma_iter_prealloc(&vmi)) 424d4af56c5SLiam R. Howlett return -ENOMEM; 425d4af56c5SLiam R. Howlett 42664ac4940SHuang Shijie if (vma->vm_file) { 4271da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 42883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 42964ac4940SHuang Shijie } 4301da177e4SLinus Torvalds 43179e4f2caSLiam R. Howlett vma_iter_store(&vmi, vma); 4321da177e4SLinus Torvalds 433c154124fSLiam R. Howlett if (mapping) { 434c154124fSLiam R. Howlett __vma_link_file(vma, mapping); 43583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 436c154124fSLiam R. Howlett } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds mm->map_count++; 4391da177e4SLinus Torvalds validate_mm(mm); 440d4af56c5SLiam R. Howlett return 0; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* 44468cefec5SLiam R. Howlett * init_multi_vma_prep() - Initializer for struct vma_prepare 44568cefec5SLiam R. Howlett * @vp: The vma_prepare struct 44668cefec5SLiam R. Howlett * @vma: The vma that will be altered once locked 44768cefec5SLiam R. Howlett * @next: The next vma if it is to be adjusted 44868cefec5SLiam R. Howlett * @remove: The first vma to be removed 44968cefec5SLiam R. Howlett * @remove2: The second vma to be removed 45068cefec5SLiam R. Howlett */ 45168cefec5SLiam R. Howlett static inline void init_multi_vma_prep(struct vma_prepare *vp, 45268cefec5SLiam R. Howlett struct vm_area_struct *vma, struct vm_area_struct *next, 45368cefec5SLiam R. Howlett struct vm_area_struct *remove, struct vm_area_struct *remove2) 45468cefec5SLiam R. Howlett { 45568cefec5SLiam R. Howlett memset(vp, 0, sizeof(struct vma_prepare)); 45668cefec5SLiam R. Howlett vp->vma = vma; 45768cefec5SLiam R. Howlett vp->anon_vma = vma->anon_vma; 45868cefec5SLiam R. Howlett vp->remove = remove; 45968cefec5SLiam R. Howlett vp->remove2 = remove2; 46068cefec5SLiam R. Howlett vp->adj_next = next; 46168cefec5SLiam R. Howlett if (!vp->anon_vma && next) 46268cefec5SLiam R. Howlett vp->anon_vma = next->anon_vma; 46368cefec5SLiam R. Howlett 46468cefec5SLiam R. Howlett vp->file = vma->vm_file; 46568cefec5SLiam R. Howlett if (vp->file) 46668cefec5SLiam R. Howlett vp->mapping = vma->vm_file->f_mapping; 46768cefec5SLiam R. Howlett 46868cefec5SLiam R. Howlett } 46968cefec5SLiam R. Howlett 47068cefec5SLiam R. Howlett /* 47168cefec5SLiam R. Howlett * init_vma_prep() - Initializer wrapper for vma_prepare struct 47268cefec5SLiam R. Howlett * @vp: The vma_prepare struct 47368cefec5SLiam R. Howlett * @vma: The vma that will be altered once locked 47468cefec5SLiam R. Howlett */ 47568cefec5SLiam R. Howlett static inline void init_vma_prep(struct vma_prepare *vp, 47668cefec5SLiam R. Howlett struct vm_area_struct *vma) 47768cefec5SLiam R. Howlett { 47868cefec5SLiam R. Howlett init_multi_vma_prep(vp, vma, NULL, NULL, NULL); 47968cefec5SLiam R. Howlett } 48068cefec5SLiam R. Howlett 48168cefec5SLiam R. Howlett 48268cefec5SLiam R. Howlett /* 483440703e0SLiam R. Howlett * vma_prepare() - Helper function for handling locking VMAs prior to altering 484440703e0SLiam R. Howlett * @vp: The initialized vma_prepare struct 485440703e0SLiam R. Howlett */ 486440703e0SLiam R. Howlett static inline void vma_prepare(struct vma_prepare *vp) 487440703e0SLiam R. Howlett { 48810fca64aSSuren Baghdasaryan vma_start_write(vp->vma); 48910fca64aSSuren Baghdasaryan if (vp->adj_next) 49010fca64aSSuren Baghdasaryan vma_start_write(vp->adj_next); 49110fca64aSSuren Baghdasaryan /* vp->insert is always a newly created VMA, no need for locking */ 49210fca64aSSuren Baghdasaryan if (vp->remove) 49310fca64aSSuren Baghdasaryan vma_start_write(vp->remove); 49410fca64aSSuren Baghdasaryan if (vp->remove2) 49510fca64aSSuren Baghdasaryan vma_start_write(vp->remove2); 49610fca64aSSuren Baghdasaryan 497440703e0SLiam R. Howlett if (vp->file) { 498440703e0SLiam R. Howlett uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); 499440703e0SLiam R. Howlett 500440703e0SLiam R. Howlett if (vp->adj_next) 501440703e0SLiam R. Howlett uprobe_munmap(vp->adj_next, vp->adj_next->vm_start, 502440703e0SLiam R. Howlett vp->adj_next->vm_end); 503440703e0SLiam R. Howlett 504440703e0SLiam R. Howlett i_mmap_lock_write(vp->mapping); 505440703e0SLiam R. Howlett if (vp->insert && vp->insert->vm_file) { 506440703e0SLiam R. Howlett /* 507440703e0SLiam R. Howlett * Put into interval tree now, so instantiated pages 508440703e0SLiam R. Howlett * are visible to arm/parisc __flush_dcache_page 509440703e0SLiam R. Howlett * throughout; but we cannot insert into address 510440703e0SLiam R. Howlett * space until vma start or end is updated. 511440703e0SLiam R. Howlett */ 512440703e0SLiam R. Howlett __vma_link_file(vp->insert, 513440703e0SLiam R. Howlett vp->insert->vm_file->f_mapping); 514440703e0SLiam R. Howlett } 515440703e0SLiam R. Howlett } 516440703e0SLiam R. Howlett 517440703e0SLiam R. Howlett if (vp->anon_vma) { 518440703e0SLiam R. Howlett anon_vma_lock_write(vp->anon_vma); 519440703e0SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vp->vma); 520440703e0SLiam R. Howlett if (vp->adj_next) 521440703e0SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vp->adj_next); 522440703e0SLiam R. Howlett } 523440703e0SLiam R. Howlett 524440703e0SLiam R. Howlett if (vp->file) { 525440703e0SLiam R. Howlett flush_dcache_mmap_lock(vp->mapping); 526440703e0SLiam R. Howlett vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap); 527440703e0SLiam R. Howlett if (vp->adj_next) 528440703e0SLiam R. Howlett vma_interval_tree_remove(vp->adj_next, 529440703e0SLiam R. Howlett &vp->mapping->i_mmap); 530440703e0SLiam R. Howlett } 531440703e0SLiam R. Howlett 532440703e0SLiam R. Howlett } 533440703e0SLiam R. Howlett 534440703e0SLiam R. Howlett /* 535440703e0SLiam R. Howlett * vma_complete- Helper function for handling the unlocking after altering VMAs, 536440703e0SLiam R. Howlett * or for inserting a VMA. 537440703e0SLiam R. Howlett * 538440703e0SLiam R. Howlett * @vp: The vma_prepare struct 539440703e0SLiam R. Howlett * @vmi: The vma iterator 540440703e0SLiam R. Howlett * @mm: The mm_struct 541440703e0SLiam R. Howlett */ 542440703e0SLiam R. Howlett static inline void vma_complete(struct vma_prepare *vp, 543440703e0SLiam R. Howlett struct vma_iterator *vmi, struct mm_struct *mm) 544440703e0SLiam R. Howlett { 545440703e0SLiam R. Howlett if (vp->file) { 546440703e0SLiam R. Howlett if (vp->adj_next) 547440703e0SLiam R. Howlett vma_interval_tree_insert(vp->adj_next, 548440703e0SLiam R. Howlett &vp->mapping->i_mmap); 549440703e0SLiam R. Howlett vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap); 550440703e0SLiam R. Howlett flush_dcache_mmap_unlock(vp->mapping); 551440703e0SLiam R. Howlett } 552440703e0SLiam R. Howlett 553440703e0SLiam R. Howlett if (vp->remove && vp->file) { 554440703e0SLiam R. Howlett __remove_shared_vm_struct(vp->remove, vp->file, vp->mapping); 555440703e0SLiam R. Howlett if (vp->remove2) 556440703e0SLiam R. Howlett __remove_shared_vm_struct(vp->remove2, vp->file, 557440703e0SLiam R. Howlett vp->mapping); 558440703e0SLiam R. Howlett } else if (vp->insert) { 559440703e0SLiam R. Howlett /* 560440703e0SLiam R. Howlett * split_vma has split insert from vma, and needs 561440703e0SLiam R. Howlett * us to insert it before dropping the locks 562440703e0SLiam R. Howlett * (it may either follow vma or precede it). 563440703e0SLiam R. Howlett */ 564440703e0SLiam R. Howlett vma_iter_store(vmi, vp->insert); 565440703e0SLiam R. Howlett mm->map_count++; 566440703e0SLiam R. Howlett } 567440703e0SLiam R. Howlett 568440703e0SLiam R. Howlett if (vp->anon_vma) { 569440703e0SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vp->vma); 570440703e0SLiam R. Howlett if (vp->adj_next) 571440703e0SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vp->adj_next); 572440703e0SLiam R. Howlett anon_vma_unlock_write(vp->anon_vma); 573440703e0SLiam R. Howlett } 574440703e0SLiam R. Howlett 575440703e0SLiam R. Howlett if (vp->file) { 576440703e0SLiam R. Howlett i_mmap_unlock_write(vp->mapping); 577440703e0SLiam R. Howlett uprobe_mmap(vp->vma); 578440703e0SLiam R. Howlett 579440703e0SLiam R. Howlett if (vp->adj_next) 580440703e0SLiam R. Howlett uprobe_mmap(vp->adj_next); 581440703e0SLiam R. Howlett } 582440703e0SLiam R. Howlett 583440703e0SLiam R. Howlett if (vp->remove) { 584440703e0SLiam R. Howlett again: 585457f67beSSuren Baghdasaryan vma_mark_detached(vp->remove, true); 586440703e0SLiam R. Howlett if (vp->file) { 587440703e0SLiam R. Howlett uprobe_munmap(vp->remove, vp->remove->vm_start, 588440703e0SLiam R. Howlett vp->remove->vm_end); 589440703e0SLiam R. Howlett fput(vp->file); 590440703e0SLiam R. Howlett } 591440703e0SLiam R. Howlett if (vp->remove->anon_vma) 592440703e0SLiam R. Howlett anon_vma_merge(vp->vma, vp->remove); 593440703e0SLiam R. Howlett mm->map_count--; 594440703e0SLiam R. Howlett mpol_put(vma_policy(vp->remove)); 595440703e0SLiam R. Howlett if (!vp->remove2) 596440703e0SLiam R. Howlett WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end); 597440703e0SLiam R. Howlett vm_area_free(vp->remove); 598440703e0SLiam R. Howlett 599440703e0SLiam R. Howlett /* 600440703e0SLiam R. Howlett * In mprotect's case 6 (see comments on vma_merge), 6015ff783f1SVlastimil Babka * we are removing both mid and next vmas 602440703e0SLiam R. Howlett */ 603440703e0SLiam R. Howlett if (vp->remove2) { 604440703e0SLiam R. Howlett vp->remove = vp->remove2; 605440703e0SLiam R. Howlett vp->remove2 = NULL; 606440703e0SLiam R. Howlett goto again; 607440703e0SLiam R. Howlett } 608440703e0SLiam R. Howlett } 609440703e0SLiam R. Howlett if (vp->insert && vp->file) 610440703e0SLiam R. Howlett uprobe_mmap(vp->insert); 611440703e0SLiam R. Howlett } 612440703e0SLiam R. Howlett 613440703e0SLiam R. Howlett /* 61404241ffeSLiam R. Howlett * dup_anon_vma() - Helper function to duplicate anon_vma 61504241ffeSLiam R. Howlett * @dst: The destination VMA 61604241ffeSLiam R. Howlett * @src: The source VMA 61704241ffeSLiam R. Howlett * 61804241ffeSLiam R. Howlett * Returns: 0 on success. 61904241ffeSLiam R. Howlett */ 62004241ffeSLiam R. Howlett static inline int dup_anon_vma(struct vm_area_struct *dst, 62104241ffeSLiam R. Howlett struct vm_area_struct *src) 62204241ffeSLiam R. Howlett { 62304241ffeSLiam R. Howlett /* 62404241ffeSLiam R. Howlett * Easily overlooked: when mprotect shifts the boundary, make sure the 62504241ffeSLiam R. Howlett * expanding vma has anon_vma set if the shrinking vma had, to cover any 62604241ffeSLiam R. Howlett * anon pages imported. 62704241ffeSLiam R. Howlett */ 62804241ffeSLiam R. Howlett if (src->anon_vma && !dst->anon_vma) { 62904241ffeSLiam R. Howlett dst->anon_vma = src->anon_vma; 63004241ffeSLiam R. Howlett return anon_vma_clone(dst, src); 63104241ffeSLiam R. Howlett } 63204241ffeSLiam R. Howlett 63304241ffeSLiam R. Howlett return 0; 63404241ffeSLiam R. Howlett } 63504241ffeSLiam R. Howlett 63604241ffeSLiam R. Howlett /* 6379303d3e1SLiam R. Howlett * vma_expand - Expand an existing VMA 6389303d3e1SLiam R. Howlett * 6399303d3e1SLiam R. Howlett * @vmi: The vma iterator 6409303d3e1SLiam R. Howlett * @vma: The vma to expand 6419303d3e1SLiam R. Howlett * @start: The start of the vma 6429303d3e1SLiam R. Howlett * @end: The exclusive end of the vma 6439303d3e1SLiam R. Howlett * @pgoff: The page offset of vma 6449303d3e1SLiam R. Howlett * @next: The current of next vma. 6459303d3e1SLiam R. Howlett * 6469303d3e1SLiam R. Howlett * Expand @vma to @start and @end. Can expand off the start and end. Will 6479303d3e1SLiam R. Howlett * expand over @next if it's different from @vma and @end == @next->vm_end. 6489303d3e1SLiam R. Howlett * Checking if the @vma can expand and merge with @next needs to be handled by 6499303d3e1SLiam R. Howlett * the caller. 6509303d3e1SLiam R. Howlett * 6519303d3e1SLiam R. Howlett * Returns: 0 on success 6529303d3e1SLiam R. Howlett */ 6537c9813e8SLiam R. Howlett int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, 6549303d3e1SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 6559303d3e1SLiam R. Howlett struct vm_area_struct *next) 6569303d3e1SLiam R. Howlett { 65768cefec5SLiam R. Howlett bool remove_next = false; 6589303d3e1SLiam R. Howlett struct vma_prepare vp; 6599303d3e1SLiam R. Howlett 6609303d3e1SLiam R. Howlett if (next && (vma != next) && (end == next->vm_end)) { 66104241ffeSLiam R. Howlett int ret; 6629303d3e1SLiam R. Howlett 66304241ffeSLiam R. Howlett remove_next = true; 66404241ffeSLiam R. Howlett ret = dup_anon_vma(vma, next); 66504241ffeSLiam R. Howlett if (ret) 66604241ffeSLiam R. Howlett return ret; 6679303d3e1SLiam R. Howlett } 6689303d3e1SLiam R. Howlett 66968cefec5SLiam R. Howlett init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); 6709303d3e1SLiam R. Howlett /* Not merging but overwriting any part of next is not handled. */ 6719303d3e1SLiam R. Howlett VM_WARN_ON(next && !vp.remove && 6729303d3e1SLiam R. Howlett next != vma && end > next->vm_start); 6739303d3e1SLiam R. Howlett /* Only handles expanding */ 6749303d3e1SLiam R. Howlett VM_WARN_ON(vma->vm_start < start || vma->vm_end > end); 6759303d3e1SLiam R. Howlett 6769303d3e1SLiam R. Howlett if (vma_iter_prealloc(vmi)) 6779303d3e1SLiam R. Howlett goto nomem; 6789303d3e1SLiam R. Howlett 679ccf1d78dSSuren Baghdasaryan vma_prepare(&vp); 6809303d3e1SLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 6819303d3e1SLiam R. Howlett /* VMA iterator points to previous, so set to start if necessary */ 6829303d3e1SLiam R. Howlett if (vma_iter_addr(vmi) != start) 6839303d3e1SLiam R. Howlett vma_iter_set(vmi, start); 6849303d3e1SLiam R. Howlett 6859303d3e1SLiam R. Howlett vma->vm_start = start; 6869303d3e1SLiam R. Howlett vma->vm_end = end; 6879303d3e1SLiam R. Howlett vma->vm_pgoff = pgoff; 6889303d3e1SLiam R. Howlett /* Note: mas must be pointing to the expanding VMA */ 6899303d3e1SLiam R. Howlett vma_iter_store(vmi, vma); 6909303d3e1SLiam R. Howlett 6919303d3e1SLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 6929303d3e1SLiam R. Howlett validate_mm(vma->vm_mm); 6939303d3e1SLiam R. Howlett return 0; 6949303d3e1SLiam R. Howlett 6959303d3e1SLiam R. Howlett nomem: 6969303d3e1SLiam R. Howlett return -ENOMEM; 6979303d3e1SLiam R. Howlett } 698cf51e86dSLiam R. Howlett 699cf51e86dSLiam R. Howlett /* 700cf51e86dSLiam R. Howlett * vma_shrink() - Reduce an existing VMAs memory area 701cf51e86dSLiam R. Howlett * @vmi: The vma iterator 702cf51e86dSLiam R. Howlett * @vma: The VMA to modify 703cf51e86dSLiam R. Howlett * @start: The new start 704cf51e86dSLiam R. Howlett * @end: The new end 705cf51e86dSLiam R. Howlett * 706cf51e86dSLiam R. Howlett * Returns: 0 on success, -ENOMEM otherwise 707cf51e86dSLiam R. Howlett */ 708cf51e86dSLiam R. Howlett int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, 709cf51e86dSLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff) 710cf51e86dSLiam R. Howlett { 711cf51e86dSLiam R. Howlett struct vma_prepare vp; 712cf51e86dSLiam R. Howlett 713cf51e86dSLiam R. Howlett WARN_ON((vma->vm_start != start) && (vma->vm_end != end)); 714cf51e86dSLiam R. Howlett 715cf51e86dSLiam R. Howlett if (vma_iter_prealloc(vmi)) 716cf51e86dSLiam R. Howlett return -ENOMEM; 717cf51e86dSLiam R. Howlett 718cf51e86dSLiam R. Howlett init_vma_prep(&vp, vma); 719cf51e86dSLiam R. Howlett vma_prepare(&vp); 720ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, start, end, 0); 721cf51e86dSLiam R. Howlett 722cf51e86dSLiam R. Howlett if (vma->vm_start < start) 723cf51e86dSLiam R. Howlett vma_iter_clear(vmi, vma->vm_start, start); 724cf51e86dSLiam R. Howlett 725cf51e86dSLiam R. Howlett if (vma->vm_end > end) 726cf51e86dSLiam R. Howlett vma_iter_clear(vmi, end, vma->vm_end); 727cf51e86dSLiam R. Howlett 728cf51e86dSLiam R. Howlett vma->vm_start = start; 729cf51e86dSLiam R. Howlett vma->vm_end = end; 730cf51e86dSLiam R. Howlett vma->vm_pgoff = pgoff; 731cf51e86dSLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 732cf51e86dSLiam R. Howlett validate_mm(vma->vm_mm); 733cf51e86dSLiam R. Howlett return 0; 734cf51e86dSLiam R. Howlett } 735cf51e86dSLiam R. Howlett 7369303d3e1SLiam R. Howlett /* 7371da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 738714965caSVlastimil Babka * per-vma resources, so we don't attempt to merge those if the caller indicates 739714965caSVlastimil Babka * the current vma may be removed as part of the merge. 7401da177e4SLinus Torvalds */ 7412dbf4010SVlastimil Babka static inline bool is_mergeable_vma(struct vm_area_struct *vma, 74219a809afSAndrea Arcangeli struct file *file, unsigned long vm_flags, 7439a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 744714965caSVlastimil Babka struct anon_vma_name *anon_name, bool may_remove_vma) 7451da177e4SLinus Torvalds { 74634228d47SCyrill Gorcunov /* 74734228d47SCyrill Gorcunov * VM_SOFTDIRTY should not prevent from VMA merging, if we 74834228d47SCyrill Gorcunov * match the flags but dirty bit -- the caller should mark 74934228d47SCyrill Gorcunov * merged VMA as dirty. If dirty bit won't be excluded from 7508bb4e7a2SWei Yang * comparison, we increase pressure on the memory system forcing 75134228d47SCyrill Gorcunov * the kernel to generate new VMAs when old one could be 75234228d47SCyrill Gorcunov * extended instead. 75334228d47SCyrill Gorcunov */ 75434228d47SCyrill Gorcunov if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 7552dbf4010SVlastimil Babka return false; 7561da177e4SLinus Torvalds if (vma->vm_file != file) 7572dbf4010SVlastimil Babka return false; 758714965caSVlastimil Babka if (may_remove_vma && vma->vm_ops && vma->vm_ops->close) 7592dbf4010SVlastimil Babka return false; 76019a809afSAndrea Arcangeli if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 7612dbf4010SVlastimil Babka return false; 7625c26f6acSSuren Baghdasaryan if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 7632dbf4010SVlastimil Babka return false; 7642dbf4010SVlastimil Babka return true; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 7672dbf4010SVlastimil Babka static inline bool is_mergeable_anon_vma(struct anon_vma *anon_vma1, 7682dbf4010SVlastimil Babka struct anon_vma *anon_vma2, struct vm_area_struct *vma) 7691da177e4SLinus Torvalds { 770965f55deSShaohua Li /* 771965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 772965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 773965f55deSShaohua Li */ 774965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 775965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 7762dbf4010SVlastimil Babka return true; 777965f55deSShaohua Li return anon_vma1 == anon_vma2; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 7821da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 7831da177e4SLinus Torvalds * 7841da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 7851da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 7861da177e4SLinus Torvalds * 7871da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 78845e55300SPeter Collingbourne * indices (16TB on ia32) because do_mmap() does not permit mmap's which 7891da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 790714965caSVlastimil Babka * 791714965caSVlastimil Babka * We assume the vma may be removed as part of the merge. 7921da177e4SLinus Torvalds */ 7932dbf4010SVlastimil Babka static bool 7941da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 79519a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 7962dbf4010SVlastimil Babka pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 7975c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 7981da177e4SLinus Torvalds { 799714965caSVlastimil Babka if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, true) && 800965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 8011da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 8022dbf4010SVlastimil Babka return true; 8031da177e4SLinus Torvalds } 8042dbf4010SVlastimil Babka return false; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds /* 8081da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 8091da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 8101da177e4SLinus Torvalds * 8111da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 8121da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 813714965caSVlastimil Babka * 814714965caSVlastimil Babka * We assume that vma is not removed as part of the merge. 8151da177e4SLinus Torvalds */ 8162dbf4010SVlastimil Babka static bool 8171da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 81819a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 8192dbf4010SVlastimil Babka pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 8205c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 8211da177e4SLinus Torvalds { 822714965caSVlastimil Babka if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, false) && 823965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 8241da177e4SLinus Torvalds pgoff_t vm_pglen; 825d6e93217SLibin vm_pglen = vma_pages(vma); 8261da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 8272dbf4010SVlastimil Babka return true; 8281da177e4SLinus Torvalds } 8292dbf4010SVlastimil Babka return false; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* 8339a10064fSColin Cross * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 8349a10064fSColin Cross * figure out whether that can be merged with its predecessor or its 8359a10064fSColin Cross * successor. Or both (it neatly fills a hole). 8361da177e4SLinus Torvalds * 8371da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 8381da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 8391da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 8401da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 8411da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 8421da177e4SLinus Torvalds * case has already been eliminated. 8431da177e4SLinus Torvalds * 844fcfccd91SLorenzo Stoakes * The following mprotect cases have to be considered, where **** is 8451da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 846fcfccd91SLorenzo Stoakes * vma, PPPP is the previous vma, CCCC is a concurrent vma that starts 847fcfccd91SLorenzo Stoakes * at the same address as **** and is of the same or larger span, and 848fcfccd91SLorenzo Stoakes * NNNN the next vma after ****: 8491da177e4SLinus Torvalds * 850fcfccd91SLorenzo Stoakes * **** **** **** 851fcfccd91SLorenzo Stoakes * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPCCCCCC 8525d42ab29SWei Yang * cannot merge might become might become 853fcfccd91SLorenzo Stoakes * PPNNNNNNNNNN PPPPPPPPPPCC 8545d42ab29SWei Yang * mmap, brk or case 4 below case 5 below 8555d42ab29SWei Yang * mremap move: 856fcfccd91SLorenzo Stoakes * **** **** 857fcfccd91SLorenzo Stoakes * PPPP NNNN PPPPCCCCNNNN 8585d42ab29SWei Yang * might become might become 8595d42ab29SWei Yang * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 860fcfccd91SLorenzo Stoakes * PPPPPPPPNNNN 2 or PPPPPPPPNNNN 7 or 861fcfccd91SLorenzo Stoakes * PPPPNNNNNNNN 3 PPPPNNNNNNNN 8 8621da177e4SLinus Torvalds * 863fcfccd91SLorenzo Stoakes * It is important for case 8 that the vma CCCC overlapping the 864fcfccd91SLorenzo Stoakes * region **** is never going to extended over NNNN. Instead NNNN must 865fcfccd91SLorenzo Stoakes * be extended in region **** and CCCC must be removed. This way in 8660503ea8fSLiam R. Howlett * all cases where vma_merge succeeds, the moment vma_merge drops the 867e86f15eeSAndrea Arcangeli * rmap_locks, the properties of the merged vma will be already 868e86f15eeSAndrea Arcangeli * correct for the whole merged range. Some of those properties like 869e86f15eeSAndrea Arcangeli * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 870e86f15eeSAndrea Arcangeli * be correct for the whole merged range immediately after the 871fcfccd91SLorenzo Stoakes * rmap_locks are released. Otherwise if NNNN would be removed and 872fcfccd91SLorenzo Stoakes * CCCC would be extended over the NNNN range, remove_migration_ptes 873e86f15eeSAndrea Arcangeli * or other rmap walkers (if working on addresses beyond the "end" 874fcfccd91SLorenzo Stoakes * parameter) may establish ptes with the wrong permissions of CCCC 875fcfccd91SLorenzo Stoakes * instead of the right permissions of NNNN. 8760503ea8fSLiam R. Howlett * 8770503ea8fSLiam R. Howlett * In the code below: 8780503ea8fSLiam R. Howlett * PPPP is represented by *prev 879fcfccd91SLorenzo Stoakes * CCCC is represented by *curr or not represented at all (NULL) 880fcfccd91SLorenzo Stoakes * NNNN is represented by *next or not represented at all (NULL) 881fcfccd91SLorenzo Stoakes * **** is not represented - it will be merged and the vma containing the 8829e8a39d2SVlastimil Babka * area is returned, or the function will return NULL 8831da177e4SLinus Torvalds */ 8849760ebffSLiam R. Howlett struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, 8851da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 8861da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 8871da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 88819a809afSAndrea Arcangeli pgoff_t pgoff, struct mempolicy *policy, 8899a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 8905c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 8911da177e4SLinus Torvalds { 892b0729ae0SLorenzo Stoakes struct vm_area_struct *curr, *next, *res; 8930503ea8fSLiam R. Howlett struct vm_area_struct *vma, *adjust, *remove, *remove2; 8940173db4fSLorenzo Stoakes struct vma_prepare vp; 8950173db4fSLorenzo Stoakes pgoff_t vma_pgoff; 8960173db4fSLorenzo Stoakes int err = 0; 897eef19944SJakub Matěna bool merge_prev = false; 898eef19944SJakub Matěna bool merge_next = false; 8990503ea8fSLiam R. Howlett bool vma_expanded = false; 9000503ea8fSLiam R. Howlett unsigned long vma_start = addr; 9010173db4fSLorenzo Stoakes unsigned long vma_end = end; 9020173db4fSLorenzo Stoakes pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 9030173db4fSLorenzo Stoakes long adj_start = 0; 9041da177e4SLinus Torvalds 9050503ea8fSLiam R. Howlett validate_mm(mm); 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 9081da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 9091da177e4SLinus Torvalds */ 9101da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 9111da177e4SLinus Torvalds return NULL; 9121da177e4SLinus Torvalds 91300cd00a6SLorenzo Stoakes /* Does the input range span an existing VMA? (cases 5 - 8) */ 91400cd00a6SLorenzo Stoakes curr = find_vma_intersection(mm, prev ? prev->vm_end : 0, end); 9151da177e4SLinus Torvalds 91600cd00a6SLorenzo Stoakes if (!curr || /* cases 1 - 4 */ 91700cd00a6SLorenzo Stoakes end == curr->vm_end) /* cases 6 - 8, adjacent VMA */ 91800cd00a6SLorenzo Stoakes next = vma_lookup(mm, end); 91900cd00a6SLorenzo Stoakes else 92000cd00a6SLorenzo Stoakes next = NULL; /* case 5 */ 921e86f15eeSAndrea Arcangeli 9220503ea8fSLiam R. Howlett if (prev) { 9230503ea8fSLiam R. Howlett vma_start = prev->vm_start; 9240503ea8fSLiam R. Howlett vma_pgoff = prev->vm_pgoff; 9250173db4fSLorenzo Stoakes 926eef19944SJakub Matěna /* Can we merge the predecessor? */ 9270173db4fSLorenzo Stoakes if (addr == prev->vm_end && mpol_equal(vma_policy(prev), policy) 9280503ea8fSLiam R. Howlett && can_vma_merge_after(prev, vm_flags, anon_vma, file, 9290503ea8fSLiam R. Howlett pgoff, vm_userfaultfd_ctx, anon_name)) { 930eef19944SJakub Matěna merge_prev = true; 93118b098afSLiam R. Howlett vma_prev(vmi); 9321da177e4SLinus Torvalds } 9330503ea8fSLiam R. Howlett } 934b0729ae0SLorenzo Stoakes 935eef19944SJakub Matěna /* Can we merge the successor? */ 93600cd00a6SLorenzo Stoakes if (next && mpol_equal(policy, vma_policy(next)) && 9370173db4fSLorenzo Stoakes can_vma_merge_before(next, vm_flags, anon_vma, file, pgoff+pglen, 9389a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 939eef19944SJakub Matěna merge_next = true; 940eef19944SJakub Matěna } 9410503ea8fSLiam R. Howlett 94229417d29SLorenzo Stoakes /* Verify some invariant that must be enforced by the caller. */ 94329417d29SLorenzo Stoakes VM_WARN_ON(prev && addr <= prev->vm_start); 94429417d29SLorenzo Stoakes VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); 94529417d29SLorenzo Stoakes VM_WARN_ON(addr >= end); 94629417d29SLorenzo Stoakes 9470173db4fSLorenzo Stoakes if (!merge_prev && !merge_next) 9480173db4fSLorenzo Stoakes return NULL; /* Not mergeable. */ 9490173db4fSLorenzo Stoakes 9500173db4fSLorenzo Stoakes res = vma = prev; 9510503ea8fSLiam R. Howlett remove = remove2 = adjust = NULL; 9520173db4fSLorenzo Stoakes 953eef19944SJakub Matěna /* Can we merge both the predecessor and the successor? */ 954eef19944SJakub Matěna if (merge_prev && merge_next && 9550503ea8fSLiam R. Howlett is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) { 9565ff783f1SVlastimil Babka remove = next; /* case 1 */ 9570503ea8fSLiam R. Howlett vma_end = next->vm_end; 95850dac011SVlastimil Babka err = dup_anon_vma(prev, next); 959fcfccd91SLorenzo Stoakes if (curr) { /* case 6 */ 960fcfccd91SLorenzo Stoakes remove = curr; 9610503ea8fSLiam R. Howlett remove2 = next; 9625ff783f1SVlastimil Babka if (!next->anon_vma) 963fcfccd91SLorenzo Stoakes err = dup_anon_vma(prev, curr); 9640503ea8fSLiam R. Howlett } 9650173db4fSLorenzo Stoakes } else if (merge_prev) { /* case 2 */ 966fcfccd91SLorenzo Stoakes if (curr) { 967fcfccd91SLorenzo Stoakes err = dup_anon_vma(prev, curr); 968fcfccd91SLorenzo Stoakes if (end == curr->vm_end) { /* case 7 */ 969fcfccd91SLorenzo Stoakes remove = curr; 9700503ea8fSLiam R. Howlett } else { /* case 5 */ 971fcfccd91SLorenzo Stoakes adjust = curr; 972fcfccd91SLorenzo Stoakes adj_start = (end - curr->vm_start); 9730503ea8fSLiam R. Howlett } 9740503ea8fSLiam R. Howlett } 9750173db4fSLorenzo Stoakes } else { /* merge_next */ 976eef19944SJakub Matěna res = next; 9770503ea8fSLiam R. Howlett if (prev && addr < prev->vm_end) { /* case 4 */ 9780503ea8fSLiam R. Howlett vma_end = addr; 979183b7a60SVlastimil Babka adjust = next; 9801e76454fSVlastimil Babka adj_start = -(prev->vm_end - addr); 981183b7a60SVlastimil Babka err = dup_anon_vma(next, prev); 9820503ea8fSLiam R. Howlett } else { 983b0729ae0SLorenzo Stoakes /* 984b0729ae0SLorenzo Stoakes * Note that cases 3 and 8 are the ONLY ones where prev 985b0729ae0SLorenzo Stoakes * is permitted to be (but is not necessarily) NULL. 986b0729ae0SLorenzo Stoakes */ 9870503ea8fSLiam R. Howlett vma = next; /* case 3 */ 9880503ea8fSLiam R. Howlett vma_start = addr; 9890503ea8fSLiam R. Howlett vma_end = next->vm_end; 9907e775787SVlastimil Babka vma_pgoff = next->vm_pgoff - pglen; 991fcfccd91SLorenzo Stoakes if (curr) { /* case 8 */ 992fcfccd91SLorenzo Stoakes vma_pgoff = curr->vm_pgoff; 993fcfccd91SLorenzo Stoakes remove = curr; 994fcfccd91SLorenzo Stoakes err = dup_anon_vma(next, curr); 9950503ea8fSLiam R. Howlett } 9960503ea8fSLiam R. Howlett } 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9990173db4fSLorenzo Stoakes /* Error in anon_vma clone. */ 1000eef19944SJakub Matěna if (err) 10011da177e4SLinus Torvalds return NULL; 10020503ea8fSLiam R. Howlett 10030503ea8fSLiam R. Howlett if (vma_iter_prealloc(vmi)) 10040503ea8fSLiam R. Howlett return NULL; 10050503ea8fSLiam R. Howlett 10060503ea8fSLiam R. Howlett init_multi_vma_prep(&vp, vma, adjust, remove, remove2); 10070503ea8fSLiam R. Howlett VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma && 10080503ea8fSLiam R. Howlett vp.anon_vma != adjust->anon_vma); 10090503ea8fSLiam R. Howlett 10100503ea8fSLiam R. Howlett vma_prepare(&vp); 1011ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, vma_start, vma_end, adj_start); 10120503ea8fSLiam R. Howlett if (vma_start < vma->vm_start || vma_end > vma->vm_end) 10130503ea8fSLiam R. Howlett vma_expanded = true; 10140503ea8fSLiam R. Howlett 10150503ea8fSLiam R. Howlett vma->vm_start = vma_start; 10160503ea8fSLiam R. Howlett vma->vm_end = vma_end; 10170503ea8fSLiam R. Howlett vma->vm_pgoff = vma_pgoff; 10180503ea8fSLiam R. Howlett 10190503ea8fSLiam R. Howlett if (vma_expanded) 10200503ea8fSLiam R. Howlett vma_iter_store(vmi, vma); 10210503ea8fSLiam R. Howlett 10221e76454fSVlastimil Babka if (adj_start) { 10231e76454fSVlastimil Babka adjust->vm_start += adj_start; 10241e76454fSVlastimil Babka adjust->vm_pgoff += adj_start >> PAGE_SHIFT; 10251e76454fSVlastimil Babka if (adj_start < 0) { 10260503ea8fSLiam R. Howlett WARN_ON(vma_expanded); 10270503ea8fSLiam R. Howlett vma_iter_store(vmi, next); 10280503ea8fSLiam R. Howlett } 10290503ea8fSLiam R. Howlett } 10300503ea8fSLiam R. Howlett 10310503ea8fSLiam R. Howlett vma_complete(&vp, vmi, mm); 10320503ea8fSLiam R. Howlett vma_iter_free(vmi); 10330503ea8fSLiam R. Howlett validate_mm(mm); 1034eef19944SJakub Matěna khugepaged_enter_vma(res, vm_flags); 10351da177e4SLinus Torvalds 10369760ebffSLiam R. Howlett return res; 1037f2ebfe43SLiam R. Howlett } 1038f2ebfe43SLiam R. Howlett 10391da177e4SLinus Torvalds /* 1040b4f315b4SEthon Paul * Rough compatibility check to quickly see if it's even worth looking 1041d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1042d0e9fe17SLinus Torvalds * 1043d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1044d0e9fe17SLinus Torvalds * in things that mprotect may change. 1045d0e9fe17SLinus Torvalds * 1046d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1047d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1048d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1049d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1050d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1051d0e9fe17SLinus Torvalds */ 1052d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1053d0e9fe17SLinus Torvalds { 1054d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1055d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1056d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 10576cb4d9a2SAnshuman Khandual !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1058d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1059d0e9fe17SLinus Torvalds } 1060d0e9fe17SLinus Torvalds 1061d0e9fe17SLinus Torvalds /* 1062d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1063d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1064d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1065d0e9fe17SLinus Torvalds * to share the anon_vma. 1066d0e9fe17SLinus Torvalds * 10675b449489SFlorian Rommel * NOTE! This runs with mmap_lock held for reading, so it is possible that 1068d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1069d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1070d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1071d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 10724db0c3c2SJason Low * we do that READ_ONCE() to make sure that we never re-load the pointer. 1073d0e9fe17SLinus Torvalds * 1074d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1075d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1076d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1077d0e9fe17SLinus Torvalds * a fork). 1078d0e9fe17SLinus Torvalds * 1079d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1080d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 10815b449489SFlorian Rommel * a read lock on the mmap_lock. 1082d0e9fe17SLinus Torvalds */ 1083d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1084d0e9fe17SLinus Torvalds { 1085d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 10864db0c3c2SJason Low struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1087d0e9fe17SLinus Torvalds 1088d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1089d0e9fe17SLinus Torvalds return anon_vma; 1090d0e9fe17SLinus Torvalds } 1091d0e9fe17SLinus Torvalds return NULL; 1092d0e9fe17SLinus Torvalds } 1093d0e9fe17SLinus Torvalds 1094d0e9fe17SLinus Torvalds /* 10951da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 10961da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 10971da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 10981da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 10991da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 11001da177e4SLinus Torvalds * mprotect. 11011da177e4SLinus Torvalds */ 11021da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 11031da177e4SLinus Torvalds { 1104763ecb03SLiam R. Howlett MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); 1105a67c8caaSMiaohe Lin struct anon_vma *anon_vma = NULL; 1106763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next; 11071da177e4SLinus Torvalds 1108a67c8caaSMiaohe Lin /* Try next first. */ 1109763ecb03SLiam R. Howlett next = mas_walk(&mas); 1110763ecb03SLiam R. Howlett if (next) { 1111763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(next, vma, next); 1112d0e9fe17SLinus Torvalds if (anon_vma) 1113d0e9fe17SLinus Torvalds return anon_vma; 1114a67c8caaSMiaohe Lin } 11151da177e4SLinus Torvalds 1116763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1117763ecb03SLiam R. Howlett VM_BUG_ON_VMA(prev != vma, vma); 1118763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 1119a67c8caaSMiaohe Lin /* Try prev next. */ 1120763ecb03SLiam R. Howlett if (prev) 1121763ecb03SLiam R. Howlett anon_vma = reusable_anon_vma(prev, prev, vma); 1122a67c8caaSMiaohe Lin 11231da177e4SLinus Torvalds /* 1124a67c8caaSMiaohe Lin * We might reach here with anon_vma == NULL if we can't find 1125a67c8caaSMiaohe Lin * any reusable anon_vma. 11261da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 11271da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 11281da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 11291da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 11301da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 11311da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 11321da177e4SLinus Torvalds */ 1133a67c8caaSMiaohe Lin return anon_vma; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds /* 113740401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 113840401530SAl Viro * low as possible but still greater than mmap_min_addr 113940401530SAl Viro */ 114040401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 114140401530SAl Viro { 114240401530SAl Viro hint &= PAGE_MASK; 114340401530SAl Viro if (((void *)hint != NULL) && 114440401530SAl Viro (hint < mmap_min_addr)) 114540401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 114640401530SAl Viro return hint; 114740401530SAl Viro } 114840401530SAl Viro 1149*3c54a298SLorenzo Stoakes bool mlock_future_check(struct mm_struct *mm, unsigned long flags, 1150*3c54a298SLorenzo Stoakes unsigned long bytes) 1151363ee17fSDavidlohr Bueso { 1152*3c54a298SLorenzo Stoakes unsigned long locked_pages, limit_pages; 1153363ee17fSDavidlohr Bueso 1154*3c54a298SLorenzo Stoakes if (!(flags & VM_LOCKED) || capable(CAP_IPC_LOCK)) 1155*3c54a298SLorenzo Stoakes return true; 1156*3c54a298SLorenzo Stoakes 1157*3c54a298SLorenzo Stoakes locked_pages = bytes >> PAGE_SHIFT; 1158*3c54a298SLorenzo Stoakes locked_pages += mm->locked_vm; 1159*3c54a298SLorenzo Stoakes 1160*3c54a298SLorenzo Stoakes limit_pages = rlimit(RLIMIT_MEMLOCK); 1161*3c54a298SLorenzo Stoakes limit_pages >>= PAGE_SHIFT; 1162*3c54a298SLorenzo Stoakes 1163*3c54a298SLorenzo Stoakes return locked_pages <= limit_pages; 1164363ee17fSDavidlohr Bueso } 1165363ee17fSDavidlohr Bueso 1166be83bbf8SLinus Torvalds static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) 1167be83bbf8SLinus Torvalds { 1168be83bbf8SLinus Torvalds if (S_ISREG(inode->i_mode)) 1169423913adSLinus Torvalds return MAX_LFS_FILESIZE; 1170be83bbf8SLinus Torvalds 1171be83bbf8SLinus Torvalds if (S_ISBLK(inode->i_mode)) 1172be83bbf8SLinus Torvalds return MAX_LFS_FILESIZE; 1173be83bbf8SLinus Torvalds 117476f34950SIvan Khoronzhuk if (S_ISSOCK(inode->i_mode)) 117576f34950SIvan Khoronzhuk return MAX_LFS_FILESIZE; 117676f34950SIvan Khoronzhuk 1177be83bbf8SLinus Torvalds /* Special "we do even unsigned file positions" case */ 1178be83bbf8SLinus Torvalds if (file->f_mode & FMODE_UNSIGNED_OFFSET) 1179be83bbf8SLinus Torvalds return 0; 1180be83bbf8SLinus Torvalds 1181be83bbf8SLinus Torvalds /* Yes, random drivers might want more. But I'm tired of buggy drivers */ 1182be83bbf8SLinus Torvalds return ULONG_MAX; 1183be83bbf8SLinus Torvalds } 1184be83bbf8SLinus Torvalds 1185be83bbf8SLinus Torvalds static inline bool file_mmap_ok(struct file *file, struct inode *inode, 1186be83bbf8SLinus Torvalds unsigned long pgoff, unsigned long len) 1187be83bbf8SLinus Torvalds { 1188be83bbf8SLinus Torvalds u64 maxsize = file_mmap_size_max(file, inode); 1189be83bbf8SLinus Torvalds 1190be83bbf8SLinus Torvalds if (maxsize && len > maxsize) 1191be83bbf8SLinus Torvalds return false; 1192be83bbf8SLinus Torvalds maxsize -= len; 1193be83bbf8SLinus Torvalds if (pgoff > maxsize >> PAGE_SHIFT) 1194be83bbf8SLinus Torvalds return false; 1195be83bbf8SLinus Torvalds return true; 1196be83bbf8SLinus Torvalds } 1197be83bbf8SLinus Torvalds 119840401530SAl Viro /* 11993e4e28c5SMichel Lespinasse * The caller must write-lock current->mm->mmap_lock. 12001da177e4SLinus Torvalds */ 12011fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, unsigned long addr, 12021da177e4SLinus Torvalds unsigned long len, unsigned long prot, 120345e55300SPeter Collingbourne unsigned long flags, unsigned long pgoff, 120445e55300SPeter Collingbourne unsigned long *populate, struct list_head *uf) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 120745e55300SPeter Collingbourne vm_flags_t vm_flags; 120862b5f7d0SDave Hansen int pkey = 0; 12091da177e4SLinus Torvalds 1210524e00b3SLiam R. Howlett validate_mm(mm); 121141badc15SMichel Lespinasse *populate = 0; 1212bebeb3d6SMichel Lespinasse 1213e37609bbSPiotr Kwapulinski if (!len) 1214e37609bbSPiotr Kwapulinski return -EINVAL; 1215e37609bbSPiotr Kwapulinski 12161da177e4SLinus Torvalds /* 12171da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 12181da177e4SLinus Torvalds * 12191da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 12201da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 12211da177e4SLinus Torvalds */ 12221da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 122390f8572bSEric W. Biederman if (!(file && path_noexec(&file->f_path))) 12241da177e4SLinus Torvalds prot |= PROT_EXEC; 12251da177e4SLinus Torvalds 1226a4ff8e86SMichal Hocko /* force arch specific MAP_FIXED handling in get_unmapped_area */ 1227a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) 1228a4ff8e86SMichal Hocko flags |= MAP_FIXED; 1229a4ff8e86SMichal Hocko 12307cd94146SEric Paris if (!(flags & MAP_FIXED)) 12317cd94146SEric Paris addr = round_hint_to_min(addr); 12327cd94146SEric Paris 12331da177e4SLinus Torvalds /* Careful about overflows.. */ 12341da177e4SLinus Torvalds len = PAGE_ALIGN(len); 12359206de95SAl Viro if (!len) 12361da177e4SLinus Torvalds return -ENOMEM; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds /* offset overflow? */ 12391da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 12401da177e4SLinus Torvalds return -EOVERFLOW; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* Too many mappings? */ 12431da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 12441da177e4SLinus Torvalds return -ENOMEM; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 12471da177e4SLinus Torvalds * that it represents a valid section of the address space. 12481da177e4SLinus Torvalds */ 12491da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 1250ff68dac6SGaowei Pu if (IS_ERR_VALUE(addr)) 12511da177e4SLinus Torvalds return addr; 12521da177e4SLinus Torvalds 1253a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) { 125435e43c5fSLiam Howlett if (find_vma_intersection(mm, addr, addr + len)) 1255a4ff8e86SMichal Hocko return -EEXIST; 1256a4ff8e86SMichal Hocko } 1257a4ff8e86SMichal Hocko 125862b5f7d0SDave Hansen if (prot == PROT_EXEC) { 125962b5f7d0SDave Hansen pkey = execute_only_pkey(mm); 126062b5f7d0SDave Hansen if (pkey < 0) 126162b5f7d0SDave Hansen pkey = 0; 126262b5f7d0SDave Hansen } 126362b5f7d0SDave Hansen 12641da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 12651da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 12661da177e4SLinus Torvalds * of the memory object, so we don't do any here. 12671da177e4SLinus Torvalds */ 126845e55300SPeter Collingbourne vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | 12691da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 12701da177e4SLinus Torvalds 1271cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 12721da177e4SLinus Torvalds if (!can_do_mlock()) 12731da177e4SLinus Torvalds return -EPERM; 1274ba470de4SRik van Riel 1275*3c54a298SLorenzo Stoakes if (!mlock_future_check(mm, vm_flags, len)) 12761da177e4SLinus Torvalds return -EAGAIN; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (file) { 1279077bf22bSOleg Nesterov struct inode *inode = file_inode(file); 12801c972597SDan Williams unsigned long flags_mask; 12811c972597SDan Williams 1282be83bbf8SLinus Torvalds if (!file_mmap_ok(file, inode, pgoff, len)) 1283be83bbf8SLinus Torvalds return -EOVERFLOW; 1284be83bbf8SLinus Torvalds 12851c972597SDan Williams flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; 1286077bf22bSOleg Nesterov 12871da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 12881da177e4SLinus Torvalds case MAP_SHARED: 12891c972597SDan Williams /* 12901c972597SDan Williams * Force use of MAP_SHARED_VALIDATE with non-legacy 12911c972597SDan Williams * flags. E.g. MAP_SYNC is dangerous to use with 12921c972597SDan Williams * MAP_SHARED as you don't know which consistency model 12931c972597SDan Williams * you will get. We silently ignore unsupported flags 12941c972597SDan Williams * with MAP_SHARED to preserve backward compatibility. 12951c972597SDan Williams */ 12961c972597SDan Williams flags &= LEGACY_MAP_MASK; 1297e4a9bc58SJoe Perches fallthrough; 12981c972597SDan Williams case MAP_SHARED_VALIDATE: 12991c972597SDan Williams if (flags & ~flags_mask) 13001c972597SDan Williams return -EOPNOTSUPP; 1301dc617f29SDarrick J. Wong if (prot & PROT_WRITE) { 1302dc617f29SDarrick J. Wong if (!(file->f_mode & FMODE_WRITE)) 13031da177e4SLinus Torvalds return -EACCES; 1304dc617f29SDarrick J. Wong if (IS_SWAPFILE(file->f_mapping->host)) 1305dc617f29SDarrick J. Wong return -ETXTBSY; 1306dc617f29SDarrick J. Wong } 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds /* 13091da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 13101da177e4SLinus Torvalds * file.. 13111da177e4SLinus Torvalds */ 13121da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 13131da177e4SLinus Torvalds return -EACCES; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13161da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 13171da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 1318e4a9bc58SJoe Perches fallthrough; 13191da177e4SLinus Torvalds case MAP_PRIVATE: 13201da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 13211da177e4SLinus Torvalds return -EACCES; 132290f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 132380c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 132480c5606cSLinus Torvalds return -EPERM; 132580c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 132680c5606cSLinus Torvalds } 132780c5606cSLinus Torvalds 132872c2d531SAl Viro if (!file->f_op->mmap) 132980c5606cSLinus Torvalds return -ENODEV; 1330b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1331b2c56e4fSOleg Nesterov return -EINVAL; 13321da177e4SLinus Torvalds break; 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds default: 13351da177e4SLinus Torvalds return -EINVAL; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds } else { 13381da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 13391da177e4SLinus Torvalds case MAP_SHARED: 1340b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1341b2c56e4fSOleg Nesterov return -EINVAL; 1342ce363942STejun Heo /* 1343ce363942STejun Heo * Ignore pgoff. 1344ce363942STejun Heo */ 1345ce363942STejun Heo pgoff = 0; 13461da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 13471da177e4SLinus Torvalds break; 13481da177e4SLinus Torvalds case MAP_PRIVATE: 13491da177e4SLinus Torvalds /* 13501da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 13511da177e4SLinus Torvalds */ 13521da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 13531da177e4SLinus Torvalds break; 13541da177e4SLinus Torvalds default: 13551da177e4SLinus Torvalds return -EINVAL; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 1359c22c0d63SMichel Lespinasse /* 1360c22c0d63SMichel Lespinasse * Set 'VM_NORESERVE' if we should not account for the 1361c22c0d63SMichel Lespinasse * memory use of this mapping. 1362c22c0d63SMichel Lespinasse */ 1363c22c0d63SMichel Lespinasse if (flags & MAP_NORESERVE) { 1364c22c0d63SMichel Lespinasse /* We honor MAP_NORESERVE if allowed to overcommit */ 1365c22c0d63SMichel Lespinasse if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1366c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1367c22c0d63SMichel Lespinasse 1368c22c0d63SMichel Lespinasse /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 1369c22c0d63SMichel Lespinasse if (file && is_file_hugepages(file)) 1370c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1371c22c0d63SMichel Lespinasse } 1372c22c0d63SMichel Lespinasse 1373897ab3e0SMike Rapoport addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); 137409a9f1d2SMichel Lespinasse if (!IS_ERR_VALUE(addr) && 137509a9f1d2SMichel Lespinasse ((vm_flags & VM_LOCKED) || 137609a9f1d2SMichel Lespinasse (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) 137741badc15SMichel Lespinasse *populate = len; 1378bebeb3d6SMichel Lespinasse return addr; 13790165ab44SMiklos Szeredi } 13806be5ceb0SLinus Torvalds 1381a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1382a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1383a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 138466f0dc48SHugh Dickins { 138566f0dc48SHugh Dickins struct file *file = NULL; 13861e3ee14bSChen Gang unsigned long retval; 138766f0dc48SHugh Dickins 138866f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1389120a795dSAl Viro audit_mmap_fd(fd, flags); 139066f0dc48SHugh Dickins file = fget(fd); 139166f0dc48SHugh Dickins if (!file) 13921e3ee14bSChen Gang return -EBADF; 13937bba8f0eSZhen Lei if (is_file_hugepages(file)) { 1394af73e4d9SNaoya Horiguchi len = ALIGN(len, huge_page_size(hstate_file(file))); 13957bba8f0eSZhen Lei } else if (unlikely(flags & MAP_HUGETLB)) { 1396493af578SJörn Engel retval = -EINVAL; 1397493af578SJörn Engel goto out_fput; 13987bba8f0eSZhen Lei } 139966f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 1400c103a4dcSAndrew Morton struct hstate *hs; 1401af73e4d9SNaoya Horiguchi 140220ac2893SAnshuman Khandual hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 1403091d0d55SLi Zefan if (!hs) 1404091d0d55SLi Zefan return -EINVAL; 1405091d0d55SLi Zefan 1406091d0d55SLi Zefan len = ALIGN(len, huge_page_size(hs)); 140766f0dc48SHugh Dickins /* 140866f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 140966f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 141066f0dc48SHugh Dickins */ 1411af73e4d9SNaoya Horiguchi file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, 141242d7395fSAndi Kleen VM_NORESERVE, 141383c1fd76Szhangyiru HUGETLB_ANONHUGE_INODE, 141442d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 141566f0dc48SHugh Dickins if (IS_ERR(file)) 141666f0dc48SHugh Dickins return PTR_ERR(file); 141766f0dc48SHugh Dickins } 141866f0dc48SHugh Dickins 14199fbeb5abSMichal Hocko retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1420493af578SJörn Engel out_fput: 142166f0dc48SHugh Dickins if (file) 142266f0dc48SHugh Dickins fput(file); 142366f0dc48SHugh Dickins return retval; 142466f0dc48SHugh Dickins } 142566f0dc48SHugh Dickins 1426a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1427a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1428a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1429a90f590aSDominik Brodowski { 1430a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1431a90f590aSDominik Brodowski } 1432a90f590aSDominik Brodowski 1433a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1434a4679373SChristoph Hellwig struct mmap_arg_struct { 1435a4679373SChristoph Hellwig unsigned long addr; 1436a4679373SChristoph Hellwig unsigned long len; 1437a4679373SChristoph Hellwig unsigned long prot; 1438a4679373SChristoph Hellwig unsigned long flags; 1439a4679373SChristoph Hellwig unsigned long fd; 1440a4679373SChristoph Hellwig unsigned long offset; 1441a4679373SChristoph Hellwig }; 1442a4679373SChristoph Hellwig 1443a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1444a4679373SChristoph Hellwig { 1445a4679373SChristoph Hellwig struct mmap_arg_struct a; 1446a4679373SChristoph Hellwig 1447a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1448a4679373SChristoph Hellwig return -EFAULT; 1449de1741a1SAlexander Kuleshov if (offset_in_page(a.offset)) 1450a4679373SChristoph Hellwig return -EINVAL; 1451a4679373SChristoph Hellwig 1452a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1453a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1454a4679373SChristoph Hellwig } 1455a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1456a4679373SChristoph Hellwig 14574e950f6fSAlexey Dobriyan /* 14588bb4e7a2SWei Yang * Some shared mappings will want the pages marked read-only 14594e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 14604e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 14614e950f6fSAlexey Dobriyan * VM_SHARED bit). 14624e950f6fSAlexey Dobriyan */ 14636d2329f8SAndrea Arcangeli int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 14644e950f6fSAlexey Dobriyan { 1465ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 14668a04446aSKirill A. Shutemov const struct vm_operations_struct *vm_ops = vma->vm_ops; 14674e950f6fSAlexey Dobriyan 14684e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 14694e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 14704e950f6fSAlexey Dobriyan return 0; 14714e950f6fSAlexey Dobriyan 14724e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 14738a04446aSKirill A. Shutemov if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) 14744e950f6fSAlexey Dobriyan return 1; 14754e950f6fSAlexey Dobriyan 147664e45507SPeter Feiner /* The open routine did something to the protections that pgprot_modify 147764e45507SPeter Feiner * won't preserve? */ 14786d2329f8SAndrea Arcangeli if (pgprot_val(vm_page_prot) != 14796d2329f8SAndrea Arcangeli pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) 14804e950f6fSAlexey Dobriyan return 0; 14814e950f6fSAlexey Dobriyan 1482f96f7a40SDavid Hildenbrand /* 1483f96f7a40SDavid Hildenbrand * Do we need to track softdirty? hugetlb does not support softdirty 1484f96f7a40SDavid Hildenbrand * tracking yet. 1485f96f7a40SDavid Hildenbrand */ 1486f96f7a40SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 148764e45507SPeter Feiner return 1; 148864e45507SPeter Feiner 148951d3d5ebSDavid Hildenbrand /* Do we need write faults for uffd-wp tracking? */ 149051d3d5ebSDavid Hildenbrand if (userfaultfd_wp(vma)) 149151d3d5ebSDavid Hildenbrand return 1; 149251d3d5ebSDavid Hildenbrand 14934e950f6fSAlexey Dobriyan /* Specialty mapping? */ 14944b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 14954e950f6fSAlexey Dobriyan return 0; 14964e950f6fSAlexey Dobriyan 14974e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 14984e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 1499f56753acSChristoph Hellwig mapping_can_writeback(vma->vm_file->f_mapping); 15004e950f6fSAlexey Dobriyan } 15014e950f6fSAlexey Dobriyan 1502fc8744adSLinus Torvalds /* 1503fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 15045a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1505fc8744adSLinus Torvalds */ 1506ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1507fc8744adSLinus Torvalds { 15085a6fe125SMel Gorman /* 15095a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 15105a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 15115a6fe125SMel Gorman */ 15125a6fe125SMel Gorman if (file && is_file_hugepages(file)) 15135a6fe125SMel Gorman return 0; 15145a6fe125SMel Gorman 1515fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1516fc8744adSLinus Torvalds } 1517fc8744adSLinus Torvalds 15183499a131SLiam R. Howlett /** 15193499a131SLiam R. Howlett * unmapped_area() - Find an area between the low_limit and the high_limit with 15203499a131SLiam R. Howlett * the correct alignment and offset, all from @info. Note: current->mm is used 15213499a131SLiam R. Howlett * for the search. 15223499a131SLiam R. Howlett * 152382b24936SVernon Yang * @info: The unmapped area information including the range [low_limit - 152482b24936SVernon Yang * high_limit), the alignment offset and mask. 15253499a131SLiam R. Howlett * 15263499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 15273499a131SLiam R. Howlett */ 1528baceaf1cSJaewon Kim static unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1529db4fbfb9SMichel Lespinasse { 15306b008640SLinus Torvalds unsigned long length, gap; 15316b008640SLinus Torvalds unsigned long low_limit, high_limit; 153258c5d0d6SLiam R. Howlett struct vm_area_struct *tmp; 1533db4fbfb9SMichel Lespinasse 15343499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1535db4fbfb9SMichel Lespinasse 1536db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1537db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1538db4fbfb9SMichel Lespinasse if (length < info->length) 1539db4fbfb9SMichel Lespinasse return -ENOMEM; 1540db4fbfb9SMichel Lespinasse 154158c5d0d6SLiam R. Howlett low_limit = info->low_limit; 15426b008640SLinus Torvalds if (low_limit < mmap_min_addr) 15436b008640SLinus Torvalds low_limit = mmap_min_addr; 15446b008640SLinus Torvalds high_limit = info->high_limit; 154558c5d0d6SLiam R. Howlett retry: 15466b008640SLinus Torvalds if (mas_empty_area(&mas, low_limit, high_limit - 1, length)) 15473499a131SLiam R. Howlett return -ENOMEM; 15483499a131SLiam R. Howlett 1549d4af56c5SLiam R. Howlett gap = mas.index; 1550d4af56c5SLiam R. Howlett gap += (info->align_offset - gap) & info->align_mask; 155158c5d0d6SLiam R. Howlett tmp = mas_next(&mas, ULONG_MAX); 155258c5d0d6SLiam R. Howlett if (tmp && (tmp->vm_flags & VM_GROWSDOWN)) { /* Avoid prev check if possible */ 155358c5d0d6SLiam R. Howlett if (vm_start_gap(tmp) < gap + length - 1) { 155458c5d0d6SLiam R. Howlett low_limit = tmp->vm_end; 155558c5d0d6SLiam R. Howlett mas_reset(&mas); 155658c5d0d6SLiam R. Howlett goto retry; 155758c5d0d6SLiam R. Howlett } 155858c5d0d6SLiam R. Howlett } else { 155958c5d0d6SLiam R. Howlett tmp = mas_prev(&mas, 0); 156058c5d0d6SLiam R. Howlett if (tmp && vm_end_gap(tmp) > gap) { 156158c5d0d6SLiam R. Howlett low_limit = vm_end_gap(tmp); 156258c5d0d6SLiam R. Howlett mas_reset(&mas); 156358c5d0d6SLiam R. Howlett goto retry; 156458c5d0d6SLiam R. Howlett } 156558c5d0d6SLiam R. Howlett } 156658c5d0d6SLiam R. Howlett 15673499a131SLiam R. Howlett return gap; 1568db4fbfb9SMichel Lespinasse } 1569db4fbfb9SMichel Lespinasse 15703499a131SLiam R. Howlett /** 15713499a131SLiam R. Howlett * unmapped_area_topdown() - Find an area between the low_limit and the 157282b24936SVernon Yang * high_limit with the correct alignment and offset at the highest available 15733499a131SLiam R. Howlett * address, all from @info. Note: current->mm is used for the search. 15743499a131SLiam R. Howlett * 157582b24936SVernon Yang * @info: The unmapped area information including the range [low_limit - 157682b24936SVernon Yang * high_limit), the alignment offset and mask. 15773499a131SLiam R. Howlett * 15783499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 15793499a131SLiam R. Howlett */ 1580baceaf1cSJaewon Kim static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 1581db4fbfb9SMichel Lespinasse { 15826b008640SLinus Torvalds unsigned long length, gap, gap_end; 15836b008640SLinus Torvalds unsigned long low_limit, high_limit; 158458c5d0d6SLiam R. Howlett struct vm_area_struct *tmp; 1585d4af56c5SLiam R. Howlett 15863499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1587db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1588db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1589db4fbfb9SMichel Lespinasse if (length < info->length) 1590db4fbfb9SMichel Lespinasse return -ENOMEM; 1591db4fbfb9SMichel Lespinasse 15926b008640SLinus Torvalds low_limit = info->low_limit; 15936b008640SLinus Torvalds if (low_limit < mmap_min_addr) 15946b008640SLinus Torvalds low_limit = mmap_min_addr; 159558c5d0d6SLiam R. Howlett high_limit = info->high_limit; 159658c5d0d6SLiam R. Howlett retry: 15976b008640SLinus Torvalds if (mas_empty_area_rev(&mas, low_limit, high_limit - 1, length)) 15983499a131SLiam R. Howlett return -ENOMEM; 15993499a131SLiam R. Howlett 1600d4af56c5SLiam R. Howlett gap = mas.last + 1 - info->length; 1601d4af56c5SLiam R. Howlett gap -= (gap - info->align_offset) & info->align_mask; 160258c5d0d6SLiam R. Howlett gap_end = mas.last; 160358c5d0d6SLiam R. Howlett tmp = mas_next(&mas, ULONG_MAX); 160458c5d0d6SLiam R. Howlett if (tmp && (tmp->vm_flags & VM_GROWSDOWN)) { /* Avoid prev check if possible */ 160558c5d0d6SLiam R. Howlett if (vm_start_gap(tmp) <= gap_end) { 160658c5d0d6SLiam R. Howlett high_limit = vm_start_gap(tmp); 160758c5d0d6SLiam R. Howlett mas_reset(&mas); 160858c5d0d6SLiam R. Howlett goto retry; 160958c5d0d6SLiam R. Howlett } 161058c5d0d6SLiam R. Howlett } else { 161158c5d0d6SLiam R. Howlett tmp = mas_prev(&mas, 0); 161258c5d0d6SLiam R. Howlett if (tmp && vm_end_gap(tmp) > gap) { 161358c5d0d6SLiam R. Howlett high_limit = tmp->vm_start; 161458c5d0d6SLiam R. Howlett mas_reset(&mas); 161558c5d0d6SLiam R. Howlett goto retry; 161658c5d0d6SLiam R. Howlett } 161758c5d0d6SLiam R. Howlett } 161858c5d0d6SLiam R. Howlett 16193499a131SLiam R. Howlett return gap; 1620db4fbfb9SMichel Lespinasse } 1621db4fbfb9SMichel Lespinasse 1622baceaf1cSJaewon Kim /* 1623baceaf1cSJaewon Kim * Search for an unmapped address range. 1624baceaf1cSJaewon Kim * 1625baceaf1cSJaewon Kim * We are looking for a range that: 1626baceaf1cSJaewon Kim * - does not intersect with any VMA; 1627baceaf1cSJaewon Kim * - is contained within the [low_limit, high_limit) interval; 1628baceaf1cSJaewon Kim * - is at least the desired size. 1629baceaf1cSJaewon Kim * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) 1630baceaf1cSJaewon Kim */ 1631baceaf1cSJaewon Kim unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) 1632baceaf1cSJaewon Kim { 1633df529cabSJaewon Kim unsigned long addr; 1634df529cabSJaewon Kim 1635baceaf1cSJaewon Kim if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) 1636df529cabSJaewon Kim addr = unmapped_area_topdown(info); 1637baceaf1cSJaewon Kim else 1638df529cabSJaewon Kim addr = unmapped_area(info); 1639df529cabSJaewon Kim 1640df529cabSJaewon Kim trace_vm_unmapped_area(addr, info); 1641df529cabSJaewon Kim return addr; 1642baceaf1cSJaewon Kim } 1643f6795053SSteve Capper 16441da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 16451da177e4SLinus Torvalds * For shmat() with addr=0. 16461da177e4SLinus Torvalds * 16471da177e4SLinus Torvalds * Ugly calling convention alert: 16481da177e4SLinus Torvalds * Return value with the low bits set means error value, 16491da177e4SLinus Torvalds * ie 16501da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 16511da177e4SLinus Torvalds * error = ret; 16521da177e4SLinus Torvalds * 16531da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 16541da177e4SLinus Torvalds */ 16551da177e4SLinus Torvalds unsigned long 16564b439e25SChristophe Leroy generic_get_unmapped_area(struct file *filp, unsigned long addr, 16574b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16584b439e25SChristophe Leroy unsigned long flags) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 16611be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 1662db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 16632cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 16641da177e4SLinus Torvalds 1665f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 16661da177e4SLinus Torvalds return -ENOMEM; 16671da177e4SLinus Torvalds 166806abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 166906abdfb4SBenjamin Herrenschmidt return addr; 167006abdfb4SBenjamin Herrenschmidt 16711da177e4SLinus Torvalds if (addr) { 16721da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 16731be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1674f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 16751be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 16761be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 16771da177e4SLinus Torvalds return addr; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 1680db4fbfb9SMichel Lespinasse info.flags = 0; 1681db4fbfb9SMichel Lespinasse info.length = len; 16824e99b021SHeiko Carstens info.low_limit = mm->mmap_base; 1683f6795053SSteve Capper info.high_limit = mmap_end; 1684db4fbfb9SMichel Lespinasse info.align_mask = 0; 168509ef5283SJaewon Kim info.align_offset = 0; 1686db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 16871da177e4SLinus Torvalds } 16884b439e25SChristophe Leroy 16894b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA 16904b439e25SChristophe Leroy unsigned long 16914b439e25SChristophe Leroy arch_get_unmapped_area(struct file *filp, unsigned long addr, 16924b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 16934b439e25SChristophe Leroy unsigned long flags) 16944b439e25SChristophe Leroy { 16954b439e25SChristophe Leroy return generic_get_unmapped_area(filp, addr, len, pgoff, flags); 16964b439e25SChristophe Leroy } 16971da177e4SLinus Torvalds #endif 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds /* 17001da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 17011da177e4SLinus Torvalds * stack's low limit (the base): 17021da177e4SLinus Torvalds */ 17031da177e4SLinus Torvalds unsigned long 17044b439e25SChristophe Leroy generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 170543cca0b1SYang Fan unsigned long len, unsigned long pgoff, 170643cca0b1SYang Fan unsigned long flags) 17071da177e4SLinus Torvalds { 17081be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 17091da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1710db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 17112cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds /* requested length too big for entire address space */ 1714f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 17151da177e4SLinus Torvalds return -ENOMEM; 17161da177e4SLinus Torvalds 171706abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 171806abdfb4SBenjamin Herrenschmidt return addr; 171906abdfb4SBenjamin Herrenschmidt 17201da177e4SLinus Torvalds /* requesting a specific address */ 17211da177e4SLinus Torvalds if (addr) { 17221da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 17231be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1724f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 17251be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 17261be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 17271da177e4SLinus Torvalds return addr; 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds 1730db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 1731db4fbfb9SMichel Lespinasse info.length = len; 17326b008640SLinus Torvalds info.low_limit = PAGE_SIZE; 1733f6795053SSteve Capper info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); 1734db4fbfb9SMichel Lespinasse info.align_mask = 0; 173509ef5283SJaewon Kim info.align_offset = 0; 1736db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1737b716ad95SXiao Guangrong 17381da177e4SLinus Torvalds /* 17391da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 17401da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 17411da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 17421da177e4SLinus Torvalds * allocations. 17431da177e4SLinus Torvalds */ 1744de1741a1SAlexander Kuleshov if (offset_in_page(addr)) { 1745db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 1746db4fbfb9SMichel Lespinasse info.flags = 0; 1747db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1748f6795053SSteve Capper info.high_limit = mmap_end; 1749db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1750db4fbfb9SMichel Lespinasse } 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds return addr; 17531da177e4SLinus Torvalds } 17544b439e25SChristophe Leroy 17554b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 17564b439e25SChristophe Leroy unsigned long 17574b439e25SChristophe Leroy arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 17584b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 17594b439e25SChristophe Leroy unsigned long flags) 17604b439e25SChristophe Leroy { 17614b439e25SChristophe Leroy return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); 17624b439e25SChristophe Leroy } 17631da177e4SLinus Torvalds #endif 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds unsigned long 17661da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 17671da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 17681da177e4SLinus Torvalds { 176906abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 177006abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 177107ab67c8SLinus Torvalds 17729206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 17739206de95SAl Viro if (error) 17749206de95SAl Viro return error; 17759206de95SAl Viro 17769206de95SAl Viro /* Careful about overflows.. */ 17779206de95SAl Viro if (len > TASK_SIZE) 17789206de95SAl Viro return -ENOMEM; 17799206de95SAl Viro 178007ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 1781c01d5b30SHugh Dickins if (file) { 1782c01d5b30SHugh Dickins if (file->f_op->get_unmapped_area) 178307ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 1784c01d5b30SHugh Dickins } else if (flags & MAP_SHARED) { 1785c01d5b30SHugh Dickins /* 1786c01d5b30SHugh Dickins * mmap_region() will call shmem_zero_setup() to create a file, 1787c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge. 178845e55300SPeter Collingbourne * do_mmap() will clear pgoff, so match alignment. 1789c01d5b30SHugh Dickins */ 1790c01d5b30SHugh Dickins pgoff = 0; 1791c01d5b30SHugh Dickins get_area = shmem_get_unmapped_area; 1792c01d5b30SHugh Dickins } 1793c01d5b30SHugh Dickins 179407ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 179507ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 179607ab67c8SLinus Torvalds return addr; 179707ab67c8SLinus Torvalds 17981da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 17991da177e4SLinus Torvalds return -ENOMEM; 1800de1741a1SAlexander Kuleshov if (offset_in_page(addr)) 18011da177e4SLinus Torvalds return -EINVAL; 180206abdfb4SBenjamin Herrenschmidt 18039ac4ed4bSAl Viro error = security_mmap_addr(addr); 18049ac4ed4bSAl Viro return error ? error : addr; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 18081da177e4SLinus Torvalds 1809be8432e7SLiam R. Howlett /** 1810abdba2ddSLiam R. Howlett * find_vma_intersection() - Look up the first VMA which intersects the interval 1811abdba2ddSLiam R. Howlett * @mm: The process address space. 1812abdba2ddSLiam R. Howlett * @start_addr: The inclusive start user address. 1813abdba2ddSLiam R. Howlett * @end_addr: The exclusive end user address. 1814abdba2ddSLiam R. Howlett * 1815abdba2ddSLiam R. Howlett * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 1816abdba2ddSLiam R. Howlett * start_addr < end_addr. 1817abdba2ddSLiam R. Howlett */ 1818abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 1819abdba2ddSLiam R. Howlett unsigned long start_addr, 1820abdba2ddSLiam R. Howlett unsigned long end_addr) 1821abdba2ddSLiam R. Howlett { 1822abdba2ddSLiam R. Howlett unsigned long index = start_addr; 1823abdba2ddSLiam R. Howlett 1824abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 18257964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 1826abdba2ddSLiam R. Howlett } 1827abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 1828abdba2ddSLiam R. Howlett 1829abdba2ddSLiam R. Howlett /** 1830be8432e7SLiam R. Howlett * find_vma() - Find the VMA for a given address, or the next VMA. 1831be8432e7SLiam R. Howlett * @mm: The mm_struct to check 1832be8432e7SLiam R. Howlett * @addr: The address 1833be8432e7SLiam R. Howlett * 1834be8432e7SLiam R. Howlett * Returns: The VMA associated with addr, or the next VMA. 1835be8432e7SLiam R. Howlett * May return %NULL in the case of no VMA at addr or above. 1836be8432e7SLiam R. Howlett */ 18371da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 18381da177e4SLinus Torvalds { 1839be8432e7SLiam R. Howlett unsigned long index = addr; 18401da177e4SLinus Torvalds 18415b78ed24SLuigi Rizzo mmap_assert_locked(mm); 18427964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, ULONG_MAX); 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 18451da177e4SLinus Torvalds 18467fdbd37dSLiam R. Howlett /** 18477fdbd37dSLiam R. Howlett * find_vma_prev() - Find the VMA for a given address, or the next vma and 18487fdbd37dSLiam R. Howlett * set %pprev to the previous VMA, if any. 18497fdbd37dSLiam R. Howlett * @mm: The mm_struct to check 18507fdbd37dSLiam R. Howlett * @addr: The address 18517fdbd37dSLiam R. Howlett * @pprev: The pointer to set to the previous VMA 18527fdbd37dSLiam R. Howlett * 18537fdbd37dSLiam R. Howlett * Note that RCU lock is missing here since the external mmap_lock() is used 18547fdbd37dSLiam R. Howlett * instead. 18557fdbd37dSLiam R. Howlett * 18567fdbd37dSLiam R. Howlett * Returns: The VMA associated with @addr, or the next vma. 18577fdbd37dSLiam R. Howlett * May return %NULL in the case of no vma at addr or above. 18586bd4837dSKOSAKI Motohiro */ 18591da177e4SLinus Torvalds struct vm_area_struct * 18601da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 18611da177e4SLinus Torvalds struct vm_area_struct **pprev) 18621da177e4SLinus Torvalds { 18636bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 18647fdbd37dSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 18651da177e4SLinus Torvalds 18667fdbd37dSLiam R. Howlett vma = mas_walk(&mas); 18677fdbd37dSLiam R. Howlett *pprev = mas_prev(&mas, 0); 18687fdbd37dSLiam R. Howlett if (!vma) 18697fdbd37dSLiam R. Howlett vma = mas_next(&mas, ULONG_MAX); 18706bd4837dSKOSAKI Motohiro return vma; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* 18741da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 18751da177e4SLinus Torvalds * update accounting. This is shared with both the 18761da177e4SLinus Torvalds * grow-up and grow-down cases. 18771da177e4SLinus Torvalds */ 18781be7107fSHugh Dickins static int acct_stack_growth(struct vm_area_struct *vma, 18791be7107fSHugh Dickins unsigned long size, unsigned long grow) 18801da177e4SLinus Torvalds { 18811da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 18821be7107fSHugh Dickins unsigned long new_start; 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds /* address space limit tests */ 188584638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, grow)) 18861da177e4SLinus Torvalds return -ENOMEM; 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds /* Stack limit test */ 188924c79d8eSKrzysztof Opasiak if (size > rlimit(RLIMIT_STACK)) 18901da177e4SLinus Torvalds return -ENOMEM; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds /* mlock limit tests */ 1893*3c54a298SLorenzo Stoakes if (!mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) 18941da177e4SLinus Torvalds return -ENOMEM; 18951da177e4SLinus Torvalds 18960d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 18970d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 18980d59a01bSAdam Litke vma->vm_end - size; 18990d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 19000d59a01bSAdam Litke return -EFAULT; 19010d59a01bSAdam Litke 19021da177e4SLinus Torvalds /* 19031da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 19041da177e4SLinus Torvalds * update security statistics. 19051da177e4SLinus Torvalds */ 190605fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 19071da177e4SLinus Torvalds return -ENOMEM; 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds return 0; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 191246dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 19131da177e4SLinus Torvalds /* 191446dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 191546dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 19161da177e4SLinus Torvalds */ 191746dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 19181da177e4SLinus Torvalds { 191909357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 19201be7107fSHugh Dickins struct vm_area_struct *next; 19211be7107fSHugh Dickins unsigned long gap_addr; 192212352d3cSKonstantin Khlebnikov int error = 0; 1923d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 19261da177e4SLinus Torvalds return -EFAULT; 19271da177e4SLinus Torvalds 1928bd726c90SHelge Deller /* Guard against exceeding limits of the address space. */ 19291be7107fSHugh Dickins address &= PAGE_MASK; 193037511fb5SHelge Deller if (address >= (TASK_SIZE & PAGE_MASK)) 193112352d3cSKonstantin Khlebnikov return -ENOMEM; 1932bd726c90SHelge Deller address += PAGE_SIZE; 193312352d3cSKonstantin Khlebnikov 19341be7107fSHugh Dickins /* Enforce stack_guard_gap */ 19351be7107fSHugh Dickins gap_addr = address + stack_guard_gap; 1936bd726c90SHelge Deller 1937bd726c90SHelge Deller /* Guard against overflow */ 1938bd726c90SHelge Deller if (gap_addr < address || gap_addr > TASK_SIZE) 1939bd726c90SHelge Deller gap_addr = TASK_SIZE; 1940bd726c90SHelge Deller 1941763ecb03SLiam R. Howlett next = find_vma_intersection(mm, vma->vm_end, gap_addr); 1942763ecb03SLiam R. Howlett if (next && vma_is_accessible(next)) { 19431be7107fSHugh Dickins if (!(next->vm_flags & VM_GROWSUP)) 19441be7107fSHugh Dickins return -ENOMEM; 19451be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 19461be7107fSHugh Dickins } 19471be7107fSHugh Dickins 1948c5d5546eSVernon Yang if (mas_preallocate(&mas, GFP_KERNEL)) 19491da177e4SLinus Torvalds return -ENOMEM; 19501da177e4SLinus Torvalds 1951d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 1952d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 1953d4af56c5SLiam R. Howlett mas_destroy(&mas); 1954d4af56c5SLiam R. Howlett return -ENOMEM; 1955d4af56c5SLiam R. Howlett } 1956d4af56c5SLiam R. Howlett 19571da177e4SLinus Torvalds /* 19581da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 1959c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 19601da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 19611da177e4SLinus Torvalds */ 196212352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 19651da177e4SLinus Torvalds if (address > vma->vm_end) { 19661da177e4SLinus Torvalds unsigned long size, grow; 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds size = address - vma->vm_start; 19691da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 19701da177e4SLinus Torvalds 197142c36f63SHugh Dickins error = -ENOMEM; 197242c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 19731da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 19743af9e859SEric B Munson if (!error) { 19754128997bSMichel Lespinasse /* 1976524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 1977524e00b3SLiam R. Howlett * we need to protect against concurrent vma 1978524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 1979524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 1980524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 1981524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 1982524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 19834128997bSMichel Lespinasse */ 198409357814SOleg Nesterov spin_lock(&mm->page_table_lock); 198587e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 198609357814SOleg Nesterov mm->locked_vm += grow; 198784638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 1988bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 19891da177e4SLinus Torvalds vma->vm_end = address; 1990d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 1991fbcc3104SLiam R. Howlett mas_set_range(&mas, vma->vm_start, address - 1); 1992fbcc3104SLiam R. Howlett mas_store_prealloc(&mas, vma); 1993bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 199409357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 19954128997bSMichel Lespinasse 19963af9e859SEric B Munson perf_event_mmap(vma); 19973af9e859SEric B Munson } 19981da177e4SLinus Torvalds } 199942c36f63SHugh Dickins } 200012352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2001c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2002d4af56c5SLiam R. Howlett mas_destroy(&mas); 20031da177e4SLinus Torvalds return error; 20041da177e4SLinus Torvalds } 200546dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 200646dea3d0SHugh Dickins 20071da177e4SLinus Torvalds /* 20081da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 20091da177e4SLinus Torvalds */ 2010524e00b3SLiam R. Howlett int expand_downwards(struct vm_area_struct *vma, unsigned long address) 20111da177e4SLinus Torvalds { 201209357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 2013763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); 20141be7107fSHugh Dickins struct vm_area_struct *prev; 20150a1d5299SJann Horn int error = 0; 20161da177e4SLinus Torvalds 20178869477aSEric Paris address &= PAGE_MASK; 20180a1d5299SJann Horn if (address < mmap_min_addr) 20190a1d5299SJann Horn return -EPERM; 20208869477aSEric Paris 20211be7107fSHugh Dickins /* Enforce stack_guard_gap */ 2022763ecb03SLiam R. Howlett prev = mas_prev(&mas, 0); 20231be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 202432e4e6d5SOleg Nesterov if (prev && !(prev->vm_flags & VM_GROWSDOWN) && 20253122e80eSAnshuman Khandual vma_is_accessible(prev)) { 202632e4e6d5SOleg Nesterov if (address - prev->vm_end < stack_guard_gap) 202732e4e6d5SOleg Nesterov return -ENOMEM; 20281be7107fSHugh Dickins } 20291be7107fSHugh Dickins 2030c5d5546eSVernon Yang if (mas_preallocate(&mas, GFP_KERNEL)) 203112352d3cSKonstantin Khlebnikov return -ENOMEM; 20321da177e4SLinus Torvalds 2033d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2034d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2035d4af56c5SLiam R. Howlett mas_destroy(&mas); 2036d4af56c5SLiam R. Howlett return -ENOMEM; 2037d4af56c5SLiam R. Howlett } 2038d4af56c5SLiam R. Howlett 20391da177e4SLinus Torvalds /* 20401da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2041c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 20421da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 20431da177e4SLinus Torvalds */ 204412352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 20471da177e4SLinus Torvalds if (address < vma->vm_start) { 20481da177e4SLinus Torvalds unsigned long size, grow; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds size = vma->vm_end - address; 20511da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 20521da177e4SLinus Torvalds 2053a626ca6aSLinus Torvalds error = -ENOMEM; 2054a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 20551da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 20561da177e4SLinus Torvalds if (!error) { 20574128997bSMichel Lespinasse /* 2058524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2059524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2060524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2061524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2062524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2063524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2064524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 20654128997bSMichel Lespinasse */ 206609357814SOleg Nesterov spin_lock(&mm->page_table_lock); 206787e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 206809357814SOleg Nesterov mm->locked_vm += grow; 206984638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2070bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 20711da177e4SLinus Torvalds vma->vm_start = address; 20721da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2073d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2074fbcc3104SLiam R. Howlett mas_set_range(&mas, address, vma->vm_end - 1); 2075fbcc3104SLiam R. Howlett mas_store_prealloc(&mas, vma); 2076bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 207709357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 20784128997bSMichel Lespinasse 20793af9e859SEric B Munson perf_event_mmap(vma); 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds } 2082a626ca6aSLinus Torvalds } 208312352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2084c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2085d4af56c5SLiam R. Howlett mas_destroy(&mas); 20861da177e4SLinus Torvalds return error; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 20891be7107fSHugh Dickins /* enforced gap between the expanding stack and other mappings. */ 20901be7107fSHugh Dickins unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; 20911be7107fSHugh Dickins 20921be7107fSHugh Dickins static int __init cmdline_parse_stack_guard_gap(char *p) 20931be7107fSHugh Dickins { 20941be7107fSHugh Dickins unsigned long val; 20951be7107fSHugh Dickins char *endptr; 20961be7107fSHugh Dickins 20971be7107fSHugh Dickins val = simple_strtoul(p, &endptr, 10); 20981be7107fSHugh Dickins if (!*endptr) 20991be7107fSHugh Dickins stack_guard_gap = val << PAGE_SHIFT; 21001be7107fSHugh Dickins 2101e6d09493SRandy Dunlap return 1; 21021be7107fSHugh Dickins } 21031be7107fSHugh Dickins __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); 21041be7107fSHugh Dickins 2105b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2106b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2107b6a2fea3SOllie Wild { 2108b6a2fea3SOllie Wild return expand_upwards(vma, address); 2109b6a2fea3SOllie Wild } 2110b6a2fea3SOllie Wild 2111b6a2fea3SOllie Wild struct vm_area_struct * 2112b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2113b6a2fea3SOllie Wild { 2114b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2115b6a2fea3SOllie Wild 2116b6a2fea3SOllie Wild addr &= PAGE_MASK; 2117b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2118b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2119b6a2fea3SOllie Wild return vma; 21204d45e75aSJann Horn if (!prev || expand_stack(prev, addr)) 2121b6a2fea3SOllie Wild return NULL; 2122cea10a19SMichel Lespinasse if (prev->vm_flags & VM_LOCKED) 2123fc05f566SKirill A. Shutemov populate_vma_page_range(prev, addr, prev->vm_end, NULL); 2124b6a2fea3SOllie Wild return prev; 2125b6a2fea3SOllie Wild } 2126b6a2fea3SOllie Wild #else 2127b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2128b6a2fea3SOllie Wild { 2129b6a2fea3SOllie Wild return expand_downwards(vma, address); 2130b6a2fea3SOllie Wild } 2131b6a2fea3SOllie Wild 21321da177e4SLinus Torvalds struct vm_area_struct * 21331da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 21341da177e4SLinus Torvalds { 21351da177e4SLinus Torvalds struct vm_area_struct *vma; 21361da177e4SLinus Torvalds unsigned long start; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds addr &= PAGE_MASK; 21391da177e4SLinus Torvalds vma = find_vma(mm, addr); 21401da177e4SLinus Torvalds if (!vma) 21411da177e4SLinus Torvalds return NULL; 21421da177e4SLinus Torvalds if (vma->vm_start <= addr) 21431da177e4SLinus Torvalds return vma; 21441da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 21451da177e4SLinus Torvalds return NULL; 21461da177e4SLinus Torvalds start = vma->vm_start; 21471da177e4SLinus Torvalds if (expand_stack(vma, addr)) 21481da177e4SLinus Torvalds return NULL; 2149cea10a19SMichel Lespinasse if (vma->vm_flags & VM_LOCKED) 2150fc05f566SKirill A. Shutemov populate_vma_page_range(vma, addr, start, NULL); 21511da177e4SLinus Torvalds return vma; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds #endif 21541da177e4SLinus Torvalds 2155e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(find_extend_vma); 2156e1d6d01aSJesse Barnes 21572c0b3814SHugh Dickins /* 2158763ecb03SLiam R. Howlett * Ok - we have the memory areas we should free on a maple tree so release them, 2159763ecb03SLiam R. Howlett * and do the vma updates. 21601da177e4SLinus Torvalds * 21612c0b3814SHugh Dickins * Called with the mm semaphore held. 21621da177e4SLinus Torvalds */ 2163763ecb03SLiam R. Howlett static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) 21641da177e4SLinus Torvalds { 21654f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 2166763ecb03SLiam R. Howlett struct vm_area_struct *vma; 21674f74d2c8SLinus Torvalds 2168365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2169365e9c87SHugh Dickins update_hiwater_vm(mm); 2170763ecb03SLiam R. Howlett mas_for_each(mas, vma, ULONG_MAX) { 2171ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 21721da177e4SLinus Torvalds 21734f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 21744f74d2c8SLinus Torvalds nr_accounted += nrpages; 217584638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, -nrpages); 21760d2ebf9cSSuren Baghdasaryan remove_vma(vma, false); 2177763ecb03SLiam R. Howlett } 21784f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 21791da177e4SLinus Torvalds validate_mm(mm); 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* 21831da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 21841da177e4SLinus Torvalds * 2185f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 21861da177e4SLinus Torvalds */ 2187763ecb03SLiam R. Howlett static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, 2188e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2189763ecb03SLiam R. Howlett struct vm_area_struct *next, 219068f48381SSuren Baghdasaryan unsigned long start, unsigned long end, bool mm_wr_locked) 21911da177e4SLinus Torvalds { 2192d16dfc55SPeter Zijlstra struct mmu_gather tlb; 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds lru_add_drain(); 2195a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm); 2196365e9c87SHugh Dickins update_hiwater_rss(mm); 219768f48381SSuren Baghdasaryan unmap_vmas(&tlb, mt, vma, start, end, mm_wr_locked); 2198763ecb03SLiam R. Howlett free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 219998e51a22SSuren Baghdasaryan next ? next->vm_start : USER_PGTABLES_CEILING, 220098e51a22SSuren Baghdasaryan mm_wr_locked); 2201ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds /* 2205def5efe0SDavid Rientjes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 2206def5efe0SDavid Rientjes * has already been checked or doesn't make sense to fail. 22070fd5a9e2SLiam R. Howlett * VMA Iterator will point to the end VMA. 22081da177e4SLinus Torvalds */ 22099760ebffSLiam R. Howlett int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 22101da177e4SLinus Torvalds unsigned long addr, int new_below) 22111da177e4SLinus Torvalds { 2212b2b3b886SLiam R. Howlett struct vma_prepare vp; 22131da177e4SLinus Torvalds struct vm_area_struct *new; 2214e3975891SChen Gang int err; 22159760ebffSLiam R. Howlett 2216b50e195fSLiam R. Howlett validate_mm(vma->vm_mm); 22171da177e4SLinus Torvalds 2218b2b3b886SLiam R. Howlett WARN_ON(vma->vm_start >= addr); 2219b2b3b886SLiam R. Howlett WARN_ON(vma->vm_end <= addr); 2220b2b3b886SLiam R. Howlett 2221dd3b614fSDmitry Safonov if (vma->vm_ops && vma->vm_ops->may_split) { 2222dd3b614fSDmitry Safonov err = vma->vm_ops->may_split(vma, addr); 222331383c68SDan Williams if (err) 222431383c68SDan Williams return err; 222531383c68SDan Williams } 22261da177e4SLinus Torvalds 22273928d4f5SLinus Torvalds new = vm_area_dup(vma); 22281da177e4SLinus Torvalds if (!new) 2229e3975891SChen Gang return -ENOMEM; 22301da177e4SLinus Torvalds 2231b2b3b886SLiam R. Howlett err = -ENOMEM; 2232b2b3b886SLiam R. Howlett if (vma_iter_prealloc(vmi)) 2233b2b3b886SLiam R. Howlett goto out_free_vma; 2234b2b3b886SLiam R. Howlett 2235b2b3b886SLiam R. Howlett if (new_below) { 22361da177e4SLinus Torvalds new->vm_end = addr; 2237b2b3b886SLiam R. Howlett } else { 22381da177e4SLinus Torvalds new->vm_start = addr; 22391da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 2242ef0855d3SOleg Nesterov err = vma_dup_policy(vma, new); 2243ef0855d3SOleg Nesterov if (err) 2244b2b3b886SLiam R. Howlett goto out_free_vmi; 22451da177e4SLinus Torvalds 2246c4ea95d7SDaniel Forrest err = anon_vma_clone(new, vma); 2247c4ea95d7SDaniel Forrest if (err) 22485beb4930SRik van Riel goto out_free_mpol; 22495beb4930SRik van Riel 2250e9714acfSKonstantin Khlebnikov if (new->vm_file) 22511da177e4SLinus Torvalds get_file(new->vm_file); 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 22541da177e4SLinus Torvalds new->vm_ops->open(new); 22551da177e4SLinus Torvalds 2256b2b3b886SLiam R. Howlett init_vma_prep(&vp, vma); 2257b2b3b886SLiam R. Howlett vp.insert = new; 2258b2b3b886SLiam R. Howlett vma_prepare(&vp); 2259ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, vma->vm_start, addr, 0); 22601da177e4SLinus Torvalds 2261b2b3b886SLiam R. Howlett if (new_below) { 2262b2b3b886SLiam R. Howlett vma->vm_start = addr; 2263b2b3b886SLiam R. Howlett vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT; 2264b2b3b886SLiam R. Howlett } else { 2265b2b3b886SLiam R. Howlett vma->vm_end = addr; 22669760ebffSLiam R. Howlett } 22675beb4930SRik van Riel 2268b2b3b886SLiam R. Howlett /* vma_complete stores the new vma */ 2269b2b3b886SLiam R. Howlett vma_complete(&vp, vmi, vma->vm_mm); 2270b2b3b886SLiam R. Howlett 2271b2b3b886SLiam R. Howlett /* Success. */ 2272b2b3b886SLiam R. Howlett if (new_below) 2273b2b3b886SLiam R. Howlett vma_next(vmi); 2274b50e195fSLiam R. Howlett validate_mm(vma->vm_mm); 2275b2b3b886SLiam R. Howlett return 0; 2276b2b3b886SLiam R. Howlett 22775beb4930SRik van Riel out_free_mpol: 2278ef0855d3SOleg Nesterov mpol_put(vma_policy(new)); 2279b2b3b886SLiam R. Howlett out_free_vmi: 2280b2b3b886SLiam R. Howlett vma_iter_free(vmi); 22815beb4930SRik van Riel out_free_vma: 22823928d4f5SLinus Torvalds vm_area_free(new); 2283b50e195fSLiam R. Howlett validate_mm(vma->vm_mm); 22845beb4930SRik van Riel return err; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 2287659ace58SKOSAKI Motohiro /* 2288659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2289659ace58SKOSAKI Motohiro * either for the first part or the tail. 2290659ace58SKOSAKI Motohiro */ 22919760ebffSLiam R. Howlett int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 2292659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2293659ace58SKOSAKI Motohiro { 22949760ebffSLiam R. Howlett if (vma->vm_mm->map_count >= sysctl_max_map_count) 2295659ace58SKOSAKI Motohiro return -ENOMEM; 2296659ace58SKOSAKI Motohiro 22979760ebffSLiam R. Howlett return __split_vma(vmi, vma, addr, new_below); 2298f2ebfe43SLiam R. Howlett } 2299f2ebfe43SLiam R. Howlett 2300763ecb03SLiam R. Howlett static inline int munmap_sidetree(struct vm_area_struct *vma, 2301763ecb03SLiam R. Howlett struct ma_state *mas_detach) 23021da177e4SLinus Torvalds { 230373046fd0SSuren Baghdasaryan vma_start_write(vma); 2304763ecb03SLiam R. Howlett mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); 2305763ecb03SLiam R. Howlett if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) 2306763ecb03SLiam R. Howlett return -ENOMEM; 230711f9a21aSLiam R. Howlett 2308457f67beSSuren Baghdasaryan vma_mark_detached(vma, true); 2309763ecb03SLiam R. Howlett if (vma->vm_flags & VM_LOCKED) 2310763ecb03SLiam R. Howlett vma->vm_mm->locked_vm -= vma_pages(vma); 231111f9a21aSLiam R. Howlett 2312763ecb03SLiam R. Howlett return 0; 231311f9a21aSLiam R. Howlett } 231411f9a21aSLiam R. Howlett 231511f9a21aSLiam R. Howlett /* 2316183654ceSLiam R. Howlett * do_vmi_align_munmap() - munmap the aligned region from @start to @end. 2317183654ceSLiam R. Howlett * @vmi: The vma iterator 231811f9a21aSLiam R. Howlett * @vma: The starting vm_area_struct 231911f9a21aSLiam R. Howlett * @mm: The mm_struct 232011f9a21aSLiam R. Howlett * @start: The aligned start address to munmap. 232111f9a21aSLiam R. Howlett * @end: The aligned end address to munmap. 232211f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 23238651a137SLorenzo Stoakes * @downgrade: Set to true to attempt a write downgrade of the mmap_lock 232411f9a21aSLiam R. Howlett * 232511f9a21aSLiam R. Howlett * If @downgrade is true, check return code for potential release of the lock. 232611f9a21aSLiam R. Howlett */ 232711f9a21aSLiam R. Howlett static int 2328183654ceSLiam R. Howlett do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 232911f9a21aSLiam R. Howlett struct mm_struct *mm, unsigned long start, 233011f9a21aSLiam R. Howlett unsigned long end, struct list_head *uf, bool downgrade) 233111f9a21aSLiam R. Howlett { 2332763ecb03SLiam R. Howlett struct vm_area_struct *prev, *next = NULL; 2333763ecb03SLiam R. Howlett struct maple_tree mt_detach; 2334763ecb03SLiam R. Howlett int count = 0; 2335d4af56c5SLiam R. Howlett int error = -ENOMEM; 2336763ecb03SLiam R. Howlett MA_STATE(mas_detach, &mt_detach, 0, 0); 23373dd44325SLiam R. Howlett mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); 2338763ecb03SLiam R. Howlett mt_set_external_lock(&mt_detach, &mm->mmap_lock); 2339524e00b3SLiam R. Howlett 23401da177e4SLinus Torvalds /* 23411da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 23421da177e4SLinus Torvalds * 23431da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 23441da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 23451da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 23461da177e4SLinus Torvalds */ 2347763ecb03SLiam R. Howlett 2348763ecb03SLiam R. Howlett /* Does it split the first one? */ 2349146425a3SHugh Dickins if (start > vma->vm_start) { 2350659ace58SKOSAKI Motohiro 2351659ace58SKOSAKI Motohiro /* 2352659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2353659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2354659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2355659ace58SKOSAKI Motohiro */ 2356659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2357d4af56c5SLiam R. Howlett goto map_count_exceeded; 2358659ace58SKOSAKI Motohiro 23599760ebffSLiam R. Howlett error = __split_vma(vmi, vma, start, 0); 23601da177e4SLinus Torvalds if (error) 2361763ecb03SLiam R. Howlett goto start_split_failed; 236211f9a21aSLiam R. Howlett 23630fd5a9e2SLiam R. Howlett vma = vma_iter_load(vmi); 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 2366183654ceSLiam R. Howlett prev = vma_prev(vmi); 2367763ecb03SLiam R. Howlett if (unlikely((!prev))) 2368183654ceSLiam R. Howlett vma_iter_set(vmi, start); 236911f9a21aSLiam R. Howlett 2370763ecb03SLiam R. Howlett /* 2371763ecb03SLiam R. Howlett * Detach a range of VMAs from the mm. Using next as a temp variable as 2372763ecb03SLiam R. Howlett * it is always overwritten. 2373763ecb03SLiam R. Howlett */ 2374183654ceSLiam R. Howlett for_each_vma_range(*vmi, next, end) { 2375763ecb03SLiam R. Howlett /* Does it split the end? */ 2376763ecb03SLiam R. Howlett if (next->vm_end > end) { 23776b73cff2SLiam R. Howlett error = __split_vma(vmi, next, end, 0); 23781da177e4SLinus Torvalds if (error) 2379763ecb03SLiam R. Howlett goto end_split_failed; 238011f9a21aSLiam R. Howlett } 2381763ecb03SLiam R. Howlett error = munmap_sidetree(next, &mas_detach); 2382763ecb03SLiam R. Howlett if (error) 2383763ecb03SLiam R. Howlett goto munmap_sidetree_failed; 2384763ecb03SLiam R. Howlett 2385763ecb03SLiam R. Howlett count++; 2386763ecb03SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 2387763ecb03SLiam R. Howlett BUG_ON(next->vm_start < start); 2388763ecb03SLiam R. Howlett BUG_ON(next->vm_start > end); 2389763ecb03SLiam R. Howlett #endif 2390763ecb03SLiam R. Howlett } 2391763ecb03SLiam R. Howlett 239215c0c60bSLiam R. Howlett if (vma_iter_end(vmi) > end) 239315c0c60bSLiam R. Howlett next = vma_iter_load(vmi); 239415c0c60bSLiam R. Howlett 239515c0c60bSLiam R. Howlett if (!next) 2396183654ceSLiam R. Howlett next = vma_next(vmi); 239715c0c60bSLiam R. Howlett 23982376dd7cSAndrea Arcangeli if (unlikely(uf)) { 23992376dd7cSAndrea Arcangeli /* 24002376dd7cSAndrea Arcangeli * If userfaultfd_unmap_prep returns an error the vmas 2401f0953a1bSIngo Molnar * will remain split, but userland will get a 24022376dd7cSAndrea Arcangeli * highly unexpected error anyway. This is no 24032376dd7cSAndrea Arcangeli * different than the case where the first of the two 24042376dd7cSAndrea Arcangeli * __split_vma fails, but we don't undo the first 24052376dd7cSAndrea Arcangeli * split, despite we could. This is unlikely enough 24062376dd7cSAndrea Arcangeli * failure that it's not worth optimizing it for. 24072376dd7cSAndrea Arcangeli */ 240869dbe6daSLiam R. Howlett error = userfaultfd_unmap_prep(mm, start, end, uf); 240911f9a21aSLiam R. Howlett 24102376dd7cSAndrea Arcangeli if (error) 2411d4af56c5SLiam R. Howlett goto userfaultfd_error; 24122376dd7cSAndrea Arcangeli } 24132376dd7cSAndrea Arcangeli 2414763ecb03SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 2415763ecb03SLiam R. Howlett /* Make sure no VMAs are about to be lost. */ 2416763ecb03SLiam R. Howlett { 2417763ecb03SLiam R. Howlett MA_STATE(test, &mt_detach, start, end - 1); 2418763ecb03SLiam R. Howlett struct vm_area_struct *vma_mas, *vma_test; 2419763ecb03SLiam R. Howlett int test_count = 0; 2420763ecb03SLiam R. Howlett 2421183654ceSLiam R. Howlett vma_iter_set(vmi, start); 2422763ecb03SLiam R. Howlett rcu_read_lock(); 2423763ecb03SLiam R. Howlett vma_test = mas_find(&test, end - 1); 2424183654ceSLiam R. Howlett for_each_vma_range(*vmi, vma_mas, end) { 2425763ecb03SLiam R. Howlett BUG_ON(vma_mas != vma_test); 2426763ecb03SLiam R. Howlett test_count++; 2427763ecb03SLiam R. Howlett vma_test = mas_next(&test, end - 1); 2428763ecb03SLiam R. Howlett } 2429763ecb03SLiam R. Howlett rcu_read_unlock(); 2430763ecb03SLiam R. Howlett BUG_ON(count != test_count); 2431763ecb03SLiam R. Howlett } 2432763ecb03SLiam R. Howlett #endif 24330378c0a0SLiam R. Howlett /* Point of no return */ 2434183654ceSLiam R. Howlett vma_iter_set(vmi, start); 2435183654ceSLiam R. Howlett if (vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL)) 24360378c0a0SLiam R. Howlett return -ENOMEM; 24370378c0a0SLiam R. Howlett 2438763ecb03SLiam R. Howlett mm->map_count -= count; 243911f9a21aSLiam R. Howlett /* 244011f9a21aSLiam R. Howlett * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or 244111f9a21aSLiam R. Howlett * VM_GROWSUP VMA. Such VMAs can change their size under 244211f9a21aSLiam R. Howlett * down_read(mmap_lock) and collide with the VMA we are about to unmap. 244311f9a21aSLiam R. Howlett */ 244411f9a21aSLiam R. Howlett if (downgrade) { 2445763ecb03SLiam R. Howlett if (next && (next->vm_flags & VM_GROWSDOWN)) 244611f9a21aSLiam R. Howlett downgrade = false; 244711f9a21aSLiam R. Howlett else if (prev && (prev->vm_flags & VM_GROWSUP)) 244811f9a21aSLiam R. Howlett downgrade = false; 244911f9a21aSLiam R. Howlett else 2450d8ed45c5SMichel Lespinasse mmap_write_downgrade(mm); 245111f9a21aSLiam R. Howlett } 2452dd2283f2SYang Shi 245368f48381SSuren Baghdasaryan /* 245468f48381SSuren Baghdasaryan * We can free page tables without write-locking mmap_lock because VMAs 245568f48381SSuren Baghdasaryan * were isolated before we downgraded mmap_lock. 245668f48381SSuren Baghdasaryan */ 245768f48381SSuren Baghdasaryan unmap_region(mm, &mt_detach, vma, prev, next, start, end, !downgrade); 2458763ecb03SLiam R. Howlett /* Statistics and freeing VMAs */ 2459763ecb03SLiam R. Howlett mas_set(&mas_detach, start); 2460763ecb03SLiam R. Howlett remove_mt(mm, &mas_detach); 2461763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 24621da177e4SLinus Torvalds 2463524e00b3SLiam R. Howlett 2464524e00b3SLiam R. Howlett validate_mm(mm); 2465dd2283f2SYang Shi return downgrade ? 1 : 0; 2466d4af56c5SLiam R. Howlett 2467d4af56c5SLiam R. Howlett userfaultfd_error: 2468763ecb03SLiam R. Howlett munmap_sidetree_failed: 2469763ecb03SLiam R. Howlett end_split_failed: 2470763ecb03SLiam R. Howlett __mt_destroy(&mt_detach); 2471763ecb03SLiam R. Howlett start_split_failed: 2472763ecb03SLiam R. Howlett map_count_exceeded: 2473d4af56c5SLiam R. Howlett return error; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 247611f9a21aSLiam R. Howlett /* 2477183654ceSLiam R. Howlett * do_vmi_munmap() - munmap a given range. 2478183654ceSLiam R. Howlett * @vmi: The vma iterator 247911f9a21aSLiam R. Howlett * @mm: The mm_struct 248011f9a21aSLiam R. Howlett * @start: The start address to munmap 248111f9a21aSLiam R. Howlett * @len: The length of the range to munmap 248211f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 248311f9a21aSLiam R. Howlett * @downgrade: set to true if the user wants to attempt to write_downgrade the 24848651a137SLorenzo Stoakes * mmap_lock 248511f9a21aSLiam R. Howlett * 248611f9a21aSLiam R. Howlett * This function takes a @mas that is either pointing to the previous VMA or set 248711f9a21aSLiam R. Howlett * to MA_START and sets it up to remove the mapping(s). The @len will be 248811f9a21aSLiam R. Howlett * aligned and any arch_unmap work will be preformed. 248911f9a21aSLiam R. Howlett * 249011f9a21aSLiam R. Howlett * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. 249111f9a21aSLiam R. Howlett */ 2492183654ceSLiam R. Howlett int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 249311f9a21aSLiam R. Howlett unsigned long start, size_t len, struct list_head *uf, 249411f9a21aSLiam R. Howlett bool downgrade) 249511f9a21aSLiam R. Howlett { 249611f9a21aSLiam R. Howlett unsigned long end; 249711f9a21aSLiam R. Howlett struct vm_area_struct *vma; 249811f9a21aSLiam R. Howlett 249911f9a21aSLiam R. Howlett if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 250011f9a21aSLiam R. Howlett return -EINVAL; 250111f9a21aSLiam R. Howlett 250211f9a21aSLiam R. Howlett end = start + PAGE_ALIGN(len); 250311f9a21aSLiam R. Howlett if (end == start) 250411f9a21aSLiam R. Howlett return -EINVAL; 250511f9a21aSLiam R. Howlett 250611f9a21aSLiam R. Howlett /* arch_unmap() might do unmaps itself. */ 250711f9a21aSLiam R. Howlett arch_unmap(mm, start, end); 250811f9a21aSLiam R. Howlett 250911f9a21aSLiam R. Howlett /* Find the first overlapping VMA */ 2510183654ceSLiam R. Howlett vma = vma_find(vmi, end); 251111f9a21aSLiam R. Howlett if (!vma) 251211f9a21aSLiam R. Howlett return 0; 251311f9a21aSLiam R. Howlett 2514183654ceSLiam R. Howlett return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); 251511f9a21aSLiam R. Howlett } 251611f9a21aSLiam R. Howlett 251711f9a21aSLiam R. Howlett /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. 251811f9a21aSLiam R. Howlett * @mm: The mm_struct 251911f9a21aSLiam R. Howlett * @start: The start address to munmap 252011f9a21aSLiam R. Howlett * @len: The length to be munmapped. 252111f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 252211f9a21aSLiam R. Howlett */ 2523dd2283f2SYang Shi int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2524dd2283f2SYang Shi struct list_head *uf) 2525dd2283f2SYang Shi { 2526183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 252711f9a21aSLiam R. Howlett 2528183654ceSLiam R. Howlett return do_vmi_munmap(&vmi, mm, start, len, uf, false); 2529dd2283f2SYang Shi } 2530dd2283f2SYang Shi 2531e99668a5SLiam R. Howlett unsigned long mmap_region(struct file *file, unsigned long addr, 2532e99668a5SLiam R. Howlett unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, 2533e99668a5SLiam R. Howlett struct list_head *uf) 2534e99668a5SLiam R. Howlett { 2535e99668a5SLiam R. Howlett struct mm_struct *mm = current->mm; 2536e99668a5SLiam R. Howlett struct vm_area_struct *vma = NULL; 2537e99668a5SLiam R. Howlett struct vm_area_struct *next, *prev, *merge; 2538e99668a5SLiam R. Howlett pgoff_t pglen = len >> PAGE_SHIFT; 2539e99668a5SLiam R. Howlett unsigned long charged = 0; 2540e99668a5SLiam R. Howlett unsigned long end = addr + len; 2541e99668a5SLiam R. Howlett unsigned long merge_start = addr, merge_end = end; 2542e99668a5SLiam R. Howlett pgoff_t vm_pgoff; 2543e99668a5SLiam R. Howlett int error; 2544183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 2545e99668a5SLiam R. Howlett 2546e99668a5SLiam R. Howlett /* Check against address space limit. */ 2547e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { 2548e99668a5SLiam R. Howlett unsigned long nr_pages; 2549e99668a5SLiam R. Howlett 2550e99668a5SLiam R. Howlett /* 2551e99668a5SLiam R. Howlett * MAP_FIXED may remove pages of mappings that intersects with 2552e99668a5SLiam R. Howlett * requested mapping. Account for the pages it would unmap. 2553e99668a5SLiam R. Howlett */ 2554e99668a5SLiam R. Howlett nr_pages = count_vma_pages_range(mm, addr, end); 2555e99668a5SLiam R. Howlett 2556e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, 2557e99668a5SLiam R. Howlett (len >> PAGE_SHIFT) - nr_pages)) 2558e99668a5SLiam R. Howlett return -ENOMEM; 2559e99668a5SLiam R. Howlett } 2560e99668a5SLiam R. Howlett 2561e99668a5SLiam R. Howlett /* Unmap any existing mapping in the area */ 2562183654ceSLiam R. Howlett if (do_vmi_munmap(&vmi, mm, addr, len, uf, false)) 2563e99668a5SLiam R. Howlett return -ENOMEM; 2564e99668a5SLiam R. Howlett 2565e99668a5SLiam R. Howlett /* 2566e99668a5SLiam R. Howlett * Private writable mapping: check memory availability 2567e99668a5SLiam R. Howlett */ 2568e99668a5SLiam R. Howlett if (accountable_mapping(file, vm_flags)) { 2569e99668a5SLiam R. Howlett charged = len >> PAGE_SHIFT; 2570e99668a5SLiam R. Howlett if (security_vm_enough_memory_mm(mm, charged)) 2571e99668a5SLiam R. Howlett return -ENOMEM; 2572e99668a5SLiam R. Howlett vm_flags |= VM_ACCOUNT; 2573e99668a5SLiam R. Howlett } 2574e99668a5SLiam R. Howlett 2575183654ceSLiam R. Howlett next = vma_next(&vmi); 2576183654ceSLiam R. Howlett prev = vma_prev(&vmi); 2577e99668a5SLiam R. Howlett if (vm_flags & VM_SPECIAL) 2578e99668a5SLiam R. Howlett goto cannot_expand; 2579e99668a5SLiam R. Howlett 2580e99668a5SLiam R. Howlett /* Attempt to expand an old mapping */ 2581e99668a5SLiam R. Howlett /* Check next */ 2582e99668a5SLiam R. Howlett if (next && next->vm_start == end && !vma_policy(next) && 2583e99668a5SLiam R. Howlett can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, 2584e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL)) { 2585e99668a5SLiam R. Howlett merge_end = next->vm_end; 2586e99668a5SLiam R. Howlett vma = next; 2587e99668a5SLiam R. Howlett vm_pgoff = next->vm_pgoff - pglen; 2588e99668a5SLiam R. Howlett } 2589e99668a5SLiam R. Howlett 2590e99668a5SLiam R. Howlett /* Check prev */ 2591e99668a5SLiam R. Howlett if (prev && prev->vm_end == addr && !vma_policy(prev) && 2592e99668a5SLiam R. Howlett (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, 2593e99668a5SLiam R. Howlett pgoff, vma->vm_userfaultfd_ctx, NULL) : 2594e99668a5SLiam R. Howlett can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, 2595e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL))) { 2596e99668a5SLiam R. Howlett merge_start = prev->vm_start; 2597e99668a5SLiam R. Howlett vma = prev; 2598e99668a5SLiam R. Howlett vm_pgoff = prev->vm_pgoff; 2599e99668a5SLiam R. Howlett } 2600e99668a5SLiam R. Howlett 2601e99668a5SLiam R. Howlett 2602e99668a5SLiam R. Howlett /* Actually expand, if possible */ 2603e99668a5SLiam R. Howlett if (vma && 26043c441ab7SLiam R. Howlett !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { 2605e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vm_flags); 2606e99668a5SLiam R. Howlett goto expanded; 2607e99668a5SLiam R. Howlett } 2608e99668a5SLiam R. Howlett 2609e99668a5SLiam R. Howlett cannot_expand: 26105c1c03deSLiam R. Howlett if (prev) 26115c1c03deSLiam R. Howlett vma_iter_next_range(&vmi); 26125c1c03deSLiam R. Howlett 2613e99668a5SLiam R. Howlett /* 2614e99668a5SLiam R. Howlett * Determine the object being mapped and call the appropriate 2615e99668a5SLiam R. Howlett * specific mapper. the address has already been validated, but 2616e99668a5SLiam R. Howlett * not unmapped, but the maps are removed from the list. 2617e99668a5SLiam R. Howlett */ 2618e99668a5SLiam R. Howlett vma = vm_area_alloc(mm); 2619e99668a5SLiam R. Howlett if (!vma) { 2620e99668a5SLiam R. Howlett error = -ENOMEM; 2621e99668a5SLiam R. Howlett goto unacct_error; 2622e99668a5SLiam R. Howlett } 2623e99668a5SLiam R. Howlett 26240fd5a9e2SLiam R. Howlett vma_iter_set(&vmi, addr); 2625e99668a5SLiam R. Howlett vma->vm_start = addr; 2626e99668a5SLiam R. Howlett vma->vm_end = end; 26271c71222eSSuren Baghdasaryan vm_flags_init(vma, vm_flags); 2628e99668a5SLiam R. Howlett vma->vm_page_prot = vm_get_page_prot(vm_flags); 2629e99668a5SLiam R. Howlett vma->vm_pgoff = pgoff; 2630e99668a5SLiam R. Howlett 2631e99668a5SLiam R. Howlett if (file) { 2632e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) { 2633e99668a5SLiam R. Howlett error = mapping_map_writable(file->f_mapping); 2634e99668a5SLiam R. Howlett if (error) 2635e99668a5SLiam R. Howlett goto free_vma; 2636e99668a5SLiam R. Howlett } 2637e99668a5SLiam R. Howlett 2638e99668a5SLiam R. Howlett vma->vm_file = get_file(file); 2639e99668a5SLiam R. Howlett error = call_mmap(file, vma); 2640e99668a5SLiam R. Howlett if (error) 2641e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2642e99668a5SLiam R. Howlett 2643a57b7051SLiam Howlett /* 2644a57b7051SLiam Howlett * Expansion is handled above, merging is handled below. 2645a57b7051SLiam Howlett * Drivers should not alter the address of the VMA. 2646e99668a5SLiam R. Howlett */ 2647a57b7051SLiam Howlett error = -EINVAL; 2648cc8d1b09SLiam R. Howlett if (WARN_ON((addr != vma->vm_start))) 2649a57b7051SLiam Howlett goto close_and_free_vma; 2650e99668a5SLiam R. Howlett 2651cc8d1b09SLiam R. Howlett vma_iter_set(&vmi, addr); 2652e99668a5SLiam R. Howlett /* 2653e99668a5SLiam R. Howlett * If vm_flags changed after call_mmap(), we should try merge 2654e99668a5SLiam R. Howlett * vma again as we may succeed this time. 2655e99668a5SLiam R. Howlett */ 2656e99668a5SLiam R. Howlett if (unlikely(vm_flags != vma->vm_flags && prev)) { 26579760ebffSLiam R. Howlett merge = vma_merge(&vmi, mm, prev, vma->vm_start, 26589760ebffSLiam R. Howlett vma->vm_end, vma->vm_flags, NULL, 26599760ebffSLiam R. Howlett vma->vm_file, vma->vm_pgoff, NULL, 26609760ebffSLiam R. Howlett NULL_VM_UFFD_CTX, NULL); 2661e99668a5SLiam R. Howlett if (merge) { 2662e99668a5SLiam R. Howlett /* 2663e99668a5SLiam R. Howlett * ->mmap() can change vma->vm_file and fput 2664e99668a5SLiam R. Howlett * the original file. So fput the vma->vm_file 2665e99668a5SLiam R. Howlett * here or we would add an extra fput for file 2666e99668a5SLiam R. Howlett * and cause general protection fault 2667e99668a5SLiam R. Howlett * ultimately. 2668e99668a5SLiam R. Howlett */ 2669e99668a5SLiam R. Howlett fput(vma->vm_file); 2670e99668a5SLiam R. Howlett vm_area_free(vma); 2671e99668a5SLiam R. Howlett vma = merge; 2672e99668a5SLiam R. Howlett /* Update vm_flags to pick up the change. */ 2673e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2674e99668a5SLiam R. Howlett goto unmap_writable; 2675e99668a5SLiam R. Howlett } 2676e99668a5SLiam R. Howlett } 2677e99668a5SLiam R. Howlett 2678e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2679e99668a5SLiam R. Howlett } else if (vm_flags & VM_SHARED) { 2680e99668a5SLiam R. Howlett error = shmem_zero_setup(vma); 2681e99668a5SLiam R. Howlett if (error) 2682e99668a5SLiam R. Howlett goto free_vma; 2683e99668a5SLiam R. Howlett } else { 2684e99668a5SLiam R. Howlett vma_set_anonymous(vma); 2685e99668a5SLiam R. Howlett } 2686e99668a5SLiam R. Howlett 2687b507808eSJoey Gouly if (map_deny_write_exec(vma, vma->vm_flags)) { 2688b507808eSJoey Gouly error = -EACCES; 2689b507808eSJoey Gouly goto close_and_free_vma; 2690b507808eSJoey Gouly } 2691b507808eSJoey Gouly 2692e99668a5SLiam R. Howlett /* Allow architectures to sanity-check the vm_flags */ 2693e99668a5SLiam R. Howlett error = -EINVAL; 2694cc8d1b09SLiam R. Howlett if (!arch_validate_flags(vma->vm_flags)) 2695deb0f656SCarlos Llamas goto close_and_free_vma; 2696e99668a5SLiam R. Howlett 2697e99668a5SLiam R. Howlett error = -ENOMEM; 2698cc8d1b09SLiam R. Howlett if (vma_iter_prealloc(&vmi)) 26995789151eSMike Kravetz goto close_and_free_vma; 2700e99668a5SLiam R. Howlett 2701e99668a5SLiam R. Howlett if (vma->vm_file) 2702e99668a5SLiam R. Howlett i_mmap_lock_write(vma->vm_file->f_mapping); 2703e99668a5SLiam R. Howlett 2704183654ceSLiam R. Howlett vma_iter_store(&vmi, vma); 2705e99668a5SLiam R. Howlett mm->map_count++; 2706e99668a5SLiam R. Howlett if (vma->vm_file) { 2707e99668a5SLiam R. Howlett if (vma->vm_flags & VM_SHARED) 2708e99668a5SLiam R. Howlett mapping_allow_writable(vma->vm_file->f_mapping); 2709e99668a5SLiam R. Howlett 2710e99668a5SLiam R. Howlett flush_dcache_mmap_lock(vma->vm_file->f_mapping); 2711e99668a5SLiam R. Howlett vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); 2712e99668a5SLiam R. Howlett flush_dcache_mmap_unlock(vma->vm_file->f_mapping); 2713e99668a5SLiam R. Howlett i_mmap_unlock_write(vma->vm_file->f_mapping); 2714e99668a5SLiam R. Howlett } 2715e99668a5SLiam R. Howlett 2716e99668a5SLiam R. Howlett /* 2717e99668a5SLiam R. Howlett * vma_merge() calls khugepaged_enter_vma() either, the below 2718e99668a5SLiam R. Howlett * call covers the non-merge case. 2719e99668a5SLiam R. Howlett */ 2720e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vma->vm_flags); 2721e99668a5SLiam R. Howlett 2722e99668a5SLiam R. Howlett /* Once vma denies write, undo our temporary denial count */ 2723e99668a5SLiam R. Howlett unmap_writable: 2724e99668a5SLiam R. Howlett if (file && vm_flags & VM_SHARED) 2725e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2726e99668a5SLiam R. Howlett file = vma->vm_file; 2727d7597f59SStefan Roesch ksm_add_vma(vma); 2728e99668a5SLiam R. Howlett expanded: 2729e99668a5SLiam R. Howlett perf_event_mmap(vma); 2730e99668a5SLiam R. Howlett 2731e99668a5SLiam R. Howlett vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); 2732e99668a5SLiam R. Howlett if (vm_flags & VM_LOCKED) { 2733e99668a5SLiam R. Howlett if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || 2734e99668a5SLiam R. Howlett is_vm_hugetlb_page(vma) || 2735e99668a5SLiam R. Howlett vma == get_gate_vma(current->mm)) 2736e430a95aSSuren Baghdasaryan vm_flags_clear(vma, VM_LOCKED_MASK); 2737e99668a5SLiam R. Howlett else 2738e99668a5SLiam R. Howlett mm->locked_vm += (len >> PAGE_SHIFT); 2739e99668a5SLiam R. Howlett } 2740e99668a5SLiam R. Howlett 2741e99668a5SLiam R. Howlett if (file) 2742e99668a5SLiam R. Howlett uprobe_mmap(vma); 2743e99668a5SLiam R. Howlett 2744e99668a5SLiam R. Howlett /* 2745e99668a5SLiam R. Howlett * New (or expanded) vma always get soft dirty status. 2746e99668a5SLiam R. Howlett * Otherwise user-space soft-dirty page tracker won't 2747e99668a5SLiam R. Howlett * be able to distinguish situation when vma area unmapped, 2748e99668a5SLiam R. Howlett * then new mapped in-place (which must be aimed as 2749e99668a5SLiam R. Howlett * a completely new data area). 2750e99668a5SLiam R. Howlett */ 27511c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_SOFTDIRTY); 2752e99668a5SLiam R. Howlett 2753e99668a5SLiam R. Howlett vma_set_page_prot(vma); 2754e99668a5SLiam R. Howlett 2755e99668a5SLiam R. Howlett validate_mm(mm); 2756e99668a5SLiam R. Howlett return addr; 2757e99668a5SLiam R. Howlett 2758deb0f656SCarlos Llamas close_and_free_vma: 2759cc8d1b09SLiam R. Howlett if (file && vma->vm_ops && vma->vm_ops->close) 2760deb0f656SCarlos Llamas vma->vm_ops->close(vma); 2761cc8d1b09SLiam R. Howlett 2762cc8d1b09SLiam R. Howlett if (file || vma->vm_file) { 2763e99668a5SLiam R. Howlett unmap_and_free_vma: 2764e99668a5SLiam R. Howlett fput(vma->vm_file); 2765e99668a5SLiam R. Howlett vma->vm_file = NULL; 2766e99668a5SLiam R. Howlett 2767e99668a5SLiam R. Howlett /* Undo any partial mapping done by a device driver. */ 2768cc8d1b09SLiam R. Howlett unmap_region(mm, &mm->mm_mt, vma, prev, next, vma->vm_start, 276968f48381SSuren Baghdasaryan vma->vm_end, true); 2770cc8d1b09SLiam R. Howlett } 2771cc674ab3SLi Zetao if (file && (vm_flags & VM_SHARED)) 2772e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2773e99668a5SLiam R. Howlett free_vma: 2774e99668a5SLiam R. Howlett vm_area_free(vma); 2775e99668a5SLiam R. Howlett unacct_error: 2776e99668a5SLiam R. Howlett if (charged) 2777e99668a5SLiam R. Howlett vm_unacct_memory(charged); 2778e99668a5SLiam R. Howlett validate_mm(mm); 2779e99668a5SLiam R. Howlett return error; 2780e99668a5SLiam R. Howlett } 2781e99668a5SLiam R. Howlett 2782dd2283f2SYang Shi static int __vm_munmap(unsigned long start, size_t len, bool downgrade) 2783a46ef99dSLinus Torvalds { 2784a46ef99dSLinus Torvalds int ret; 2785bfce281cSAl Viro struct mm_struct *mm = current->mm; 2786897ab3e0SMike Rapoport LIST_HEAD(uf); 2787183654ceSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 2788a46ef99dSLinus Torvalds 2789d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2790ae798783SMichal Hocko return -EINTR; 2791ae798783SMichal Hocko 2792183654ceSLiam R. Howlett ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade); 2793dd2283f2SYang Shi /* 2794c1e8d7c6SMichel Lespinasse * Returning 1 indicates mmap_lock is downgraded. 2795dd2283f2SYang Shi * But 1 is not legal return value of vm_munmap() and munmap(), reset 2796dd2283f2SYang Shi * it to 0 before return. 2797dd2283f2SYang Shi */ 2798dd2283f2SYang Shi if (ret == 1) { 2799d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2800dd2283f2SYang Shi ret = 0; 2801dd2283f2SYang Shi } else 2802d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2803dd2283f2SYang Shi 2804897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 2805a46ef99dSLinus Torvalds return ret; 2806a46ef99dSLinus Torvalds } 2807dd2283f2SYang Shi 2808dd2283f2SYang Shi int vm_munmap(unsigned long start, size_t len) 2809dd2283f2SYang Shi { 2810dd2283f2SYang Shi return __vm_munmap(start, len, false); 2811dd2283f2SYang Shi } 2812a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2813a46ef99dSLinus Torvalds 28146a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 28151da177e4SLinus Torvalds { 2816ce18d171SCatalin Marinas addr = untagged_addr(addr); 2817dd2283f2SYang Shi return __vm_munmap(addr, len, true); 28181da177e4SLinus Torvalds } 28191da177e4SLinus Torvalds 2820c8d78c18SKirill A. Shutemov 2821c8d78c18SKirill A. Shutemov /* 2822c8d78c18SKirill A. Shutemov * Emulation of deprecated remap_file_pages() syscall. 2823c8d78c18SKirill A. Shutemov */ 2824c8d78c18SKirill A. Shutemov SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, 2825c8d78c18SKirill A. Shutemov unsigned long, prot, unsigned long, pgoff, unsigned long, flags) 2826c8d78c18SKirill A. Shutemov { 2827c8d78c18SKirill A. Shutemov 2828c8d78c18SKirill A. Shutemov struct mm_struct *mm = current->mm; 2829c8d78c18SKirill A. Shutemov struct vm_area_struct *vma; 2830c8d78c18SKirill A. Shutemov unsigned long populate = 0; 2831c8d78c18SKirill A. Shutemov unsigned long ret = -EINVAL; 2832c8d78c18SKirill A. Shutemov struct file *file; 2833c8d78c18SKirill A. Shutemov 2834ee65728eSMike Rapoport pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", 2835c8d78c18SKirill A. Shutemov current->comm, current->pid); 2836c8d78c18SKirill A. Shutemov 2837c8d78c18SKirill A. Shutemov if (prot) 2838c8d78c18SKirill A. Shutemov return ret; 2839c8d78c18SKirill A. Shutemov start = start & PAGE_MASK; 2840c8d78c18SKirill A. Shutemov size = size & PAGE_MASK; 2841c8d78c18SKirill A. Shutemov 2842c8d78c18SKirill A. Shutemov if (start + size <= start) 2843c8d78c18SKirill A. Shutemov return ret; 2844c8d78c18SKirill A. Shutemov 2845c8d78c18SKirill A. Shutemov /* Does pgoff wrap? */ 2846c8d78c18SKirill A. Shutemov if (pgoff + (size >> PAGE_SHIFT) < pgoff) 2847c8d78c18SKirill A. Shutemov return ret; 2848c8d78c18SKirill A. Shutemov 2849d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2850dc0ef0dfSMichal Hocko return -EINTR; 2851dc0ef0dfSMichal Hocko 28529b593cb2SLiam R. Howlett vma = vma_lookup(mm, start); 2853c8d78c18SKirill A. Shutemov 2854c8d78c18SKirill A. Shutemov if (!vma || !(vma->vm_flags & VM_SHARED)) 2855c8d78c18SKirill A. Shutemov goto out; 2856c8d78c18SKirill A. Shutemov 285748f7df32SKirill A. Shutemov if (start + size > vma->vm_end) { 2858763ecb03SLiam R. Howlett VMA_ITERATOR(vmi, mm, vma->vm_end); 2859763ecb03SLiam R. Howlett struct vm_area_struct *next, *prev = vma; 286048f7df32SKirill A. Shutemov 2861763ecb03SLiam R. Howlett for_each_vma_range(vmi, next, start + size) { 286248f7df32SKirill A. Shutemov /* hole between vmas ? */ 2863763ecb03SLiam R. Howlett if (next->vm_start != prev->vm_end) 286448f7df32SKirill A. Shutemov goto out; 286548f7df32SKirill A. Shutemov 286648f7df32SKirill A. Shutemov if (next->vm_file != vma->vm_file) 286748f7df32SKirill A. Shutemov goto out; 286848f7df32SKirill A. Shutemov 286948f7df32SKirill A. Shutemov if (next->vm_flags != vma->vm_flags) 287048f7df32SKirill A. Shutemov goto out; 287148f7df32SKirill A. Shutemov 28721db43d3fSLiam Howlett if (start + size <= next->vm_end) 28731db43d3fSLiam Howlett break; 28741db43d3fSLiam Howlett 2875763ecb03SLiam R. Howlett prev = next; 287648f7df32SKirill A. Shutemov } 287748f7df32SKirill A. Shutemov 287848f7df32SKirill A. Shutemov if (!next) 2879c8d78c18SKirill A. Shutemov goto out; 2880c8d78c18SKirill A. Shutemov } 2881c8d78c18SKirill A. Shutemov 2882c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; 2883c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; 2884c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; 2885c8d78c18SKirill A. Shutemov 2886c8d78c18SKirill A. Shutemov flags &= MAP_NONBLOCK; 2887c8d78c18SKirill A. Shutemov flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; 2888fce000b1SLiam Howlett if (vma->vm_flags & VM_LOCKED) 2889c8d78c18SKirill A. Shutemov flags |= MAP_LOCKED; 289048f7df32SKirill A. Shutemov 2891c8d78c18SKirill A. Shutemov file = get_file(vma->vm_file); 289245e55300SPeter Collingbourne ret = do_mmap(vma->vm_file, start, size, 2893897ab3e0SMike Rapoport prot, flags, pgoff, &populate, NULL); 2894c8d78c18SKirill A. Shutemov fput(file); 2895c8d78c18SKirill A. Shutemov out: 2896d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2897c8d78c18SKirill A. Shutemov if (populate) 2898c8d78c18SKirill A. Shutemov mm_populate(ret, populate); 2899c8d78c18SKirill A. Shutemov if (!IS_ERR_VALUE(ret)) 2900c8d78c18SKirill A. Shutemov ret = 0; 2901c8d78c18SKirill A. Shutemov return ret; 2902c8d78c18SKirill A. Shutemov } 2903c8d78c18SKirill A. Shutemov 29041da177e4SLinus Torvalds /* 290527b26701SLiam R. Howlett * do_vma_munmap() - Unmap a full or partial vma. 290627b26701SLiam R. Howlett * @vmi: The vma iterator pointing at the vma 290727b26701SLiam R. Howlett * @vma: The first vma to be munmapped 290827b26701SLiam R. Howlett * @start: the start of the address to unmap 290927b26701SLiam R. Howlett * @end: The end of the address to unmap 29102e7ce7d3SLiam R. Howlett * @uf: The userfaultfd list_head 291127b26701SLiam R. Howlett * @downgrade: Attempt to downgrade or not 29122e7ce7d3SLiam R. Howlett * 291327b26701SLiam R. Howlett * Returns: 0 on success and not downgraded, 1 on success and downgraded. 291427b26701SLiam R. Howlett * unmaps a VMA mapping when the vma iterator is already in position. 291527b26701SLiam R. Howlett * Does not handle alignment. 29161da177e4SLinus Torvalds */ 291727b26701SLiam R. Howlett int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 291827b26701SLiam R. Howlett unsigned long start, unsigned long end, 291927b26701SLiam R. Howlett struct list_head *uf, bool downgrade) 29202e7ce7d3SLiam R. Howlett { 29212e7ce7d3SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 29222e7ce7d3SLiam R. Howlett int ret; 29232e7ce7d3SLiam R. Howlett 292427b26701SLiam R. Howlett arch_unmap(mm, start, end); 292527b26701SLiam R. Howlett ret = do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); 2926b50e195fSLiam R. Howlett validate_mm(mm); 29272e7ce7d3SLiam R. Howlett return ret; 29282e7ce7d3SLiam R. Howlett } 29292e7ce7d3SLiam R. Howlett 29302e7ce7d3SLiam R. Howlett /* 29312e7ce7d3SLiam R. Howlett * do_brk_flags() - Increase the brk vma if the flags match. 293292fed820SLiam R. Howlett * @vmi: The vma iterator 29332e7ce7d3SLiam R. Howlett * @addr: The start address 29342e7ce7d3SLiam R. Howlett * @len: The length of the increase 29352e7ce7d3SLiam R. Howlett * @vma: The vma, 29362e7ce7d3SLiam R. Howlett * @flags: The VMA Flags 29372e7ce7d3SLiam R. Howlett * 29382e7ce7d3SLiam R. Howlett * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags 29392e7ce7d3SLiam R. Howlett * do not match then create a new anonymous VMA. Eventually we may be able to 29402e7ce7d3SLiam R. Howlett * do some brk-specific accounting here. 29412e7ce7d3SLiam R. Howlett */ 294292fed820SLiam R. Howlett static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, 2943763ecb03SLiam R. Howlett unsigned long addr, unsigned long len, unsigned long flags) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 2946287051b1SLiam R. Howlett struct vma_prepare vp; 29472e7ce7d3SLiam R. Howlett 2948b50e195fSLiam R. Howlett validate_mm(mm); 29492e7ce7d3SLiam R. Howlett /* 29502e7ce7d3SLiam R. Howlett * Check against address space limits by the changed size 29512e7ce7d3SLiam R. Howlett * Note: This happens *after* clearing old mappings in some code paths. 29522e7ce7d3SLiam R. Howlett */ 295316e72e9bSDenys Vlasenko flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 295484638335SKonstantin Khlebnikov if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) 29551da177e4SLinus Torvalds return -ENOMEM; 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 29581da177e4SLinus Torvalds return -ENOMEM; 29591da177e4SLinus Torvalds 2960191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 29611da177e4SLinus Torvalds return -ENOMEM; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds /* 29642e7ce7d3SLiam R. Howlett * Expand the existing vma if possible; Note that singular lists do not 29652e7ce7d3SLiam R. Howlett * occur after forking, so the expand will only happen on new VMAs. 29661da177e4SLinus Torvalds */ 29676c28ca64SLiam Howlett if (vma && vma->vm_end == addr && !vma_policy(vma) && 29686c28ca64SLiam Howlett can_vma_merge_after(vma, flags, NULL, NULL, 29696c28ca64SLiam Howlett addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) { 297092fed820SLiam R. Howlett if (vma_iter_prealloc(vmi)) 2971675eaca1SAlistair Popple goto unacct_fail; 297228c5609fSLiam Howlett 2973287051b1SLiam R. Howlett init_vma_prep(&vp, vma); 2974287051b1SLiam R. Howlett vma_prepare(&vp); 2975ccf1d78dSSuren Baghdasaryan vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0); 29762e7ce7d3SLiam R. Howlett vma->vm_end = addr + len; 29771c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_SOFTDIRTY); 297892fed820SLiam R. Howlett vma_iter_store(vmi, vma); 29792e7ce7d3SLiam R. Howlett 2980287051b1SLiam R. Howlett vma_complete(&vp, vmi, mm); 29812e7ce7d3SLiam R. Howlett khugepaged_enter_vma(vma, flags); 29822e7ce7d3SLiam R. Howlett goto out; 29832e7ce7d3SLiam R. Howlett } 29842e7ce7d3SLiam R. Howlett 29852e7ce7d3SLiam R. Howlett /* create a vma struct for an anonymous mapping */ 29862e7ce7d3SLiam R. Howlett vma = vm_area_alloc(mm); 29872e7ce7d3SLiam R. Howlett if (!vma) 2988675eaca1SAlistair Popple goto unacct_fail; 29891da177e4SLinus Torvalds 2990bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 29911da177e4SLinus Torvalds vma->vm_start = addr; 29921da177e4SLinus Torvalds vma->vm_end = addr + len; 29932e7ce7d3SLiam R. Howlett vma->vm_pgoff = addr >> PAGE_SHIFT; 29941c71222eSSuren Baghdasaryan vm_flags_init(vma, flags); 29953ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 299692fed820SLiam R. Howlett if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) 29972e7ce7d3SLiam R. Howlett goto mas_store_fail; 2998d4af56c5SLiam R. Howlett 29992e7ce7d3SLiam R. Howlett mm->map_count++; 3000d7597f59SStefan Roesch ksm_add_vma(vma); 30011da177e4SLinus Torvalds out: 30023af9e859SEric B Munson perf_event_mmap(vma); 30031da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 300484638335SKonstantin Khlebnikov mm->data_vm += len >> PAGE_SHIFT; 3005128557ffSMichel Lespinasse if (flags & VM_LOCKED) 3006ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 30071c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_SOFTDIRTY); 3008763ecb03SLiam R. Howlett validate_mm(mm); 30095d22fc25SLinus Torvalds return 0; 3010d4af56c5SLiam R. Howlett 30112e7ce7d3SLiam R. Howlett mas_store_fail: 3012d4af56c5SLiam R. Howlett vm_area_free(vma); 3013675eaca1SAlistair Popple unacct_fail: 30142e7ce7d3SLiam R. Howlett vm_unacct_memory(len >> PAGE_SHIFT); 30152e7ce7d3SLiam R. Howlett return -ENOMEM; 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds 3018bb177a73SMichal Hocko int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) 3019e4eb1ff6SLinus Torvalds { 3020e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 30212e7ce7d3SLiam R. Howlett struct vm_area_struct *vma = NULL; 3022bb177a73SMichal Hocko unsigned long len; 30235d22fc25SLinus Torvalds int ret; 3024128557ffSMichel Lespinasse bool populate; 3025897ab3e0SMike Rapoport LIST_HEAD(uf); 302692fed820SLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 3027e4eb1ff6SLinus Torvalds 3028bb177a73SMichal Hocko len = PAGE_ALIGN(request); 3029bb177a73SMichal Hocko if (len < request) 3030bb177a73SMichal Hocko return -ENOMEM; 3031bb177a73SMichal Hocko if (!len) 3032bb177a73SMichal Hocko return 0; 3033bb177a73SMichal Hocko 3034d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 30352d6c9282SMichal Hocko return -EINTR; 30362d6c9282SMichal Hocko 30372e7ce7d3SLiam R. Howlett /* Until we need other flags, refuse anything except VM_EXEC. */ 30382e7ce7d3SLiam R. Howlett if ((flags & (~VM_EXEC)) != 0) 30392e7ce7d3SLiam R. Howlett return -EINVAL; 30402e7ce7d3SLiam R. Howlett 30412e7ce7d3SLiam R. Howlett ret = check_brk_limits(addr, len); 30422e7ce7d3SLiam R. Howlett if (ret) 30432e7ce7d3SLiam R. Howlett goto limits_failed; 30442e7ce7d3SLiam R. Howlett 3045183654ceSLiam R. Howlett ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0); 30462e7ce7d3SLiam R. Howlett if (ret) 30472e7ce7d3SLiam R. Howlett goto munmap_failed; 30482e7ce7d3SLiam R. Howlett 304992fed820SLiam R. Howlett vma = vma_prev(&vmi); 305092fed820SLiam R. Howlett ret = do_brk_flags(&vmi, vma, addr, len, flags); 3051128557ffSMichel Lespinasse populate = ((mm->def_flags & VM_LOCKED) != 0); 3052d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3053897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 30545d22fc25SLinus Torvalds if (populate && !ret) 3055128557ffSMichel Lespinasse mm_populate(addr, len); 3056e4eb1ff6SLinus Torvalds return ret; 30572e7ce7d3SLiam R. Howlett 30582e7ce7d3SLiam R. Howlett munmap_failed: 30592e7ce7d3SLiam R. Howlett limits_failed: 30602e7ce7d3SLiam R. Howlett mmap_write_unlock(mm); 30612e7ce7d3SLiam R. Howlett return ret; 3062e4eb1ff6SLinus Torvalds } 306316e72e9bSDenys Vlasenko EXPORT_SYMBOL(vm_brk_flags); 306416e72e9bSDenys Vlasenko 306516e72e9bSDenys Vlasenko int vm_brk(unsigned long addr, unsigned long len) 306616e72e9bSDenys Vlasenko { 306716e72e9bSDenys Vlasenko return vm_brk_flags(addr, len, 0); 306816e72e9bSDenys Vlasenko } 3069e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds /* Release all mmaps. */ 30721da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 30731da177e4SLinus Torvalds { 3074d16dfc55SPeter Zijlstra struct mmu_gather tlb; 3075ba470de4SRik van Riel struct vm_area_struct *vma; 30761da177e4SLinus Torvalds unsigned long nr_accounted = 0; 3077763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 3078763ecb03SLiam R. Howlett int count = 0; 30791da177e4SLinus Torvalds 3080d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 3081cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 3082d6dd61c8SJeremy Fitzhardinge 3083bf3980c8SSuren Baghdasaryan mmap_read_lock(mm); 30849480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 30859480c53eSJeremy Fitzhardinge 3086763ecb03SLiam R. Howlett vma = mas_find(&mas, ULONG_MAX); 308764591e86SSuren Baghdasaryan if (!vma) { 308864591e86SSuren Baghdasaryan /* Can happen if dup_mmap() received an OOM */ 3089bf3980c8SSuren Baghdasaryan mmap_read_unlock(mm); 30909480c53eSJeremy Fitzhardinge return; 309164591e86SSuren Baghdasaryan } 30929480c53eSJeremy Fitzhardinge 30931da177e4SLinus Torvalds lru_add_drain(); 30941da177e4SLinus Torvalds flush_cache_mm(mm); 3095d8b45053SWill Deacon tlb_gather_mmu_fullmm(&tlb, mm); 3096901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 3097763ecb03SLiam R. Howlett /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ 309868f48381SSuren Baghdasaryan unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX, false); 3099bf3980c8SSuren Baghdasaryan mmap_read_unlock(mm); 3100bf3980c8SSuren Baghdasaryan 3101bf3980c8SSuren Baghdasaryan /* 3102bf3980c8SSuren Baghdasaryan * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper 3103b3541d91SSuren Baghdasaryan * because the memory has been already freed. 3104bf3980c8SSuren Baghdasaryan */ 3105bf3980c8SSuren Baghdasaryan set_bit(MMF_OOM_SKIP, &mm->flags); 3106bf3980c8SSuren Baghdasaryan mmap_write_lock(mm); 31073dd44325SLiam R. Howlett mt_clear_in_rcu(&mm->mm_mt); 3108763ecb03SLiam R. Howlett free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, 310998e51a22SSuren Baghdasaryan USER_PGTABLES_CEILING, true); 3110ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 31111da177e4SLinus Torvalds 3112763ecb03SLiam R. Howlett /* 3113763ecb03SLiam R. Howlett * Walk the list again, actually closing and freeing it, with preemption 3114763ecb03SLiam R. Howlett * enabled, without holding any MM locks besides the unreachable 3115763ecb03SLiam R. Howlett * mmap_write_lock. 3116763ecb03SLiam R. Howlett */ 3117763ecb03SLiam R. Howlett do { 31184f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 31194f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 31200d2ebf9cSSuren Baghdasaryan remove_vma(vma, true); 3121763ecb03SLiam R. Howlett count++; 31220a3b3c25SPaul E. McKenney cond_resched(); 3123763ecb03SLiam R. Howlett } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); 3124763ecb03SLiam R. Howlett 3125763ecb03SLiam R. Howlett BUG_ON(count != mm->map_count); 3126d4af56c5SLiam R. Howlett 3127d4af56c5SLiam R. Howlett trace_exit_mmap(mm); 3128d4af56c5SLiam R. Howlett __mt_destroy(&mm->mm_mt); 312964591e86SSuren Baghdasaryan mmap_write_unlock(mm); 31304f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 31341da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 3135c8c06efaSDavidlohr Bueso * then i_mmap_rwsem is taken here. 31361da177e4SLinus Torvalds */ 31371da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 31381da177e4SLinus Torvalds { 3139d4af56c5SLiam R. Howlett unsigned long charged = vma_pages(vma); 31401da177e4SLinus Torvalds 3141d4af56c5SLiam R. Howlett 3142d0601a50SLiam R. Howlett if (find_vma_intersection(mm, vma->vm_start, vma->vm_end)) 3143c9d13f5fSChen Gang return -ENOMEM; 3144d4af56c5SLiam R. Howlett 3145c9d13f5fSChen Gang if ((vma->vm_flags & VM_ACCOUNT) && 3146d4af56c5SLiam R. Howlett security_vm_enough_memory_mm(mm, charged)) 3147c9d13f5fSChen Gang return -ENOMEM; 3148c9d13f5fSChen Gang 31491da177e4SLinus Torvalds /* 31501da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 31511da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 31521da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 31531da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 31541da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 31551da177e4SLinus Torvalds * 31561da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 31571da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 31581da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 31598332326eSLiao Pingfang * Similarly in do_mmap and in do_brk_flags. 31601da177e4SLinus Torvalds */ 31618a9cc3b5SOleg Nesterov if (vma_is_anonymous(vma)) { 31621da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 31631da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 31641da177e4SLinus Torvalds } 31652b144498SSrikar Dronamraju 3166763ecb03SLiam R. Howlett if (vma_link(mm, vma)) { 3167d4af56c5SLiam R. Howlett vm_unacct_memory(charged); 3168d4af56c5SLiam R. Howlett return -ENOMEM; 3169d4af56c5SLiam R. Howlett } 3170d4af56c5SLiam R. Howlett 31711da177e4SLinus Torvalds return 0; 31721da177e4SLinus Torvalds } 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds /* 31751da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 31761da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 31771da177e4SLinus Torvalds */ 31781da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 317938a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 318038a76013SMichel Lespinasse bool *need_rmap_locks) 31811da177e4SLinus Torvalds { 31821da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 31831da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 31841da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 31851da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 3186948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 3187076f16bfSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 31881da177e4SLinus Torvalds 3189b50e195fSLiam R. Howlett validate_mm(mm); 31901da177e4SLinus Torvalds /* 31911da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 31921da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 31931da177e4SLinus Torvalds */ 3194ce75799bSOleg Nesterov if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 31951da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 3196948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 3197948f017bSAndrea Arcangeli } 31981da177e4SLinus Torvalds 3199763ecb03SLiam R. Howlett new_vma = find_vma_prev(mm, addr, &prev); 3200763ecb03SLiam R. Howlett if (new_vma && new_vma->vm_start < addr + len) 32016597d783SHugh Dickins return NULL; /* should never get here */ 3202524e00b3SLiam R. Howlett 32039760ebffSLiam R. Howlett new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags, 320419a809afSAndrea Arcangeli vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), 32055c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 32061da177e4SLinus Torvalds if (new_vma) { 32071da177e4SLinus Torvalds /* 32081da177e4SLinus Torvalds * Source vma may have been merged into new_vma 32091da177e4SLinus Torvalds */ 3210948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 3211948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 3212948f017bSAndrea Arcangeli /* 3213948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 3214948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 3215948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 3216948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 3217948f017bSAndrea Arcangeli * destination address of the mremap to allow 3218948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 3219948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 3220948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 3221948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 3222948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 3223948f017bSAndrea Arcangeli */ 322481d1b09cSSasha Levin VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 322538a76013SMichel Lespinasse *vmap = vma = new_vma; 3226108d6642SMichel Lespinasse } 322738a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 32281da177e4SLinus Torvalds } else { 32293928d4f5SLinus Torvalds new_vma = vm_area_dup(vma); 3230e3975891SChen Gang if (!new_vma) 3231e3975891SChen Gang goto out; 32321da177e4SLinus Torvalds new_vma->vm_start = addr; 32331da177e4SLinus Torvalds new_vma->vm_end = addr + len; 32341da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 3235ef0855d3SOleg Nesterov if (vma_dup_policy(vma, new_vma)) 3236523d4e20SMichel Lespinasse goto out_free_vma; 3237523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 3238523d4e20SMichel Lespinasse goto out_free_mempol; 3239e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 32401da177e4SLinus Torvalds get_file(new_vma->vm_file); 32411da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 32421da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 3243d6ac235dSSuren Baghdasaryan vma_start_write(new_vma); 3244763ecb03SLiam R. Howlett if (vma_link(mm, new_vma)) 3245524e00b3SLiam R. Howlett goto out_vma_link; 324638a76013SMichel Lespinasse *need_rmap_locks = false; 32471da177e4SLinus Torvalds } 3248b50e195fSLiam R. Howlett validate_mm(mm); 32491da177e4SLinus Torvalds return new_vma; 32505beb4930SRik van Riel 3251524e00b3SLiam R. Howlett out_vma_link: 3252524e00b3SLiam R. Howlett if (new_vma->vm_ops && new_vma->vm_ops->close) 3253524e00b3SLiam R. Howlett new_vma->vm_ops->close(new_vma); 325492b73996SLiam Howlett 325592b73996SLiam Howlett if (new_vma->vm_file) 325692b73996SLiam Howlett fput(new_vma->vm_file); 325792b73996SLiam Howlett 325892b73996SLiam Howlett unlink_anon_vmas(new_vma); 32595beb4930SRik van Riel out_free_mempol: 3260ef0855d3SOleg Nesterov mpol_put(vma_policy(new_vma)); 32615beb4930SRik van Riel out_free_vma: 32623928d4f5SLinus Torvalds vm_area_free(new_vma); 3263e3975891SChen Gang out: 3264b50e195fSLiam R. Howlett validate_mm(mm); 32655beb4930SRik van Riel return NULL; 32661da177e4SLinus Torvalds } 3267119f657cSakpm@osdl.org 3268119f657cSakpm@osdl.org /* 3269119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 3270119f657cSakpm@osdl.org * number of pages 3271119f657cSakpm@osdl.org */ 327284638335SKonstantin Khlebnikov bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) 3273119f657cSakpm@osdl.org { 327484638335SKonstantin Khlebnikov if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) 327584638335SKonstantin Khlebnikov return false; 3276119f657cSakpm@osdl.org 3277d977d56cSKonstantin Khlebnikov if (is_data_mapping(flags) && 3278d977d56cSKonstantin Khlebnikov mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { 3279f4fcd558SKonstantin Khlebnikov /* Workaround for Valgrind */ 3280f4fcd558SKonstantin Khlebnikov if (rlimit(RLIMIT_DATA) == 0 && 3281f4fcd558SKonstantin Khlebnikov mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) 3282f4fcd558SKonstantin Khlebnikov return true; 328357a7702bSDavid Woodhouse 328457a7702bSDavid Woodhouse pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", 3285d977d56cSKonstantin Khlebnikov current->comm, current->pid, 3286d977d56cSKonstantin Khlebnikov (mm->data_vm + npages) << PAGE_SHIFT, 328757a7702bSDavid Woodhouse rlimit(RLIMIT_DATA), 328857a7702bSDavid Woodhouse ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); 328957a7702bSDavid Woodhouse 329057a7702bSDavid Woodhouse if (!ignore_rlimit_data) 3291d977d56cSKonstantin Khlebnikov return false; 3292d977d56cSKonstantin Khlebnikov } 3293119f657cSakpm@osdl.org 329484638335SKonstantin Khlebnikov return true; 329584638335SKonstantin Khlebnikov } 329684638335SKonstantin Khlebnikov 329784638335SKonstantin Khlebnikov void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) 329884638335SKonstantin Khlebnikov { 32997866076bSPeng Liu WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); 330084638335SKonstantin Khlebnikov 3301d977d56cSKonstantin Khlebnikov if (is_exec_mapping(flags)) 330284638335SKonstantin Khlebnikov mm->exec_vm += npages; 3303d977d56cSKonstantin Khlebnikov else if (is_stack_mapping(flags)) 330484638335SKonstantin Khlebnikov mm->stack_vm += npages; 3305d977d56cSKonstantin Khlebnikov else if (is_data_mapping(flags)) 330684638335SKonstantin Khlebnikov mm->data_vm += npages; 3307119f657cSakpm@osdl.org } 3308fa5dc22fSRoland McGrath 3309b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf); 3310a62c34bdSAndy Lutomirski 3311a62c34bdSAndy Lutomirski /* 3312a62c34bdSAndy Lutomirski * Having a close hook prevents vma merging regardless of flags. 3313a62c34bdSAndy Lutomirski */ 3314a62c34bdSAndy Lutomirski static void special_mapping_close(struct vm_area_struct *vma) 3315a62c34bdSAndy Lutomirski { 3316a62c34bdSAndy Lutomirski } 3317a62c34bdSAndy Lutomirski 3318a62c34bdSAndy Lutomirski static const char *special_mapping_name(struct vm_area_struct *vma) 3319a62c34bdSAndy Lutomirski { 3320a62c34bdSAndy Lutomirski return ((struct vm_special_mapping *)vma->vm_private_data)->name; 3321a62c34bdSAndy Lutomirski } 3322a62c34bdSAndy Lutomirski 332314d07113SBrian Geffon static int special_mapping_mremap(struct vm_area_struct *new_vma) 3324b059a453SDmitry Safonov { 3325b059a453SDmitry Safonov struct vm_special_mapping *sm = new_vma->vm_private_data; 3326b059a453SDmitry Safonov 3327280e87e9SDmitry Safonov if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) 3328280e87e9SDmitry Safonov return -EFAULT; 3329280e87e9SDmitry Safonov 3330b059a453SDmitry Safonov if (sm->mremap) 3331b059a453SDmitry Safonov return sm->mremap(sm, new_vma); 3332280e87e9SDmitry Safonov 3333b059a453SDmitry Safonov return 0; 3334b059a453SDmitry Safonov } 3335b059a453SDmitry Safonov 3336871402e0SDmitry Safonov static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) 3337871402e0SDmitry Safonov { 3338871402e0SDmitry Safonov /* 3339871402e0SDmitry Safonov * Forbid splitting special mappings - kernel has expectations over 3340871402e0SDmitry Safonov * the number of pages in mapping. Together with VM_DONTEXPAND 3341871402e0SDmitry Safonov * the size of vma should stay the same over the special mapping's 3342871402e0SDmitry Safonov * lifetime. 3343871402e0SDmitry Safonov */ 3344871402e0SDmitry Safonov return -EINVAL; 3345871402e0SDmitry Safonov } 3346871402e0SDmitry Safonov 3347a62c34bdSAndy Lutomirski static const struct vm_operations_struct special_mapping_vmops = { 3348a62c34bdSAndy Lutomirski .close = special_mapping_close, 3349a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3350b059a453SDmitry Safonov .mremap = special_mapping_mremap, 3351a62c34bdSAndy Lutomirski .name = special_mapping_name, 3352af34ebebSDmitry Safonov /* vDSO code relies that VVAR can't be accessed remotely */ 3353af34ebebSDmitry Safonov .access = NULL, 3354871402e0SDmitry Safonov .may_split = special_mapping_split, 3355a62c34bdSAndy Lutomirski }; 3356a62c34bdSAndy Lutomirski 3357a62c34bdSAndy Lutomirski static const struct vm_operations_struct legacy_special_mapping_vmops = { 3358a62c34bdSAndy Lutomirski .close = special_mapping_close, 3359a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3360a62c34bdSAndy Lutomirski }; 3361fa5dc22fSRoland McGrath 3362b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf) 3363fa5dc22fSRoland McGrath { 336411bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 3365b1d0e4f5SNick Piggin pgoff_t pgoff; 3366fa5dc22fSRoland McGrath struct page **pages; 3367fa5dc22fSRoland McGrath 3368f872f540SAndy Lutomirski if (vma->vm_ops == &legacy_special_mapping_vmops) { 3369a62c34bdSAndy Lutomirski pages = vma->vm_private_data; 3370f872f540SAndy Lutomirski } else { 3371f872f540SAndy Lutomirski struct vm_special_mapping *sm = vma->vm_private_data; 3372f872f540SAndy Lutomirski 3373f872f540SAndy Lutomirski if (sm->fault) 337411bac800SDave Jiang return sm->fault(sm, vmf->vma, vmf); 3375f872f540SAndy Lutomirski 3376f872f540SAndy Lutomirski pages = sm->pages; 3377f872f540SAndy Lutomirski } 3378a62c34bdSAndy Lutomirski 33798a9cc3b5SOleg Nesterov for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) 3380b1d0e4f5SNick Piggin pgoff--; 3381fa5dc22fSRoland McGrath 3382fa5dc22fSRoland McGrath if (*pages) { 3383fa5dc22fSRoland McGrath struct page *page = *pages; 3384fa5dc22fSRoland McGrath get_page(page); 3385b1d0e4f5SNick Piggin vmf->page = page; 3386b1d0e4f5SNick Piggin return 0; 3387fa5dc22fSRoland McGrath } 3388fa5dc22fSRoland McGrath 3389b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 3390fa5dc22fSRoland McGrath } 3391fa5dc22fSRoland McGrath 3392a62c34bdSAndy Lutomirski static struct vm_area_struct *__install_special_mapping( 3393a62c34bdSAndy Lutomirski struct mm_struct *mm, 3394fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 339527f28b97SChen Gang unsigned long vm_flags, void *priv, 339627f28b97SChen Gang const struct vm_operations_struct *ops) 3397fa5dc22fSRoland McGrath { 3398462e635eSTavis Ormandy int ret; 3399fa5dc22fSRoland McGrath struct vm_area_struct *vma; 3400fa5dc22fSRoland McGrath 3401b50e195fSLiam R. Howlett validate_mm(mm); 3402490fc053SLinus Torvalds vma = vm_area_alloc(mm); 3403fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 34043935ed6aSStefani Seibold return ERR_PTR(-ENOMEM); 3405fa5dc22fSRoland McGrath 3406fa5dc22fSRoland McGrath vma->vm_start = addr; 3407fa5dc22fSRoland McGrath vma->vm_end = addr + len; 3408fa5dc22fSRoland McGrath 3409e430a95aSSuren Baghdasaryan vm_flags_init(vma, (vm_flags | mm->def_flags | 3410e430a95aSSuren Baghdasaryan VM_DONTEXPAND | VM_SOFTDIRTY) & ~VM_LOCKED_MASK); 34113ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 3412fa5dc22fSRoland McGrath 3413a62c34bdSAndy Lutomirski vma->vm_ops = ops; 3414a62c34bdSAndy Lutomirski vma->vm_private_data = priv; 3415fa5dc22fSRoland McGrath 3416462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 3417462e635eSTavis Ormandy if (ret) 3418462e635eSTavis Ormandy goto out; 3419fa5dc22fSRoland McGrath 342084638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); 3421fa5dc22fSRoland McGrath 3422cdd6c482SIngo Molnar perf_event_mmap(vma); 3423089dd79dSPeter Zijlstra 3424b50e195fSLiam R. Howlett validate_mm(mm); 34253935ed6aSStefani Seibold return vma; 3426462e635eSTavis Ormandy 3427462e635eSTavis Ormandy out: 34283928d4f5SLinus Torvalds vm_area_free(vma); 3429b50e195fSLiam R. Howlett validate_mm(mm); 34303935ed6aSStefani Seibold return ERR_PTR(ret); 34313935ed6aSStefani Seibold } 34323935ed6aSStefani Seibold 34332eefd878SDmitry Safonov bool vma_is_special_mapping(const struct vm_area_struct *vma, 34342eefd878SDmitry Safonov const struct vm_special_mapping *sm) 34352eefd878SDmitry Safonov { 34362eefd878SDmitry Safonov return vma->vm_private_data == sm && 34372eefd878SDmitry Safonov (vma->vm_ops == &special_mapping_vmops || 34382eefd878SDmitry Safonov vma->vm_ops == &legacy_special_mapping_vmops); 34392eefd878SDmitry Safonov } 34402eefd878SDmitry Safonov 3441a62c34bdSAndy Lutomirski /* 3442c1e8d7c6SMichel Lespinasse * Called with mm->mmap_lock held for writing. 3443a62c34bdSAndy Lutomirski * Insert a new vma covering the given region, with the given flags. 3444a62c34bdSAndy Lutomirski * Its pages are supplied by the given array of struct page *. 3445a62c34bdSAndy Lutomirski * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 3446a62c34bdSAndy Lutomirski * The region past the last page supplied will always produce SIGBUS. 3447a62c34bdSAndy Lutomirski * The array pointer and the pages it points to are assumed to stay alive 3448a62c34bdSAndy Lutomirski * for as long as this mapping might exist. 3449a62c34bdSAndy Lutomirski */ 3450a62c34bdSAndy Lutomirski struct vm_area_struct *_install_special_mapping( 3451a62c34bdSAndy Lutomirski struct mm_struct *mm, 3452a62c34bdSAndy Lutomirski unsigned long addr, unsigned long len, 3453a62c34bdSAndy Lutomirski unsigned long vm_flags, const struct vm_special_mapping *spec) 3454a62c34bdSAndy Lutomirski { 345527f28b97SChen Gang return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, 345627f28b97SChen Gang &special_mapping_vmops); 3457a62c34bdSAndy Lutomirski } 3458a62c34bdSAndy Lutomirski 34593935ed6aSStefani Seibold int install_special_mapping(struct mm_struct *mm, 34603935ed6aSStefani Seibold unsigned long addr, unsigned long len, 34613935ed6aSStefani Seibold unsigned long vm_flags, struct page **pages) 34623935ed6aSStefani Seibold { 3463a62c34bdSAndy Lutomirski struct vm_area_struct *vma = __install_special_mapping( 346427f28b97SChen Gang mm, addr, len, vm_flags, (void *)pages, 346527f28b97SChen Gang &legacy_special_mapping_vmops); 34663935ed6aSStefani Seibold 346714bd5b45SDuan Jiong return PTR_ERR_OR_ZERO(vma); 3468fa5dc22fSRoland McGrath } 34697906d00cSAndrea Arcangeli 34707906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 34717906d00cSAndrea Arcangeli 3472454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 34737906d00cSAndrea Arcangeli { 3474f808c13fSDavidlohr Bueso if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 34757906d00cSAndrea Arcangeli /* 34767906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 34777906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 34787906d00cSAndrea Arcangeli */ 3479da1c55f1SMichel Lespinasse down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 34807906d00cSAndrea Arcangeli /* 34817906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 34825a505085SIngo Molnar * anon_vma->root->rwsem. If some other vma in this mm shares 34837906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 34847906d00cSAndrea Arcangeli * 34857906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 34867906d00cSAndrea Arcangeli * can't change from under us thanks to the 34875a505085SIngo Molnar * anon_vma->root->rwsem. 34887906d00cSAndrea Arcangeli */ 34897906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 3490f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 34917906d00cSAndrea Arcangeli BUG(); 34927906d00cSAndrea Arcangeli } 34937906d00cSAndrea Arcangeli } 34947906d00cSAndrea Arcangeli 3495454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 34967906d00cSAndrea Arcangeli { 34977906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 34987906d00cSAndrea Arcangeli /* 34997906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 35007906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 35017906d00cSAndrea Arcangeli * 35027906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 35037906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 35047906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 35057906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 35067906d00cSAndrea Arcangeli */ 35077906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 35087906d00cSAndrea Arcangeli BUG(); 3509da1c55f1SMichel Lespinasse down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 35107906d00cSAndrea Arcangeli } 35117906d00cSAndrea Arcangeli } 35127906d00cSAndrea Arcangeli 35137906d00cSAndrea Arcangeli /* 35147906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 35157906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 35167906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 35177906d00cSAndrea Arcangeli * 3518c1e8d7c6SMichel Lespinasse * The caller must take the mmap_lock in write mode before calling 35197906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 3520c1e8d7c6SMichel Lespinasse * mmap_lock until mm_drop_all_locks() returns. 35217906d00cSAndrea Arcangeli * 3522c1e8d7c6SMichel Lespinasse * mmap_lock in write mode is required in order to block all operations 35237906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 352427ba0644SKirill A. Shutemov * altering the vma layout. It's also needed in write mode to avoid new 35257906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 35267906d00cSAndrea Arcangeli * 35277906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 35287906d00cSAndrea Arcangeli * or it would deadlock. 35297906d00cSAndrea Arcangeli * 3530bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 35317906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 35327906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 35337906d00cSAndrea Arcangeli * 353488f306b6SKirill A. Shutemov * We take locks in following order, accordingly to comment at beginning 353588f306b6SKirill A. Shutemov * of mm/rmap.c: 353688f306b6SKirill A. Shutemov * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 353788f306b6SKirill A. Shutemov * hugetlb mapping); 3538eeff9a5dSSuren Baghdasaryan * - all vmas marked locked 353988f306b6SKirill A. Shutemov * - all i_mmap_rwsem locks; 354088f306b6SKirill A. Shutemov * - all anon_vma->rwseml 354188f306b6SKirill A. Shutemov * 354288f306b6SKirill A. Shutemov * We can take all locks within these types randomly because the VM code 354388f306b6SKirill A. Shutemov * doesn't nest them and we protected from parallel mm_take_all_locks() by 354488f306b6SKirill A. Shutemov * mm_all_locks_mutex. 35457906d00cSAndrea Arcangeli * 35467906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 35477906d00cSAndrea Arcangeli * that may have to take thousand of locks. 35487906d00cSAndrea Arcangeli * 35497906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 35507906d00cSAndrea Arcangeli */ 35517906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 35527906d00cSAndrea Arcangeli { 35537906d00cSAndrea Arcangeli struct vm_area_struct *vma; 35545beb4930SRik van Riel struct anon_vma_chain *avc; 3555763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 35567906d00cSAndrea Arcangeli 3557325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 35587906d00cSAndrea Arcangeli 35597906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 35607906d00cSAndrea Arcangeli 3561763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35627906d00cSAndrea Arcangeli if (signal_pending(current)) 35637906d00cSAndrea Arcangeli goto out_unlock; 3564eeff9a5dSSuren Baghdasaryan vma_start_write(vma); 3565eeff9a5dSSuren Baghdasaryan } 3566eeff9a5dSSuren Baghdasaryan 3567eeff9a5dSSuren Baghdasaryan mas_set(&mas, 0); 3568eeff9a5dSSuren Baghdasaryan mas_for_each(&mas, vma, ULONG_MAX) { 3569eeff9a5dSSuren Baghdasaryan if (signal_pending(current)) 3570eeff9a5dSSuren Baghdasaryan goto out_unlock; 357188f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 357288f306b6SKirill A. Shutemov is_vm_hugetlb_page(vma)) 357388f306b6SKirill A. Shutemov vm_lock_mapping(mm, vma->vm_file->f_mapping); 357488f306b6SKirill A. Shutemov } 357588f306b6SKirill A. Shutemov 3576763ecb03SLiam R. Howlett mas_set(&mas, 0); 3577763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 357888f306b6SKirill A. Shutemov if (signal_pending(current)) 357988f306b6SKirill A. Shutemov goto out_unlock; 358088f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 358188f306b6SKirill A. Shutemov !is_vm_hugetlb_page(vma)) 3582454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 35837906d00cSAndrea Arcangeli } 35847cd5a02fSPeter Zijlstra 3585763ecb03SLiam R. Howlett mas_set(&mas, 0); 3586763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 35877cd5a02fSPeter Zijlstra if (signal_pending(current)) 35887cd5a02fSPeter Zijlstra goto out_unlock; 35897cd5a02fSPeter Zijlstra if (vma->anon_vma) 35905beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 35915beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 35927cd5a02fSPeter Zijlstra } 35937cd5a02fSPeter Zijlstra 3594584cff54SKautuk Consul return 0; 35957906d00cSAndrea Arcangeli 35967906d00cSAndrea Arcangeli out_unlock: 35977906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 3598584cff54SKautuk Consul return -EINTR; 35997906d00cSAndrea Arcangeli } 36007906d00cSAndrea Arcangeli 36017906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 36027906d00cSAndrea Arcangeli { 3603f808c13fSDavidlohr Bueso if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 36047906d00cSAndrea Arcangeli /* 36057906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 36067906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 36077906d00cSAndrea Arcangeli * 36087906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 3609bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 36107906d00cSAndrea Arcangeli * never see our bitflag. 36117906d00cSAndrea Arcangeli * 36127906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 36137906d00cSAndrea Arcangeli * can't change from under us until we release the 36145a505085SIngo Molnar * anon_vma->root->rwsem. 36157906d00cSAndrea Arcangeli */ 36167906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 3617f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 36187906d00cSAndrea Arcangeli BUG(); 361908b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 36207906d00cSAndrea Arcangeli } 36217906d00cSAndrea Arcangeli } 36227906d00cSAndrea Arcangeli 36237906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 36247906d00cSAndrea Arcangeli { 36257906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 36267906d00cSAndrea Arcangeli /* 36277906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 36287906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 36297906d00cSAndrea Arcangeli */ 363083cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 36317906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 36327906d00cSAndrea Arcangeli &mapping->flags)) 36337906d00cSAndrea Arcangeli BUG(); 36347906d00cSAndrea Arcangeli } 36357906d00cSAndrea Arcangeli } 36367906d00cSAndrea Arcangeli 36377906d00cSAndrea Arcangeli /* 3638c1e8d7c6SMichel Lespinasse * The mmap_lock cannot be released by the caller until 36397906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 36407906d00cSAndrea Arcangeli */ 36417906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 36427906d00cSAndrea Arcangeli { 36437906d00cSAndrea Arcangeli struct vm_area_struct *vma; 36445beb4930SRik van Riel struct anon_vma_chain *avc; 3645763ecb03SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 36467906d00cSAndrea Arcangeli 3647325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 36487906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 36497906d00cSAndrea Arcangeli 3650763ecb03SLiam R. Howlett mas_for_each(&mas, vma, ULONG_MAX) { 36517906d00cSAndrea Arcangeli if (vma->anon_vma) 36525beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 36535beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 36547906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 36557906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 36567906d00cSAndrea Arcangeli } 3657eeff9a5dSSuren Baghdasaryan vma_end_write_all(mm); 36587906d00cSAndrea Arcangeli 36597906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 36607906d00cSAndrea Arcangeli } 36618feae131SDavid Howells 36628feae131SDavid Howells /* 36633edf41d8Sseokhoon.yoon * initialise the percpu counter for VM 36648feae131SDavid Howells */ 36658feae131SDavid Howells void __init mmap_init(void) 36668feae131SDavid Howells { 366700a62ce9SKOSAKI Motohiro int ret; 366800a62ce9SKOSAKI Motohiro 3669908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 367000a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 36718feae131SDavid Howells } 3672c9b1d098SAndrew Shewmaker 3673c9b1d098SAndrew Shewmaker /* 3674c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 3675c9b1d098SAndrew Shewmaker * 3676c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 3677c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 3678c9b1d098SAndrew Shewmaker * mode. 3679c9b1d098SAndrew Shewmaker * 3680c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 3681c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 3682c9b1d098SAndrew Shewmaker */ 36831640879aSAndrew Shewmaker static int init_user_reserve(void) 3684c9b1d098SAndrew Shewmaker { 3685c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 3686c9b1d098SAndrew Shewmaker 3687c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 3688c9b1d098SAndrew Shewmaker 3689c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 3690c9b1d098SAndrew Shewmaker return 0; 3691c9b1d098SAndrew Shewmaker } 3692a64fb3cdSPaul Gortmaker subsys_initcall(init_user_reserve); 36934eeab4f5SAndrew Shewmaker 36944eeab4f5SAndrew Shewmaker /* 36954eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 36964eeab4f5SAndrew Shewmaker * 36974eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 36984eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 36994eeab4f5SAndrew Shewmaker * 37004eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 37014eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 37024eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 37034eeab4f5SAndrew Shewmaker */ 37041640879aSAndrew Shewmaker static int init_admin_reserve(void) 37054eeab4f5SAndrew Shewmaker { 37064eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 37074eeab4f5SAndrew Shewmaker 3708c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37094eeab4f5SAndrew Shewmaker 37104eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 37114eeab4f5SAndrew Shewmaker return 0; 37124eeab4f5SAndrew Shewmaker } 3713a64fb3cdSPaul Gortmaker subsys_initcall(init_admin_reserve); 37141640879aSAndrew Shewmaker 37151640879aSAndrew Shewmaker /* 37161640879aSAndrew Shewmaker * Reinititalise user and admin reserves if memory is added or removed. 37171640879aSAndrew Shewmaker * 37181640879aSAndrew Shewmaker * The default user reserve max is 128MB, and the default max for the 37191640879aSAndrew Shewmaker * admin reserve is 8MB. These are usually, but not always, enough to 37201640879aSAndrew Shewmaker * enable recovery from a memory hogging process using login/sshd, a shell, 37211640879aSAndrew Shewmaker * and tools like top. It may make sense to increase or even disable the 37221640879aSAndrew Shewmaker * reserve depending on the existence of swap or variations in the recovery 37231640879aSAndrew Shewmaker * tools. So, the admin may have changed them. 37241640879aSAndrew Shewmaker * 37251640879aSAndrew Shewmaker * If memory is added and the reserves have been eliminated or increased above 37261640879aSAndrew Shewmaker * the default max, then we'll trust the admin. 37271640879aSAndrew Shewmaker * 37281640879aSAndrew Shewmaker * If memory is removed and there isn't enough free memory, then we 37291640879aSAndrew Shewmaker * need to reset the reserves. 37301640879aSAndrew Shewmaker * 37311640879aSAndrew Shewmaker * Otherwise keep the reserve set by the admin. 37321640879aSAndrew Shewmaker */ 37331640879aSAndrew Shewmaker static int reserve_mem_notifier(struct notifier_block *nb, 37341640879aSAndrew Shewmaker unsigned long action, void *data) 37351640879aSAndrew Shewmaker { 37361640879aSAndrew Shewmaker unsigned long tmp, free_kbytes; 37371640879aSAndrew Shewmaker 37381640879aSAndrew Shewmaker switch (action) { 37391640879aSAndrew Shewmaker case MEM_ONLINE: 37401640879aSAndrew Shewmaker /* Default max is 128MB. Leave alone if modified by operator. */ 37411640879aSAndrew Shewmaker tmp = sysctl_user_reserve_kbytes; 37421640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 17)) 37431640879aSAndrew Shewmaker init_user_reserve(); 37441640879aSAndrew Shewmaker 37451640879aSAndrew Shewmaker /* Default max is 8MB. Leave alone if modified by operator. */ 37461640879aSAndrew Shewmaker tmp = sysctl_admin_reserve_kbytes; 37471640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 13)) 37481640879aSAndrew Shewmaker init_admin_reserve(); 37491640879aSAndrew Shewmaker 37501640879aSAndrew Shewmaker break; 37511640879aSAndrew Shewmaker case MEM_OFFLINE: 3752c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 37531640879aSAndrew Shewmaker 37541640879aSAndrew Shewmaker if (sysctl_user_reserve_kbytes > free_kbytes) { 37551640879aSAndrew Shewmaker init_user_reserve(); 37561640879aSAndrew Shewmaker pr_info("vm.user_reserve_kbytes reset to %lu\n", 37571640879aSAndrew Shewmaker sysctl_user_reserve_kbytes); 37581640879aSAndrew Shewmaker } 37591640879aSAndrew Shewmaker 37601640879aSAndrew Shewmaker if (sysctl_admin_reserve_kbytes > free_kbytes) { 37611640879aSAndrew Shewmaker init_admin_reserve(); 37621640879aSAndrew Shewmaker pr_info("vm.admin_reserve_kbytes reset to %lu\n", 37631640879aSAndrew Shewmaker sysctl_admin_reserve_kbytes); 37641640879aSAndrew Shewmaker } 37651640879aSAndrew Shewmaker break; 37661640879aSAndrew Shewmaker default: 37671640879aSAndrew Shewmaker break; 37681640879aSAndrew Shewmaker } 37691640879aSAndrew Shewmaker return NOTIFY_OK; 37701640879aSAndrew Shewmaker } 37711640879aSAndrew Shewmaker 37721640879aSAndrew Shewmaker static int __meminit init_reserve_notifier(void) 37731640879aSAndrew Shewmaker { 37741eeaa4fdSLiu Shixin if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI)) 3775b1de0d13SMitchel Humpherys pr_err("Failed registering memory add/remove notifier for admin reserve\n"); 37761640879aSAndrew Shewmaker 37771640879aSAndrew Shewmaker return 0; 37781640879aSAndrew Shewmaker } 3779a64fb3cdSPaul Gortmaker subsys_initcall(init_reserve_notifier); 3780