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> 394c91c07cSLorenzo Stoakes #include <linux/uio.h> 401da177e4SLinus Torvalds #include <asm/tlb.h> 411da177e4SLinus Torvalds #include <asm/tlbflush.h> 42eb8cdec4SBernd Schmidt #include <asm/mmu_context.h> 438feae131SDavid Howells #include "internal.h" 448feae131SDavid Howells 451da177e4SLinus Torvalds void *high_memory; 46944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory); 471da177e4SLinus Torvalds struct page *mem_map; 481da177e4SLinus Torvalds unsigned long max_mapnr; 495b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr); 504266c97aSHugh Dickins unsigned long highest_memmap_pfn; 51fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; 521da177e4SLinus Torvalds int heap_stack_gap = 0; 531da177e4SLinus Torvalds 5433e5d769SDavid Howells atomic_long_t mmap_pages_allocated; 558feae131SDavid Howells 561da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 571da177e4SLinus Torvalds 588feae131SDavid Howells /* list of mapped, potentially shareable regions */ 598feae131SDavid Howells static struct kmem_cache *vm_region_jar; 608feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT; 618feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem); 621da177e4SLinus Torvalds 63f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = { 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * Return the total memory allocated for this pointer, not 681da177e4SLinus Torvalds * just what the caller asked for. 691da177e4SLinus Torvalds * 701da177e4SLinus Torvalds * Doesn't have to be accurate, i.e. may have races. 711da177e4SLinus Torvalds */ 721da177e4SLinus Torvalds unsigned int kobjsize(const void *objp) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds struct page *page; 751da177e4SLinus Torvalds 764016a139SMichael Hennerich /* 774016a139SMichael Hennerich * If the object we have should not have ksize performed on it, 784016a139SMichael Hennerich * return size of 0 794016a139SMichael Hennerich */ 805a1603beSPaul Mundt if (!objp || !virt_addr_valid(objp)) 816cfd53fcSPaul Mundt return 0; 826cfd53fcSPaul Mundt 836cfd53fcSPaul Mundt page = virt_to_head_page(objp); 846cfd53fcSPaul Mundt 856cfd53fcSPaul Mundt /* 866cfd53fcSPaul Mundt * If the allocator sets PageSlab, we know the pointer came from 876cfd53fcSPaul Mundt * kmalloc(). 886cfd53fcSPaul Mundt */ 891da177e4SLinus Torvalds if (PageSlab(page)) 901da177e4SLinus Torvalds return ksize(objp); 911da177e4SLinus Torvalds 926cfd53fcSPaul Mundt /* 93ab2e83eaSPaul Mundt * If it's not a compound page, see if we have a matching VMA 94ab2e83eaSPaul Mundt * region. This test is intentionally done in reverse order, 95ab2e83eaSPaul Mundt * so if there's no VMA, we still fall through and hand back 96ab2e83eaSPaul Mundt * PAGE_SIZE for 0-order pages. 97ab2e83eaSPaul Mundt */ 98ab2e83eaSPaul Mundt if (!PageCompound(page)) { 99ab2e83eaSPaul Mundt struct vm_area_struct *vma; 100ab2e83eaSPaul Mundt 101ab2e83eaSPaul Mundt vma = find_vma(current->mm, (unsigned long)objp); 102ab2e83eaSPaul Mundt if (vma) 103ab2e83eaSPaul Mundt return vma->vm_end - vma->vm_start; 104ab2e83eaSPaul Mundt } 105ab2e83eaSPaul Mundt 106ab2e83eaSPaul Mundt /* 1076cfd53fcSPaul Mundt * The ksize() function is only guaranteed to work for pointers 1085a1603beSPaul Mundt * returned by kmalloc(). So handle arbitrary pointers here. 1096cfd53fcSPaul Mundt */ 110a50b854eSMatthew Wilcox (Oracle) return page_size(page); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 113dfc2f91aSPaul Mundt /** 114dfc2f91aSPaul Mundt * follow_pfn - look up PFN at a user virtual address 115dfc2f91aSPaul Mundt * @vma: memory mapping 116dfc2f91aSPaul Mundt * @address: user virtual address 117dfc2f91aSPaul Mundt * @pfn: location to store found PFN 118dfc2f91aSPaul Mundt * 119dfc2f91aSPaul Mundt * Only IO mappings and raw PFN mappings are allowed. 120dfc2f91aSPaul Mundt * 121dfc2f91aSPaul Mundt * Returns zero and the pfn at @pfn on success, -ve otherwise. 122dfc2f91aSPaul Mundt */ 123dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address, 124dfc2f91aSPaul Mundt unsigned long *pfn) 125dfc2f91aSPaul Mundt { 126dfc2f91aSPaul Mundt if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 127dfc2f91aSPaul Mundt return -EINVAL; 128dfc2f91aSPaul Mundt 129dfc2f91aSPaul Mundt *pfn = address >> PAGE_SHIFT; 130dfc2f91aSPaul Mundt return 0; 131dfc2f91aSPaul Mundt } 132dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn); 133dfc2f91aSPaul Mundt 134f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 1351da177e4SLinus Torvalds 136b3bdda02SChristoph Lameter void vfree(const void *addr) 1371da177e4SLinus Torvalds { 1381da177e4SLinus Torvalds kfree(addr); 1391da177e4SLinus Torvalds } 140b5073173SPaul Mundt EXPORT_SYMBOL(vfree); 1411da177e4SLinus Torvalds 14288dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds /* 1458518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 1468518609dSRobert P. J. Day * returns only a logical address. 1471da177e4SLinus Torvalds */ 14884097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 1491da177e4SLinus Torvalds } 150b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc); 1511da177e4SLinus Torvalds 152041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align, 153041de93fSChristoph Hellwig unsigned long start, unsigned long end, gfp_t gfp_mask, 154041de93fSChristoph Hellwig pgprot_t prot, unsigned long vm_flags, int node, 155041de93fSChristoph Hellwig const void *caller) 156041de93fSChristoph Hellwig { 157041de93fSChristoph Hellwig return __vmalloc(size, gfp_mask); 158041de93fSChristoph Hellwig } 159041de93fSChristoph Hellwig 1602b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, 1612b905948SChristoph Hellwig int node, const void *caller) 162a7c3e901SMichal Hocko { 1632b905948SChristoph Hellwig return __vmalloc(size, gfp_mask); 164a7c3e901SMichal Hocko } 165a7c3e901SMichal Hocko 166ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) 167f905bc44SPaul Mundt { 168f905bc44SPaul Mundt void *ret; 169f905bc44SPaul Mundt 17088dca4caSChristoph Hellwig ret = __vmalloc(size, flags); 171f905bc44SPaul Mundt if (ret) { 172f905bc44SPaul Mundt struct vm_area_struct *vma; 173f905bc44SPaul Mundt 174d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 175f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret); 176f905bc44SPaul Mundt if (vma) 1771c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_USERMAP); 178d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 179f905bc44SPaul Mundt } 180f905bc44SPaul Mundt 181f905bc44SPaul Mundt return ret; 182f905bc44SPaul Mundt } 183ed81745aSAndrii Nakryiko 184ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size) 185ed81745aSAndrii Nakryiko { 186ed81745aSAndrii Nakryiko return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO); 187ed81745aSAndrii Nakryiko } 188f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user); 189f905bc44SPaul Mundt 190b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr) 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds return virt_to_page(addr); 1931da177e4SLinus Torvalds } 194b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page); 1951da177e4SLinus Torvalds 196b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr)); 1991da177e4SLinus Torvalds } 200b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn); 2011da177e4SLinus Torvalds 2024c91c07cSLorenzo Stoakes long vread_iter(struct iov_iter *iter, const char *addr, size_t count) 2031da177e4SLinus Torvalds { 2049bde916bSChen Gang /* Don't allow overflow */ 2054c91c07cSLorenzo Stoakes if ((unsigned long) addr + count < count) 2064c91c07cSLorenzo Stoakes count = -(unsigned long) addr; 2079bde916bSChen Gang 2084c91c07cSLorenzo Stoakes return copy_to_iter(addr, count, iter); 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 5478220543dSMatthew Wilcox (Oracle) static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm) 5483034097aSDavid Howells { 5498feae131SDavid Howells vma->vm_mm = mm; 5508feae131SDavid Howells 5518feae131SDavid Howells /* add the VMA to the mapping */ 5528feae131SDavid Howells if (vma->vm_file) { 5538220543dSMatthew Wilcox (Oracle) struct address_space *mapping = vma->vm_file->f_mapping; 5548feae131SDavid Howells 55583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 5568feae131SDavid Howells flush_dcache_mmap_lock(mapping); 5576b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5588feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 55983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5608feae131SDavid Howells } 5618220543dSMatthew Wilcox (Oracle) } 5628feae131SDavid Howells 5638220543dSMatthew Wilcox (Oracle) static void cleanup_vma_from_mm(struct vm_area_struct *vma) 5648220543dSMatthew Wilcox (Oracle) { 5657964cf8cSLiam R. Howlett vma->vm_mm->map_count--; 5668feae131SDavid Howells /* remove the VMA from the mapping */ 5678feae131SDavid Howells if (vma->vm_file) { 5687964cf8cSLiam R. Howlett struct address_space *mapping; 5698feae131SDavid Howells mapping = vma->vm_file->f_mapping; 5708feae131SDavid Howells 57183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 5728feae131SDavid Howells flush_dcache_mmap_lock(mapping); 5736b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 5748feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 57583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5768feae131SDavid Howells } 5778220543dSMatthew Wilcox (Oracle) } 57847d9644dSLiam R. Howlett 5798220543dSMatthew Wilcox (Oracle) /* 5808220543dSMatthew Wilcox (Oracle) * delete a VMA from its owning mm_struct and address space 5818220543dSMatthew Wilcox (Oracle) */ 5828220543dSMatthew Wilcox (Oracle) static int delete_vma_from_mm(struct vm_area_struct *vma) 5838220543dSMatthew Wilcox (Oracle) { 58447d9644dSLiam R. Howlett VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start); 5858220543dSMatthew Wilcox (Oracle) 58647d9644dSLiam R. Howlett if (vma_iter_prealloc(&vmi)) { 5878220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 5888220543dSMatthew Wilcox (Oracle) current->pid); 5898220543dSMatthew Wilcox (Oracle) return -ENOMEM; 5908220543dSMatthew Wilcox (Oracle) } 5918220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 5928feae131SDavid Howells 5938feae131SDavid Howells /* remove from the MM's tree and list */ 59447d9644dSLiam R. Howlett vma_iter_clear(&vmi, vma->vm_start, vma->vm_end); 5958220543dSMatthew Wilcox (Oracle) return 0; 5968feae131SDavid Howells } 5978feae131SDavid Howells /* 5988feae131SDavid Howells * destroy a VMA record 5998feae131SDavid Howells */ 6008feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 6018feae131SDavid Howells { 6028feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 6038feae131SDavid Howells vma->vm_ops->close(vma); 604e9714acfSKonstantin Khlebnikov if (vma->vm_file) 6058feae131SDavid Howells fput(vma->vm_file); 6068feae131SDavid Howells put_nommu_region(vma->vm_region); 6073928d4f5SLinus Torvalds vm_area_free(vma); 6083034097aSDavid Howells } 6093034097aSDavid Howells 610abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 611abdba2ddSLiam R. Howlett unsigned long start_addr, 612abdba2ddSLiam R. Howlett unsigned long end_addr) 613abdba2ddSLiam R. Howlett { 614abdba2ddSLiam R. Howlett unsigned long index = start_addr; 615abdba2ddSLiam R. Howlett 616abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 617abdba2ddSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 618abdba2ddSLiam R. Howlett } 619abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 620abdba2ddSLiam R. Howlett 6213034097aSDavid Howells /* 6223034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 623c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6243034097aSDavid Howells */ 6253034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 6263034097aSDavid Howells { 62747d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 6283034097aSDavid Howells 62947d9644dSLiam R. Howlett return vma_iter_load(&vmi); 6303034097aSDavid Howells } 6313034097aSDavid Howells EXPORT_SYMBOL(find_vma); 6323034097aSDavid Howells 6333034097aSDavid Howells /* 634*d85a143bSLinus Torvalds * At least xtensa ends up having protection faults even with no 635*d85a143bSLinus Torvalds * MMU.. No stack expansion, at least. 636*d85a143bSLinus Torvalds */ 637*d85a143bSLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 638*d85a143bSLinus Torvalds unsigned long addr, struct pt_regs *regs) 639*d85a143bSLinus Torvalds { 640*d85a143bSLinus Torvalds mmap_read_lock(mm); 641*d85a143bSLinus Torvalds return vma_lookup(mm, addr); 642*d85a143bSLinus Torvalds } 643*d85a143bSLinus Torvalds 644*d85a143bSLinus Torvalds /* 6458feae131SDavid Howells * expand a stack to a given address 6468feae131SDavid Howells * - not supported under NOMMU conditions 6478feae131SDavid Howells */ 6488d7071afSLinus Torvalds int expand_stack_locked(struct vm_area_struct *vma, unsigned long addr) 64957c8f63eSGreg Ungerer { 65057c8f63eSGreg Ungerer return -ENOMEM; 65157c8f63eSGreg Ungerer } 65257c8f63eSGreg Ungerer 6538d7071afSLinus Torvalds struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr) 6548d7071afSLinus Torvalds { 6558d7071afSLinus Torvalds mmap_read_unlock(mm); 6568d7071afSLinus Torvalds return NULL; 6578d7071afSLinus Torvalds } 6588d7071afSLinus Torvalds 659930e652aSDavid Howells /* 6606fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 661c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6626fa5f80bSDavid Howells */ 6638feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 6648feae131SDavid Howells unsigned long addr, 6658feae131SDavid Howells unsigned long len) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct vm_area_struct *vma; 6688feae131SDavid Howells unsigned long end = addr + len; 66947d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 6701da177e4SLinus Torvalds 67147d9644dSLiam R. Howlett vma = vma_iter_load(&vmi); 672524e00b3SLiam R. Howlett if (!vma) 6738feae131SDavid Howells return NULL; 674524e00b3SLiam R. Howlett if (vma->vm_start != addr) 675524e00b3SLiam R. Howlett return NULL; 676524e00b3SLiam R. Howlett if (vma->vm_end != end) 677524e00b3SLiam R. Howlett return NULL; 678524e00b3SLiam R. Howlett 6798feae131SDavid Howells return vma; 6808feae131SDavid Howells } 6811da177e4SLinus Torvalds 6823034097aSDavid Howells /* 6831da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 6841da177e4SLinus Torvalds * mapping we're capable of supporting 6851da177e4SLinus Torvalds */ 6861da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 6871da177e4SLinus Torvalds unsigned long addr, 6881da177e4SLinus Torvalds unsigned long len, 6891da177e4SLinus Torvalds unsigned long prot, 6901da177e4SLinus Torvalds unsigned long flags, 6911da177e4SLinus Torvalds unsigned long pgoff, 6921da177e4SLinus Torvalds unsigned long *_capabilities) 6931da177e4SLinus Torvalds { 6948feae131SDavid Howells unsigned long capabilities, rlen; 6951da177e4SLinus Torvalds int ret; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* do the simple checks first */ 69822cc877bSLeon Romanovsky if (flags & MAP_FIXED) 6991da177e4SLinus Torvalds return -EINVAL; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 7021da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 7031da177e4SLinus Torvalds return -EINVAL; 7041da177e4SLinus Torvalds 705f81cff0dSMike Frysinger if (!len) 7061da177e4SLinus Torvalds return -EINVAL; 7071da177e4SLinus Torvalds 708f81cff0dSMike Frysinger /* Careful about overflows.. */ 7098feae131SDavid Howells rlen = PAGE_ALIGN(len); 7108feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 711f81cff0dSMike Frysinger return -ENOMEM; 712f81cff0dSMike Frysinger 7131da177e4SLinus Torvalds /* offset overflow? */ 7148feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 715f81cff0dSMike Frysinger return -EOVERFLOW; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds if (file) { 7181da177e4SLinus Torvalds /* files must support mmap */ 71972c2d531SAl Viro if (!file->f_op->mmap) 7201da177e4SLinus Torvalds return -ENODEV; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 7231da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 7241da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 7251da177e4SLinus Torvalds */ 726b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 727b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 728b4caecd4SChristoph Hellwig } else { 7291da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 7301da177e4SLinus Torvalds * defaults */ 731496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 7321da177e4SLinus Torvalds case S_IFREG: 7331da177e4SLinus Torvalds case S_IFBLK: 734b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 7351da177e4SLinus Torvalds break; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds case S_IFCHR: 7381da177e4SLinus Torvalds capabilities = 739b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 740b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 741b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 7421da177e4SLinus Torvalds break; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds default: 7451da177e4SLinus Torvalds return -EINVAL; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 7501da177e4SLinus Torvalds * device */ 7511da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 752b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7536e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 754b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 7551da177e4SLinus Torvalds 75628d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 75728d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 75828d7a6aeSGraff Yang return -EACCES; 75928d7a6aeSGraff Yang 7601da177e4SLinus Torvalds if (flags & MAP_SHARED) { 7611da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 7621da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 7631da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 7641da177e4SLinus Torvalds return -EACCES; 7651da177e4SLinus Torvalds 766496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 7671da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 7681da177e4SLinus Torvalds return -EACCES; 7691da177e4SLinus Torvalds 770b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 7711da177e4SLinus Torvalds return -ENODEV; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 774b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 775ac714904SChoi Gi-yong } else { 7761da177e4SLinus Torvalds /* we're going to read the file into private memory we 7771da177e4SLinus Torvalds * allocate */ 778b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 7791da177e4SLinus Torvalds return -ENODEV; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 7821da177e4SLinus Torvalds * shared with the backing device */ 7831da177e4SLinus Torvalds if (prot & PROT_WRITE) 784b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 787b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 788b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 789b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 790b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 7913c7b2045SBernd Schmidt ) { 792b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7933c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 79422cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 7953c7b2045SBernd Schmidt return -EINVAL; 7963c7b2045SBernd Schmidt } 7973c7b2045SBernd Schmidt } 7983c7b2045SBernd Schmidt } 7993c7b2045SBernd Schmidt 8001da177e4SLinus Torvalds /* handle executable mappings and implied executable 8011da177e4SLinus Torvalds * mappings */ 80290f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 8031da177e4SLinus Torvalds if (prot & PROT_EXEC) 8041da177e4SLinus Torvalds return -EPERM; 805ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 8061da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 8071da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 808b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 8091da177e4SLinus Torvalds prot |= PROT_EXEC; 8101da177e4SLinus Torvalds } 811ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 8121da177e4SLinus Torvalds (prot & PROT_EXEC) && 813b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 8141da177e4SLinus Torvalds ) { 8151da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 816b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8171da177e4SLinus Torvalds } 818ac714904SChoi Gi-yong } else { 8191da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 8201da177e4SLinus Torvalds * privately mapped 8211da177e4SLinus Torvalds */ 822b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 8251da177e4SLinus Torvalds if ((prot & PROT_READ) && 8261da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 8271da177e4SLinus Torvalds prot |= PROT_EXEC; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* allow the security API to have its say */ 831e5467859SAl Viro ret = security_mmap_addr(addr); 832e5467859SAl Viro if (ret < 0) 833e5467859SAl Viro return ret; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* looks okay */ 8361da177e4SLinus Torvalds *_capabilities = capabilities; 8371da177e4SLinus Torvalds return 0; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* 8411da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 8421da177e4SLinus Torvalds * now know into VMA flags 8431da177e4SLinus Torvalds */ 8441da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 8451da177e4SLinus Torvalds unsigned long prot, 8461da177e4SLinus Torvalds unsigned long flags, 8471da177e4SLinus Torvalds unsigned long capabilities) 8481da177e4SLinus Torvalds { 8491da177e4SLinus Torvalds unsigned long vm_flags; 8501da177e4SLinus Torvalds 851e6bfb709SDave Hansen vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); 8521da177e4SLinus Torvalds 853b6b7a8faSDavid Hildenbrand if (!file) { 854b6b7a8faSDavid Hildenbrand /* 855b6b7a8faSDavid Hildenbrand * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because 856b6b7a8faSDavid Hildenbrand * there is no fork(). 8571da177e4SLinus Torvalds */ 858b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 859b6b7a8faSDavid Hildenbrand } else if (flags & MAP_PRIVATE) { 860b6b7a8faSDavid Hildenbrand /* MAP_PRIVATE file mapping */ 861b6b7a8faSDavid Hildenbrand if (capabilities & NOMMU_MAP_DIRECT) 862b6b7a8faSDavid Hildenbrand vm_flags |= (capabilities & NOMMU_VMFLAGS); 863b6b7a8faSDavid Hildenbrand else 864b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 865b6b7a8faSDavid Hildenbrand 866b6b7a8faSDavid Hildenbrand if (!(prot & PROT_WRITE) && !current->ptrace) 867b6b7a8faSDavid Hildenbrand /* 868b6b7a8faSDavid Hildenbrand * R/O private file mapping which cannot be used to 869b6b7a8faSDavid Hildenbrand * modify memory, especially also not via active ptrace 870b6b7a8faSDavid Hildenbrand * (e.g., set breakpoints) or later by upgrading 871b6b7a8faSDavid Hildenbrand * permissions (no mprotect()). We can try overlaying 872b6b7a8faSDavid Hildenbrand * the file mapping, which will work e.g., on chardevs, 873b6b7a8faSDavid Hildenbrand * ramfs/tmpfs/shmfs and romfs/cramf. 874b6b7a8faSDavid Hildenbrand */ 875b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYOVERLAY; 876b6b7a8faSDavid Hildenbrand } else { 877b6b7a8faSDavid Hildenbrand /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */ 878b6b7a8faSDavid Hildenbrand vm_flags |= VM_SHARED | VM_MAYSHARE | 879b6b7a8faSDavid Hildenbrand (capabilities & NOMMU_VMFLAGS); 880b6b7a8faSDavid Hildenbrand } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds return vm_flags; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* 8868feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 8878feae131SDavid Howells * pins the storage) 8881da177e4SLinus Torvalds */ 8898feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 8901da177e4SLinus Torvalds { 8911da177e4SLinus Torvalds int ret; 8921da177e4SLinus Torvalds 893f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 894dd8632a1SPaul Mundt if (ret == 0) { 895dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 896645d83c5SDavid Howells return 0; 897dd8632a1SPaul Mundt } 8981da177e4SLinus Torvalds if (ret != -ENOSYS) 8991da177e4SLinus Torvalds return ret; 9001da177e4SLinus Torvalds 9013fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 9023fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 9033fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 9041da177e4SLinus Torvalds return -ENODEV; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds /* 9081da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 9091da177e4SLinus Torvalds */ 9108feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 9118feae131SDavid Howells struct vm_region *region, 912645d83c5SDavid Howells unsigned long len, 913645d83c5SDavid Howells unsigned long capabilities) 9141da177e4SLinus Torvalds { 915dbc8358cSJoonsoo Kim unsigned long total, point; 9161da177e4SLinus Torvalds void *base; 9178feae131SDavid Howells int ret, order; 9181da177e4SLinus Torvalds 919b6b7a8faSDavid Hildenbrand /* 920b6b7a8faSDavid Hildenbrand * Invoke the file's mapping function so that it can keep track of 921b6b7a8faSDavid Hildenbrand * shared mappings on devices or memory. VM_MAYOVERLAY will be set if 922b6b7a8faSDavid Hildenbrand * it may attempt to share, which will make is_nommu_shared_mapping() 923b6b7a8faSDavid Hildenbrand * happy. 9241da177e4SLinus Torvalds */ 925b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 926f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 9271da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 928fc4f4be9SDavid Hildenbrand if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags))) 929fc4f4be9SDavid Hildenbrand ret = -ENOSYS; 930fc4f4be9SDavid Hildenbrand if (ret == 0) { 931dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 932645d83c5SDavid Howells return 0; 9331da177e4SLinus Torvalds } 934dd8632a1SPaul Mundt if (ret != -ENOSYS) 935dd8632a1SPaul Mundt return ret; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 9381da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 9391da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9428feae131SDavid Howells 9431da177e4SLinus Torvalds /* allocate some memory to hold the mapping 9441da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 9451da177e4SLinus Torvalds * we're allocating is smaller than a page 9461da177e4SLinus Torvalds */ 947f67d9b15SBob Liu order = get_order(len); 9488feae131SDavid Howells total = 1 << order; 949f67d9b15SBob Liu point = len >> PAGE_SHIFT; 950dd8632a1SPaul Mundt 951dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 95222cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 953dbc8358cSJoonsoo Kim total = point; 9548feae131SDavid Howells 955da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 956dbc8358cSJoonsoo Kim if (!base) 957dbc8358cSJoonsoo Kim goto enomem; 9588feae131SDavid Howells 959dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 960dbc8358cSJoonsoo Kim 9611c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY); 9621c71222eSSuren Baghdasaryan region->vm_flags = vma->vm_flags; 9638feae131SDavid Howells region->vm_start = (unsigned long) base; 964f67d9b15SBob Liu region->vm_end = region->vm_start + len; 965dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 9668feae131SDavid Howells 9678feae131SDavid Howells vma->vm_start = region->vm_start; 9688feae131SDavid Howells vma->vm_end = region->vm_start + len; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if (vma->vm_file) { 9711da177e4SLinus Torvalds /* read the contents of a file into the copy */ 9721da177e4SLinus Torvalds loff_t fpos; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds fpos = vma->vm_pgoff; 9751da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 9761da177e4SLinus Torvalds 977b4bf802aSChristoph Hellwig ret = kernel_read(vma->vm_file, base, len, &fpos); 9781da177e4SLinus Torvalds if (ret < 0) 9791da177e4SLinus Torvalds goto error_free; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds /* clear the last little bit */ 982f67d9b15SBob Liu if (ret < len) 983f67d9b15SBob Liu memset(base + ret, 0, len - ret); 9841da177e4SLinus Torvalds 985bfd40eafSKirill A. Shutemov } else { 986bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds return 0; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds error_free: 9927223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 9938feae131SDavid Howells region->vm_start = vma->vm_start = 0; 9948feae131SDavid Howells region->vm_end = vma->vm_end = 0; 995dd8632a1SPaul Mundt region->vm_top = 0; 9961da177e4SLinus Torvalds return ret; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds enomem: 999b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 100005ae6fa3SGreg Ungerer len, current->pid, current->comm); 10019af744d7SMichal Hocko show_free_areas(0, NULL); 10021da177e4SLinus Torvalds return -ENOMEM; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds /* 10061da177e4SLinus Torvalds * handle mapping creation for uClinux 10071da177e4SLinus Torvalds */ 10081fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, 10091da177e4SLinus Torvalds unsigned long addr, 10101da177e4SLinus Torvalds unsigned long len, 10111da177e4SLinus Torvalds unsigned long prot, 10121da177e4SLinus Torvalds unsigned long flags, 1013bebeb3d6SMichel Lespinasse unsigned long pgoff, 1014897ab3e0SMike Rapoport unsigned long *populate, 1015897ab3e0SMike Rapoport struct list_head *uf) 10161da177e4SLinus Torvalds { 10178feae131SDavid Howells struct vm_area_struct *vma; 10188feae131SDavid Howells struct vm_region *region; 10191da177e4SLinus Torvalds struct rb_node *rb; 102045e55300SPeter Collingbourne vm_flags_t vm_flags; 10211fcfd8dbSOleg Nesterov unsigned long capabilities, result; 10221da177e4SLinus Torvalds int ret; 102347d9644dSLiam R. Howlett VMA_ITERATOR(vmi, current->mm, 0); 10241da177e4SLinus Torvalds 102541badc15SMichel Lespinasse *populate = 0; 1026bebeb3d6SMichel Lespinasse 10271da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 10281da177e4SLinus Torvalds * mapping */ 10291da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 10301da177e4SLinus Torvalds &capabilities); 103122cc877bSLeon Romanovsky if (ret < 0) 10321da177e4SLinus Torvalds return ret; 10331da177e4SLinus Torvalds 103406aab5a3SDavid Howells /* we ignore the address hint */ 103506aab5a3SDavid Howells addr = 0; 1036f67d9b15SBob Liu len = PAGE_ALIGN(len); 103706aab5a3SDavid Howells 10381da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 10391da177e4SLinus Torvalds * now know into VMA flags */ 104045e55300SPeter Collingbourne vm_flags = determine_vm_flags(file, prot, flags, capabilities); 10411da177e4SLinus Torvalds 10428220543dSMatthew Wilcox (Oracle) 10438feae131SDavid Howells /* we're going to need to record the mapping */ 10448feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 10458feae131SDavid Howells if (!region) 10468feae131SDavid Howells goto error_getting_region; 10471da177e4SLinus Torvalds 1048490fc053SLinus Torvalds vma = vm_area_alloc(current->mm); 10498feae131SDavid Howells if (!vma) 10508feae131SDavid Howells goto error_getting_vma; 10511da177e4SLinus Torvalds 105247d9644dSLiam R. Howlett if (vma_iter_prealloc(&vmi)) 105347d9644dSLiam R. Howlett goto error_vma_iter_prealloc; 10548220543dSMatthew Wilcox (Oracle) 10551e2ae599SDavid Howells region->vm_usage = 1; 10568feae131SDavid Howells region->vm_flags = vm_flags; 10578feae131SDavid Howells region->vm_pgoff = pgoff; 10588feae131SDavid Howells 10591c71222eSSuren Baghdasaryan vm_flags_init(vma, vm_flags); 10608feae131SDavid Howells vma->vm_pgoff = pgoff; 10618feae131SDavid Howells 10628feae131SDavid Howells if (file) { 1063cb0942b8SAl Viro region->vm_file = get_file(file); 1064cb0942b8SAl Viro vma->vm_file = get_file(file); 10658feae131SDavid Howells } 10668feae131SDavid Howells 10678feae131SDavid Howells down_write(&nommu_region_sem); 10688feae131SDavid Howells 10698feae131SDavid Howells /* if we want to share, we need to check for regions created by other 10701da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 10718feae131SDavid Howells * - we can only share with a superset match on most regular files 10721da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 10731da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 10741da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 10751da177e4SLinus Torvalds * than here 10761da177e4SLinus Torvalds */ 1077fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vm_flags)) { 10788feae131SDavid Howells struct vm_region *pregion; 10798feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 10801da177e4SLinus Torvalds 10818feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 10828feae131SDavid Howells pgend = pgoff + pglen; 1083165b2392SDavid Howells 10848feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 10858feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 10861da177e4SLinus Torvalds 1087fc4f4be9SDavid Hildenbrand if (!is_nommu_shared_mapping(pregion->vm_flags)) 10881da177e4SLinus Torvalds continue; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1091496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1092496ad9aaSAl Viro file_inode(file)) 10931da177e4SLinus Torvalds continue; 10941da177e4SLinus Torvalds 10958feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 10961da177e4SLinus Torvalds continue; 10971da177e4SLinus Torvalds 10988feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 10998feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 11008feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 11018feae131SDavid Howells if (pgoff >= rpgend) 11021da177e4SLinus Torvalds continue; 11031da177e4SLinus Torvalds 11048feae131SDavid Howells /* handle inexactly overlapping matches between 11058feae131SDavid Howells * mappings */ 11068feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 11078feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 11088feae131SDavid Howells /* new mapping is not a subset of the region */ 1109b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 11101da177e4SLinus Torvalds goto sharing_violation; 11111da177e4SLinus Torvalds continue; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds 11148feae131SDavid Howells /* we've found a region we can share */ 11151e2ae599SDavid Howells pregion->vm_usage++; 11168feae131SDavid Howells vma->vm_region = pregion; 11178feae131SDavid Howells start = pregion->vm_start; 11188feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 11198feae131SDavid Howells vma->vm_start = start; 11208feae131SDavid Howells vma->vm_end = start + len; 11211da177e4SLinus Torvalds 112222cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 11231c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY); 112422cc877bSLeon Romanovsky else { 11258feae131SDavid Howells ret = do_mmap_shared_file(vma); 11268feae131SDavid Howells if (ret < 0) { 11278feae131SDavid Howells vma->vm_region = NULL; 11288feae131SDavid Howells vma->vm_start = 0; 11298feae131SDavid Howells vma->vm_end = 0; 11301e2ae599SDavid Howells pregion->vm_usage--; 11318feae131SDavid Howells pregion = NULL; 11328feae131SDavid Howells goto error_just_free; 11331da177e4SLinus Torvalds } 11348feae131SDavid Howells } 11358feae131SDavid Howells fput(region->vm_file); 11368feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 11378feae131SDavid Howells region = pregion; 11388feae131SDavid Howells result = start; 11398feae131SDavid Howells goto share; 11408feae131SDavid Howells } 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 11431da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 11441da177e4SLinus Torvalds * tell us the location of a shared mapping 11451da177e4SLinus Torvalds */ 1146b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11471da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 11481da177e4SLinus Torvalds pgoff, flags); 1149bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 11501da177e4SLinus Torvalds ret = addr; 1151bb005a59SNamhyung Kim if (ret != -ENOSYS) 11528feae131SDavid Howells goto error_just_free; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* the driver refused to tell us where to site 11551da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 11561da177e4SLinus Torvalds * it */ 1157bb005a59SNamhyung Kim ret = -ENODEV; 1158b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 11598feae131SDavid Howells goto error_just_free; 11601da177e4SLinus Torvalds 1161b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 11628feae131SDavid Howells } else { 11638feae131SDavid Howells vma->vm_start = region->vm_start = addr; 11648feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11698feae131SDavid Howells vma->vm_region = region; 11701da177e4SLinus Torvalds 1171645d83c5SDavid Howells /* set up the mapping 1172b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1173645d83c5SDavid Howells */ 11741da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 11758feae131SDavid Howells ret = do_mmap_shared_file(vma); 11761da177e4SLinus Torvalds else 1177645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 11781da177e4SLinus Torvalds if (ret < 0) 1179645d83c5SDavid Howells goto error_just_free; 1180645d83c5SDavid Howells add_nommu_region(region); 11818feae131SDavid Howells 1182ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 11830bf5f949SChristoph Hellwig if (!vma->vm_file && 11840bf5f949SChristoph Hellwig (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) || 11850bf5f949SChristoph Hellwig !(flags & MAP_UNINITIALIZED))) 1186ea637639SJie Zhang memset((void *)region->vm_start, 0, 1187ea637639SJie Zhang region->vm_end - region->vm_start); 1188ea637639SJie Zhang 11891da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 11908feae131SDavid Howells result = vma->vm_start; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 11931da177e4SLinus Torvalds 11948feae131SDavid Howells share: 119507f1bc5aSLiam R. Howlett BUG_ON(!vma->vm_region); 119607f1bc5aSLiam R. Howlett setup_vma_to_mm(vma, current->mm); 119707f1bc5aSLiam R. Howlett current->mm->map_count++; 119807f1bc5aSLiam R. Howlett /* add the VMA to the tree */ 119907f1bc5aSLiam R. Howlett vma_iter_store(&vmi, vma); 12001da177e4SLinus Torvalds 1201cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable 1202cfe79c00SMike Frysinger * mapping of it is made */ 1203cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1204a75a2df6SChristoph Hellwig flush_icache_user_range(region->vm_start, region->vm_end); 1205cfe79c00SMike Frysinger region->vm_icache_flushed = true; 1206cfe79c00SMike Frysinger } 12071da177e4SLinus Torvalds 1208cfe79c00SMike Frysinger up_write(&nommu_region_sem); 12091da177e4SLinus Torvalds 12108feae131SDavid Howells return result; 12111da177e4SLinus Torvalds 12128feae131SDavid Howells error_just_free: 12138feae131SDavid Howells up_write(&nommu_region_sem); 12148feae131SDavid Howells error: 121547d9644dSLiam R. Howlett vma_iter_free(&vmi); 121689a86402SDavid Howells if (region->vm_file) 12178feae131SDavid Howells fput(region->vm_file); 12188feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 121989a86402SDavid Howells if (vma->vm_file) 12208feae131SDavid Howells fput(vma->vm_file); 12213928d4f5SLinus Torvalds vm_area_free(vma); 12221da177e4SLinus Torvalds return ret; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds sharing_violation: 12258feae131SDavid Howells up_write(&nommu_region_sem); 122622cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n"); 12278feae131SDavid Howells ret = -EINVAL; 12288feae131SDavid Howells goto error; 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds error_getting_vma: 12318feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 123222cc877bSLeon Romanovsky pr_warn("Allocation of vma 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 12378feae131SDavid Howells error_getting_region: 123822cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", 12391da177e4SLinus Torvalds len, current->pid); 12409af744d7SMichal Hocko show_free_areas(0, NULL); 12411da177e4SLinus Torvalds return -ENOMEM; 12428220543dSMatthew Wilcox (Oracle) 124347d9644dSLiam R. Howlett error_vma_iter_prealloc: 12448220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 12458220543dSMatthew Wilcox (Oracle) vm_area_free(vma); 12468220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", current->pid); 12478220543dSMatthew Wilcox (Oracle) show_free_areas(0, NULL); 12488220543dSMatthew Wilcox (Oracle) return -ENOMEM; 12498220543dSMatthew Wilcox (Oracle) 12501da177e4SLinus Torvalds } 12516be5ceb0SLinus Torvalds 1252a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1253a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1254a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 125566f0dc48SHugh Dickins { 125666f0dc48SHugh Dickins struct file *file = NULL; 125766f0dc48SHugh Dickins unsigned long retval = -EBADF; 125866f0dc48SHugh Dickins 1259120a795dSAl Viro audit_mmap_fd(fd, flags); 126066f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 126166f0dc48SHugh Dickins file = fget(fd); 126266f0dc48SHugh Dickins if (!file) 126366f0dc48SHugh Dickins goto out; 126466f0dc48SHugh Dickins } 126566f0dc48SHugh Dickins 1266ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 126766f0dc48SHugh Dickins 126866f0dc48SHugh Dickins if (file) 126966f0dc48SHugh Dickins fput(file); 127066f0dc48SHugh Dickins out: 127166f0dc48SHugh Dickins return retval; 127266f0dc48SHugh Dickins } 127366f0dc48SHugh Dickins 1274a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1275a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1276a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1277a90f590aSDominik Brodowski { 1278a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1279a90f590aSDominik Brodowski } 1280a90f590aSDominik Brodowski 1281a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1282a4679373SChristoph Hellwig struct mmap_arg_struct { 1283a4679373SChristoph Hellwig unsigned long addr; 1284a4679373SChristoph Hellwig unsigned long len; 1285a4679373SChristoph Hellwig unsigned long prot; 1286a4679373SChristoph Hellwig unsigned long flags; 1287a4679373SChristoph Hellwig unsigned long fd; 1288a4679373SChristoph Hellwig unsigned long offset; 1289a4679373SChristoph Hellwig }; 1290a4679373SChristoph Hellwig 1291a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1292a4679373SChristoph Hellwig { 1293a4679373SChristoph Hellwig struct mmap_arg_struct a; 1294a4679373SChristoph Hellwig 1295a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1296a4679373SChristoph Hellwig return -EFAULT; 12971824cb75SAlexander Kuleshov if (offset_in_page(a.offset)) 1298a4679373SChristoph Hellwig return -EINVAL; 1299a4679373SChristoph Hellwig 1300a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1301a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1302a4679373SChristoph Hellwig } 1303a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1304a4679373SChristoph Hellwig 13051da177e4SLinus Torvalds /* 13068feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 13078feae131SDavid Howells * for the first part or the tail. 13081da177e4SLinus Torvalds */ 13099760ebffSLiam R. Howlett int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 13109760ebffSLiam R. Howlett unsigned long addr, int new_below) 13111da177e4SLinus Torvalds { 13128feae131SDavid Howells struct vm_area_struct *new; 13138feae131SDavid Howells struct vm_region *region; 13148feae131SDavid Howells unsigned long npages; 13159760ebffSLiam R. Howlett struct mm_struct *mm; 13161da177e4SLinus Torvalds 1317779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1318779c1023SDavid Howells * only a single usage on the region) */ 1319779c1023SDavid Howells if (vma->vm_file) 13208feae131SDavid Howells return -ENOMEM; 13211da177e4SLinus Torvalds 13229760ebffSLiam R. Howlett mm = vma->vm_mm; 13238feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 13248feae131SDavid Howells return -ENOMEM; 13251da177e4SLinus Torvalds 13268feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 13278feae131SDavid Howells if (!region) 13288feae131SDavid Howells return -ENOMEM; 13298feae131SDavid Howells 13303928d4f5SLinus Torvalds new = vm_area_dup(vma); 13318220543dSMatthew Wilcox (Oracle) if (!new) 13328220543dSMatthew Wilcox (Oracle) goto err_vma_dup; 13338220543dSMatthew Wilcox (Oracle) 133447d9644dSLiam R. Howlett if (vma_iter_prealloc(vmi)) { 13358220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 13368220543dSMatthew Wilcox (Oracle) current->pid); 133747d9644dSLiam R. Howlett goto err_vmi_preallocate; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13408feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 13418feae131SDavid Howells *region = *vma->vm_region; 13428feae131SDavid Howells new->vm_region = region; 13438feae131SDavid Howells 13448feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 13458feae131SDavid Howells 13468feae131SDavid Howells if (new_below) { 1347dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 13488feae131SDavid Howells } else { 13498feae131SDavid Howells region->vm_start = new->vm_start = addr; 13508feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 13511da177e4SLinus Torvalds } 13528feae131SDavid Howells 13538feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 13548feae131SDavid Howells new->vm_ops->open(new); 13558feae131SDavid Howells 13568feae131SDavid Howells down_write(&nommu_region_sem); 13578feae131SDavid Howells delete_nommu_region(vma->vm_region); 13588feae131SDavid Howells if (new_below) { 13598feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 13608feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 13618feae131SDavid Howells } else { 13628feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1363dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 13648feae131SDavid Howells } 13658feae131SDavid Howells add_nommu_region(vma->vm_region); 13668feae131SDavid Howells add_nommu_region(new->vm_region); 13678feae131SDavid Howells up_write(&nommu_region_sem); 13688220543dSMatthew Wilcox (Oracle) 13698220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(vma, mm); 13708220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(new, mm); 137147d9644dSLiam R. Howlett vma_iter_store(vmi, new); 1372fd9edbdbSLiam Howlett mm->map_count++; 13738feae131SDavid Howells return 0; 13748220543dSMatthew Wilcox (Oracle) 137547d9644dSLiam R. Howlett err_vmi_preallocate: 13768220543dSMatthew Wilcox (Oracle) vm_area_free(new); 13778220543dSMatthew Wilcox (Oracle) err_vma_dup: 13788220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 13798220543dSMatthew Wilcox (Oracle) return -ENOMEM; 13808feae131SDavid Howells } 13818feae131SDavid Howells 13828feae131SDavid Howells /* 13838feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 13848feae131SDavid Howells * the end 13858feae131SDavid Howells */ 138607f1bc5aSLiam R. Howlett static int vmi_shrink_vma(struct vma_iterator *vmi, 13878feae131SDavid Howells struct vm_area_struct *vma, 13888feae131SDavid Howells unsigned long from, unsigned long to) 13898feae131SDavid Howells { 13908feae131SDavid Howells struct vm_region *region; 13918feae131SDavid Howells 13928feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 13938feae131SDavid Howells * and list */ 139407f1bc5aSLiam R. Howlett if (vma_iter_prealloc(vmi)) { 139507f1bc5aSLiam R. Howlett pr_warn("Allocation of vma tree for process %d failed\n", 139607f1bc5aSLiam R. Howlett current->pid); 13978220543dSMatthew Wilcox (Oracle) return -ENOMEM; 139807f1bc5aSLiam R. Howlett } 139907f1bc5aSLiam R. Howlett 140007f1bc5aSLiam R. Howlett if (from > vma->vm_start) { 140107f1bc5aSLiam R. Howlett vma_iter_clear(vmi, from, vma->vm_end); 14028feae131SDavid Howells vma->vm_end = from; 140307f1bc5aSLiam R. Howlett } else { 140407f1bc5aSLiam R. Howlett vma_iter_clear(vmi, vma->vm_start, to); 14058feae131SDavid Howells vma->vm_start = to; 140607f1bc5aSLiam R. Howlett } 14078feae131SDavid Howells 14088feae131SDavid Howells /* cut the backing region down to size */ 14098feae131SDavid Howells region = vma->vm_region; 14101e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 14118feae131SDavid Howells 14128feae131SDavid Howells down_write(&nommu_region_sem); 14138feae131SDavid Howells delete_nommu_region(region); 1414dd8632a1SPaul Mundt if (from > region->vm_start) { 1415dd8632a1SPaul Mundt to = region->vm_top; 1416dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1417dd8632a1SPaul Mundt } else { 14188feae131SDavid Howells region->vm_start = to; 1419dd8632a1SPaul Mundt } 14208feae131SDavid Howells add_nommu_region(region); 14218feae131SDavid Howells up_write(&nommu_region_sem); 14228feae131SDavid Howells 14238feae131SDavid Howells free_page_series(from, to); 14248feae131SDavid Howells return 0; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14273034097aSDavid Howells /* 14283034097aSDavid Howells * release a mapping 14298feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 14308feae131SDavid Howells * VMA, though it need not cover the whole VMA 14313034097aSDavid Howells */ 1432897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) 14331da177e4SLinus Torvalds { 143447d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 14358feae131SDavid Howells struct vm_area_struct *vma; 1436f67d9b15SBob Liu unsigned long end; 14378220543dSMatthew Wilcox (Oracle) int ret = 0; 14381da177e4SLinus Torvalds 1439f67d9b15SBob Liu len = PAGE_ALIGN(len); 14408feae131SDavid Howells if (len == 0) 14411da177e4SLinus Torvalds return -EINVAL; 14421da177e4SLinus Torvalds 1443f67d9b15SBob Liu end = start + len; 1444f67d9b15SBob Liu 14458feae131SDavid Howells /* find the first potentially overlapping VMA */ 144647d9644dSLiam R. Howlett vma = vma_find(&vmi, end); 14478feae131SDavid Howells if (!vma) { 1448ac714904SChoi Gi-yong static int limit; 144933e5d769SDavid Howells if (limit < 5) { 145022cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 145133e5d769SDavid Howells current->pid, current->comm, 145233e5d769SDavid Howells start, start + len - 1); 145333e5d769SDavid Howells limit++; 145433e5d769SDavid Howells } 14558feae131SDavid Howells return -EINVAL; 14568feae131SDavid Howells } 14571da177e4SLinus Torvalds 14588feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 14598feae131SDavid Howells if (vma->vm_file) { 14608feae131SDavid Howells do { 146122cc877bSLeon Romanovsky if (start > vma->vm_start) 14628feae131SDavid Howells return -EINVAL; 14638feae131SDavid Howells if (end == vma->vm_end) 14648feae131SDavid Howells goto erase_whole_vma; 146547d9644dSLiam R. Howlett vma = vma_find(&vmi, end); 1466d75a310cSNamhyung Kim } while (vma); 14678feae131SDavid Howells return -EINVAL; 14688feae131SDavid Howells } else { 14698feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 14708feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 14718feae131SDavid Howells goto erase_whole_vma; 147222cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 14738feae131SDavid Howells return -EINVAL; 14741824cb75SAlexander Kuleshov if (offset_in_page(start)) 14758feae131SDavid Howells return -EINVAL; 14761824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end)) 14778feae131SDavid Howells return -EINVAL; 14788feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 14799760ebffSLiam R. Howlett ret = split_vma(&vmi, vma, start, 1); 148022cc877bSLeon Romanovsky if (ret < 0) 14818feae131SDavid Howells return ret; 14828feae131SDavid Howells } 148307f1bc5aSLiam R. Howlett return vmi_shrink_vma(&vmi, vma, start, end); 14848feae131SDavid Howells } 14851da177e4SLinus Torvalds 14868feae131SDavid Howells erase_whole_vma: 14878220543dSMatthew Wilcox (Oracle) if (delete_vma_from_mm(vma)) 14888220543dSMatthew Wilcox (Oracle) ret = -ENOMEM; 148980be727eSLiam Howlett else 14908feae131SDavid Howells delete_vma(mm, vma); 14918220543dSMatthew Wilcox (Oracle) return ret; 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 1494bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 14953034097aSDavid Howells { 1496bfce281cSAl Viro struct mm_struct *mm = current->mm; 14973034097aSDavid Howells int ret; 14983034097aSDavid Howells 1499d8ed45c5SMichel Lespinasse mmap_write_lock(mm); 1500897ab3e0SMike Rapoport ret = do_munmap(mm, addr, len, NULL); 1501d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 15023034097aSDavid Howells return ret; 15033034097aSDavid Howells } 1504a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1505a46ef99dSLinus Torvalds 1506a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1507a46ef99dSLinus Torvalds { 1508bfce281cSAl Viro return vm_munmap(addr, len); 1509a46ef99dSLinus Torvalds } 15103034097aSDavid Howells 15113034097aSDavid Howells /* 15128feae131SDavid Howells * release all the mappings made in a process's VM space 15133034097aSDavid Howells */ 15141da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 15151da177e4SLinus Torvalds { 15168220543dSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0); 15178feae131SDavid Howells struct vm_area_struct *vma; 15181da177e4SLinus Torvalds 15198feae131SDavid Howells if (!mm) 15208feae131SDavid Howells return; 15218feae131SDavid Howells 15221da177e4SLinus Torvalds mm->total_vm = 0; 15231da177e4SLinus Torvalds 15248220543dSMatthew Wilcox (Oracle) /* 15258220543dSMatthew Wilcox (Oracle) * Lock the mm to avoid assert complaining even though this is the only 15268220543dSMatthew Wilcox (Oracle) * user of the mm 15278220543dSMatthew Wilcox (Oracle) */ 15288220543dSMatthew Wilcox (Oracle) mmap_write_lock(mm); 15298220543dSMatthew Wilcox (Oracle) for_each_vma(vmi, vma) { 15308220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 15318feae131SDavid Howells delete_vma(mm, vma); 153204c34961SSteven J. Magnani cond_resched(); 15331da177e4SLinus Torvalds } 1534524e00b3SLiam R. Howlett __mt_destroy(&mm->mm_mt); 15358220543dSMatthew Wilcox (Oracle) mmap_write_unlock(mm); 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 15385d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len) 15391da177e4SLinus Torvalds { 15401da177e4SLinus Torvalds return -ENOMEM; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds /* 15446fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 15456fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 15461da177e4SLinus Torvalds * 15476fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 15488feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 15498feae131SDavid Howells * block is not shareable 15501da177e4SLinus Torvalds * 15516fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 15521da177e4SLinus Torvalds */ 15534b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 15541da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 15551da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 15561da177e4SLinus Torvalds { 15576fa5f80bSDavid Howells struct vm_area_struct *vma; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds /* insanity checks first */ 1560f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1561f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 15628feae131SDavid Howells if (old_len == 0 || new_len == 0) 15631da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15641da177e4SLinus Torvalds 15651824cb75SAlexander Kuleshov if (offset_in_page(addr)) 15668feae131SDavid Howells return -EINVAL; 15678feae131SDavid Howells 15681da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 15691da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15701da177e4SLinus Torvalds 15718feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 15726fa5f80bSDavid Howells if (!vma) 15731da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15741da177e4SLinus Torvalds 15756fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 15761da177e4SLinus Torvalds return (unsigned long) -EFAULT; 15771da177e4SLinus Torvalds 1578fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vma->vm_flags)) 15791da177e4SLinus Torvalds return (unsigned long) -EPERM; 15801da177e4SLinus Torvalds 15818feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 15821da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* all checks complete - do it */ 15856fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 15866fa5f80bSDavid Howells return vma->vm_start; 15876fa5f80bSDavid Howells } 15886fa5f80bSDavid Howells 15896a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 15906a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 15916a6160a7SHeiko Carstens unsigned long, new_addr) 15926fa5f80bSDavid Howells { 15936fa5f80bSDavid Howells unsigned long ret; 15946fa5f80bSDavid Howells 1595d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 15966fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 1597d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 15986fa5f80bSDavid Howells return ret; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds 1601df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1602df06b37fSKeith Busch unsigned int foll_flags) 16031da177e4SLinus Torvalds { 16041da177e4SLinus Torvalds return NULL; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16078f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 16088f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 16091da177e4SLinus Torvalds { 16108f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 16118f3b1327SBob Liu return -EINVAL; 16128f3b1327SBob Liu 16131c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 161466aa2b4bSGreg Ungerer return 0; 16151da177e4SLinus Torvalds } 161622c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 16171da177e4SLinus Torvalds 16183c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 16193c0b9de6SLinus Torvalds { 16203c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 16213c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 16223c0b9de6SLinus Torvalds 16233c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 16243c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 16253c0b9de6SLinus Torvalds } 16263c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 16273c0b9de6SLinus Torvalds 1628f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1629f905bc44SPaul Mundt unsigned long pgoff) 1630f905bc44SPaul Mundt { 1631f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1632f905bc44SPaul Mundt 1633f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1634f905bc44SPaul Mundt return -EINVAL; 1635f905bc44SPaul Mundt 1636f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1637f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1638f905bc44SPaul Mundt 1639f905bc44SPaul Mundt return 0; 1640f905bc44SPaul Mundt } 1641f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1642f905bc44SPaul Mundt 16432bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf) 1644b0e15190SDavid Howells { 1645b0e15190SDavid Howells BUG(); 1646d0217ac0SNick Piggin return 0; 1647b0e15190SDavid Howells } 1648b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 16490ec76a11SDavid Howells 16503f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf, 1651bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff) 1652f1820361SKirill A. Shutemov { 1653f1820361SKirill A. Shutemov BUG(); 16543f98a28cSGeert Uytterhoeven return 0; 1655f1820361SKirill A. Shutemov } 1656f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1657f1820361SKirill A. Shutemov 1658d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, 1659d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags) 16600ec76a11SDavid Howells { 16610ec76a11SDavid Howells struct vm_area_struct *vma; 1662442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 16630ec76a11SDavid Howells 1664d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 16651e426fe2SKonstantin Khlebnikov return 0; 16660ec76a11SDavid Howells 16670ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 16680159b141SDavid Howells vma = find_vma(mm, addr); 16690159b141SDavid Howells if (vma) { 16700ec76a11SDavid Howells /* don't overrun this mapping */ 16710ec76a11SDavid Howells if (addr + len >= vma->vm_end) 16720ec76a11SDavid Howells len = vma->vm_end - addr; 16730ec76a11SDavid Howells 16740ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1675d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 16767959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 16777959722bSJie Zhang (void *) addr, buf, len); 1678d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 16797959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 16807959722bSJie Zhang buf, (void *) addr, len); 16810ec76a11SDavid Howells else 16820ec76a11SDavid Howells len = 0; 16830ec76a11SDavid Howells } else { 16840ec76a11SDavid Howells len = 0; 16850ec76a11SDavid Howells } 16860ec76a11SDavid Howells 1687d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 1688f55f199bSMike Frysinger 1689f55f199bSMike Frysinger return len; 1690f55f199bSMike Frysinger } 1691f55f199bSMike Frysinger 1692f55f199bSMike Frysinger /** 1693b7701a5fSMike Rapoport * access_remote_vm - access another process' address space 1694f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1695f55f199bSMike Frysinger * @addr: start address to access 1696f55f199bSMike Frysinger * @buf: source or destination buffer 1697f55f199bSMike Frysinger * @len: number of bytes to transfer 16986347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 1699f55f199bSMike Frysinger * 1700f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1701f55f199bSMike Frysinger */ 1702f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 17036347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 1704f55f199bSMike Frysinger { 1705d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 1706f55f199bSMike Frysinger } 1707f55f199bSMike Frysinger 1708f55f199bSMike Frysinger /* 1709f55f199bSMike Frysinger * Access another process' address space. 1710f55f199bSMike Frysinger * - source/target buffer must be kernel space 1711f55f199bSMike Frysinger */ 1712f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, 1713f307ab6dSLorenzo Stoakes unsigned int gup_flags) 1714f55f199bSMike Frysinger { 1715f55f199bSMike Frysinger struct mm_struct *mm; 1716f55f199bSMike Frysinger 1717f55f199bSMike Frysinger if (addr + len < addr) 1718f55f199bSMike Frysinger return 0; 1719f55f199bSMike Frysinger 1720f55f199bSMike Frysinger mm = get_task_mm(tsk); 1721f55f199bSMike Frysinger if (!mm) 1722f55f199bSMike Frysinger return 0; 1723f55f199bSMike Frysinger 1724d3f5ffcaSJohn Hubbard len = __access_remote_vm(mm, addr, buf, len, gup_flags); 1725f55f199bSMike Frysinger 17260ec76a11SDavid Howells mmput(mm); 17270ec76a11SDavid Howells return len; 17280ec76a11SDavid Howells } 1729fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 17307e660872SDavid Howells 17317e660872SDavid Howells /** 17327e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 17337e660872SDavid Howells * @inode: The inode to check 17347e660872SDavid Howells * @size: The current filesize of the inode 17357e660872SDavid Howells * @newsize: The proposed filesize of the inode 17367e660872SDavid Howells * 17377e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 1738c08b342cSRandy Dunlap * make sure that any outstanding VMAs aren't broken and then shrink the 1739c08b342cSRandy Dunlap * vm_regions that extend beyond so that do_mmap() doesn't 17407e660872SDavid Howells * automatically grant mappings that are too large. 17417e660872SDavid Howells */ 17427e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 17437e660872SDavid Howells size_t newsize) 17447e660872SDavid Howells { 17457e660872SDavid Howells struct vm_area_struct *vma; 17467e660872SDavid Howells struct vm_region *region; 17477e660872SDavid Howells pgoff_t low, high; 17487e660872SDavid Howells size_t r_size, r_top; 17497e660872SDavid Howells 17507e660872SDavid Howells low = newsize >> PAGE_SHIFT; 17517e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 17527e660872SDavid Howells 17537e660872SDavid Howells down_write(&nommu_region_sem); 17541acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 17557e660872SDavid Howells 17567e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 17576b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 17587e660872SDavid Howells /* found one - only interested if it's shared out of the page 17597e660872SDavid Howells * cache */ 17607e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 17611acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17627e660872SDavid Howells up_write(&nommu_region_sem); 17637e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 17647e660872SDavid Howells } 17657e660872SDavid Howells } 17667e660872SDavid Howells 17677e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 17687e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 17697e660872SDavid Howells * 17707e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 17717e660872SDavid Howells * shouldn't be any 17727e660872SDavid Howells */ 17731acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 17747e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 17757e660872SDavid Howells continue; 17767e660872SDavid Howells 17777e660872SDavid Howells region = vma->vm_region; 17787e660872SDavid Howells r_size = region->vm_top - region->vm_start; 17797e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 17807e660872SDavid Howells 17817e660872SDavid Howells if (r_top > newsize) { 17827e660872SDavid Howells region->vm_top -= r_top - newsize; 17837e660872SDavid Howells if (region->vm_end > region->vm_top) 17847e660872SDavid Howells region->vm_end = region->vm_top; 17857e660872SDavid Howells } 17867e660872SDavid Howells } 17877e660872SDavid Howells 17881acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17897e660872SDavid Howells up_write(&nommu_region_sem); 17907e660872SDavid Howells return 0; 17917e660872SDavid Howells } 1792c9b1d098SAndrew Shewmaker 1793c9b1d098SAndrew Shewmaker /* 1794c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 1795c9b1d098SAndrew Shewmaker * 1796c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 1797c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 1798c9b1d098SAndrew Shewmaker * mode. 1799c9b1d098SAndrew Shewmaker * 1800c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 1801c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 1802c9b1d098SAndrew Shewmaker */ 1803c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 1804c9b1d098SAndrew Shewmaker { 1805c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 1806c9b1d098SAndrew Shewmaker 1807c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 1808c9b1d098SAndrew Shewmaker 1809c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 1810c9b1d098SAndrew Shewmaker return 0; 1811c9b1d098SAndrew Shewmaker } 1812a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve); 18134eeab4f5SAndrew Shewmaker 18144eeab4f5SAndrew Shewmaker /* 18154eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 18164eeab4f5SAndrew Shewmaker * 18174eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 18184eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 18194eeab4f5SAndrew Shewmaker * 18204eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 18214eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 18224eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 18234eeab4f5SAndrew Shewmaker */ 18244eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 18254eeab4f5SAndrew Shewmaker { 18264eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 18274eeab4f5SAndrew Shewmaker 1828c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 18294eeab4f5SAndrew Shewmaker 18304eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 18314eeab4f5SAndrew Shewmaker return 0; 18324eeab4f5SAndrew Shewmaker } 1833a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve); 1834