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 134b3bdda02SChristoph Lameter void vfree(const void *addr) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds kfree(addr); 1371da177e4SLinus Torvalds } 138b5073173SPaul Mundt EXPORT_SYMBOL(vfree); 1391da177e4SLinus Torvalds 14088dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds /* 1438518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 1448518609dSRobert P. J. Day * returns only a logical address. 1451da177e4SLinus Torvalds */ 14684097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 1471da177e4SLinus Torvalds } 148b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc); 1491da177e4SLinus Torvalds 150041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align, 151041de93fSChristoph Hellwig unsigned long start, unsigned long end, gfp_t gfp_mask, 152041de93fSChristoph Hellwig pgprot_t prot, unsigned long vm_flags, int node, 153041de93fSChristoph Hellwig const void *caller) 154041de93fSChristoph Hellwig { 155041de93fSChristoph Hellwig return __vmalloc(size, gfp_mask); 156041de93fSChristoph Hellwig } 157041de93fSChristoph Hellwig 1582b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, 1592b905948SChristoph Hellwig int node, const void *caller) 160a7c3e901SMichal Hocko { 1612b905948SChristoph Hellwig return __vmalloc(size, gfp_mask); 162a7c3e901SMichal Hocko } 163a7c3e901SMichal Hocko 164ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) 165f905bc44SPaul Mundt { 166f905bc44SPaul Mundt void *ret; 167f905bc44SPaul Mundt 16888dca4caSChristoph Hellwig ret = __vmalloc(size, flags); 169f905bc44SPaul Mundt if (ret) { 170f905bc44SPaul Mundt struct vm_area_struct *vma; 171f905bc44SPaul Mundt 172d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 173f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret); 174f905bc44SPaul Mundt if (vma) 1751c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_USERMAP); 176d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 177f905bc44SPaul Mundt } 178f905bc44SPaul Mundt 179f905bc44SPaul Mundt return ret; 180f905bc44SPaul Mundt } 181ed81745aSAndrii Nakryiko 182ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size) 183ed81745aSAndrii Nakryiko { 184ed81745aSAndrii Nakryiko return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO); 185ed81745aSAndrii Nakryiko } 186f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user); 187f905bc44SPaul Mundt 188b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds return virt_to_page(addr); 1911da177e4SLinus Torvalds } 192b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page); 1931da177e4SLinus Torvalds 194b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr)); 1971da177e4SLinus Torvalds } 198b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn); 1991da177e4SLinus Torvalds 2004c91c07cSLorenzo Stoakes long vread_iter(struct iov_iter *iter, const char *addr, size_t count) 2011da177e4SLinus Torvalds { 2029bde916bSChen Gang /* Don't allow overflow */ 2034c91c07cSLorenzo Stoakes if ((unsigned long) addr + count < count) 2044c91c07cSLorenzo Stoakes count = -(unsigned long) addr; 2059bde916bSChen Gang 2064c91c07cSLorenzo Stoakes return copy_to_iter(addr, count, iter); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* 210e1c05067SMasahiro Yamada * vmalloc - allocate virtually contiguous memory 2111da177e4SLinus Torvalds * 2121da177e4SLinus Torvalds * @size: allocation size 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 215e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space. 2161da177e4SLinus Torvalds * 217c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 2181da177e4SLinus Torvalds * use __vmalloc() instead. 2191da177e4SLinus Torvalds */ 2201da177e4SLinus Torvalds void *vmalloc(unsigned long size) 2211da177e4SLinus Torvalds { 222176056fdSChen Li return __vmalloc(size, GFP_KERNEL); 2231da177e4SLinus Torvalds } 224f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc); 225f6138882SAndrew Morton 2260fc74d82SLinus Torvalds void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __weak __alias(__vmalloc); 2270fc74d82SLinus Torvalds 228e1ca7788SDave Young /* 229e1c05067SMasahiro Yamada * vzalloc - allocate virtually contiguous memory with zero fill 230e1ca7788SDave Young * 231e1ca7788SDave Young * @size: allocation size 232e1ca7788SDave Young * 233e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 234e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space. 235e1ca7788SDave Young * The memory allocated is set to zero. 236e1ca7788SDave Young * 237e1ca7788SDave Young * For tight control over page level allocator and protection flags 238e1ca7788SDave Young * use __vmalloc() instead. 239e1ca7788SDave Young */ 240e1ca7788SDave Young void *vzalloc(unsigned long size) 241e1ca7788SDave Young { 242176056fdSChen Li return __vmalloc(size, GFP_KERNEL | __GFP_ZERO); 243e1ca7788SDave Young } 244e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 245e1ca7788SDave Young 246e1ca7788SDave Young /** 247e1ca7788SDave Young * vmalloc_node - allocate memory on a specific node 248e1ca7788SDave Young * @size: allocation size 249e1ca7788SDave Young * @node: numa node 250e1ca7788SDave Young * 251e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 252e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 253e1ca7788SDave Young * 254e1ca7788SDave Young * For tight control over page level allocator and protection flags 255e1ca7788SDave Young * use __vmalloc() instead. 256e1ca7788SDave Young */ 257f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node) 258f6138882SAndrew Morton { 259f6138882SAndrew Morton return vmalloc(size); 260f6138882SAndrew Morton } 2619a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node); 262e1ca7788SDave Young 263e1ca7788SDave Young /** 264e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 265e1ca7788SDave Young * @size: allocation size 266e1ca7788SDave Young * @node: numa node 267e1ca7788SDave Young * 268e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 269e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 270e1ca7788SDave Young * The memory allocated is set to zero. 271e1ca7788SDave Young * 272e1ca7788SDave Young * For tight control over page level allocator and protection flags 273e1ca7788SDave Young * use __vmalloc() instead. 274e1ca7788SDave Young */ 275e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 276e1ca7788SDave Young { 277e1ca7788SDave Young return vzalloc(size); 278e1ca7788SDave Young } 279e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 2801da177e4SLinus Torvalds 2811af446edSPaul Mundt /** 282b5073173SPaul Mundt * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 2831da177e4SLinus Torvalds * @size: allocation size 2841da177e4SLinus Torvalds * 2851da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 286e1c05067SMasahiro Yamada * page level allocator and map them into contiguous kernel virtual space. 2871da177e4SLinus Torvalds */ 2881da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 2891da177e4SLinus Torvalds { 29088dca4caSChristoph Hellwig return __vmalloc(size, GFP_KERNEL); 2911da177e4SLinus Torvalds } 292b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32); 293b5073173SPaul Mundt 294b5073173SPaul Mundt /** 295b5073173SPaul Mundt * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 296b5073173SPaul Mundt * @size: allocation size 297b5073173SPaul Mundt * 298b5073173SPaul Mundt * The resulting memory area is 32bit addressable and zeroed so it can be 299b5073173SPaul Mundt * mapped to userspace without leaking data. 300f905bc44SPaul Mundt * 301f905bc44SPaul Mundt * VM_USERMAP is set on the corresponding VMA so that subsequent calls to 302f905bc44SPaul Mundt * remap_vmalloc_range() are permissible. 303b5073173SPaul Mundt */ 304b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size) 305b5073173SPaul Mundt { 306f905bc44SPaul Mundt /* 307f905bc44SPaul Mundt * We'll have to sort out the ZONE_DMA bits for 64-bit, 308f905bc44SPaul Mundt * but for now this can simply use vmalloc_user() directly. 309f905bc44SPaul Mundt */ 310f905bc44SPaul Mundt return vmalloc_user(size); 311b5073173SPaul Mundt } 312b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds BUG(); 3171da177e4SLinus Torvalds return NULL; 3181da177e4SLinus Torvalds } 319b5073173SPaul Mundt EXPORT_SYMBOL(vmap); 3201da177e4SLinus Torvalds 321b3bdda02SChristoph Lameter void vunmap(const void *addr) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds BUG(); 3241da177e4SLinus Torvalds } 325b5073173SPaul Mundt EXPORT_SYMBOL(vunmap); 3261da177e4SLinus Torvalds 327d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node) 328eb6434d9SPaul Mundt { 329eb6434d9SPaul Mundt BUG(); 330eb6434d9SPaul Mundt return NULL; 331eb6434d9SPaul Mundt } 332eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram); 333eb6434d9SPaul Mundt 334eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count) 335eb6434d9SPaul Mundt { 336eb6434d9SPaul Mundt BUG(); 337eb6434d9SPaul Mundt } 338eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram); 339eb6434d9SPaul Mundt 340eb6434d9SPaul Mundt void vm_unmap_aliases(void) 341eb6434d9SPaul Mundt { 342eb6434d9SPaul Mundt } 343eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases); 344eb6434d9SPaul Mundt 34529c185e5SPaul Mundt void free_vm_area(struct vm_struct *area) 34629c185e5SPaul Mundt { 34729c185e5SPaul Mundt BUG(); 34829c185e5SPaul Mundt } 34929c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area); 35029c185e5SPaul Mundt 351b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 352b5073173SPaul Mundt struct page *page) 353b5073173SPaul Mundt { 354b5073173SPaul Mundt return -EINVAL; 355b5073173SPaul Mundt } 356b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page); 357b5073173SPaul Mundt 358a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages, 359a667d745SSouptick Joarder unsigned long num) 360a667d745SSouptick Joarder { 361a667d745SSouptick Joarder return -EINVAL; 362a667d745SSouptick Joarder } 363a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages); 364a667d745SSouptick Joarder 365a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, 366a667d745SSouptick Joarder unsigned long num) 367a667d745SSouptick Joarder { 368a667d745SSouptick Joarder return -EINVAL; 369a667d745SSouptick Joarder } 370a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero); 371a667d745SSouptick Joarder 3721eeb66a1SChristoph Hellwig /* 3731da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel 3741da177e4SLinus Torvalds * lock, except when an application is doing something nasty 3751da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped 3761da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need 3771da177e4SLinus Torvalds * to invoke file system routines that need the global lock. 3781da177e4SLinus Torvalds */ 3796a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds if (brk < mm->start_brk || brk > mm->context.end_brk) 3841da177e4SLinus Torvalds return mm->brk; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds if (mm->brk == brk) 3871da177e4SLinus Torvalds return mm->brk; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* 3901da177e4SLinus Torvalds * Always allow shrinking brk 3911da177e4SLinus Torvalds */ 3921da177e4SLinus Torvalds if (brk <= mm->brk) { 3931da177e4SLinus Torvalds mm->brk = brk; 3941da177e4SLinus Torvalds return brk; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* 3981da177e4SLinus Torvalds * Ok, looks good - let it rip. 3991da177e4SLinus Torvalds */ 400a75a2df6SChristoph Hellwig flush_icache_user_range(mm->brk, brk); 4011da177e4SLinus Torvalds return mm->brk = brk; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4048feae131SDavid Howells /* 4053edf41d8Sseokhoon.yoon * initialise the percpu counter for VM and region record slabs 4068feae131SDavid Howells */ 4078feae131SDavid Howells void __init mmap_init(void) 4081da177e4SLinus Torvalds { 40900a62ce9SKOSAKI Motohiro int ret; 41000a62ce9SKOSAKI Motohiro 411908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 41200a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 4135d097056SVladimir Davydov vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT); 4148feae131SDavid Howells } 4151da177e4SLinus Torvalds 4168feae131SDavid Howells /* 4178feae131SDavid Howells * validate the region tree 4188feae131SDavid Howells * - the caller must hold the region lock 4198feae131SDavid Howells */ 4208feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS 4218feae131SDavid Howells static noinline void validate_nommu_regions(void) 4228feae131SDavid Howells { 4238feae131SDavid Howells struct vm_region *region, *last; 4248feae131SDavid Howells struct rb_node *p, *lastp; 4251da177e4SLinus Torvalds 4268feae131SDavid Howells lastp = rb_first(&nommu_region_tree); 4278feae131SDavid Howells if (!lastp) 4288feae131SDavid Howells return; 4298feae131SDavid Howells 4308feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 431c9427bc0SGeliang Tang BUG_ON(last->vm_end <= last->vm_start); 432c9427bc0SGeliang Tang BUG_ON(last->vm_top < last->vm_end); 4338feae131SDavid Howells 4348feae131SDavid Howells while ((p = rb_next(lastp))) { 4358feae131SDavid Howells region = rb_entry(p, struct vm_region, vm_rb); 4368feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 4378feae131SDavid Howells 438c9427bc0SGeliang Tang BUG_ON(region->vm_end <= region->vm_start); 439c9427bc0SGeliang Tang BUG_ON(region->vm_top < region->vm_end); 440c9427bc0SGeliang Tang BUG_ON(region->vm_start < last->vm_top); 4418feae131SDavid Howells 4428feae131SDavid Howells lastp = p; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds } 4458feae131SDavid Howells #else 44633e5d769SDavid Howells static void validate_nommu_regions(void) 44733e5d769SDavid Howells { 44833e5d769SDavid Howells } 4498feae131SDavid Howells #endif 4508feae131SDavid Howells 4518feae131SDavid Howells /* 4528feae131SDavid Howells * add a region into the global tree 4538feae131SDavid Howells */ 4548feae131SDavid Howells static void add_nommu_region(struct vm_region *region) 4558feae131SDavid Howells { 4568feae131SDavid Howells struct vm_region *pregion; 4578feae131SDavid Howells struct rb_node **p, *parent; 4588feae131SDavid Howells 4598feae131SDavid Howells validate_nommu_regions(); 4608feae131SDavid Howells 4618feae131SDavid Howells parent = NULL; 4628feae131SDavid Howells p = &nommu_region_tree.rb_node; 4638feae131SDavid Howells while (*p) { 4648feae131SDavid Howells parent = *p; 4658feae131SDavid Howells pregion = rb_entry(parent, struct vm_region, vm_rb); 4668feae131SDavid Howells if (region->vm_start < pregion->vm_start) 4678feae131SDavid Howells p = &(*p)->rb_left; 4688feae131SDavid Howells else if (region->vm_start > pregion->vm_start) 4698feae131SDavid Howells p = &(*p)->rb_right; 4708feae131SDavid Howells else if (pregion == region) 4718feae131SDavid Howells return; 4728feae131SDavid Howells else 4738feae131SDavid Howells BUG(); 4748feae131SDavid Howells } 4758feae131SDavid Howells 4768feae131SDavid Howells rb_link_node(®ion->vm_rb, parent, p); 4778feae131SDavid Howells rb_insert_color(®ion->vm_rb, &nommu_region_tree); 4788feae131SDavid Howells 4798feae131SDavid Howells validate_nommu_regions(); 4808feae131SDavid Howells } 4818feae131SDavid Howells 4828feae131SDavid Howells /* 4838feae131SDavid Howells * delete a region from the global tree 4848feae131SDavid Howells */ 4858feae131SDavid Howells static void delete_nommu_region(struct vm_region *region) 4868feae131SDavid Howells { 4878feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 4888feae131SDavid Howells 4898feae131SDavid Howells validate_nommu_regions(); 4908feae131SDavid Howells rb_erase(®ion->vm_rb, &nommu_region_tree); 4918feae131SDavid Howells validate_nommu_regions(); 4928feae131SDavid Howells } 4938feae131SDavid Howells 4948feae131SDavid Howells /* 4958feae131SDavid Howells * free a contiguous series of pages 4968feae131SDavid Howells */ 4978feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to) 4988feae131SDavid Howells { 4998feae131SDavid Howells for (; from < to; from += PAGE_SIZE) { 5009330723cSLinus Walleij struct page *page = virt_to_page((void *)from); 5018feae131SDavid Howells 50233e5d769SDavid Howells atomic_long_dec(&mmap_pages_allocated); 5038feae131SDavid Howells put_page(page); 5048feae131SDavid Howells } 5058feae131SDavid Howells } 5068feae131SDavid Howells 5078feae131SDavid Howells /* 5088feae131SDavid Howells * release a reference to a region 50933e5d769SDavid Howells * - the caller must hold the region semaphore for writing, which this releases 510dd8632a1SPaul Mundt * - the region may not have been added to the tree yet, in which case vm_top 5118feae131SDavid Howells * will equal vm_start 5128feae131SDavid Howells */ 5138feae131SDavid Howells static void __put_nommu_region(struct vm_region *region) 5148feae131SDavid Howells __releases(nommu_region_sem) 5158feae131SDavid Howells { 5168feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 5178feae131SDavid Howells 5181e2ae599SDavid Howells if (--region->vm_usage == 0) { 519dd8632a1SPaul Mundt if (region->vm_top > region->vm_start) 5208feae131SDavid Howells delete_nommu_region(region); 5218feae131SDavid Howells up_write(&nommu_region_sem); 5228feae131SDavid Howells 5238feae131SDavid Howells if (region->vm_file) 5248feae131SDavid Howells fput(region->vm_file); 5258feae131SDavid Howells 5268feae131SDavid Howells /* IO memory and memory shared directly out of the pagecache 5278feae131SDavid Howells * from ramfs/tmpfs mustn't be released here */ 52822cc877bSLeon Romanovsky if (region->vm_flags & VM_MAPPED_COPY) 529dd8632a1SPaul Mundt free_page_series(region->vm_start, region->vm_top); 5308feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 5318feae131SDavid Howells } else { 5328feae131SDavid Howells up_write(&nommu_region_sem); 5338feae131SDavid Howells } 5348feae131SDavid Howells } 5358feae131SDavid Howells 5368feae131SDavid Howells /* 5378feae131SDavid Howells * release a reference to a region 5388feae131SDavid Howells */ 5398feae131SDavid Howells static void put_nommu_region(struct vm_region *region) 5408feae131SDavid Howells { 5418feae131SDavid Howells down_write(&nommu_region_sem); 5428feae131SDavid Howells __put_nommu_region(region); 5438feae131SDavid Howells } 5441da177e4SLinus Torvalds 5458220543dSMatthew Wilcox (Oracle) static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm) 5463034097aSDavid Howells { 5478feae131SDavid Howells vma->vm_mm = mm; 5488feae131SDavid Howells 5498feae131SDavid Howells /* add the VMA to the mapping */ 5508feae131SDavid Howells if (vma->vm_file) { 5518220543dSMatthew Wilcox (Oracle) struct address_space *mapping = vma->vm_file->f_mapping; 5528feae131SDavid Howells 55383cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 5548feae131SDavid Howells flush_dcache_mmap_lock(mapping); 5556b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 5568feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 55783cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5588feae131SDavid Howells } 5598220543dSMatthew Wilcox (Oracle) } 5608feae131SDavid Howells 5618220543dSMatthew Wilcox (Oracle) static void cleanup_vma_from_mm(struct vm_area_struct *vma) 5628220543dSMatthew Wilcox (Oracle) { 5637964cf8cSLiam R. Howlett vma->vm_mm->map_count--; 5648feae131SDavid Howells /* remove the VMA from the mapping */ 5658feae131SDavid Howells if (vma->vm_file) { 5667964cf8cSLiam R. Howlett struct address_space *mapping; 5678feae131SDavid Howells mapping = vma->vm_file->f_mapping; 5688feae131SDavid Howells 56983cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 5708feae131SDavid Howells flush_dcache_mmap_lock(mapping); 5716b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 5728feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 57383cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 5748feae131SDavid Howells } 5758220543dSMatthew Wilcox (Oracle) } 57647d9644dSLiam R. Howlett 5778220543dSMatthew Wilcox (Oracle) /* 5788220543dSMatthew Wilcox (Oracle) * delete a VMA from its owning mm_struct and address space 5798220543dSMatthew Wilcox (Oracle) */ 5808220543dSMatthew Wilcox (Oracle) static int delete_vma_from_mm(struct vm_area_struct *vma) 5818220543dSMatthew Wilcox (Oracle) { 58247d9644dSLiam R. Howlett VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start); 5838220543dSMatthew Wilcox (Oracle) 584b5df0922SLiam R. Howlett vma_iter_config(&vmi, vma->vm_start, vma->vm_end); 585b5df0922SLiam R. Howlett if (vma_iter_prealloc(&vmi, vma)) { 5868220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n", 5878220543dSMatthew Wilcox (Oracle) current->pid); 5888220543dSMatthew Wilcox (Oracle) return -ENOMEM; 5898220543dSMatthew Wilcox (Oracle) } 5908220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 5918feae131SDavid Howells 5928feae131SDavid Howells /* remove from the MM's tree and list */ 593b5df0922SLiam R. Howlett vma_iter_clear(&vmi); 5948220543dSMatthew Wilcox (Oracle) return 0; 5958feae131SDavid Howells } 5968feae131SDavid Howells /* 5978feae131SDavid Howells * destroy a VMA record 5988feae131SDavid Howells */ 5998feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 6008feae131SDavid Howells { 6018feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 6028feae131SDavid Howells vma->vm_ops->close(vma); 603e9714acfSKonstantin Khlebnikov if (vma->vm_file) 6048feae131SDavid Howells fput(vma->vm_file); 6058feae131SDavid Howells put_nommu_region(vma->vm_region); 6063928d4f5SLinus Torvalds vm_area_free(vma); 6073034097aSDavid Howells } 6083034097aSDavid Howells 609abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, 610abdba2ddSLiam R. Howlett unsigned long start_addr, 611abdba2ddSLiam R. Howlett unsigned long end_addr) 612abdba2ddSLiam R. Howlett { 613abdba2ddSLiam R. Howlett unsigned long index = start_addr; 614abdba2ddSLiam R. Howlett 615abdba2ddSLiam R. Howlett mmap_assert_locked(mm); 616abdba2ddSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1); 617abdba2ddSLiam R. Howlett } 618abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection); 619abdba2ddSLiam R. Howlett 6203034097aSDavid Howells /* 6213034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 622c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6233034097aSDavid Howells */ 6243034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 6253034097aSDavid Howells { 62647d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 6273034097aSDavid Howells 62847d9644dSLiam R. Howlett return vma_iter_load(&vmi); 6293034097aSDavid Howells } 6303034097aSDavid Howells EXPORT_SYMBOL(find_vma); 6313034097aSDavid Howells 6323034097aSDavid Howells /* 633d85a143bSLinus Torvalds * At least xtensa ends up having protection faults even with no 634d85a143bSLinus Torvalds * MMU.. No stack expansion, at least. 635d85a143bSLinus Torvalds */ 636d85a143bSLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm, 637d85a143bSLinus Torvalds unsigned long addr, struct pt_regs *regs) 638d85a143bSLinus Torvalds { 63903f88937SMax Filippov struct vm_area_struct *vma; 64003f88937SMax Filippov 641d85a143bSLinus Torvalds mmap_read_lock(mm); 64203f88937SMax Filippov vma = vma_lookup(mm, addr); 64303f88937SMax Filippov if (!vma) 64403f88937SMax Filippov mmap_read_unlock(mm); 64503f88937SMax Filippov return vma; 646d85a143bSLinus Torvalds } 647d85a143bSLinus Torvalds 648d85a143bSLinus Torvalds /* 6498feae131SDavid Howells * expand a stack to a given address 6508feae131SDavid Howells * - not supported under NOMMU conditions 6518feae131SDavid Howells */ 6528d7071afSLinus Torvalds int expand_stack_locked(struct vm_area_struct *vma, unsigned long addr) 65357c8f63eSGreg Ungerer { 65457c8f63eSGreg Ungerer return -ENOMEM; 65557c8f63eSGreg Ungerer } 65657c8f63eSGreg Ungerer 6578d7071afSLinus Torvalds struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr) 6588d7071afSLinus Torvalds { 6598d7071afSLinus Torvalds mmap_read_unlock(mm); 6608d7071afSLinus Torvalds return NULL; 6618d7071afSLinus Torvalds } 6628d7071afSLinus Torvalds 663930e652aSDavid Howells /* 6646fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 665c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked 6666fa5f80bSDavid Howells */ 6678feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 6688feae131SDavid Howells unsigned long addr, 6698feae131SDavid Howells unsigned long len) 6701da177e4SLinus Torvalds { 6711da177e4SLinus Torvalds struct vm_area_struct *vma; 6728feae131SDavid Howells unsigned long end = addr + len; 67347d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr); 6741da177e4SLinus Torvalds 67547d9644dSLiam R. Howlett vma = vma_iter_load(&vmi); 676524e00b3SLiam R. Howlett if (!vma) 6778feae131SDavid Howells return NULL; 678524e00b3SLiam R. Howlett if (vma->vm_start != addr) 679524e00b3SLiam R. Howlett return NULL; 680524e00b3SLiam R. Howlett if (vma->vm_end != end) 681524e00b3SLiam R. Howlett return NULL; 682524e00b3SLiam R. Howlett 6838feae131SDavid Howells return vma; 6848feae131SDavid Howells } 6851da177e4SLinus Torvalds 6863034097aSDavid Howells /* 6871da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 6881da177e4SLinus Torvalds * mapping we're capable of supporting 6891da177e4SLinus Torvalds */ 6901da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 6911da177e4SLinus Torvalds unsigned long addr, 6921da177e4SLinus Torvalds unsigned long len, 6931da177e4SLinus Torvalds unsigned long prot, 6941da177e4SLinus Torvalds unsigned long flags, 6951da177e4SLinus Torvalds unsigned long pgoff, 6961da177e4SLinus Torvalds unsigned long *_capabilities) 6971da177e4SLinus Torvalds { 6988feae131SDavid Howells unsigned long capabilities, rlen; 6991da177e4SLinus Torvalds int ret; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds /* do the simple checks first */ 70222cc877bSLeon Romanovsky if (flags & MAP_FIXED) 7031da177e4SLinus Torvalds return -EINVAL; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 7061da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 7071da177e4SLinus Torvalds return -EINVAL; 7081da177e4SLinus Torvalds 709f81cff0dSMike Frysinger if (!len) 7101da177e4SLinus Torvalds return -EINVAL; 7111da177e4SLinus Torvalds 712f81cff0dSMike Frysinger /* Careful about overflows.. */ 7138feae131SDavid Howells rlen = PAGE_ALIGN(len); 7148feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 715f81cff0dSMike Frysinger return -ENOMEM; 716f81cff0dSMike Frysinger 7171da177e4SLinus Torvalds /* offset overflow? */ 7188feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 719f81cff0dSMike Frysinger return -EOVERFLOW; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds if (file) { 7221da177e4SLinus Torvalds /* files must support mmap */ 72372c2d531SAl Viro if (!file->f_op->mmap) 7241da177e4SLinus Torvalds return -ENODEV; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 7271da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 7281da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 7291da177e4SLinus Torvalds */ 730b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 731b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 732b4caecd4SChristoph Hellwig } else { 7331da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 7341da177e4SLinus Torvalds * defaults */ 735496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 7361da177e4SLinus Torvalds case S_IFREG: 7371da177e4SLinus Torvalds case S_IFBLK: 738b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 7391da177e4SLinus Torvalds break; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds case S_IFCHR: 7421da177e4SLinus Torvalds capabilities = 743b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 744b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 745b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 7461da177e4SLinus Torvalds break; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds default: 7491da177e4SLinus Torvalds return -EINVAL; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 7541da177e4SLinus Torvalds * device */ 7551da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 756b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7576e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 758b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 7591da177e4SLinus Torvalds 76028d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 76128d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 76228d7a6aeSGraff Yang return -EACCES; 76328d7a6aeSGraff Yang 7641da177e4SLinus Torvalds if (flags & MAP_SHARED) { 7651da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 7661da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 7671da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 7681da177e4SLinus Torvalds return -EACCES; 7691da177e4SLinus Torvalds 770496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 7711da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 7721da177e4SLinus Torvalds return -EACCES; 7731da177e4SLinus Torvalds 774b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 7751da177e4SLinus Torvalds return -ENODEV; 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 778b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 779ac714904SChoi Gi-yong } else { 7801da177e4SLinus Torvalds /* we're going to read the file into private memory we 7811da177e4SLinus Torvalds * allocate */ 782b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 7831da177e4SLinus Torvalds return -ENODEV; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 7861da177e4SLinus Torvalds * shared with the backing device */ 7871da177e4SLinus Torvalds if (prot & PROT_WRITE) 788b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 791b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 792b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 793b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 794b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 7953c7b2045SBernd Schmidt ) { 796b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 7973c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 79822cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 7993c7b2045SBernd Schmidt return -EINVAL; 8003c7b2045SBernd Schmidt } 8013c7b2045SBernd Schmidt } 8023c7b2045SBernd Schmidt } 8033c7b2045SBernd Schmidt 8041da177e4SLinus Torvalds /* handle executable mappings and implied executable 8051da177e4SLinus Torvalds * mappings */ 80690f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 8071da177e4SLinus Torvalds if (prot & PROT_EXEC) 8081da177e4SLinus Torvalds return -EPERM; 809ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 8101da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 8111da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 812b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 8131da177e4SLinus Torvalds prot |= PROT_EXEC; 8141da177e4SLinus Torvalds } 815ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 8161da177e4SLinus Torvalds (prot & PROT_EXEC) && 817b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 8181da177e4SLinus Torvalds ) { 8191da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 820b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 8211da177e4SLinus Torvalds } 822ac714904SChoi Gi-yong } else { 8231da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 8241da177e4SLinus Torvalds * privately mapped 8251da177e4SLinus Torvalds */ 826b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 8291da177e4SLinus Torvalds if ((prot & PROT_READ) && 8301da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 8311da177e4SLinus Torvalds prot |= PROT_EXEC; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* allow the security API to have its say */ 835e5467859SAl Viro ret = security_mmap_addr(addr); 836e5467859SAl Viro if (ret < 0) 837e5467859SAl Viro return ret; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* looks okay */ 8401da177e4SLinus Torvalds *_capabilities = capabilities; 8411da177e4SLinus Torvalds return 0; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* 8451da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 8461da177e4SLinus Torvalds * now know into VMA flags 8471da177e4SLinus Torvalds */ 8481da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 8491da177e4SLinus Torvalds unsigned long prot, 8501da177e4SLinus Torvalds unsigned long flags, 8511da177e4SLinus Torvalds unsigned long capabilities) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds unsigned long vm_flags; 8541da177e4SLinus Torvalds 855e6bfb709SDave Hansen vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); 8561da177e4SLinus Torvalds 857b6b7a8faSDavid Hildenbrand if (!file) { 858b6b7a8faSDavid Hildenbrand /* 859b6b7a8faSDavid Hildenbrand * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because 860b6b7a8faSDavid Hildenbrand * there is no fork(). 8611da177e4SLinus Torvalds */ 862b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 863b6b7a8faSDavid Hildenbrand } else if (flags & MAP_PRIVATE) { 864b6b7a8faSDavid Hildenbrand /* MAP_PRIVATE file mapping */ 865b6b7a8faSDavid Hildenbrand if (capabilities & NOMMU_MAP_DIRECT) 866b6b7a8faSDavid Hildenbrand vm_flags |= (capabilities & NOMMU_VMFLAGS); 867b6b7a8faSDavid Hildenbrand else 868b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 869b6b7a8faSDavid Hildenbrand 870b6b7a8faSDavid Hildenbrand if (!(prot & PROT_WRITE) && !current->ptrace) 871b6b7a8faSDavid Hildenbrand /* 872b6b7a8faSDavid Hildenbrand * R/O private file mapping which cannot be used to 873b6b7a8faSDavid Hildenbrand * modify memory, especially also not via active ptrace 874b6b7a8faSDavid Hildenbrand * (e.g., set breakpoints) or later by upgrading 875b6b7a8faSDavid Hildenbrand * permissions (no mprotect()). We can try overlaying 876b6b7a8faSDavid Hildenbrand * the file mapping, which will work e.g., on chardevs, 877b6b7a8faSDavid Hildenbrand * ramfs/tmpfs/shmfs and romfs/cramf. 878b6b7a8faSDavid Hildenbrand */ 879b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYOVERLAY; 880b6b7a8faSDavid Hildenbrand } else { 881b6b7a8faSDavid Hildenbrand /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */ 882b6b7a8faSDavid Hildenbrand vm_flags |= VM_SHARED | VM_MAYSHARE | 883b6b7a8faSDavid Hildenbrand (capabilities & NOMMU_VMFLAGS); 884b6b7a8faSDavid Hildenbrand } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds return vm_flags; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds /* 8908feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 8918feae131SDavid Howells * pins the storage) 8921da177e4SLinus Torvalds */ 8938feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds int ret; 8961da177e4SLinus Torvalds 897f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 898dd8632a1SPaul Mundt if (ret == 0) { 899dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 900645d83c5SDavid Howells return 0; 901dd8632a1SPaul Mundt } 9021da177e4SLinus Torvalds if (ret != -ENOSYS) 9031da177e4SLinus Torvalds return ret; 9041da177e4SLinus Torvalds 9053fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 9063fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 9073fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 9081da177e4SLinus Torvalds return -ENODEV; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds /* 9121da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 9131da177e4SLinus Torvalds */ 9148feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 9158feae131SDavid Howells struct vm_region *region, 916645d83c5SDavid Howells unsigned long len, 917645d83c5SDavid Howells unsigned long capabilities) 9181da177e4SLinus Torvalds { 919dbc8358cSJoonsoo Kim unsigned long total, point; 9201da177e4SLinus Torvalds void *base; 9218feae131SDavid Howells int ret, order; 9221da177e4SLinus Torvalds 923b6b7a8faSDavid Hildenbrand /* 924b6b7a8faSDavid Hildenbrand * Invoke the file's mapping function so that it can keep track of 925b6b7a8faSDavid Hildenbrand * shared mappings on devices or memory. VM_MAYOVERLAY will be set if 926b6b7a8faSDavid Hildenbrand * it may attempt to share, which will make is_nommu_shared_mapping() 927b6b7a8faSDavid Hildenbrand * happy. 9281da177e4SLinus Torvalds */ 929b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 930f74ac015SMiklos Szeredi ret = call_mmap(vma->vm_file, vma); 9311da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 932fc4f4be9SDavid Hildenbrand if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags))) 933fc4f4be9SDavid Hildenbrand ret = -ENOSYS; 934fc4f4be9SDavid Hildenbrand if (ret == 0) { 935dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 936645d83c5SDavid Howells return 0; 9371da177e4SLinus Torvalds } 938dd8632a1SPaul Mundt if (ret != -ENOSYS) 939dd8632a1SPaul Mundt return ret; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 9421da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 9431da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9468feae131SDavid Howells 9471da177e4SLinus Torvalds /* allocate some memory to hold the mapping 9481da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 9491da177e4SLinus Torvalds * we're allocating is smaller than a page 9501da177e4SLinus Torvalds */ 951f67d9b15SBob Liu order = get_order(len); 9528feae131SDavid Howells total = 1 << order; 953f67d9b15SBob Liu point = len >> PAGE_SHIFT; 954dd8632a1SPaul Mundt 955dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 95622cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 957dbc8358cSJoonsoo Kim total = point; 9588feae131SDavid Howells 959da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 960dbc8358cSJoonsoo Kim if (!base) 961dbc8358cSJoonsoo Kim goto enomem; 9628feae131SDavid Howells 963dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 964dbc8358cSJoonsoo Kim 9651c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY); 9661c71222eSSuren Baghdasaryan region->vm_flags = vma->vm_flags; 9678feae131SDavid Howells region->vm_start = (unsigned long) base; 968f67d9b15SBob Liu region->vm_end = region->vm_start + len; 969dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 9708feae131SDavid Howells 9718feae131SDavid Howells vma->vm_start = region->vm_start; 9728feae131SDavid Howells vma->vm_end = region->vm_start + len; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (vma->vm_file) { 9751da177e4SLinus Torvalds /* read the contents of a file into the copy */ 9761da177e4SLinus Torvalds loff_t fpos; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds fpos = vma->vm_pgoff; 9791da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 9801da177e4SLinus Torvalds 981b4bf802aSChristoph Hellwig ret = kernel_read(vma->vm_file, base, len, &fpos); 9821da177e4SLinus Torvalds if (ret < 0) 9831da177e4SLinus Torvalds goto error_free; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* clear the last little bit */ 986f67d9b15SBob Liu if (ret < len) 987f67d9b15SBob Liu memset(base + ret, 0, len - ret); 9881da177e4SLinus Torvalds 989bfd40eafSKirill A. Shutemov } else { 990bfd40eafSKirill A. Shutemov vma_set_anonymous(vma); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds return 0; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds error_free: 9967223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 9978feae131SDavid Howells region->vm_start = vma->vm_start = 0; 9988feae131SDavid Howells region->vm_end = vma->vm_end = 0; 999dd8632a1SPaul Mundt region->vm_top = 0; 10001da177e4SLinus Torvalds return ret; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds enomem: 1003b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 100405ae6fa3SGreg Ungerer len, current->pid, current->comm); 10051279aa06SKefeng Wang show_mem(); 10061da177e4SLinus Torvalds return -ENOMEM; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* 10101da177e4SLinus Torvalds * handle mapping creation for uClinux 10111da177e4SLinus Torvalds */ 10121fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, 10131da177e4SLinus Torvalds unsigned long addr, 10141da177e4SLinus Torvalds unsigned long len, 10151da177e4SLinus Torvalds unsigned long prot, 10161da177e4SLinus Torvalds unsigned long flags, 1017592b5fadSYu-cheng Yu vm_flags_t vm_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; 10251fcfd8dbSOleg Nesterov unsigned long capabilities, result; 10261da177e4SLinus Torvalds int ret; 102747d9644dSLiam R. Howlett VMA_ITERATOR(vmi, current->mm, 0); 10281da177e4SLinus Torvalds 102941badc15SMichel Lespinasse *populate = 0; 1030bebeb3d6SMichel Lespinasse 10311da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 10321da177e4SLinus Torvalds * mapping */ 10331da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 10341da177e4SLinus Torvalds &capabilities); 103522cc877bSLeon Romanovsky if (ret < 0) 10361da177e4SLinus Torvalds return ret; 10371da177e4SLinus Torvalds 103806aab5a3SDavid Howells /* we ignore the address hint */ 103906aab5a3SDavid Howells addr = 0; 1040f67d9b15SBob Liu len = PAGE_ALIGN(len); 104106aab5a3SDavid Howells 10421da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 10431da177e4SLinus Torvalds * now know into VMA flags */ 1044592b5fadSYu-cheng Yu vm_flags |= determine_vm_flags(file, prot, flags, capabilities); 10451da177e4SLinus Torvalds 10468220543dSMatthew Wilcox (Oracle) 10478feae131SDavid Howells /* we're going to need to record the mapping */ 10488feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 10498feae131SDavid Howells if (!region) 10508feae131SDavid Howells goto error_getting_region; 10511da177e4SLinus Torvalds 1052490fc053SLinus Torvalds vma = vm_area_alloc(current->mm); 10538feae131SDavid Howells if (!vma) 10548feae131SDavid Howells goto error_getting_vma; 10551da177e4SLinus Torvalds 10561e2ae599SDavid Howells region->vm_usage = 1; 10578feae131SDavid Howells region->vm_flags = vm_flags; 10588feae131SDavid Howells region->vm_pgoff = pgoff; 10598feae131SDavid Howells 10601c71222eSSuren Baghdasaryan vm_flags_init(vma, vm_flags); 10618feae131SDavid Howells vma->vm_pgoff = pgoff; 10628feae131SDavid Howells 10638feae131SDavid Howells if (file) { 1064cb0942b8SAl Viro region->vm_file = get_file(file); 1065cb0942b8SAl Viro vma->vm_file = get_file(file); 10668feae131SDavid Howells } 10678feae131SDavid Howells 10688feae131SDavid Howells down_write(&nommu_region_sem); 10698feae131SDavid Howells 10708feae131SDavid Howells /* if we want to share, we need to check for regions created by other 10711da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 10728feae131SDavid Howells * - we can only share with a superset match on most regular files 10731da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 10741da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 10751da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 10761da177e4SLinus Torvalds * than here 10771da177e4SLinus Torvalds */ 1078fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vm_flags)) { 10798feae131SDavid Howells struct vm_region *pregion; 10808feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 10811da177e4SLinus Torvalds 10828feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 10838feae131SDavid Howells pgend = pgoff + pglen; 1084165b2392SDavid Howells 10858feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 10868feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 10871da177e4SLinus Torvalds 1088fc4f4be9SDavid Hildenbrand if (!is_nommu_shared_mapping(pregion->vm_flags)) 10891da177e4SLinus Torvalds continue; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1092496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1093496ad9aaSAl Viro file_inode(file)) 10941da177e4SLinus Torvalds continue; 10951da177e4SLinus Torvalds 10968feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 10971da177e4SLinus Torvalds continue; 10981da177e4SLinus Torvalds 10998feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 11008feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 11018feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 11028feae131SDavid Howells if (pgoff >= rpgend) 11031da177e4SLinus Torvalds continue; 11041da177e4SLinus Torvalds 11058feae131SDavid Howells /* handle inexactly overlapping matches between 11068feae131SDavid Howells * mappings */ 11078feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 11088feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 11098feae131SDavid Howells /* new mapping is not a subset of the region */ 1110b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 11111da177e4SLinus Torvalds goto sharing_violation; 11121da177e4SLinus Torvalds continue; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11158feae131SDavid Howells /* we've found a region we can share */ 11161e2ae599SDavid Howells pregion->vm_usage++; 11178feae131SDavid Howells vma->vm_region = pregion; 11188feae131SDavid Howells start = pregion->vm_start; 11198feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 11208feae131SDavid Howells vma->vm_start = start; 11218feae131SDavid Howells vma->vm_end = start + len; 11221da177e4SLinus Torvalds 112322cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 11241c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY); 112522cc877bSLeon Romanovsky else { 11268feae131SDavid Howells ret = do_mmap_shared_file(vma); 11278feae131SDavid Howells if (ret < 0) { 11288feae131SDavid Howells vma->vm_region = NULL; 11298feae131SDavid Howells vma->vm_start = 0; 11308feae131SDavid Howells vma->vm_end = 0; 11311e2ae599SDavid Howells pregion->vm_usage--; 11328feae131SDavid Howells pregion = NULL; 11338feae131SDavid Howells goto error_just_free; 11341da177e4SLinus Torvalds } 11358feae131SDavid Howells } 11368feae131SDavid Howells fput(region->vm_file); 11378feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 11388feae131SDavid Howells region = pregion; 11398feae131SDavid Howells result = start; 11408feae131SDavid Howells goto share; 11418feae131SDavid Howells } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 11441da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 11451da177e4SLinus Torvalds * tell us the location of a shared mapping 11461da177e4SLinus Torvalds */ 1147b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11481da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 11491da177e4SLinus Torvalds pgoff, flags); 1150bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 11511da177e4SLinus Torvalds ret = addr; 1152bb005a59SNamhyung Kim if (ret != -ENOSYS) 11538feae131SDavid Howells goto error_just_free; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds /* the driver refused to tell us where to site 11561da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 11571da177e4SLinus Torvalds * it */ 1158bb005a59SNamhyung Kim ret = -ENODEV; 1159b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 11608feae131SDavid Howells goto error_just_free; 11611da177e4SLinus Torvalds 1162b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 11638feae131SDavid Howells } else { 11648feae131SDavid Howells vma->vm_start = region->vm_start = addr; 11658feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11708feae131SDavid Howells vma->vm_region = region; 11711da177e4SLinus Torvalds 1172645d83c5SDavid Howells /* set up the mapping 1173b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1174645d83c5SDavid Howells */ 11751da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 11768feae131SDavid Howells ret = do_mmap_shared_file(vma); 11771da177e4SLinus Torvalds else 1178645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 11791da177e4SLinus Torvalds if (ret < 0) 1180645d83c5SDavid Howells goto error_just_free; 1181645d83c5SDavid Howells add_nommu_region(region); 11828feae131SDavid Howells 1183ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 11840bf5f949SChristoph Hellwig if (!vma->vm_file && 11850bf5f949SChristoph Hellwig (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) || 11860bf5f949SChristoph Hellwig !(flags & MAP_UNINITIALIZED))) 1187ea637639SJie Zhang memset((void *)region->vm_start, 0, 1188ea637639SJie Zhang region->vm_end - region->vm_start); 1189ea637639SJie Zhang 11901da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 11918feae131SDavid Howells result = vma->vm_start; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 11941da177e4SLinus Torvalds 11958feae131SDavid Howells share: 119607f1bc5aSLiam R. Howlett BUG_ON(!vma->vm_region); 1197b5df0922SLiam R. Howlett vma_iter_config(&vmi, vma->vm_start, vma->vm_end); 1198b5df0922SLiam R. Howlett if (vma_iter_prealloc(&vmi, vma)) 1199b5df0922SLiam R. Howlett goto error_just_free; 1200b5df0922SLiam R. Howlett 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); 12391279aa06SKefeng 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); 12451279aa06SKefeng Wang show_mem(); 12461da177e4SLinus Torvalds return -ENOMEM; 12471da177e4SLinus Torvalds } 12486be5ceb0SLinus Torvalds 1249a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, 1250a90f590aSDominik Brodowski unsigned long prot, unsigned long flags, 1251a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff) 125266f0dc48SHugh Dickins { 125366f0dc48SHugh Dickins struct file *file = NULL; 125466f0dc48SHugh Dickins unsigned long retval = -EBADF; 125566f0dc48SHugh Dickins 1256120a795dSAl Viro audit_mmap_fd(fd, flags); 125766f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 125866f0dc48SHugh Dickins file = fget(fd); 125966f0dc48SHugh Dickins if (!file) 126066f0dc48SHugh Dickins goto out; 126166f0dc48SHugh Dickins } 126266f0dc48SHugh Dickins 1263ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 126466f0dc48SHugh Dickins 126566f0dc48SHugh Dickins if (file) 126666f0dc48SHugh Dickins fput(file); 126766f0dc48SHugh Dickins out: 126866f0dc48SHugh Dickins return retval; 126966f0dc48SHugh Dickins } 127066f0dc48SHugh Dickins 1271a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1272a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags, 1273a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff) 1274a90f590aSDominik Brodowski { 1275a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); 1276a90f590aSDominik Brodowski } 1277a90f590aSDominik Brodowski 1278a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1279a4679373SChristoph Hellwig struct mmap_arg_struct { 1280a4679373SChristoph Hellwig unsigned long addr; 1281a4679373SChristoph Hellwig unsigned long len; 1282a4679373SChristoph Hellwig unsigned long prot; 1283a4679373SChristoph Hellwig unsigned long flags; 1284a4679373SChristoph Hellwig unsigned long fd; 1285a4679373SChristoph Hellwig unsigned long offset; 1286a4679373SChristoph Hellwig }; 1287a4679373SChristoph Hellwig 1288a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1289a4679373SChristoph Hellwig { 1290a4679373SChristoph Hellwig struct mmap_arg_struct a; 1291a4679373SChristoph Hellwig 1292a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1293a4679373SChristoph Hellwig return -EFAULT; 12941824cb75SAlexander Kuleshov if (offset_in_page(a.offset)) 1295a4679373SChristoph Hellwig return -EINVAL; 1296a4679373SChristoph Hellwig 1297a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1298a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1299a4679373SChristoph Hellwig } 1300a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1301a4679373SChristoph Hellwig 13021da177e4SLinus Torvalds /* 13038feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 13048feae131SDavid Howells * for the first part or the tail. 13051da177e4SLinus Torvalds */ 1306*adb20b0cSLorenzo Stoakes static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 13079760ebffSLiam R. Howlett unsigned long addr, int new_below) 13081da177e4SLinus Torvalds { 13098feae131SDavid Howells struct vm_area_struct *new; 13108feae131SDavid Howells struct vm_region *region; 13118feae131SDavid Howells unsigned long npages; 13129760ebffSLiam R. Howlett struct mm_struct *mm; 13131da177e4SLinus Torvalds 1314779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1315779c1023SDavid Howells * only a single usage on the region) */ 1316779c1023SDavid Howells if (vma->vm_file) 13178feae131SDavid Howells return -ENOMEM; 13181da177e4SLinus Torvalds 13199760ebffSLiam R. Howlett mm = vma->vm_mm; 13208feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 13218feae131SDavid Howells return -ENOMEM; 13221da177e4SLinus Torvalds 13238feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 13248feae131SDavid Howells if (!region) 13258feae131SDavid Howells return -ENOMEM; 13268feae131SDavid Howells 13273928d4f5SLinus Torvalds new = vm_area_dup(vma); 13288220543dSMatthew Wilcox (Oracle) if (!new) 13298220543dSMatthew Wilcox (Oracle) goto err_vma_dup; 13308220543dSMatthew Wilcox (Oracle) 13318feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 13328feae131SDavid Howells *region = *vma->vm_region; 13338feae131SDavid Howells new->vm_region = region; 13348feae131SDavid Howells 13358feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 13368feae131SDavid Howells 13378feae131SDavid Howells if (new_below) { 1338dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 13398feae131SDavid Howells } else { 13408feae131SDavid Howells region->vm_start = new->vm_start = addr; 13418feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 13421da177e4SLinus Torvalds } 13438feae131SDavid Howells 1344b5df0922SLiam R. Howlett vma_iter_config(vmi, new->vm_start, new->vm_end); 1345b5df0922SLiam R. Howlett if (vma_iter_prealloc(vmi, vma)) { 1346b5df0922SLiam R. Howlett pr_warn("Allocation of vma tree for process %d failed\n", 1347b5df0922SLiam R. Howlett current->pid); 1348b5df0922SLiam R. Howlett goto err_vmi_preallocate; 1349b5df0922SLiam R. Howlett } 1350b5df0922SLiam R. Howlett 13518feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 13528feae131SDavid Howells new->vm_ops->open(new); 13538feae131SDavid Howells 13548feae131SDavid Howells down_write(&nommu_region_sem); 13558feae131SDavid Howells delete_nommu_region(vma->vm_region); 13568feae131SDavid Howells if (new_below) { 13578feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 13588feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 13598feae131SDavid Howells } else { 13608feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1361dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 13628feae131SDavid Howells } 13638feae131SDavid Howells add_nommu_region(vma->vm_region); 13648feae131SDavid Howells add_nommu_region(new->vm_region); 13658feae131SDavid Howells up_write(&nommu_region_sem); 13668220543dSMatthew Wilcox (Oracle) 13678220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(vma, mm); 13688220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(new, mm); 136947d9644dSLiam R. Howlett vma_iter_store(vmi, new); 1370fd9edbdbSLiam Howlett mm->map_count++; 13718feae131SDavid Howells return 0; 13728220543dSMatthew Wilcox (Oracle) 137347d9644dSLiam R. Howlett err_vmi_preallocate: 13748220543dSMatthew Wilcox (Oracle) vm_area_free(new); 13758220543dSMatthew Wilcox (Oracle) err_vma_dup: 13768220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region); 13778220543dSMatthew Wilcox (Oracle) return -ENOMEM; 13788feae131SDavid Howells } 13798feae131SDavid Howells 13808feae131SDavid Howells /* 13818feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 13828feae131SDavid Howells * the end 13838feae131SDavid Howells */ 138407f1bc5aSLiam R. Howlett static int vmi_shrink_vma(struct vma_iterator *vmi, 13858feae131SDavid Howells struct vm_area_struct *vma, 13868feae131SDavid Howells unsigned long from, unsigned long to) 13878feae131SDavid Howells { 13888feae131SDavid Howells struct vm_region *region; 13898feae131SDavid Howells 13908feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 13918feae131SDavid Howells * and list */ 139207f1bc5aSLiam R. Howlett if (from > vma->vm_start) { 1393f72cf24aSLiam R. Howlett if (vma_iter_clear_gfp(vmi, from, vma->vm_end, GFP_KERNEL)) 1394f72cf24aSLiam R. Howlett return -ENOMEM; 13958feae131SDavid Howells vma->vm_end = from; 139607f1bc5aSLiam R. Howlett } else { 1397f72cf24aSLiam R. Howlett if (vma_iter_clear_gfp(vmi, vma->vm_start, to, GFP_KERNEL)) 1398f72cf24aSLiam R. Howlett return -ENOMEM; 13998feae131SDavid Howells vma->vm_start = to; 140007f1bc5aSLiam R. Howlett } 14018feae131SDavid Howells 14028feae131SDavid Howells /* cut the backing region down to size */ 14038feae131SDavid Howells region = vma->vm_region; 14041e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 14058feae131SDavid Howells 14068feae131SDavid Howells down_write(&nommu_region_sem); 14078feae131SDavid Howells delete_nommu_region(region); 1408dd8632a1SPaul Mundt if (from > region->vm_start) { 1409dd8632a1SPaul Mundt to = region->vm_top; 1410dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1411dd8632a1SPaul Mundt } else { 14128feae131SDavid Howells region->vm_start = to; 1413dd8632a1SPaul Mundt } 14148feae131SDavid Howells add_nommu_region(region); 14158feae131SDavid Howells up_write(&nommu_region_sem); 14168feae131SDavid Howells 14178feae131SDavid Howells free_page_series(from, to); 14188feae131SDavid Howells return 0; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14213034097aSDavid Howells /* 14223034097aSDavid Howells * release a mapping 14238feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 14248feae131SDavid Howells * VMA, though it need not cover the whole VMA 14253034097aSDavid Howells */ 1426897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) 14271da177e4SLinus Torvalds { 142847d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, start); 14298feae131SDavid Howells struct vm_area_struct *vma; 1430f67d9b15SBob Liu unsigned long end; 14318220543dSMatthew Wilcox (Oracle) int ret = 0; 14321da177e4SLinus Torvalds 1433f67d9b15SBob Liu len = PAGE_ALIGN(len); 14348feae131SDavid Howells if (len == 0) 14351da177e4SLinus Torvalds return -EINVAL; 14361da177e4SLinus Torvalds 1437f67d9b15SBob Liu end = start + len; 1438f67d9b15SBob Liu 14398feae131SDavid Howells /* find the first potentially overlapping VMA */ 144047d9644dSLiam R. Howlett vma = vma_find(&vmi, end); 14418feae131SDavid Howells if (!vma) { 1442ac714904SChoi Gi-yong static int limit; 144333e5d769SDavid Howells if (limit < 5) { 144422cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 144533e5d769SDavid Howells current->pid, current->comm, 144633e5d769SDavid Howells start, start + len - 1); 144733e5d769SDavid Howells limit++; 144833e5d769SDavid Howells } 14498feae131SDavid Howells return -EINVAL; 14508feae131SDavid Howells } 14511da177e4SLinus Torvalds 14528feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 14538feae131SDavid Howells if (vma->vm_file) { 14548feae131SDavid Howells do { 145522cc877bSLeon Romanovsky if (start > vma->vm_start) 14568feae131SDavid Howells return -EINVAL; 14578feae131SDavid Howells if (end == vma->vm_end) 14588feae131SDavid Howells goto erase_whole_vma; 145947d9644dSLiam R. Howlett vma = vma_find(&vmi, end); 1460d75a310cSNamhyung Kim } while (vma); 14618feae131SDavid Howells return -EINVAL; 14628feae131SDavid Howells } else { 14638feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 14648feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 14658feae131SDavid Howells goto erase_whole_vma; 146622cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 14678feae131SDavid Howells return -EINVAL; 14681824cb75SAlexander Kuleshov if (offset_in_page(start)) 14698feae131SDavid Howells return -EINVAL; 14701824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end)) 14718feae131SDavid Howells return -EINVAL; 14728feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 14739760ebffSLiam R. Howlett ret = split_vma(&vmi, vma, start, 1); 147422cc877bSLeon Romanovsky if (ret < 0) 14758feae131SDavid Howells return ret; 14768feae131SDavid Howells } 147707f1bc5aSLiam R. Howlett return vmi_shrink_vma(&vmi, vma, start, end); 14788feae131SDavid Howells } 14791da177e4SLinus Torvalds 14808feae131SDavid Howells erase_whole_vma: 14818220543dSMatthew Wilcox (Oracle) if (delete_vma_from_mm(vma)) 14828220543dSMatthew Wilcox (Oracle) ret = -ENOMEM; 148380be727eSLiam Howlett else 14848feae131SDavid Howells delete_vma(mm, vma); 14858220543dSMatthew Wilcox (Oracle) return ret; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 1488bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 14893034097aSDavid Howells { 1490bfce281cSAl Viro struct mm_struct *mm = current->mm; 14913034097aSDavid Howells int ret; 14923034097aSDavid Howells 1493d8ed45c5SMichel Lespinasse mmap_write_lock(mm); 1494897ab3e0SMike Rapoport ret = do_munmap(mm, addr, len, NULL); 1495d8ed45c5SMichel Lespinasse mmap_write_unlock(mm); 14963034097aSDavid Howells return ret; 14973034097aSDavid Howells } 1498a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1499a46ef99dSLinus Torvalds 1500a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1501a46ef99dSLinus Torvalds { 1502bfce281cSAl Viro return vm_munmap(addr, len); 1503a46ef99dSLinus Torvalds } 15043034097aSDavid Howells 15053034097aSDavid Howells /* 15068feae131SDavid Howells * release all the mappings made in a process's VM space 15073034097aSDavid Howells */ 15081da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 15091da177e4SLinus Torvalds { 15108220543dSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0); 15118feae131SDavid Howells struct vm_area_struct *vma; 15121da177e4SLinus Torvalds 15138feae131SDavid Howells if (!mm) 15148feae131SDavid Howells return; 15158feae131SDavid Howells 15161da177e4SLinus Torvalds mm->total_vm = 0; 15171da177e4SLinus Torvalds 15188220543dSMatthew Wilcox (Oracle) /* 15198220543dSMatthew Wilcox (Oracle) * Lock the mm to avoid assert complaining even though this is the only 15208220543dSMatthew Wilcox (Oracle) * user of the mm 15218220543dSMatthew Wilcox (Oracle) */ 15228220543dSMatthew Wilcox (Oracle) mmap_write_lock(mm); 15238220543dSMatthew Wilcox (Oracle) for_each_vma(vmi, vma) { 15248220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma); 15258feae131SDavid Howells delete_vma(mm, vma); 152604c34961SSteven J. Magnani cond_resched(); 15271da177e4SLinus Torvalds } 1528524e00b3SLiam R. Howlett __mt_destroy(&mm->mm_mt); 15298220543dSMatthew Wilcox (Oracle) mmap_write_unlock(mm); 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds /* 15336fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 15346fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 15351da177e4SLinus Torvalds * 15366fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 15378feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 15388feae131SDavid Howells * block is not shareable 15391da177e4SLinus Torvalds * 15406fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 15411da177e4SLinus Torvalds */ 15424b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 15431da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 15441da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 15451da177e4SLinus Torvalds { 15466fa5f80bSDavid Howells struct vm_area_struct *vma; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* insanity checks first */ 1549f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1550f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 15518feae131SDavid Howells if (old_len == 0 || new_len == 0) 15521da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15531da177e4SLinus Torvalds 15541824cb75SAlexander Kuleshov if (offset_in_page(addr)) 15558feae131SDavid Howells return -EINVAL; 15568feae131SDavid Howells 15571da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 15581da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15591da177e4SLinus Torvalds 15608feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 15616fa5f80bSDavid Howells if (!vma) 15621da177e4SLinus Torvalds return (unsigned long) -EINVAL; 15631da177e4SLinus Torvalds 15646fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 15651da177e4SLinus Torvalds return (unsigned long) -EFAULT; 15661da177e4SLinus Torvalds 1567fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vma->vm_flags)) 15681da177e4SLinus Torvalds return (unsigned long) -EPERM; 15691da177e4SLinus Torvalds 15708feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 15711da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds /* all checks complete - do it */ 15746fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 15756fa5f80bSDavid Howells return vma->vm_start; 15766fa5f80bSDavid Howells } 15776fa5f80bSDavid Howells 15786a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 15796a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 15806a6160a7SHeiko Carstens unsigned long, new_addr) 15816fa5f80bSDavid Howells { 15826fa5f80bSDavid Howells unsigned long ret; 15836fa5f80bSDavid Howells 1584d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 15856fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 1586d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 15876fa5f80bSDavid Howells return ret; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 1590df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1591df06b37fSKeith Busch unsigned int foll_flags) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds return NULL; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15968f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 15978f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 15981da177e4SLinus Torvalds { 15998f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 16008f3b1327SBob Liu return -EINVAL; 16018f3b1327SBob Liu 16021c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 160366aa2b4bSGreg Ungerer return 0; 16041da177e4SLinus Torvalds } 160522c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 16061da177e4SLinus Torvalds 16073c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 16083c0b9de6SLinus Torvalds { 16093c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 16103c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 16113c0b9de6SLinus Torvalds 16123c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 16133c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 16143c0b9de6SLinus Torvalds } 16153c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 16163c0b9de6SLinus Torvalds 1617f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1618f905bc44SPaul Mundt unsigned long pgoff) 1619f905bc44SPaul Mundt { 1620f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1621f905bc44SPaul Mundt 1622f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1623f905bc44SPaul Mundt return -EINVAL; 1624f905bc44SPaul Mundt 1625f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1626f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1627f905bc44SPaul Mundt 1628f905bc44SPaul Mundt return 0; 1629f905bc44SPaul Mundt } 1630f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1631f905bc44SPaul Mundt 16322bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf) 1633b0e15190SDavid Howells { 1634b0e15190SDavid Howells BUG(); 1635d0217ac0SNick Piggin return 0; 1636b0e15190SDavid Howells } 1637b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 16380ec76a11SDavid Howells 16393f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf, 1640bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff) 1641f1820361SKirill A. Shutemov { 1642f1820361SKirill A. Shutemov BUG(); 16433f98a28cSGeert Uytterhoeven return 0; 1644f1820361SKirill A. Shutemov } 1645f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1646f1820361SKirill A. Shutemov 1647c43cfa42SLorenzo Stoakes static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, 1648c43cfa42SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 16490ec76a11SDavid Howells { 16500ec76a11SDavid Howells struct vm_area_struct *vma; 1651442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 16520ec76a11SDavid Howells 1653d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm)) 16541e426fe2SKonstantin Khlebnikov return 0; 16550ec76a11SDavid Howells 16560ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 16570159b141SDavid Howells vma = find_vma(mm, addr); 16580159b141SDavid Howells if (vma) { 16590ec76a11SDavid Howells /* don't overrun this mapping */ 16600ec76a11SDavid Howells if (addr + len >= vma->vm_end) 16610ec76a11SDavid Howells len = vma->vm_end - addr; 16620ec76a11SDavid Howells 16630ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1664d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 16657959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 16667959722bSJie Zhang (void *) addr, buf, len); 1667d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 16687959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 16697959722bSJie Zhang buf, (void *) addr, len); 16700ec76a11SDavid Howells else 16710ec76a11SDavid Howells len = 0; 16720ec76a11SDavid Howells } else { 16730ec76a11SDavid Howells len = 0; 16740ec76a11SDavid Howells } 16750ec76a11SDavid Howells 1676d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 1677f55f199bSMike Frysinger 1678f55f199bSMike Frysinger return len; 1679f55f199bSMike Frysinger } 1680f55f199bSMike Frysinger 1681f55f199bSMike Frysinger /** 1682b7701a5fSMike Rapoport * access_remote_vm - access another process' address space 1683f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1684f55f199bSMike Frysinger * @addr: start address to access 1685f55f199bSMike Frysinger * @buf: source or destination buffer 1686f55f199bSMike Frysinger * @len: number of bytes to transfer 16876347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 1688f55f199bSMike Frysinger * 1689f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1690f55f199bSMike Frysinger */ 1691f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 16926347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 1693f55f199bSMike Frysinger { 1694d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags); 1695f55f199bSMike Frysinger } 1696f55f199bSMike Frysinger 1697f55f199bSMike Frysinger /* 1698f55f199bSMike Frysinger * Access another process' address space. 1699f55f199bSMike Frysinger * - source/target buffer must be kernel space 1700f55f199bSMike Frysinger */ 1701f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, 1702f307ab6dSLorenzo Stoakes unsigned int gup_flags) 1703f55f199bSMike Frysinger { 1704f55f199bSMike Frysinger struct mm_struct *mm; 1705f55f199bSMike Frysinger 1706f55f199bSMike Frysinger if (addr + len < addr) 1707f55f199bSMike Frysinger return 0; 1708f55f199bSMike Frysinger 1709f55f199bSMike Frysinger mm = get_task_mm(tsk); 1710f55f199bSMike Frysinger if (!mm) 1711f55f199bSMike Frysinger return 0; 1712f55f199bSMike Frysinger 1713d3f5ffcaSJohn Hubbard len = __access_remote_vm(mm, addr, buf, len, gup_flags); 1714f55f199bSMike Frysinger 17150ec76a11SDavid Howells mmput(mm); 17160ec76a11SDavid Howells return len; 17170ec76a11SDavid Howells } 1718fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 17197e660872SDavid Howells 17207e660872SDavid Howells /** 17217e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 17227e660872SDavid Howells * @inode: The inode to check 17237e660872SDavid Howells * @size: The current filesize of the inode 17247e660872SDavid Howells * @newsize: The proposed filesize of the inode 17257e660872SDavid Howells * 17267e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 1727c08b342cSRandy Dunlap * make sure that any outstanding VMAs aren't broken and then shrink the 1728c08b342cSRandy Dunlap * vm_regions that extend beyond so that do_mmap() doesn't 17297e660872SDavid Howells * automatically grant mappings that are too large. 17307e660872SDavid Howells */ 17317e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 17327e660872SDavid Howells size_t newsize) 17337e660872SDavid Howells { 17347e660872SDavid Howells struct vm_area_struct *vma; 17357e660872SDavid Howells struct vm_region *region; 17367e660872SDavid Howells pgoff_t low, high; 17377e660872SDavid Howells size_t r_size, r_top; 17387e660872SDavid Howells 17397e660872SDavid Howells low = newsize >> PAGE_SHIFT; 17407e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 17417e660872SDavid Howells 17427e660872SDavid Howells down_write(&nommu_region_sem); 17431acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 17447e660872SDavid Howells 17457e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 17466b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 17477e660872SDavid Howells /* found one - only interested if it's shared out of the page 17487e660872SDavid Howells * cache */ 17497e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 17501acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17517e660872SDavid Howells up_write(&nommu_region_sem); 17527e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 17537e660872SDavid Howells } 17547e660872SDavid Howells } 17557e660872SDavid Howells 17567e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 17577e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 17587e660872SDavid Howells * 17597e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 17607e660872SDavid Howells * shouldn't be any 17617e660872SDavid Howells */ 17621acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 17637e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 17647e660872SDavid Howells continue; 17657e660872SDavid Howells 17667e660872SDavid Howells region = vma->vm_region; 17677e660872SDavid Howells r_size = region->vm_top - region->vm_start; 17687e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 17697e660872SDavid Howells 17707e660872SDavid Howells if (r_top > newsize) { 17717e660872SDavid Howells region->vm_top -= r_top - newsize; 17727e660872SDavid Howells if (region->vm_end > region->vm_top) 17737e660872SDavid Howells region->vm_end = region->vm_top; 17747e660872SDavid Howells } 17757e660872SDavid Howells } 17767e660872SDavid Howells 17771acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 17787e660872SDavid Howells up_write(&nommu_region_sem); 17797e660872SDavid Howells return 0; 17807e660872SDavid Howells } 1781c9b1d098SAndrew Shewmaker 1782c9b1d098SAndrew Shewmaker /* 1783c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 1784c9b1d098SAndrew Shewmaker * 1785c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 1786c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 1787c9b1d098SAndrew Shewmaker * mode. 1788c9b1d098SAndrew Shewmaker * 1789c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 1790c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 1791c9b1d098SAndrew Shewmaker */ 1792c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 1793c9b1d098SAndrew Shewmaker { 1794c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 1795c9b1d098SAndrew Shewmaker 1796d5a6474dSZhangPeng free_kbytes = K(global_zone_page_state(NR_FREE_PAGES)); 1797c9b1d098SAndrew Shewmaker 1798c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 1799c9b1d098SAndrew Shewmaker return 0; 1800c9b1d098SAndrew Shewmaker } 1801a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve); 18024eeab4f5SAndrew Shewmaker 18034eeab4f5SAndrew Shewmaker /* 18044eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 18054eeab4f5SAndrew Shewmaker * 18064eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 18074eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 18084eeab4f5SAndrew Shewmaker * 18094eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 18104eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 18114eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 18124eeab4f5SAndrew Shewmaker */ 18134eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 18144eeab4f5SAndrew Shewmaker { 18154eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 18164eeab4f5SAndrew Shewmaker 1817d5a6474dSZhangPeng free_kbytes = K(global_zone_page_state(NR_FREE_PAGES)); 18184eeab4f5SAndrew Shewmaker 18194eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 18204eeab4f5SAndrew Shewmaker return 0; 18214eeab4f5SAndrew Shewmaker } 1822a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve); 1823