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 5603034097aSDavid Howells /* 5613034097aSDavid Howells * add a VMA into a process's mm_struct in the appropriate place in the list 5628feae131SDavid Howells * and tree and add to the address space's page tree also if not an anonymous 5638feae131SDavid Howells * page 564c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock held writelocked 5653034097aSDavid Howells */ 5668feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) 5673034097aSDavid Howells { 5688feae131SDavid Howells struct address_space *mapping; 569524e00b3SLiam R. Howlett struct vm_area_struct *prev; 570524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end); 5713034097aSDavid Howells 5728feae131SDavid Howells BUG_ON(!vma->vm_region); 5738feae131SDavid Howells 5748feae131SDavid Howells mm->map_count++; 5758feae131SDavid Howells vma->vm_mm = mm; 5768feae131SDavid Howells 5778feae131SDavid Howells /* add the VMA to the mapping */ 5788feae131SDavid Howells if (vma->vm_file) { 5798feae131SDavid Howells mapping = vma->vm_file->f_mapping; 5808feae131SDavid Howells 58183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 5828feae131SDavid Howells flush_dcache_mmap_lock(mapping); 5836b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5848feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 58583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5868feae131SDavid Howells } 5878feae131SDavid Howells 588524e00b3SLiam R. Howlett prev = mas_prev(&mas, 0); 589524e00b3SLiam R. Howlett mas_reset(&mas); 5908feae131SDavid Howells /* add the VMA to the tree */ 591524e00b3SLiam R. Howlett vma_mas_store(vma, &mas); 592aba6dfb7SWei Yang __vma_link_list(mm, vma, prev); 5938feae131SDavid Howells } 5948feae131SDavid Howells 5958feae131SDavid Howells /* 5968feae131SDavid Howells * delete a VMA from its owning mm_struct and address space 5978feae131SDavid Howells */ 5988feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma) 5998feae131SDavid Howells { 600524e00b3SLiam R. Howlett MA_STATE(mas, &vma->vm_mm->mm_mt, 0, 0); 6018feae131SDavid Howells 602*7964cf8cSLiam R. Howlett vma->vm_mm->map_count--; 6038feae131SDavid Howells /* remove the VMA from the mapping */ 6048feae131SDavid Howells if (vma->vm_file) { 605*7964cf8cSLiam R. Howlett struct address_space *mapping; 6068feae131SDavid Howells mapping = vma->vm_file->f_mapping; 6078feae131SDavid Howells 60883cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 6098feae131SDavid Howells flush_dcache_mmap_lock(mapping); 6106b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 6118feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 61283cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 6138feae131SDavid Howells } 6148feae131SDavid Howells 6158feae131SDavid Howells /* remove from the MM's tree and list */ 616524e00b3SLiam R. Howlett vma_mas_remove(vma, &mas); 617*7964cf8cSLiam R. Howlett __vma_unlink_list(vma->vm_mm, vma); 6188feae131SDavid Howells } 6198feae131SDavid Howells 6208feae131SDavid Howells /* 6218feae131SDavid Howells * destroy a VMA record 6228feae131SDavid Howells */ 6238feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 6248feae131SDavid Howells { 6258feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 6268feae131SDavid Howells vma->vm_ops->close(vma); 627e9714acfSKonstantin Khlebnikov if (vma->vm_file) 6288feae131SDavid Howells fput(vma->vm_file); 6298feae131SDavid Howells put_nommu_region(vma->vm_region); 6303928d4f5SLinus Torvalds vm_area_free(vma); 6313034097aSDavid Howells } 6323034097aSDavid Howells 633abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 634abdba2ddSLiam R. Howlett unsigned long start_addr, 635abdba2ddSLiam R. Howlett unsigned long end_addr) 636abdba2ddSLiam R. Howlett { 637abdba2ddSLiam R. Howlett unsigned long index = start_addr; 638abdba2ddSLiam R. Howlett 639abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 640abdba2ddSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 641abdba2ddSLiam R. Howlett } 642abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 643abdba2ddSLiam R. Howlett 6443034097aSDavid Howells /* 6453034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 646c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6473034097aSDavid Howells */ 6483034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 6493034097aSDavid Howells { 650524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 6513034097aSDavid Howells 652*7964cf8cSLiam R. Howlett return mas_walk(&mas); 6533034097aSDavid Howells } 6543034097aSDavid Howells EXPORT_SYMBOL(find_vma); 6553034097aSDavid Howells 6563034097aSDavid Howells /* 657930e652aSDavid Howells * find a VMA 658930e652aSDavid Howells * - we don't extend stack VMAs under NOMMU conditions 659930e652aSDavid Howells */ 660930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 661930e652aSDavid Howells { 6627561e8caSDavid Howells return find_vma(mm, addr); 663930e652aSDavid Howells } 664930e652aSDavid Howells 6658feae131SDavid Howells /* 6668feae131SDavid Howells * expand a stack to a given address 6678feae131SDavid Howells * - not supported under NOMMU conditions 6688feae131SDavid Howells */ 66957c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address) 67057c8f63eSGreg Ungerer { 67157c8f63eSGreg Ungerer return -ENOMEM; 67257c8f63eSGreg Ungerer } 67357c8f63eSGreg Ungerer 674930e652aSDavid Howells /* 6756fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 676c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6776fa5f80bSDavid Howells */ 6788feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 6798feae131SDavid Howells unsigned long addr, 6808feae131SDavid Howells unsigned long len) 6811da177e4SLinus Torvalds { 6821da177e4SLinus Torvalds struct vm_area_struct *vma; 6838feae131SDavid Howells unsigned long end = addr + len; 684524e00b3SLiam R. Howlett MA_STATE(mas, &mm->mm_mt, addr, addr); 6851da177e4SLinus Torvalds 686524e00b3SLiam R. Howlett vma = mas_walk(&mas); 687524e00b3SLiam R. Howlett if (!vma) 6888feae131SDavid Howells return NULL; 689524e00b3SLiam R. Howlett if (vma->vm_start != addr) 690524e00b3SLiam R. Howlett return NULL; 691524e00b3SLiam R. Howlett if (vma->vm_end != end) 692524e00b3SLiam R. Howlett return NULL; 693524e00b3SLiam R. Howlett 6948feae131SDavid Howells return vma; 6958feae131SDavid Howells } 6961da177e4SLinus Torvalds 6973034097aSDavid Howells /* 6981da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 6991da177e4SLinus Torvalds * mapping we're capable of supporting 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 7021da177e4SLinus Torvalds unsigned long addr, 7031da177e4SLinus Torvalds unsigned long len, 7041da177e4SLinus Torvalds unsigned long prot, 7051da177e4SLinus Torvalds unsigned long flags, 7061da177e4SLinus Torvalds unsigned long pgoff, 7071da177e4SLinus Torvalds unsigned long *_capabilities) 7081da177e4SLinus Torvalds { 7098feae131SDavid Howells unsigned long capabilities, rlen; 7101da177e4SLinus Torvalds int ret; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* do the simple checks first */ 71322cc877bSLeon Romanovsky if (flags & MAP_FIXED) 7141da177e4SLinus Torvalds return -EINVAL; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 7171da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 7181da177e4SLinus Torvalds return -EINVAL; 7191da177e4SLinus Torvalds 720f81cff0dSMike Frysinger if (!len) 7211da177e4SLinus Torvalds return -EINVAL; 7221da177e4SLinus Torvalds 723f81cff0dSMike Frysinger /* Careful about overflows.. */ 7248feae131SDavid Howells rlen = PAGE_ALIGN(len); 7258feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 726f81cff0dSMike Frysinger return -ENOMEM; 727f81cff0dSMike Frysinger 7281da177e4SLinus Torvalds /* offset overflow? */ 7298feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 730f81cff0dSMike Frysinger return -EOVERFLOW; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds if (file) { 7331da177e4SLinus Torvalds /* files must support mmap */ 73472c2d531SAl Viro if (!file->f_op->mmap) 7351da177e4SLinus Torvalds return -ENODEV; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 7381da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 7391da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 7401da177e4SLinus Torvalds */ 741b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 742b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 743b4caecd4SChristoph Hellwig } else { 7441da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 7451da177e4SLinus Torvalds * defaults */ 746496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 7471da177e4SLinus Torvalds case S_IFREG: 7481da177e4SLinus Torvalds case S_IFBLK: 749b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 7501da177e4SLinus Torvalds break; 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds case S_IFCHR: 7531da177e4SLinus Torvalds capabilities = 754b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 755b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 756b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 7571da177e4SLinus Torvalds break; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds default: 7601da177e4SLinus Torvalds return -EINVAL; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 7651da177e4SLinus Torvalds * device */ 7661da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 767b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7686e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 769b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 7701da177e4SLinus Torvalds 77128d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 77228d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 77328d7a6aeSGraff Yang return -EACCES; 77428d7a6aeSGraff Yang 7751da177e4SLinus Torvalds if (flags & MAP_SHARED) { 7761da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 7771da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 7781da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 7791da177e4SLinus Torvalds return -EACCES; 7801da177e4SLinus Torvalds 781496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 7821da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 7831da177e4SLinus Torvalds return -EACCES; 7841da177e4SLinus Torvalds 785b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 7861da177e4SLinus Torvalds return -ENODEV; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 789b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 790ac714904SChoi Gi-yong } else { 7911da177e4SLinus Torvalds /* we're going to read the file into private memory we 7921da177e4SLinus Torvalds * allocate */ 793b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 7941da177e4SLinus Torvalds return -ENODEV; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 7971da177e4SLinus Torvalds * shared with the backing device */ 7981da177e4SLinus Torvalds if (prot & PROT_WRITE) 799b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 803b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 804b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 805b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 8063c7b2045SBernd Schmidt ) { 807b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8083c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 80922cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 8103c7b2045SBernd Schmidt return -EINVAL; 8113c7b2045SBernd Schmidt } 8123c7b2045SBernd Schmidt } 8133c7b2045SBernd Schmidt } 8143c7b2045SBernd Schmidt 8151da177e4SLinus Torvalds /* handle executable mappings and implied executable 8161da177e4SLinus Torvalds * mappings */ 81790f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 8181da177e4SLinus Torvalds if (prot & PROT_EXEC) 8191da177e4SLinus Torvalds return -EPERM; 820ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 8211da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 8221da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 823b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 8241da177e4SLinus Torvalds prot |= PROT_EXEC; 8251da177e4SLinus Torvalds } 826ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 8271da177e4SLinus Torvalds (prot & PROT_EXEC) && 828b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 8291da177e4SLinus Torvalds ) { 8301da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 831b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8321da177e4SLinus Torvalds } 833ac714904SChoi Gi-yong } else { 8341da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 8351da177e4SLinus Torvalds * privately mapped 8361da177e4SLinus Torvalds */ 837b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 8401da177e4SLinus Torvalds if ((prot & PROT_READ) && 8411da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 8421da177e4SLinus Torvalds prot |= PROT_EXEC; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds /* allow the security API to have its say */ 846e5467859SAl Viro ret = security_mmap_addr(addr); 847e5467859SAl Viro if (ret < 0) 848e5467859SAl Viro return ret; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds /* looks okay */ 8511da177e4SLinus Torvalds *_capabilities = capabilities; 8521da177e4SLinus Torvalds return 0; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* 8561da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 8571da177e4SLinus Torvalds * now know into VMA flags 8581da177e4SLinus Torvalds */ 8591da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 8601da177e4SLinus Torvalds unsigned long prot, 8611da177e4SLinus Torvalds unsigned long flags, 8621da177e4SLinus Torvalds unsigned long capabilities) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds unsigned long vm_flags; 8651da177e4SLinus Torvalds 866e6bfb709SDave Hansen vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); 8671da177e4SLinus Torvalds /* vm_flags |= mm->def_flags; */ 8681da177e4SLinus Torvalds 869b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) { 8701da177e4SLinus Torvalds /* attempt to share read-only copies of mapped file chunks */ 8713c7b2045SBernd Schmidt vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 8721da177e4SLinus Torvalds if (file && !(prot & PROT_WRITE)) 8731da177e4SLinus Torvalds vm_flags |= VM_MAYSHARE; 8743c7b2045SBernd Schmidt } else { 8751da177e4SLinus Torvalds /* overlay a shareable mapping on the backing device or inode 8761da177e4SLinus Torvalds * if possible - used for chardevs, ramfs/tmpfs/shmfs and 8771da177e4SLinus Torvalds * romfs/cramfs */ 878b4caecd4SChristoph Hellwig vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS); 8791da177e4SLinus Torvalds if (flags & MAP_SHARED) 8803c7b2045SBernd Schmidt vm_flags |= VM_SHARED; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* refuse to let anyone share private mappings with this process if 8841da177e4SLinus Torvalds * it's being traced - otherwise breakpoints set in it may interfere 8851da177e4SLinus Torvalds * with another untraced process 8861da177e4SLinus Torvalds */ 887a288eeccSTejun Heo if ((flags & MAP_PRIVATE) && current->ptrace) 8881da177e4SLinus Torvalds vm_flags &= ~VM_MAYSHARE; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds return vm_flags; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds /* 8948feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 8958feae131SDavid Howells * pins the storage) 8961da177e4SLinus Torvalds */ 8978feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds int ret; 9001da177e4SLinus Torvalds 901f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 902dd8632a1SPaul Mundt if (ret == 0) { 903dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 904645d83c5SDavid Howells return 0; 905dd8632a1SPaul Mundt } 9061da177e4SLinus Torvalds if (ret != -ENOSYS) 9071da177e4SLinus Torvalds return ret; 9081da177e4SLinus Torvalds 9093fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 9103fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 9113fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 9121da177e4SLinus Torvalds return -ENODEV; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* 9161da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 9171da177e4SLinus Torvalds */ 9188feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 9198feae131SDavid Howells struct vm_region *region, 920645d83c5SDavid Howells unsigned long len, 921645d83c5SDavid Howells unsigned long capabilities) 9221da177e4SLinus Torvalds { 923dbc8358cSJoonsoo Kim unsigned long total, point; 9241da177e4SLinus Torvalds void *base; 9258feae131SDavid Howells int ret, order; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds /* invoke the file's mapping function so that it can keep track of 9281da177e4SLinus Torvalds * shared mappings on devices or memory 9291da177e4SLinus Torvalds * - VM_MAYSHARE will be set if it may attempt to share 9301da177e4SLinus Torvalds */ 931b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 932f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 933dd8632a1SPaul Mundt if (ret == 0) { 9341da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 935dd8632a1SPaul Mundt BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); 936dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 937645d83c5SDavid Howells return 0; 9381da177e4SLinus Torvalds } 939dd8632a1SPaul Mundt if (ret != -ENOSYS) 940dd8632a1SPaul Mundt return ret; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 9431da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 9441da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9478feae131SDavid Howells 9481da177e4SLinus Torvalds /* allocate some memory to hold the mapping 9491da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 9501da177e4SLinus Torvalds * we're allocating is smaller than a page 9511da177e4SLinus Torvalds */ 952f67d9b15SBob Liu order = get_order(len); 9538feae131SDavid Howells total = 1 << order; 954f67d9b15SBob Liu point = len >> PAGE_SHIFT; 955dd8632a1SPaul Mundt 956dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 95722cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 958dbc8358cSJoonsoo Kim total = point; 9598feae131SDavid Howells 960da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 961dbc8358cSJoonsoo Kim if (!base) 962dbc8358cSJoonsoo Kim goto enomem; 9638feae131SDavid Howells 964dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 965dbc8358cSJoonsoo Kim 9668feae131SDavid Howells region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; 9678feae131SDavid Howells region->vm_start = (unsigned long) base; 968f67d9b15SBob Liu region->vm_end = region->vm_start + len; 969dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 9708feae131SDavid Howells 9718feae131SDavid Howells vma->vm_start = region->vm_start; 9728feae131SDavid Howells vma->vm_end = region->vm_start + len; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (vma->vm_file) { 9751da177e4SLinus Torvalds /* read the contents of a file into the copy */ 9761da177e4SLinus Torvalds loff_t fpos; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds fpos = vma->vm_pgoff; 9791da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 9801da177e4SLinus Torvalds 981b4bf802aSChristoph Hellwig ret = kernel_read(vma->vm_file, base, len, &fpos); 9821da177e4SLinus Torvalds if (ret < 0) 9831da177e4SLinus Torvalds goto error_free; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* clear the last little bit */ 986f67d9b15SBob Liu if (ret < len) 987f67d9b15SBob Liu memset(base + ret, 0, len - ret); 9881da177e4SLinus Torvalds 989bfd40eafSKirill A. Shutemov } else { 990bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds return 0; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds error_free: 9967223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 9978feae131SDavid Howells region->vm_start = vma->vm_start = 0; 9988feae131SDavid Howells region->vm_end = vma->vm_end = 0; 999dd8632a1SPaul Mundt region->vm_top = 0; 10001da177e4SLinus Torvalds return ret; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds enomem: 1003b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 100405ae6fa3SGreg Ungerer len, current->pid, current->comm); 10059af744d7SMichal Hocko show_free_areas(0, NULL); 10061da177e4SLinus Torvalds return -ENOMEM; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* 10101da177e4SLinus Torvalds * handle mapping creation for uClinux 10111da177e4SLinus Torvalds */ 10121fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, 10131da177e4SLinus Torvalds unsigned long addr, 10141da177e4SLinus Torvalds unsigned long len, 10151da177e4SLinus Torvalds unsigned long prot, 10161da177e4SLinus Torvalds unsigned long flags, 1017bebeb3d6SMichel Lespinasse unsigned long pgoff, 1018897ab3e0SMike Rapoport unsigned long *populate, 1019897ab3e0SMike Rapoport struct list_head *uf) 10201da177e4SLinus Torvalds { 10218feae131SDavid Howells struct vm_area_struct *vma; 10228feae131SDavid Howells struct vm_region *region; 10231da177e4SLinus Torvalds struct rb_node *rb; 102445e55300SPeter Collingbourne vm_flags_t vm_flags; 10251fcfd8dbSOleg Nesterov unsigned long capabilities, result; 10261da177e4SLinus Torvalds int ret; 10271da177e4SLinus Torvalds 102841badc15SMichel Lespinasse *populate = 0; 1029bebeb3d6SMichel Lespinasse 10301da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 10311da177e4SLinus Torvalds * mapping */ 10321da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 10331da177e4SLinus Torvalds &capabilities); 103422cc877bSLeon Romanovsky if (ret < 0) 10351da177e4SLinus Torvalds return ret; 10361da177e4SLinus Torvalds 103706aab5a3SDavid Howells /* we ignore the address hint */ 103806aab5a3SDavid Howells addr = 0; 1039f67d9b15SBob Liu len = PAGE_ALIGN(len); 104006aab5a3SDavid Howells 10411da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 10421da177e4SLinus Torvalds * now know into VMA flags */ 104345e55300SPeter Collingbourne vm_flags = determine_vm_flags(file, prot, flags, capabilities); 10441da177e4SLinus Torvalds 10458feae131SDavid Howells /* we're going to need to record the mapping */ 10468feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 10478feae131SDavid Howells if (!region) 10488feae131SDavid Howells goto error_getting_region; 10491da177e4SLinus Torvalds 1050490fc053SLinus Torvalds vma = vm_area_alloc(current->mm); 10518feae131SDavid Howells if (!vma) 10528feae131SDavid Howells goto error_getting_vma; 10531da177e4SLinus Torvalds 10541e2ae599SDavid Howells region->vm_usage = 1; 10558feae131SDavid Howells region->vm_flags = vm_flags; 10568feae131SDavid Howells region->vm_pgoff = pgoff; 10578feae131SDavid Howells 10588feae131SDavid Howells vma->vm_flags = vm_flags; 10598feae131SDavid Howells vma->vm_pgoff = pgoff; 10608feae131SDavid Howells 10618feae131SDavid Howells if (file) { 1062cb0942b8SAl Viro region->vm_file = get_file(file); 1063cb0942b8SAl Viro vma->vm_file = get_file(file); 10648feae131SDavid Howells } 10658feae131SDavid Howells 10668feae131SDavid Howells down_write(&nommu_region_sem); 10678feae131SDavid Howells 10688feae131SDavid Howells /* if we want to share, we need to check for regions created by other 10691da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 10708feae131SDavid Howells * - we can only share with a superset match on most regular files 10711da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 10721da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 10731da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 10741da177e4SLinus Torvalds * than here 10751da177e4SLinus Torvalds */ 10761da177e4SLinus Torvalds if (vm_flags & VM_MAYSHARE) { 10778feae131SDavid Howells struct vm_region *pregion; 10788feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 10791da177e4SLinus Torvalds 10808feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 10818feae131SDavid Howells pgend = pgoff + pglen; 1082165b2392SDavid Howells 10838feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 10848feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 10851da177e4SLinus Torvalds 10868feae131SDavid Howells if (!(pregion->vm_flags & VM_MAYSHARE)) 10871da177e4SLinus Torvalds continue; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1090496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1091496ad9aaSAl Viro file_inode(file)) 10921da177e4SLinus Torvalds continue; 10931da177e4SLinus Torvalds 10948feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 10951da177e4SLinus Torvalds continue; 10961da177e4SLinus Torvalds 10978feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 10988feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 10998feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 11008feae131SDavid Howells if (pgoff >= rpgend) 11011da177e4SLinus Torvalds continue; 11021da177e4SLinus Torvalds 11038feae131SDavid Howells /* handle inexactly overlapping matches between 11048feae131SDavid Howells * mappings */ 11058feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 11068feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 11078feae131SDavid Howells /* new mapping is not a subset of the region */ 1108b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 11091da177e4SLinus Torvalds goto sharing_violation; 11101da177e4SLinus Torvalds continue; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11138feae131SDavid Howells /* we've found a region we can share */ 11141e2ae599SDavid Howells pregion->vm_usage++; 11158feae131SDavid Howells vma->vm_region = pregion; 11168feae131SDavid Howells start = pregion->vm_start; 11178feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 11188feae131SDavid Howells vma->vm_start = start; 11198feae131SDavid Howells vma->vm_end = start + len; 11201da177e4SLinus Torvalds 112122cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 11228feae131SDavid Howells vma->vm_flags |= VM_MAPPED_COPY; 112322cc877bSLeon Romanovsky else { 11248feae131SDavid Howells ret = do_mmap_shared_file(vma); 11258feae131SDavid Howells if (ret < 0) { 11268feae131SDavid Howells vma->vm_region = NULL; 11278feae131SDavid Howells vma->vm_start = 0; 11288feae131SDavid Howells vma->vm_end = 0; 11291e2ae599SDavid Howells pregion->vm_usage--; 11308feae131SDavid Howells pregion = NULL; 11318feae131SDavid Howells goto error_just_free; 11321da177e4SLinus Torvalds } 11338feae131SDavid Howells } 11348feae131SDavid Howells fput(region->vm_file); 11358feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 11368feae131SDavid Howells region = pregion; 11378feae131SDavid Howells result = start; 11388feae131SDavid Howells goto share; 11398feae131SDavid Howells } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 11421da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 11431da177e4SLinus Torvalds * tell us the location of a shared mapping 11441da177e4SLinus Torvalds */ 1145b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11461da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 11471da177e4SLinus Torvalds pgoff, flags); 1148bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 11491da177e4SLinus Torvalds ret = addr; 1150bb005a59SNamhyung Kim if (ret != -ENOSYS) 11518feae131SDavid Howells goto error_just_free; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* the driver refused to tell us where to site 11541da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 11551da177e4SLinus Torvalds * it */ 1156bb005a59SNamhyung Kim ret = -ENODEV; 1157b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 11588feae131SDavid Howells goto error_just_free; 11591da177e4SLinus Torvalds 1160b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 11618feae131SDavid Howells } else { 11628feae131SDavid Howells vma->vm_start = region->vm_start = addr; 11638feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 11688feae131SDavid Howells vma->vm_region = region; 11691da177e4SLinus Torvalds 1170645d83c5SDavid Howells /* set up the mapping 1171b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1172645d83c5SDavid Howells */ 11731da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 11748feae131SDavid Howells ret = do_mmap_shared_file(vma); 11751da177e4SLinus Torvalds else 1176645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 11771da177e4SLinus Torvalds if (ret < 0) 1178645d83c5SDavid Howells goto error_just_free; 1179645d83c5SDavid Howells add_nommu_region(region); 11808feae131SDavid Howells 1181ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 11820bf5f949SChristoph Hellwig if (!vma->vm_file && 11830bf5f949SChristoph Hellwig (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) || 11840bf5f949SChristoph Hellwig !(flags & MAP_UNINITIALIZED))) 1185ea637639SJie Zhang memset((void *)region->vm_start, 0, 1186ea637639SJie Zhang region->vm_end - region->vm_start); 1187ea637639SJie Zhang 11881da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 11898feae131SDavid Howells result = vma->vm_start; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 11921da177e4SLinus Torvalds 11938feae131SDavid Howells share: 11948feae131SDavid Howells add_vma_to_mm(current->mm, vma); 11951da177e4SLinus Torvalds 1196cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable 1197cfe79c00SMike Frysinger * mapping of it is made */ 1198cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1199a75a2df6SChristoph Hellwig flush_icache_user_range(region->vm_start, region->vm_end); 1200cfe79c00SMike Frysinger region->vm_icache_flushed = true; 1201cfe79c00SMike Frysinger } 12021da177e4SLinus Torvalds 1203cfe79c00SMike Frysinger up_write(&nommu_region_sem); 12041da177e4SLinus Torvalds 12058feae131SDavid Howells return result; 12061da177e4SLinus Torvalds 12078feae131SDavid Howells error_just_free: 12088feae131SDavid Howells up_write(&nommu_region_sem); 12098feae131SDavid Howells error: 121089a86402SDavid Howells if (region->vm_file) 12118feae131SDavid Howells fput(region->vm_file); 12128feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 121389a86402SDavid Howells if (vma->vm_file) 12148feae131SDavid Howells fput(vma->vm_file); 12153928d4f5SLinus Torvalds vm_area_free(vma); 12161da177e4SLinus Torvalds return ret; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds sharing_violation: 12198feae131SDavid Howells up_write(&nommu_region_sem); 122022cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n"); 12218feae131SDavid Howells ret = -EINVAL; 12228feae131SDavid Howells goto error; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds error_getting_vma: 12258feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 122622cc877bSLeon Romanovsky pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", 12271da177e4SLinus Torvalds len, current->pid); 12289af744d7SMichal Hocko show_free_areas(0, NULL); 12291da177e4SLinus Torvalds return -ENOMEM; 12301da177e4SLinus Torvalds 12318feae131SDavid Howells error_getting_region: 123222cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", 12331da177e4SLinus Torvalds len, current->pid); 12349af744d7SMichal Hocko show_free_areas(0, NULL); 12351da177e4SLinus Torvalds return -ENOMEM; 12361da177e4SLinus Torvalds } 12376be5ceb0SLinus Torvalds 1238a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1239a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1240a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 124166f0dc48SHugh Dickins { 124266f0dc48SHugh Dickins struct file *file = NULL; 124366f0dc48SHugh Dickins unsigned long retval = -EBADF; 124466f0dc48SHugh Dickins 1245120a795dSAl Viro audit_mmap_fd(fd, flags); 124666f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 124766f0dc48SHugh Dickins file = fget(fd); 124866f0dc48SHugh Dickins if (!file) 124966f0dc48SHugh Dickins goto out; 125066f0dc48SHugh Dickins } 125166f0dc48SHugh Dickins 1252ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 125366f0dc48SHugh Dickins 125466f0dc48SHugh Dickins if (file) 125566f0dc48SHugh Dickins fput(file); 125666f0dc48SHugh Dickins out: 125766f0dc48SHugh Dickins return retval; 125866f0dc48SHugh Dickins } 125966f0dc48SHugh Dickins 1260a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1261a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1262a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1263a90f590aSDominik Brodowski { 1264a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1265a90f590aSDominik Brodowski } 1266a90f590aSDominik Brodowski 1267a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1268a4679373SChristoph Hellwig struct mmap_arg_struct { 1269a4679373SChristoph Hellwig unsigned long addr; 1270a4679373SChristoph Hellwig unsigned long len; 1271a4679373SChristoph Hellwig unsigned long prot; 1272a4679373SChristoph Hellwig unsigned long flags; 1273a4679373SChristoph Hellwig unsigned long fd; 1274a4679373SChristoph Hellwig unsigned long offset; 1275a4679373SChristoph Hellwig }; 1276a4679373SChristoph Hellwig 1277a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1278a4679373SChristoph Hellwig { 1279a4679373SChristoph Hellwig struct mmap_arg_struct a; 1280a4679373SChristoph Hellwig 1281a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1282a4679373SChristoph Hellwig return -EFAULT; 12831824cb75SAlexander Kuleshov if (offset_in_page(a.offset)) 1284a4679373SChristoph Hellwig return -EINVAL; 1285a4679373SChristoph Hellwig 1286a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1287a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1288a4679373SChristoph Hellwig } 1289a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1290a4679373SChristoph Hellwig 12911da177e4SLinus Torvalds /* 12928feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 12938feae131SDavid Howells * for the first part or the tail. 12941da177e4SLinus Torvalds */ 12958feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 12968feae131SDavid Howells unsigned long addr, int new_below) 12971da177e4SLinus Torvalds { 12988feae131SDavid Howells struct vm_area_struct *new; 12998feae131SDavid Howells struct vm_region *region; 13008feae131SDavid Howells unsigned long npages; 13011da177e4SLinus Torvalds 1302779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1303779c1023SDavid Howells * only a single usage on the region) */ 1304779c1023SDavid Howells if (vma->vm_file) 13058feae131SDavid Howells return -ENOMEM; 13061da177e4SLinus Torvalds 13078feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 13088feae131SDavid Howells return -ENOMEM; 13091da177e4SLinus Torvalds 13108feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 13118feae131SDavid Howells if (!region) 13128feae131SDavid Howells return -ENOMEM; 13138feae131SDavid Howells 13143928d4f5SLinus Torvalds new = vm_area_dup(vma); 13158feae131SDavid Howells if (!new) { 13168feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 13178feae131SDavid Howells return -ENOMEM; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13208feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 13218feae131SDavid Howells *region = *vma->vm_region; 13228feae131SDavid Howells new->vm_region = region; 13238feae131SDavid Howells 13248feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 13258feae131SDavid Howells 13268feae131SDavid Howells if (new_below) { 1327dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 13288feae131SDavid Howells } else { 13298feae131SDavid Howells region->vm_start = new->vm_start = addr; 13308feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 13311da177e4SLinus Torvalds } 13328feae131SDavid Howells 13338feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 13348feae131SDavid Howells new->vm_ops->open(new); 13358feae131SDavid Howells 13368feae131SDavid Howells delete_vma_from_mm(vma); 13378feae131SDavid Howells down_write(&nommu_region_sem); 13388feae131SDavid Howells delete_nommu_region(vma->vm_region); 13398feae131SDavid Howells if (new_below) { 13408feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 13418feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 13428feae131SDavid Howells } else { 13438feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1344dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 13458feae131SDavid Howells } 13468feae131SDavid Howells add_nommu_region(vma->vm_region); 13478feae131SDavid Howells add_nommu_region(new->vm_region); 13488feae131SDavid Howells up_write(&nommu_region_sem); 13498feae131SDavid Howells add_vma_to_mm(mm, vma); 13508feae131SDavid Howells add_vma_to_mm(mm, new); 13518feae131SDavid Howells return 0; 13528feae131SDavid Howells } 13538feae131SDavid Howells 13548feae131SDavid Howells /* 13558feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 13568feae131SDavid Howells * the end 13578feae131SDavid Howells */ 13588feae131SDavid Howells static int shrink_vma(struct mm_struct *mm, 13598feae131SDavid Howells struct vm_area_struct *vma, 13608feae131SDavid Howells unsigned long from, unsigned long to) 13618feae131SDavid Howells { 13628feae131SDavid Howells struct vm_region *region; 13638feae131SDavid Howells 13648feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 13658feae131SDavid Howells * and list */ 13668feae131SDavid Howells delete_vma_from_mm(vma); 13678feae131SDavid Howells if (from > vma->vm_start) 13688feae131SDavid Howells vma->vm_end = from; 13698feae131SDavid Howells else 13708feae131SDavid Howells vma->vm_start = to; 13718feae131SDavid Howells add_vma_to_mm(mm, vma); 13728feae131SDavid Howells 13738feae131SDavid Howells /* cut the backing region down to size */ 13748feae131SDavid Howells region = vma->vm_region; 13751e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 13768feae131SDavid Howells 13778feae131SDavid Howells down_write(&nommu_region_sem); 13788feae131SDavid Howells delete_nommu_region(region); 1379dd8632a1SPaul Mundt if (from > region->vm_start) { 1380dd8632a1SPaul Mundt to = region->vm_top; 1381dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1382dd8632a1SPaul Mundt } else { 13838feae131SDavid Howells region->vm_start = to; 1384dd8632a1SPaul Mundt } 13858feae131SDavid Howells add_nommu_region(region); 13868feae131SDavid Howells up_write(&nommu_region_sem); 13878feae131SDavid Howells 13888feae131SDavid Howells free_page_series(from, to); 13898feae131SDavid Howells return 0; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 13923034097aSDavid Howells /* 13933034097aSDavid Howells * release a mapping 13948feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 13958feae131SDavid Howells * VMA, though it need not cover the whole VMA 13963034097aSDavid Howells */ 1397897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) 13981da177e4SLinus Torvalds { 13998feae131SDavid Howells struct vm_area_struct *vma; 1400f67d9b15SBob Liu unsigned long end; 14018feae131SDavid Howells int ret; 14021da177e4SLinus Torvalds 1403f67d9b15SBob Liu len = PAGE_ALIGN(len); 14048feae131SDavid Howells if (len == 0) 14051da177e4SLinus Torvalds return -EINVAL; 14061da177e4SLinus Torvalds 1407f67d9b15SBob Liu end = start + len; 1408f67d9b15SBob Liu 14098feae131SDavid Howells /* find the first potentially overlapping VMA */ 14108feae131SDavid Howells vma = find_vma(mm, start); 14118feae131SDavid Howells if (!vma) { 1412ac714904SChoi Gi-yong static int limit; 141333e5d769SDavid Howells if (limit < 5) { 141422cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 141533e5d769SDavid Howells current->pid, current->comm, 141633e5d769SDavid Howells start, start + len - 1); 141733e5d769SDavid Howells limit++; 141833e5d769SDavid Howells } 14198feae131SDavid Howells return -EINVAL; 14208feae131SDavid Howells } 14211da177e4SLinus Torvalds 14228feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 14238feae131SDavid Howells if (vma->vm_file) { 14248feae131SDavid Howells do { 142522cc877bSLeon Romanovsky if (start > vma->vm_start) 14268feae131SDavid Howells return -EINVAL; 14278feae131SDavid Howells if (end == vma->vm_end) 14288feae131SDavid Howells goto erase_whole_vma; 1429d75a310cSNamhyung Kim vma = vma->vm_next; 1430d75a310cSNamhyung Kim } while (vma); 14318feae131SDavid Howells return -EINVAL; 14328feae131SDavid Howells } else { 14338feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 14348feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 14358feae131SDavid Howells goto erase_whole_vma; 143622cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 14378feae131SDavid Howells return -EINVAL; 14381824cb75SAlexander Kuleshov if (offset_in_page(start)) 14398feae131SDavid Howells return -EINVAL; 14401824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end)) 14418feae131SDavid Howells return -EINVAL; 14428feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 14438feae131SDavid Howells ret = split_vma(mm, vma, start, 1); 144422cc877bSLeon Romanovsky if (ret < 0) 14458feae131SDavid Howells return ret; 14468feae131SDavid Howells } 14478feae131SDavid Howells return shrink_vma(mm, vma, start, end); 14488feae131SDavid Howells } 14491da177e4SLinus Torvalds 14508feae131SDavid Howells erase_whole_vma: 14518feae131SDavid Howells delete_vma_from_mm(vma); 14528feae131SDavid Howells delete_vma(mm, vma); 14531da177e4SLinus Torvalds return 0; 14541da177e4SLinus Torvalds } 14551da177e4SLinus Torvalds 1456bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 14573034097aSDavid Howells { 1458bfce281cSAl Viro struct mm_struct *mm = current->mm; 14593034097aSDavid Howells int ret; 14603034097aSDavid Howells 1461d8ed45c5SMichel Lespinasse mmap_write_lock(mm); 1462897ab3e0SMike Rapoport ret = do_munmap(mm, addr, len, NULL); 1463d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 14643034097aSDavid Howells return ret; 14653034097aSDavid Howells } 1466a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1467a46ef99dSLinus Torvalds 1468a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1469a46ef99dSLinus Torvalds { 1470bfce281cSAl Viro return vm_munmap(addr, len); 1471a46ef99dSLinus Torvalds } 14723034097aSDavid Howells 14733034097aSDavid Howells /* 14748feae131SDavid Howells * release all the mappings made in a process's VM space 14753034097aSDavid Howells */ 14761da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 14771da177e4SLinus Torvalds { 14788feae131SDavid Howells struct vm_area_struct *vma; 14791da177e4SLinus Torvalds 14808feae131SDavid Howells if (!mm) 14818feae131SDavid Howells return; 14828feae131SDavid Howells 14831da177e4SLinus Torvalds mm->total_vm = 0; 14841da177e4SLinus Torvalds 14858feae131SDavid Howells while ((vma = mm->mmap)) { 14868feae131SDavid Howells mm->mmap = vma->vm_next; 14878feae131SDavid Howells delete_vma_from_mm(vma); 14888feae131SDavid Howells delete_vma(mm, vma); 148904c34961SSteven J. Magnani cond_resched(); 14901da177e4SLinus Torvalds } 1491524e00b3SLiam R. Howlett __mt_destroy(&mm->mm_mt); 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 14945d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len) 14951da177e4SLinus Torvalds { 14961da177e4SLinus Torvalds return -ENOMEM; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds /* 15006fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 15016fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 15021da177e4SLinus Torvalds * 15036fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 15048feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 15058feae131SDavid Howells * block is not shareable 15061da177e4SLinus Torvalds * 15076fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 15081da177e4SLinus Torvalds */ 15094b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 15101da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 15111da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 15121da177e4SLinus Torvalds { 15136fa5f80bSDavid Howells struct vm_area_struct *vma; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds /* insanity checks first */ 1516f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1517f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 15188feae131SDavid Howells if (old_len == 0 || new_len == 0) 15191da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15201da177e4SLinus Torvalds 15211824cb75SAlexander Kuleshov if (offset_in_page(addr)) 15228feae131SDavid Howells return -EINVAL; 15238feae131SDavid Howells 15241da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 15251da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15261da177e4SLinus Torvalds 15278feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 15286fa5f80bSDavid Howells if (!vma) 15291da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15301da177e4SLinus Torvalds 15316fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 15321da177e4SLinus Torvalds return (unsigned long) -EFAULT; 15331da177e4SLinus Torvalds 15346fa5f80bSDavid Howells if (vma->vm_flags & VM_MAYSHARE) 15351da177e4SLinus Torvalds return (unsigned long) -EPERM; 15361da177e4SLinus Torvalds 15378feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 15381da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds /* all checks complete - do it */ 15416fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 15426fa5f80bSDavid Howells return vma->vm_start; 15436fa5f80bSDavid Howells } 15446fa5f80bSDavid Howells 15456a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 15466a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 15476a6160a7SHeiko Carstens unsigned long, new_addr) 15486fa5f80bSDavid Howells { 15496fa5f80bSDavid Howells unsigned long ret; 15506fa5f80bSDavid Howells 1551d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 15526fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 1553d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 15546fa5f80bSDavid Howells return ret; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 1557df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1558df06b37fSKeith Busch unsigned int foll_flags) 15591da177e4SLinus Torvalds { 15601da177e4SLinus Torvalds return NULL; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds 15638f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 15648f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 15651da177e4SLinus Torvalds { 15668f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 15678f3b1327SBob Liu return -EINVAL; 15688f3b1327SBob Liu 1569314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 157066aa2b4bSGreg Ungerer return 0; 15711da177e4SLinus Torvalds } 157222c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 15731da177e4SLinus Torvalds 15743c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 15753c0b9de6SLinus Torvalds { 15763c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 15773c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 15783c0b9de6SLinus Torvalds 15793c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 15803c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 15813c0b9de6SLinus Torvalds } 15823c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 15833c0b9de6SLinus Torvalds 1584f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1585f905bc44SPaul Mundt unsigned long pgoff) 1586f905bc44SPaul Mundt { 1587f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1588f905bc44SPaul Mundt 1589f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1590f905bc44SPaul Mundt return -EINVAL; 1591f905bc44SPaul Mundt 1592f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1593f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1594f905bc44SPaul Mundt 1595f905bc44SPaul Mundt return 0; 1596f905bc44SPaul Mundt } 1597f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1598f905bc44SPaul Mundt 15992bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf) 1600b0e15190SDavid Howells { 1601b0e15190SDavid Howells BUG(); 1602d0217ac0SNick Piggin return 0; 1603b0e15190SDavid Howells } 1604b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 16050ec76a11SDavid Howells 16063f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf, 1607bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff) 1608f1820361SKirill A. Shutemov { 1609f1820361SKirill A. Shutemov BUG(); 16103f98a28cSGeert Uytterhoeven return 0; 1611f1820361SKirill A. Shutemov } 1612f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1613f1820361SKirill A. Shutemov 1614d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, 1615d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags) 16160ec76a11SDavid Howells { 16170ec76a11SDavid Howells struct vm_area_struct *vma; 1618442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 16190ec76a11SDavid Howells 1620d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 16211e426fe2SKonstantin Khlebnikov return 0; 16220ec76a11SDavid Howells 16230ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 16240159b141SDavid Howells vma = find_vma(mm, addr); 16250159b141SDavid Howells if (vma) { 16260ec76a11SDavid Howells /* don't overrun this mapping */ 16270ec76a11SDavid Howells if (addr + len >= vma->vm_end) 16280ec76a11SDavid Howells len = vma->vm_end - addr; 16290ec76a11SDavid Howells 16300ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1631d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 16327959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 16337959722bSJie Zhang (void *) addr, buf, len); 1634d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 16357959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 16367959722bSJie Zhang buf, (void *) addr, len); 16370ec76a11SDavid Howells else 16380ec76a11SDavid Howells len = 0; 16390ec76a11SDavid Howells } else { 16400ec76a11SDavid Howells len = 0; 16410ec76a11SDavid Howells } 16420ec76a11SDavid Howells 1643d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 1644f55f199bSMike Frysinger 1645f55f199bSMike Frysinger return len; 1646f55f199bSMike Frysinger } 1647f55f199bSMike Frysinger 1648f55f199bSMike Frysinger /** 1649b7701a5fSMike Rapoport * access_remote_vm - access another process' address space 1650f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1651f55f199bSMike Frysinger * @addr: start address to access 1652f55f199bSMike Frysinger * @buf: source or destination buffer 1653f55f199bSMike Frysinger * @len: number of bytes to transfer 16546347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 1655f55f199bSMike Frysinger * 1656f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1657f55f199bSMike Frysinger */ 1658f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 16596347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 1660f55f199bSMike Frysinger { 1661d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 1662f55f199bSMike Frysinger } 1663f55f199bSMike Frysinger 1664f55f199bSMike Frysinger /* 1665f55f199bSMike Frysinger * Access another process' address space. 1666f55f199bSMike Frysinger * - source/target buffer must be kernel space 1667f55f199bSMike Frysinger */ 1668f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, 1669f307ab6dSLorenzo Stoakes unsigned int gup_flags) 1670f55f199bSMike Frysinger { 1671f55f199bSMike Frysinger struct mm_struct *mm; 1672f55f199bSMike Frysinger 1673f55f199bSMike Frysinger if (addr + len < addr) 1674f55f199bSMike Frysinger return 0; 1675f55f199bSMike Frysinger 1676f55f199bSMike Frysinger mm = get_task_mm(tsk); 1677f55f199bSMike Frysinger if (!mm) 1678f55f199bSMike Frysinger return 0; 1679f55f199bSMike Frysinger 1680d3f5ffcaSJohn Hubbard len = __access_remote_vm(mm, addr, buf, len, gup_flags); 1681f55f199bSMike Frysinger 16820ec76a11SDavid Howells mmput(mm); 16830ec76a11SDavid Howells return len; 16840ec76a11SDavid Howells } 1685fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 16867e660872SDavid Howells 16877e660872SDavid Howells /** 16887e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 16897e660872SDavid Howells * @inode: The inode to check 16907e660872SDavid Howells * @size: The current filesize of the inode 16917e660872SDavid Howells * @newsize: The proposed filesize of the inode 16927e660872SDavid Howells * 16937e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 1694c08b342cSRandy Dunlap * make sure that any outstanding VMAs aren't broken and then shrink the 1695c08b342cSRandy Dunlap * vm_regions that extend beyond so that do_mmap() doesn't 16967e660872SDavid Howells * automatically grant mappings that are too large. 16977e660872SDavid Howells */ 16987e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 16997e660872SDavid Howells size_t newsize) 17007e660872SDavid Howells { 17017e660872SDavid Howells struct vm_area_struct *vma; 17027e660872SDavid Howells struct vm_region *region; 17037e660872SDavid Howells pgoff_t low, high; 17047e660872SDavid Howells size_t r_size, r_top; 17057e660872SDavid Howells 17067e660872SDavid Howells low = newsize >> PAGE_SHIFT; 17077e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 17087e660872SDavid Howells 17097e660872SDavid Howells down_write(&nommu_region_sem); 17101acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 17117e660872SDavid Howells 17127e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 17136b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 17147e660872SDavid Howells /* found one - only interested if it's shared out of the page 17157e660872SDavid Howells * cache */ 17167e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 17171acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17187e660872SDavid Howells up_write(&nommu_region_sem); 17197e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 17207e660872SDavid Howells } 17217e660872SDavid Howells } 17227e660872SDavid Howells 17237e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 17247e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 17257e660872SDavid Howells * 17267e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 17277e660872SDavid Howells * shouldn't be any 17287e660872SDavid Howells */ 17291acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 17307e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 17317e660872SDavid Howells continue; 17327e660872SDavid Howells 17337e660872SDavid Howells region = vma->vm_region; 17347e660872SDavid Howells r_size = region->vm_top - region->vm_start; 17357e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 17367e660872SDavid Howells 17377e660872SDavid Howells if (r_top > newsize) { 17387e660872SDavid Howells region->vm_top -= r_top - newsize; 17397e660872SDavid Howells if (region->vm_end > region->vm_top) 17407e660872SDavid Howells region->vm_end = region->vm_top; 17417e660872SDavid Howells } 17427e660872SDavid Howells } 17437e660872SDavid Howells 17441acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17457e660872SDavid Howells up_write(&nommu_region_sem); 17467e660872SDavid Howells return 0; 17477e660872SDavid Howells } 1748c9b1d098SAndrew Shewmaker 1749c9b1d098SAndrew Shewmaker /* 1750c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 1751c9b1d098SAndrew Shewmaker * 1752c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 1753c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 1754c9b1d098SAndrew Shewmaker * mode. 1755c9b1d098SAndrew Shewmaker * 1756c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 1757c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 1758c9b1d098SAndrew Shewmaker */ 1759c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 1760c9b1d098SAndrew Shewmaker { 1761c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 1762c9b1d098SAndrew Shewmaker 1763c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 1764c9b1d098SAndrew Shewmaker 1765c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 1766c9b1d098SAndrew Shewmaker return 0; 1767c9b1d098SAndrew Shewmaker } 1768a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve); 17694eeab4f5SAndrew Shewmaker 17704eeab4f5SAndrew Shewmaker /* 17714eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 17724eeab4f5SAndrew Shewmaker * 17734eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 17744eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 17754eeab4f5SAndrew Shewmaker * 17764eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 17774eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 17784eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 17794eeab4f5SAndrew Shewmaker */ 17804eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 17814eeab4f5SAndrew Shewmaker { 17824eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 17834eeab4f5SAndrew Shewmaker 1784c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 17854eeab4f5SAndrew Shewmaker 17864eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 17874eeab4f5SAndrew Shewmaker return 0; 17884eeab4f5SAndrew Shewmaker } 1789a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve); 1790