1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * mm/mmap.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Written by obz. 61da177e4SLinus Torvalds * 7046c6884SAlan Cox * Address space accounting code <alan@lxorguk.ukuu.org.uk> 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 10b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11b1de0d13SMitchel Humpherys 12e8420a8eSCyril Hrubis #include <linux/kernel.h> 131da177e4SLinus Torvalds #include <linux/slab.h> 144af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 1617fca131SArnd Bergmann #include <linux/mm_inline.h> 171da177e4SLinus Torvalds #include <linux/shm.h> 181da177e4SLinus Torvalds #include <linux/mman.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/swap.h> 211da177e4SLinus Torvalds #include <linux/syscalls.h> 22c59ede7bSRandy.Dunlap #include <linux/capability.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/file.h> 251da177e4SLinus Torvalds #include <linux/fs.h> 261da177e4SLinus Torvalds #include <linux/personality.h> 271da177e4SLinus Torvalds #include <linux/security.h> 281da177e4SLinus Torvalds #include <linux/hugetlb.h> 29c01d5b30SHugh Dickins #include <linux/shmem_fs.h> 301da177e4SLinus Torvalds #include <linux/profile.h> 31b95f1b31SPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/mount.h> 331da177e4SLinus Torvalds #include <linux/mempolicy.h> 341da177e4SLinus Torvalds #include <linux/rmap.h> 35cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h> 3682f71ae4SKonstantin Khlebnikov #include <linux/mmdebug.h> 37cdd6c482SIngo Molnar #include <linux/perf_event.h> 38120a795dSAl Viro #include <linux/audit.h> 39b15d00b6SAndrea Arcangeli #include <linux/khugepaged.h> 402b144498SSrikar Dronamraju #include <linux/uprobes.h> 411640879aSAndrew Shewmaker #include <linux/notifier.h> 421640879aSAndrew Shewmaker #include <linux/memory.h> 43b1de0d13SMitchel Humpherys #include <linux/printk.h> 4419a809afSAndrea Arcangeli #include <linux/userfaultfd_k.h> 45d977d56cSKonstantin Khlebnikov #include <linux/moduleparam.h> 4662b5f7d0SDave Hansen #include <linux/pkeys.h> 4721292580SAndrea Arcangeli #include <linux/oom.h> 4804f5866eSAndrea Arcangeli #include <linux/sched/mm.h> 491da177e4SLinus Torvalds 507c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 511da177e4SLinus Torvalds #include <asm/cacheflush.h> 521da177e4SLinus Torvalds #include <asm/tlb.h> 53d6dd61c8SJeremy Fitzhardinge #include <asm/mmu_context.h> 541da177e4SLinus Torvalds 55df529cabSJaewon Kim #define CREATE_TRACE_POINTS 56df529cabSJaewon Kim #include <trace/events/mmap.h> 57df529cabSJaewon Kim 5842b77728SJan Beulich #include "internal.h" 5942b77728SJan Beulich 603a459756SKirill Korotaev #ifndef arch_mmap_check 613a459756SKirill Korotaev #define arch_mmap_check(addr, len, flags) (0) 623a459756SKirill Korotaev #endif 633a459756SKirill Korotaev 64d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS 65d07e2259SDaniel Cashman const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; 66d07e2259SDaniel Cashman const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; 67d07e2259SDaniel Cashman int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; 68d07e2259SDaniel Cashman #endif 69d07e2259SDaniel Cashman #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS 70d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; 71d07e2259SDaniel Cashman const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; 72d07e2259SDaniel Cashman int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; 73d07e2259SDaniel Cashman #endif 74d07e2259SDaniel Cashman 75f4fcd558SKonstantin Khlebnikov static bool ignore_rlimit_data; 76d977d56cSKonstantin Khlebnikov core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); 77d07e2259SDaniel Cashman 78e0da382cSHugh Dickins static void unmap_region(struct mm_struct *mm, 79e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 80e0da382cSHugh Dickins unsigned long start, unsigned long end); 81e0da382cSHugh Dickins 8264e45507SPeter Feiner static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) 8364e45507SPeter Feiner { 8464e45507SPeter Feiner return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); 8564e45507SPeter Feiner } 8664e45507SPeter Feiner 8764e45507SPeter Feiner /* Update vma->vm_page_prot to reflect vma->vm_flags. */ 8864e45507SPeter Feiner void vma_set_page_prot(struct vm_area_struct *vma) 8964e45507SPeter Feiner { 9064e45507SPeter Feiner unsigned long vm_flags = vma->vm_flags; 916d2329f8SAndrea Arcangeli pgprot_t vm_page_prot; 9264e45507SPeter Feiner 936d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); 946d2329f8SAndrea Arcangeli if (vma_wants_writenotify(vma, vm_page_prot)) { 9564e45507SPeter Feiner vm_flags &= ~VM_SHARED; 966d2329f8SAndrea Arcangeli vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); 9764e45507SPeter Feiner } 98c1e8d7c6SMichel Lespinasse /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ 996d2329f8SAndrea Arcangeli WRITE_ONCE(vma->vm_page_prot, vm_page_prot); 10064e45507SPeter Feiner } 10164e45507SPeter Feiner 1021da177e4SLinus Torvalds /* 103c8c06efaSDavidlohr Bueso * Requires inode->i_mapping->i_mmap_rwsem 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds static void __remove_shared_vm_struct(struct vm_area_struct *vma, 1061da177e4SLinus Torvalds struct file *file, struct address_space *mapping) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 1094bb5f5d9SDavid Herrmann mapping_unmap_writable(mapping); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 1126b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 1131da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* 1176b2dbba8SMichel Lespinasse * Unlink a file-based vm structure from its interval tree, to hide 118a8fb5618SHugh Dickins * vma from rmap and vmtruncate before freeing its page tables. 1191da177e4SLinus Torvalds */ 120a8fb5618SHugh Dickins void unlink_file_vma(struct vm_area_struct *vma) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds struct file *file = vma->vm_file; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds if (file) { 1251da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 12683cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 1271da177e4SLinus Torvalds __remove_shared_vm_struct(vma, file, mapping); 12883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 1291da177e4SLinus Torvalds } 130a8fb5618SHugh Dickins } 131a8fb5618SHugh Dickins 132a8fb5618SHugh Dickins /* 133a8fb5618SHugh Dickins * Close a vm structure and free it, returning the next. 134a8fb5618SHugh Dickins */ 135a8fb5618SHugh Dickins static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) 136a8fb5618SHugh Dickins { 137a8fb5618SHugh Dickins struct vm_area_struct *next = vma->vm_next; 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)); 1453928d4f5SLinus Torvalds vm_area_free(vma); 146a8fb5618SHugh Dickins return next; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1492e7ce7d3SLiam R. Howlett /* 1502e7ce7d3SLiam R. Howlett * check_brk_limits() - Use platform specific check of range & verify mlock 1512e7ce7d3SLiam R. Howlett * limits. 1522e7ce7d3SLiam R. Howlett * @addr: The address to check 1532e7ce7d3SLiam R. Howlett * @len: The size of increase. 1542e7ce7d3SLiam R. Howlett * 1552e7ce7d3SLiam R. Howlett * Return: 0 on success. 1562e7ce7d3SLiam R. Howlett */ 1572e7ce7d3SLiam R. Howlett static int check_brk_limits(unsigned long addr, unsigned long len) 1582e7ce7d3SLiam R. Howlett { 1592e7ce7d3SLiam R. Howlett unsigned long mapped_addr; 1602e7ce7d3SLiam R. Howlett 1612e7ce7d3SLiam R. Howlett mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); 1622e7ce7d3SLiam R. Howlett if (IS_ERR_VALUE(mapped_addr)) 1632e7ce7d3SLiam R. Howlett return mapped_addr; 1642e7ce7d3SLiam R. Howlett 1652e7ce7d3SLiam R. Howlett return mlock_future_check(current->mm, current->mm->def_flags, len); 1662e7ce7d3SLiam R. Howlett } 1672e7ce7d3SLiam R. Howlett static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, 1682e7ce7d3SLiam R. Howlett unsigned long newbrk, unsigned long oldbrk, 169bb177a73SMichal Hocko struct list_head *uf); 1702e7ce7d3SLiam R. Howlett static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *brkvma, 1712e7ce7d3SLiam R. Howlett unsigned long addr, unsigned long request, 1722e7ce7d3SLiam R. Howlett unsigned long flags); 1736a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 1741da177e4SLinus Torvalds { 1759bc8039eSYang Shi unsigned long newbrk, oldbrk, origbrk; 1761da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1772e7ce7d3SLiam R. Howlett struct vm_area_struct *brkvma, *next = NULL; 178a5b4592cSJiri Kosina unsigned long min_brk; 179128557ffSMichel Lespinasse bool populate; 1809bc8039eSYang Shi bool downgraded = false; 181897ab3e0SMike Rapoport LIST_HEAD(uf); 1822e7ce7d3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 1831da177e4SLinus Torvalds 184d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 185dc0ef0dfSMichal Hocko return -EINTR; 1861da177e4SLinus Torvalds 1879bc8039eSYang Shi origbrk = mm->brk; 1889bc8039eSYang Shi 189a5b4592cSJiri Kosina #ifdef CONFIG_COMPAT_BRK 1905520e894SJiri Kosina /* 1915520e894SJiri Kosina * CONFIG_COMPAT_BRK can still be overridden by setting 1925520e894SJiri Kosina * randomize_va_space to 2, which will still cause mm->start_brk 1935520e894SJiri Kosina * to be arbitrarily shifted 1945520e894SJiri Kosina */ 1954471a675SJiri Kosina if (current->brk_randomized) 1965520e894SJiri Kosina min_brk = mm->start_brk; 1975520e894SJiri Kosina else 1985520e894SJiri Kosina min_brk = mm->end_data; 199a5b4592cSJiri Kosina #else 200a5b4592cSJiri Kosina min_brk = mm->start_brk; 201a5b4592cSJiri Kosina #endif 202a5b4592cSJiri Kosina if (brk < min_brk) 2031da177e4SLinus Torvalds goto out; 2041e624196SRam Gupta 2051e624196SRam Gupta /* 2061e624196SRam Gupta * Check against rlimit here. If this check is done later after the test 2071e624196SRam Gupta * of oldbrk with newbrk then it can escape the test and let the data 2081e624196SRam Gupta * segment grow beyond its set limit the in case where the limit is 2091e624196SRam Gupta * not page aligned -Ram Gupta 2101e624196SRam Gupta */ 2118764b338SCyrill Gorcunov if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, 2128764b338SCyrill Gorcunov mm->end_data, mm->start_data)) 2131e624196SRam Gupta goto out; 2141e624196SRam Gupta 2151da177e4SLinus Torvalds newbrk = PAGE_ALIGN(brk); 2161da177e4SLinus Torvalds oldbrk = PAGE_ALIGN(mm->brk); 2179bc8039eSYang Shi if (oldbrk == newbrk) { 2189bc8039eSYang Shi mm->brk = brk; 2199bc8039eSYang Shi goto success; 2209bc8039eSYang Shi } 2211da177e4SLinus Torvalds 2229bc8039eSYang Shi /* 2239bc8039eSYang Shi * Always allow shrinking brk. 2242e7ce7d3SLiam R. Howlett * do_brk_munmap() may downgrade mmap_lock to read. 2259bc8039eSYang Shi */ 2261da177e4SLinus Torvalds if (brk <= mm->brk) { 2279bc8039eSYang Shi int ret; 2289bc8039eSYang Shi 2292e7ce7d3SLiam R. Howlett /* Search one past newbrk */ 2302e7ce7d3SLiam R. Howlett mas_set(&mas, newbrk); 2312e7ce7d3SLiam R. Howlett brkvma = mas_find(&mas, oldbrk); 2322e7ce7d3SLiam R. Howlett BUG_ON(brkvma == NULL); 2332e7ce7d3SLiam R. Howlett if (brkvma->vm_start >= oldbrk) 2342e7ce7d3SLiam R. Howlett goto out; /* mapping intersects with an existing non-brk vma. */ 2359bc8039eSYang Shi /* 2362e7ce7d3SLiam R. Howlett * mm->brk must be protected by write mmap_lock. 2372e7ce7d3SLiam R. Howlett * do_brk_munmap() may downgrade the lock, so update it 2382e7ce7d3SLiam R. Howlett * before calling do_brk_munmap(). 2399bc8039eSYang Shi */ 2409bc8039eSYang Shi mm->brk = brk; 2412e7ce7d3SLiam R. Howlett mas.last = oldbrk - 1; 2422e7ce7d3SLiam R. Howlett ret = do_brk_munmap(&mas, brkvma, newbrk, oldbrk, &uf); 2432e7ce7d3SLiam R. Howlett if (ret == 1) { 2442e7ce7d3SLiam R. Howlett downgraded = true; 2452e7ce7d3SLiam R. Howlett goto success; 2462e7ce7d3SLiam R. Howlett } else if (!ret) 2472e7ce7d3SLiam R. Howlett goto success; 2482e7ce7d3SLiam R. Howlett 2499bc8039eSYang Shi mm->brk = origbrk; 2501da177e4SLinus Torvalds goto out; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2532e7ce7d3SLiam R. Howlett if (check_brk_limits(oldbrk, newbrk - oldbrk)) 2542e7ce7d3SLiam R. Howlett goto out; 2552e7ce7d3SLiam R. Howlett 2562e7ce7d3SLiam R. Howlett /* 2572e7ce7d3SLiam R. Howlett * Only check if the next VMA is within the stack_guard_gap of the 2582e7ce7d3SLiam R. Howlett * expansion area 2592e7ce7d3SLiam R. Howlett */ 2602e7ce7d3SLiam R. Howlett mas_set(&mas, oldbrk); 2612e7ce7d3SLiam R. Howlett next = mas_find(&mas, newbrk - 1 + PAGE_SIZE + stack_guard_gap); 2621be7107fSHugh Dickins if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) 2631da177e4SLinus Torvalds goto out; 2641da177e4SLinus Torvalds 2652e7ce7d3SLiam R. Howlett brkvma = mas_prev(&mas, mm->start_brk); 2661da177e4SLinus Torvalds /* Ok, looks good - let it rip. */ 2672e7ce7d3SLiam R. Howlett if (do_brk_flags(&mas, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) 2681da177e4SLinus Torvalds goto out; 2692e7ce7d3SLiam R. Howlett 2701da177e4SLinus Torvalds mm->brk = brk; 2719bc8039eSYang Shi 2729bc8039eSYang Shi success: 273128557ffSMichel Lespinasse populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; 2749bc8039eSYang Shi if (downgraded) 275d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2769bc8039eSYang Shi else 277d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 278897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 279128557ffSMichel Lespinasse if (populate) 280128557ffSMichel Lespinasse mm_populate(oldbrk, newbrk - oldbrk); 281128557ffSMichel Lespinasse return brk; 282128557ffSMichel Lespinasse 2831da177e4SLinus Torvalds out: 284d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 285b7204006SAdrian Huang return origbrk; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288d4af56c5SLiam R. Howlett #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 289d4af56c5SLiam R. Howlett extern void mt_validate(struct maple_tree *mt); 290d4af56c5SLiam R. Howlett extern void mt_dump(const struct maple_tree *mt); 2911da177e4SLinus Torvalds 292d4af56c5SLiam R. Howlett /* Validate the maple tree */ 293d4af56c5SLiam R. Howlett static void validate_mm_mt(struct mm_struct *mm) 294d4af56c5SLiam R. Howlett { 295d4af56c5SLiam R. Howlett struct maple_tree *mt = &mm->mm_mt; 296d4af56c5SLiam R. Howlett struct vm_area_struct *vma_mt, *vma = mm->mmap; 297d4af56c5SLiam R. Howlett 298d4af56c5SLiam R. Howlett MA_STATE(mas, mt, 0, 0); 299d4af56c5SLiam R. Howlett 300d4af56c5SLiam R. Howlett mt_validate(&mm->mm_mt); 301d4af56c5SLiam R. Howlett mas_for_each(&mas, vma_mt, ULONG_MAX) { 302d4af56c5SLiam R. Howlett if (xa_is_zero(vma_mt)) 303d4af56c5SLiam R. Howlett continue; 304d4af56c5SLiam R. Howlett 305d4af56c5SLiam R. Howlett if (!vma) 306d4af56c5SLiam R. Howlett break; 307d4af56c5SLiam R. Howlett 308d4af56c5SLiam R. Howlett if ((vma != vma_mt) || 309d4af56c5SLiam R. Howlett (vma->vm_start != vma_mt->vm_start) || 310d4af56c5SLiam R. Howlett (vma->vm_end != vma_mt->vm_end) || 311d4af56c5SLiam R. Howlett (vma->vm_start != mas.index) || 312d4af56c5SLiam R. Howlett (vma->vm_end - 1 != mas.last)) { 313d4af56c5SLiam R. Howlett pr_emerg("issue in %s\n", current->comm); 314d4af56c5SLiam R. Howlett dump_stack(); 315d4af56c5SLiam R. Howlett dump_vma(vma_mt); 316524e00b3SLiam R. Howlett pr_emerg("and vm_next\n"); 317d4af56c5SLiam R. Howlett dump_vma(vma->vm_next); 318d4af56c5SLiam R. Howlett pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, 319d4af56c5SLiam R. Howlett mas.index, mas.last); 320d4af56c5SLiam R. Howlett pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, 321d4af56c5SLiam R. Howlett vma_mt->vm_start, vma_mt->vm_end); 322524e00b3SLiam R. Howlett if (vma->vm_prev) { 323524e00b3SLiam R. Howlett pr_emerg("ll prev: %p %lu - %lu\n", 324524e00b3SLiam R. Howlett vma->vm_prev, vma->vm_prev->vm_start, 325524e00b3SLiam R. Howlett vma->vm_prev->vm_end); 326524e00b3SLiam R. Howlett } 327524e00b3SLiam R. Howlett pr_emerg("ll vma: %p %lu - %lu\n", vma, 328d4af56c5SLiam R. Howlett vma->vm_start, vma->vm_end); 329524e00b3SLiam R. Howlett if (vma->vm_next) { 330524e00b3SLiam R. Howlett pr_emerg("ll next: %p %lu - %lu\n", 331524e00b3SLiam R. Howlett vma->vm_next, vma->vm_next->vm_start, 332524e00b3SLiam R. Howlett vma->vm_next->vm_end); 333524e00b3SLiam R. Howlett } 334d4af56c5SLiam R. Howlett 335d4af56c5SLiam R. Howlett mt_dump(mas.tree); 336d4af56c5SLiam R. Howlett if (vma_mt->vm_end != mas.last + 1) { 337d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", 338d4af56c5SLiam R. Howlett mm, vma_mt->vm_start, vma_mt->vm_end, 339d4af56c5SLiam R. Howlett mas.index, mas.last); 340d4af56c5SLiam R. Howlett mt_dump(mas.tree); 341d4af56c5SLiam R. Howlett } 342d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); 343d4af56c5SLiam R. Howlett if (vma_mt->vm_start != mas.index) { 344d4af56c5SLiam R. Howlett pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", 345d4af56c5SLiam R. Howlett mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); 346d4af56c5SLiam R. Howlett mt_dump(mas.tree); 347d4af56c5SLiam R. Howlett } 348d4af56c5SLiam R. Howlett VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); 349d4af56c5SLiam R. Howlett } 350d4af56c5SLiam R. Howlett VM_BUG_ON(vma != vma_mt); 351d4af56c5SLiam R. Howlett vma = vma->vm_next; 352d4af56c5SLiam R. Howlett 353d4af56c5SLiam R. Howlett } 354d4af56c5SLiam R. Howlett VM_BUG_ON(vma); 355d4af56c5SLiam R. Howlett } 3561da177e4SLinus Torvalds 357eafd4dc4SRashika Kheria static void validate_mm(struct mm_struct *mm) 3581da177e4SLinus Torvalds { 3591da177e4SLinus Torvalds int bug = 0; 3601da177e4SLinus Torvalds int i = 0; 3615a0768f6SMichel Lespinasse unsigned long highest_address = 0; 362ed8ea815SMichel Lespinasse struct vm_area_struct *vma = mm->mmap; 363ff26f70fSAndrew Morton 364524e00b3SLiam R. Howlett validate_mm_mt(mm); 365524e00b3SLiam R. Howlett 366ed8ea815SMichel Lespinasse while (vma) { 367524e00b3SLiam R. Howlett #ifdef CONFIG_DEBUG_VM_RB 36812352d3cSKonstantin Khlebnikov struct anon_vma *anon_vma = vma->anon_vma; 369ed8ea815SMichel Lespinasse struct anon_vma_chain *avc; 370ff26f70fSAndrew Morton 37112352d3cSKonstantin Khlebnikov if (anon_vma) { 37212352d3cSKonstantin Khlebnikov anon_vma_lock_read(anon_vma); 373ed8ea815SMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 374ed8ea815SMichel Lespinasse anon_vma_interval_tree_verify(avc); 37512352d3cSKonstantin Khlebnikov anon_vma_unlock_read(anon_vma); 37612352d3cSKonstantin Khlebnikov } 377524e00b3SLiam R. Howlett #endif 37812352d3cSKonstantin Khlebnikov 3791be7107fSHugh Dickins highest_address = vm_end_gap(vma); 380ed8ea815SMichel Lespinasse vma = vma->vm_next; 3811da177e4SLinus Torvalds i++; 3821da177e4SLinus Torvalds } 3835a0768f6SMichel Lespinasse if (i != mm->map_count) { 3848542bdfcSSasha Levin pr_emerg("map_count %d vm_next %d\n", mm->map_count, i); 3855a0768f6SMichel Lespinasse bug = 1; 3865a0768f6SMichel Lespinasse } 3875a0768f6SMichel Lespinasse if (highest_address != mm->highest_vm_end) { 3888542bdfcSSasha Levin pr_emerg("mm->highest_vm_end %lx, found %lx\n", 3895a0768f6SMichel Lespinasse mm->highest_vm_end, highest_address); 3905a0768f6SMichel Lespinasse bug = 1; 3915a0768f6SMichel Lespinasse } 39296dad67fSSasha Levin VM_BUG_ON_MM(bug, mm); 3931da177e4SLinus Torvalds } 394524e00b3SLiam R. Howlett 395524e00b3SLiam R. Howlett #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ 396d4af56c5SLiam R. Howlett #define validate_mm_mt(root) do { } while (0) 3971da177e4SLinus Torvalds #define validate_mm(mm) do { } while (0) 398524e00b3SLiam R. Howlett #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 399d3737187SMichel Lespinasse 400bf181b9fSMichel Lespinasse /* 401bf181b9fSMichel Lespinasse * vma has some anon_vma assigned, and is already inserted on that 402bf181b9fSMichel Lespinasse * anon_vma's interval trees. 403bf181b9fSMichel Lespinasse * 404bf181b9fSMichel Lespinasse * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 405bf181b9fSMichel Lespinasse * vma must be removed from the anon_vma's interval trees using 406bf181b9fSMichel Lespinasse * anon_vma_interval_tree_pre_update_vma(). 407bf181b9fSMichel Lespinasse * 408bf181b9fSMichel Lespinasse * After the update, the vma will be reinserted using 409bf181b9fSMichel Lespinasse * anon_vma_interval_tree_post_update_vma(). 410bf181b9fSMichel Lespinasse * 411c1e8d7c6SMichel Lespinasse * The entire update must be protected by exclusive mmap_lock and by 412bf181b9fSMichel Lespinasse * the root anon_vma's mutex. 413bf181b9fSMichel Lespinasse */ 414bf181b9fSMichel Lespinasse static inline void 415bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 416bf181b9fSMichel Lespinasse { 417bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 418bf181b9fSMichel Lespinasse 419bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 420bf181b9fSMichel Lespinasse anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 421bf181b9fSMichel Lespinasse } 422bf181b9fSMichel Lespinasse 423bf181b9fSMichel Lespinasse static inline void 424bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 425bf181b9fSMichel Lespinasse { 426bf181b9fSMichel Lespinasse struct anon_vma_chain *avc; 427bf181b9fSMichel Lespinasse 428bf181b9fSMichel Lespinasse list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 429bf181b9fSMichel Lespinasse anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 430bf181b9fSMichel Lespinasse } 431bf181b9fSMichel Lespinasse 432524e00b3SLiam R. Howlett /* 433524e00b3SLiam R. Howlett * range_has_overlap() - Check the @start - @end range for overlapping VMAs and 434524e00b3SLiam R. Howlett * sets up a pointer to the previous VMA 435524e00b3SLiam R. Howlett * @mm: the mm struct 436524e00b3SLiam R. Howlett * @start: the start address of the range 437524e00b3SLiam R. Howlett * @end: the end address of the range 438524e00b3SLiam R. Howlett * @pprev: the pointer to the pointer of the previous VMA 439524e00b3SLiam R. Howlett * 440524e00b3SLiam R. Howlett * Returns: True if there is an overlapping VMA, false otherwise 441524e00b3SLiam R. Howlett */ 442524e00b3SLiam R. Howlett static inline 443524e00b3SLiam R. Howlett bool range_has_overlap(struct mm_struct *mm, unsigned long start, 444524e00b3SLiam R. Howlett unsigned long end, struct vm_area_struct **pprev) 4451da177e4SLinus Torvalds { 446524e00b3SLiam R. Howlett struct vm_area_struct *existing; 4471da177e4SLinus Torvalds 448524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 449524e00b3SLiam R. Howlett existing = mas_find(&mas, end - 1); 450524e00b3SLiam R. Howlett *pprev = mas_prev(&mas, 0); 451524e00b3SLiam R. Howlett return existing ? true : false; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4543903b55aSLiam R. Howlett /* 455f39af059SMatthew Wilcox (Oracle) * __vma_next() - Get the next VMA. 4563903b55aSLiam R. Howlett * @mm: The mm_struct. 4573903b55aSLiam R. Howlett * @vma: The current vma. 4583903b55aSLiam R. Howlett * 4593903b55aSLiam R. Howlett * If @vma is NULL, return the first vma in the mm. 4603903b55aSLiam R. Howlett * 4613903b55aSLiam R. Howlett * Returns: The next VMA after @vma. 4623903b55aSLiam R. Howlett */ 463f39af059SMatthew Wilcox (Oracle) static inline struct vm_area_struct *__vma_next(struct mm_struct *mm, 4643903b55aSLiam R. Howlett struct vm_area_struct *vma) 4653903b55aSLiam R. Howlett { 4663903b55aSLiam R. Howlett if (!vma) 4673903b55aSLiam R. Howlett return mm->mmap; 4683903b55aSLiam R. Howlett 4693903b55aSLiam R. Howlett return vma->vm_next; 4703903b55aSLiam R. Howlett } 471fb8090b6SLiam R. Howlett 472e8420a8eSCyril Hrubis static unsigned long count_vma_pages_range(struct mm_struct *mm, 473e8420a8eSCyril Hrubis unsigned long addr, unsigned long end) 474e8420a8eSCyril Hrubis { 4752e3af1dbSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, addr); 476e8420a8eSCyril Hrubis struct vm_area_struct *vma; 4772e3af1dbSMatthew Wilcox (Oracle) unsigned long nr_pages = 0; 478e8420a8eSCyril Hrubis 4792e3af1dbSMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) { 4802e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_start = max(addr, vma->vm_start); 4812e3af1dbSMatthew Wilcox (Oracle) unsigned long vm_end = min(end, vma->vm_end); 482e8420a8eSCyril Hrubis 4832e3af1dbSMatthew Wilcox (Oracle) nr_pages += PHYS_PFN(vm_end - vm_start); 484e8420a8eSCyril Hrubis } 485e8420a8eSCyril Hrubis 486e8420a8eSCyril Hrubis return nr_pages; 487e8420a8eSCyril Hrubis } 488e8420a8eSCyril Hrubis 489cb8f488cSDenys Vlasenko static void __vma_link_file(struct vm_area_struct *vma) 4901da177e4SLinus Torvalds { 4911da177e4SLinus Torvalds struct file *file; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds file = vma->vm_file; 4941da177e4SLinus Torvalds if (file) { 4951da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds if (vma->vm_flags & VM_SHARED) 498cf508b58SMiaohe Lin mapping_allow_writable(mapping); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 5016b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5021da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 506d4af56c5SLiam R. Howlett /* 507d4af56c5SLiam R. Howlett * vma_mas_store() - Store a VMA in the maple tree. 508d4af56c5SLiam R. Howlett * @vma: The vm_area_struct 509d4af56c5SLiam R. Howlett * @mas: The maple state 510d4af56c5SLiam R. Howlett * 511d4af56c5SLiam R. Howlett * Efficient way to store a VMA in the maple tree when the @mas has already 512d4af56c5SLiam R. Howlett * walked to the correct location. 513d4af56c5SLiam R. Howlett * 514d4af56c5SLiam R. Howlett * Note: the end address is inclusive in the maple tree. 515d4af56c5SLiam R. Howlett */ 516d4af56c5SLiam R. Howlett void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas) 517d4af56c5SLiam R. Howlett { 518d4af56c5SLiam R. Howlett trace_vma_store(mas->tree, vma); 519d4af56c5SLiam R. Howlett mas_set_range(mas, vma->vm_start, vma->vm_end - 1); 520d4af56c5SLiam R. Howlett mas_store_prealloc(mas, vma); 521d4af56c5SLiam R. Howlett } 522d4af56c5SLiam R. Howlett 523d4af56c5SLiam R. Howlett /* 524d4af56c5SLiam R. Howlett * vma_mas_remove() - Remove a VMA from the maple tree. 525d4af56c5SLiam R. Howlett * @vma: The vm_area_struct 526d4af56c5SLiam R. Howlett * @mas: The maple state 527d4af56c5SLiam R. Howlett * 528d4af56c5SLiam R. Howlett * Efficient way to remove a VMA from the maple tree when the @mas has already 529d4af56c5SLiam R. Howlett * been established and points to the correct location. 530d4af56c5SLiam R. Howlett * Note: the end address is inclusive in the maple tree. 531d4af56c5SLiam R. Howlett */ 532d4af56c5SLiam R. Howlett void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas) 533d4af56c5SLiam R. Howlett { 534d4af56c5SLiam R. Howlett trace_vma_mas_szero(mas->tree, vma->vm_start, vma->vm_end - 1); 535d4af56c5SLiam R. Howlett mas->index = vma->vm_start; 536d4af56c5SLiam R. Howlett mas->last = vma->vm_end - 1; 537d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 538d4af56c5SLiam R. Howlett } 539d4af56c5SLiam R. Howlett 540d4af56c5SLiam R. Howlett /* 541d4af56c5SLiam R. Howlett * vma_mas_szero() - Set a given range to zero. Used when modifying a 542d4af56c5SLiam R. Howlett * vm_area_struct start or end. 543d4af56c5SLiam R. Howlett * 544d4af56c5SLiam R. Howlett * @mm: The struct_mm 545d4af56c5SLiam R. Howlett * @start: The start address to zero 546d4af56c5SLiam R. Howlett * @end: The end address to zero. 547d4af56c5SLiam R. Howlett */ 548d4af56c5SLiam R. Howlett static inline void vma_mas_szero(struct ma_state *mas, unsigned long start, 549d4af56c5SLiam R. Howlett unsigned long end) 550d4af56c5SLiam R. Howlett { 551d4af56c5SLiam R. Howlett trace_vma_mas_szero(mas->tree, start, end - 1); 552d4af56c5SLiam R. Howlett mas_set_range(mas, start, end - 1); 553d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 554d4af56c5SLiam R. Howlett } 555d4af56c5SLiam R. Howlett 556d4af56c5SLiam R. Howlett static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma, 557524e00b3SLiam R. Howlett struct vm_area_struct *prev) 5581da177e4SLinus Torvalds { 559d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 5601da177e4SLinus Torvalds struct address_space *mapping = NULL; 5611da177e4SLinus Torvalds 562d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 563d4af56c5SLiam R. Howlett return -ENOMEM; 564d4af56c5SLiam R. Howlett 56564ac4940SHuang Shijie if (vma->vm_file) { 5661da177e4SLinus Torvalds mapping = vma->vm_file->f_mapping; 56783cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 56864ac4940SHuang Shijie } 5691da177e4SLinus Torvalds 570d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 571524e00b3SLiam R. Howlett __vma_link_list(mm, vma, prev); 5721da177e4SLinus Torvalds __vma_link_file(vma); 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds if (mapping) 57583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds mm->map_count++; 5781da177e4SLinus Torvalds validate_mm(mm); 579d4af56c5SLiam R. Howlett return 0; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 58388f6b4c3SKautuk Consul * Helper for vma_adjust() in the split_vma insert case: insert a vma into the 584524e00b3SLiam R. Howlett * mm's list and the mm tree. It has already been inserted into the interval tree. 5851da177e4SLinus Torvalds */ 586d4af56c5SLiam R. Howlett static void __insert_vm_struct(struct mm_struct *mm, struct ma_state *mas, 5873b0e81a1SLiam R. Howlett struct vm_area_struct *vma, unsigned long location) 5881da177e4SLinus Torvalds { 5896597d783SHugh Dickins struct vm_area_struct *prev; 5901da177e4SLinus Torvalds 5913b0e81a1SLiam R. Howlett mas_set(mas, location); 592524e00b3SLiam R. Howlett prev = mas_prev(mas, 0); 593d4af56c5SLiam R. Howlett vma_mas_store(vma, mas); 594d4af56c5SLiam R. Howlett __vma_link_list(mm, vma, prev); 5951da177e4SLinus Torvalds mm->map_count++; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* 5994dd1b841SLiam R. Howlett * vma_expand - Expand an existing VMA 6004dd1b841SLiam R. Howlett * 6014dd1b841SLiam R. Howlett * @mas: The maple state 6024dd1b841SLiam R. Howlett * @vma: The vma to expand 6034dd1b841SLiam R. Howlett * @start: The start of the vma 6044dd1b841SLiam R. Howlett * @end: The exclusive end of the vma 6054dd1b841SLiam R. Howlett * @pgoff: The page offset of vma 6064dd1b841SLiam R. Howlett * @next: The current of next vma. 6074dd1b841SLiam R. Howlett * 6084dd1b841SLiam R. Howlett * Expand @vma to @start and @end. Can expand off the start and end. Will 6094dd1b841SLiam R. Howlett * expand over @next if it's different from @vma and @end == @next->vm_end. 6104dd1b841SLiam R. Howlett * Checking if the @vma can expand and merge with @next needs to be handled by 6114dd1b841SLiam R. Howlett * the caller. 6124dd1b841SLiam R. Howlett * 6134dd1b841SLiam R. Howlett * Returns: 0 on success 6144dd1b841SLiam R. Howlett */ 6154dd1b841SLiam R. Howlett inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma, 6164dd1b841SLiam R. Howlett unsigned long start, unsigned long end, pgoff_t pgoff, 6174dd1b841SLiam R. Howlett struct vm_area_struct *next) 6184dd1b841SLiam R. Howlett { 6194dd1b841SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 6204dd1b841SLiam R. Howlett struct address_space *mapping = NULL; 6214dd1b841SLiam R. Howlett struct rb_root_cached *root = NULL; 6224dd1b841SLiam R. Howlett struct anon_vma *anon_vma = vma->anon_vma; 6234dd1b841SLiam R. Howlett struct file *file = vma->vm_file; 6244dd1b841SLiam R. Howlett bool remove_next = false; 6254dd1b841SLiam R. Howlett 6264dd1b841SLiam R. Howlett if (next && (vma != next) && (end == next->vm_end)) { 6274dd1b841SLiam R. Howlett remove_next = true; 6284dd1b841SLiam R. Howlett if (next->anon_vma && !vma->anon_vma) { 6294dd1b841SLiam R. Howlett int error; 6304dd1b841SLiam R. Howlett 6314dd1b841SLiam R. Howlett anon_vma = next->anon_vma; 6324dd1b841SLiam R. Howlett vma->anon_vma = anon_vma; 6334dd1b841SLiam R. Howlett error = anon_vma_clone(vma, next); 6344dd1b841SLiam R. Howlett if (error) 6354dd1b841SLiam R. Howlett return error; 6364dd1b841SLiam R. Howlett } 6374dd1b841SLiam R. Howlett } 6384dd1b841SLiam R. Howlett 6394dd1b841SLiam R. Howlett /* Not merging but overwriting any part of next is not handled. */ 6404dd1b841SLiam R. Howlett VM_BUG_ON(next && !remove_next && next != vma && end > next->vm_start); 6414dd1b841SLiam R. Howlett /* Only handles expanding */ 6424dd1b841SLiam R. Howlett VM_BUG_ON(vma->vm_start < start || vma->vm_end > end); 6434dd1b841SLiam R. Howlett 6444dd1b841SLiam R. Howlett if (mas_preallocate(mas, vma, GFP_KERNEL)) 6454dd1b841SLiam R. Howlett goto nomem; 6464dd1b841SLiam R. Howlett 6474dd1b841SLiam R. Howlett vma_adjust_trans_huge(vma, start, end, 0); 6484dd1b841SLiam R. Howlett 6494dd1b841SLiam R. Howlett if (file) { 6504dd1b841SLiam R. Howlett mapping = file->f_mapping; 6514dd1b841SLiam R. Howlett root = &mapping->i_mmap; 6524dd1b841SLiam R. Howlett uprobe_munmap(vma, vma->vm_start, vma->vm_end); 6534dd1b841SLiam R. Howlett i_mmap_lock_write(mapping); 6544dd1b841SLiam R. Howlett } 6554dd1b841SLiam R. Howlett 6564dd1b841SLiam R. Howlett if (anon_vma) { 6574dd1b841SLiam R. Howlett anon_vma_lock_write(anon_vma); 6584dd1b841SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 6594dd1b841SLiam R. Howlett } 6604dd1b841SLiam R. Howlett 6614dd1b841SLiam R. Howlett if (file) { 6624dd1b841SLiam R. Howlett flush_dcache_mmap_lock(mapping); 6634dd1b841SLiam R. Howlett vma_interval_tree_remove(vma, root); 6644dd1b841SLiam R. Howlett } 6654dd1b841SLiam R. Howlett 6664dd1b841SLiam R. Howlett vma->vm_start = start; 6674dd1b841SLiam R. Howlett vma->vm_end = end; 6684dd1b841SLiam R. Howlett vma->vm_pgoff = pgoff; 6694dd1b841SLiam R. Howlett /* Note: mas must be pointing to the expanding VMA */ 6704dd1b841SLiam R. Howlett vma_mas_store(vma, mas); 6714dd1b841SLiam R. Howlett 6724dd1b841SLiam R. Howlett if (file) { 6734dd1b841SLiam R. Howlett vma_interval_tree_insert(vma, root); 6744dd1b841SLiam R. Howlett flush_dcache_mmap_unlock(mapping); 6754dd1b841SLiam R. Howlett } 6764dd1b841SLiam R. Howlett 6774dd1b841SLiam R. Howlett /* Expanding over the next vma */ 6784dd1b841SLiam R. Howlett if (remove_next) { 6794dd1b841SLiam R. Howlett /* Remove from mm linked list - also updates highest_vm_end */ 6804dd1b841SLiam R. Howlett __vma_unlink_list(mm, next); 6814dd1b841SLiam R. Howlett 6824dd1b841SLiam R. Howlett if (file) 6834dd1b841SLiam R. Howlett __remove_shared_vm_struct(next, file, mapping); 6844dd1b841SLiam R. Howlett 6854dd1b841SLiam R. Howlett } else if (!next) { 6864dd1b841SLiam R. Howlett mm->highest_vm_end = vm_end_gap(vma); 6874dd1b841SLiam R. Howlett } 6884dd1b841SLiam R. Howlett 6894dd1b841SLiam R. Howlett if (anon_vma) { 6904dd1b841SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 6914dd1b841SLiam R. Howlett anon_vma_unlock_write(anon_vma); 6924dd1b841SLiam R. Howlett } 6934dd1b841SLiam R. Howlett 6944dd1b841SLiam R. Howlett if (file) { 6954dd1b841SLiam R. Howlett i_mmap_unlock_write(mapping); 6964dd1b841SLiam R. Howlett uprobe_mmap(vma); 6974dd1b841SLiam R. Howlett } 6984dd1b841SLiam R. Howlett 6994dd1b841SLiam R. Howlett if (remove_next) { 7004dd1b841SLiam R. Howlett if (file) { 7014dd1b841SLiam R. Howlett uprobe_munmap(next, next->vm_start, next->vm_end); 7024dd1b841SLiam R. Howlett fput(file); 7034dd1b841SLiam R. Howlett } 7044dd1b841SLiam R. Howlett if (next->anon_vma) 7054dd1b841SLiam R. Howlett anon_vma_merge(vma, next); 7064dd1b841SLiam R. Howlett mm->map_count--; 7074dd1b841SLiam R. Howlett mpol_put(vma_policy(next)); 7084dd1b841SLiam R. Howlett vm_area_free(next); 7094dd1b841SLiam R. Howlett } 7104dd1b841SLiam R. Howlett 7114dd1b841SLiam R. Howlett validate_mm(mm); 7124dd1b841SLiam R. Howlett return 0; 7134dd1b841SLiam R. Howlett 7144dd1b841SLiam R. Howlett nomem: 7154dd1b841SLiam R. Howlett return -ENOMEM; 7164dd1b841SLiam R. Howlett } 7174dd1b841SLiam R. Howlett 7184dd1b841SLiam R. Howlett /* 7191da177e4SLinus Torvalds * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that 7201da177e4SLinus Torvalds * is already present in an i_mmap tree without adjusting the tree. 7211da177e4SLinus Torvalds * The following helper function should be used when such adjustments 7221da177e4SLinus Torvalds * are necessary. The "insert" vma (if any) is to be inserted 7231da177e4SLinus Torvalds * before we drop the necessary locks. 7241da177e4SLinus Torvalds */ 725e86f15eeSAndrea Arcangeli int __vma_adjust(struct vm_area_struct *vma, unsigned long start, 726e86f15eeSAndrea Arcangeli unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert, 727e86f15eeSAndrea Arcangeli struct vm_area_struct *expand) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 730524e00b3SLiam R. Howlett struct vm_area_struct *next_next, *next = find_vma(mm, vma->vm_end); 731524e00b3SLiam R. Howlett struct vm_area_struct *orig_vma = vma; 7321da177e4SLinus Torvalds struct address_space *mapping = NULL; 733f808c13fSDavidlohr Bueso struct rb_root_cached *root = NULL; 734012f1800SRik van Riel struct anon_vma *anon_vma = NULL; 7351da177e4SLinus Torvalds struct file *file = vma->vm_file; 736524e00b3SLiam R. Howlett bool vma_changed = false; 7371da177e4SLinus Torvalds long adjust_next = 0; 7381da177e4SLinus Torvalds int remove_next = 0; 739d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 740734537c9SKirill A. Shutemov struct vm_area_struct *exporter = NULL, *importer = NULL; 7413b0e81a1SLiam R. Howlett unsigned long ll_prev = vma->vm_start; /* linked list prev. */ 742287d97acSLinus Torvalds 743d4af56c5SLiam R. Howlett if (next && !insert) { 7441da177e4SLinus Torvalds if (end >= next->vm_end) { 7451da177e4SLinus Torvalds /* 7461da177e4SLinus Torvalds * vma expands, overlapping all the next, and 7471da177e4SLinus Torvalds * perhaps the one after too (mprotect case 6). 74886d12e47SAndrea Arcangeli * The only other cases that gets here are 749e86f15eeSAndrea Arcangeli * case 1, case 7 and case 8. 750e86f15eeSAndrea Arcangeli */ 751e86f15eeSAndrea Arcangeli if (next == expand) { 752e86f15eeSAndrea Arcangeli /* 753e86f15eeSAndrea Arcangeli * The only case where we don't expand "vma" 754e86f15eeSAndrea Arcangeli * and we expand "next" instead is case 8. 755e86f15eeSAndrea Arcangeli */ 756e86f15eeSAndrea Arcangeli VM_WARN_ON(end != next->vm_end); 757e86f15eeSAndrea Arcangeli /* 758e86f15eeSAndrea Arcangeli * remove_next == 3 means we're 759e86f15eeSAndrea Arcangeli * removing "vma" and that to do so we 760e86f15eeSAndrea Arcangeli * swapped "vma" and "next". 761e86f15eeSAndrea Arcangeli */ 762e86f15eeSAndrea Arcangeli remove_next = 3; 763e86f15eeSAndrea Arcangeli VM_WARN_ON(file != next->vm_file); 764e86f15eeSAndrea Arcangeli swap(vma, next); 765e86f15eeSAndrea Arcangeli } else { 766e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != vma); 767e86f15eeSAndrea Arcangeli /* 768e86f15eeSAndrea Arcangeli * case 1, 6, 7, remove_next == 2 is case 6, 769e86f15eeSAndrea Arcangeli * remove_next == 1 is case 1 or 7. 7701da177e4SLinus Torvalds */ 771734537c9SKirill A. Shutemov remove_next = 1 + (end > next->vm_end); 772d4af56c5SLiam R. Howlett if (remove_next == 2) 773d4af56c5SLiam R. Howlett next_next = find_vma(mm, next->vm_end); 774d4af56c5SLiam R. Howlett 775e86f15eeSAndrea Arcangeli VM_WARN_ON(remove_next == 2 && 776e86f15eeSAndrea Arcangeli end != next->vm_next->vm_end); 777e86f15eeSAndrea Arcangeli } 778e86f15eeSAndrea Arcangeli 779287d97acSLinus Torvalds exporter = next; 7801da177e4SLinus Torvalds importer = vma; 781734537c9SKirill A. Shutemov 782734537c9SKirill A. Shutemov /* 783734537c9SKirill A. Shutemov * If next doesn't have anon_vma, import from vma after 784734537c9SKirill A. Shutemov * next, if the vma overlaps with it. 785734537c9SKirill A. Shutemov */ 78697a42cd4SAndrea Arcangeli if (remove_next == 2 && !next->anon_vma) 787734537c9SKirill A. Shutemov exporter = next->vm_next; 788734537c9SKirill A. Shutemov 7891da177e4SLinus Torvalds } else if (end > next->vm_start) { 7901da177e4SLinus Torvalds /* 7911da177e4SLinus Torvalds * vma expands, overlapping part of the next: 7921da177e4SLinus Torvalds * mprotect case 5 shifting the boundary up. 7931da177e4SLinus Torvalds */ 794f9d86a60SWei Yang adjust_next = (end - next->vm_start); 795287d97acSLinus Torvalds exporter = next; 7961da177e4SLinus Torvalds importer = vma; 797e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 7981da177e4SLinus Torvalds } else if (end < vma->vm_end) { 7991da177e4SLinus Torvalds /* 8001da177e4SLinus Torvalds * vma shrinks, and !insert tells it's not 8011da177e4SLinus Torvalds * split_vma inserting another: so it must be 8021da177e4SLinus Torvalds * mprotect case 4 shifting the boundary down. 8031da177e4SLinus Torvalds */ 804f9d86a60SWei Yang adjust_next = -(vma->vm_end - end); 805287d97acSLinus Torvalds exporter = vma; 8061da177e4SLinus Torvalds importer = next; 807e86f15eeSAndrea Arcangeli VM_WARN_ON(expand != importer); 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 8105beb4930SRik van Riel /* 8115beb4930SRik van Riel * Easily overlooked: when mprotect shifts the boundary, 8125beb4930SRik van Riel * make sure the expanding vma has anon_vma set if the 8135beb4930SRik van Riel * shrinking vma had, to cover any anon pages imported. 8145beb4930SRik van Riel */ 815287d97acSLinus Torvalds if (exporter && exporter->anon_vma && !importer->anon_vma) { 816c4ea95d7SDaniel Forrest int error; 817c4ea95d7SDaniel Forrest 818287d97acSLinus Torvalds importer->anon_vma = exporter->anon_vma; 819b800c91aSKonstantin Khlebnikov error = anon_vma_clone(importer, exporter); 8203fe89b3eSLeon Yu if (error) 821b800c91aSKonstantin Khlebnikov return error; 822b800c91aSKonstantin Khlebnikov } 8235beb4930SRik van Riel } 82437f9f559SKirill A. Shutemov 825d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 826d4af56c5SLiam R. Howlett return -ENOMEM; 827d4af56c5SLiam R. Howlett 828d4af56c5SLiam R. Howlett vma_adjust_trans_huge(orig_vma, start, end, adjust_next); 8291da177e4SLinus Torvalds if (file) { 8301da177e4SLinus Torvalds mapping = file->f_mapping; 8311da177e4SLinus Torvalds root = &mapping->i_mmap; 832cbc91f71SSrikar Dronamraju uprobe_munmap(vma, vma->vm_start, vma->vm_end); 833682968e0SSrikar Dronamraju 834682968e0SSrikar Dronamraju if (adjust_next) 83527ba0644SKirill A. Shutemov uprobe_munmap(next, next->vm_start, next->vm_end); 836682968e0SSrikar Dronamraju 83783cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 8381da177e4SLinus Torvalds if (insert) { 8391da177e4SLinus Torvalds /* 8406b2dbba8SMichel Lespinasse * Put into interval tree now, so instantiated pages 8411da177e4SLinus Torvalds * are visible to arm/parisc __flush_dcache_page 8421da177e4SLinus Torvalds * throughout; but we cannot insert into address 8431da177e4SLinus Torvalds * space until vma start or end is updated. 8441da177e4SLinus Torvalds */ 8451da177e4SLinus Torvalds __vma_link_file(insert); 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 849012f1800SRik van Riel anon_vma = vma->anon_vma; 850bf181b9fSMichel Lespinasse if (!anon_vma && adjust_next) 851bf181b9fSMichel Lespinasse anon_vma = next->anon_vma; 852bf181b9fSMichel Lespinasse if (anon_vma) { 853e86f15eeSAndrea Arcangeli VM_WARN_ON(adjust_next && next->anon_vma && 854e86f15eeSAndrea Arcangeli anon_vma != next->anon_vma); 8554fc3f1d6SIngo Molnar anon_vma_lock_write(anon_vma); 856bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 857bf181b9fSMichel Lespinasse if (adjust_next) 858bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(next); 859bf181b9fSMichel Lespinasse } 860012f1800SRik van Riel 8610fc48a6eSWei Yang if (file) { 8621da177e4SLinus Torvalds flush_dcache_mmap_lock(mapping); 8636b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, root); 8641da177e4SLinus Torvalds if (adjust_next) 8656b2dbba8SMichel Lespinasse vma_interval_tree_remove(next, root); 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 868d3737187SMichel Lespinasse if (start != vma->vm_start) { 8693b0e81a1SLiam R. Howlett if ((vma->vm_start < start) && 8703b0e81a1SLiam R. Howlett (!insert || (insert->vm_end != start))) { 871524e00b3SLiam R. Howlett vma_mas_szero(&mas, vma->vm_start, start); 8723b0e81a1SLiam R. Howlett VM_WARN_ON(insert && insert->vm_start > vma->vm_start); 8733b0e81a1SLiam R. Howlett } else { 874524e00b3SLiam R. Howlett vma_changed = true; 8753b0e81a1SLiam R. Howlett } 8761da177e4SLinus Torvalds vma->vm_start = start; 877d3737187SMichel Lespinasse } 878d3737187SMichel Lespinasse if (end != vma->vm_end) { 8793b0e81a1SLiam R. Howlett if (vma->vm_end > end) { 8803b0e81a1SLiam R. Howlett if (!insert || (insert->vm_start != end)) { 881524e00b3SLiam R. Howlett vma_mas_szero(&mas, end, vma->vm_end); 8823b0e81a1SLiam R. Howlett VM_WARN_ON(insert && 8833b0e81a1SLiam R. Howlett insert->vm_end < vma->vm_end); 8843b0e81a1SLiam R. Howlett } else if (insert->vm_start == end) { 8853b0e81a1SLiam R. Howlett ll_prev = vma->vm_end; 8863b0e81a1SLiam R. Howlett } 8873b0e81a1SLiam R. Howlett } else { 888524e00b3SLiam R. Howlett vma_changed = true; 8893b0e81a1SLiam R. Howlett } 8901da177e4SLinus Torvalds vma->vm_end = end; 891524e00b3SLiam R. Howlett if (!next) 892524e00b3SLiam R. Howlett mm->highest_vm_end = vm_end_gap(vma); 893d3737187SMichel Lespinasse } 894d4af56c5SLiam R. Howlett 895524e00b3SLiam R. Howlett if (vma_changed) 896d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 897d4af56c5SLiam R. Howlett 8981da177e4SLinus Torvalds vma->vm_pgoff = pgoff; 8991da177e4SLinus Torvalds if (adjust_next) { 900f9d86a60SWei Yang next->vm_start += adjust_next; 901f9d86a60SWei Yang next->vm_pgoff += adjust_next >> PAGE_SHIFT; 902d4af56c5SLiam R. Howlett vma_mas_store(next, &mas); 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 9050fc48a6eSWei Yang if (file) { 9061da177e4SLinus Torvalds if (adjust_next) 9076b2dbba8SMichel Lespinasse vma_interval_tree_insert(next, root); 9086b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, root); 9091da177e4SLinus Torvalds flush_dcache_mmap_unlock(mapping); 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds if (remove_next) { 9131da177e4SLinus Torvalds /* 9141da177e4SLinus Torvalds * vma_merge has merged next into vma, and needs 9151da177e4SLinus Torvalds * us to remove next before dropping the locks. 916d4af56c5SLiam R. Howlett * Since we have expanded over this vma, the maple tree will 917d4af56c5SLiam R. Howlett * have overwritten by storing the value 9181da177e4SLinus Torvalds */ 919524e00b3SLiam R. Howlett __vma_unlink_list(mm, next); 920d4af56c5SLiam R. Howlett if (remove_next == 2) 921524e00b3SLiam R. Howlett __vma_unlink_list(mm, next_next); 922524e00b3SLiam R. Howlett 923d4af56c5SLiam R. Howlett if (file) { 9241da177e4SLinus Torvalds __remove_shared_vm_struct(next, file, mapping); 925d4af56c5SLiam R. Howlett if (remove_next == 2) 926d4af56c5SLiam R. Howlett __remove_shared_vm_struct(next_next, file, mapping); 927d4af56c5SLiam R. Howlett } 9281da177e4SLinus Torvalds } else if (insert) { 9291da177e4SLinus Torvalds /* 9301da177e4SLinus Torvalds * split_vma has split insert from vma, and needs 9311da177e4SLinus Torvalds * us to insert it before dropping the locks 9321da177e4SLinus Torvalds * (it may either follow vma or precede it). 9331da177e4SLinus Torvalds */ 9343b0e81a1SLiam R. Howlett __insert_vm_struct(mm, &mas, insert, ll_prev); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937bf181b9fSMichel Lespinasse if (anon_vma) { 938bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 939bf181b9fSMichel Lespinasse if (adjust_next) 940bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(next); 94108b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 942bf181b9fSMichel Lespinasse } 9431da177e4SLinus Torvalds 9440fc48a6eSWei Yang if (file) { 945808fbdbeSWei Yang i_mmap_unlock_write(mapping); 9467b2d81d4SIngo Molnar uprobe_mmap(vma); 9472b144498SSrikar Dronamraju 9482b144498SSrikar Dronamraju if (adjust_next) 9497b2d81d4SIngo Molnar uprobe_mmap(next); 9502b144498SSrikar Dronamraju } 9512b144498SSrikar Dronamraju 9521da177e4SLinus Torvalds if (remove_next) { 953d4af56c5SLiam R. Howlett again: 954925d1c40SMatt Helsley if (file) { 955cbc91f71SSrikar Dronamraju uprobe_munmap(next, next->vm_start, next->vm_end); 9561da177e4SLinus Torvalds fput(file); 957925d1c40SMatt Helsley } 9585beb4930SRik van Riel if (next->anon_vma) 9595beb4930SRik van Riel anon_vma_merge(vma, next); 9601da177e4SLinus Torvalds mm->map_count--; 9613964acd0SOleg Nesterov mpol_put(vma_policy(next)); 962524e00b3SLiam R. Howlett if (remove_next != 2) 963524e00b3SLiam R. Howlett BUG_ON(vma->vm_end < next->vm_end); 9643928d4f5SLinus Torvalds vm_area_free(next); 965524e00b3SLiam R. Howlett 9661da177e4SLinus Torvalds /* 9671da177e4SLinus Torvalds * In mprotect's case 6 (see comments on vma_merge), 9681da177e4SLinus Torvalds * we must remove another next too. It would clutter 9691da177e4SLinus Torvalds * up the code too much to do both in one go. 9701da177e4SLinus Torvalds */ 971e86f15eeSAndrea Arcangeli if (remove_next != 3) { 972e86f15eeSAndrea Arcangeli /* 973e86f15eeSAndrea Arcangeli * If "next" was removed and vma->vm_end was 974e86f15eeSAndrea Arcangeli * expanded (up) over it, in turn 975e86f15eeSAndrea Arcangeli * "next->vm_prev->vm_end" changed and the 976e86f15eeSAndrea Arcangeli * "vma->vm_next" gap must be updated. 977e86f15eeSAndrea Arcangeli */ 978d4af56c5SLiam R. Howlett next = next_next; 979e86f15eeSAndrea Arcangeli } else { 980e86f15eeSAndrea Arcangeli /* 981e86f15eeSAndrea Arcangeli * For the scope of the comment "next" and 982e86f15eeSAndrea Arcangeli * "vma" considered pre-swap(): if "vma" was 983e86f15eeSAndrea Arcangeli * removed, next->vm_start was expanded (down) 984e86f15eeSAndrea Arcangeli * over it and the "next" gap must be updated. 985e86f15eeSAndrea Arcangeli * Because of the swap() the post-swap() "vma" 986e86f15eeSAndrea Arcangeli * actually points to pre-swap() "next" 987e86f15eeSAndrea Arcangeli * (post-swap() "next" as opposed is now a 988e86f15eeSAndrea Arcangeli * dangling pointer). 989e86f15eeSAndrea Arcangeli */ 990e86f15eeSAndrea Arcangeli next = vma; 991e86f15eeSAndrea Arcangeli } 992734537c9SKirill A. Shutemov if (remove_next == 2) { 993734537c9SKirill A. Shutemov remove_next = 1; 9941da177e4SLinus Torvalds goto again; 995524e00b3SLiam R. Howlett } else if (!next) { 996fb8c41e9SAndrea Arcangeli /* 997fb8c41e9SAndrea Arcangeli * If remove_next == 2 we obviously can't 998fb8c41e9SAndrea Arcangeli * reach this path. 999fb8c41e9SAndrea Arcangeli * 1000fb8c41e9SAndrea Arcangeli * If remove_next == 3 we can't reach this 1001fb8c41e9SAndrea Arcangeli * path because pre-swap() next is always not 1002fb8c41e9SAndrea Arcangeli * NULL. pre-swap() "next" is not being 1003fb8c41e9SAndrea Arcangeli * removed and its next->vm_end is not altered 1004fb8c41e9SAndrea Arcangeli * (and furthermore "end" already matches 1005fb8c41e9SAndrea Arcangeli * next->vm_end in remove_next == 3). 1006fb8c41e9SAndrea Arcangeli * 1007fb8c41e9SAndrea Arcangeli * We reach this only in the remove_next == 1 1008fb8c41e9SAndrea Arcangeli * case if the "next" vma that was removed was 1009fb8c41e9SAndrea Arcangeli * the highest vma of the mm. However in such 1010fb8c41e9SAndrea Arcangeli * case next->vm_end == "end" and the extended 1011fb8c41e9SAndrea Arcangeli * "vma" has vma->vm_end == next->vm_end so 1012fb8c41e9SAndrea Arcangeli * mm->highest_vm_end doesn't need any update 1013fb8c41e9SAndrea Arcangeli * in remove_next == 1 case. 1014fb8c41e9SAndrea Arcangeli */ 10151be7107fSHugh Dickins VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma)); 1016fb8c41e9SAndrea Arcangeli } 10171da177e4SLinus Torvalds } 10182b144498SSrikar Dronamraju if (insert && file) 10197b2d81d4SIngo Molnar uprobe_mmap(insert); 10201da177e4SLinus Torvalds 10213b0e81a1SLiam R. Howlett mas_destroy(&mas); 10221da177e4SLinus Torvalds validate_mm(mm); 10235beb4930SRik van Riel return 0; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 10271da177e4SLinus Torvalds * If the vma has a ->close operation then the driver probably needs to release 10281da177e4SLinus Torvalds * per-vma resources, so we don't attempt to merge those. 10291da177e4SLinus Torvalds */ 10301da177e4SLinus Torvalds static inline int is_mergeable_vma(struct vm_area_struct *vma, 103119a809afSAndrea Arcangeli struct file *file, unsigned long vm_flags, 10329a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 10335c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 10341da177e4SLinus Torvalds { 103534228d47SCyrill Gorcunov /* 103634228d47SCyrill Gorcunov * VM_SOFTDIRTY should not prevent from VMA merging, if we 103734228d47SCyrill Gorcunov * match the flags but dirty bit -- the caller should mark 103834228d47SCyrill Gorcunov * merged VMA as dirty. If dirty bit won't be excluded from 10398bb4e7a2SWei Yang * comparison, we increase pressure on the memory system forcing 104034228d47SCyrill Gorcunov * the kernel to generate new VMAs when old one could be 104134228d47SCyrill Gorcunov * extended instead. 104234228d47SCyrill Gorcunov */ 104334228d47SCyrill Gorcunov if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 10441da177e4SLinus Torvalds return 0; 10451da177e4SLinus Torvalds if (vma->vm_file != file) 10461da177e4SLinus Torvalds return 0; 10471da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->close) 10481da177e4SLinus Torvalds return 0; 104919a809afSAndrea Arcangeli if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 105019a809afSAndrea Arcangeli return 0; 10515c26f6acSSuren Baghdasaryan if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 10529a10064fSColin Cross return 0; 10531da177e4SLinus Torvalds return 1; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, 1057965f55deSShaohua Li struct anon_vma *anon_vma2, 1058965f55deSShaohua Li struct vm_area_struct *vma) 10591da177e4SLinus Torvalds { 1060965f55deSShaohua Li /* 1061965f55deSShaohua Li * The list_is_singular() test is to avoid merging VMA cloned from 1062965f55deSShaohua Li * parents. This can improve scalability caused by anon_vma lock. 1063965f55deSShaohua Li */ 1064965f55deSShaohua Li if ((!anon_vma1 || !anon_vma2) && (!vma || 1065965f55deSShaohua Li list_is_singular(&vma->anon_vma_chain))) 1066965f55deSShaohua Li return 1; 1067965f55deSShaohua Li return anon_vma1 == anon_vma2; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* 10711da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 10721da177e4SLinus Torvalds * in front of (at a lower virtual address and file offset than) the vma. 10731da177e4SLinus Torvalds * 10741da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 10751da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 10761da177e4SLinus Torvalds * 10771da177e4SLinus Torvalds * We don't check here for the merged mmap wrapping around the end of pagecache 107845e55300SPeter Collingbourne * indices (16TB on ia32) because do_mmap() does not permit mmap's which 10791da177e4SLinus Torvalds * wrap, nor mmaps which cover the final page at index -1UL. 10801da177e4SLinus Torvalds */ 10811da177e4SLinus Torvalds static int 10821da177e4SLinus Torvalds can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 108319a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 108419a809afSAndrea Arcangeli pgoff_t vm_pgoff, 10859a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 10865c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 10871da177e4SLinus Torvalds { 10889a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 1089965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 10901da177e4SLinus Torvalds if (vma->vm_pgoff == vm_pgoff) 10911da177e4SLinus Torvalds return 1; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds return 0; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds /* 10971da177e4SLinus Torvalds * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 10981da177e4SLinus Torvalds * beyond (at a higher virtual address and file offset than) the vma. 10991da177e4SLinus Torvalds * 11001da177e4SLinus Torvalds * We cannot merge two vmas if they have differently assigned (non-NULL) 11011da177e4SLinus Torvalds * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds static int 11041da177e4SLinus Torvalds can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 110519a809afSAndrea Arcangeli struct anon_vma *anon_vma, struct file *file, 110619a809afSAndrea Arcangeli pgoff_t vm_pgoff, 11079a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 11085c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 11091da177e4SLinus Torvalds { 11109a10064fSColin Cross if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && 1111965f55deSShaohua Li is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 11121da177e4SLinus Torvalds pgoff_t vm_pglen; 1113d6e93217SLibin vm_pglen = vma_pages(vma); 11141da177e4SLinus Torvalds if (vma->vm_pgoff + vm_pglen == vm_pgoff) 11151da177e4SLinus Torvalds return 1; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds return 0; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /* 11219a10064fSColin Cross * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 11229a10064fSColin Cross * figure out whether that can be merged with its predecessor or its 11239a10064fSColin Cross * successor. Or both (it neatly fills a hole). 11241da177e4SLinus Torvalds * 11251da177e4SLinus Torvalds * In most cases - when called for mmap, brk or mremap - [addr,end) is 11261da177e4SLinus Torvalds * certain not to be mapped by the time vma_merge is called; but when 11271da177e4SLinus Torvalds * called for mprotect, it is certain to be already mapped (either at 11281da177e4SLinus Torvalds * an offset within prev, or at the start of next), and the flags of 11291da177e4SLinus Torvalds * this area are about to be changed to vm_flags - and the no-change 11301da177e4SLinus Torvalds * case has already been eliminated. 11311da177e4SLinus Torvalds * 11321da177e4SLinus Torvalds * The following mprotect cases have to be considered, where AAAA is 11331da177e4SLinus Torvalds * the area passed down from mprotect_fixup, never extending beyond one 11341da177e4SLinus Torvalds * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: 11351da177e4SLinus Torvalds * 11365d42ab29SWei Yang * AAAA AAAA AAAA 11375d42ab29SWei Yang * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN 11385d42ab29SWei Yang * cannot merge might become might become 11395d42ab29SWei Yang * PPNNNNNNNNNN PPPPPPPPPPNN 11405d42ab29SWei Yang * mmap, brk or case 4 below case 5 below 11415d42ab29SWei Yang * mremap move: 11425d42ab29SWei Yang * AAAA AAAA 11435d42ab29SWei Yang * PPPP NNNN PPPPNNNNXXXX 11445d42ab29SWei Yang * might become might become 11455d42ab29SWei Yang * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 11465d42ab29SWei Yang * PPPPPPPPNNNN 2 or PPPPPPPPXXXX 7 or 11475d42ab29SWei Yang * PPPPNNNNNNNN 3 PPPPXXXXXXXX 8 11481da177e4SLinus Torvalds * 11498bb4e7a2SWei Yang * It is important for case 8 that the vma NNNN overlapping the 1150e86f15eeSAndrea Arcangeli * region AAAA is never going to extended over XXXX. Instead XXXX must 1151e86f15eeSAndrea Arcangeli * be extended in region AAAA and NNNN must be removed. This way in 1152e86f15eeSAndrea Arcangeli * all cases where vma_merge succeeds, the moment vma_adjust drops the 1153e86f15eeSAndrea Arcangeli * rmap_locks, the properties of the merged vma will be already 1154e86f15eeSAndrea Arcangeli * correct for the whole merged range. Some of those properties like 1155e86f15eeSAndrea Arcangeli * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 1156e86f15eeSAndrea Arcangeli * be correct for the whole merged range immediately after the 1157e86f15eeSAndrea Arcangeli * rmap_locks are released. Otherwise if XXXX would be removed and 1158e86f15eeSAndrea Arcangeli * NNNN would be extended over the XXXX range, remove_migration_ptes 1159e86f15eeSAndrea Arcangeli * or other rmap walkers (if working on addresses beyond the "end" 1160e86f15eeSAndrea Arcangeli * parameter) may establish ptes with the wrong permissions of NNNN 1161e86f15eeSAndrea Arcangeli * instead of the right permissions of XXXX. 11621da177e4SLinus Torvalds */ 11631da177e4SLinus Torvalds struct vm_area_struct *vma_merge(struct mm_struct *mm, 11641da177e4SLinus Torvalds struct vm_area_struct *prev, unsigned long addr, 11651da177e4SLinus Torvalds unsigned long end, unsigned long vm_flags, 11661da177e4SLinus Torvalds struct anon_vma *anon_vma, struct file *file, 116719a809afSAndrea Arcangeli pgoff_t pgoff, struct mempolicy *policy, 11689a10064fSColin Cross struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 11695c26f6acSSuren Baghdasaryan struct anon_vma_name *anon_name) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 11721da177e4SLinus Torvalds struct vm_area_struct *area, *next; 11735beb4930SRik van Riel int err; 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds /* 11761da177e4SLinus Torvalds * We later require that vma->vm_flags == vm_flags, 11771da177e4SLinus Torvalds * so this tests vma->vm_flags & VM_SPECIAL, too. 11781da177e4SLinus Torvalds */ 11791da177e4SLinus Torvalds if (vm_flags & VM_SPECIAL) 11801da177e4SLinus Torvalds return NULL; 11811da177e4SLinus Torvalds 1182f39af059SMatthew Wilcox (Oracle) next = __vma_next(mm, prev); 11831da177e4SLinus Torvalds area = next; 1184e86f15eeSAndrea Arcangeli if (area && area->vm_end == end) /* cases 6, 7, 8 */ 11851da177e4SLinus Torvalds next = next->vm_next; 11861da177e4SLinus Torvalds 1187e86f15eeSAndrea Arcangeli /* verify some invariant that must be enforced by the caller */ 1188e86f15eeSAndrea Arcangeli VM_WARN_ON(prev && addr <= prev->vm_start); 1189e86f15eeSAndrea Arcangeli VM_WARN_ON(area && end > area->vm_end); 1190e86f15eeSAndrea Arcangeli VM_WARN_ON(addr >= end); 1191e86f15eeSAndrea Arcangeli 11921da177e4SLinus Torvalds /* 11931da177e4SLinus Torvalds * Can it merge with the predecessor? 11941da177e4SLinus Torvalds */ 11951da177e4SLinus Torvalds if (prev && prev->vm_end == addr && 11961da177e4SLinus Torvalds mpol_equal(vma_policy(prev), policy) && 11971da177e4SLinus Torvalds can_vma_merge_after(prev, vm_flags, 119819a809afSAndrea Arcangeli anon_vma, file, pgoff, 11999a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * OK, it can. Can we now merge in the successor as well? 12021da177e4SLinus Torvalds */ 12031da177e4SLinus Torvalds if (next && end == next->vm_start && 12041da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 12051da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 120619a809afSAndrea Arcangeli anon_vma, file, 120719a809afSAndrea Arcangeli pgoff+pglen, 12089a10064fSColin Cross vm_userfaultfd_ctx, anon_name) && 12091da177e4SLinus Torvalds is_mergeable_anon_vma(prev->anon_vma, 1210965f55deSShaohua Li next->anon_vma, NULL)) { 12111da177e4SLinus Torvalds /* cases 1, 6 */ 1212e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1213e86f15eeSAndrea Arcangeli next->vm_end, prev->vm_pgoff, NULL, 1214e86f15eeSAndrea Arcangeli prev); 12151da177e4SLinus Torvalds } else /* cases 2, 5, 7 */ 1216e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1217e86f15eeSAndrea Arcangeli end, prev->vm_pgoff, NULL, prev); 12185beb4930SRik van Riel if (err) 12195beb4930SRik van Riel return NULL; 1220c791576cSYang Shi khugepaged_enter_vma(prev, vm_flags); 12211da177e4SLinus Torvalds return prev; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds /* 12251da177e4SLinus Torvalds * Can this new request be merged in front of next? 12261da177e4SLinus Torvalds */ 12271da177e4SLinus Torvalds if (next && end == next->vm_start && 12281da177e4SLinus Torvalds mpol_equal(policy, vma_policy(next)) && 12291da177e4SLinus Torvalds can_vma_merge_before(next, vm_flags, 123019a809afSAndrea Arcangeli anon_vma, file, pgoff+pglen, 12319a10064fSColin Cross vm_userfaultfd_ctx, anon_name)) { 12321da177e4SLinus Torvalds if (prev && addr < prev->vm_end) /* case 4 */ 1233e86f15eeSAndrea Arcangeli err = __vma_adjust(prev, prev->vm_start, 1234e86f15eeSAndrea Arcangeli addr, prev->vm_pgoff, NULL, next); 1235e86f15eeSAndrea Arcangeli else { /* cases 3, 8 */ 1236e86f15eeSAndrea Arcangeli err = __vma_adjust(area, addr, next->vm_end, 1237e86f15eeSAndrea Arcangeli next->vm_pgoff - pglen, NULL, next); 1238e86f15eeSAndrea Arcangeli /* 1239e86f15eeSAndrea Arcangeli * In case 3 area is already equal to next and 1240e86f15eeSAndrea Arcangeli * this is a noop, but in case 8 "area" has 1241e86f15eeSAndrea Arcangeli * been removed and next was expanded over it. 1242e86f15eeSAndrea Arcangeli */ 1243e86f15eeSAndrea Arcangeli area = next; 1244e86f15eeSAndrea Arcangeli } 12455beb4930SRik van Riel if (err) 12465beb4930SRik van Riel return NULL; 1247c791576cSYang Shi khugepaged_enter_vma(area, vm_flags); 12481da177e4SLinus Torvalds return area; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds return NULL; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds /* 1255b4f315b4SEthon Paul * Rough compatibility check to quickly see if it's even worth looking 1256d0e9fe17SLinus Torvalds * at sharing an anon_vma. 1257d0e9fe17SLinus Torvalds * 1258d0e9fe17SLinus Torvalds * They need to have the same vm_file, and the flags can only differ 1259d0e9fe17SLinus Torvalds * in things that mprotect may change. 1260d0e9fe17SLinus Torvalds * 1261d0e9fe17SLinus Torvalds * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1262d0e9fe17SLinus Torvalds * we can merge the two vma's. For example, we refuse to merge a vma if 1263d0e9fe17SLinus Torvalds * there is a vm_ops->close() function, because that indicates that the 1264d0e9fe17SLinus Torvalds * driver is doing some kind of reference counting. But that doesn't 1265d0e9fe17SLinus Torvalds * really matter for the anon_vma sharing case. 1266d0e9fe17SLinus Torvalds */ 1267d0e9fe17SLinus Torvalds static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1268d0e9fe17SLinus Torvalds { 1269d0e9fe17SLinus Torvalds return a->vm_end == b->vm_start && 1270d0e9fe17SLinus Torvalds mpol_equal(vma_policy(a), vma_policy(b)) && 1271d0e9fe17SLinus Torvalds a->vm_file == b->vm_file && 12726cb4d9a2SAnshuman Khandual !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1273d0e9fe17SLinus Torvalds b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1274d0e9fe17SLinus Torvalds } 1275d0e9fe17SLinus Torvalds 1276d0e9fe17SLinus Torvalds /* 1277d0e9fe17SLinus Torvalds * Do some basic sanity checking to see if we can re-use the anon_vma 1278d0e9fe17SLinus Torvalds * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1279d0e9fe17SLinus Torvalds * the same as 'old', the other will be the new one that is trying 1280d0e9fe17SLinus Torvalds * to share the anon_vma. 1281d0e9fe17SLinus Torvalds * 12825b449489SFlorian Rommel * NOTE! This runs with mmap_lock held for reading, so it is possible that 1283d0e9fe17SLinus Torvalds * the anon_vma of 'old' is concurrently in the process of being set up 1284d0e9fe17SLinus Torvalds * by another page fault trying to merge _that_. But that's ok: if it 1285d0e9fe17SLinus Torvalds * is being set up, that automatically means that it will be a singleton 1286d0e9fe17SLinus Torvalds * acceptable for merging, so we can do all of this optimistically. But 12874db0c3c2SJason Low * we do that READ_ONCE() to make sure that we never re-load the pointer. 1288d0e9fe17SLinus Torvalds * 1289d0e9fe17SLinus Torvalds * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1290d0e9fe17SLinus Torvalds * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1291d0e9fe17SLinus Torvalds * is to return an anon_vma that is "complex" due to having gone through 1292d0e9fe17SLinus Torvalds * a fork). 1293d0e9fe17SLinus Torvalds * 1294d0e9fe17SLinus Torvalds * We also make sure that the two vma's are compatible (adjacent, 1295d0e9fe17SLinus Torvalds * and with the same memory policies). That's all stable, even with just 12965b449489SFlorian Rommel * a read lock on the mmap_lock. 1297d0e9fe17SLinus Torvalds */ 1298d0e9fe17SLinus Torvalds static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) 1299d0e9fe17SLinus Torvalds { 1300d0e9fe17SLinus Torvalds if (anon_vma_compatible(a, b)) { 13014db0c3c2SJason Low struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1302d0e9fe17SLinus Torvalds 1303d0e9fe17SLinus Torvalds if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1304d0e9fe17SLinus Torvalds return anon_vma; 1305d0e9fe17SLinus Torvalds } 1306d0e9fe17SLinus Torvalds return NULL; 1307d0e9fe17SLinus Torvalds } 1308d0e9fe17SLinus Torvalds 1309d0e9fe17SLinus Torvalds /* 13101da177e4SLinus Torvalds * find_mergeable_anon_vma is used by anon_vma_prepare, to check 13111da177e4SLinus Torvalds * neighbouring vmas for a suitable anon_vma, before it goes off 13121da177e4SLinus Torvalds * to allocate a new anon_vma. It checks because a repetitive 13131da177e4SLinus Torvalds * sequence of mprotects and faults may otherwise lead to distinct 13141da177e4SLinus Torvalds * anon_vmas being allocated, preventing vma merge in subsequent 13151da177e4SLinus Torvalds * mprotect. 13161da177e4SLinus Torvalds */ 13171da177e4SLinus Torvalds struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 13181da177e4SLinus Torvalds { 1319a67c8caaSMiaohe Lin struct anon_vma *anon_vma = NULL; 13201da177e4SLinus Torvalds 1321a67c8caaSMiaohe Lin /* Try next first. */ 1322a67c8caaSMiaohe Lin if (vma->vm_next) { 1323a67c8caaSMiaohe Lin anon_vma = reusable_anon_vma(vma->vm_next, vma, vma->vm_next); 1324d0e9fe17SLinus Torvalds if (anon_vma) 1325d0e9fe17SLinus Torvalds return anon_vma; 1326a67c8caaSMiaohe Lin } 13271da177e4SLinus Torvalds 1328a67c8caaSMiaohe Lin /* Try prev next. */ 1329a67c8caaSMiaohe Lin if (vma->vm_prev) 1330a67c8caaSMiaohe Lin anon_vma = reusable_anon_vma(vma->vm_prev, vma->vm_prev, vma); 1331a67c8caaSMiaohe Lin 13321da177e4SLinus Torvalds /* 1333a67c8caaSMiaohe Lin * We might reach here with anon_vma == NULL if we can't find 1334a67c8caaSMiaohe Lin * any reusable anon_vma. 13351da177e4SLinus Torvalds * There's no absolute need to look only at touching neighbours: 13361da177e4SLinus Torvalds * we could search further afield for "compatible" anon_vmas. 13371da177e4SLinus Torvalds * But it would probably just be a waste of time searching, 13381da177e4SLinus Torvalds * or lead to too many vmas hanging off the same anon_vma. 13391da177e4SLinus Torvalds * We're trying to allow mprotect remerging later on, 13401da177e4SLinus Torvalds * not trying to minimize memory used for anon_vmas. 13411da177e4SLinus Torvalds */ 1342a67c8caaSMiaohe Lin return anon_vma; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* 134640401530SAl Viro * If a hint addr is less than mmap_min_addr change hint to be as 134740401530SAl Viro * low as possible but still greater than mmap_min_addr 134840401530SAl Viro */ 134940401530SAl Viro static inline unsigned long round_hint_to_min(unsigned long hint) 135040401530SAl Viro { 135140401530SAl Viro hint &= PAGE_MASK; 135240401530SAl Viro if (((void *)hint != NULL) && 135340401530SAl Viro (hint < mmap_min_addr)) 135440401530SAl Viro return PAGE_ALIGN(mmap_min_addr); 135540401530SAl Viro return hint; 135640401530SAl Viro } 135740401530SAl Viro 13586aeb2542SMike Rapoport int mlock_future_check(struct mm_struct *mm, unsigned long flags, 1359363ee17fSDavidlohr Bueso unsigned long len) 1360363ee17fSDavidlohr Bueso { 1361363ee17fSDavidlohr Bueso unsigned long locked, lock_limit; 1362363ee17fSDavidlohr Bueso 1363363ee17fSDavidlohr Bueso /* mlock MCL_FUTURE? */ 1364363ee17fSDavidlohr Bueso if (flags & VM_LOCKED) { 1365363ee17fSDavidlohr Bueso locked = len >> PAGE_SHIFT; 1366363ee17fSDavidlohr Bueso locked += mm->locked_vm; 1367363ee17fSDavidlohr Bueso lock_limit = rlimit(RLIMIT_MEMLOCK); 1368363ee17fSDavidlohr Bueso lock_limit >>= PAGE_SHIFT; 1369363ee17fSDavidlohr Bueso if (locked > lock_limit && !capable(CAP_IPC_LOCK)) 1370363ee17fSDavidlohr Bueso return -EAGAIN; 1371363ee17fSDavidlohr Bueso } 1372363ee17fSDavidlohr Bueso return 0; 1373363ee17fSDavidlohr Bueso } 1374363ee17fSDavidlohr Bueso 1375be83bbf8SLinus Torvalds static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) 1376be83bbf8SLinus Torvalds { 1377be83bbf8SLinus Torvalds if (S_ISREG(inode->i_mode)) 1378423913adSLinus Torvalds return MAX_LFS_FILESIZE; 1379be83bbf8SLinus Torvalds 1380be83bbf8SLinus Torvalds if (S_ISBLK(inode->i_mode)) 1381be83bbf8SLinus Torvalds return MAX_LFS_FILESIZE; 1382be83bbf8SLinus Torvalds 138376f34950SIvan Khoronzhuk if (S_ISSOCK(inode->i_mode)) 138476f34950SIvan Khoronzhuk return MAX_LFS_FILESIZE; 138576f34950SIvan Khoronzhuk 1386be83bbf8SLinus Torvalds /* Special "we do even unsigned file positions" case */ 1387be83bbf8SLinus Torvalds if (file->f_mode & FMODE_UNSIGNED_OFFSET) 1388be83bbf8SLinus Torvalds return 0; 1389be83bbf8SLinus Torvalds 1390be83bbf8SLinus Torvalds /* Yes, random drivers might want more. But I'm tired of buggy drivers */ 1391be83bbf8SLinus Torvalds return ULONG_MAX; 1392be83bbf8SLinus Torvalds } 1393be83bbf8SLinus Torvalds 1394be83bbf8SLinus Torvalds static inline bool file_mmap_ok(struct file *file, struct inode *inode, 1395be83bbf8SLinus Torvalds unsigned long pgoff, unsigned long len) 1396be83bbf8SLinus Torvalds { 1397be83bbf8SLinus Torvalds u64 maxsize = file_mmap_size_max(file, inode); 1398be83bbf8SLinus Torvalds 1399be83bbf8SLinus Torvalds if (maxsize && len > maxsize) 1400be83bbf8SLinus Torvalds return false; 1401be83bbf8SLinus Torvalds maxsize -= len; 1402be83bbf8SLinus Torvalds if (pgoff > maxsize >> PAGE_SHIFT) 1403be83bbf8SLinus Torvalds return false; 1404be83bbf8SLinus Torvalds return true; 1405be83bbf8SLinus Torvalds } 1406be83bbf8SLinus Torvalds 140740401530SAl Viro /* 14083e4e28c5SMichel Lespinasse * The caller must write-lock current->mm->mmap_lock. 14091da177e4SLinus Torvalds */ 14101fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, unsigned long addr, 14111da177e4SLinus Torvalds unsigned long len, unsigned long prot, 141245e55300SPeter Collingbourne unsigned long flags, unsigned long pgoff, 141345e55300SPeter Collingbourne unsigned long *populate, struct list_head *uf) 14141da177e4SLinus Torvalds { 14151da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 141645e55300SPeter Collingbourne vm_flags_t vm_flags; 141762b5f7d0SDave Hansen int pkey = 0; 14181da177e4SLinus Torvalds 1419524e00b3SLiam R. Howlett validate_mm(mm); 142041badc15SMichel Lespinasse *populate = 0; 1421bebeb3d6SMichel Lespinasse 1422e37609bbSPiotr Kwapulinski if (!len) 1423e37609bbSPiotr Kwapulinski return -EINVAL; 1424e37609bbSPiotr Kwapulinski 14251da177e4SLinus Torvalds /* 14261da177e4SLinus Torvalds * Does the application expect PROT_READ to imply PROT_EXEC? 14271da177e4SLinus Torvalds * 14281da177e4SLinus Torvalds * (the exception is when the underlying filesystem is noexec 14291da177e4SLinus Torvalds * mounted, in which case we dont add PROT_EXEC.) 14301da177e4SLinus Torvalds */ 14311da177e4SLinus Torvalds if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) 143290f8572bSEric W. Biederman if (!(file && path_noexec(&file->f_path))) 14331da177e4SLinus Torvalds prot |= PROT_EXEC; 14341da177e4SLinus Torvalds 1435a4ff8e86SMichal Hocko /* force arch specific MAP_FIXED handling in get_unmapped_area */ 1436a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) 1437a4ff8e86SMichal Hocko flags |= MAP_FIXED; 1438a4ff8e86SMichal Hocko 14397cd94146SEric Paris if (!(flags & MAP_FIXED)) 14407cd94146SEric Paris addr = round_hint_to_min(addr); 14417cd94146SEric Paris 14421da177e4SLinus Torvalds /* Careful about overflows.. */ 14431da177e4SLinus Torvalds len = PAGE_ALIGN(len); 14449206de95SAl Viro if (!len) 14451da177e4SLinus Torvalds return -ENOMEM; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds /* offset overflow? */ 14481da177e4SLinus Torvalds if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 14491da177e4SLinus Torvalds return -EOVERFLOW; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds /* Too many mappings? */ 14521da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 14531da177e4SLinus Torvalds return -ENOMEM; 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds /* Obtain the address to map to. we verify (or select) it and ensure 14561da177e4SLinus Torvalds * that it represents a valid section of the address space. 14571da177e4SLinus Torvalds */ 14581da177e4SLinus Torvalds addr = get_unmapped_area(file, addr, len, pgoff, flags); 1459ff68dac6SGaowei Pu if (IS_ERR_VALUE(addr)) 14601da177e4SLinus Torvalds return addr; 14611da177e4SLinus Torvalds 1462a4ff8e86SMichal Hocko if (flags & MAP_FIXED_NOREPLACE) { 146335e43c5fSLiam Howlett if (find_vma_intersection(mm, addr, addr + len)) 1464a4ff8e86SMichal Hocko return -EEXIST; 1465a4ff8e86SMichal Hocko } 1466a4ff8e86SMichal Hocko 146762b5f7d0SDave Hansen if (prot == PROT_EXEC) { 146862b5f7d0SDave Hansen pkey = execute_only_pkey(mm); 146962b5f7d0SDave Hansen if (pkey < 0) 147062b5f7d0SDave Hansen pkey = 0; 147162b5f7d0SDave Hansen } 147262b5f7d0SDave Hansen 14731da177e4SLinus Torvalds /* Do simple checking here so the lower-level routines won't have 14741da177e4SLinus Torvalds * to. we assume access permissions have been handled by the open 14751da177e4SLinus Torvalds * of the memory object, so we don't do any here. 14761da177e4SLinus Torvalds */ 147745e55300SPeter Collingbourne vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | 14781da177e4SLinus Torvalds mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 14791da177e4SLinus Torvalds 1480cdf7b341SHuang Shijie if (flags & MAP_LOCKED) 14811da177e4SLinus Torvalds if (!can_do_mlock()) 14821da177e4SLinus Torvalds return -EPERM; 1483ba470de4SRik van Riel 1484363ee17fSDavidlohr Bueso if (mlock_future_check(mm, vm_flags, len)) 14851da177e4SLinus Torvalds return -EAGAIN; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds if (file) { 1488077bf22bSOleg Nesterov struct inode *inode = file_inode(file); 14891c972597SDan Williams unsigned long flags_mask; 14901c972597SDan Williams 1491be83bbf8SLinus Torvalds if (!file_mmap_ok(file, inode, pgoff, len)) 1492be83bbf8SLinus Torvalds return -EOVERFLOW; 1493be83bbf8SLinus Torvalds 14941c972597SDan Williams flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; 1495077bf22bSOleg Nesterov 14961da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 14971da177e4SLinus Torvalds case MAP_SHARED: 14981c972597SDan Williams /* 14991c972597SDan Williams * Force use of MAP_SHARED_VALIDATE with non-legacy 15001c972597SDan Williams * flags. E.g. MAP_SYNC is dangerous to use with 15011c972597SDan Williams * MAP_SHARED as you don't know which consistency model 15021c972597SDan Williams * you will get. We silently ignore unsupported flags 15031c972597SDan Williams * with MAP_SHARED to preserve backward compatibility. 15041c972597SDan Williams */ 15051c972597SDan Williams flags &= LEGACY_MAP_MASK; 1506e4a9bc58SJoe Perches fallthrough; 15071c972597SDan Williams case MAP_SHARED_VALIDATE: 15081c972597SDan Williams if (flags & ~flags_mask) 15091c972597SDan Williams return -EOPNOTSUPP; 1510dc617f29SDarrick J. Wong if (prot & PROT_WRITE) { 1511dc617f29SDarrick J. Wong if (!(file->f_mode & FMODE_WRITE)) 15121da177e4SLinus Torvalds return -EACCES; 1513dc617f29SDarrick J. Wong if (IS_SWAPFILE(file->f_mapping->host)) 1514dc617f29SDarrick J. Wong return -ETXTBSY; 1515dc617f29SDarrick J. Wong } 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds /* 15181da177e4SLinus Torvalds * Make sure we don't allow writing to an append-only 15191da177e4SLinus Torvalds * file.. 15201da177e4SLinus Torvalds */ 15211da177e4SLinus Torvalds if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) 15221da177e4SLinus Torvalds return -EACCES; 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 15251da177e4SLinus Torvalds if (!(file->f_mode & FMODE_WRITE)) 15261da177e4SLinus Torvalds vm_flags &= ~(VM_MAYWRITE | VM_SHARED); 1527e4a9bc58SJoe Perches fallthrough; 15281da177e4SLinus Torvalds case MAP_PRIVATE: 15291da177e4SLinus Torvalds if (!(file->f_mode & FMODE_READ)) 15301da177e4SLinus Torvalds return -EACCES; 153190f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 153280c5606cSLinus Torvalds if (vm_flags & VM_EXEC) 153380c5606cSLinus Torvalds return -EPERM; 153480c5606cSLinus Torvalds vm_flags &= ~VM_MAYEXEC; 153580c5606cSLinus Torvalds } 153680c5606cSLinus Torvalds 153772c2d531SAl Viro if (!file->f_op->mmap) 153880c5606cSLinus Torvalds return -ENODEV; 1539b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1540b2c56e4fSOleg Nesterov return -EINVAL; 15411da177e4SLinus Torvalds break; 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds default: 15441da177e4SLinus Torvalds return -EINVAL; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds } else { 15471da177e4SLinus Torvalds switch (flags & MAP_TYPE) { 15481da177e4SLinus Torvalds case MAP_SHARED: 1549b2c56e4fSOleg Nesterov if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) 1550b2c56e4fSOleg Nesterov return -EINVAL; 1551ce363942STejun Heo /* 1552ce363942STejun Heo * Ignore pgoff. 1553ce363942STejun Heo */ 1554ce363942STejun Heo pgoff = 0; 15551da177e4SLinus Torvalds vm_flags |= VM_SHARED | VM_MAYSHARE; 15561da177e4SLinus Torvalds break; 15571da177e4SLinus Torvalds case MAP_PRIVATE: 15581da177e4SLinus Torvalds /* 15591da177e4SLinus Torvalds * Set pgoff according to addr for anon_vma. 15601da177e4SLinus Torvalds */ 15611da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 15621da177e4SLinus Torvalds break; 15631da177e4SLinus Torvalds default: 15641da177e4SLinus Torvalds return -EINVAL; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds } 15671da177e4SLinus Torvalds 1568c22c0d63SMichel Lespinasse /* 1569c22c0d63SMichel Lespinasse * Set 'VM_NORESERVE' if we should not account for the 1570c22c0d63SMichel Lespinasse * memory use of this mapping. 1571c22c0d63SMichel Lespinasse */ 1572c22c0d63SMichel Lespinasse if (flags & MAP_NORESERVE) { 1573c22c0d63SMichel Lespinasse /* We honor MAP_NORESERVE if allowed to overcommit */ 1574c22c0d63SMichel Lespinasse if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1575c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1576c22c0d63SMichel Lespinasse 1577c22c0d63SMichel Lespinasse /* hugetlb applies strict overcommit unless MAP_NORESERVE */ 1578c22c0d63SMichel Lespinasse if (file && is_file_hugepages(file)) 1579c22c0d63SMichel Lespinasse vm_flags |= VM_NORESERVE; 1580c22c0d63SMichel Lespinasse } 1581c22c0d63SMichel Lespinasse 1582897ab3e0SMike Rapoport addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); 158309a9f1d2SMichel Lespinasse if (!IS_ERR_VALUE(addr) && 158409a9f1d2SMichel Lespinasse ((vm_flags & VM_LOCKED) || 158509a9f1d2SMichel Lespinasse (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) 158641badc15SMichel Lespinasse *populate = len; 1587bebeb3d6SMichel Lespinasse return addr; 15880165ab44SMiklos Szeredi } 15896be5ceb0SLinus Torvalds 1590a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1591a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1592a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 159366f0dc48SHugh Dickins { 159466f0dc48SHugh Dickins struct file *file = NULL; 15951e3ee14bSChen Gang unsigned long retval; 159666f0dc48SHugh Dickins 159766f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 1598120a795dSAl Viro audit_mmap_fd(fd, flags); 159966f0dc48SHugh Dickins file = fget(fd); 160066f0dc48SHugh Dickins if (!file) 16011e3ee14bSChen Gang return -EBADF; 16027bba8f0eSZhen Lei if (is_file_hugepages(file)) { 1603af73e4d9SNaoya Horiguchi len = ALIGN(len, huge_page_size(hstate_file(file))); 16047bba8f0eSZhen Lei } else if (unlikely(flags & MAP_HUGETLB)) { 1605493af578SJörn Engel retval = -EINVAL; 1606493af578SJörn Engel goto out_fput; 16077bba8f0eSZhen Lei } 160866f0dc48SHugh Dickins } else if (flags & MAP_HUGETLB) { 1609c103a4dcSAndrew Morton struct hstate *hs; 1610af73e4d9SNaoya Horiguchi 161120ac2893SAnshuman Khandual hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 1612091d0d55SLi Zefan if (!hs) 1613091d0d55SLi Zefan return -EINVAL; 1614091d0d55SLi Zefan 1615091d0d55SLi Zefan len = ALIGN(len, huge_page_size(hs)); 161666f0dc48SHugh Dickins /* 161766f0dc48SHugh Dickins * VM_NORESERVE is used because the reservations will be 161866f0dc48SHugh Dickins * taken when vm_ops->mmap() is called 161966f0dc48SHugh Dickins */ 1620af73e4d9SNaoya Horiguchi file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, 162142d7395fSAndi Kleen VM_NORESERVE, 162283c1fd76Szhangyiru HUGETLB_ANONHUGE_INODE, 162342d7395fSAndi Kleen (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); 162466f0dc48SHugh Dickins if (IS_ERR(file)) 162566f0dc48SHugh Dickins return PTR_ERR(file); 162666f0dc48SHugh Dickins } 162766f0dc48SHugh Dickins 16289fbeb5abSMichal Hocko retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1629493af578SJörn Engel out_fput: 163066f0dc48SHugh Dickins if (file) 163166f0dc48SHugh Dickins fput(file); 163266f0dc48SHugh Dickins return retval; 163366f0dc48SHugh Dickins } 163466f0dc48SHugh Dickins 1635a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1636a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1637a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1638a90f590aSDominik Brodowski { 1639a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1640a90f590aSDominik Brodowski } 1641a90f590aSDominik Brodowski 1642a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1643a4679373SChristoph Hellwig struct mmap_arg_struct { 1644a4679373SChristoph Hellwig unsigned long addr; 1645a4679373SChristoph Hellwig unsigned long len; 1646a4679373SChristoph Hellwig unsigned long prot; 1647a4679373SChristoph Hellwig unsigned long flags; 1648a4679373SChristoph Hellwig unsigned long fd; 1649a4679373SChristoph Hellwig unsigned long offset; 1650a4679373SChristoph Hellwig }; 1651a4679373SChristoph Hellwig 1652a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1653a4679373SChristoph Hellwig { 1654a4679373SChristoph Hellwig struct mmap_arg_struct a; 1655a4679373SChristoph Hellwig 1656a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1657a4679373SChristoph Hellwig return -EFAULT; 1658de1741a1SAlexander Kuleshov if (offset_in_page(a.offset)) 1659a4679373SChristoph Hellwig return -EINVAL; 1660a4679373SChristoph Hellwig 1661a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1662a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1663a4679373SChristoph Hellwig } 1664a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1665a4679373SChristoph Hellwig 16664e950f6fSAlexey Dobriyan /* 16678bb4e7a2SWei Yang * Some shared mappings will want the pages marked read-only 16684e950f6fSAlexey Dobriyan * to track write events. If so, we'll downgrade vm_page_prot 16694e950f6fSAlexey Dobriyan * to the private version (using protection_map[] without the 16704e950f6fSAlexey Dobriyan * VM_SHARED bit). 16714e950f6fSAlexey Dobriyan */ 16726d2329f8SAndrea Arcangeli int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 16734e950f6fSAlexey Dobriyan { 1674ca16d140SKOSAKI Motohiro vm_flags_t vm_flags = vma->vm_flags; 16758a04446aSKirill A. Shutemov const struct vm_operations_struct *vm_ops = vma->vm_ops; 16764e950f6fSAlexey Dobriyan 16774e950f6fSAlexey Dobriyan /* If it was private or non-writable, the write bit is already clear */ 16784e950f6fSAlexey Dobriyan if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) 16794e950f6fSAlexey Dobriyan return 0; 16804e950f6fSAlexey Dobriyan 16814e950f6fSAlexey Dobriyan /* The backer wishes to know when pages are first written to? */ 16828a04446aSKirill A. Shutemov if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) 16834e950f6fSAlexey Dobriyan return 1; 16844e950f6fSAlexey Dobriyan 168564e45507SPeter Feiner /* The open routine did something to the protections that pgprot_modify 168664e45507SPeter Feiner * won't preserve? */ 16876d2329f8SAndrea Arcangeli if (pgprot_val(vm_page_prot) != 16886d2329f8SAndrea Arcangeli pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) 16894e950f6fSAlexey Dobriyan return 0; 16904e950f6fSAlexey Dobriyan 1691f96f7a40SDavid Hildenbrand /* 1692f96f7a40SDavid Hildenbrand * Do we need to track softdirty? hugetlb does not support softdirty 1693f96f7a40SDavid Hildenbrand * tracking yet. 1694f96f7a40SDavid Hildenbrand */ 1695f96f7a40SDavid Hildenbrand if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 169664e45507SPeter Feiner return 1; 169764e45507SPeter Feiner 16984e950f6fSAlexey Dobriyan /* Specialty mapping? */ 16994b6e1e37SKonstantin Khlebnikov if (vm_flags & VM_PFNMAP) 17004e950f6fSAlexey Dobriyan return 0; 17014e950f6fSAlexey Dobriyan 17024e950f6fSAlexey Dobriyan /* Can the mapping track the dirty pages? */ 17034e950f6fSAlexey Dobriyan return vma->vm_file && vma->vm_file->f_mapping && 1704f56753acSChristoph Hellwig mapping_can_writeback(vma->vm_file->f_mapping); 17054e950f6fSAlexey Dobriyan } 17064e950f6fSAlexey Dobriyan 1707fc8744adSLinus Torvalds /* 1708fc8744adSLinus Torvalds * We account for memory if it's a private writeable mapping, 17095a6fe125SMel Gorman * not hugepages and VM_NORESERVE wasn't set. 1710fc8744adSLinus Torvalds */ 1711ca16d140SKOSAKI Motohiro static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) 1712fc8744adSLinus Torvalds { 17135a6fe125SMel Gorman /* 17145a6fe125SMel Gorman * hugetlb has its own accounting separate from the core VM 17155a6fe125SMel Gorman * VM_HUGETLB may not be set yet so we cannot check for that flag. 17165a6fe125SMel Gorman */ 17175a6fe125SMel Gorman if (file && is_file_hugepages(file)) 17185a6fe125SMel Gorman return 0; 17195a6fe125SMel Gorman 1720fc8744adSLinus Torvalds return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1721fc8744adSLinus Torvalds } 1722fc8744adSLinus Torvalds 17233499a131SLiam R. Howlett /** 17243499a131SLiam R. Howlett * unmapped_area() - Find an area between the low_limit and the high_limit with 17253499a131SLiam R. Howlett * the correct alignment and offset, all from @info. Note: current->mm is used 17263499a131SLiam R. Howlett * for the search. 17273499a131SLiam R. Howlett * 17283499a131SLiam R. Howlett * @info: The unmapped area information including the range (low_limit - 17293499a131SLiam R. Howlett * hight_limit), the alignment offset and mask. 17303499a131SLiam R. Howlett * 17313499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 17323499a131SLiam R. Howlett */ 1733baceaf1cSJaewon Kim static unsigned long unmapped_area(struct vm_unmapped_area_info *info) 1734db4fbfb9SMichel Lespinasse { 17353499a131SLiam R. Howlett unsigned long length, gap; 1736db4fbfb9SMichel Lespinasse 17373499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1738db4fbfb9SMichel Lespinasse 1739db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1740db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1741db4fbfb9SMichel Lespinasse if (length < info->length) 1742db4fbfb9SMichel Lespinasse return -ENOMEM; 1743db4fbfb9SMichel Lespinasse 17443499a131SLiam R. Howlett if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, 17453499a131SLiam R. Howlett length)) 17463499a131SLiam R. Howlett return -ENOMEM; 17473499a131SLiam R. Howlett 1748d4af56c5SLiam R. Howlett gap = mas.index; 1749d4af56c5SLiam R. Howlett gap += (info->align_offset - gap) & info->align_mask; 17503499a131SLiam R. Howlett return gap; 1751db4fbfb9SMichel Lespinasse } 1752db4fbfb9SMichel Lespinasse 17533499a131SLiam R. Howlett /** 17543499a131SLiam R. Howlett * unmapped_area_topdown() - Find an area between the low_limit and the 17553499a131SLiam R. Howlett * high_limit with * the correct alignment and offset at the highest available 17563499a131SLiam R. Howlett * address, all from @info. Note: current->mm is used for the search. 17573499a131SLiam R. Howlett * 17583499a131SLiam R. Howlett * @info: The unmapped area information including the range (low_limit - 17593499a131SLiam R. Howlett * hight_limit), the alignment offset and mask. 17603499a131SLiam R. Howlett * 17613499a131SLiam R. Howlett * Return: A memory address or -ENOMEM. 17623499a131SLiam R. Howlett */ 1763baceaf1cSJaewon Kim static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) 1764db4fbfb9SMichel Lespinasse { 17653499a131SLiam R. Howlett unsigned long length, gap; 1766d4af56c5SLiam R. Howlett 17673499a131SLiam R. Howlett MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 1768db4fbfb9SMichel Lespinasse /* Adjust search length to account for worst case alignment overhead */ 1769db4fbfb9SMichel Lespinasse length = info->length + info->align_mask; 1770db4fbfb9SMichel Lespinasse if (length < info->length) 1771db4fbfb9SMichel Lespinasse return -ENOMEM; 1772db4fbfb9SMichel Lespinasse 17733499a131SLiam R. Howlett if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, 17743499a131SLiam R. Howlett length)) 17753499a131SLiam R. Howlett return -ENOMEM; 17763499a131SLiam R. Howlett 1777d4af56c5SLiam R. Howlett gap = mas.last + 1 - info->length; 1778d4af56c5SLiam R. Howlett gap -= (gap - info->align_offset) & info->align_mask; 17793499a131SLiam R. Howlett return gap; 1780db4fbfb9SMichel Lespinasse } 1781db4fbfb9SMichel Lespinasse 1782baceaf1cSJaewon Kim /* 1783baceaf1cSJaewon Kim * Search for an unmapped address range. 1784baceaf1cSJaewon Kim * 1785baceaf1cSJaewon Kim * We are looking for a range that: 1786baceaf1cSJaewon Kim * - does not intersect with any VMA; 1787baceaf1cSJaewon Kim * - is contained within the [low_limit, high_limit) interval; 1788baceaf1cSJaewon Kim * - is at least the desired size. 1789baceaf1cSJaewon Kim * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) 1790baceaf1cSJaewon Kim */ 1791baceaf1cSJaewon Kim unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) 1792baceaf1cSJaewon Kim { 1793df529cabSJaewon Kim unsigned long addr; 1794df529cabSJaewon Kim 1795baceaf1cSJaewon Kim if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) 1796df529cabSJaewon Kim addr = unmapped_area_topdown(info); 1797baceaf1cSJaewon Kim else 1798df529cabSJaewon Kim addr = unmapped_area(info); 1799df529cabSJaewon Kim 1800df529cabSJaewon Kim trace_vm_unmapped_area(addr, info); 1801df529cabSJaewon Kim return addr; 1802baceaf1cSJaewon Kim } 1803f6795053SSteve Capper 18041da177e4SLinus Torvalds /* Get an address range which is currently unmapped. 18051da177e4SLinus Torvalds * For shmat() with addr=0. 18061da177e4SLinus Torvalds * 18071da177e4SLinus Torvalds * Ugly calling convention alert: 18081da177e4SLinus Torvalds * Return value with the low bits set means error value, 18091da177e4SLinus Torvalds * ie 18101da177e4SLinus Torvalds * if (ret & ~PAGE_MASK) 18111da177e4SLinus Torvalds * error = ret; 18121da177e4SLinus Torvalds * 18131da177e4SLinus Torvalds * This function "knows" that -ENOMEM has the bits set. 18141da177e4SLinus Torvalds */ 18151da177e4SLinus Torvalds unsigned long 18164b439e25SChristophe Leroy generic_get_unmapped_area(struct file *filp, unsigned long addr, 18174b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 18184b439e25SChristophe Leroy unsigned long flags) 18191da177e4SLinus Torvalds { 18201da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 18211be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 1822db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 18232cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 18241da177e4SLinus Torvalds 1825f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 18261da177e4SLinus Torvalds return -ENOMEM; 18271da177e4SLinus Torvalds 182806abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 182906abdfb4SBenjamin Herrenschmidt return addr; 183006abdfb4SBenjamin Herrenschmidt 18311da177e4SLinus Torvalds if (addr) { 18321da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 18331be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1834f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 18351be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 18361be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 18371da177e4SLinus Torvalds return addr; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840db4fbfb9SMichel Lespinasse info.flags = 0; 1841db4fbfb9SMichel Lespinasse info.length = len; 18424e99b021SHeiko Carstens info.low_limit = mm->mmap_base; 1843f6795053SSteve Capper info.high_limit = mmap_end; 1844db4fbfb9SMichel Lespinasse info.align_mask = 0; 184509ef5283SJaewon Kim info.align_offset = 0; 1846db4fbfb9SMichel Lespinasse return vm_unmapped_area(&info); 18471da177e4SLinus Torvalds } 18484b439e25SChristophe Leroy 18494b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA 18504b439e25SChristophe Leroy unsigned long 18514b439e25SChristophe Leroy arch_get_unmapped_area(struct file *filp, unsigned long addr, 18524b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 18534b439e25SChristophe Leroy unsigned long flags) 18544b439e25SChristophe Leroy { 18554b439e25SChristophe Leroy return generic_get_unmapped_area(filp, addr, len, pgoff, flags); 18564b439e25SChristophe Leroy } 18571da177e4SLinus Torvalds #endif 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds /* 18601da177e4SLinus Torvalds * This mmap-allocator allocates new areas top-down from below the 18611da177e4SLinus Torvalds * stack's low limit (the base): 18621da177e4SLinus Torvalds */ 18631da177e4SLinus Torvalds unsigned long 18644b439e25SChristophe Leroy generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 186543cca0b1SYang Fan unsigned long len, unsigned long pgoff, 186643cca0b1SYang Fan unsigned long flags) 18671da177e4SLinus Torvalds { 18681be7107fSHugh Dickins struct vm_area_struct *vma, *prev; 18691da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 1870db4fbfb9SMichel Lespinasse struct vm_unmapped_area_info info; 18712cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* requested length too big for entire address space */ 1874f6795053SSteve Capper if (len > mmap_end - mmap_min_addr) 18751da177e4SLinus Torvalds return -ENOMEM; 18761da177e4SLinus Torvalds 187706abdfb4SBenjamin Herrenschmidt if (flags & MAP_FIXED) 187806abdfb4SBenjamin Herrenschmidt return addr; 187906abdfb4SBenjamin Herrenschmidt 18801da177e4SLinus Torvalds /* requesting a specific address */ 18811da177e4SLinus Torvalds if (addr) { 18821da177e4SLinus Torvalds addr = PAGE_ALIGN(addr); 18831be7107fSHugh Dickins vma = find_vma_prev(mm, addr, &prev); 1884f6795053SSteve Capper if (mmap_end - len >= addr && addr >= mmap_min_addr && 18851be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma)) && 18861be7107fSHugh Dickins (!prev || addr >= vm_end_gap(prev))) 18871da177e4SLinus Torvalds return addr; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 1890db4fbfb9SMichel Lespinasse info.flags = VM_UNMAPPED_AREA_TOPDOWN; 1891db4fbfb9SMichel Lespinasse info.length = len; 18922afc745fSAkira Takeuchi info.low_limit = max(PAGE_SIZE, mmap_min_addr); 1893f6795053SSteve Capper info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); 1894db4fbfb9SMichel Lespinasse info.align_mask = 0; 189509ef5283SJaewon Kim info.align_offset = 0; 1896db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1897b716ad95SXiao Guangrong 18981da177e4SLinus Torvalds /* 18991da177e4SLinus Torvalds * A failed mmap() very likely causes application failure, 19001da177e4SLinus Torvalds * so fall back to the bottom-up function here. This scenario 19011da177e4SLinus Torvalds * can happen with large stack limits and large mmap() 19021da177e4SLinus Torvalds * allocations. 19031da177e4SLinus Torvalds */ 1904de1741a1SAlexander Kuleshov if (offset_in_page(addr)) { 1905db4fbfb9SMichel Lespinasse VM_BUG_ON(addr != -ENOMEM); 1906db4fbfb9SMichel Lespinasse info.flags = 0; 1907db4fbfb9SMichel Lespinasse info.low_limit = TASK_UNMAPPED_BASE; 1908f6795053SSteve Capper info.high_limit = mmap_end; 1909db4fbfb9SMichel Lespinasse addr = vm_unmapped_area(&info); 1910db4fbfb9SMichel Lespinasse } 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds return addr; 19131da177e4SLinus Torvalds } 19144b439e25SChristophe Leroy 19154b439e25SChristophe Leroy #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN 19164b439e25SChristophe Leroy unsigned long 19174b439e25SChristophe Leroy arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, 19184b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 19194b439e25SChristophe Leroy unsigned long flags) 19204b439e25SChristophe Leroy { 19214b439e25SChristophe Leroy return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); 19224b439e25SChristophe Leroy } 19231da177e4SLinus Torvalds #endif 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds unsigned long 19261da177e4SLinus Torvalds get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, 19271da177e4SLinus Torvalds unsigned long pgoff, unsigned long flags) 19281da177e4SLinus Torvalds { 192906abdfb4SBenjamin Herrenschmidt unsigned long (*get_area)(struct file *, unsigned long, 193006abdfb4SBenjamin Herrenschmidt unsigned long, unsigned long, unsigned long); 193107ab67c8SLinus Torvalds 19329206de95SAl Viro unsigned long error = arch_mmap_check(addr, len, flags); 19339206de95SAl Viro if (error) 19349206de95SAl Viro return error; 19359206de95SAl Viro 19369206de95SAl Viro /* Careful about overflows.. */ 19379206de95SAl Viro if (len > TASK_SIZE) 19389206de95SAl Viro return -ENOMEM; 19399206de95SAl Viro 194007ab67c8SLinus Torvalds get_area = current->mm->get_unmapped_area; 1941c01d5b30SHugh Dickins if (file) { 1942c01d5b30SHugh Dickins if (file->f_op->get_unmapped_area) 194307ab67c8SLinus Torvalds get_area = file->f_op->get_unmapped_area; 1944c01d5b30SHugh Dickins } else if (flags & MAP_SHARED) { 1945c01d5b30SHugh Dickins /* 1946c01d5b30SHugh Dickins * mmap_region() will call shmem_zero_setup() to create a file, 1947c01d5b30SHugh Dickins * so use shmem's get_unmapped_area in case it can be huge. 194845e55300SPeter Collingbourne * do_mmap() will clear pgoff, so match alignment. 1949c01d5b30SHugh Dickins */ 1950c01d5b30SHugh Dickins pgoff = 0; 1951c01d5b30SHugh Dickins get_area = shmem_get_unmapped_area; 1952f35b5d7dSRik van Riel } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 1953f35b5d7dSRik van Riel /* Ensures that larger anonymous mappings are THP aligned. */ 1954f35b5d7dSRik van Riel get_area = thp_get_unmapped_area; 1955c01d5b30SHugh Dickins } 1956c01d5b30SHugh Dickins 195707ab67c8SLinus Torvalds addr = get_area(file, addr, len, pgoff, flags); 195807ab67c8SLinus Torvalds if (IS_ERR_VALUE(addr)) 195907ab67c8SLinus Torvalds return addr; 196007ab67c8SLinus Torvalds 19611da177e4SLinus Torvalds if (addr > TASK_SIZE - len) 19621da177e4SLinus Torvalds return -ENOMEM; 1963de1741a1SAlexander Kuleshov if (offset_in_page(addr)) 19641da177e4SLinus Torvalds return -EINVAL; 196506abdfb4SBenjamin Herrenschmidt 19669ac4ed4bSAl Viro error = security_mmap_addr(addr); 19679ac4ed4bSAl Viro return error ? error : addr; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds EXPORT_SYMBOL(get_unmapped_area); 19711da177e4SLinus Torvalds 1972be8432e7SLiam R. Howlett /** 1973abdba2ddSLiam R. Howlett * find_vma_intersection() - Look up the first VMA which intersects the interval 1974abdba2ddSLiam R. Howlett * @mm: The process address space. 1975abdba2ddSLiam R. Howlett * @start_addr: The inclusive start user address. 1976abdba2ddSLiam R. Howlett * @end_addr: The exclusive end user address. 1977abdba2ddSLiam R. Howlett * 1978abdba2ddSLiam R. Howlett * Returns: The first VMA within the provided range, %NULL otherwise. Assumes 1979abdba2ddSLiam R. Howlett * start_addr < end_addr. 1980abdba2ddSLiam R. Howlett */ 1981abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 1982abdba2ddSLiam R. Howlett unsigned long start_addr, 1983abdba2ddSLiam R. Howlett unsigned long end_addr) 1984abdba2ddSLiam R. Howlett { 1985abdba2ddSLiam R. Howlett unsigned long index = start_addr; 1986abdba2ddSLiam R. Howlett 1987abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 19887964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 1989abdba2ddSLiam R. Howlett } 1990abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 1991abdba2ddSLiam R. Howlett 1992abdba2ddSLiam R. Howlett /** 1993be8432e7SLiam R. Howlett * find_vma() - Find the VMA for a given address, or the next VMA. 1994be8432e7SLiam R. Howlett * @mm: The mm_struct to check 1995be8432e7SLiam R. Howlett * @addr: The address 1996be8432e7SLiam R. Howlett * 1997be8432e7SLiam R. Howlett * Returns: The VMA associated with addr, or the next VMA. 1998be8432e7SLiam R. Howlett * May return %NULL in the case of no VMA at addr or above. 1999be8432e7SLiam R. Howlett */ 20001da177e4SLinus Torvalds struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 20011da177e4SLinus Torvalds { 2002be8432e7SLiam R. Howlett unsigned long index = addr; 20031da177e4SLinus Torvalds 20045b78ed24SLuigi Rizzo mmap_assert_locked(mm); 20057964cf8cSLiam R. Howlett return mt_find(&mm->mm_mt, &index, ULONG_MAX); 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds EXPORT_SYMBOL(find_vma); 20081da177e4SLinus Torvalds 20097fdbd37dSLiam R. Howlett /** 20107fdbd37dSLiam R. Howlett * find_vma_prev() - Find the VMA for a given address, or the next vma and 20117fdbd37dSLiam R. Howlett * set %pprev to the previous VMA, if any. 20127fdbd37dSLiam R. Howlett * @mm: The mm_struct to check 20137fdbd37dSLiam R. Howlett * @addr: The address 20147fdbd37dSLiam R. Howlett * @pprev: The pointer to set to the previous VMA 20157fdbd37dSLiam R. Howlett * 20167fdbd37dSLiam R. Howlett * Note that RCU lock is missing here since the external mmap_lock() is used 20177fdbd37dSLiam R. Howlett * instead. 20187fdbd37dSLiam R. Howlett * 20197fdbd37dSLiam R. Howlett * Returns: The VMA associated with @addr, or the next vma. 20207fdbd37dSLiam R. Howlett * May return %NULL in the case of no vma at addr or above. 20216bd4837dSKOSAKI Motohiro */ 20221da177e4SLinus Torvalds struct vm_area_struct * 20231da177e4SLinus Torvalds find_vma_prev(struct mm_struct *mm, unsigned long addr, 20241da177e4SLinus Torvalds struct vm_area_struct **pprev) 20251da177e4SLinus Torvalds { 20266bd4837dSKOSAKI Motohiro struct vm_area_struct *vma; 20277fdbd37dSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 20281da177e4SLinus Torvalds 20297fdbd37dSLiam R. Howlett vma = mas_walk(&mas); 20307fdbd37dSLiam R. Howlett *pprev = mas_prev(&mas, 0); 20317fdbd37dSLiam R. Howlett if (!vma) 20327fdbd37dSLiam R. Howlett vma = mas_next(&mas, ULONG_MAX); 20336bd4837dSKOSAKI Motohiro return vma; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds /* 20371da177e4SLinus Torvalds * Verify that the stack growth is acceptable and 20381da177e4SLinus Torvalds * update accounting. This is shared with both the 20391da177e4SLinus Torvalds * grow-up and grow-down cases. 20401da177e4SLinus Torvalds */ 20411be7107fSHugh Dickins static int acct_stack_growth(struct vm_area_struct *vma, 20421be7107fSHugh Dickins unsigned long size, unsigned long grow) 20431da177e4SLinus Torvalds { 20441da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 20451be7107fSHugh Dickins unsigned long new_start; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* address space limit tests */ 204884638335SKonstantin Khlebnikov if (!may_expand_vm(mm, vma->vm_flags, grow)) 20491da177e4SLinus Torvalds return -ENOMEM; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds /* Stack limit test */ 205224c79d8eSKrzysztof Opasiak if (size > rlimit(RLIMIT_STACK)) 20531da177e4SLinus Torvalds return -ENOMEM; 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds /* mlock limit tests */ 2056c5d8a364SMiaohe Lin if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) 20571da177e4SLinus Torvalds return -ENOMEM; 20581da177e4SLinus Torvalds 20590d59a01bSAdam Litke /* Check to ensure the stack will not grow into a hugetlb-only region */ 20600d59a01bSAdam Litke new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : 20610d59a01bSAdam Litke vma->vm_end - size; 20620d59a01bSAdam Litke if (is_hugepage_only_range(vma->vm_mm, new_start, size)) 20630d59a01bSAdam Litke return -EFAULT; 20640d59a01bSAdam Litke 20651da177e4SLinus Torvalds /* 20661da177e4SLinus Torvalds * Overcommit.. This must be the final test, as it will 20671da177e4SLinus Torvalds * update security statistics. 20681da177e4SLinus Torvalds */ 206905fa199dSHugh Dickins if (security_vm_enough_memory_mm(mm, grow)) 20701da177e4SLinus Torvalds return -ENOMEM; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds return 0; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 207546dea3d0SHugh Dickins #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) 20761da177e4SLinus Torvalds /* 207746dea3d0SHugh Dickins * PA-RISC uses this for its stack; IA64 for its Register Backing Store. 207846dea3d0SHugh Dickins * vma is the last one with address > vma->vm_end. Have to extend vma. 20791da177e4SLinus Torvalds */ 208046dea3d0SHugh Dickins int expand_upwards(struct vm_area_struct *vma, unsigned long address) 20811da177e4SLinus Torvalds { 208209357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 20831be7107fSHugh Dickins struct vm_area_struct *next; 20841be7107fSHugh Dickins unsigned long gap_addr; 208512352d3cSKonstantin Khlebnikov int error = 0; 2086d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSUP)) 20891da177e4SLinus Torvalds return -EFAULT; 20901da177e4SLinus Torvalds 2091bd726c90SHelge Deller /* Guard against exceeding limits of the address space. */ 20921be7107fSHugh Dickins address &= PAGE_MASK; 209337511fb5SHelge Deller if (address >= (TASK_SIZE & PAGE_MASK)) 209412352d3cSKonstantin Khlebnikov return -ENOMEM; 2095bd726c90SHelge Deller address += PAGE_SIZE; 209612352d3cSKonstantin Khlebnikov 20971be7107fSHugh Dickins /* Enforce stack_guard_gap */ 20981be7107fSHugh Dickins gap_addr = address + stack_guard_gap; 2099bd726c90SHelge Deller 2100bd726c90SHelge Deller /* Guard against overflow */ 2101bd726c90SHelge Deller if (gap_addr < address || gap_addr > TASK_SIZE) 2102bd726c90SHelge Deller gap_addr = TASK_SIZE; 2103bd726c90SHelge Deller 21041be7107fSHugh Dickins next = vma->vm_next; 21053122e80eSAnshuman Khandual if (next && next->vm_start < gap_addr && vma_is_accessible(next)) { 21061be7107fSHugh Dickins if (!(next->vm_flags & VM_GROWSUP)) 21071be7107fSHugh Dickins return -ENOMEM; 21081be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 21091be7107fSHugh Dickins } 21101be7107fSHugh Dickins 2111d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 21121da177e4SLinus Torvalds return -ENOMEM; 21131da177e4SLinus Torvalds 2114d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2115d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2116d4af56c5SLiam R. Howlett mas_destroy(&mas); 2117d4af56c5SLiam R. Howlett return -ENOMEM; 2118d4af56c5SLiam R. Howlett } 2119d4af56c5SLiam R. Howlett 21201da177e4SLinus Torvalds /* 21211da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2122c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 21231da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 21241da177e4SLinus Torvalds */ 212512352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 21261da177e4SLinus Torvalds 21271da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 21281da177e4SLinus Torvalds if (address > vma->vm_end) { 21291da177e4SLinus Torvalds unsigned long size, grow; 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds size = address - vma->vm_start; 21321da177e4SLinus Torvalds grow = (address - vma->vm_end) >> PAGE_SHIFT; 21331da177e4SLinus Torvalds 213442c36f63SHugh Dickins error = -ENOMEM; 213542c36f63SHugh Dickins if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { 21361da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 21373af9e859SEric B Munson if (!error) { 21384128997bSMichel Lespinasse /* 2139524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2140524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2141524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2142524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2143524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2144524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2145524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 21464128997bSMichel Lespinasse */ 214709357814SOleg Nesterov spin_lock(&mm->page_table_lock); 214887e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 214909357814SOleg Nesterov mm->locked_vm += grow; 215084638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2151bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 21521da177e4SLinus Torvalds vma->vm_end = address; 2153d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2154d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 2155bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 2156524e00b3SLiam R. Howlett if (!vma->vm_next) 21571be7107fSHugh Dickins mm->highest_vm_end = vm_end_gap(vma); 215809357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 21594128997bSMichel Lespinasse 21603af9e859SEric B Munson perf_event_mmap(vma); 21613af9e859SEric B Munson } 21621da177e4SLinus Torvalds } 216342c36f63SHugh Dickins } 216412352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2165c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2166d4af56c5SLiam R. Howlett mas_destroy(&mas); 21671da177e4SLinus Torvalds return error; 21681da177e4SLinus Torvalds } 216946dea3d0SHugh Dickins #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ 217046dea3d0SHugh Dickins 21711da177e4SLinus Torvalds /* 21721da177e4SLinus Torvalds * vma is the first one with address < vma->vm_start. Have to extend vma. 21731da177e4SLinus Torvalds */ 2174524e00b3SLiam R. Howlett int expand_downwards(struct vm_area_struct *vma, unsigned long address) 21751da177e4SLinus Torvalds { 217609357814SOleg Nesterov struct mm_struct *mm = vma->vm_mm; 21771be7107fSHugh Dickins struct vm_area_struct *prev; 21780a1d5299SJann Horn int error = 0; 2179d4af56c5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, 0, 0); 21801da177e4SLinus Torvalds 21818869477aSEric Paris address &= PAGE_MASK; 21820a1d5299SJann Horn if (address < mmap_min_addr) 21830a1d5299SJann Horn return -EPERM; 21848869477aSEric Paris 21851be7107fSHugh Dickins /* Enforce stack_guard_gap */ 21861be7107fSHugh Dickins prev = vma->vm_prev; 21871be7107fSHugh Dickins /* Check that both stack segments have the same anon_vma? */ 218832e4e6d5SOleg Nesterov if (prev && !(prev->vm_flags & VM_GROWSDOWN) && 21893122e80eSAnshuman Khandual vma_is_accessible(prev)) { 219032e4e6d5SOleg Nesterov if (address - prev->vm_end < stack_guard_gap) 219132e4e6d5SOleg Nesterov return -ENOMEM; 21921be7107fSHugh Dickins } 21931be7107fSHugh Dickins 2194d4af56c5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) 219512352d3cSKonstantin Khlebnikov return -ENOMEM; 21961da177e4SLinus Torvalds 2197d4af56c5SLiam R. Howlett /* We must make sure the anon_vma is allocated. */ 2198d4af56c5SLiam R. Howlett if (unlikely(anon_vma_prepare(vma))) { 2199d4af56c5SLiam R. Howlett mas_destroy(&mas); 2200d4af56c5SLiam R. Howlett return -ENOMEM; 2201d4af56c5SLiam R. Howlett } 2202d4af56c5SLiam R. Howlett 22031da177e4SLinus Torvalds /* 22041da177e4SLinus Torvalds * vma->vm_start/vm_end cannot change under us because the caller 2205c1e8d7c6SMichel Lespinasse * is required to hold the mmap_lock in read mode. We need the 22061da177e4SLinus Torvalds * anon_vma lock to serialize against concurrent expand_stacks. 22071da177e4SLinus Torvalds */ 220812352d3cSKonstantin Khlebnikov anon_vma_lock_write(vma->anon_vma); 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds /* Somebody else might have raced and expanded it already */ 22111da177e4SLinus Torvalds if (address < vma->vm_start) { 22121da177e4SLinus Torvalds unsigned long size, grow; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds size = vma->vm_end - address; 22151da177e4SLinus Torvalds grow = (vma->vm_start - address) >> PAGE_SHIFT; 22161da177e4SLinus Torvalds 2217a626ca6aSLinus Torvalds error = -ENOMEM; 2218a626ca6aSLinus Torvalds if (grow <= vma->vm_pgoff) { 22191da177e4SLinus Torvalds error = acct_stack_growth(vma, size, grow); 22201da177e4SLinus Torvalds if (!error) { 22214128997bSMichel Lespinasse /* 2222524e00b3SLiam R. Howlett * We only hold a shared mmap_lock lock here, so 2223524e00b3SLiam R. Howlett * we need to protect against concurrent vma 2224524e00b3SLiam R. Howlett * expansions. anon_vma_lock_write() doesn't 2225524e00b3SLiam R. Howlett * help here, as we don't guarantee that all 2226524e00b3SLiam R. Howlett * growable vmas in a mm share the same root 2227524e00b3SLiam R. Howlett * anon vma. So, we reuse mm->page_table_lock 2228524e00b3SLiam R. Howlett * to guard against concurrent vma expansions. 22294128997bSMichel Lespinasse */ 223009357814SOleg Nesterov spin_lock(&mm->page_table_lock); 223187e8827bSOleg Nesterov if (vma->vm_flags & VM_LOCKED) 223209357814SOleg Nesterov mm->locked_vm += grow; 223384638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, grow); 2234bf181b9fSMichel Lespinasse anon_vma_interval_tree_pre_update_vma(vma); 22351da177e4SLinus Torvalds vma->vm_start = address; 22361da177e4SLinus Torvalds vma->vm_pgoff -= grow; 2237d4af56c5SLiam R. Howlett /* Overwrite old entry in mtree. */ 2238d4af56c5SLiam R. Howlett vma_mas_store(vma, &mas); 2239bf181b9fSMichel Lespinasse anon_vma_interval_tree_post_update_vma(vma); 224009357814SOleg Nesterov spin_unlock(&mm->page_table_lock); 22414128997bSMichel Lespinasse 22423af9e859SEric B Munson perf_event_mmap(vma); 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds } 2245a626ca6aSLinus Torvalds } 224612352d3cSKonstantin Khlebnikov anon_vma_unlock_write(vma->anon_vma); 2247c791576cSYang Shi khugepaged_enter_vma(vma, vma->vm_flags); 2248d4af56c5SLiam R. Howlett mas_destroy(&mas); 22491da177e4SLinus Torvalds return error; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds 22521be7107fSHugh Dickins /* enforced gap between the expanding stack and other mappings. */ 22531be7107fSHugh Dickins unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; 22541be7107fSHugh Dickins 22551be7107fSHugh Dickins static int __init cmdline_parse_stack_guard_gap(char *p) 22561be7107fSHugh Dickins { 22571be7107fSHugh Dickins unsigned long val; 22581be7107fSHugh Dickins char *endptr; 22591be7107fSHugh Dickins 22601be7107fSHugh Dickins val = simple_strtoul(p, &endptr, 10); 22611be7107fSHugh Dickins if (!*endptr) 22621be7107fSHugh Dickins stack_guard_gap = val << PAGE_SHIFT; 22631be7107fSHugh Dickins 2264e6d09493SRandy Dunlap return 1; 22651be7107fSHugh Dickins } 22661be7107fSHugh Dickins __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); 22671be7107fSHugh Dickins 2268b6a2fea3SOllie Wild #ifdef CONFIG_STACK_GROWSUP 2269b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2270b6a2fea3SOllie Wild { 2271b6a2fea3SOllie Wild return expand_upwards(vma, address); 2272b6a2fea3SOllie Wild } 2273b6a2fea3SOllie Wild 2274b6a2fea3SOllie Wild struct vm_area_struct * 2275b6a2fea3SOllie Wild find_extend_vma(struct mm_struct *mm, unsigned long addr) 2276b6a2fea3SOllie Wild { 2277b6a2fea3SOllie Wild struct vm_area_struct *vma, *prev; 2278b6a2fea3SOllie Wild 2279b6a2fea3SOllie Wild addr &= PAGE_MASK; 2280b6a2fea3SOllie Wild vma = find_vma_prev(mm, addr, &prev); 2281b6a2fea3SOllie Wild if (vma && (vma->vm_start <= addr)) 2282b6a2fea3SOllie Wild return vma; 22834d45e75aSJann Horn if (!prev || expand_stack(prev, addr)) 2284b6a2fea3SOllie Wild return NULL; 2285cea10a19SMichel Lespinasse if (prev->vm_flags & VM_LOCKED) 2286fc05f566SKirill A. Shutemov populate_vma_page_range(prev, addr, prev->vm_end, NULL); 2287b6a2fea3SOllie Wild return prev; 2288b6a2fea3SOllie Wild } 2289b6a2fea3SOllie Wild #else 2290b6a2fea3SOllie Wild int expand_stack(struct vm_area_struct *vma, unsigned long address) 2291b6a2fea3SOllie Wild { 2292b6a2fea3SOllie Wild return expand_downwards(vma, address); 2293b6a2fea3SOllie Wild } 2294b6a2fea3SOllie Wild 22951da177e4SLinus Torvalds struct vm_area_struct * 22961da177e4SLinus Torvalds find_extend_vma(struct mm_struct *mm, unsigned long addr) 22971da177e4SLinus Torvalds { 22981da177e4SLinus Torvalds struct vm_area_struct *vma; 22991da177e4SLinus Torvalds unsigned long start; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds addr &= PAGE_MASK; 23021da177e4SLinus Torvalds vma = find_vma(mm, addr); 23031da177e4SLinus Torvalds if (!vma) 23041da177e4SLinus Torvalds return NULL; 23051da177e4SLinus Torvalds if (vma->vm_start <= addr) 23061da177e4SLinus Torvalds return vma; 23071da177e4SLinus Torvalds if (!(vma->vm_flags & VM_GROWSDOWN)) 23081da177e4SLinus Torvalds return NULL; 23091da177e4SLinus Torvalds start = vma->vm_start; 23101da177e4SLinus Torvalds if (expand_stack(vma, addr)) 23111da177e4SLinus Torvalds return NULL; 2312cea10a19SMichel Lespinasse if (vma->vm_flags & VM_LOCKED) 2313fc05f566SKirill A. Shutemov populate_vma_page_range(vma, addr, start, NULL); 23141da177e4SLinus Torvalds return vma; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds #endif 23171da177e4SLinus Torvalds 2318e1d6d01aSJesse Barnes EXPORT_SYMBOL_GPL(find_extend_vma); 2319e1d6d01aSJesse Barnes 23202c0b3814SHugh Dickins /* 23212c0b3814SHugh Dickins * Ok - we have the memory areas we should free on the vma list, 23222c0b3814SHugh Dickins * so release them, and do the vma updates. 23231da177e4SLinus Torvalds * 23242c0b3814SHugh Dickins * Called with the mm semaphore held. 23251da177e4SLinus Torvalds */ 23262c0b3814SHugh Dickins static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) 23271da177e4SLinus Torvalds { 23284f74d2c8SLinus Torvalds unsigned long nr_accounted = 0; 23294f74d2c8SLinus Torvalds 2330365e9c87SHugh Dickins /* Update high watermark before we lower total_vm */ 2331365e9c87SHugh Dickins update_hiwater_vm(mm); 23322c0b3814SHugh Dickins do { 2333ab50b8edSHugh Dickins long nrpages = vma_pages(vma); 23341da177e4SLinus Torvalds 23354f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 23364f74d2c8SLinus Torvalds nr_accounted += nrpages; 233784638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, -nrpages); 2338a8fb5618SHugh Dickins vma = remove_vma(vma); 2339146425a3SHugh Dickins } while (vma); 23404f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 23411da177e4SLinus Torvalds validate_mm(mm); 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds /* 23451da177e4SLinus Torvalds * Get rid of page table information in the indicated region. 23461da177e4SLinus Torvalds * 2347f10df686SPaolo 'Blaisorblade' Giarrusso * Called with the mm semaphore held. 23481da177e4SLinus Torvalds */ 23491da177e4SLinus Torvalds static void unmap_region(struct mm_struct *mm, 2350e0da382cSHugh Dickins struct vm_area_struct *vma, struct vm_area_struct *prev, 2351e0da382cSHugh Dickins unsigned long start, unsigned long end) 23521da177e4SLinus Torvalds { 2353f39af059SMatthew Wilcox (Oracle) struct vm_area_struct *next = __vma_next(mm, prev); 2354d16dfc55SPeter Zijlstra struct mmu_gather tlb; 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds lru_add_drain(); 2357a72afd87SWill Deacon tlb_gather_mmu(&tlb, mm); 2358365e9c87SHugh Dickins update_hiwater_rss(mm); 23594f74d2c8SLinus Torvalds unmap_vmas(&tlb, vma, start, end); 2360d16dfc55SPeter Zijlstra free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 23616ee8630eSHugh Dickins next ? next->vm_start : USER_PGTABLES_CEILING); 2362ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds /* 2366def5efe0SDavid Rientjes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 2367def5efe0SDavid Rientjes * has already been checked or doesn't make sense to fail. 23681da177e4SLinus Torvalds */ 2369def5efe0SDavid Rientjes int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 23701da177e4SLinus Torvalds unsigned long addr, int new_below) 23711da177e4SLinus Torvalds { 23721da177e4SLinus Torvalds struct vm_area_struct *new; 2373e3975891SChen Gang int err; 2374d4af56c5SLiam R. Howlett validate_mm_mt(mm); 23751da177e4SLinus Torvalds 2376dd3b614fSDmitry Safonov if (vma->vm_ops && vma->vm_ops->may_split) { 2377dd3b614fSDmitry Safonov err = vma->vm_ops->may_split(vma, addr); 237831383c68SDan Williams if (err) 237931383c68SDan Williams return err; 238031383c68SDan Williams } 23811da177e4SLinus Torvalds 23823928d4f5SLinus Torvalds new = vm_area_dup(vma); 23831da177e4SLinus Torvalds if (!new) 2384e3975891SChen Gang return -ENOMEM; 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds if (new_below) 23871da177e4SLinus Torvalds new->vm_end = addr; 23881da177e4SLinus Torvalds else { 23891da177e4SLinus Torvalds new->vm_start = addr; 23901da177e4SLinus Torvalds new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 2393ef0855d3SOleg Nesterov err = vma_dup_policy(vma, new); 2394ef0855d3SOleg Nesterov if (err) 23955beb4930SRik van Riel goto out_free_vma; 23961da177e4SLinus Torvalds 2397c4ea95d7SDaniel Forrest err = anon_vma_clone(new, vma); 2398c4ea95d7SDaniel Forrest if (err) 23995beb4930SRik van Riel goto out_free_mpol; 24005beb4930SRik van Riel 2401e9714acfSKonstantin Khlebnikov if (new->vm_file) 24021da177e4SLinus Torvalds get_file(new->vm_file); 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds if (new->vm_ops && new->vm_ops->open) 24051da177e4SLinus Torvalds new->vm_ops->open(new); 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds if (new_below) 24085beb4930SRik van Riel err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + 24091da177e4SLinus Torvalds ((addr - new->vm_start) >> PAGE_SHIFT), new); 24101da177e4SLinus Torvalds else 24115beb4930SRik van Riel err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); 24121da177e4SLinus Torvalds 24135beb4930SRik van Riel /* Success. */ 24145beb4930SRik van Riel if (!err) 24151da177e4SLinus Torvalds return 0; 24165beb4930SRik van Riel 2417d4af56c5SLiam R. Howlett /* Avoid vm accounting in close() operation */ 2418d4af56c5SLiam R. Howlett new->vm_start = new->vm_end; 2419d4af56c5SLiam R. Howlett new->vm_pgoff = 0; 24205beb4930SRik van Riel /* Clean everything up if vma_adjust failed. */ 242158927533SRik van Riel if (new->vm_ops && new->vm_ops->close) 24225beb4930SRik van Riel new->vm_ops->close(new); 2423e9714acfSKonstantin Khlebnikov if (new->vm_file) 24245beb4930SRik van Riel fput(new->vm_file); 24252aeadc30SAndrea Arcangeli unlink_anon_vmas(new); 24265beb4930SRik van Riel out_free_mpol: 2427ef0855d3SOleg Nesterov mpol_put(vma_policy(new)); 24285beb4930SRik van Riel out_free_vma: 24293928d4f5SLinus Torvalds vm_area_free(new); 2430d4af56c5SLiam R. Howlett validate_mm_mt(mm); 24315beb4930SRik van Riel return err; 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 2434659ace58SKOSAKI Motohiro /* 2435659ace58SKOSAKI Motohiro * Split a vma into two pieces at address 'addr', a new vma is allocated 2436659ace58SKOSAKI Motohiro * either for the first part or the tail. 2437659ace58SKOSAKI Motohiro */ 2438659ace58SKOSAKI Motohiro int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 2439659ace58SKOSAKI Motohiro unsigned long addr, int new_below) 2440659ace58SKOSAKI Motohiro { 2441659ace58SKOSAKI Motohiro if (mm->map_count >= sysctl_max_map_count) 2442659ace58SKOSAKI Motohiro return -ENOMEM; 2443659ace58SKOSAKI Motohiro 2444659ace58SKOSAKI Motohiro return __split_vma(mm, vma, addr, new_below); 2445659ace58SKOSAKI Motohiro } 2446659ace58SKOSAKI Motohiro 244711f9a21aSLiam R. Howlett static inline int 244811f9a21aSLiam R. Howlett unlock_range(struct vm_area_struct *start, struct vm_area_struct **tail, 244911f9a21aSLiam R. Howlett unsigned long limit) 24501da177e4SLinus Torvalds { 245111f9a21aSLiam R. Howlett struct mm_struct *mm = start->vm_mm; 245211f9a21aSLiam R. Howlett struct vm_area_struct *tmp = start; 245311f9a21aSLiam R. Howlett int count = 0; 245411f9a21aSLiam R. Howlett 245511f9a21aSLiam R. Howlett while (tmp && tmp->vm_start < limit) { 245611f9a21aSLiam R. Howlett *tail = tmp; 245711f9a21aSLiam R. Howlett count++; 245811f9a21aSLiam R. Howlett if (tmp->vm_flags & VM_LOCKED) 245911f9a21aSLiam R. Howlett mm->locked_vm -= vma_pages(tmp); 246011f9a21aSLiam R. Howlett 246111f9a21aSLiam R. Howlett tmp = tmp->vm_next; 246211f9a21aSLiam R. Howlett } 246311f9a21aSLiam R. Howlett 246411f9a21aSLiam R. Howlett return count; 246511f9a21aSLiam R. Howlett } 246611f9a21aSLiam R. Howlett 246711f9a21aSLiam R. Howlett /* 246811f9a21aSLiam R. Howlett * do_mas_align_munmap() - munmap the aligned region from @start to @end. 246911f9a21aSLiam R. Howlett * @mas: The maple_state, ideally set up to alter the correct tree location. 247011f9a21aSLiam R. Howlett * @vma: The starting vm_area_struct 247111f9a21aSLiam R. Howlett * @mm: The mm_struct 247211f9a21aSLiam R. Howlett * @start: The aligned start address to munmap. 247311f9a21aSLiam R. Howlett * @end: The aligned end address to munmap. 247411f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 247511f9a21aSLiam R. Howlett * @downgrade: Set to true to attempt a write downgrade of the mmap_sem 247611f9a21aSLiam R. Howlett * 247711f9a21aSLiam R. Howlett * If @downgrade is true, check return code for potential release of the lock. 247811f9a21aSLiam R. Howlett */ 247911f9a21aSLiam R. Howlett static int 248011f9a21aSLiam R. Howlett do_mas_align_munmap(struct ma_state *mas, struct vm_area_struct *vma, 248111f9a21aSLiam R. Howlett struct mm_struct *mm, unsigned long start, 248211f9a21aSLiam R. Howlett unsigned long end, struct list_head *uf, bool downgrade) 248311f9a21aSLiam R. Howlett { 248411f9a21aSLiam R. Howlett struct vm_area_struct *prev, *last; 2485d4af56c5SLiam R. Howlett int error = -ENOMEM; 2486524e00b3SLiam R. Howlett /* we have start < vma->vm_end */ 2487524e00b3SLiam R. Howlett 248811f9a21aSLiam R. Howlett if (mas_preallocate(mas, vma, GFP_KERNEL)) 248911f9a21aSLiam R. Howlett return -ENOMEM; 249011f9a21aSLiam R. Howlett 249111f9a21aSLiam R. Howlett mas->last = end - 1; 24921da177e4SLinus Torvalds /* 24931da177e4SLinus Torvalds * If we need to split any vma, do it now to save pain later. 24941da177e4SLinus Torvalds * 24951da177e4SLinus Torvalds * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 24961da177e4SLinus Torvalds * unmapped vm_area_struct will remain in use: so lower split_vma 24971da177e4SLinus Torvalds * places tmp vma above, and higher split_vma places tmp vma below. 24981da177e4SLinus Torvalds */ 2499146425a3SHugh Dickins if (start > vma->vm_start) { 2500659ace58SKOSAKI Motohiro 2501659ace58SKOSAKI Motohiro /* 2502659ace58SKOSAKI Motohiro * Make sure that map_count on return from munmap() will 2503659ace58SKOSAKI Motohiro * not exceed its limit; but let map_count go just above 2504659ace58SKOSAKI Motohiro * its limit temporarily, to help free resources as expected. 2505659ace58SKOSAKI Motohiro */ 2506659ace58SKOSAKI Motohiro if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 2507d4af56c5SLiam R. Howlett goto map_count_exceeded; 2508659ace58SKOSAKI Motohiro 2509659ace58SKOSAKI Motohiro error = __split_vma(mm, vma, start, 0); 25101da177e4SLinus Torvalds if (error) 2511d4af56c5SLiam R. Howlett goto split_failed; 251211f9a21aSLiam R. Howlett 2513146425a3SHugh Dickins prev = vma; 251411f9a21aSLiam R. Howlett vma = __vma_next(mm, prev); 251511f9a21aSLiam R. Howlett mas->index = start; 251611f9a21aSLiam R. Howlett mas_reset(mas); 251711f9a21aSLiam R. Howlett } else { 251811f9a21aSLiam R. Howlett prev = vma->vm_prev; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 252111f9a21aSLiam R. Howlett if (vma->vm_end >= end) 252211f9a21aSLiam R. Howlett last = vma; 252311f9a21aSLiam R. Howlett else 252411f9a21aSLiam R. Howlett last = find_vma_intersection(mm, end - 1, end); 252511f9a21aSLiam R. Howlett 25261da177e4SLinus Torvalds /* Does it split the last one? */ 252711f9a21aSLiam R. Howlett if (last && end < last->vm_end) { 2528d4af56c5SLiam R. Howlett error = __split_vma(mm, last, end, 1); 252911f9a21aSLiam R. Howlett 25301da177e4SLinus Torvalds if (error) 2531d4af56c5SLiam R. Howlett goto split_failed; 253211f9a21aSLiam R. Howlett 253311f9a21aSLiam R. Howlett if (vma == last) 2534f39af059SMatthew Wilcox (Oracle) vma = __vma_next(mm, prev); 253511f9a21aSLiam R. Howlett mas_reset(mas); 253611f9a21aSLiam R. Howlett } 25371da177e4SLinus Torvalds 25382376dd7cSAndrea Arcangeli if (unlikely(uf)) { 25392376dd7cSAndrea Arcangeli /* 25402376dd7cSAndrea Arcangeli * If userfaultfd_unmap_prep returns an error the vmas 2541f0953a1bSIngo Molnar * will remain split, but userland will get a 25422376dd7cSAndrea Arcangeli * highly unexpected error anyway. This is no 25432376dd7cSAndrea Arcangeli * different than the case where the first of the two 25442376dd7cSAndrea Arcangeli * __split_vma fails, but we don't undo the first 25452376dd7cSAndrea Arcangeli * split, despite we could. This is unlikely enough 25462376dd7cSAndrea Arcangeli * failure that it's not worth optimizing it for. 25472376dd7cSAndrea Arcangeli */ 2548*69dbe6daSLiam R. Howlett error = userfaultfd_unmap_prep(mm, start, end, uf); 254911f9a21aSLiam R. Howlett 25502376dd7cSAndrea Arcangeli if (error) 2551d4af56c5SLiam R. Howlett goto userfaultfd_error; 25522376dd7cSAndrea Arcangeli } 25532376dd7cSAndrea Arcangeli 255411f9a21aSLiam R. Howlett /* 255511f9a21aSLiam R. Howlett * unlock any mlock()ed ranges before detaching vmas, count the number 255611f9a21aSLiam R. Howlett * of VMAs to be dropped, and return the tail entry of the affected 255711f9a21aSLiam R. Howlett * area. 255811f9a21aSLiam R. Howlett */ 255911f9a21aSLiam R. Howlett mm->map_count -= unlock_range(vma, &last, end); 256011f9a21aSLiam R. Howlett /* Drop removed area from the tree */ 256111f9a21aSLiam R. Howlett mas_store_prealloc(mas, NULL); 25621da177e4SLinus Torvalds 256311f9a21aSLiam R. Howlett /* Detach vmas from the MM linked list */ 256411f9a21aSLiam R. Howlett vma->vm_prev = NULL; 256511f9a21aSLiam R. Howlett if (prev) 256611f9a21aSLiam R. Howlett prev->vm_next = last->vm_next; 256711f9a21aSLiam R. Howlett else 256811f9a21aSLiam R. Howlett mm->mmap = last->vm_next; 256911f9a21aSLiam R. Howlett 257011f9a21aSLiam R. Howlett if (last->vm_next) { 257111f9a21aSLiam R. Howlett last->vm_next->vm_prev = prev; 257211f9a21aSLiam R. Howlett last->vm_next = NULL; 257311f9a21aSLiam R. Howlett } else 257411f9a21aSLiam R. Howlett mm->highest_vm_end = prev ? vm_end_gap(prev) : 0; 257511f9a21aSLiam R. Howlett 257611f9a21aSLiam R. Howlett /* 257711f9a21aSLiam R. Howlett * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or 257811f9a21aSLiam R. Howlett * VM_GROWSUP VMA. Such VMAs can change their size under 257911f9a21aSLiam R. Howlett * down_read(mmap_lock) and collide with the VMA we are about to unmap. 258011f9a21aSLiam R. Howlett */ 258111f9a21aSLiam R. Howlett if (downgrade) { 258211f9a21aSLiam R. Howlett if (last && (last->vm_flags & VM_GROWSDOWN)) 258311f9a21aSLiam R. Howlett downgrade = false; 258411f9a21aSLiam R. Howlett else if (prev && (prev->vm_flags & VM_GROWSUP)) 258511f9a21aSLiam R. Howlett downgrade = false; 258611f9a21aSLiam R. Howlett else 2587d8ed45c5SMichel Lespinasse mmap_write_downgrade(mm); 258811f9a21aSLiam R. Howlett } 2589dd2283f2SYang Shi 2590dd2283f2SYang Shi unmap_region(mm, vma, prev, start, end); 2591dd2283f2SYang Shi 25921da177e4SLinus Torvalds /* Fix up all other VM information */ 25932c0b3814SHugh Dickins remove_vma_list(mm, vma); 25941da177e4SLinus Torvalds 2595524e00b3SLiam R. Howlett 2596524e00b3SLiam R. Howlett validate_mm(mm); 2597dd2283f2SYang Shi return downgrade ? 1 : 0; 2598d4af56c5SLiam R. Howlett 2599d4af56c5SLiam R. Howlett map_count_exceeded: 2600d4af56c5SLiam R. Howlett split_failed: 2601d4af56c5SLiam R. Howlett userfaultfd_error: 260211f9a21aSLiam R. Howlett mas_destroy(mas); 2603d4af56c5SLiam R. Howlett return error; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 260611f9a21aSLiam R. Howlett /* 260711f9a21aSLiam R. Howlett * do_mas_munmap() - munmap a given range. 260811f9a21aSLiam R. Howlett * @mas: The maple state 260911f9a21aSLiam R. Howlett * @mm: The mm_struct 261011f9a21aSLiam R. Howlett * @start: The start address to munmap 261111f9a21aSLiam R. Howlett * @len: The length of the range to munmap 261211f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 261311f9a21aSLiam R. Howlett * @downgrade: set to true if the user wants to attempt to write_downgrade the 261411f9a21aSLiam R. Howlett * mmap_sem 261511f9a21aSLiam R. Howlett * 261611f9a21aSLiam R. Howlett * This function takes a @mas that is either pointing to the previous VMA or set 261711f9a21aSLiam R. Howlett * to MA_START and sets it up to remove the mapping(s). The @len will be 261811f9a21aSLiam R. Howlett * aligned and any arch_unmap work will be preformed. 261911f9a21aSLiam R. Howlett * 262011f9a21aSLiam R. Howlett * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. 262111f9a21aSLiam R. Howlett */ 262211f9a21aSLiam R. Howlett int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm, 262311f9a21aSLiam R. Howlett unsigned long start, size_t len, struct list_head *uf, 262411f9a21aSLiam R. Howlett bool downgrade) 262511f9a21aSLiam R. Howlett { 262611f9a21aSLiam R. Howlett unsigned long end; 262711f9a21aSLiam R. Howlett struct vm_area_struct *vma; 262811f9a21aSLiam R. Howlett 262911f9a21aSLiam R. Howlett if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 263011f9a21aSLiam R. Howlett return -EINVAL; 263111f9a21aSLiam R. Howlett 263211f9a21aSLiam R. Howlett end = start + PAGE_ALIGN(len); 263311f9a21aSLiam R. Howlett if (end == start) 263411f9a21aSLiam R. Howlett return -EINVAL; 263511f9a21aSLiam R. Howlett 263611f9a21aSLiam R. Howlett /* arch_unmap() might do unmaps itself. */ 263711f9a21aSLiam R. Howlett arch_unmap(mm, start, end); 263811f9a21aSLiam R. Howlett 263911f9a21aSLiam R. Howlett /* Find the first overlapping VMA */ 264011f9a21aSLiam R. Howlett vma = mas_find(mas, end - 1); 264111f9a21aSLiam R. Howlett if (!vma) 264211f9a21aSLiam R. Howlett return 0; 264311f9a21aSLiam R. Howlett 264411f9a21aSLiam R. Howlett return do_mas_align_munmap(mas, vma, mm, start, end, uf, downgrade); 264511f9a21aSLiam R. Howlett } 264611f9a21aSLiam R. Howlett 264711f9a21aSLiam R. Howlett /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. 264811f9a21aSLiam R. Howlett * @mm: The mm_struct 264911f9a21aSLiam R. Howlett * @start: The start address to munmap 265011f9a21aSLiam R. Howlett * @len: The length to be munmapped. 265111f9a21aSLiam R. Howlett * @uf: The userfaultfd list_head 265211f9a21aSLiam R. Howlett */ 2653dd2283f2SYang Shi int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, 2654dd2283f2SYang Shi struct list_head *uf) 2655dd2283f2SYang Shi { 265611f9a21aSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 265711f9a21aSLiam R. Howlett 265811f9a21aSLiam R. Howlett return do_mas_munmap(&mas, mm, start, len, uf, false); 2659dd2283f2SYang Shi } 2660dd2283f2SYang Shi 2661e99668a5SLiam R. Howlett unsigned long mmap_region(struct file *file, unsigned long addr, 2662e99668a5SLiam R. Howlett unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, 2663e99668a5SLiam R. Howlett struct list_head *uf) 2664e99668a5SLiam R. Howlett { 2665e99668a5SLiam R. Howlett struct mm_struct *mm = current->mm; 2666e99668a5SLiam R. Howlett struct vm_area_struct *vma = NULL; 2667e99668a5SLiam R. Howlett struct vm_area_struct *next, *prev, *merge; 2668e99668a5SLiam R. Howlett pgoff_t pglen = len >> PAGE_SHIFT; 2669e99668a5SLiam R. Howlett unsigned long charged = 0; 2670e99668a5SLiam R. Howlett unsigned long end = addr + len; 2671e99668a5SLiam R. Howlett unsigned long merge_start = addr, merge_end = end; 2672e99668a5SLiam R. Howlett pgoff_t vm_pgoff; 2673e99668a5SLiam R. Howlett int error; 2674e99668a5SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, end - 1); 2675e99668a5SLiam R. Howlett 2676e99668a5SLiam R. Howlett /* Check against address space limit. */ 2677e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { 2678e99668a5SLiam R. Howlett unsigned long nr_pages; 2679e99668a5SLiam R. Howlett 2680e99668a5SLiam R. Howlett /* 2681e99668a5SLiam R. Howlett * MAP_FIXED may remove pages of mappings that intersects with 2682e99668a5SLiam R. Howlett * requested mapping. Account for the pages it would unmap. 2683e99668a5SLiam R. Howlett */ 2684e99668a5SLiam R. Howlett nr_pages = count_vma_pages_range(mm, addr, end); 2685e99668a5SLiam R. Howlett 2686e99668a5SLiam R. Howlett if (!may_expand_vm(mm, vm_flags, 2687e99668a5SLiam R. Howlett (len >> PAGE_SHIFT) - nr_pages)) 2688e99668a5SLiam R. Howlett return -ENOMEM; 2689e99668a5SLiam R. Howlett } 2690e99668a5SLiam R. Howlett 2691e99668a5SLiam R. Howlett /* Unmap any existing mapping in the area */ 269211f9a21aSLiam R. Howlett if (do_mas_munmap(&mas, mm, addr, len, uf, false)) 2693e99668a5SLiam R. Howlett return -ENOMEM; 2694e99668a5SLiam R. Howlett 2695e99668a5SLiam R. Howlett /* 2696e99668a5SLiam R. Howlett * Private writable mapping: check memory availability 2697e99668a5SLiam R. Howlett */ 2698e99668a5SLiam R. Howlett if (accountable_mapping(file, vm_flags)) { 2699e99668a5SLiam R. Howlett charged = len >> PAGE_SHIFT; 2700e99668a5SLiam R. Howlett if (security_vm_enough_memory_mm(mm, charged)) 2701e99668a5SLiam R. Howlett return -ENOMEM; 2702e99668a5SLiam R. Howlett vm_flags |= VM_ACCOUNT; 2703e99668a5SLiam R. Howlett } 2704e99668a5SLiam R. Howlett 2705e99668a5SLiam R. Howlett next = mas_next(&mas, ULONG_MAX); 2706e99668a5SLiam R. Howlett prev = mas_prev(&mas, 0); 2707e99668a5SLiam R. Howlett if (vm_flags & VM_SPECIAL) 2708e99668a5SLiam R. Howlett goto cannot_expand; 2709e99668a5SLiam R. Howlett 2710e99668a5SLiam R. Howlett /* Attempt to expand an old mapping */ 2711e99668a5SLiam R. Howlett /* Check next */ 2712e99668a5SLiam R. Howlett if (next && next->vm_start == end && !vma_policy(next) && 2713e99668a5SLiam R. Howlett can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, 2714e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL)) { 2715e99668a5SLiam R. Howlett merge_end = next->vm_end; 2716e99668a5SLiam R. Howlett vma = next; 2717e99668a5SLiam R. Howlett vm_pgoff = next->vm_pgoff - pglen; 2718e99668a5SLiam R. Howlett } 2719e99668a5SLiam R. Howlett 2720e99668a5SLiam R. Howlett /* Check prev */ 2721e99668a5SLiam R. Howlett if (prev && prev->vm_end == addr && !vma_policy(prev) && 2722e99668a5SLiam R. Howlett (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, 2723e99668a5SLiam R. Howlett pgoff, vma->vm_userfaultfd_ctx, NULL) : 2724e99668a5SLiam R. Howlett can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, 2725e99668a5SLiam R. Howlett NULL_VM_UFFD_CTX, NULL))) { 2726e99668a5SLiam R. Howlett merge_start = prev->vm_start; 2727e99668a5SLiam R. Howlett vma = prev; 2728e99668a5SLiam R. Howlett vm_pgoff = prev->vm_pgoff; 2729e99668a5SLiam R. Howlett } 2730e99668a5SLiam R. Howlett 2731e99668a5SLiam R. Howlett 2732e99668a5SLiam R. Howlett /* Actually expand, if possible */ 2733e99668a5SLiam R. Howlett if (vma && 2734e99668a5SLiam R. Howlett !vma_expand(&mas, vma, merge_start, merge_end, vm_pgoff, next)) { 2735e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vm_flags); 2736e99668a5SLiam R. Howlett goto expanded; 2737e99668a5SLiam R. Howlett } 2738e99668a5SLiam R. Howlett 2739e99668a5SLiam R. Howlett mas.index = addr; 2740e99668a5SLiam R. Howlett mas.last = end - 1; 2741e99668a5SLiam R. Howlett cannot_expand: 2742e99668a5SLiam R. Howlett /* 2743e99668a5SLiam R. Howlett * Determine the object being mapped and call the appropriate 2744e99668a5SLiam R. Howlett * specific mapper. the address has already been validated, but 2745e99668a5SLiam R. Howlett * not unmapped, but the maps are removed from the list. 2746e99668a5SLiam R. Howlett */ 2747e99668a5SLiam R. Howlett vma = vm_area_alloc(mm); 2748e99668a5SLiam R. Howlett if (!vma) { 2749e99668a5SLiam R. Howlett error = -ENOMEM; 2750e99668a5SLiam R. Howlett goto unacct_error; 2751e99668a5SLiam R. Howlett } 2752e99668a5SLiam R. Howlett 2753e99668a5SLiam R. Howlett vma->vm_start = addr; 2754e99668a5SLiam R. Howlett vma->vm_end = end; 2755e99668a5SLiam R. Howlett vma->vm_flags = vm_flags; 2756e99668a5SLiam R. Howlett vma->vm_page_prot = vm_get_page_prot(vm_flags); 2757e99668a5SLiam R. Howlett vma->vm_pgoff = pgoff; 2758e99668a5SLiam R. Howlett 2759e99668a5SLiam R. Howlett if (file) { 2760e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) { 2761e99668a5SLiam R. Howlett error = mapping_map_writable(file->f_mapping); 2762e99668a5SLiam R. Howlett if (error) 2763e99668a5SLiam R. Howlett goto free_vma; 2764e99668a5SLiam R. Howlett } 2765e99668a5SLiam R. Howlett 2766e99668a5SLiam R. Howlett vma->vm_file = get_file(file); 2767e99668a5SLiam R. Howlett error = call_mmap(file, vma); 2768e99668a5SLiam R. Howlett if (error) 2769e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2770e99668a5SLiam R. Howlett 2771e99668a5SLiam R. Howlett /* Can addr have changed?? 2772e99668a5SLiam R. Howlett * 2773e99668a5SLiam R. Howlett * Answer: Yes, several device drivers can do it in their 2774e99668a5SLiam R. Howlett * f_op->mmap method. -DaveM 2775e99668a5SLiam R. Howlett */ 2776e99668a5SLiam R. Howlett WARN_ON_ONCE(addr != vma->vm_start); 2777e99668a5SLiam R. Howlett 2778e99668a5SLiam R. Howlett addr = vma->vm_start; 2779e99668a5SLiam R. Howlett mas_reset(&mas); 2780e99668a5SLiam R. Howlett 2781e99668a5SLiam R. Howlett /* 2782e99668a5SLiam R. Howlett * If vm_flags changed after call_mmap(), we should try merge 2783e99668a5SLiam R. Howlett * vma again as we may succeed this time. 2784e99668a5SLiam R. Howlett */ 2785e99668a5SLiam R. Howlett if (unlikely(vm_flags != vma->vm_flags && prev)) { 2786e99668a5SLiam R. Howlett merge = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags, 2787e99668a5SLiam R. Howlett NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL); 2788e99668a5SLiam R. Howlett if (merge) { 2789e99668a5SLiam R. Howlett /* 2790e99668a5SLiam R. Howlett * ->mmap() can change vma->vm_file and fput 2791e99668a5SLiam R. Howlett * the original file. So fput the vma->vm_file 2792e99668a5SLiam R. Howlett * here or we would add an extra fput for file 2793e99668a5SLiam R. Howlett * and cause general protection fault 2794e99668a5SLiam R. Howlett * ultimately. 2795e99668a5SLiam R. Howlett */ 2796e99668a5SLiam R. Howlett fput(vma->vm_file); 2797e99668a5SLiam R. Howlett vm_area_free(vma); 2798e99668a5SLiam R. Howlett vma = merge; 2799e99668a5SLiam R. Howlett /* Update vm_flags to pick up the change. */ 2800e99668a5SLiam R. Howlett addr = vma->vm_start; 2801e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2802e99668a5SLiam R. Howlett goto unmap_writable; 2803e99668a5SLiam R. Howlett } 2804e99668a5SLiam R. Howlett } 2805e99668a5SLiam R. Howlett 2806e99668a5SLiam R. Howlett vm_flags = vma->vm_flags; 2807e99668a5SLiam R. Howlett } else if (vm_flags & VM_SHARED) { 2808e99668a5SLiam R. Howlett error = shmem_zero_setup(vma); 2809e99668a5SLiam R. Howlett if (error) 2810e99668a5SLiam R. Howlett goto free_vma; 2811e99668a5SLiam R. Howlett } else { 2812e99668a5SLiam R. Howlett vma_set_anonymous(vma); 2813e99668a5SLiam R. Howlett } 2814e99668a5SLiam R. Howlett 2815e99668a5SLiam R. Howlett /* Allow architectures to sanity-check the vm_flags */ 2816e99668a5SLiam R. Howlett if (!arch_validate_flags(vma->vm_flags)) { 2817e99668a5SLiam R. Howlett error = -EINVAL; 2818e99668a5SLiam R. Howlett if (file) 2819e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2820e99668a5SLiam R. Howlett else 2821e99668a5SLiam R. Howlett goto free_vma; 2822e99668a5SLiam R. Howlett } 2823e99668a5SLiam R. Howlett 2824e99668a5SLiam R. Howlett if (mas_preallocate(&mas, vma, GFP_KERNEL)) { 2825e99668a5SLiam R. Howlett error = -ENOMEM; 2826e99668a5SLiam R. Howlett if (file) 2827e99668a5SLiam R. Howlett goto unmap_and_free_vma; 2828e99668a5SLiam R. Howlett else 2829e99668a5SLiam R. Howlett goto free_vma; 2830e99668a5SLiam R. Howlett } 2831e99668a5SLiam R. Howlett 2832e99668a5SLiam R. Howlett if (vma->vm_file) 2833e99668a5SLiam R. Howlett i_mmap_lock_write(vma->vm_file->f_mapping); 2834e99668a5SLiam R. Howlett 2835e99668a5SLiam R. Howlett vma_mas_store(vma, &mas); 2836e99668a5SLiam R. Howlett __vma_link_list(mm, vma, prev); 2837e99668a5SLiam R. Howlett mm->map_count++; 2838e99668a5SLiam R. Howlett if (vma->vm_file) { 2839e99668a5SLiam R. Howlett if (vma->vm_flags & VM_SHARED) 2840e99668a5SLiam R. Howlett mapping_allow_writable(vma->vm_file->f_mapping); 2841e99668a5SLiam R. Howlett 2842e99668a5SLiam R. Howlett flush_dcache_mmap_lock(vma->vm_file->f_mapping); 2843e99668a5SLiam R. Howlett vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); 2844e99668a5SLiam R. Howlett flush_dcache_mmap_unlock(vma->vm_file->f_mapping); 2845e99668a5SLiam R. Howlett i_mmap_unlock_write(vma->vm_file->f_mapping); 2846e99668a5SLiam R. Howlett } 2847e99668a5SLiam R. Howlett 2848e99668a5SLiam R. Howlett /* 2849e99668a5SLiam R. Howlett * vma_merge() calls khugepaged_enter_vma() either, the below 2850e99668a5SLiam R. Howlett * call covers the non-merge case. 2851e99668a5SLiam R. Howlett */ 2852e99668a5SLiam R. Howlett khugepaged_enter_vma(vma, vma->vm_flags); 2853e99668a5SLiam R. Howlett 2854e99668a5SLiam R. Howlett /* Once vma denies write, undo our temporary denial count */ 2855e99668a5SLiam R. Howlett unmap_writable: 2856e99668a5SLiam R. Howlett if (file && vm_flags & VM_SHARED) 2857e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2858e99668a5SLiam R. Howlett file = vma->vm_file; 2859e99668a5SLiam R. Howlett expanded: 2860e99668a5SLiam R. Howlett perf_event_mmap(vma); 2861e99668a5SLiam R. Howlett 2862e99668a5SLiam R. Howlett vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); 2863e99668a5SLiam R. Howlett if (vm_flags & VM_LOCKED) { 2864e99668a5SLiam R. Howlett if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || 2865e99668a5SLiam R. Howlett is_vm_hugetlb_page(vma) || 2866e99668a5SLiam R. Howlett vma == get_gate_vma(current->mm)) 2867e99668a5SLiam R. Howlett vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 2868e99668a5SLiam R. Howlett else 2869e99668a5SLiam R. Howlett mm->locked_vm += (len >> PAGE_SHIFT); 2870e99668a5SLiam R. Howlett } 2871e99668a5SLiam R. Howlett 2872e99668a5SLiam R. Howlett if (file) 2873e99668a5SLiam R. Howlett uprobe_mmap(vma); 2874e99668a5SLiam R. Howlett 2875e99668a5SLiam R. Howlett /* 2876e99668a5SLiam R. Howlett * New (or expanded) vma always get soft dirty status. 2877e99668a5SLiam R. Howlett * Otherwise user-space soft-dirty page tracker won't 2878e99668a5SLiam R. Howlett * be able to distinguish situation when vma area unmapped, 2879e99668a5SLiam R. Howlett * then new mapped in-place (which must be aimed as 2880e99668a5SLiam R. Howlett * a completely new data area). 2881e99668a5SLiam R. Howlett */ 2882e99668a5SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 2883e99668a5SLiam R. Howlett 2884e99668a5SLiam R. Howlett vma_set_page_prot(vma); 2885e99668a5SLiam R. Howlett 2886e99668a5SLiam R. Howlett validate_mm(mm); 2887e99668a5SLiam R. Howlett return addr; 2888e99668a5SLiam R. Howlett 2889e99668a5SLiam R. Howlett unmap_and_free_vma: 2890e99668a5SLiam R. Howlett fput(vma->vm_file); 2891e99668a5SLiam R. Howlett vma->vm_file = NULL; 2892e99668a5SLiam R. Howlett 2893e99668a5SLiam R. Howlett /* Undo any partial mapping done by a device driver. */ 2894e99668a5SLiam R. Howlett unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); 2895e99668a5SLiam R. Howlett if (vm_flags & VM_SHARED) 2896e99668a5SLiam R. Howlett mapping_unmap_writable(file->f_mapping); 2897e99668a5SLiam R. Howlett free_vma: 2898e99668a5SLiam R. Howlett vm_area_free(vma); 2899e99668a5SLiam R. Howlett unacct_error: 2900e99668a5SLiam R. Howlett if (charged) 2901e99668a5SLiam R. Howlett vm_unacct_memory(charged); 2902e99668a5SLiam R. Howlett validate_mm(mm); 2903e99668a5SLiam R. Howlett return error; 2904e99668a5SLiam R. Howlett } 2905e99668a5SLiam R. Howlett 2906dd2283f2SYang Shi static int __vm_munmap(unsigned long start, size_t len, bool downgrade) 2907a46ef99dSLinus Torvalds { 2908a46ef99dSLinus Torvalds int ret; 2909bfce281cSAl Viro struct mm_struct *mm = current->mm; 2910897ab3e0SMike Rapoport LIST_HEAD(uf); 291111f9a21aSLiam R. Howlett MA_STATE(mas, &mm->mm_mt, start, start); 2912a46ef99dSLinus Torvalds 2913d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2914ae798783SMichal Hocko return -EINTR; 2915ae798783SMichal Hocko 291611f9a21aSLiam R. Howlett ret = do_mas_munmap(&mas, mm, start, len, &uf, downgrade); 2917dd2283f2SYang Shi /* 2918c1e8d7c6SMichel Lespinasse * Returning 1 indicates mmap_lock is downgraded. 2919dd2283f2SYang Shi * But 1 is not legal return value of vm_munmap() and munmap(), reset 2920dd2283f2SYang Shi * it to 0 before return. 2921dd2283f2SYang Shi */ 2922dd2283f2SYang Shi if (ret == 1) { 2923d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2924dd2283f2SYang Shi ret = 0; 2925dd2283f2SYang Shi } else 2926d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 2927dd2283f2SYang Shi 2928897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 2929a46ef99dSLinus Torvalds return ret; 2930a46ef99dSLinus Torvalds } 2931dd2283f2SYang Shi 2932dd2283f2SYang Shi int vm_munmap(unsigned long start, size_t len) 2933dd2283f2SYang Shi { 2934dd2283f2SYang Shi return __vm_munmap(start, len, false); 2935dd2283f2SYang Shi } 2936a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 2937a46ef99dSLinus Torvalds 29386a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 29391da177e4SLinus Torvalds { 2940ce18d171SCatalin Marinas addr = untagged_addr(addr); 2941dd2283f2SYang Shi return __vm_munmap(addr, len, true); 29421da177e4SLinus Torvalds } 29431da177e4SLinus Torvalds 2944c8d78c18SKirill A. Shutemov 2945c8d78c18SKirill A. Shutemov /* 2946c8d78c18SKirill A. Shutemov * Emulation of deprecated remap_file_pages() syscall. 2947c8d78c18SKirill A. Shutemov */ 2948c8d78c18SKirill A. Shutemov SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, 2949c8d78c18SKirill A. Shutemov unsigned long, prot, unsigned long, pgoff, unsigned long, flags) 2950c8d78c18SKirill A. Shutemov { 2951c8d78c18SKirill A. Shutemov 2952c8d78c18SKirill A. Shutemov struct mm_struct *mm = current->mm; 2953c8d78c18SKirill A. Shutemov struct vm_area_struct *vma; 2954c8d78c18SKirill A. Shutemov unsigned long populate = 0; 2955c8d78c18SKirill A. Shutemov unsigned long ret = -EINVAL; 2956c8d78c18SKirill A. Shutemov struct file *file; 2957c8d78c18SKirill A. Shutemov 2958ee65728eSMike Rapoport pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", 2959c8d78c18SKirill A. Shutemov current->comm, current->pid); 2960c8d78c18SKirill A. Shutemov 2961c8d78c18SKirill A. Shutemov if (prot) 2962c8d78c18SKirill A. Shutemov return ret; 2963c8d78c18SKirill A. Shutemov start = start & PAGE_MASK; 2964c8d78c18SKirill A. Shutemov size = size & PAGE_MASK; 2965c8d78c18SKirill A. Shutemov 2966c8d78c18SKirill A. Shutemov if (start + size <= start) 2967c8d78c18SKirill A. Shutemov return ret; 2968c8d78c18SKirill A. Shutemov 2969c8d78c18SKirill A. Shutemov /* Does pgoff wrap? */ 2970c8d78c18SKirill A. Shutemov if (pgoff + (size >> PAGE_SHIFT) < pgoff) 2971c8d78c18SKirill A. Shutemov return ret; 2972c8d78c18SKirill A. Shutemov 2973d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 2974dc0ef0dfSMichal Hocko return -EINTR; 2975dc0ef0dfSMichal Hocko 29769b593cb2SLiam R. Howlett vma = vma_lookup(mm, start); 2977c8d78c18SKirill A. Shutemov 2978c8d78c18SKirill A. Shutemov if (!vma || !(vma->vm_flags & VM_SHARED)) 2979c8d78c18SKirill A. Shutemov goto out; 2980c8d78c18SKirill A. Shutemov 298148f7df32SKirill A. Shutemov if (start + size > vma->vm_end) { 298248f7df32SKirill A. Shutemov struct vm_area_struct *next; 298348f7df32SKirill A. Shutemov 298448f7df32SKirill A. Shutemov for (next = vma->vm_next; next; next = next->vm_next) { 298548f7df32SKirill A. Shutemov /* hole between vmas ? */ 298648f7df32SKirill A. Shutemov if (next->vm_start != next->vm_prev->vm_end) 298748f7df32SKirill A. Shutemov goto out; 298848f7df32SKirill A. Shutemov 298948f7df32SKirill A. Shutemov if (next->vm_file != vma->vm_file) 299048f7df32SKirill A. Shutemov goto out; 299148f7df32SKirill A. Shutemov 299248f7df32SKirill A. Shutemov if (next->vm_flags != vma->vm_flags) 299348f7df32SKirill A. Shutemov goto out; 299448f7df32SKirill A. Shutemov 299548f7df32SKirill A. Shutemov if (start + size <= next->vm_end) 299648f7df32SKirill A. Shutemov break; 299748f7df32SKirill A. Shutemov } 299848f7df32SKirill A. Shutemov 299948f7df32SKirill A. Shutemov if (!next) 3000c8d78c18SKirill A. Shutemov goto out; 3001c8d78c18SKirill A. Shutemov } 3002c8d78c18SKirill A. Shutemov 3003c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; 3004c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; 3005c8d78c18SKirill A. Shutemov prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; 3006c8d78c18SKirill A. Shutemov 3007c8d78c18SKirill A. Shutemov flags &= MAP_NONBLOCK; 3008c8d78c18SKirill A. Shutemov flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; 3009fce000b1SLiam Howlett if (vma->vm_flags & VM_LOCKED) 3010c8d78c18SKirill A. Shutemov flags |= MAP_LOCKED; 301148f7df32SKirill A. Shutemov 3012c8d78c18SKirill A. Shutemov file = get_file(vma->vm_file); 301345e55300SPeter Collingbourne ret = do_mmap(vma->vm_file, start, size, 3014897ab3e0SMike Rapoport prot, flags, pgoff, &populate, NULL); 3015c8d78c18SKirill A. Shutemov fput(file); 3016c8d78c18SKirill A. Shutemov out: 3017d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3018c8d78c18SKirill A. Shutemov if (populate) 3019c8d78c18SKirill A. Shutemov mm_populate(ret, populate); 3020c8d78c18SKirill A. Shutemov if (!IS_ERR_VALUE(ret)) 3021c8d78c18SKirill A. Shutemov ret = 0; 3022c8d78c18SKirill A. Shutemov return ret; 3023c8d78c18SKirill A. Shutemov } 3024c8d78c18SKirill A. Shutemov 30251da177e4SLinus Torvalds /* 30262e7ce7d3SLiam R. Howlett * brk_munmap() - Unmap a parital vma. 30272e7ce7d3SLiam R. Howlett * @mas: The maple tree state. 30282e7ce7d3SLiam R. Howlett * @vma: The vma to be modified 30292e7ce7d3SLiam R. Howlett * @newbrk: the start of the address to unmap 30302e7ce7d3SLiam R. Howlett * @oldbrk: The end of the address to unmap 30312e7ce7d3SLiam R. Howlett * @uf: The userfaultfd list_head 30322e7ce7d3SLiam R. Howlett * 30332e7ce7d3SLiam R. Howlett * Returns: 1 on success. 30342e7ce7d3SLiam R. Howlett * unmaps a partial VMA mapping. Does not handle alignment, downgrades lock if 30352e7ce7d3SLiam R. Howlett * possible. 30361da177e4SLinus Torvalds */ 30372e7ce7d3SLiam R. Howlett static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, 30382e7ce7d3SLiam R. Howlett unsigned long newbrk, unsigned long oldbrk, 30392e7ce7d3SLiam R. Howlett struct list_head *uf) 30402e7ce7d3SLiam R. Howlett { 30412e7ce7d3SLiam R. Howlett struct mm_struct *mm = vma->vm_mm; 30422e7ce7d3SLiam R. Howlett int ret; 30432e7ce7d3SLiam R. Howlett 30442e7ce7d3SLiam R. Howlett arch_unmap(mm, newbrk, oldbrk); 304567e7c167SLiam R. Howlett ret = do_mas_align_munmap(mas, vma, mm, newbrk, oldbrk, uf, true); 30462e7ce7d3SLiam R. Howlett validate_mm_mt(mm); 30472e7ce7d3SLiam R. Howlett return ret; 30482e7ce7d3SLiam R. Howlett } 30492e7ce7d3SLiam R. Howlett 30502e7ce7d3SLiam R. Howlett /* 30512e7ce7d3SLiam R. Howlett * do_brk_flags() - Increase the brk vma if the flags match. 30522e7ce7d3SLiam R. Howlett * @mas: The maple tree state. 30532e7ce7d3SLiam R. Howlett * @addr: The start address 30542e7ce7d3SLiam R. Howlett * @len: The length of the increase 30552e7ce7d3SLiam R. Howlett * @vma: The vma, 30562e7ce7d3SLiam R. Howlett * @flags: The VMA Flags 30572e7ce7d3SLiam R. Howlett * 30582e7ce7d3SLiam R. Howlett * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags 30592e7ce7d3SLiam R. Howlett * do not match then create a new anonymous VMA. Eventually we may be able to 30602e7ce7d3SLiam R. Howlett * do some brk-specific accounting here. 30612e7ce7d3SLiam R. Howlett */ 30622e7ce7d3SLiam R. Howlett static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, 30632e7ce7d3SLiam R. Howlett unsigned long addr, unsigned long len, 30642e7ce7d3SLiam R. Howlett unsigned long flags) 30651da177e4SLinus Torvalds { 30661da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 30672e7ce7d3SLiam R. Howlett struct vm_area_struct *prev = NULL; 30682e7ce7d3SLiam R. Howlett 3069d4af56c5SLiam R. Howlett validate_mm_mt(mm); 30702e7ce7d3SLiam R. Howlett /* 30712e7ce7d3SLiam R. Howlett * Check against address space limits by the changed size 30722e7ce7d3SLiam R. Howlett * Note: This happens *after* clearing old mappings in some code paths. 30732e7ce7d3SLiam R. Howlett */ 307416e72e9bSDenys Vlasenko flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; 307584638335SKonstantin Khlebnikov if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) 30761da177e4SLinus Torvalds return -ENOMEM; 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds if (mm->map_count > sysctl_max_map_count) 30791da177e4SLinus Torvalds return -ENOMEM; 30801da177e4SLinus Torvalds 3081191c5424SAl Viro if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) 30821da177e4SLinus Torvalds return -ENOMEM; 30831da177e4SLinus Torvalds 30841da177e4SLinus Torvalds /* 30852e7ce7d3SLiam R. Howlett * Expand the existing vma if possible; Note that singular lists do not 30862e7ce7d3SLiam R. Howlett * occur after forking, so the expand will only happen on new VMAs. 30871da177e4SLinus Torvalds */ 30882e7ce7d3SLiam R. Howlett if (vma && 30892e7ce7d3SLiam R. Howlett (!vma->anon_vma || list_is_singular(&vma->anon_vma_chain)) && 30902e7ce7d3SLiam R. Howlett ((vma->vm_flags & ~VM_SOFTDIRTY) == flags)) { 30912e7ce7d3SLiam R. Howlett mas->index = vma->vm_start; 30922e7ce7d3SLiam R. Howlett mas->last = addr + len - 1; 30932e7ce7d3SLiam R. Howlett vma_adjust_trans_huge(vma, addr, addr + len, 0); 30942e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 30952e7ce7d3SLiam R. Howlett anon_vma_lock_write(vma->anon_vma); 30962e7ce7d3SLiam R. Howlett anon_vma_interval_tree_pre_update_vma(vma); 30971da177e4SLinus Torvalds } 30982e7ce7d3SLiam R. Howlett vma->vm_end = addr + len; 30992e7ce7d3SLiam R. Howlett vma->vm_flags |= VM_SOFTDIRTY; 31002e7ce7d3SLiam R. Howlett if (mas_store_gfp(mas, vma, GFP_KERNEL)) 31012e7ce7d3SLiam R. Howlett goto mas_expand_failed; 31022e7ce7d3SLiam R. Howlett 31032e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 31042e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 31052e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 31062e7ce7d3SLiam R. Howlett } 31072e7ce7d3SLiam R. Howlett khugepaged_enter_vma(vma, flags); 31082e7ce7d3SLiam R. Howlett goto out; 31092e7ce7d3SLiam R. Howlett } 31102e7ce7d3SLiam R. Howlett prev = vma; 31112e7ce7d3SLiam R. Howlett 31122e7ce7d3SLiam R. Howlett /* create a vma struct for an anonymous mapping */ 31132e7ce7d3SLiam R. Howlett vma = vm_area_alloc(mm); 31142e7ce7d3SLiam R. Howlett if (!vma) 31152e7ce7d3SLiam R. Howlett goto vma_alloc_fail; 31161da177e4SLinus Torvalds 3117bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 31181da177e4SLinus Torvalds vma->vm_start = addr; 31191da177e4SLinus Torvalds vma->vm_end = addr + len; 31202e7ce7d3SLiam R. Howlett vma->vm_pgoff = addr >> PAGE_SHIFT; 31211da177e4SLinus Torvalds vma->vm_flags = flags; 31223ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(flags); 31232e7ce7d3SLiam R. Howlett mas_set_range(mas, vma->vm_start, addr + len - 1); 31242e7ce7d3SLiam R. Howlett if (mas_store_gfp(mas, vma, GFP_KERNEL)) 31252e7ce7d3SLiam R. Howlett goto mas_store_fail; 3126d4af56c5SLiam R. Howlett 31272e7ce7d3SLiam R. Howlett if (!prev) 31282e7ce7d3SLiam R. Howlett prev = mas_prev(mas, 0); 31292e7ce7d3SLiam R. Howlett 31302e7ce7d3SLiam R. Howlett __vma_link_list(mm, vma, prev); 31312e7ce7d3SLiam R. Howlett mm->map_count++; 31321da177e4SLinus Torvalds out: 31333af9e859SEric B Munson perf_event_mmap(vma); 31341da177e4SLinus Torvalds mm->total_vm += len >> PAGE_SHIFT; 313584638335SKonstantin Khlebnikov mm->data_vm += len >> PAGE_SHIFT; 3136128557ffSMichel Lespinasse if (flags & VM_LOCKED) 3137ba470de4SRik van Riel mm->locked_vm += (len >> PAGE_SHIFT); 3138d9104d1cSCyrill Gorcunov vma->vm_flags |= VM_SOFTDIRTY; 3139d4af56c5SLiam R. Howlett validate_mm_mt(mm); 31405d22fc25SLinus Torvalds return 0; 3141d4af56c5SLiam R. Howlett 31422e7ce7d3SLiam R. Howlett mas_store_fail: 3143d4af56c5SLiam R. Howlett vm_area_free(vma); 31442e7ce7d3SLiam R. Howlett vma_alloc_fail: 31452e7ce7d3SLiam R. Howlett vm_unacct_memory(len >> PAGE_SHIFT); 31462e7ce7d3SLiam R. Howlett return -ENOMEM; 31472e7ce7d3SLiam R. Howlett 31482e7ce7d3SLiam R. Howlett mas_expand_failed: 31492e7ce7d3SLiam R. Howlett if (vma->anon_vma) { 31502e7ce7d3SLiam R. Howlett anon_vma_interval_tree_post_update_vma(vma); 31512e7ce7d3SLiam R. Howlett anon_vma_unlock_write(vma->anon_vma); 31522e7ce7d3SLiam R. Howlett } 3153d4af56c5SLiam R. Howlett return -ENOMEM; 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds 3156bb177a73SMichal Hocko int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) 3157e4eb1ff6SLinus Torvalds { 3158e4eb1ff6SLinus Torvalds struct mm_struct *mm = current->mm; 31592e7ce7d3SLiam R. Howlett struct vm_area_struct *vma = NULL; 3160bb177a73SMichal Hocko unsigned long len; 31615d22fc25SLinus Torvalds int ret; 3162128557ffSMichel Lespinasse bool populate; 3163897ab3e0SMike Rapoport LIST_HEAD(uf); 31642e7ce7d3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 3165e4eb1ff6SLinus Torvalds 3166bb177a73SMichal Hocko len = PAGE_ALIGN(request); 3167bb177a73SMichal Hocko if (len < request) 3168bb177a73SMichal Hocko return -ENOMEM; 3169bb177a73SMichal Hocko if (!len) 3170bb177a73SMichal Hocko return 0; 3171bb177a73SMichal Hocko 3172d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(mm)) 31732d6c9282SMichal Hocko return -EINTR; 31742d6c9282SMichal Hocko 31752e7ce7d3SLiam R. Howlett /* Until we need other flags, refuse anything except VM_EXEC. */ 31762e7ce7d3SLiam R. Howlett if ((flags & (~VM_EXEC)) != 0) 31772e7ce7d3SLiam R. Howlett return -EINVAL; 31782e7ce7d3SLiam R. Howlett 31792e7ce7d3SLiam R. Howlett ret = check_brk_limits(addr, len); 31802e7ce7d3SLiam R. Howlett if (ret) 31812e7ce7d3SLiam R. Howlett goto limits_failed; 31822e7ce7d3SLiam R. Howlett 318311f9a21aSLiam R. Howlett ret = do_mas_munmap(&mas, mm, addr, len, &uf, 0); 31842e7ce7d3SLiam R. Howlett if (ret) 31852e7ce7d3SLiam R. Howlett goto munmap_failed; 31862e7ce7d3SLiam R. Howlett 31872e7ce7d3SLiam R. Howlett vma = mas_prev(&mas, 0); 31882e7ce7d3SLiam R. Howlett if (!vma || vma->vm_end != addr || vma_policy(vma) || 31892e7ce7d3SLiam R. Howlett !can_vma_merge_after(vma, flags, NULL, NULL, 31902e7ce7d3SLiam R. Howlett addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) 31912e7ce7d3SLiam R. Howlett vma = NULL; 31922e7ce7d3SLiam R. Howlett 31932e7ce7d3SLiam R. Howlett ret = do_brk_flags(&mas, vma, addr, len, flags); 3194128557ffSMichel Lespinasse populate = ((mm->def_flags & VM_LOCKED) != 0); 3195d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 3196897ab3e0SMike Rapoport userfaultfd_unmap_complete(mm, &uf); 31975d22fc25SLinus Torvalds if (populate && !ret) 3198128557ffSMichel Lespinasse mm_populate(addr, len); 3199e4eb1ff6SLinus Torvalds return ret; 32002e7ce7d3SLiam R. Howlett 32012e7ce7d3SLiam R. Howlett munmap_failed: 32022e7ce7d3SLiam R. Howlett limits_failed: 32032e7ce7d3SLiam R. Howlett mmap_write_unlock(mm); 32042e7ce7d3SLiam R. Howlett return ret; 3205e4eb1ff6SLinus Torvalds } 320616e72e9bSDenys Vlasenko EXPORT_SYMBOL(vm_brk_flags); 320716e72e9bSDenys Vlasenko 320816e72e9bSDenys Vlasenko int vm_brk(unsigned long addr, unsigned long len) 320916e72e9bSDenys Vlasenko { 321016e72e9bSDenys Vlasenko return vm_brk_flags(addr, len, 0); 321116e72e9bSDenys Vlasenko } 3212e4eb1ff6SLinus Torvalds EXPORT_SYMBOL(vm_brk); 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds /* Release all mmaps. */ 32151da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 32161da177e4SLinus Torvalds { 3217d16dfc55SPeter Zijlstra struct mmu_gather tlb; 3218ba470de4SRik van Riel struct vm_area_struct *vma; 32191da177e4SLinus Torvalds unsigned long nr_accounted = 0; 32201da177e4SLinus Torvalds 3221d6dd61c8SJeremy Fitzhardinge /* mm's last user has gone, and its about to be pulled down */ 3222cddb8a5cSAndrea Arcangeli mmu_notifier_release(mm); 3223d6dd61c8SJeremy Fitzhardinge 322427ae357fSDavid Rientjes if (unlikely(mm_is_oom_victim(mm))) { 322527ae357fSDavid Rientjes /* 322627ae357fSDavid Rientjes * Manually reap the mm to free as much memory as possible. 322727ae357fSDavid Rientjes * Then, as the oom reaper does, set MMF_OOM_SKIP to disregard 3228c1e8d7c6SMichel Lespinasse * this mm from further consideration. Taking mm->mmap_lock for 322927ae357fSDavid Rientjes * write after setting MMF_OOM_SKIP will guarantee that the oom 3230c1e8d7c6SMichel Lespinasse * reaper will not run on this mm again after mmap_lock is 323127ae357fSDavid Rientjes * dropped. 323227ae357fSDavid Rientjes * 3233c1e8d7c6SMichel Lespinasse * Nothing can be holding mm->mmap_lock here and the above call 323427ae357fSDavid Rientjes * to mmu_notifier_release(mm) ensures mmu notifier callbacks in 323527ae357fSDavid Rientjes * __oom_reap_task_mm() will not block. 323627ae357fSDavid Rientjes */ 323793065ac7SMichal Hocko (void)__oom_reap_task_mm(mm); 323827ae357fSDavid Rientjes set_bit(MMF_OOM_SKIP, &mm->flags); 323927ae357fSDavid Rientjes } 324027ae357fSDavid Rientjes 324164591e86SSuren Baghdasaryan mmap_write_lock(mm); 32429480c53eSJeremy Fitzhardinge arch_exit_mmap(mm); 32439480c53eSJeremy Fitzhardinge 3244ba470de4SRik van Riel vma = mm->mmap; 324564591e86SSuren Baghdasaryan if (!vma) { 324664591e86SSuren Baghdasaryan /* Can happen if dup_mmap() received an OOM */ 324764591e86SSuren Baghdasaryan mmap_write_unlock(mm); 32489480c53eSJeremy Fitzhardinge return; 324964591e86SSuren Baghdasaryan } 32509480c53eSJeremy Fitzhardinge 32511da177e4SLinus Torvalds lru_add_drain(); 32521da177e4SLinus Torvalds flush_cache_mm(mm); 3253d8b45053SWill Deacon tlb_gather_mmu_fullmm(&tlb, mm); 3254901608d9SOleg Nesterov /* update_hiwater_rss(mm) here? but nobody should be looking */ 3255e0da382cSHugh Dickins /* Use -1 here to ensure all VMAs in the mm are unmapped */ 32564f74d2c8SLinus Torvalds unmap_vmas(&tlb, vma, 0, -1); 32576ee8630eSHugh Dickins free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); 3258ae8eba8bSWill Deacon tlb_finish_mmu(&tlb); 32591da177e4SLinus Torvalds 326064591e86SSuren Baghdasaryan /* Walk the list again, actually closing and freeing it. */ 32614f74d2c8SLinus Torvalds while (vma) { 32624f74d2c8SLinus Torvalds if (vma->vm_flags & VM_ACCOUNT) 32634f74d2c8SLinus Torvalds nr_accounted += vma_pages(vma); 3264a8fb5618SHugh Dickins vma = remove_vma(vma); 32650a3b3c25SPaul E. McKenney cond_resched(); 32664f74d2c8SLinus Torvalds } 3267d4af56c5SLiam R. Howlett 3268d4af56c5SLiam R. Howlett trace_exit_mmap(mm); 3269d4af56c5SLiam R. Howlett __mt_destroy(&mm->mm_mt); 3270f798a1d4SSuren Baghdasaryan mm->mmap = NULL; 327164591e86SSuren Baghdasaryan mmap_write_unlock(mm); 32724f74d2c8SLinus Torvalds vm_unacct_memory(nr_accounted); 32731da177e4SLinus Torvalds } 32741da177e4SLinus Torvalds 32751da177e4SLinus Torvalds /* Insert vm structure into process list sorted by address 32761da177e4SLinus Torvalds * and into the inode's i_mmap tree. If vm_file is non-NULL 3277c8c06efaSDavidlohr Bueso * then i_mmap_rwsem is taken here. 32781da177e4SLinus Torvalds */ 32791da177e4SLinus Torvalds int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) 32801da177e4SLinus Torvalds { 32816597d783SHugh Dickins struct vm_area_struct *prev; 3282d4af56c5SLiam R. Howlett unsigned long charged = vma_pages(vma); 32831da177e4SLinus Torvalds 3284d4af56c5SLiam R. Howlett 3285524e00b3SLiam R. Howlett if (range_has_overlap(mm, vma->vm_start, vma->vm_end, &prev)) 3286c9d13f5fSChen Gang return -ENOMEM; 3287d4af56c5SLiam R. Howlett 3288c9d13f5fSChen Gang if ((vma->vm_flags & VM_ACCOUNT) && 3289d4af56c5SLiam R. Howlett security_vm_enough_memory_mm(mm, charged)) 3290c9d13f5fSChen Gang return -ENOMEM; 3291c9d13f5fSChen Gang 32921da177e4SLinus Torvalds /* 32931da177e4SLinus Torvalds * The vm_pgoff of a purely anonymous vma should be irrelevant 32941da177e4SLinus Torvalds * until its first write fault, when page's anon_vma and index 32951da177e4SLinus Torvalds * are set. But now set the vm_pgoff it will almost certainly 32961da177e4SLinus Torvalds * end up with (unless mremap moves it elsewhere before that 32971da177e4SLinus Torvalds * first wfault), so /proc/pid/maps tells a consistent story. 32981da177e4SLinus Torvalds * 32991da177e4SLinus Torvalds * By setting it to reflect the virtual start address of the 33001da177e4SLinus Torvalds * vma, merges and splits can happen in a seamless way, just 33011da177e4SLinus Torvalds * using the existing file pgoff checks and manipulations. 33028332326eSLiao Pingfang * Similarly in do_mmap and in do_brk_flags. 33031da177e4SLinus Torvalds */ 33048a9cc3b5SOleg Nesterov if (vma_is_anonymous(vma)) { 33051da177e4SLinus Torvalds BUG_ON(vma->anon_vma); 33061da177e4SLinus Torvalds vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; 33071da177e4SLinus Torvalds } 33082b144498SSrikar Dronamraju 3309524e00b3SLiam R. Howlett if (vma_link(mm, vma, prev)) { 3310d4af56c5SLiam R. Howlett vm_unacct_memory(charged); 3311d4af56c5SLiam R. Howlett return -ENOMEM; 3312d4af56c5SLiam R. Howlett } 3313d4af56c5SLiam R. Howlett 33141da177e4SLinus Torvalds return 0; 33151da177e4SLinus Torvalds } 33161da177e4SLinus Torvalds 33171da177e4SLinus Torvalds /* 33181da177e4SLinus Torvalds * Copy the vma structure to a new location in the same mm, 33191da177e4SLinus Torvalds * prior to moving page table entries, to effect an mremap move. 33201da177e4SLinus Torvalds */ 33211da177e4SLinus Torvalds struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 332238a76013SMichel Lespinasse unsigned long addr, unsigned long len, pgoff_t pgoff, 332338a76013SMichel Lespinasse bool *need_rmap_locks) 33241da177e4SLinus Torvalds { 33251da177e4SLinus Torvalds struct vm_area_struct *vma = *vmap; 33261da177e4SLinus Torvalds unsigned long vma_start = vma->vm_start; 33271da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 33281da177e4SLinus Torvalds struct vm_area_struct *new_vma, *prev; 3329948f017bSAndrea Arcangeli bool faulted_in_anon_vma = true; 33301da177e4SLinus Torvalds 3331d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33321da177e4SLinus Torvalds /* 33331da177e4SLinus Torvalds * If anonymous vma has not yet been faulted, update new pgoff 33341da177e4SLinus Torvalds * to match new location, to increase its chance of merging. 33351da177e4SLinus Torvalds */ 3336ce75799bSOleg Nesterov if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 33371da177e4SLinus Torvalds pgoff = addr >> PAGE_SHIFT; 3338948f017bSAndrea Arcangeli faulted_in_anon_vma = false; 3339948f017bSAndrea Arcangeli } 33401da177e4SLinus Torvalds 3341524e00b3SLiam R. Howlett if (range_has_overlap(mm, addr, addr + len, &prev)) 33426597d783SHugh Dickins return NULL; /* should never get here */ 3343524e00b3SLiam R. Howlett 33441da177e4SLinus Torvalds new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, 334519a809afSAndrea Arcangeli vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), 33465c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 33471da177e4SLinus Torvalds if (new_vma) { 33481da177e4SLinus Torvalds /* 33491da177e4SLinus Torvalds * Source vma may have been merged into new_vma 33501da177e4SLinus Torvalds */ 3351948f017bSAndrea Arcangeli if (unlikely(vma_start >= new_vma->vm_start && 3352948f017bSAndrea Arcangeli vma_start < new_vma->vm_end)) { 3353948f017bSAndrea Arcangeli /* 3354948f017bSAndrea Arcangeli * The only way we can get a vma_merge with 3355948f017bSAndrea Arcangeli * self during an mremap is if the vma hasn't 3356948f017bSAndrea Arcangeli * been faulted in yet and we were allowed to 3357948f017bSAndrea Arcangeli * reset the dst vma->vm_pgoff to the 3358948f017bSAndrea Arcangeli * destination address of the mremap to allow 3359948f017bSAndrea Arcangeli * the merge to happen. mremap must change the 3360948f017bSAndrea Arcangeli * vm_pgoff linearity between src and dst vmas 3361948f017bSAndrea Arcangeli * (in turn preventing a vma_merge) to be 3362948f017bSAndrea Arcangeli * safe. It is only safe to keep the vm_pgoff 3363948f017bSAndrea Arcangeli * linear if there are no pages mapped yet. 3364948f017bSAndrea Arcangeli */ 336581d1b09cSSasha Levin VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 336638a76013SMichel Lespinasse *vmap = vma = new_vma; 3367108d6642SMichel Lespinasse } 336838a76013SMichel Lespinasse *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 33691da177e4SLinus Torvalds } else { 33703928d4f5SLinus Torvalds new_vma = vm_area_dup(vma); 3371e3975891SChen Gang if (!new_vma) 3372e3975891SChen Gang goto out; 33731da177e4SLinus Torvalds new_vma->vm_start = addr; 33741da177e4SLinus Torvalds new_vma->vm_end = addr + len; 33751da177e4SLinus Torvalds new_vma->vm_pgoff = pgoff; 3376ef0855d3SOleg Nesterov if (vma_dup_policy(vma, new_vma)) 3377523d4e20SMichel Lespinasse goto out_free_vma; 3378523d4e20SMichel Lespinasse if (anon_vma_clone(new_vma, vma)) 3379523d4e20SMichel Lespinasse goto out_free_mempol; 3380e9714acfSKonstantin Khlebnikov if (new_vma->vm_file) 33811da177e4SLinus Torvalds get_file(new_vma->vm_file); 33821da177e4SLinus Torvalds if (new_vma->vm_ops && new_vma->vm_ops->open) 33831da177e4SLinus Torvalds new_vma->vm_ops->open(new_vma); 3384524e00b3SLiam R. Howlett if (vma_link(mm, new_vma, prev)) 3385524e00b3SLiam R. Howlett goto out_vma_link; 338638a76013SMichel Lespinasse *need_rmap_locks = false; 33871da177e4SLinus Torvalds } 3388d4af56c5SLiam R. Howlett validate_mm_mt(mm); 33891da177e4SLinus Torvalds return new_vma; 33905beb4930SRik van Riel 3391524e00b3SLiam R. Howlett out_vma_link: 3392524e00b3SLiam R. Howlett if (new_vma->vm_ops && new_vma->vm_ops->close) 3393524e00b3SLiam R. Howlett new_vma->vm_ops->close(new_vma); 33945beb4930SRik van Riel out_free_mempol: 3395ef0855d3SOleg Nesterov mpol_put(vma_policy(new_vma)); 33965beb4930SRik van Riel out_free_vma: 33973928d4f5SLinus Torvalds vm_area_free(new_vma); 3398e3975891SChen Gang out: 3399d4af56c5SLiam R. Howlett validate_mm_mt(mm); 34005beb4930SRik van Riel return NULL; 34011da177e4SLinus Torvalds } 3402119f657cSakpm@osdl.org 3403119f657cSakpm@osdl.org /* 3404119f657cSakpm@osdl.org * Return true if the calling process may expand its vm space by the passed 3405119f657cSakpm@osdl.org * number of pages 3406119f657cSakpm@osdl.org */ 340784638335SKonstantin Khlebnikov bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) 3408119f657cSakpm@osdl.org { 340984638335SKonstantin Khlebnikov if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) 341084638335SKonstantin Khlebnikov return false; 3411119f657cSakpm@osdl.org 3412d977d56cSKonstantin Khlebnikov if (is_data_mapping(flags) && 3413d977d56cSKonstantin Khlebnikov mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { 3414f4fcd558SKonstantin Khlebnikov /* Workaround for Valgrind */ 3415f4fcd558SKonstantin Khlebnikov if (rlimit(RLIMIT_DATA) == 0 && 3416f4fcd558SKonstantin Khlebnikov mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) 3417f4fcd558SKonstantin Khlebnikov return true; 341857a7702bSDavid Woodhouse 341957a7702bSDavid Woodhouse pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", 3420d977d56cSKonstantin Khlebnikov current->comm, current->pid, 3421d977d56cSKonstantin Khlebnikov (mm->data_vm + npages) << PAGE_SHIFT, 342257a7702bSDavid Woodhouse rlimit(RLIMIT_DATA), 342357a7702bSDavid Woodhouse ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); 342457a7702bSDavid Woodhouse 342557a7702bSDavid Woodhouse if (!ignore_rlimit_data) 3426d977d56cSKonstantin Khlebnikov return false; 3427d977d56cSKonstantin Khlebnikov } 3428119f657cSakpm@osdl.org 342984638335SKonstantin Khlebnikov return true; 343084638335SKonstantin Khlebnikov } 343184638335SKonstantin Khlebnikov 343284638335SKonstantin Khlebnikov void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) 343384638335SKonstantin Khlebnikov { 34347866076bSPeng Liu WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); 343584638335SKonstantin Khlebnikov 3436d977d56cSKonstantin Khlebnikov if (is_exec_mapping(flags)) 343784638335SKonstantin Khlebnikov mm->exec_vm += npages; 3438d977d56cSKonstantin Khlebnikov else if (is_stack_mapping(flags)) 343984638335SKonstantin Khlebnikov mm->stack_vm += npages; 3440d977d56cSKonstantin Khlebnikov else if (is_data_mapping(flags)) 344184638335SKonstantin Khlebnikov mm->data_vm += npages; 3442119f657cSakpm@osdl.org } 3443fa5dc22fSRoland McGrath 3444b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf); 3445a62c34bdSAndy Lutomirski 3446a62c34bdSAndy Lutomirski /* 3447a62c34bdSAndy Lutomirski * Having a close hook prevents vma merging regardless of flags. 3448a62c34bdSAndy Lutomirski */ 3449a62c34bdSAndy Lutomirski static void special_mapping_close(struct vm_area_struct *vma) 3450a62c34bdSAndy Lutomirski { 3451a62c34bdSAndy Lutomirski } 3452a62c34bdSAndy Lutomirski 3453a62c34bdSAndy Lutomirski static const char *special_mapping_name(struct vm_area_struct *vma) 3454a62c34bdSAndy Lutomirski { 3455a62c34bdSAndy Lutomirski return ((struct vm_special_mapping *)vma->vm_private_data)->name; 3456a62c34bdSAndy Lutomirski } 3457a62c34bdSAndy Lutomirski 345814d07113SBrian Geffon static int special_mapping_mremap(struct vm_area_struct *new_vma) 3459b059a453SDmitry Safonov { 3460b059a453SDmitry Safonov struct vm_special_mapping *sm = new_vma->vm_private_data; 3461b059a453SDmitry Safonov 3462280e87e9SDmitry Safonov if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) 3463280e87e9SDmitry Safonov return -EFAULT; 3464280e87e9SDmitry Safonov 3465b059a453SDmitry Safonov if (sm->mremap) 3466b059a453SDmitry Safonov return sm->mremap(sm, new_vma); 3467280e87e9SDmitry Safonov 3468b059a453SDmitry Safonov return 0; 3469b059a453SDmitry Safonov } 3470b059a453SDmitry Safonov 3471871402e0SDmitry Safonov static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) 3472871402e0SDmitry Safonov { 3473871402e0SDmitry Safonov /* 3474871402e0SDmitry Safonov * Forbid splitting special mappings - kernel has expectations over 3475871402e0SDmitry Safonov * the number of pages in mapping. Together with VM_DONTEXPAND 3476871402e0SDmitry Safonov * the size of vma should stay the same over the special mapping's 3477871402e0SDmitry Safonov * lifetime. 3478871402e0SDmitry Safonov */ 3479871402e0SDmitry Safonov return -EINVAL; 3480871402e0SDmitry Safonov } 3481871402e0SDmitry Safonov 3482a62c34bdSAndy Lutomirski static const struct vm_operations_struct special_mapping_vmops = { 3483a62c34bdSAndy Lutomirski .close = special_mapping_close, 3484a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3485b059a453SDmitry Safonov .mremap = special_mapping_mremap, 3486a62c34bdSAndy Lutomirski .name = special_mapping_name, 3487af34ebebSDmitry Safonov /* vDSO code relies that VVAR can't be accessed remotely */ 3488af34ebebSDmitry Safonov .access = NULL, 3489871402e0SDmitry Safonov .may_split = special_mapping_split, 3490a62c34bdSAndy Lutomirski }; 3491a62c34bdSAndy Lutomirski 3492a62c34bdSAndy Lutomirski static const struct vm_operations_struct legacy_special_mapping_vmops = { 3493a62c34bdSAndy Lutomirski .close = special_mapping_close, 3494a62c34bdSAndy Lutomirski .fault = special_mapping_fault, 3495a62c34bdSAndy Lutomirski }; 3496fa5dc22fSRoland McGrath 3497b3ec9f33SSouptick Joarder static vm_fault_t special_mapping_fault(struct vm_fault *vmf) 3498fa5dc22fSRoland McGrath { 349911bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 3500b1d0e4f5SNick Piggin pgoff_t pgoff; 3501fa5dc22fSRoland McGrath struct page **pages; 3502fa5dc22fSRoland McGrath 3503f872f540SAndy Lutomirski if (vma->vm_ops == &legacy_special_mapping_vmops) { 3504a62c34bdSAndy Lutomirski pages = vma->vm_private_data; 3505f872f540SAndy Lutomirski } else { 3506f872f540SAndy Lutomirski struct vm_special_mapping *sm = vma->vm_private_data; 3507f872f540SAndy Lutomirski 3508f872f540SAndy Lutomirski if (sm->fault) 350911bac800SDave Jiang return sm->fault(sm, vmf->vma, vmf); 3510f872f540SAndy Lutomirski 3511f872f540SAndy Lutomirski pages = sm->pages; 3512f872f540SAndy Lutomirski } 3513a62c34bdSAndy Lutomirski 35148a9cc3b5SOleg Nesterov for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) 3515b1d0e4f5SNick Piggin pgoff--; 3516fa5dc22fSRoland McGrath 3517fa5dc22fSRoland McGrath if (*pages) { 3518fa5dc22fSRoland McGrath struct page *page = *pages; 3519fa5dc22fSRoland McGrath get_page(page); 3520b1d0e4f5SNick Piggin vmf->page = page; 3521b1d0e4f5SNick Piggin return 0; 3522fa5dc22fSRoland McGrath } 3523fa5dc22fSRoland McGrath 3524b1d0e4f5SNick Piggin return VM_FAULT_SIGBUS; 3525fa5dc22fSRoland McGrath } 3526fa5dc22fSRoland McGrath 3527a62c34bdSAndy Lutomirski static struct vm_area_struct *__install_special_mapping( 3528a62c34bdSAndy Lutomirski struct mm_struct *mm, 3529fa5dc22fSRoland McGrath unsigned long addr, unsigned long len, 353027f28b97SChen Gang unsigned long vm_flags, void *priv, 353127f28b97SChen Gang const struct vm_operations_struct *ops) 3532fa5dc22fSRoland McGrath { 3533462e635eSTavis Ormandy int ret; 3534fa5dc22fSRoland McGrath struct vm_area_struct *vma; 3535fa5dc22fSRoland McGrath 3536d4af56c5SLiam R. Howlett validate_mm_mt(mm); 3537490fc053SLinus Torvalds vma = vm_area_alloc(mm); 3538fa5dc22fSRoland McGrath if (unlikely(vma == NULL)) 35393935ed6aSStefani Seibold return ERR_PTR(-ENOMEM); 3540fa5dc22fSRoland McGrath 3541fa5dc22fSRoland McGrath vma->vm_start = addr; 3542fa5dc22fSRoland McGrath vma->vm_end = addr + len; 3543fa5dc22fSRoland McGrath 3544d9104d1cSCyrill Gorcunov vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY; 35451fc09228SHugh Dickins vma->vm_flags &= VM_LOCKED_CLEAR_MASK; 35463ed75eb8SColy Li vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 3547fa5dc22fSRoland McGrath 3548a62c34bdSAndy Lutomirski vma->vm_ops = ops; 3549a62c34bdSAndy Lutomirski vma->vm_private_data = priv; 3550fa5dc22fSRoland McGrath 3551462e635eSTavis Ormandy ret = insert_vm_struct(mm, vma); 3552462e635eSTavis Ormandy if (ret) 3553462e635eSTavis Ormandy goto out; 3554fa5dc22fSRoland McGrath 355584638335SKonstantin Khlebnikov vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); 3556fa5dc22fSRoland McGrath 3557cdd6c482SIngo Molnar perf_event_mmap(vma); 3558089dd79dSPeter Zijlstra 3559d4af56c5SLiam R. Howlett validate_mm_mt(mm); 35603935ed6aSStefani Seibold return vma; 3561462e635eSTavis Ormandy 3562462e635eSTavis Ormandy out: 35633928d4f5SLinus Torvalds vm_area_free(vma); 3564d4af56c5SLiam R. Howlett validate_mm_mt(mm); 35653935ed6aSStefani Seibold return ERR_PTR(ret); 35663935ed6aSStefani Seibold } 35673935ed6aSStefani Seibold 35682eefd878SDmitry Safonov bool vma_is_special_mapping(const struct vm_area_struct *vma, 35692eefd878SDmitry Safonov const struct vm_special_mapping *sm) 35702eefd878SDmitry Safonov { 35712eefd878SDmitry Safonov return vma->vm_private_data == sm && 35722eefd878SDmitry Safonov (vma->vm_ops == &special_mapping_vmops || 35732eefd878SDmitry Safonov vma->vm_ops == &legacy_special_mapping_vmops); 35742eefd878SDmitry Safonov } 35752eefd878SDmitry Safonov 3576a62c34bdSAndy Lutomirski /* 3577c1e8d7c6SMichel Lespinasse * Called with mm->mmap_lock held for writing. 3578a62c34bdSAndy Lutomirski * Insert a new vma covering the given region, with the given flags. 3579a62c34bdSAndy Lutomirski * Its pages are supplied by the given array of struct page *. 3580a62c34bdSAndy Lutomirski * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. 3581a62c34bdSAndy Lutomirski * The region past the last page supplied will always produce SIGBUS. 3582a62c34bdSAndy Lutomirski * The array pointer and the pages it points to are assumed to stay alive 3583a62c34bdSAndy Lutomirski * for as long as this mapping might exist. 3584a62c34bdSAndy Lutomirski */ 3585a62c34bdSAndy Lutomirski struct vm_area_struct *_install_special_mapping( 3586a62c34bdSAndy Lutomirski struct mm_struct *mm, 3587a62c34bdSAndy Lutomirski unsigned long addr, unsigned long len, 3588a62c34bdSAndy Lutomirski unsigned long vm_flags, const struct vm_special_mapping *spec) 3589a62c34bdSAndy Lutomirski { 359027f28b97SChen Gang return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, 359127f28b97SChen Gang &special_mapping_vmops); 3592a62c34bdSAndy Lutomirski } 3593a62c34bdSAndy Lutomirski 35943935ed6aSStefani Seibold int install_special_mapping(struct mm_struct *mm, 35953935ed6aSStefani Seibold unsigned long addr, unsigned long len, 35963935ed6aSStefani Seibold unsigned long vm_flags, struct page **pages) 35973935ed6aSStefani Seibold { 3598a62c34bdSAndy Lutomirski struct vm_area_struct *vma = __install_special_mapping( 359927f28b97SChen Gang mm, addr, len, vm_flags, (void *)pages, 360027f28b97SChen Gang &legacy_special_mapping_vmops); 36013935ed6aSStefani Seibold 360214bd5b45SDuan Jiong return PTR_ERR_OR_ZERO(vma); 3603fa5dc22fSRoland McGrath } 36047906d00cSAndrea Arcangeli 36057906d00cSAndrea Arcangeli static DEFINE_MUTEX(mm_all_locks_mutex); 36067906d00cSAndrea Arcangeli 3607454ed842SPeter Zijlstra static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 36087906d00cSAndrea Arcangeli { 3609f808c13fSDavidlohr Bueso if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 36107906d00cSAndrea Arcangeli /* 36117906d00cSAndrea Arcangeli * The LSB of head.next can't change from under us 36127906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 36137906d00cSAndrea Arcangeli */ 3614da1c55f1SMichel Lespinasse down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 36157906d00cSAndrea Arcangeli /* 36167906d00cSAndrea Arcangeli * We can safely modify head.next after taking the 36175a505085SIngo Molnar * anon_vma->root->rwsem. If some other vma in this mm shares 36187906d00cSAndrea Arcangeli * the same anon_vma we won't take it again. 36197906d00cSAndrea Arcangeli * 36207906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 36217906d00cSAndrea Arcangeli * can't change from under us thanks to the 36225a505085SIngo Molnar * anon_vma->root->rwsem. 36237906d00cSAndrea Arcangeli */ 36247906d00cSAndrea Arcangeli if (__test_and_set_bit(0, (unsigned long *) 3625f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 36267906d00cSAndrea Arcangeli BUG(); 36277906d00cSAndrea Arcangeli } 36287906d00cSAndrea Arcangeli } 36297906d00cSAndrea Arcangeli 3630454ed842SPeter Zijlstra static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 36317906d00cSAndrea Arcangeli { 36327906d00cSAndrea Arcangeli if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 36337906d00cSAndrea Arcangeli /* 36347906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change from under us because 36357906d00cSAndrea Arcangeli * we hold the mm_all_locks_mutex. 36367906d00cSAndrea Arcangeli * 36377906d00cSAndrea Arcangeli * Operations on ->flags have to be atomic because 36387906d00cSAndrea Arcangeli * even if AS_MM_ALL_LOCKS is stable thanks to the 36397906d00cSAndrea Arcangeli * mm_all_locks_mutex, there may be other cpus 36407906d00cSAndrea Arcangeli * changing other bitflags in parallel to us. 36417906d00cSAndrea Arcangeli */ 36427906d00cSAndrea Arcangeli if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 36437906d00cSAndrea Arcangeli BUG(); 3644da1c55f1SMichel Lespinasse down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 36457906d00cSAndrea Arcangeli } 36467906d00cSAndrea Arcangeli } 36477906d00cSAndrea Arcangeli 36487906d00cSAndrea Arcangeli /* 36497906d00cSAndrea Arcangeli * This operation locks against the VM for all pte/vma/mm related 36507906d00cSAndrea Arcangeli * operations that could ever happen on a certain mm. This includes 36517906d00cSAndrea Arcangeli * vmtruncate, try_to_unmap, and all page faults. 36527906d00cSAndrea Arcangeli * 3653c1e8d7c6SMichel Lespinasse * The caller must take the mmap_lock in write mode before calling 36547906d00cSAndrea Arcangeli * mm_take_all_locks(). The caller isn't allowed to release the 3655c1e8d7c6SMichel Lespinasse * mmap_lock until mm_drop_all_locks() returns. 36567906d00cSAndrea Arcangeli * 3657c1e8d7c6SMichel Lespinasse * mmap_lock in write mode is required in order to block all operations 36587906d00cSAndrea Arcangeli * that could modify pagetables and free pages without need of 365927ba0644SKirill A. Shutemov * altering the vma layout. It's also needed in write mode to avoid new 36607906d00cSAndrea Arcangeli * anon_vmas to be associated with existing vmas. 36617906d00cSAndrea Arcangeli * 36627906d00cSAndrea Arcangeli * A single task can't take more than one mm_take_all_locks() in a row 36637906d00cSAndrea Arcangeli * or it would deadlock. 36647906d00cSAndrea Arcangeli * 3665bf181b9fSMichel Lespinasse * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 36667906d00cSAndrea Arcangeli * mapping->flags avoid to take the same lock twice, if more than one 36677906d00cSAndrea Arcangeli * vma in this mm is backed by the same anon_vma or address_space. 36687906d00cSAndrea Arcangeli * 366988f306b6SKirill A. Shutemov * We take locks in following order, accordingly to comment at beginning 367088f306b6SKirill A. Shutemov * of mm/rmap.c: 367188f306b6SKirill A. Shutemov * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 367288f306b6SKirill A. Shutemov * hugetlb mapping); 367388f306b6SKirill A. Shutemov * - all i_mmap_rwsem locks; 367488f306b6SKirill A. Shutemov * - all anon_vma->rwseml 367588f306b6SKirill A. Shutemov * 367688f306b6SKirill A. Shutemov * We can take all locks within these types randomly because the VM code 367788f306b6SKirill A. Shutemov * doesn't nest them and we protected from parallel mm_take_all_locks() by 367888f306b6SKirill A. Shutemov * mm_all_locks_mutex. 36797906d00cSAndrea Arcangeli * 36807906d00cSAndrea Arcangeli * mm_take_all_locks() and mm_drop_all_locks are expensive operations 36817906d00cSAndrea Arcangeli * that may have to take thousand of locks. 36827906d00cSAndrea Arcangeli * 36837906d00cSAndrea Arcangeli * mm_take_all_locks() can fail if it's interrupted by signals. 36847906d00cSAndrea Arcangeli */ 36857906d00cSAndrea Arcangeli int mm_take_all_locks(struct mm_struct *mm) 36867906d00cSAndrea Arcangeli { 36877906d00cSAndrea Arcangeli struct vm_area_struct *vma; 36885beb4930SRik van Riel struct anon_vma_chain *avc; 36897906d00cSAndrea Arcangeli 3690325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 36917906d00cSAndrea Arcangeli 36927906d00cSAndrea Arcangeli mutex_lock(&mm_all_locks_mutex); 36937906d00cSAndrea Arcangeli 36947906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 36957906d00cSAndrea Arcangeli if (signal_pending(current)) 36967906d00cSAndrea Arcangeli goto out_unlock; 369788f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 369888f306b6SKirill A. Shutemov is_vm_hugetlb_page(vma)) 369988f306b6SKirill A. Shutemov vm_lock_mapping(mm, vma->vm_file->f_mapping); 370088f306b6SKirill A. Shutemov } 370188f306b6SKirill A. Shutemov 370288f306b6SKirill A. Shutemov for (vma = mm->mmap; vma; vma = vma->vm_next) { 370388f306b6SKirill A. Shutemov if (signal_pending(current)) 370488f306b6SKirill A. Shutemov goto out_unlock; 370588f306b6SKirill A. Shutemov if (vma->vm_file && vma->vm_file->f_mapping && 370688f306b6SKirill A. Shutemov !is_vm_hugetlb_page(vma)) 3707454ed842SPeter Zijlstra vm_lock_mapping(mm, vma->vm_file->f_mapping); 37087906d00cSAndrea Arcangeli } 37097cd5a02fSPeter Zijlstra 37107cd5a02fSPeter Zijlstra for (vma = mm->mmap; vma; vma = vma->vm_next) { 37117cd5a02fSPeter Zijlstra if (signal_pending(current)) 37127cd5a02fSPeter Zijlstra goto out_unlock; 37137cd5a02fSPeter Zijlstra if (vma->anon_vma) 37145beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 37155beb4930SRik van Riel vm_lock_anon_vma(mm, avc->anon_vma); 37167cd5a02fSPeter Zijlstra } 37177cd5a02fSPeter Zijlstra 3718584cff54SKautuk Consul return 0; 37197906d00cSAndrea Arcangeli 37207906d00cSAndrea Arcangeli out_unlock: 37217906d00cSAndrea Arcangeli mm_drop_all_locks(mm); 3722584cff54SKautuk Consul return -EINTR; 37237906d00cSAndrea Arcangeli } 37247906d00cSAndrea Arcangeli 37257906d00cSAndrea Arcangeli static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 37267906d00cSAndrea Arcangeli { 3727f808c13fSDavidlohr Bueso if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 37287906d00cSAndrea Arcangeli /* 37297906d00cSAndrea Arcangeli * The LSB of head.next can't change to 0 from under 37307906d00cSAndrea Arcangeli * us because we hold the mm_all_locks_mutex. 37317906d00cSAndrea Arcangeli * 37327906d00cSAndrea Arcangeli * We must however clear the bitflag before unlocking 3733bf181b9fSMichel Lespinasse * the vma so the users using the anon_vma->rb_root will 37347906d00cSAndrea Arcangeli * never see our bitflag. 37357906d00cSAndrea Arcangeli * 37367906d00cSAndrea Arcangeli * No need of atomic instructions here, head.next 37377906d00cSAndrea Arcangeli * can't change from under us until we release the 37385a505085SIngo Molnar * anon_vma->root->rwsem. 37397906d00cSAndrea Arcangeli */ 37407906d00cSAndrea Arcangeli if (!__test_and_clear_bit(0, (unsigned long *) 3741f808c13fSDavidlohr Bueso &anon_vma->root->rb_root.rb_root.rb_node)) 37427906d00cSAndrea Arcangeli BUG(); 374308b52706SKonstantin Khlebnikov anon_vma_unlock_write(anon_vma); 37447906d00cSAndrea Arcangeli } 37457906d00cSAndrea Arcangeli } 37467906d00cSAndrea Arcangeli 37477906d00cSAndrea Arcangeli static void vm_unlock_mapping(struct address_space *mapping) 37487906d00cSAndrea Arcangeli { 37497906d00cSAndrea Arcangeli if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 37507906d00cSAndrea Arcangeli /* 37517906d00cSAndrea Arcangeli * AS_MM_ALL_LOCKS can't change to 0 from under us 37527906d00cSAndrea Arcangeli * because we hold the mm_all_locks_mutex. 37537906d00cSAndrea Arcangeli */ 375483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 37557906d00cSAndrea Arcangeli if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 37567906d00cSAndrea Arcangeli &mapping->flags)) 37577906d00cSAndrea Arcangeli BUG(); 37587906d00cSAndrea Arcangeli } 37597906d00cSAndrea Arcangeli } 37607906d00cSAndrea Arcangeli 37617906d00cSAndrea Arcangeli /* 3762c1e8d7c6SMichel Lespinasse * The mmap_lock cannot be released by the caller until 37637906d00cSAndrea Arcangeli * mm_drop_all_locks() returns. 37647906d00cSAndrea Arcangeli */ 37657906d00cSAndrea Arcangeli void mm_drop_all_locks(struct mm_struct *mm) 37667906d00cSAndrea Arcangeli { 37677906d00cSAndrea Arcangeli struct vm_area_struct *vma; 37685beb4930SRik van Riel struct anon_vma_chain *avc; 37697906d00cSAndrea Arcangeli 3770325bca1fSRolf Eike Beer mmap_assert_write_locked(mm); 37717906d00cSAndrea Arcangeli BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 37727906d00cSAndrea Arcangeli 37737906d00cSAndrea Arcangeli for (vma = mm->mmap; vma; vma = vma->vm_next) { 37747906d00cSAndrea Arcangeli if (vma->anon_vma) 37755beb4930SRik van Riel list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 37765beb4930SRik van Riel vm_unlock_anon_vma(avc->anon_vma); 37777906d00cSAndrea Arcangeli if (vma->vm_file && vma->vm_file->f_mapping) 37787906d00cSAndrea Arcangeli vm_unlock_mapping(vma->vm_file->f_mapping); 37797906d00cSAndrea Arcangeli } 37807906d00cSAndrea Arcangeli 37817906d00cSAndrea Arcangeli mutex_unlock(&mm_all_locks_mutex); 37827906d00cSAndrea Arcangeli } 37838feae131SDavid Howells 37848feae131SDavid Howells /* 37853edf41d8Sseokhoon.yoon * initialise the percpu counter for VM 37868feae131SDavid Howells */ 37878feae131SDavid Howells void __init mmap_init(void) 37888feae131SDavid Howells { 378900a62ce9SKOSAKI Motohiro int ret; 379000a62ce9SKOSAKI Motohiro 3791908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 379200a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 37938feae131SDavid Howells } 3794c9b1d098SAndrew Shewmaker 3795c9b1d098SAndrew Shewmaker /* 3796c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 3797c9b1d098SAndrew Shewmaker * 3798c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 3799c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 3800c9b1d098SAndrew Shewmaker * mode. 3801c9b1d098SAndrew Shewmaker * 3802c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 3803c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 3804c9b1d098SAndrew Shewmaker */ 38051640879aSAndrew Shewmaker static int init_user_reserve(void) 3806c9b1d098SAndrew Shewmaker { 3807c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 3808c9b1d098SAndrew Shewmaker 3809c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 3810c9b1d098SAndrew Shewmaker 3811c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 3812c9b1d098SAndrew Shewmaker return 0; 3813c9b1d098SAndrew Shewmaker } 3814a64fb3cdSPaul Gortmaker subsys_initcall(init_user_reserve); 38154eeab4f5SAndrew Shewmaker 38164eeab4f5SAndrew Shewmaker /* 38174eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 38184eeab4f5SAndrew Shewmaker * 38194eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 38204eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 38214eeab4f5SAndrew Shewmaker * 38224eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 38234eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 38244eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 38254eeab4f5SAndrew Shewmaker */ 38261640879aSAndrew Shewmaker static int init_admin_reserve(void) 38274eeab4f5SAndrew Shewmaker { 38284eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 38294eeab4f5SAndrew Shewmaker 3830c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 38314eeab4f5SAndrew Shewmaker 38324eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 38334eeab4f5SAndrew Shewmaker return 0; 38344eeab4f5SAndrew Shewmaker } 3835a64fb3cdSPaul Gortmaker subsys_initcall(init_admin_reserve); 38361640879aSAndrew Shewmaker 38371640879aSAndrew Shewmaker /* 38381640879aSAndrew Shewmaker * Reinititalise user and admin reserves if memory is added or removed. 38391640879aSAndrew Shewmaker * 38401640879aSAndrew Shewmaker * The default user reserve max is 128MB, and the default max for the 38411640879aSAndrew Shewmaker * admin reserve is 8MB. These are usually, but not always, enough to 38421640879aSAndrew Shewmaker * enable recovery from a memory hogging process using login/sshd, a shell, 38431640879aSAndrew Shewmaker * and tools like top. It may make sense to increase or even disable the 38441640879aSAndrew Shewmaker * reserve depending on the existence of swap or variations in the recovery 38451640879aSAndrew Shewmaker * tools. So, the admin may have changed them. 38461640879aSAndrew Shewmaker * 38471640879aSAndrew Shewmaker * If memory is added and the reserves have been eliminated or increased above 38481640879aSAndrew Shewmaker * the default max, then we'll trust the admin. 38491640879aSAndrew Shewmaker * 38501640879aSAndrew Shewmaker * If memory is removed and there isn't enough free memory, then we 38511640879aSAndrew Shewmaker * need to reset the reserves. 38521640879aSAndrew Shewmaker * 38531640879aSAndrew Shewmaker * Otherwise keep the reserve set by the admin. 38541640879aSAndrew Shewmaker */ 38551640879aSAndrew Shewmaker static int reserve_mem_notifier(struct notifier_block *nb, 38561640879aSAndrew Shewmaker unsigned long action, void *data) 38571640879aSAndrew Shewmaker { 38581640879aSAndrew Shewmaker unsigned long tmp, free_kbytes; 38591640879aSAndrew Shewmaker 38601640879aSAndrew Shewmaker switch (action) { 38611640879aSAndrew Shewmaker case MEM_ONLINE: 38621640879aSAndrew Shewmaker /* Default max is 128MB. Leave alone if modified by operator. */ 38631640879aSAndrew Shewmaker tmp = sysctl_user_reserve_kbytes; 38641640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 17)) 38651640879aSAndrew Shewmaker init_user_reserve(); 38661640879aSAndrew Shewmaker 38671640879aSAndrew Shewmaker /* Default max is 8MB. Leave alone if modified by operator. */ 38681640879aSAndrew Shewmaker tmp = sysctl_admin_reserve_kbytes; 38691640879aSAndrew Shewmaker if (0 < tmp && tmp < (1UL << 13)) 38701640879aSAndrew Shewmaker init_admin_reserve(); 38711640879aSAndrew Shewmaker 38721640879aSAndrew Shewmaker break; 38731640879aSAndrew Shewmaker case MEM_OFFLINE: 3874c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 38751640879aSAndrew Shewmaker 38761640879aSAndrew Shewmaker if (sysctl_user_reserve_kbytes > free_kbytes) { 38771640879aSAndrew Shewmaker init_user_reserve(); 38781640879aSAndrew Shewmaker pr_info("vm.user_reserve_kbytes reset to %lu\n", 38791640879aSAndrew Shewmaker sysctl_user_reserve_kbytes); 38801640879aSAndrew Shewmaker } 38811640879aSAndrew Shewmaker 38821640879aSAndrew Shewmaker if (sysctl_admin_reserve_kbytes > free_kbytes) { 38831640879aSAndrew Shewmaker init_admin_reserve(); 38841640879aSAndrew Shewmaker pr_info("vm.admin_reserve_kbytes reset to %lu\n", 38851640879aSAndrew Shewmaker sysctl_admin_reserve_kbytes); 38861640879aSAndrew Shewmaker } 38871640879aSAndrew Shewmaker break; 38881640879aSAndrew Shewmaker default: 38891640879aSAndrew Shewmaker break; 38901640879aSAndrew Shewmaker } 38911640879aSAndrew Shewmaker return NOTIFY_OK; 38921640879aSAndrew Shewmaker } 38931640879aSAndrew Shewmaker 38941640879aSAndrew Shewmaker static struct notifier_block reserve_mem_nb = { 38951640879aSAndrew Shewmaker .notifier_call = reserve_mem_notifier, 38961640879aSAndrew Shewmaker }; 38971640879aSAndrew Shewmaker 38981640879aSAndrew Shewmaker static int __meminit init_reserve_notifier(void) 38991640879aSAndrew Shewmaker { 39001640879aSAndrew Shewmaker if (register_hotmemory_notifier(&reserve_mem_nb)) 3901b1de0d13SMitchel Humpherys pr_err("Failed registering memory add/remove notifier for admin reserve\n"); 39021640879aSAndrew Shewmaker 39031640879aSAndrew Shewmaker return 0; 39041640879aSAndrew Shewmaker } 3905a64fb3cdSPaul Gortmaker subsys_initcall(init_reserve_notifier); 3906