1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/nommu.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Replacement code for mm functions to support CPU's that don't 61da177e4SLinus Torvalds * have any form of memory management unit (thus no virtual memory). 71da177e4SLinus Torvalds * 8dd19d293SStephen Kitt * See Documentation/admin-guide/mm/nommu-mmap.rst 91da177e4SLinus Torvalds * 108feae131SDavid Howells * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com> 111da177e4SLinus Torvalds * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com> 121da177e4SLinus Torvalds * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org> 131da177e4SLinus Torvalds * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com> 1429c185e5SPaul Mundt * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org> 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 17b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18b1de0d13SMitchel Humpherys 19b95f1b31SPaul Gortmaker #include <linux/export.h> 201da177e4SLinus Torvalds #include <linux/mm.h> 216e84f315SIngo Molnar #include <linux/sched/mm.h> 221da177e4SLinus Torvalds #include <linux/mman.h> 231da177e4SLinus Torvalds #include <linux/swap.h> 241da177e4SLinus Torvalds #include <linux/file.h> 251da177e4SLinus Torvalds #include <linux/highmem.h> 261da177e4SLinus Torvalds #include <linux/pagemap.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/vmalloc.h> 291da177e4SLinus Torvalds #include <linux/backing-dev.h> 303b32123dSGideon Israel Dsouza #include <linux/compiler.h> 311da177e4SLinus Torvalds #include <linux/mount.h> 321da177e4SLinus Torvalds #include <linux/personality.h> 331da177e4SLinus Torvalds #include <linux/security.h> 341da177e4SLinus Torvalds #include <linux/syscalls.h> 35120a795dSAl Viro #include <linux/audit.h> 36b1de0d13SMitchel Humpherys #include <linux/printk.h> 371da177e4SLinus Torvalds 387c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 391da177e4SLinus Torvalds #include <asm/tlb.h> 401da177e4SLinus Torvalds #include <asm/tlbflush.h> 41eb8cdec4SBernd Schmidt #include <asm/mmu_context.h> 428feae131SDavid Howells #include "internal.h" 438feae131SDavid Howells 441da177e4SLinus Torvalds void *high_memory; 45944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory); 461da177e4SLinus Torvalds struct page *mem_map; 471da177e4SLinus Torvalds unsigned long max_mapnr; 485b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr); 494266c97aSHugh Dickins unsigned long highest_memmap_pfn; 50fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; 511da177e4SLinus Torvalds int heap_stack_gap = 0; 521da177e4SLinus Torvalds 5333e5d769SDavid Howells atomic_long_t mmap_pages_allocated; 548feae131SDavid Howells 551da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 561da177e4SLinus Torvalds 578feae131SDavid Howells /* list of mapped, potentially shareable regions */ 588feae131SDavid Howells static struct kmem_cache *vm_region_jar; 598feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT; 608feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem); 611da177e4SLinus Torvalds 62f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = { 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * Return the total memory allocated for this pointer, not 671da177e4SLinus Torvalds * just what the caller asked for. 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds * Doesn't have to be accurate, i.e. may have races. 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds unsigned int kobjsize(const void *objp) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct page *page; 741da177e4SLinus Torvalds 754016a139SMichael Hennerich /* 764016a139SMichael Hennerich * If the object we have should not have ksize performed on it, 774016a139SMichael Hennerich * return size of 0 784016a139SMichael Hennerich */ 795a1603beSPaul Mundt if (!objp || !virt_addr_valid(objp)) 806cfd53fcSPaul Mundt return 0; 816cfd53fcSPaul Mundt 826cfd53fcSPaul Mundt page = virt_to_head_page(objp); 836cfd53fcSPaul Mundt 846cfd53fcSPaul Mundt /* 856cfd53fcSPaul Mundt * If the allocator sets PageSlab, we know the pointer came from 866cfd53fcSPaul Mundt * kmalloc(). 876cfd53fcSPaul Mundt */ 881da177e4SLinus Torvalds if (PageSlab(page)) 891da177e4SLinus Torvalds return ksize(objp); 901da177e4SLinus Torvalds 916cfd53fcSPaul Mundt /* 92ab2e83eaSPaul Mundt * If it's not a compound page, see if we have a matching VMA 93ab2e83eaSPaul Mundt * region. This test is intentionally done in reverse order, 94ab2e83eaSPaul Mundt * so if there's no VMA, we still fall through and hand back 95ab2e83eaSPaul Mundt * PAGE_SIZE for 0-order pages. 96ab2e83eaSPaul Mundt */ 97ab2e83eaSPaul Mundt if (!PageCompound(page)) { 98ab2e83eaSPaul Mundt struct vm_area_struct *vma; 99ab2e83eaSPaul Mundt 100ab2e83eaSPaul Mundt vma = find_vma(current->mm, (unsigned long)objp); 101ab2e83eaSPaul Mundt if (vma) 102ab2e83eaSPaul Mundt return vma->vm_end - vma->vm_start; 103ab2e83eaSPaul Mundt } 104ab2e83eaSPaul Mundt 105ab2e83eaSPaul Mundt /* 1066cfd53fcSPaul Mundt * The ksize() function is only guaranteed to work for pointers 1075a1603beSPaul Mundt * returned by kmalloc(). So handle arbitrary pointers here. 1086cfd53fcSPaul Mundt */ 109a50b854eSMatthew Wilcox (Oracle) return page_size(page); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 112dfc2f91aSPaul Mundt /** 113dfc2f91aSPaul Mundt * follow_pfn - look up PFN at a user virtual address 114dfc2f91aSPaul Mundt * @vma: memory mapping 115dfc2f91aSPaul Mundt * @address: user virtual address 116dfc2f91aSPaul Mundt * @pfn: location to store found PFN 117dfc2f91aSPaul Mundt * 118dfc2f91aSPaul Mundt * Only IO mappings and raw PFN mappings are allowed. 119dfc2f91aSPaul Mundt * 120dfc2f91aSPaul Mundt * Returns zero and the pfn at @pfn on success, -ve otherwise. 121dfc2f91aSPaul Mundt */ 122dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address, 123dfc2f91aSPaul Mundt unsigned long *pfn) 124dfc2f91aSPaul Mundt { 125dfc2f91aSPaul Mundt if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 126dfc2f91aSPaul Mundt return -EINVAL; 127dfc2f91aSPaul Mundt 128dfc2f91aSPaul Mundt *pfn = address >> PAGE_SHIFT; 129dfc2f91aSPaul Mundt return 0; 130dfc2f91aSPaul Mundt } 131dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn); 132dfc2f91aSPaul Mundt 133f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 1341da177e4SLinus Torvalds 135b3bdda02SChristoph Lameter void vfree(const void *addr) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds kfree(addr); 1381da177e4SLinus Torvalds } 139b5073173SPaul Mundt EXPORT_SYMBOL(vfree); 1401da177e4SLinus Torvalds 14188dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds /* 1448518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 1458518609dSRobert P. J. Day * returns only a logical address. 1461da177e4SLinus Torvalds */ 14784097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 1481da177e4SLinus Torvalds } 149b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc); 1501da177e4SLinus Torvalds 151041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align, 152041de93fSChristoph Hellwig unsigned long start, unsigned long end, gfp_t gfp_mask, 153041de93fSChristoph Hellwig pgprot_t prot, unsigned long vm_flags, int node, 154041de93fSChristoph Hellwig const void *caller) 155041de93fSChristoph Hellwig { 156041de93fSChristoph Hellwig return __vmalloc(size, gfp_mask); 157041de93fSChristoph Hellwig } 158041de93fSChristoph Hellwig 1592b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, 1602b905948SChristoph Hellwig int node, const void *caller) 161a7c3e901SMichal Hocko { 1622b905948SChristoph Hellwig return __vmalloc(size, gfp_mask); 163a7c3e901SMichal Hocko } 164a7c3e901SMichal Hocko 165ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) 166f905bc44SPaul Mundt { 167f905bc44SPaul Mundt void *ret; 168f905bc44SPaul Mundt 16988dca4caSChristoph Hellwig ret = __vmalloc(size, flags); 170f905bc44SPaul Mundt if (ret) { 171f905bc44SPaul Mundt struct vm_area_struct *vma; 172f905bc44SPaul Mundt 173d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 174f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret); 175f905bc44SPaul Mundt if (vma) 176f905bc44SPaul Mundt vma->vm_flags |= VM_USERMAP; 177d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 178f905bc44SPaul Mundt } 179f905bc44SPaul Mundt 180f905bc44SPaul Mundt return ret; 181f905bc44SPaul Mundt } 182ed81745aSAndrii Nakryiko 183ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size) 184ed81745aSAndrii Nakryiko { 185ed81745aSAndrii Nakryiko return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO); 186ed81745aSAndrii Nakryiko } 187f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user); 188f905bc44SPaul Mundt 189b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds return virt_to_page(addr); 1921da177e4SLinus Torvalds } 193b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page); 1941da177e4SLinus Torvalds 195b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr)); 1981da177e4SLinus Torvalds } 199b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn); 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 2021da177e4SLinus Torvalds { 2039bde916bSChen Gang /* Don't allow overflow */ 2049bde916bSChen Gang if ((unsigned long) buf + count < count) 2059bde916bSChen Gang count = -(unsigned long) buf; 2069bde916bSChen Gang 2071da177e4SLinus Torvalds memcpy(buf, addr, count); 2081da177e4SLinus Torvalds return count; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* 212e1c05067SMasahiro Yamada * vmalloc - allocate virtually contiguous memory 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * @size: allocation size 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 217e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space. 2181da177e4SLinus Torvalds * 219c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 2201da177e4SLinus Torvalds * use __vmalloc() instead. 2211da177e4SLinus Torvalds */ 2221da177e4SLinus Torvalds void *vmalloc(unsigned long size) 2231da177e4SLinus Torvalds { 224176056fdSChen Li return __vmalloc(size, GFP_KERNEL); 2251da177e4SLinus Torvalds } 226f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc); 227f6138882SAndrew Morton 2280fc74d82SLinus Torvalds void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __weak __alias(__vmalloc); 2290fc74d82SLinus Torvalds 230e1ca7788SDave Young /* 231e1c05067SMasahiro Yamada * vzalloc - allocate virtually contiguous memory with zero fill 232e1ca7788SDave Young * 233e1ca7788SDave Young * @size: allocation size 234e1ca7788SDave Young * 235e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 236e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space. 237e1ca7788SDave Young * The memory allocated is set to zero. 238e1ca7788SDave Young * 239e1ca7788SDave Young * For tight control over page level allocator and protection flags 240e1ca7788SDave Young * use __vmalloc() instead. 241e1ca7788SDave Young */ 242e1ca7788SDave Young void *vzalloc(unsigned long size) 243e1ca7788SDave Young { 244176056fdSChen Li return __vmalloc(size, GFP_KERNEL | __GFP_ZERO); 245e1ca7788SDave Young } 246e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 247e1ca7788SDave Young 248e1ca7788SDave Young /** 249e1ca7788SDave Young * vmalloc_node - allocate memory on a specific node 250e1ca7788SDave Young * @size: allocation size 251e1ca7788SDave Young * @node: numa node 252e1ca7788SDave Young * 253e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 254e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 255e1ca7788SDave Young * 256e1ca7788SDave Young * For tight control over page level allocator and protection flags 257e1ca7788SDave Young * use __vmalloc() instead. 258e1ca7788SDave Young */ 259f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node) 260f6138882SAndrew Morton { 261f6138882SAndrew Morton return vmalloc(size); 262f6138882SAndrew Morton } 2639a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node); 264e1ca7788SDave Young 265e1ca7788SDave Young /** 266e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 267e1ca7788SDave Young * @size: allocation size 268e1ca7788SDave Young * @node: numa node 269e1ca7788SDave Young * 270e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 271e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 272e1ca7788SDave Young * The memory allocated is set to zero. 273e1ca7788SDave Young * 274e1ca7788SDave Young * For tight control over page level allocator and protection flags 275e1ca7788SDave Young * use __vmalloc() instead. 276e1ca7788SDave Young */ 277e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 278e1ca7788SDave Young { 279e1ca7788SDave Young return vzalloc(size); 280e1ca7788SDave Young } 281e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 2821da177e4SLinus Torvalds 2831af446edSPaul Mundt /** 284b5073173SPaul Mundt * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 2851da177e4SLinus Torvalds * @size: allocation size 2861da177e4SLinus Torvalds * 2871da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 288e1c05067SMasahiro Yamada * page level allocator and map them into contiguous kernel virtual space. 2891da177e4SLinus Torvalds */ 2901da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 2911da177e4SLinus Torvalds { 29288dca4caSChristoph Hellwig return __vmalloc(size, GFP_KERNEL); 2931da177e4SLinus Torvalds } 294b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32); 295b5073173SPaul Mundt 296b5073173SPaul Mundt /** 297b5073173SPaul Mundt * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 298b5073173SPaul Mundt * @size: allocation size 299b5073173SPaul Mundt * 300b5073173SPaul Mundt * The resulting memory area is 32bit addressable and zeroed so it can be 301b5073173SPaul Mundt * mapped to userspace without leaking data. 302f905bc44SPaul Mundt * 303f905bc44SPaul Mundt * VM_USERMAP is set on the corresponding VMA so that subsequent calls to 304f905bc44SPaul Mundt * remap_vmalloc_range() are permissible. 305b5073173SPaul Mundt */ 306b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size) 307b5073173SPaul Mundt { 308f905bc44SPaul Mundt /* 309f905bc44SPaul Mundt * We'll have to sort out the ZONE_DMA bits for 64-bit, 310f905bc44SPaul Mundt * but for now this can simply use vmalloc_user() directly. 311f905bc44SPaul Mundt */ 312f905bc44SPaul Mundt return vmalloc_user(size); 313b5073173SPaul Mundt } 314b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) 3171da177e4SLinus Torvalds { 3181da177e4SLinus Torvalds BUG(); 3191da177e4SLinus Torvalds return NULL; 3201da177e4SLinus Torvalds } 321b5073173SPaul Mundt EXPORT_SYMBOL(vmap); 3221da177e4SLinus Torvalds 323b3bdda02SChristoph Lameter void vunmap(const void *addr) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds BUG(); 3261da177e4SLinus Torvalds } 327b5073173SPaul Mundt EXPORT_SYMBOL(vunmap); 3281da177e4SLinus Torvalds 329d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 330eb6434d9SPaul Mundt { 331eb6434d9SPaul Mundt BUG(); 332eb6434d9SPaul Mundt return NULL; 333eb6434d9SPaul Mundt } 334eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram); 335eb6434d9SPaul Mundt 336eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count) 337eb6434d9SPaul Mundt { 338eb6434d9SPaul Mundt BUG(); 339eb6434d9SPaul Mundt } 340eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram); 341eb6434d9SPaul Mundt 342eb6434d9SPaul Mundt void vm_unmap_aliases(void) 343eb6434d9SPaul Mundt { 344eb6434d9SPaul Mundt } 345eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases); 346eb6434d9SPaul Mundt 34729c185e5SPaul Mundt void free_vm_area(struct vm_struct *area) 34829c185e5SPaul Mundt { 34929c185e5SPaul Mundt BUG(); 35029c185e5SPaul Mundt } 35129c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area); 35229c185e5SPaul Mundt 353b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 354b5073173SPaul Mundt struct page *page) 355b5073173SPaul Mundt { 356b5073173SPaul Mundt return -EINVAL; 357b5073173SPaul Mundt } 358b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page); 359b5073173SPaul Mundt 360a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 361a667d745SSouptick Joarder unsigned long num) 362a667d745SSouptick Joarder { 363a667d745SSouptick Joarder return -EINVAL; 364a667d745SSouptick Joarder } 365a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 366a667d745SSouptick Joarder 367a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 368a667d745SSouptick Joarder unsigned long num) 369a667d745SSouptick Joarder { 370a667d745SSouptick Joarder return -EINVAL; 371a667d745SSouptick Joarder } 372a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 373a667d745SSouptick Joarder 3741eeb66a1SChristoph Hellwig /* 3751da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel 3761da177e4SLinus Torvalds * lock, except when an application is doing something nasty 3771da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped 3781da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need 3791da177e4SLinus Torvalds * to invoke file system routines that need the global lock. 3801da177e4SLinus Torvalds */ 3816a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds if (brk < mm->start_brk || brk > mm->context.end_brk) 3861da177e4SLinus Torvalds return mm->brk; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (mm->brk == brk) 3891da177e4SLinus Torvalds return mm->brk; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* 3921da177e4SLinus Torvalds * Always allow shrinking brk 3931da177e4SLinus Torvalds */ 3941da177e4SLinus Torvalds if (brk <= mm->brk) { 3951da177e4SLinus Torvalds mm->brk = brk; 3961da177e4SLinus Torvalds return brk; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * Ok, looks good - let it rip. 4011da177e4SLinus Torvalds */ 402a75a2df6SChristoph Hellwig flush_icache_user_range(mm->brk, brk); 4031da177e4SLinus Torvalds return mm->brk = brk; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4068feae131SDavid Howells /* 4073edf41d8Sseokhoon.yoon * initialise the percpu counter for VM and region record slabs 4088feae131SDavid Howells */ 4098feae131SDavid Howells void __init mmap_init(void) 4101da177e4SLinus Torvalds { 41100a62ce9SKOSAKI Motohiro int ret; 41200a62ce9SKOSAKI Motohiro 413908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 41400a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 4155d097056SVladimir Davydov vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT); 4168feae131SDavid Howells } 4171da177e4SLinus Torvalds 4188feae131SDavid Howells /* 4198feae131SDavid Howells * validate the region tree 4208feae131SDavid Howells * - the caller must hold the region lock 4218feae131SDavid Howells */ 4228feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS 4238feae131SDavid Howells static noinline void validate_nommu_regions(void) 4248feae131SDavid Howells { 4258feae131SDavid Howells struct vm_region *region, *last; 4268feae131SDavid Howells struct rb_node *p, *lastp; 4271da177e4SLinus Torvalds 4288feae131SDavid Howells lastp = rb_first(&nommu_region_tree); 4298feae131SDavid Howells if (!lastp) 4308feae131SDavid Howells return; 4318feae131SDavid Howells 4328feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 433c9427bc0SGeliang Tang BUG_ON(last->vm_end <= last->vm_start); 434c9427bc0SGeliang Tang BUG_ON(last->vm_top < last->vm_end); 4358feae131SDavid Howells 4368feae131SDavid Howells while ((p = rb_next(lastp))) { 4378feae131SDavid Howells region = rb_entry(p, struct vm_region, vm_rb); 4388feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 4398feae131SDavid Howells 440c9427bc0SGeliang Tang BUG_ON(region->vm_end <= region->vm_start); 441c9427bc0SGeliang Tang BUG_ON(region->vm_top < region->vm_end); 442c9427bc0SGeliang Tang BUG_ON(region->vm_start < last->vm_top); 4438feae131SDavid Howells 4448feae131SDavid Howells lastp = p; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds } 4478feae131SDavid Howells #else 44833e5d769SDavid Howells static void validate_nommu_regions(void) 44933e5d769SDavid Howells { 45033e5d769SDavid Howells } 4518feae131SDavid Howells #endif 4528feae131SDavid Howells 4538feae131SDavid Howells /* 4548feae131SDavid Howells * add a region into the global tree 4558feae131SDavid Howells */ 4568feae131SDavid Howells static void add_nommu_region(struct vm_region *region) 4578feae131SDavid Howells { 4588feae131SDavid Howells struct vm_region *pregion; 4598feae131SDavid Howells struct rb_node **p, *parent; 4608feae131SDavid Howells 4618feae131SDavid Howells validate_nommu_regions(); 4628feae131SDavid Howells 4638feae131SDavid Howells parent = NULL; 4648feae131SDavid Howells p = &nommu_region_tree.rb_node; 4658feae131SDavid Howells while (*p) { 4668feae131SDavid Howells parent = *p; 4678feae131SDavid Howells pregion = rb_entry(parent, struct vm_region, vm_rb); 4688feae131SDavid Howells if (region->vm_start < pregion->vm_start) 4698feae131SDavid Howells p = &(*p)->rb_left; 4708feae131SDavid Howells else if (region->vm_start > pregion->vm_start) 4718feae131SDavid Howells p = &(*p)->rb_right; 4728feae131SDavid Howells else if (pregion == region) 4738feae131SDavid Howells return; 4748feae131SDavid Howells else 4758feae131SDavid Howells BUG(); 4768feae131SDavid Howells } 4778feae131SDavid Howells 4788feae131SDavid Howells rb_link_node(®ion->vm_rb, parent, p); 4798feae131SDavid Howells rb_insert_color(®ion->vm_rb, &nommu_region_tree); 4808feae131SDavid Howells 4818feae131SDavid Howells validate_nommu_regions(); 4828feae131SDavid Howells } 4838feae131SDavid Howells 4848feae131SDavid Howells /* 4858feae131SDavid Howells * delete a region from the global tree 4868feae131SDavid Howells */ 4878feae131SDavid Howells static void delete_nommu_region(struct vm_region *region) 4888feae131SDavid Howells { 4898feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 4908feae131SDavid Howells 4918feae131SDavid Howells validate_nommu_regions(); 4928feae131SDavid Howells rb_erase(®ion->vm_rb, &nommu_region_tree); 4938feae131SDavid Howells validate_nommu_regions(); 4948feae131SDavid Howells } 4958feae131SDavid Howells 4968feae131SDavid Howells /* 4978feae131SDavid Howells * free a contiguous series of pages 4988feae131SDavid Howells */ 4998feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to) 5008feae131SDavid Howells { 5018feae131SDavid Howells for (; from < to; from += PAGE_SIZE) { 5029330723cSLinus Walleij struct page *page = virt_to_page((void *)from); 5038feae131SDavid Howells 50433e5d769SDavid Howells atomic_long_dec(&mmap_pages_allocated); 5058feae131SDavid Howells put_page(page); 5068feae131SDavid Howells } 5078feae131SDavid Howells } 5088feae131SDavid Howells 5098feae131SDavid Howells /* 5108feae131SDavid Howells * release a reference to a region 51133e5d769SDavid Howells * - the caller must hold the region semaphore for writing, which this releases 512dd8632a1SPaul Mundt * - the region may not have been added to the tree yet, in which case vm_top 5138feae131SDavid Howells * will equal vm_start 5148feae131SDavid Howells */ 5158feae131SDavid Howells static void __put_nommu_region(struct vm_region *region) 5168feae131SDavid Howells __releases(nommu_region_sem) 5178feae131SDavid Howells { 5188feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 5198feae131SDavid Howells 5201e2ae599SDavid Howells if (--region->vm_usage == 0) { 521dd8632a1SPaul Mundt if (region->vm_top > region->vm_start) 5228feae131SDavid Howells delete_nommu_region(region); 5238feae131SDavid Howells up_write(&nommu_region_sem); 5248feae131SDavid Howells 5258feae131SDavid Howells if (region->vm_file) 5268feae131SDavid Howells fput(region->vm_file); 5278feae131SDavid Howells 5288feae131SDavid Howells /* IO memory and memory shared directly out of the pagecache 5298feae131SDavid Howells * from ramfs/tmpfs mustn't be released here */ 53022cc877bSLeon Romanovsky if (region->vm_flags & VM_MAPPED_COPY) 531dd8632a1SPaul Mundt free_page_series(region->vm_start, region->vm_top); 5328feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 5338feae131SDavid Howells } else { 5348feae131SDavid Howells up_write(&nommu_region_sem); 5358feae131SDavid Howells } 5368feae131SDavid Howells } 5378feae131SDavid Howells 5388feae131SDavid Howells /* 5398feae131SDavid Howells * release a reference to a region 5408feae131SDavid Howells */ 5418feae131SDavid Howells static void put_nommu_region(struct vm_region *region) 5428feae131SDavid Howells { 5438feae131SDavid Howells down_write(&nommu_region_sem); 5448feae131SDavid Howells __put_nommu_region(region); 5458feae131SDavid Howells } 5461da177e4SLinus Torvalds 547d4af56c5SLiam R. Howlett void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas) 548d4af56c5SLiam R. Howlett { 549d4af56c5SLiam R. Howlett mas_set_range(mas, vma->vm_start, vma->vm_end - 1); 550d4af56c5SLiam R. Howlett mas_store_prealloc(mas, vma); 551d4af56c5SLiam R. Howlett } 552d4af56c5SLiam R. Howlett 553d4af56c5SLiam R. Howlett void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas) 554d4af56c5SLiam R. Howlett { 555d4af56c5SLiam R. Howlett mas->index = vma->vm_start; 556d4af56c5SLiam R. Howlett mas->last = vma->vm_end - 1; 557d4af56c5SLiam R. Howlett mas_store_prealloc(mas, NULL); 558d4af56c5SLiam R. Howlett } 559d4af56c5SLiam R. Howlett 5608220543dSMatthew Wilcox (Oracle) static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm) 5613034097aSDavid Howells { 5628feae131SDavid Howells vma->vm_mm = mm; 5638feae131SDavid Howells 5648feae131SDavid Howells /* add the VMA to the mapping */ 5658feae131SDavid Howells if (vma->vm_file) { 5668220543dSMatthew Wilcox (Oracle) struct address_space *mapping = vma->vm_file->f_mapping; 5678feae131SDavid Howells 56883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 5698feae131SDavid Howells flush_dcache_mmap_lock(mapping); 5706b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5718feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 57283cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5738feae131SDavid Howells } 5748220543dSMatthew Wilcox (Oracle) } 5758feae131SDavid Howells 5768220543dSMatthew Wilcox (Oracle) /* 5778220543dSMatthew Wilcox (Oracle) * mas_add_vma_to_mm() - Maple state variant of add_mas_to_mm(). 5788220543dSMatthew Wilcox (Oracle) * @mas: The maple state with preallocations. 5798220543dSMatthew Wilcox (Oracle) * @mm: The mm_struct 5808220543dSMatthew Wilcox (Oracle) * @vma: The vma to add 5818220543dSMatthew Wilcox (Oracle) * 5828220543dSMatthew Wilcox (Oracle) */ 5838220543dSMatthew Wilcox (Oracle) static void mas_add_vma_to_mm(struct ma_state *mas, struct mm_struct *mm, 5848220543dSMatthew Wilcox (Oracle) struct vm_area_struct *vma) 5858220543dSMatthew Wilcox (Oracle) { 5868220543dSMatthew Wilcox (Oracle) BUG_ON(!vma->vm_region); 5878220543dSMatthew Wilcox (Oracle) 5888220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(vma, mm); 589fd9edbdbSLiam Howlett mm->map_count++; 5908220543dSMatthew Wilcox (Oracle) 5918feae131SDavid Howells /* add the VMA to the tree */ 5928220543dSMatthew Wilcox (Oracle) vma_mas_store(vma, mas); 5938feae131SDavid Howells } 5948feae131SDavid Howells 5958feae131SDavid Howells /* 5968220543dSMatthew Wilcox (Oracle) * add a VMA into a process's mm_struct in the appropriate place in the list 5978220543dSMatthew Wilcox (Oracle) * and tree and add to the address space's page tree also if not an anonymous 5988220543dSMatthew Wilcox (Oracle) * page 5998220543dSMatthew Wilcox (Oracle) * - should be called with mm->mmap_lock held writelocked 6008feae131SDavid Howells */ 6018220543dSMatthew Wilcox (Oracle) static int add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) 6028feae131SDavid Howells { 6038220543dSMatthew Wilcox (Oracle) MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end); 6048feae131SDavid Howells 6058220543dSMatthew Wilcox (Oracle) if (mas_preallocate(&mas, vma, GFP_KERNEL)) { 6068220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 6078220543dSMatthew Wilcox (Oracle) current->pid); 6088220543dSMatthew Wilcox (Oracle) return -ENOMEM; 6098220543dSMatthew Wilcox (Oracle) } 6108220543dSMatthew Wilcox (Oracle) mas_add_vma_to_mm(&mas, mm, vma); 6118220543dSMatthew Wilcox (Oracle) return 0; 6128220543dSMatthew Wilcox (Oracle) } 6138220543dSMatthew Wilcox (Oracle) 6148220543dSMatthew Wilcox (Oracle) static void cleanup_vma_from_mm(struct vm_area_struct *vma) 6158220543dSMatthew Wilcox (Oracle) { 6167964cf8cSLiam R. Howlett vma->vm_mm->map_count--; 6178feae131SDavid Howells /* remove the VMA from the mapping */ 6188feae131SDavid Howells if (vma->vm_file) { 6197964cf8cSLiam R. Howlett struct address_space *mapping; 6208feae131SDavid Howells mapping = vma->vm_file->f_mapping; 6218feae131SDavid Howells 62283cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 6238feae131SDavid Howells flush_dcache_mmap_lock(mapping); 6246b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 6258feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 62683cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 6278feae131SDavid Howells } 6288220543dSMatthew Wilcox (Oracle) } 6298220543dSMatthew Wilcox (Oracle) /* 6308220543dSMatthew Wilcox (Oracle) * delete a VMA from its owning mm_struct and address space 6318220543dSMatthew Wilcox (Oracle) */ 6328220543dSMatthew Wilcox (Oracle) static int delete_vma_from_mm(struct vm_area_struct *vma) 6338220543dSMatthew Wilcox (Oracle) { 6348220543dSMatthew Wilcox (Oracle) MA_STATE(mas, &vma->vm_mm->mm_mt, 0, 0); 6358220543dSMatthew Wilcox (Oracle) 6368220543dSMatthew Wilcox (Oracle) if (mas_preallocate(&mas, vma, GFP_KERNEL)) { 6378220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 6388220543dSMatthew Wilcox (Oracle) current->pid); 6398220543dSMatthew Wilcox (Oracle) return -ENOMEM; 6408220543dSMatthew Wilcox (Oracle) } 6418220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 6428feae131SDavid Howells 6438feae131SDavid Howells /* remove from the MM's tree and list */ 644524e00b3SLiam R. Howlett vma_mas_remove(vma, &mas); 6458220543dSMatthew Wilcox (Oracle) return 0; 6468feae131SDavid Howells } 6478feae131SDavid Howells 6488feae131SDavid Howells /* 6498feae131SDavid Howells * destroy a VMA record 6508feae131SDavid Howells */ 6518feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 6528feae131SDavid Howells { 6538feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 6548feae131SDavid Howells vma->vm_ops->close(vma); 655e9714acfSKonstantin Khlebnikov if (vma->vm_file) 6568feae131SDavid Howells fput(vma->vm_file); 6578feae131SDavid Howells put_nommu_region(vma->vm_region); 6583928d4f5SLinus Torvalds vm_area_free(vma); 6593034097aSDavid Howells } 6603034097aSDavid Howells 661abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 662abdba2ddSLiam R. Howlett unsigned long start_addr, 663abdba2ddSLiam R. Howlett unsigned long end_addr) 664abdba2ddSLiam R. Howlett { 665abdba2ddSLiam R. Howlett unsigned long index = start_addr; 666abdba2ddSLiam R. Howlett 667abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 668abdba2ddSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 669abdba2ddSLiam R. Howlett } 670abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 671abdba2ddSLiam R. Howlett 6723034097aSDavid Howells /* 6733034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 674c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6753034097aSDavid Howells */ 6763034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 6773034097aSDavid Howells { 678524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 6793034097aSDavid Howells 6807964cf8cSLiam R. Howlett return mas_walk(&mas); 6813034097aSDavid Howells } 6823034097aSDavid Howells EXPORT_SYMBOL(find_vma); 6833034097aSDavid Howells 6843034097aSDavid Howells /* 685930e652aSDavid Howells * find a VMA 686930e652aSDavid Howells * - we don't extend stack VMAs under NOMMU conditions 687930e652aSDavid Howells */ 688930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 689930e652aSDavid Howells { 6907561e8caSDavid Howells return find_vma(mm, addr); 691930e652aSDavid Howells } 692930e652aSDavid Howells 6938feae131SDavid Howells /* 6948feae131SDavid Howells * expand a stack to a given address 6958feae131SDavid Howells * - not supported under NOMMU conditions 6968feae131SDavid Howells */ 69757c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address) 69857c8f63eSGreg Ungerer { 69957c8f63eSGreg Ungerer return -ENOMEM; 70057c8f63eSGreg Ungerer } 70157c8f63eSGreg Ungerer 702930e652aSDavid Howells /* 7036fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 704c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 7056fa5f80bSDavid Howells */ 7068feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 7078feae131SDavid Howells unsigned long addr, 7088feae131SDavid Howells unsigned long len) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds struct vm_area_struct *vma; 7118feae131SDavid Howells unsigned long end = addr + len; 712524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 7131da177e4SLinus Torvalds 714524e00b3SLiam R. Howlett vma = mas_walk(&mas); 715524e00b3SLiam R. Howlett if (!vma) 7168feae131SDavid Howells return NULL; 717524e00b3SLiam R. Howlett if (vma->vm_start != addr) 718524e00b3SLiam R. Howlett return NULL; 719524e00b3SLiam R. Howlett if (vma->vm_end != end) 720524e00b3SLiam R. Howlett return NULL; 721524e00b3SLiam R. Howlett 7228feae131SDavid Howells return vma; 7238feae131SDavid Howells } 7241da177e4SLinus Torvalds 7253034097aSDavid Howells /* 7261da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 7271da177e4SLinus Torvalds * mapping we're capable of supporting 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 7301da177e4SLinus Torvalds unsigned long addr, 7311da177e4SLinus Torvalds unsigned long len, 7321da177e4SLinus Torvalds unsigned long prot, 7331da177e4SLinus Torvalds unsigned long flags, 7341da177e4SLinus Torvalds unsigned long pgoff, 7351da177e4SLinus Torvalds unsigned long *_capabilities) 7361da177e4SLinus Torvalds { 7378feae131SDavid Howells unsigned long capabilities, rlen; 7381da177e4SLinus Torvalds int ret; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds /* do the simple checks first */ 74122cc877bSLeon Romanovsky if (flags & MAP_FIXED) 7421da177e4SLinus Torvalds return -EINVAL; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 7451da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 7461da177e4SLinus Torvalds return -EINVAL; 7471da177e4SLinus Torvalds 748f81cff0dSMike Frysinger if (!len) 7491da177e4SLinus Torvalds return -EINVAL; 7501da177e4SLinus Torvalds 751f81cff0dSMike Frysinger /* Careful about overflows.. */ 7528feae131SDavid Howells rlen = PAGE_ALIGN(len); 7538feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 754f81cff0dSMike Frysinger return -ENOMEM; 755f81cff0dSMike Frysinger 7561da177e4SLinus Torvalds /* offset overflow? */ 7578feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 758f81cff0dSMike Frysinger return -EOVERFLOW; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds if (file) { 7611da177e4SLinus Torvalds /* files must support mmap */ 76272c2d531SAl Viro if (!file->f_op->mmap) 7631da177e4SLinus Torvalds return -ENODEV; 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 7661da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 7671da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 7681da177e4SLinus Torvalds */ 769b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 770b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 771b4caecd4SChristoph Hellwig } else { 7721da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 7731da177e4SLinus Torvalds * defaults */ 774496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 7751da177e4SLinus Torvalds case S_IFREG: 7761da177e4SLinus Torvalds case S_IFBLK: 777b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 7781da177e4SLinus Torvalds break; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds case S_IFCHR: 7811da177e4SLinus Torvalds capabilities = 782b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 783b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 784b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 7851da177e4SLinus Torvalds break; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds default: 7881da177e4SLinus Torvalds return -EINVAL; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 7931da177e4SLinus Torvalds * device */ 7941da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 795b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7966e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 797b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 7981da177e4SLinus Torvalds 79928d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 80028d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 80128d7a6aeSGraff Yang return -EACCES; 80228d7a6aeSGraff Yang 8031da177e4SLinus Torvalds if (flags & MAP_SHARED) { 8041da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 8051da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 8061da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 8071da177e4SLinus Torvalds return -EACCES; 8081da177e4SLinus Torvalds 809496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 8101da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 8111da177e4SLinus Torvalds return -EACCES; 8121da177e4SLinus Torvalds 813b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 8141da177e4SLinus Torvalds return -ENODEV; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 817b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 818ac714904SChoi Gi-yong } else { 8191da177e4SLinus Torvalds /* we're going to read the file into private memory we 8201da177e4SLinus Torvalds * allocate */ 821b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 8221da177e4SLinus Torvalds return -ENODEV; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 8251da177e4SLinus Torvalds * shared with the backing device */ 8261da177e4SLinus Torvalds if (prot & PROT_WRITE) 827b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 830b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 831b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 832b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 833b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 8343c7b2045SBernd Schmidt ) { 835b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8363c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 83722cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 8383c7b2045SBernd Schmidt return -EINVAL; 8393c7b2045SBernd Schmidt } 8403c7b2045SBernd Schmidt } 8413c7b2045SBernd Schmidt } 8423c7b2045SBernd Schmidt 8431da177e4SLinus Torvalds /* handle executable mappings and implied executable 8441da177e4SLinus Torvalds * mappings */ 84590f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 8461da177e4SLinus Torvalds if (prot & PROT_EXEC) 8471da177e4SLinus Torvalds return -EPERM; 848ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 8491da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 8501da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 851b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 8521da177e4SLinus Torvalds prot |= PROT_EXEC; 8531da177e4SLinus Torvalds } 854ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 8551da177e4SLinus Torvalds (prot & PROT_EXEC) && 856b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 8571da177e4SLinus Torvalds ) { 8581da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 859b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8601da177e4SLinus Torvalds } 861ac714904SChoi Gi-yong } else { 8621da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 8631da177e4SLinus Torvalds * privately mapped 8641da177e4SLinus Torvalds */ 865b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 8681da177e4SLinus Torvalds if ((prot & PROT_READ) && 8691da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 8701da177e4SLinus Torvalds prot |= PROT_EXEC; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds /* allow the security API to have its say */ 874e5467859SAl Viro ret = security_mmap_addr(addr); 875e5467859SAl Viro if (ret < 0) 876e5467859SAl Viro return ret; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* looks okay */ 8791da177e4SLinus Torvalds *_capabilities = capabilities; 8801da177e4SLinus Torvalds return 0; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* 8841da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 8851da177e4SLinus Torvalds * now know into VMA flags 8861da177e4SLinus Torvalds */ 8871da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 8881da177e4SLinus Torvalds unsigned long prot, 8891da177e4SLinus Torvalds unsigned long flags, 8901da177e4SLinus Torvalds unsigned long capabilities) 8911da177e4SLinus Torvalds { 8921da177e4SLinus Torvalds unsigned long vm_flags; 8931da177e4SLinus Torvalds 894e6bfb709SDave Hansen vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); 8951da177e4SLinus Torvalds 896*b6b7a8faSDavid Hildenbrand if (!file) { 897*b6b7a8faSDavid Hildenbrand /* 898*b6b7a8faSDavid Hildenbrand * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because 899*b6b7a8faSDavid Hildenbrand * there is no fork(). 9001da177e4SLinus Torvalds */ 901*b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 902*b6b7a8faSDavid Hildenbrand } else if (flags & MAP_PRIVATE) { 903*b6b7a8faSDavid Hildenbrand /* MAP_PRIVATE file mapping */ 904*b6b7a8faSDavid Hildenbrand if (capabilities & NOMMU_MAP_DIRECT) 905*b6b7a8faSDavid Hildenbrand vm_flags |= (capabilities & NOMMU_VMFLAGS); 906*b6b7a8faSDavid Hildenbrand else 907*b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 908*b6b7a8faSDavid Hildenbrand 909*b6b7a8faSDavid Hildenbrand if (!(prot & PROT_WRITE) && !current->ptrace) 910*b6b7a8faSDavid Hildenbrand /* 911*b6b7a8faSDavid Hildenbrand * R/O private file mapping which cannot be used to 912*b6b7a8faSDavid Hildenbrand * modify memory, especially also not via active ptrace 913*b6b7a8faSDavid Hildenbrand * (e.g., set breakpoints) or later by upgrading 914*b6b7a8faSDavid Hildenbrand * permissions (no mprotect()). We can try overlaying 915*b6b7a8faSDavid Hildenbrand * the file mapping, which will work e.g., on chardevs, 916*b6b7a8faSDavid Hildenbrand * ramfs/tmpfs/shmfs and romfs/cramf. 917*b6b7a8faSDavid Hildenbrand */ 918*b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYOVERLAY; 919*b6b7a8faSDavid Hildenbrand } else { 920*b6b7a8faSDavid Hildenbrand /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */ 921*b6b7a8faSDavid Hildenbrand vm_flags |= VM_SHARED | VM_MAYSHARE | 922*b6b7a8faSDavid Hildenbrand (capabilities & NOMMU_VMFLAGS); 923*b6b7a8faSDavid Hildenbrand } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds return vm_flags; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* 9298feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 9308feae131SDavid Howells * pins the storage) 9311da177e4SLinus Torvalds */ 9328feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 9331da177e4SLinus Torvalds { 9341da177e4SLinus Torvalds int ret; 9351da177e4SLinus Torvalds 936f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 937dd8632a1SPaul Mundt if (ret == 0) { 938dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 939645d83c5SDavid Howells return 0; 940dd8632a1SPaul Mundt } 9411da177e4SLinus Torvalds if (ret != -ENOSYS) 9421da177e4SLinus Torvalds return ret; 9431da177e4SLinus Torvalds 9443fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 9453fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 9463fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 9471da177e4SLinus Torvalds return -ENODEV; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds /* 9511da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 9521da177e4SLinus Torvalds */ 9538feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 9548feae131SDavid Howells struct vm_region *region, 955645d83c5SDavid Howells unsigned long len, 956645d83c5SDavid Howells unsigned long capabilities) 9571da177e4SLinus Torvalds { 958dbc8358cSJoonsoo Kim unsigned long total, point; 9591da177e4SLinus Torvalds void *base; 9608feae131SDavid Howells int ret, order; 9611da177e4SLinus Torvalds 962*b6b7a8faSDavid Hildenbrand /* 963*b6b7a8faSDavid Hildenbrand * Invoke the file's mapping function so that it can keep track of 964*b6b7a8faSDavid Hildenbrand * shared mappings on devices or memory. VM_MAYOVERLAY will be set if 965*b6b7a8faSDavid Hildenbrand * it may attempt to share, which will make is_nommu_shared_mapping() 966*b6b7a8faSDavid Hildenbrand * happy. 9671da177e4SLinus Torvalds */ 968b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 969f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 9701da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 971fc4f4be9SDavid Hildenbrand if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags))) 972fc4f4be9SDavid Hildenbrand ret = -ENOSYS; 973fc4f4be9SDavid Hildenbrand if (ret == 0) { 974dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 975645d83c5SDavid Howells return 0; 9761da177e4SLinus Torvalds } 977dd8632a1SPaul Mundt if (ret != -ENOSYS) 978dd8632a1SPaul Mundt return ret; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 9811da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 9821da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9858feae131SDavid Howells 9861da177e4SLinus Torvalds /* allocate some memory to hold the mapping 9871da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 9881da177e4SLinus Torvalds * we're allocating is smaller than a page 9891da177e4SLinus Torvalds */ 990f67d9b15SBob Liu order = get_order(len); 9918feae131SDavid Howells total = 1 << order; 992f67d9b15SBob Liu point = len >> PAGE_SHIFT; 993dd8632a1SPaul Mundt 994dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 99522cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 996dbc8358cSJoonsoo Kim total = point; 9978feae131SDavid Howells 998da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 999dbc8358cSJoonsoo Kim if (!base) 1000dbc8358cSJoonsoo Kim goto enomem; 10018feae131SDavid Howells 1002dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 1003dbc8358cSJoonsoo Kim 10048feae131SDavid Howells region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; 10058feae131SDavid Howells region->vm_start = (unsigned long) base; 1006f67d9b15SBob Liu region->vm_end = region->vm_start + len; 1007dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 10088feae131SDavid Howells 10098feae131SDavid Howells vma->vm_start = region->vm_start; 10108feae131SDavid Howells vma->vm_end = region->vm_start + len; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds if (vma->vm_file) { 10131da177e4SLinus Torvalds /* read the contents of a file into the copy */ 10141da177e4SLinus Torvalds loff_t fpos; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds fpos = vma->vm_pgoff; 10171da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 10181da177e4SLinus Torvalds 1019b4bf802aSChristoph Hellwig ret = kernel_read(vma->vm_file, base, len, &fpos); 10201da177e4SLinus Torvalds if (ret < 0) 10211da177e4SLinus Torvalds goto error_free; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds /* clear the last little bit */ 1024f67d9b15SBob Liu if (ret < len) 1025f67d9b15SBob Liu memset(base + ret, 0, len - ret); 10261da177e4SLinus Torvalds 1027bfd40eafSKirill A. Shutemov } else { 1028bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds return 0; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds error_free: 10347223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 10358feae131SDavid Howells region->vm_start = vma->vm_start = 0; 10368feae131SDavid Howells region->vm_end = vma->vm_end = 0; 1037dd8632a1SPaul Mundt region->vm_top = 0; 10381da177e4SLinus Torvalds return ret; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds enomem: 1041b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 104205ae6fa3SGreg Ungerer len, current->pid, current->comm); 10439af744d7SMichal Hocko show_free_areas(0, NULL); 10441da177e4SLinus Torvalds return -ENOMEM; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* 10481da177e4SLinus Torvalds * handle mapping creation for uClinux 10491da177e4SLinus Torvalds */ 10501fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, 10511da177e4SLinus Torvalds unsigned long addr, 10521da177e4SLinus Torvalds unsigned long len, 10531da177e4SLinus Torvalds unsigned long prot, 10541da177e4SLinus Torvalds unsigned long flags, 1055bebeb3d6SMichel Lespinasse unsigned long pgoff, 1056897ab3e0SMike Rapoport unsigned long *populate, 1057897ab3e0SMike Rapoport struct list_head *uf) 10581da177e4SLinus Torvalds { 10598feae131SDavid Howells struct vm_area_struct *vma; 10608feae131SDavid Howells struct vm_region *region; 10611da177e4SLinus Torvalds struct rb_node *rb; 106245e55300SPeter Collingbourne vm_flags_t vm_flags; 10631fcfd8dbSOleg Nesterov unsigned long capabilities, result; 10641da177e4SLinus Torvalds int ret; 10658220543dSMatthew Wilcox (Oracle) MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); 10661da177e4SLinus Torvalds 106741badc15SMichel Lespinasse *populate = 0; 1068bebeb3d6SMichel Lespinasse 10691da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 10701da177e4SLinus Torvalds * mapping */ 10711da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 10721da177e4SLinus Torvalds &capabilities); 107322cc877bSLeon Romanovsky if (ret < 0) 10741da177e4SLinus Torvalds return ret; 10751da177e4SLinus Torvalds 107606aab5a3SDavid Howells /* we ignore the address hint */ 107706aab5a3SDavid Howells addr = 0; 1078f67d9b15SBob Liu len = PAGE_ALIGN(len); 107906aab5a3SDavid Howells 10801da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 10811da177e4SLinus Torvalds * now know into VMA flags */ 108245e55300SPeter Collingbourne vm_flags = determine_vm_flags(file, prot, flags, capabilities); 10831da177e4SLinus Torvalds 10848220543dSMatthew Wilcox (Oracle) 10858feae131SDavid Howells /* we're going to need to record the mapping */ 10868feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 10878feae131SDavid Howells if (!region) 10888feae131SDavid Howells goto error_getting_region; 10891da177e4SLinus Torvalds 1090490fc053SLinus Torvalds vma = vm_area_alloc(current->mm); 10918feae131SDavid Howells if (!vma) 10928feae131SDavid Howells goto error_getting_vma; 10931da177e4SLinus Torvalds 10948220543dSMatthew Wilcox (Oracle) if (mas_preallocate(&mas, vma, GFP_KERNEL)) 10958220543dSMatthew Wilcox (Oracle) goto error_maple_preallocate; 10968220543dSMatthew Wilcox (Oracle) 10971e2ae599SDavid Howells region->vm_usage = 1; 10988feae131SDavid Howells region->vm_flags = vm_flags; 10998feae131SDavid Howells region->vm_pgoff = pgoff; 11008feae131SDavid Howells 11018feae131SDavid Howells vma->vm_flags = vm_flags; 11028feae131SDavid Howells vma->vm_pgoff = pgoff; 11038feae131SDavid Howells 11048feae131SDavid Howells if (file) { 1105cb0942b8SAl Viro region->vm_file = get_file(file); 1106cb0942b8SAl Viro vma->vm_file = get_file(file); 11078feae131SDavid Howells } 11088feae131SDavid Howells 11098feae131SDavid Howells down_write(&nommu_region_sem); 11108feae131SDavid Howells 11118feae131SDavid Howells /* if we want to share, we need to check for regions created by other 11121da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 11138feae131SDavid Howells * - we can only share with a superset match on most regular files 11141da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 11151da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 11161da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 11171da177e4SLinus Torvalds * than here 11181da177e4SLinus Torvalds */ 1119fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vm_flags)) { 11208feae131SDavid Howells struct vm_region *pregion; 11218feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 11221da177e4SLinus Torvalds 11238feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 11248feae131SDavid Howells pgend = pgoff + pglen; 1125165b2392SDavid Howells 11268feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 11278feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 11281da177e4SLinus Torvalds 1129fc4f4be9SDavid Hildenbrand if (!is_nommu_shared_mapping(pregion->vm_flags)) 11301da177e4SLinus Torvalds continue; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1133496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1134496ad9aaSAl Viro file_inode(file)) 11351da177e4SLinus Torvalds continue; 11361da177e4SLinus Torvalds 11378feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 11381da177e4SLinus Torvalds continue; 11391da177e4SLinus Torvalds 11408feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 11418feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 11428feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 11438feae131SDavid Howells if (pgoff >= rpgend) 11441da177e4SLinus Torvalds continue; 11451da177e4SLinus Torvalds 11468feae131SDavid Howells /* handle inexactly overlapping matches between 11478feae131SDavid Howells * mappings */ 11488feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 11498feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 11508feae131SDavid Howells /* new mapping is not a subset of the region */ 1151b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 11521da177e4SLinus Torvalds goto sharing_violation; 11531da177e4SLinus Torvalds continue; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11568feae131SDavid Howells /* we've found a region we can share */ 11571e2ae599SDavid Howells pregion->vm_usage++; 11588feae131SDavid Howells vma->vm_region = pregion; 11598feae131SDavid Howells start = pregion->vm_start; 11608feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 11618feae131SDavid Howells vma->vm_start = start; 11628feae131SDavid Howells vma->vm_end = start + len; 11631da177e4SLinus Torvalds 116422cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 11658feae131SDavid Howells vma->vm_flags |= VM_MAPPED_COPY; 116622cc877bSLeon Romanovsky else { 11678feae131SDavid Howells ret = do_mmap_shared_file(vma); 11688feae131SDavid Howells if (ret < 0) { 11698feae131SDavid Howells vma->vm_region = NULL; 11708feae131SDavid Howells vma->vm_start = 0; 11718feae131SDavid Howells vma->vm_end = 0; 11721e2ae599SDavid Howells pregion->vm_usage--; 11738feae131SDavid Howells pregion = NULL; 11748feae131SDavid Howells goto error_just_free; 11751da177e4SLinus Torvalds } 11768feae131SDavid Howells } 11778feae131SDavid Howells fput(region->vm_file); 11788feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 11798feae131SDavid Howells region = pregion; 11808feae131SDavid Howells result = start; 11818feae131SDavid Howells goto share; 11828feae131SDavid Howells } 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 11851da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 11861da177e4SLinus Torvalds * tell us the location of a shared mapping 11871da177e4SLinus Torvalds */ 1188b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11891da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 11901da177e4SLinus Torvalds pgoff, flags); 1191bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 11921da177e4SLinus Torvalds ret = addr; 1193bb005a59SNamhyung Kim if (ret != -ENOSYS) 11948feae131SDavid Howells goto error_just_free; 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /* the driver refused to tell us where to site 11971da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 11981da177e4SLinus Torvalds * it */ 1199bb005a59SNamhyung Kim ret = -ENODEV; 1200b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 12018feae131SDavid Howells goto error_just_free; 12021da177e4SLinus Torvalds 1203b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 12048feae131SDavid Howells } else { 12058feae131SDavid Howells vma->vm_start = region->vm_start = addr; 12068feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 12118feae131SDavid Howells vma->vm_region = region; 12121da177e4SLinus Torvalds 1213645d83c5SDavid Howells /* set up the mapping 1214b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1215645d83c5SDavid Howells */ 12161da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 12178feae131SDavid Howells ret = do_mmap_shared_file(vma); 12181da177e4SLinus Torvalds else 1219645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 12201da177e4SLinus Torvalds if (ret < 0) 1221645d83c5SDavid Howells goto error_just_free; 1222645d83c5SDavid Howells add_nommu_region(region); 12238feae131SDavid Howells 1224ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 12250bf5f949SChristoph Hellwig if (!vma->vm_file && 12260bf5f949SChristoph Hellwig (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) || 12270bf5f949SChristoph Hellwig !(flags & MAP_UNINITIALIZED))) 1228ea637639SJie Zhang memset((void *)region->vm_start, 0, 1229ea637639SJie Zhang region->vm_end - region->vm_start); 1230ea637639SJie Zhang 12311da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 12328feae131SDavid Howells result = vma->vm_start; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 12351da177e4SLinus Torvalds 12368feae131SDavid Howells share: 12378220543dSMatthew Wilcox (Oracle) mas_add_vma_to_mm(&mas, current->mm, vma); 12381da177e4SLinus Torvalds 1239cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable 1240cfe79c00SMike Frysinger * mapping of it is made */ 1241cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1242a75a2df6SChristoph Hellwig flush_icache_user_range(region->vm_start, region->vm_end); 1243cfe79c00SMike Frysinger region->vm_icache_flushed = true; 1244cfe79c00SMike Frysinger } 12451da177e4SLinus Torvalds 1246cfe79c00SMike Frysinger up_write(&nommu_region_sem); 12471da177e4SLinus Torvalds 12488feae131SDavid Howells return result; 12491da177e4SLinus Torvalds 12508feae131SDavid Howells error_just_free: 12518feae131SDavid Howells up_write(&nommu_region_sem); 12528feae131SDavid Howells error: 12537f31ccedSLiam Howlett mas_destroy(&mas); 125489a86402SDavid Howells if (region->vm_file) 12558feae131SDavid Howells fput(region->vm_file); 12568feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 125789a86402SDavid Howells if (vma->vm_file) 12588feae131SDavid Howells fput(vma->vm_file); 12593928d4f5SLinus Torvalds vm_area_free(vma); 12601da177e4SLinus Torvalds return ret; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds sharing_violation: 12638feae131SDavid Howells up_write(&nommu_region_sem); 126422cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n"); 12658feae131SDavid Howells ret = -EINVAL; 12668feae131SDavid Howells goto error; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds error_getting_vma: 12698feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 127022cc877bSLeon Romanovsky pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", 12711da177e4SLinus Torvalds len, current->pid); 12729af744d7SMichal Hocko show_free_areas(0, NULL); 12731da177e4SLinus Torvalds return -ENOMEM; 12741da177e4SLinus Torvalds 12758feae131SDavid Howells error_getting_region: 127622cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", 12771da177e4SLinus Torvalds len, current->pid); 12789af744d7SMichal Hocko show_free_areas(0, NULL); 12791da177e4SLinus Torvalds return -ENOMEM; 12808220543dSMatthew Wilcox (Oracle) 12818220543dSMatthew Wilcox (Oracle) error_maple_preallocate: 12828220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 12838220543dSMatthew Wilcox (Oracle) vm_area_free(vma); 12848220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", current->pid); 12858220543dSMatthew Wilcox (Oracle) show_free_areas(0, NULL); 12868220543dSMatthew Wilcox (Oracle) return -ENOMEM; 12878220543dSMatthew Wilcox (Oracle) 12881da177e4SLinus Torvalds } 12896be5ceb0SLinus Torvalds 1290a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1291a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1292a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 129366f0dc48SHugh Dickins { 129466f0dc48SHugh Dickins struct file *file = NULL; 129566f0dc48SHugh Dickins unsigned long retval = -EBADF; 129666f0dc48SHugh Dickins 1297120a795dSAl Viro audit_mmap_fd(fd, flags); 129866f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 129966f0dc48SHugh Dickins file = fget(fd); 130066f0dc48SHugh Dickins if (!file) 130166f0dc48SHugh Dickins goto out; 130266f0dc48SHugh Dickins } 130366f0dc48SHugh Dickins 1304ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 130566f0dc48SHugh Dickins 130666f0dc48SHugh Dickins if (file) 130766f0dc48SHugh Dickins fput(file); 130866f0dc48SHugh Dickins out: 130966f0dc48SHugh Dickins return retval; 131066f0dc48SHugh Dickins } 131166f0dc48SHugh Dickins 1312a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1313a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1314a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1315a90f590aSDominik Brodowski { 1316a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1317a90f590aSDominik Brodowski } 1318a90f590aSDominik Brodowski 1319a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1320a4679373SChristoph Hellwig struct mmap_arg_struct { 1321a4679373SChristoph Hellwig unsigned long addr; 1322a4679373SChristoph Hellwig unsigned long len; 1323a4679373SChristoph Hellwig unsigned long prot; 1324a4679373SChristoph Hellwig unsigned long flags; 1325a4679373SChristoph Hellwig unsigned long fd; 1326a4679373SChristoph Hellwig unsigned long offset; 1327a4679373SChristoph Hellwig }; 1328a4679373SChristoph Hellwig 1329a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1330a4679373SChristoph Hellwig { 1331a4679373SChristoph Hellwig struct mmap_arg_struct a; 1332a4679373SChristoph Hellwig 1333a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1334a4679373SChristoph Hellwig return -EFAULT; 13351824cb75SAlexander Kuleshov if (offset_in_page(a.offset)) 1336a4679373SChristoph Hellwig return -EINVAL; 1337a4679373SChristoph Hellwig 1338a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1339a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1340a4679373SChristoph Hellwig } 1341a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1342a4679373SChristoph Hellwig 13431da177e4SLinus Torvalds /* 13448feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 13458feae131SDavid Howells * for the first part or the tail. 13461da177e4SLinus Torvalds */ 13478feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 13488feae131SDavid Howells unsigned long addr, int new_below) 13491da177e4SLinus Torvalds { 13508feae131SDavid Howells struct vm_area_struct *new; 13518feae131SDavid Howells struct vm_region *region; 13528feae131SDavid Howells unsigned long npages; 13538220543dSMatthew Wilcox (Oracle) MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end); 13541da177e4SLinus Torvalds 1355779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1356779c1023SDavid Howells * only a single usage on the region) */ 1357779c1023SDavid Howells if (vma->vm_file) 13588feae131SDavid Howells return -ENOMEM; 13591da177e4SLinus Torvalds 1360fd9edbdbSLiam Howlett mm = vma->vm_mm; 13618feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 13628feae131SDavid Howells return -ENOMEM; 13631da177e4SLinus Torvalds 13648feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 13658feae131SDavid Howells if (!region) 13668feae131SDavid Howells return -ENOMEM; 13678feae131SDavid Howells 13683928d4f5SLinus Torvalds new = vm_area_dup(vma); 13698220543dSMatthew Wilcox (Oracle) if (!new) 13708220543dSMatthew Wilcox (Oracle) goto err_vma_dup; 13718220543dSMatthew Wilcox (Oracle) 13728220543dSMatthew Wilcox (Oracle) if (mas_preallocate(&mas, vma, GFP_KERNEL)) { 13738220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 13748220543dSMatthew Wilcox (Oracle) current->pid); 13758220543dSMatthew Wilcox (Oracle) goto err_mas_preallocate; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds 13788feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 13798feae131SDavid Howells *region = *vma->vm_region; 13808feae131SDavid Howells new->vm_region = region; 13818feae131SDavid Howells 13828feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 13838feae131SDavid Howells 13848feae131SDavid Howells if (new_below) { 1385dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 13868feae131SDavid Howells } else { 13878feae131SDavid Howells region->vm_start = new->vm_start = addr; 13888feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 13891da177e4SLinus Torvalds } 13908feae131SDavid Howells 13918feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 13928feae131SDavid Howells new->vm_ops->open(new); 13938feae131SDavid Howells 13948feae131SDavid Howells down_write(&nommu_region_sem); 13958feae131SDavid Howells delete_nommu_region(vma->vm_region); 13968feae131SDavid Howells if (new_below) { 13978feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 13988feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 13998feae131SDavid Howells } else { 14008feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1401dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 14028feae131SDavid Howells } 14038feae131SDavid Howells add_nommu_region(vma->vm_region); 14048feae131SDavid Howells add_nommu_region(new->vm_region); 14058feae131SDavid Howells up_write(&nommu_region_sem); 14068220543dSMatthew Wilcox (Oracle) 14078220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(vma, mm); 14088220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(new, mm); 14098220543dSMatthew Wilcox (Oracle) mas_set_range(&mas, vma->vm_start, vma->vm_end - 1); 14108220543dSMatthew Wilcox (Oracle) mas_store(&mas, vma); 14118220543dSMatthew Wilcox (Oracle) vma_mas_store(new, &mas); 1412fd9edbdbSLiam Howlett mm->map_count++; 14138feae131SDavid Howells return 0; 14148220543dSMatthew Wilcox (Oracle) 14158220543dSMatthew Wilcox (Oracle) err_mas_preallocate: 14168220543dSMatthew Wilcox (Oracle) vm_area_free(new); 14178220543dSMatthew Wilcox (Oracle) err_vma_dup: 14188220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 14198220543dSMatthew Wilcox (Oracle) return -ENOMEM; 14208feae131SDavid Howells } 14218feae131SDavid Howells 14228feae131SDavid Howells /* 14238feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 14248feae131SDavid Howells * the end 14258feae131SDavid Howells */ 14268feae131SDavid Howells static int shrink_vma(struct mm_struct *mm, 14278feae131SDavid Howells struct vm_area_struct *vma, 14288feae131SDavid Howells unsigned long from, unsigned long to) 14298feae131SDavid Howells { 14308feae131SDavid Howells struct vm_region *region; 14318feae131SDavid Howells 14328feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 14338feae131SDavid Howells * and list */ 14348220543dSMatthew Wilcox (Oracle) if (delete_vma_from_mm(vma)) 14358220543dSMatthew Wilcox (Oracle) return -ENOMEM; 14368feae131SDavid Howells if (from > vma->vm_start) 14378feae131SDavid Howells vma->vm_end = from; 14388feae131SDavid Howells else 14398feae131SDavid Howells vma->vm_start = to; 14408220543dSMatthew Wilcox (Oracle) if (add_vma_to_mm(mm, vma)) 14418220543dSMatthew Wilcox (Oracle) return -ENOMEM; 14428feae131SDavid Howells 14438feae131SDavid Howells /* cut the backing region down to size */ 14448feae131SDavid Howells region = vma->vm_region; 14451e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 14468feae131SDavid Howells 14478feae131SDavid Howells down_write(&nommu_region_sem); 14488feae131SDavid Howells delete_nommu_region(region); 1449dd8632a1SPaul Mundt if (from > region->vm_start) { 1450dd8632a1SPaul Mundt to = region->vm_top; 1451dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1452dd8632a1SPaul Mundt } else { 14538feae131SDavid Howells region->vm_start = to; 1454dd8632a1SPaul Mundt } 14558feae131SDavid Howells add_nommu_region(region); 14568feae131SDavid Howells up_write(&nommu_region_sem); 14578feae131SDavid Howells 14588feae131SDavid Howells free_page_series(from, to); 14598feae131SDavid Howells return 0; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14623034097aSDavid Howells /* 14633034097aSDavid Howells * release a mapping 14648feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 14658feae131SDavid Howells * VMA, though it need not cover the whole VMA 14663034097aSDavid Howells */ 1467897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) 14681da177e4SLinus Torvalds { 14698220543dSMatthew Wilcox (Oracle) MA_STATE(mas, &mm->mm_mt, start, start); 14708feae131SDavid Howells struct vm_area_struct *vma; 1471f67d9b15SBob Liu unsigned long end; 14728220543dSMatthew Wilcox (Oracle) int ret = 0; 14731da177e4SLinus Torvalds 1474f67d9b15SBob Liu len = PAGE_ALIGN(len); 14758feae131SDavid Howells if (len == 0) 14761da177e4SLinus Torvalds return -EINVAL; 14771da177e4SLinus Torvalds 1478f67d9b15SBob Liu end = start + len; 1479f67d9b15SBob Liu 14808feae131SDavid Howells /* find the first potentially overlapping VMA */ 14818220543dSMatthew Wilcox (Oracle) vma = mas_find(&mas, end - 1); 14828feae131SDavid Howells if (!vma) { 1483ac714904SChoi Gi-yong static int limit; 148433e5d769SDavid Howells if (limit < 5) { 148522cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 148633e5d769SDavid Howells current->pid, current->comm, 148733e5d769SDavid Howells start, start + len - 1); 148833e5d769SDavid Howells limit++; 148933e5d769SDavid Howells } 14908feae131SDavid Howells return -EINVAL; 14918feae131SDavid Howells } 14921da177e4SLinus Torvalds 14938feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 14948feae131SDavid Howells if (vma->vm_file) { 14958feae131SDavid Howells do { 149622cc877bSLeon Romanovsky if (start > vma->vm_start) 14978feae131SDavid Howells return -EINVAL; 14988feae131SDavid Howells if (end == vma->vm_end) 14998feae131SDavid Howells goto erase_whole_vma; 15008220543dSMatthew Wilcox (Oracle) vma = mas_next(&mas, end - 1); 1501d75a310cSNamhyung Kim } while (vma); 15028feae131SDavid Howells return -EINVAL; 15038feae131SDavid Howells } else { 15048feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 15058feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 15068feae131SDavid Howells goto erase_whole_vma; 150722cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 15088feae131SDavid Howells return -EINVAL; 15091824cb75SAlexander Kuleshov if (offset_in_page(start)) 15108feae131SDavid Howells return -EINVAL; 15111824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end)) 15128feae131SDavid Howells return -EINVAL; 15138feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 15148feae131SDavid Howells ret = split_vma(mm, vma, start, 1); 151522cc877bSLeon Romanovsky if (ret < 0) 15168feae131SDavid Howells return ret; 15178feae131SDavid Howells } 15188feae131SDavid Howells return shrink_vma(mm, vma, start, end); 15198feae131SDavid Howells } 15201da177e4SLinus Torvalds 15218feae131SDavid Howells erase_whole_vma: 15228220543dSMatthew Wilcox (Oracle) if (delete_vma_from_mm(vma)) 15238220543dSMatthew Wilcox (Oracle) ret = -ENOMEM; 152480be727eSLiam Howlett else 15258feae131SDavid Howells delete_vma(mm, vma); 15268220543dSMatthew Wilcox (Oracle) return ret; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 1529bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 15303034097aSDavid Howells { 1531bfce281cSAl Viro struct mm_struct *mm = current->mm; 15323034097aSDavid Howells int ret; 15333034097aSDavid Howells 1534d8ed45c5SMichel Lespinasse mmap_write_lock(mm); 1535897ab3e0SMike Rapoport ret = do_munmap(mm, addr, len, NULL); 1536d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 15373034097aSDavid Howells return ret; 15383034097aSDavid Howells } 1539a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1540a46ef99dSLinus Torvalds 1541a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1542a46ef99dSLinus Torvalds { 1543bfce281cSAl Viro return vm_munmap(addr, len); 1544a46ef99dSLinus Torvalds } 15453034097aSDavid Howells 15463034097aSDavid Howells /* 15478feae131SDavid Howells * release all the mappings made in a process's VM space 15483034097aSDavid Howells */ 15491da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 15501da177e4SLinus Torvalds { 15518220543dSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0); 15528feae131SDavid Howells struct vm_area_struct *vma; 15531da177e4SLinus Torvalds 15548feae131SDavid Howells if (!mm) 15558feae131SDavid Howells return; 15568feae131SDavid Howells 15571da177e4SLinus Torvalds mm->total_vm = 0; 15581da177e4SLinus Torvalds 15598220543dSMatthew Wilcox (Oracle) /* 15608220543dSMatthew Wilcox (Oracle) * Lock the mm to avoid assert complaining even though this is the only 15618220543dSMatthew Wilcox (Oracle) * user of the mm 15628220543dSMatthew Wilcox (Oracle) */ 15638220543dSMatthew Wilcox (Oracle) mmap_write_lock(mm); 15648220543dSMatthew Wilcox (Oracle) for_each_vma(vmi, vma) { 15658220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 15668feae131SDavid Howells delete_vma(mm, vma); 156704c34961SSteven J. Magnani cond_resched(); 15681da177e4SLinus Torvalds } 1569524e00b3SLiam R. Howlett __mt_destroy(&mm->mm_mt); 15708220543dSMatthew Wilcox (Oracle) mmap_write_unlock(mm); 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 15735d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len) 15741da177e4SLinus Torvalds { 15751da177e4SLinus Torvalds return -ENOMEM; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds /* 15796fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 15806fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 15811da177e4SLinus Torvalds * 15826fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 15838feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 15848feae131SDavid Howells * block is not shareable 15851da177e4SLinus Torvalds * 15866fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 15871da177e4SLinus Torvalds */ 15884b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 15891da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 15901da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 15911da177e4SLinus Torvalds { 15926fa5f80bSDavid Howells struct vm_area_struct *vma; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /* insanity checks first */ 1595f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1596f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 15978feae131SDavid Howells if (old_len == 0 || new_len == 0) 15981da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15991da177e4SLinus Torvalds 16001824cb75SAlexander Kuleshov if (offset_in_page(addr)) 16018feae131SDavid Howells return -EINVAL; 16028feae131SDavid Howells 16031da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 16041da177e4SLinus Torvalds return (unsigned long) -EINVAL; 16051da177e4SLinus Torvalds 16068feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 16076fa5f80bSDavid Howells if (!vma) 16081da177e4SLinus Torvalds return (unsigned long) -EINVAL; 16091da177e4SLinus Torvalds 16106fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 16111da177e4SLinus Torvalds return (unsigned long) -EFAULT; 16121da177e4SLinus Torvalds 1613fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vma->vm_flags)) 16141da177e4SLinus Torvalds return (unsigned long) -EPERM; 16151da177e4SLinus Torvalds 16168feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 16171da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds /* all checks complete - do it */ 16206fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 16216fa5f80bSDavid Howells return vma->vm_start; 16226fa5f80bSDavid Howells } 16236fa5f80bSDavid Howells 16246a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 16256a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 16266a6160a7SHeiko Carstens unsigned long, new_addr) 16276fa5f80bSDavid Howells { 16286fa5f80bSDavid Howells unsigned long ret; 16296fa5f80bSDavid Howells 1630d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 16316fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 1632d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 16336fa5f80bSDavid Howells return ret; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 1636df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1637df06b37fSKeith Busch unsigned int foll_flags) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds return NULL; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 16428f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 16438f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 16441da177e4SLinus Torvalds { 16458f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 16468f3b1327SBob Liu return -EINVAL; 16478f3b1327SBob Liu 1648314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 164966aa2b4bSGreg Ungerer return 0; 16501da177e4SLinus Torvalds } 165122c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 16521da177e4SLinus Torvalds 16533c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 16543c0b9de6SLinus Torvalds { 16553c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 16563c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 16573c0b9de6SLinus Torvalds 16583c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 16593c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 16603c0b9de6SLinus Torvalds } 16613c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 16623c0b9de6SLinus Torvalds 1663f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1664f905bc44SPaul Mundt unsigned long pgoff) 1665f905bc44SPaul Mundt { 1666f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1667f905bc44SPaul Mundt 1668f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1669f905bc44SPaul Mundt return -EINVAL; 1670f905bc44SPaul Mundt 1671f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1672f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1673f905bc44SPaul Mundt 1674f905bc44SPaul Mundt return 0; 1675f905bc44SPaul Mundt } 1676f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1677f905bc44SPaul Mundt 16782bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf) 1679b0e15190SDavid Howells { 1680b0e15190SDavid Howells BUG(); 1681d0217ac0SNick Piggin return 0; 1682b0e15190SDavid Howells } 1683b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 16840ec76a11SDavid Howells 16853f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf, 1686bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff) 1687f1820361SKirill A. Shutemov { 1688f1820361SKirill A. Shutemov BUG(); 16893f98a28cSGeert Uytterhoeven return 0; 1690f1820361SKirill A. Shutemov } 1691f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1692f1820361SKirill A. Shutemov 1693d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, 1694d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags) 16950ec76a11SDavid Howells { 16960ec76a11SDavid Howells struct vm_area_struct *vma; 1697442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 16980ec76a11SDavid Howells 1699d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 17001e426fe2SKonstantin Khlebnikov return 0; 17010ec76a11SDavid Howells 17020ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 17030159b141SDavid Howells vma = find_vma(mm, addr); 17040159b141SDavid Howells if (vma) { 17050ec76a11SDavid Howells /* don't overrun this mapping */ 17060ec76a11SDavid Howells if (addr + len >= vma->vm_end) 17070ec76a11SDavid Howells len = vma->vm_end - addr; 17080ec76a11SDavid Howells 17090ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1710d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 17117959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 17127959722bSJie Zhang (void *) addr, buf, len); 1713d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 17147959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 17157959722bSJie Zhang buf, (void *) addr, len); 17160ec76a11SDavid Howells else 17170ec76a11SDavid Howells len = 0; 17180ec76a11SDavid Howells } else { 17190ec76a11SDavid Howells len = 0; 17200ec76a11SDavid Howells } 17210ec76a11SDavid Howells 1722d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 1723f55f199bSMike Frysinger 1724f55f199bSMike Frysinger return len; 1725f55f199bSMike Frysinger } 1726f55f199bSMike Frysinger 1727f55f199bSMike Frysinger /** 1728b7701a5fSMike Rapoport * access_remote_vm - access another process' address space 1729f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1730f55f199bSMike Frysinger * @addr: start address to access 1731f55f199bSMike Frysinger * @buf: source or destination buffer 1732f55f199bSMike Frysinger * @len: number of bytes to transfer 17336347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 1734f55f199bSMike Frysinger * 1735f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1736f55f199bSMike Frysinger */ 1737f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 17386347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 1739f55f199bSMike Frysinger { 1740d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 1741f55f199bSMike Frysinger } 1742f55f199bSMike Frysinger 1743f55f199bSMike Frysinger /* 1744f55f199bSMike Frysinger * Access another process' address space. 1745f55f199bSMike Frysinger * - source/target buffer must be kernel space 1746f55f199bSMike Frysinger */ 1747f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, 1748f307ab6dSLorenzo Stoakes unsigned int gup_flags) 1749f55f199bSMike Frysinger { 1750f55f199bSMike Frysinger struct mm_struct *mm; 1751f55f199bSMike Frysinger 1752f55f199bSMike Frysinger if (addr + len < addr) 1753f55f199bSMike Frysinger return 0; 1754f55f199bSMike Frysinger 1755f55f199bSMike Frysinger mm = get_task_mm(tsk); 1756f55f199bSMike Frysinger if (!mm) 1757f55f199bSMike Frysinger return 0; 1758f55f199bSMike Frysinger 1759d3f5ffcaSJohn Hubbard len = __access_remote_vm(mm, addr, buf, len, gup_flags); 1760f55f199bSMike Frysinger 17610ec76a11SDavid Howells mmput(mm); 17620ec76a11SDavid Howells return len; 17630ec76a11SDavid Howells } 1764fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 17657e660872SDavid Howells 17667e660872SDavid Howells /** 17677e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 17687e660872SDavid Howells * @inode: The inode to check 17697e660872SDavid Howells * @size: The current filesize of the inode 17707e660872SDavid Howells * @newsize: The proposed filesize of the inode 17717e660872SDavid Howells * 17727e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 1773c08b342cSRandy Dunlap * make sure that any outstanding VMAs aren't broken and then shrink the 1774c08b342cSRandy Dunlap * vm_regions that extend beyond so that do_mmap() doesn't 17757e660872SDavid Howells * automatically grant mappings that are too large. 17767e660872SDavid Howells */ 17777e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 17787e660872SDavid Howells size_t newsize) 17797e660872SDavid Howells { 17807e660872SDavid Howells struct vm_area_struct *vma; 17817e660872SDavid Howells struct vm_region *region; 17827e660872SDavid Howells pgoff_t low, high; 17837e660872SDavid Howells size_t r_size, r_top; 17847e660872SDavid Howells 17857e660872SDavid Howells low = newsize >> PAGE_SHIFT; 17867e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 17877e660872SDavid Howells 17887e660872SDavid Howells down_write(&nommu_region_sem); 17891acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 17907e660872SDavid Howells 17917e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 17926b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 17937e660872SDavid Howells /* found one - only interested if it's shared out of the page 17947e660872SDavid Howells * cache */ 17957e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 17961acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17977e660872SDavid Howells up_write(&nommu_region_sem); 17987e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 17997e660872SDavid Howells } 18007e660872SDavid Howells } 18017e660872SDavid Howells 18027e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 18037e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 18047e660872SDavid Howells * 18057e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 18067e660872SDavid Howells * shouldn't be any 18077e660872SDavid Howells */ 18081acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 18097e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 18107e660872SDavid Howells continue; 18117e660872SDavid Howells 18127e660872SDavid Howells region = vma->vm_region; 18137e660872SDavid Howells r_size = region->vm_top - region->vm_start; 18147e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 18157e660872SDavid Howells 18167e660872SDavid Howells if (r_top > newsize) { 18177e660872SDavid Howells region->vm_top -= r_top - newsize; 18187e660872SDavid Howells if (region->vm_end > region->vm_top) 18197e660872SDavid Howells region->vm_end = region->vm_top; 18207e660872SDavid Howells } 18217e660872SDavid Howells } 18227e660872SDavid Howells 18231acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 18247e660872SDavid Howells up_write(&nommu_region_sem); 18257e660872SDavid Howells return 0; 18267e660872SDavid Howells } 1827c9b1d098SAndrew Shewmaker 1828c9b1d098SAndrew Shewmaker /* 1829c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 1830c9b1d098SAndrew Shewmaker * 1831c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 1832c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 1833c9b1d098SAndrew Shewmaker * mode. 1834c9b1d098SAndrew Shewmaker * 1835c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 1836c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 1837c9b1d098SAndrew Shewmaker */ 1838c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 1839c9b1d098SAndrew Shewmaker { 1840c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 1841c9b1d098SAndrew Shewmaker 1842c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 1843c9b1d098SAndrew Shewmaker 1844c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 1845c9b1d098SAndrew Shewmaker return 0; 1846c9b1d098SAndrew Shewmaker } 1847a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve); 18484eeab4f5SAndrew Shewmaker 18494eeab4f5SAndrew Shewmaker /* 18504eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 18514eeab4f5SAndrew Shewmaker * 18524eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 18534eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 18544eeab4f5SAndrew Shewmaker * 18554eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 18564eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 18574eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 18584eeab4f5SAndrew Shewmaker */ 18594eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 18604eeab4f5SAndrew Shewmaker { 18614eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 18624eeab4f5SAndrew Shewmaker 1863c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 18644eeab4f5SAndrew Shewmaker 18654eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 18664eeab4f5SAndrew Shewmaker return 0; 18674eeab4f5SAndrew Shewmaker } 1868a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve); 1869