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 /* 634d85a143bSLinus Torvalds * At least xtensa ends up having protection faults even with no 635d85a143bSLinus Torvalds * MMU.. No stack expansion, at least. 636d85a143bSLinus Torvalds */ 637d85a143bSLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 638d85a143bSLinus Torvalds unsigned long addr, struct pt_regs *regs) 639d85a143bSLinus Torvalds { 64003f88937SMax Filippov struct vm_area_struct *vma; 64103f88937SMax Filippov 642d85a143bSLinus Torvalds mmap_read_lock(mm); 64303f88937SMax Filippov vma = vma_lookup(mm, addr); 64403f88937SMax Filippov if (!vma) 64503f88937SMax Filippov mmap_read_unlock(mm); 64603f88937SMax Filippov return vma; 647d85a143bSLinus Torvalds } 648d85a143bSLinus Torvalds 649d85a143bSLinus Torvalds /* 6508feae131SDavid Howells * expand a stack to a given address 6518feae131SDavid Howells * - not supported under NOMMU conditions 6528feae131SDavid Howells */ 6538d7071afSLinus Torvalds int expand_stack_locked(struct vm_area_struct *vma, unsigned long addr) 65457c8f63eSGreg Ungerer { 65557c8f63eSGreg Ungerer return -ENOMEM; 65657c8f63eSGreg Ungerer } 65757c8f63eSGreg Ungerer 6588d7071afSLinus Torvalds struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr) 6598d7071afSLinus Torvalds { 6608d7071afSLinus Torvalds mmap_read_unlock(mm); 6618d7071afSLinus Torvalds return NULL; 6628d7071afSLinus Torvalds } 6638d7071afSLinus Torvalds 664930e652aSDavid Howells /* 6656fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 666c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6676fa5f80bSDavid Howells */ 6688feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 6698feae131SDavid Howells unsigned long addr, 6708feae131SDavid Howells unsigned long len) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct vm_area_struct *vma; 6738feae131SDavid Howells unsigned long end = addr + len; 67447d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 6751da177e4SLinus Torvalds 67647d9644dSLiam R. Howlett vma = vma_iter_load(&vmi); 677524e00b3SLiam R. Howlett if (!vma) 6788feae131SDavid Howells return NULL; 679524e00b3SLiam R. Howlett if (vma->vm_start != addr) 680524e00b3SLiam R. Howlett return NULL; 681524e00b3SLiam R. Howlett if (vma->vm_end != end) 682524e00b3SLiam R. Howlett return NULL; 683524e00b3SLiam R. Howlett 6848feae131SDavid Howells return vma; 6858feae131SDavid Howells } 6861da177e4SLinus Torvalds 6873034097aSDavid Howells /* 6881da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 6891da177e4SLinus Torvalds * mapping we're capable of supporting 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 6921da177e4SLinus Torvalds unsigned long addr, 6931da177e4SLinus Torvalds unsigned long len, 6941da177e4SLinus Torvalds unsigned long prot, 6951da177e4SLinus Torvalds unsigned long flags, 6961da177e4SLinus Torvalds unsigned long pgoff, 6971da177e4SLinus Torvalds unsigned long *_capabilities) 6981da177e4SLinus Torvalds { 6998feae131SDavid Howells unsigned long capabilities, rlen; 7001da177e4SLinus Torvalds int ret; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* do the simple checks first */ 70322cc877bSLeon Romanovsky if (flags & MAP_FIXED) 7041da177e4SLinus Torvalds return -EINVAL; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 7071da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 7081da177e4SLinus Torvalds return -EINVAL; 7091da177e4SLinus Torvalds 710f81cff0dSMike Frysinger if (!len) 7111da177e4SLinus Torvalds return -EINVAL; 7121da177e4SLinus Torvalds 713f81cff0dSMike Frysinger /* Careful about overflows.. */ 7148feae131SDavid Howells rlen = PAGE_ALIGN(len); 7158feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 716f81cff0dSMike Frysinger return -ENOMEM; 717f81cff0dSMike Frysinger 7181da177e4SLinus Torvalds /* offset overflow? */ 7198feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 720f81cff0dSMike Frysinger return -EOVERFLOW; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds if (file) { 7231da177e4SLinus Torvalds /* files must support mmap */ 72472c2d531SAl Viro if (!file->f_op->mmap) 7251da177e4SLinus Torvalds return -ENODEV; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 7281da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 7291da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 7301da177e4SLinus Torvalds */ 731b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 732b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 733b4caecd4SChristoph Hellwig } else { 7341da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 7351da177e4SLinus Torvalds * defaults */ 736496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 7371da177e4SLinus Torvalds case S_IFREG: 7381da177e4SLinus Torvalds case S_IFBLK: 739b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds case S_IFCHR: 7431da177e4SLinus Torvalds capabilities = 744b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 745b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 746b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 7471da177e4SLinus Torvalds break; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds default: 7501da177e4SLinus Torvalds return -EINVAL; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 7551da177e4SLinus Torvalds * device */ 7561da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 757b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7586e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 759b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 7601da177e4SLinus Torvalds 76128d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 76228d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 76328d7a6aeSGraff Yang return -EACCES; 76428d7a6aeSGraff Yang 7651da177e4SLinus Torvalds if (flags & MAP_SHARED) { 7661da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 7671da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 7681da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 7691da177e4SLinus Torvalds return -EACCES; 7701da177e4SLinus Torvalds 771496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 7721da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 7731da177e4SLinus Torvalds return -EACCES; 7741da177e4SLinus Torvalds 775b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 7761da177e4SLinus Torvalds return -ENODEV; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 779b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 780ac714904SChoi Gi-yong } else { 7811da177e4SLinus Torvalds /* we're going to read the file into private memory we 7821da177e4SLinus Torvalds * allocate */ 783b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 7841da177e4SLinus Torvalds return -ENODEV; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 7871da177e4SLinus Torvalds * shared with the backing device */ 7881da177e4SLinus Torvalds if (prot & PROT_WRITE) 789b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 792b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 793b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 794b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 795b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 7963c7b2045SBernd Schmidt ) { 797b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7983c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 79922cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 8003c7b2045SBernd Schmidt return -EINVAL; 8013c7b2045SBernd Schmidt } 8023c7b2045SBernd Schmidt } 8033c7b2045SBernd Schmidt } 8043c7b2045SBernd Schmidt 8051da177e4SLinus Torvalds /* handle executable mappings and implied executable 8061da177e4SLinus Torvalds * mappings */ 80790f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 8081da177e4SLinus Torvalds if (prot & PROT_EXEC) 8091da177e4SLinus Torvalds return -EPERM; 810ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 8111da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 8121da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 813b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 8141da177e4SLinus Torvalds prot |= PROT_EXEC; 8151da177e4SLinus Torvalds } 816ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 8171da177e4SLinus Torvalds (prot & PROT_EXEC) && 818b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 8191da177e4SLinus Torvalds ) { 8201da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 821b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8221da177e4SLinus Torvalds } 823ac714904SChoi Gi-yong } else { 8241da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 8251da177e4SLinus Torvalds * privately mapped 8261da177e4SLinus Torvalds */ 827b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 8301da177e4SLinus Torvalds if ((prot & PROT_READ) && 8311da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 8321da177e4SLinus Torvalds prot |= PROT_EXEC; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* allow the security API to have its say */ 836e5467859SAl Viro ret = security_mmap_addr(addr); 837e5467859SAl Viro if (ret < 0) 838e5467859SAl Viro return ret; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* looks okay */ 8411da177e4SLinus Torvalds *_capabilities = capabilities; 8421da177e4SLinus Torvalds return 0; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds /* 8461da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 8471da177e4SLinus Torvalds * now know into VMA flags 8481da177e4SLinus Torvalds */ 8491da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 8501da177e4SLinus Torvalds unsigned long prot, 8511da177e4SLinus Torvalds unsigned long flags, 8521da177e4SLinus Torvalds unsigned long capabilities) 8531da177e4SLinus Torvalds { 8541da177e4SLinus Torvalds unsigned long vm_flags; 8551da177e4SLinus Torvalds 856e6bfb709SDave Hansen vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); 8571da177e4SLinus Torvalds 858b6b7a8faSDavid Hildenbrand if (!file) { 859b6b7a8faSDavid Hildenbrand /* 860b6b7a8faSDavid Hildenbrand * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because 861b6b7a8faSDavid Hildenbrand * there is no fork(). 8621da177e4SLinus Torvalds */ 863b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 864b6b7a8faSDavid Hildenbrand } else if (flags & MAP_PRIVATE) { 865b6b7a8faSDavid Hildenbrand /* MAP_PRIVATE file mapping */ 866b6b7a8faSDavid Hildenbrand if (capabilities & NOMMU_MAP_DIRECT) 867b6b7a8faSDavid Hildenbrand vm_flags |= (capabilities & NOMMU_VMFLAGS); 868b6b7a8faSDavid Hildenbrand else 869b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 870b6b7a8faSDavid Hildenbrand 871b6b7a8faSDavid Hildenbrand if (!(prot & PROT_WRITE) && !current->ptrace) 872b6b7a8faSDavid Hildenbrand /* 873b6b7a8faSDavid Hildenbrand * R/O private file mapping which cannot be used to 874b6b7a8faSDavid Hildenbrand * modify memory, especially also not via active ptrace 875b6b7a8faSDavid Hildenbrand * (e.g., set breakpoints) or later by upgrading 876b6b7a8faSDavid Hildenbrand * permissions (no mprotect()). We can try overlaying 877b6b7a8faSDavid Hildenbrand * the file mapping, which will work e.g., on chardevs, 878b6b7a8faSDavid Hildenbrand * ramfs/tmpfs/shmfs and romfs/cramf. 879b6b7a8faSDavid Hildenbrand */ 880b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYOVERLAY; 881b6b7a8faSDavid Hildenbrand } else { 882b6b7a8faSDavid Hildenbrand /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */ 883b6b7a8faSDavid Hildenbrand vm_flags |= VM_SHARED | VM_MAYSHARE | 884b6b7a8faSDavid Hildenbrand (capabilities & NOMMU_VMFLAGS); 885b6b7a8faSDavid Hildenbrand } 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds return vm_flags; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* 8918feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 8928feae131SDavid Howells * pins the storage) 8931da177e4SLinus Torvalds */ 8948feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 8951da177e4SLinus Torvalds { 8961da177e4SLinus Torvalds int ret; 8971da177e4SLinus Torvalds 898f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 899dd8632a1SPaul Mundt if (ret == 0) { 900dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 901645d83c5SDavid Howells return 0; 902dd8632a1SPaul Mundt } 9031da177e4SLinus Torvalds if (ret != -ENOSYS) 9041da177e4SLinus Torvalds return ret; 9051da177e4SLinus Torvalds 9063fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 9073fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 9083fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 9091da177e4SLinus Torvalds return -ENODEV; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* 9131da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 9141da177e4SLinus Torvalds */ 9158feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 9168feae131SDavid Howells struct vm_region *region, 917645d83c5SDavid Howells unsigned long len, 918645d83c5SDavid Howells unsigned long capabilities) 9191da177e4SLinus Torvalds { 920dbc8358cSJoonsoo Kim unsigned long total, point; 9211da177e4SLinus Torvalds void *base; 9228feae131SDavid Howells int ret, order; 9231da177e4SLinus Torvalds 924b6b7a8faSDavid Hildenbrand /* 925b6b7a8faSDavid Hildenbrand * Invoke the file's mapping function so that it can keep track of 926b6b7a8faSDavid Hildenbrand * shared mappings on devices or memory. VM_MAYOVERLAY will be set if 927b6b7a8faSDavid Hildenbrand * it may attempt to share, which will make is_nommu_shared_mapping() 928b6b7a8faSDavid Hildenbrand * happy. 9291da177e4SLinus Torvalds */ 930b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 931f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 9321da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 933fc4f4be9SDavid Hildenbrand if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags))) 934fc4f4be9SDavid Hildenbrand ret = -ENOSYS; 935fc4f4be9SDavid Hildenbrand if (ret == 0) { 936dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 937645d83c5SDavid Howells return 0; 9381da177e4SLinus Torvalds } 939dd8632a1SPaul Mundt if (ret != -ENOSYS) 940dd8632a1SPaul Mundt return ret; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 9431da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 9441da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9478feae131SDavid Howells 9481da177e4SLinus Torvalds /* allocate some memory to hold the mapping 9491da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 9501da177e4SLinus Torvalds * we're allocating is smaller than a page 9511da177e4SLinus Torvalds */ 952f67d9b15SBob Liu order = get_order(len); 9538feae131SDavid Howells total = 1 << order; 954f67d9b15SBob Liu point = len >> PAGE_SHIFT; 955dd8632a1SPaul Mundt 956dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 95722cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 958dbc8358cSJoonsoo Kim total = point; 9598feae131SDavid Howells 960da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 961dbc8358cSJoonsoo Kim if (!base) 962dbc8358cSJoonsoo Kim goto enomem; 9638feae131SDavid Howells 964dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 965dbc8358cSJoonsoo Kim 9661c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY); 9671c71222eSSuren Baghdasaryan region->vm_flags = vma->vm_flags; 9688feae131SDavid Howells region->vm_start = (unsigned long) base; 969f67d9b15SBob Liu region->vm_end = region->vm_start + len; 970dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 9718feae131SDavid Howells 9728feae131SDavid Howells vma->vm_start = region->vm_start; 9738feae131SDavid Howells vma->vm_end = region->vm_start + len; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds if (vma->vm_file) { 9761da177e4SLinus Torvalds /* read the contents of a file into the copy */ 9771da177e4SLinus Torvalds loff_t fpos; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds fpos = vma->vm_pgoff; 9801da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 9811da177e4SLinus Torvalds 982b4bf802aSChristoph Hellwig ret = kernel_read(vma->vm_file, base, len, &fpos); 9831da177e4SLinus Torvalds if (ret < 0) 9841da177e4SLinus Torvalds goto error_free; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds /* clear the last little bit */ 987f67d9b15SBob Liu if (ret < len) 988f67d9b15SBob Liu memset(base + ret, 0, len - ret); 9891da177e4SLinus Torvalds 990bfd40eafSKirill A. Shutemov } else { 991bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds return 0; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds error_free: 9977223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 9988feae131SDavid Howells region->vm_start = vma->vm_start = 0; 9998feae131SDavid Howells region->vm_end = vma->vm_end = 0; 1000dd8632a1SPaul Mundt region->vm_top = 0; 10011da177e4SLinus Torvalds return ret; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds enomem: 1004b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 100505ae6fa3SGreg Ungerer len, current->pid, current->comm); 1006*1279aa06SKefeng Wang show_mem(); 10071da177e4SLinus Torvalds return -ENOMEM; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds /* 10111da177e4SLinus Torvalds * handle mapping creation for uClinux 10121da177e4SLinus Torvalds */ 10131fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, 10141da177e4SLinus Torvalds unsigned long addr, 10151da177e4SLinus Torvalds unsigned long len, 10161da177e4SLinus Torvalds unsigned long prot, 10171da177e4SLinus Torvalds unsigned long flags, 1018bebeb3d6SMichel Lespinasse unsigned long pgoff, 1019897ab3e0SMike Rapoport unsigned long *populate, 1020897ab3e0SMike Rapoport struct list_head *uf) 10211da177e4SLinus Torvalds { 10228feae131SDavid Howells struct vm_area_struct *vma; 10238feae131SDavid Howells struct vm_region *region; 10241da177e4SLinus Torvalds struct rb_node *rb; 102545e55300SPeter Collingbourne vm_flags_t vm_flags; 10261fcfd8dbSOleg Nesterov unsigned long capabilities, result; 10271da177e4SLinus Torvalds int ret; 102847d9644dSLiam R. Howlett VMA_ITERATOR(vmi, current->mm, 0); 10291da177e4SLinus Torvalds 103041badc15SMichel Lespinasse *populate = 0; 1031bebeb3d6SMichel Lespinasse 10321da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 10331da177e4SLinus Torvalds * mapping */ 10341da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 10351da177e4SLinus Torvalds &capabilities); 103622cc877bSLeon Romanovsky if (ret < 0) 10371da177e4SLinus Torvalds return ret; 10381da177e4SLinus Torvalds 103906aab5a3SDavid Howells /* we ignore the address hint */ 104006aab5a3SDavid Howells addr = 0; 1041f67d9b15SBob Liu len = PAGE_ALIGN(len); 104206aab5a3SDavid Howells 10431da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 10441da177e4SLinus Torvalds * now know into VMA flags */ 104545e55300SPeter Collingbourne vm_flags = determine_vm_flags(file, prot, flags, capabilities); 10461da177e4SLinus Torvalds 10478220543dSMatthew Wilcox (Oracle) 10488feae131SDavid Howells /* we're going to need to record the mapping */ 10498feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 10508feae131SDavid Howells if (!region) 10518feae131SDavid Howells goto error_getting_region; 10521da177e4SLinus Torvalds 1053490fc053SLinus Torvalds vma = vm_area_alloc(current->mm); 10548feae131SDavid Howells if (!vma) 10558feae131SDavid Howells goto error_getting_vma; 10561da177e4SLinus Torvalds 105747d9644dSLiam R. Howlett if (vma_iter_prealloc(&vmi)) 105847d9644dSLiam R. Howlett goto error_vma_iter_prealloc; 10598220543dSMatthew Wilcox (Oracle) 10601e2ae599SDavid Howells region->vm_usage = 1; 10618feae131SDavid Howells region->vm_flags = vm_flags; 10628feae131SDavid Howells region->vm_pgoff = pgoff; 10638feae131SDavid Howells 10641c71222eSSuren Baghdasaryan vm_flags_init(vma, vm_flags); 10658feae131SDavid Howells vma->vm_pgoff = pgoff; 10668feae131SDavid Howells 10678feae131SDavid Howells if (file) { 1068cb0942b8SAl Viro region->vm_file = get_file(file); 1069cb0942b8SAl Viro vma->vm_file = get_file(file); 10708feae131SDavid Howells } 10718feae131SDavid Howells 10728feae131SDavid Howells down_write(&nommu_region_sem); 10738feae131SDavid Howells 10748feae131SDavid Howells /* if we want to share, we need to check for regions created by other 10751da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 10768feae131SDavid Howells * - we can only share with a superset match on most regular files 10771da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 10781da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 10791da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 10801da177e4SLinus Torvalds * than here 10811da177e4SLinus Torvalds */ 1082fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vm_flags)) { 10838feae131SDavid Howells struct vm_region *pregion; 10848feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 10851da177e4SLinus Torvalds 10868feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 10878feae131SDavid Howells pgend = pgoff + pglen; 1088165b2392SDavid Howells 10898feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 10908feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 10911da177e4SLinus Torvalds 1092fc4f4be9SDavid Hildenbrand if (!is_nommu_shared_mapping(pregion->vm_flags)) 10931da177e4SLinus Torvalds continue; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1096496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1097496ad9aaSAl Viro file_inode(file)) 10981da177e4SLinus Torvalds continue; 10991da177e4SLinus Torvalds 11008feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 11011da177e4SLinus Torvalds continue; 11021da177e4SLinus Torvalds 11038feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 11048feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 11058feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 11068feae131SDavid Howells if (pgoff >= rpgend) 11071da177e4SLinus Torvalds continue; 11081da177e4SLinus Torvalds 11098feae131SDavid Howells /* handle inexactly overlapping matches between 11108feae131SDavid Howells * mappings */ 11118feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 11128feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 11138feae131SDavid Howells /* new mapping is not a subset of the region */ 1114b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 11151da177e4SLinus Torvalds goto sharing_violation; 11161da177e4SLinus Torvalds continue; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11198feae131SDavid Howells /* we've found a region we can share */ 11201e2ae599SDavid Howells pregion->vm_usage++; 11218feae131SDavid Howells vma->vm_region = pregion; 11228feae131SDavid Howells start = pregion->vm_start; 11238feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 11248feae131SDavid Howells vma->vm_start = start; 11258feae131SDavid Howells vma->vm_end = start + len; 11261da177e4SLinus Torvalds 112722cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 11281c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY); 112922cc877bSLeon Romanovsky else { 11308feae131SDavid Howells ret = do_mmap_shared_file(vma); 11318feae131SDavid Howells if (ret < 0) { 11328feae131SDavid Howells vma->vm_region = NULL; 11338feae131SDavid Howells vma->vm_start = 0; 11348feae131SDavid Howells vma->vm_end = 0; 11351e2ae599SDavid Howells pregion->vm_usage--; 11368feae131SDavid Howells pregion = NULL; 11378feae131SDavid Howells goto error_just_free; 11381da177e4SLinus Torvalds } 11398feae131SDavid Howells } 11408feae131SDavid Howells fput(region->vm_file); 11418feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 11428feae131SDavid Howells region = pregion; 11438feae131SDavid Howells result = start; 11448feae131SDavid Howells goto share; 11458feae131SDavid Howells } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 11481da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 11491da177e4SLinus Torvalds * tell us the location of a shared mapping 11501da177e4SLinus Torvalds */ 1151b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11521da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 11531da177e4SLinus Torvalds pgoff, flags); 1154bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 11551da177e4SLinus Torvalds ret = addr; 1156bb005a59SNamhyung Kim if (ret != -ENOSYS) 11578feae131SDavid Howells goto error_just_free; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds /* the driver refused to tell us where to site 11601da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 11611da177e4SLinus Torvalds * it */ 1162bb005a59SNamhyung Kim ret = -ENODEV; 1163b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 11648feae131SDavid Howells goto error_just_free; 11651da177e4SLinus Torvalds 1166b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 11678feae131SDavid Howells } else { 11688feae131SDavid Howells vma->vm_start = region->vm_start = addr; 11698feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11748feae131SDavid Howells vma->vm_region = region; 11751da177e4SLinus Torvalds 1176645d83c5SDavid Howells /* set up the mapping 1177b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1178645d83c5SDavid Howells */ 11791da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 11808feae131SDavid Howells ret = do_mmap_shared_file(vma); 11811da177e4SLinus Torvalds else 1182645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 11831da177e4SLinus Torvalds if (ret < 0) 1184645d83c5SDavid Howells goto error_just_free; 1185645d83c5SDavid Howells add_nommu_region(region); 11868feae131SDavid Howells 1187ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 11880bf5f949SChristoph Hellwig if (!vma->vm_file && 11890bf5f949SChristoph Hellwig (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) || 11900bf5f949SChristoph Hellwig !(flags & MAP_UNINITIALIZED))) 1191ea637639SJie Zhang memset((void *)region->vm_start, 0, 1192ea637639SJie Zhang region->vm_end - region->vm_start); 1193ea637639SJie Zhang 11941da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 11958feae131SDavid Howells result = vma->vm_start; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 11981da177e4SLinus Torvalds 11998feae131SDavid Howells share: 120007f1bc5aSLiam R. Howlett BUG_ON(!vma->vm_region); 120107f1bc5aSLiam R. Howlett setup_vma_to_mm(vma, current->mm); 120207f1bc5aSLiam R. Howlett current->mm->map_count++; 120307f1bc5aSLiam R. Howlett /* add the VMA to the tree */ 120407f1bc5aSLiam R. Howlett vma_iter_store(&vmi, vma); 12051da177e4SLinus Torvalds 1206cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable 1207cfe79c00SMike Frysinger * mapping of it is made */ 1208cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1209a75a2df6SChristoph Hellwig flush_icache_user_range(region->vm_start, region->vm_end); 1210cfe79c00SMike Frysinger region->vm_icache_flushed = true; 1211cfe79c00SMike Frysinger } 12121da177e4SLinus Torvalds 1213cfe79c00SMike Frysinger up_write(&nommu_region_sem); 12141da177e4SLinus Torvalds 12158feae131SDavid Howells return result; 12161da177e4SLinus Torvalds 12178feae131SDavid Howells error_just_free: 12188feae131SDavid Howells up_write(&nommu_region_sem); 12198feae131SDavid Howells error: 122047d9644dSLiam R. Howlett vma_iter_free(&vmi); 122189a86402SDavid Howells if (region->vm_file) 12228feae131SDavid Howells fput(region->vm_file); 12238feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 122489a86402SDavid Howells if (vma->vm_file) 12258feae131SDavid Howells fput(vma->vm_file); 12263928d4f5SLinus Torvalds vm_area_free(vma); 12271da177e4SLinus Torvalds return ret; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds sharing_violation: 12308feae131SDavid Howells up_write(&nommu_region_sem); 123122cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n"); 12328feae131SDavid Howells ret = -EINVAL; 12338feae131SDavid Howells goto error; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds error_getting_vma: 12368feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 123722cc877bSLeon Romanovsky pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", 12381da177e4SLinus Torvalds len, current->pid); 1239*1279aa06SKefeng Wang show_mem(); 12401da177e4SLinus Torvalds return -ENOMEM; 12411da177e4SLinus Torvalds 12428feae131SDavid Howells error_getting_region: 124322cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", 12441da177e4SLinus Torvalds len, current->pid); 1245*1279aa06SKefeng Wang show_mem(); 12461da177e4SLinus Torvalds return -ENOMEM; 12478220543dSMatthew Wilcox (Oracle) 124847d9644dSLiam R. Howlett error_vma_iter_prealloc: 12498220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 12508220543dSMatthew Wilcox (Oracle) vm_area_free(vma); 12518220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", current->pid); 1252*1279aa06SKefeng Wang show_mem(); 12538220543dSMatthew Wilcox (Oracle) return -ENOMEM; 12548220543dSMatthew Wilcox (Oracle) 12551da177e4SLinus Torvalds } 12566be5ceb0SLinus Torvalds 1257a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1258a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1259a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 126066f0dc48SHugh Dickins { 126166f0dc48SHugh Dickins struct file *file = NULL; 126266f0dc48SHugh Dickins unsigned long retval = -EBADF; 126366f0dc48SHugh Dickins 1264120a795dSAl Viro audit_mmap_fd(fd, flags); 126566f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 126666f0dc48SHugh Dickins file = fget(fd); 126766f0dc48SHugh Dickins if (!file) 126866f0dc48SHugh Dickins goto out; 126966f0dc48SHugh Dickins } 127066f0dc48SHugh Dickins 1271ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 127266f0dc48SHugh Dickins 127366f0dc48SHugh Dickins if (file) 127466f0dc48SHugh Dickins fput(file); 127566f0dc48SHugh Dickins out: 127666f0dc48SHugh Dickins return retval; 127766f0dc48SHugh Dickins } 127866f0dc48SHugh Dickins 1279a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1280a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1281a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1282a90f590aSDominik Brodowski { 1283a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1284a90f590aSDominik Brodowski } 1285a90f590aSDominik Brodowski 1286a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1287a4679373SChristoph Hellwig struct mmap_arg_struct { 1288a4679373SChristoph Hellwig unsigned long addr; 1289a4679373SChristoph Hellwig unsigned long len; 1290a4679373SChristoph Hellwig unsigned long prot; 1291a4679373SChristoph Hellwig unsigned long flags; 1292a4679373SChristoph Hellwig unsigned long fd; 1293a4679373SChristoph Hellwig unsigned long offset; 1294a4679373SChristoph Hellwig }; 1295a4679373SChristoph Hellwig 1296a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1297a4679373SChristoph Hellwig { 1298a4679373SChristoph Hellwig struct mmap_arg_struct a; 1299a4679373SChristoph Hellwig 1300a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1301a4679373SChristoph Hellwig return -EFAULT; 13021824cb75SAlexander Kuleshov if (offset_in_page(a.offset)) 1303a4679373SChristoph Hellwig return -EINVAL; 1304a4679373SChristoph Hellwig 1305a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1306a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1307a4679373SChristoph Hellwig } 1308a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1309a4679373SChristoph Hellwig 13101da177e4SLinus Torvalds /* 13118feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 13128feae131SDavid Howells * for the first part or the tail. 13131da177e4SLinus Torvalds */ 13149760ebffSLiam R. Howlett int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 13159760ebffSLiam R. Howlett unsigned long addr, int new_below) 13161da177e4SLinus Torvalds { 13178feae131SDavid Howells struct vm_area_struct *new; 13188feae131SDavid Howells struct vm_region *region; 13198feae131SDavid Howells unsigned long npages; 13209760ebffSLiam R. Howlett struct mm_struct *mm; 13211da177e4SLinus Torvalds 1322779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1323779c1023SDavid Howells * only a single usage on the region) */ 1324779c1023SDavid Howells if (vma->vm_file) 13258feae131SDavid Howells return -ENOMEM; 13261da177e4SLinus Torvalds 13279760ebffSLiam R. Howlett mm = vma->vm_mm; 13288feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 13298feae131SDavid Howells return -ENOMEM; 13301da177e4SLinus Torvalds 13318feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 13328feae131SDavid Howells if (!region) 13338feae131SDavid Howells return -ENOMEM; 13348feae131SDavid Howells 13353928d4f5SLinus Torvalds new = vm_area_dup(vma); 13368220543dSMatthew Wilcox (Oracle) if (!new) 13378220543dSMatthew Wilcox (Oracle) goto err_vma_dup; 13388220543dSMatthew Wilcox (Oracle) 133947d9644dSLiam R. Howlett if (vma_iter_prealloc(vmi)) { 13408220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 13418220543dSMatthew Wilcox (Oracle) current->pid); 134247d9644dSLiam R. Howlett goto err_vmi_preallocate; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13458feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 13468feae131SDavid Howells *region = *vma->vm_region; 13478feae131SDavid Howells new->vm_region = region; 13488feae131SDavid Howells 13498feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 13508feae131SDavid Howells 13518feae131SDavid Howells if (new_below) { 1352dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 13538feae131SDavid Howells } else { 13548feae131SDavid Howells region->vm_start = new->vm_start = addr; 13558feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 13561da177e4SLinus Torvalds } 13578feae131SDavid Howells 13588feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 13598feae131SDavid Howells new->vm_ops->open(new); 13608feae131SDavid Howells 13618feae131SDavid Howells down_write(&nommu_region_sem); 13628feae131SDavid Howells delete_nommu_region(vma->vm_region); 13638feae131SDavid Howells if (new_below) { 13648feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 13658feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 13668feae131SDavid Howells } else { 13678feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1368dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 13698feae131SDavid Howells } 13708feae131SDavid Howells add_nommu_region(vma->vm_region); 13718feae131SDavid Howells add_nommu_region(new->vm_region); 13728feae131SDavid Howells up_write(&nommu_region_sem); 13738220543dSMatthew Wilcox (Oracle) 13748220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(vma, mm); 13758220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(new, mm); 137647d9644dSLiam R. Howlett vma_iter_store(vmi, new); 1377fd9edbdbSLiam Howlett mm->map_count++; 13788feae131SDavid Howells return 0; 13798220543dSMatthew Wilcox (Oracle) 138047d9644dSLiam R. Howlett err_vmi_preallocate: 13818220543dSMatthew Wilcox (Oracle) vm_area_free(new); 13828220543dSMatthew Wilcox (Oracle) err_vma_dup: 13838220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 13848220543dSMatthew Wilcox (Oracle) return -ENOMEM; 13858feae131SDavid Howells } 13868feae131SDavid Howells 13878feae131SDavid Howells /* 13888feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 13898feae131SDavid Howells * the end 13908feae131SDavid Howells */ 139107f1bc5aSLiam R. Howlett static int vmi_shrink_vma(struct vma_iterator *vmi, 13928feae131SDavid Howells struct vm_area_struct *vma, 13938feae131SDavid Howells unsigned long from, unsigned long to) 13948feae131SDavid Howells { 13958feae131SDavid Howells struct vm_region *region; 13968feae131SDavid Howells 13978feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 13988feae131SDavid Howells * and list */ 139907f1bc5aSLiam R. Howlett if (vma_iter_prealloc(vmi)) { 140007f1bc5aSLiam R. Howlett pr_warn("Allocation of vma tree for process %d failed\n", 140107f1bc5aSLiam R. Howlett current->pid); 14028220543dSMatthew Wilcox (Oracle) return -ENOMEM; 140307f1bc5aSLiam R. Howlett } 140407f1bc5aSLiam R. Howlett 140507f1bc5aSLiam R. Howlett if (from > vma->vm_start) { 140607f1bc5aSLiam R. Howlett vma_iter_clear(vmi, from, vma->vm_end); 14078feae131SDavid Howells vma->vm_end = from; 140807f1bc5aSLiam R. Howlett } else { 140907f1bc5aSLiam R. Howlett vma_iter_clear(vmi, vma->vm_start, to); 14108feae131SDavid Howells vma->vm_start = to; 141107f1bc5aSLiam R. Howlett } 14128feae131SDavid Howells 14138feae131SDavid Howells /* cut the backing region down to size */ 14148feae131SDavid Howells region = vma->vm_region; 14151e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 14168feae131SDavid Howells 14178feae131SDavid Howells down_write(&nommu_region_sem); 14188feae131SDavid Howells delete_nommu_region(region); 1419dd8632a1SPaul Mundt if (from > region->vm_start) { 1420dd8632a1SPaul Mundt to = region->vm_top; 1421dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1422dd8632a1SPaul Mundt } else { 14238feae131SDavid Howells region->vm_start = to; 1424dd8632a1SPaul Mundt } 14258feae131SDavid Howells add_nommu_region(region); 14268feae131SDavid Howells up_write(&nommu_region_sem); 14278feae131SDavid Howells 14288feae131SDavid Howells free_page_series(from, to); 14298feae131SDavid Howells return 0; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 14323034097aSDavid Howells /* 14333034097aSDavid Howells * release a mapping 14348feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 14358feae131SDavid Howells * VMA, though it need not cover the whole VMA 14363034097aSDavid Howells */ 1437897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) 14381da177e4SLinus Torvalds { 143947d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 14408feae131SDavid Howells struct vm_area_struct *vma; 1441f67d9b15SBob Liu unsigned long end; 14428220543dSMatthew Wilcox (Oracle) int ret = 0; 14431da177e4SLinus Torvalds 1444f67d9b15SBob Liu len = PAGE_ALIGN(len); 14458feae131SDavid Howells if (len == 0) 14461da177e4SLinus Torvalds return -EINVAL; 14471da177e4SLinus Torvalds 1448f67d9b15SBob Liu end = start + len; 1449f67d9b15SBob Liu 14508feae131SDavid Howells /* find the first potentially overlapping VMA */ 145147d9644dSLiam R. Howlett vma = vma_find(&vmi, end); 14528feae131SDavid Howells if (!vma) { 1453ac714904SChoi Gi-yong static int limit; 145433e5d769SDavid Howells if (limit < 5) { 145522cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 145633e5d769SDavid Howells current->pid, current->comm, 145733e5d769SDavid Howells start, start + len - 1); 145833e5d769SDavid Howells limit++; 145933e5d769SDavid Howells } 14608feae131SDavid Howells return -EINVAL; 14618feae131SDavid Howells } 14621da177e4SLinus Torvalds 14638feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 14648feae131SDavid Howells if (vma->vm_file) { 14658feae131SDavid Howells do { 146622cc877bSLeon Romanovsky if (start > vma->vm_start) 14678feae131SDavid Howells return -EINVAL; 14688feae131SDavid Howells if (end == vma->vm_end) 14698feae131SDavid Howells goto erase_whole_vma; 147047d9644dSLiam R. Howlett vma = vma_find(&vmi, end); 1471d75a310cSNamhyung Kim } while (vma); 14728feae131SDavid Howells return -EINVAL; 14738feae131SDavid Howells } else { 14748feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 14758feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 14768feae131SDavid Howells goto erase_whole_vma; 147722cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 14788feae131SDavid Howells return -EINVAL; 14791824cb75SAlexander Kuleshov if (offset_in_page(start)) 14808feae131SDavid Howells return -EINVAL; 14811824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end)) 14828feae131SDavid Howells return -EINVAL; 14838feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 14849760ebffSLiam R. Howlett ret = split_vma(&vmi, vma, start, 1); 148522cc877bSLeon Romanovsky if (ret < 0) 14868feae131SDavid Howells return ret; 14878feae131SDavid Howells } 148807f1bc5aSLiam R. Howlett return vmi_shrink_vma(&vmi, vma, start, end); 14898feae131SDavid Howells } 14901da177e4SLinus Torvalds 14918feae131SDavid Howells erase_whole_vma: 14928220543dSMatthew Wilcox (Oracle) if (delete_vma_from_mm(vma)) 14938220543dSMatthew Wilcox (Oracle) ret = -ENOMEM; 149480be727eSLiam Howlett else 14958feae131SDavid Howells delete_vma(mm, vma); 14968220543dSMatthew Wilcox (Oracle) return ret; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 1499bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 15003034097aSDavid Howells { 1501bfce281cSAl Viro struct mm_struct *mm = current->mm; 15023034097aSDavid Howells int ret; 15033034097aSDavid Howells 1504d8ed45c5SMichel Lespinasse mmap_write_lock(mm); 1505897ab3e0SMike Rapoport ret = do_munmap(mm, addr, len, NULL); 1506d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 15073034097aSDavid Howells return ret; 15083034097aSDavid Howells } 1509a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1510a46ef99dSLinus Torvalds 1511a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1512a46ef99dSLinus Torvalds { 1513bfce281cSAl Viro return vm_munmap(addr, len); 1514a46ef99dSLinus Torvalds } 15153034097aSDavid Howells 15163034097aSDavid Howells /* 15178feae131SDavid Howells * release all the mappings made in a process's VM space 15183034097aSDavid Howells */ 15191da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 15201da177e4SLinus Torvalds { 15218220543dSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0); 15228feae131SDavid Howells struct vm_area_struct *vma; 15231da177e4SLinus Torvalds 15248feae131SDavid Howells if (!mm) 15258feae131SDavid Howells return; 15268feae131SDavid Howells 15271da177e4SLinus Torvalds mm->total_vm = 0; 15281da177e4SLinus Torvalds 15298220543dSMatthew Wilcox (Oracle) /* 15308220543dSMatthew Wilcox (Oracle) * Lock the mm to avoid assert complaining even though this is the only 15318220543dSMatthew Wilcox (Oracle) * user of the mm 15328220543dSMatthew Wilcox (Oracle) */ 15338220543dSMatthew Wilcox (Oracle) mmap_write_lock(mm); 15348220543dSMatthew Wilcox (Oracle) for_each_vma(vmi, vma) { 15358220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 15368feae131SDavid Howells delete_vma(mm, vma); 153704c34961SSteven J. Magnani cond_resched(); 15381da177e4SLinus Torvalds } 1539524e00b3SLiam R. Howlett __mt_destroy(&mm->mm_mt); 15408220543dSMatthew Wilcox (Oracle) mmap_write_unlock(mm); 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 15435d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len) 15441da177e4SLinus Torvalds { 15451da177e4SLinus Torvalds return -ENOMEM; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* 15496fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 15506fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 15511da177e4SLinus Torvalds * 15526fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 15538feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 15548feae131SDavid Howells * block is not shareable 15551da177e4SLinus Torvalds * 15566fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 15571da177e4SLinus Torvalds */ 15584b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 15591da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 15601da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 15611da177e4SLinus Torvalds { 15626fa5f80bSDavid Howells struct vm_area_struct *vma; 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds /* insanity checks first */ 1565f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1566f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 15678feae131SDavid Howells if (old_len == 0 || new_len == 0) 15681da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15691da177e4SLinus Torvalds 15701824cb75SAlexander Kuleshov if (offset_in_page(addr)) 15718feae131SDavid Howells return -EINVAL; 15728feae131SDavid Howells 15731da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 15741da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15751da177e4SLinus Torvalds 15768feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 15776fa5f80bSDavid Howells if (!vma) 15781da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15791da177e4SLinus Torvalds 15806fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 15811da177e4SLinus Torvalds return (unsigned long) -EFAULT; 15821da177e4SLinus Torvalds 1583fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vma->vm_flags)) 15841da177e4SLinus Torvalds return (unsigned long) -EPERM; 15851da177e4SLinus Torvalds 15868feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 15871da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* all checks complete - do it */ 15906fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 15916fa5f80bSDavid Howells return vma->vm_start; 15926fa5f80bSDavid Howells } 15936fa5f80bSDavid Howells 15946a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 15956a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 15966a6160a7SHeiko Carstens unsigned long, new_addr) 15976fa5f80bSDavid Howells { 15986fa5f80bSDavid Howells unsigned long ret; 15996fa5f80bSDavid Howells 1600d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 16016fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 1602d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 16036fa5f80bSDavid Howells return ret; 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 1606df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1607df06b37fSKeith Busch unsigned int foll_flags) 16081da177e4SLinus Torvalds { 16091da177e4SLinus Torvalds return NULL; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 16128f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 16138f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 16141da177e4SLinus Torvalds { 16158f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 16168f3b1327SBob Liu return -EINVAL; 16178f3b1327SBob Liu 16181c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 161966aa2b4bSGreg Ungerer return 0; 16201da177e4SLinus Torvalds } 162122c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 16221da177e4SLinus Torvalds 16233c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 16243c0b9de6SLinus Torvalds { 16253c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 16263c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 16273c0b9de6SLinus Torvalds 16283c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 16293c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 16303c0b9de6SLinus Torvalds } 16313c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 16323c0b9de6SLinus Torvalds 1633f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1634f905bc44SPaul Mundt unsigned long pgoff) 1635f905bc44SPaul Mundt { 1636f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1637f905bc44SPaul Mundt 1638f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1639f905bc44SPaul Mundt return -EINVAL; 1640f905bc44SPaul Mundt 1641f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1642f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1643f905bc44SPaul Mundt 1644f905bc44SPaul Mundt return 0; 1645f905bc44SPaul Mundt } 1646f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1647f905bc44SPaul Mundt 16482bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf) 1649b0e15190SDavid Howells { 1650b0e15190SDavid Howells BUG(); 1651d0217ac0SNick Piggin return 0; 1652b0e15190SDavid Howells } 1653b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 16540ec76a11SDavid Howells 16553f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf, 1656bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff) 1657f1820361SKirill A. Shutemov { 1658f1820361SKirill A. Shutemov BUG(); 16593f98a28cSGeert Uytterhoeven return 0; 1660f1820361SKirill A. Shutemov } 1661f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1662f1820361SKirill A. Shutemov 1663d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, 1664d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags) 16650ec76a11SDavid Howells { 16660ec76a11SDavid Howells struct vm_area_struct *vma; 1667442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 16680ec76a11SDavid Howells 1669d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 16701e426fe2SKonstantin Khlebnikov return 0; 16710ec76a11SDavid Howells 16720ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 16730159b141SDavid Howells vma = find_vma(mm, addr); 16740159b141SDavid Howells if (vma) { 16750ec76a11SDavid Howells /* don't overrun this mapping */ 16760ec76a11SDavid Howells if (addr + len >= vma->vm_end) 16770ec76a11SDavid Howells len = vma->vm_end - addr; 16780ec76a11SDavid Howells 16790ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1680d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 16817959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 16827959722bSJie Zhang (void *) addr, buf, len); 1683d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 16847959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 16857959722bSJie Zhang buf, (void *) addr, len); 16860ec76a11SDavid Howells else 16870ec76a11SDavid Howells len = 0; 16880ec76a11SDavid Howells } else { 16890ec76a11SDavid Howells len = 0; 16900ec76a11SDavid Howells } 16910ec76a11SDavid Howells 1692d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 1693f55f199bSMike Frysinger 1694f55f199bSMike Frysinger return len; 1695f55f199bSMike Frysinger } 1696f55f199bSMike Frysinger 1697f55f199bSMike Frysinger /** 1698b7701a5fSMike Rapoport * access_remote_vm - access another process' address space 1699f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1700f55f199bSMike Frysinger * @addr: start address to access 1701f55f199bSMike Frysinger * @buf: source or destination buffer 1702f55f199bSMike Frysinger * @len: number of bytes to transfer 17036347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 1704f55f199bSMike Frysinger * 1705f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1706f55f199bSMike Frysinger */ 1707f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 17086347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 1709f55f199bSMike Frysinger { 1710d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 1711f55f199bSMike Frysinger } 1712f55f199bSMike Frysinger 1713f55f199bSMike Frysinger /* 1714f55f199bSMike Frysinger * Access another process' address space. 1715f55f199bSMike Frysinger * - source/target buffer must be kernel space 1716f55f199bSMike Frysinger */ 1717f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, 1718f307ab6dSLorenzo Stoakes unsigned int gup_flags) 1719f55f199bSMike Frysinger { 1720f55f199bSMike Frysinger struct mm_struct *mm; 1721f55f199bSMike Frysinger 1722f55f199bSMike Frysinger if (addr + len < addr) 1723f55f199bSMike Frysinger return 0; 1724f55f199bSMike Frysinger 1725f55f199bSMike Frysinger mm = get_task_mm(tsk); 1726f55f199bSMike Frysinger if (!mm) 1727f55f199bSMike Frysinger return 0; 1728f55f199bSMike Frysinger 1729d3f5ffcaSJohn Hubbard len = __access_remote_vm(mm, addr, buf, len, gup_flags); 1730f55f199bSMike Frysinger 17310ec76a11SDavid Howells mmput(mm); 17320ec76a11SDavid Howells return len; 17330ec76a11SDavid Howells } 1734fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 17357e660872SDavid Howells 17367e660872SDavid Howells /** 17377e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 17387e660872SDavid Howells * @inode: The inode to check 17397e660872SDavid Howells * @size: The current filesize of the inode 17407e660872SDavid Howells * @newsize: The proposed filesize of the inode 17417e660872SDavid Howells * 17427e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 1743c08b342cSRandy Dunlap * make sure that any outstanding VMAs aren't broken and then shrink the 1744c08b342cSRandy Dunlap * vm_regions that extend beyond so that do_mmap() doesn't 17457e660872SDavid Howells * automatically grant mappings that are too large. 17467e660872SDavid Howells */ 17477e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 17487e660872SDavid Howells size_t newsize) 17497e660872SDavid Howells { 17507e660872SDavid Howells struct vm_area_struct *vma; 17517e660872SDavid Howells struct vm_region *region; 17527e660872SDavid Howells pgoff_t low, high; 17537e660872SDavid Howells size_t r_size, r_top; 17547e660872SDavid Howells 17557e660872SDavid Howells low = newsize >> PAGE_SHIFT; 17567e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 17577e660872SDavid Howells 17587e660872SDavid Howells down_write(&nommu_region_sem); 17591acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 17607e660872SDavid Howells 17617e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 17626b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 17637e660872SDavid Howells /* found one - only interested if it's shared out of the page 17647e660872SDavid Howells * cache */ 17657e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 17661acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17677e660872SDavid Howells up_write(&nommu_region_sem); 17687e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 17697e660872SDavid Howells } 17707e660872SDavid Howells } 17717e660872SDavid Howells 17727e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 17737e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 17747e660872SDavid Howells * 17757e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 17767e660872SDavid Howells * shouldn't be any 17777e660872SDavid Howells */ 17781acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 17797e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 17807e660872SDavid Howells continue; 17817e660872SDavid Howells 17827e660872SDavid Howells region = vma->vm_region; 17837e660872SDavid Howells r_size = region->vm_top - region->vm_start; 17847e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 17857e660872SDavid Howells 17867e660872SDavid Howells if (r_top > newsize) { 17877e660872SDavid Howells region->vm_top -= r_top - newsize; 17887e660872SDavid Howells if (region->vm_end > region->vm_top) 17897e660872SDavid Howells region->vm_end = region->vm_top; 17907e660872SDavid Howells } 17917e660872SDavid Howells } 17927e660872SDavid Howells 17931acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17947e660872SDavid Howells up_write(&nommu_region_sem); 17957e660872SDavid Howells return 0; 17967e660872SDavid Howells } 1797c9b1d098SAndrew Shewmaker 1798c9b1d098SAndrew Shewmaker /* 1799c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 1800c9b1d098SAndrew Shewmaker * 1801c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 1802c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 1803c9b1d098SAndrew Shewmaker * mode. 1804c9b1d098SAndrew Shewmaker * 1805c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 1806c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 1807c9b1d098SAndrew Shewmaker */ 1808c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 1809c9b1d098SAndrew Shewmaker { 1810c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 1811c9b1d098SAndrew Shewmaker 1812c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 1813c9b1d098SAndrew Shewmaker 1814c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 1815c9b1d098SAndrew Shewmaker return 0; 1816c9b1d098SAndrew Shewmaker } 1817a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve); 18184eeab4f5SAndrew Shewmaker 18194eeab4f5SAndrew Shewmaker /* 18204eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 18214eeab4f5SAndrew Shewmaker * 18224eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 18234eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 18244eeab4f5SAndrew Shewmaker * 18254eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 18264eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 18274eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 18284eeab4f5SAndrew Shewmaker */ 18294eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 18304eeab4f5SAndrew Shewmaker { 18314eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 18324eeab4f5SAndrew Shewmaker 1833c41f012aSMichal Hocko free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 18344eeab4f5SAndrew Shewmaker 18354eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 18364eeab4f5SAndrew Shewmaker return 0; 18374eeab4f5SAndrew Shewmaker } 1838a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve); 1839