11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/nommu.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Replacement code for mm functions to support CPU's that don't 51da177e4SLinus Torvalds * have any form of memory management unit (thus no virtual memory). 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * See Documentation/nommu-mmap.txt 81da177e4SLinus Torvalds * 98feae131SDavid Howells * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com> 101da177e4SLinus Torvalds * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com> 111da177e4SLinus Torvalds * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org> 121da177e4SLinus Torvalds * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com> 13*eb6434d9SPaul Mundt * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org> 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 16f2b8544fSDavid Howells #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/mm.h> 181da177e4SLinus Torvalds #include <linux/mman.h> 191da177e4SLinus Torvalds #include <linux/swap.h> 201da177e4SLinus Torvalds #include <linux/file.h> 211da177e4SLinus Torvalds #include <linux/highmem.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 231da177e4SLinus Torvalds #include <linux/slab.h> 241da177e4SLinus Torvalds #include <linux/vmalloc.h> 25fa8e26ccSRoland McGrath #include <linux/tracehook.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/personality.h> 301da177e4SLinus Torvalds #include <linux/security.h> 311da177e4SLinus Torvalds #include <linux/syscalls.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <asm/uaccess.h> 341da177e4SLinus Torvalds #include <asm/tlb.h> 351da177e4SLinus Torvalds #include <asm/tlbflush.h> 368feae131SDavid Howells #include "internal.h" 378feae131SDavid Howells 388feae131SDavid Howells static inline __attribute__((format(printf, 1, 2))) 398feae131SDavid Howells void no_printk(const char *fmt, ...) 408feae131SDavid Howells { 418feae131SDavid Howells } 428feae131SDavid Howells 438feae131SDavid Howells #if 0 448feae131SDavid Howells #define kenter(FMT, ...) \ 458feae131SDavid Howells printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) 468feae131SDavid Howells #define kleave(FMT, ...) \ 478feae131SDavid Howells printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) 488feae131SDavid Howells #define kdebug(FMT, ...) \ 498feae131SDavid Howells printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__) 508feae131SDavid Howells #else 518feae131SDavid Howells #define kenter(FMT, ...) \ 528feae131SDavid Howells no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) 538feae131SDavid Howells #define kleave(FMT, ...) \ 548feae131SDavid Howells no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) 558feae131SDavid Howells #define kdebug(FMT, ...) \ 568feae131SDavid Howells no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__) 578feae131SDavid Howells #endif 581da177e4SLinus Torvalds 59b291f000SNick Piggin #include "internal.h" 60b291f000SNick Piggin 611da177e4SLinus Torvalds void *high_memory; 621da177e4SLinus Torvalds struct page *mem_map; 631da177e4SLinus Torvalds unsigned long max_mapnr; 641da177e4SLinus Torvalds unsigned long num_physpages; 6580119ef5SAlan Cox atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0); 661da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */ 671da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */ 681da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; 69dd8632a1SPaul Mundt int sysctl_nr_trim_pages = 1; /* page trimming behaviour */ 701da177e4SLinus Torvalds int heap_stack_gap = 0; 711da177e4SLinus Torvalds 728feae131SDavid Howells atomic_t mmap_pages_allocated; 738feae131SDavid Howells 741da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 756a04de6dSWu, Bryan EXPORT_SYMBOL(num_physpages); 761da177e4SLinus Torvalds 778feae131SDavid Howells /* list of mapped, potentially shareable regions */ 788feae131SDavid Howells static struct kmem_cache *vm_region_jar; 798feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT; 808feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds struct vm_operations_struct generic_file_vm_ops = { 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* 861da177e4SLinus Torvalds * Handle all mappings that got truncated by a "truncate()" 871da177e4SLinus Torvalds * system call. 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * NOTE! We have to be ready to update the memory sharing 901da177e4SLinus Torvalds * between the file and the memory map for a potential last 911da177e4SLinus Torvalds * incomplete page. Ugly, but necessary. 921da177e4SLinus Torvalds */ 931da177e4SLinus Torvalds int vmtruncate(struct inode *inode, loff_t offset) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 961da177e4SLinus Torvalds unsigned long limit; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds if (inode->i_size < offset) 991da177e4SLinus Torvalds goto do_expand; 1001da177e4SLinus Torvalds i_size_write(inode, offset); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds truncate_inode_pages(mapping, offset); 1031da177e4SLinus Torvalds goto out_truncate; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds do_expand: 1061da177e4SLinus Torvalds limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 1071da177e4SLinus Torvalds if (limit != RLIM_INFINITY && offset > limit) 1081da177e4SLinus Torvalds goto out_sig; 1091da177e4SLinus Torvalds if (offset > inode->i_sb->s_maxbytes) 1101da177e4SLinus Torvalds goto out; 1111da177e4SLinus Torvalds i_size_write(inode, offset); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds out_truncate: 114acfa4380SAl Viro if (inode->i_op->truncate) 1151da177e4SLinus Torvalds inode->i_op->truncate(inode); 1161da177e4SLinus Torvalds return 0; 1171da177e4SLinus Torvalds out_sig: 1181da177e4SLinus Torvalds send_sig(SIGXFSZ, current, 0); 1191da177e4SLinus Torvalds out: 1201da177e4SLinus Torvalds return -EFBIG; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds EXPORT_SYMBOL(vmtruncate); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Return the total memory allocated for this pointer, not 1271da177e4SLinus Torvalds * just what the caller asked for. 1281da177e4SLinus Torvalds * 1291da177e4SLinus Torvalds * Doesn't have to be accurate, i.e. may have races. 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds unsigned int kobjsize(const void *objp) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds struct page *page; 1341da177e4SLinus Torvalds 1354016a139SMichael Hennerich /* 1364016a139SMichael Hennerich * If the object we have should not have ksize performed on it, 1374016a139SMichael Hennerich * return size of 0 1384016a139SMichael Hennerich */ 1395a1603beSPaul Mundt if (!objp || !virt_addr_valid(objp)) 1406cfd53fcSPaul Mundt return 0; 1416cfd53fcSPaul Mundt 1426cfd53fcSPaul Mundt page = virt_to_head_page(objp); 1436cfd53fcSPaul Mundt 1446cfd53fcSPaul Mundt /* 1456cfd53fcSPaul Mundt * If the allocator sets PageSlab, we know the pointer came from 1466cfd53fcSPaul Mundt * kmalloc(). 1476cfd53fcSPaul Mundt */ 1481da177e4SLinus Torvalds if (PageSlab(page)) 1491da177e4SLinus Torvalds return ksize(objp); 1501da177e4SLinus Torvalds 1516cfd53fcSPaul Mundt /* 152ab2e83eaSPaul Mundt * If it's not a compound page, see if we have a matching VMA 153ab2e83eaSPaul Mundt * region. This test is intentionally done in reverse order, 154ab2e83eaSPaul Mundt * so if there's no VMA, we still fall through and hand back 155ab2e83eaSPaul Mundt * PAGE_SIZE for 0-order pages. 156ab2e83eaSPaul Mundt */ 157ab2e83eaSPaul Mundt if (!PageCompound(page)) { 158ab2e83eaSPaul Mundt struct vm_area_struct *vma; 159ab2e83eaSPaul Mundt 160ab2e83eaSPaul Mundt vma = find_vma(current->mm, (unsigned long)objp); 161ab2e83eaSPaul Mundt if (vma) 162ab2e83eaSPaul Mundt return vma->vm_end - vma->vm_start; 163ab2e83eaSPaul Mundt } 164ab2e83eaSPaul Mundt 165ab2e83eaSPaul Mundt /* 1666cfd53fcSPaul Mundt * The ksize() function is only guaranteed to work for pointers 1675a1603beSPaul Mundt * returned by kmalloc(). So handle arbitrary pointers here. 1686cfd53fcSPaul Mundt */ 1695a1603beSPaul Mundt return PAGE_SIZE << compound_order(page); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 172b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 173b291f000SNick Piggin unsigned long start, int len, int flags, 1741da177e4SLinus Torvalds struct page **pages, struct vm_area_struct **vmas) 1751da177e4SLinus Torvalds { 176910e46daSSonic Zhang struct vm_area_struct *vma; 1777b4d5b8bSDavid Howells unsigned long vm_flags; 1787b4d5b8bSDavid Howells int i; 179b291f000SNick Piggin int write = !!(flags & GUP_FLAGS_WRITE); 180b291f000SNick Piggin int force = !!(flags & GUP_FLAGS_FORCE); 181b291f000SNick Piggin int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS); 1827b4d5b8bSDavid Howells 1837b4d5b8bSDavid Howells /* calculate required read or write permissions. 1847b4d5b8bSDavid Howells * - if 'force' is set, we only require the "MAY" flags. 1857b4d5b8bSDavid Howells */ 1867b4d5b8bSDavid Howells vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 1877b4d5b8bSDavid Howells vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds for (i = 0; i < len; i++) { 190910e46daSSonic Zhang vma = find_vma(mm, start); 191910e46daSSonic Zhang if (!vma) 1927b4d5b8bSDavid Howells goto finish_or_fault; 1937b4d5b8bSDavid Howells 1947b4d5b8bSDavid Howells /* protect what we can, including chardevs */ 1957b4d5b8bSDavid Howells if (vma->vm_flags & (VM_IO | VM_PFNMAP) || 196b291f000SNick Piggin (!ignore && !(vm_flags & vma->vm_flags))) 1977b4d5b8bSDavid Howells goto finish_or_fault; 198910e46daSSonic Zhang 1991da177e4SLinus Torvalds if (pages) { 2001da177e4SLinus Torvalds pages[i] = virt_to_page(start); 2011da177e4SLinus Torvalds if (pages[i]) 2021da177e4SLinus Torvalds page_cache_get(pages[i]); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds if (vmas) 205910e46daSSonic Zhang vmas[i] = vma; 2061da177e4SLinus Torvalds start += PAGE_SIZE; 2071da177e4SLinus Torvalds } 2087b4d5b8bSDavid Howells 2097b4d5b8bSDavid Howells return i; 2107b4d5b8bSDavid Howells 2117b4d5b8bSDavid Howells finish_or_fault: 2127b4d5b8bSDavid Howells return i ? : -EFAULT; 2131da177e4SLinus Torvalds } 214b291f000SNick Piggin 215b291f000SNick Piggin 216b291f000SNick Piggin /* 217b291f000SNick Piggin * get a list of pages in an address range belonging to the specified process 218b291f000SNick Piggin * and indicate the VMA that covers each page 219b291f000SNick Piggin * - this is potentially dodgy as we may end incrementing the page count of a 220b291f000SNick Piggin * slab page or a secondary page from a compound page 221b291f000SNick Piggin * - don't permit access to VMAs that don't support it, such as I/O mappings 222b291f000SNick Piggin */ 223b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 224b291f000SNick Piggin unsigned long start, int len, int write, int force, 225b291f000SNick Piggin struct page **pages, struct vm_area_struct **vmas) 226b291f000SNick Piggin { 227b291f000SNick Piggin int flags = 0; 228b291f000SNick Piggin 229b291f000SNick Piggin if (write) 230b291f000SNick Piggin flags |= GUP_FLAGS_WRITE; 231b291f000SNick Piggin if (force) 232b291f000SNick Piggin flags |= GUP_FLAGS_FORCE; 233b291f000SNick Piggin 234b291f000SNick Piggin return __get_user_pages(tsk, mm, 235b291f000SNick Piggin start, len, flags, 236b291f000SNick Piggin pages, vmas); 237b291f000SNick Piggin } 23866aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages); 23966aa2b4bSGreg Ungerer 2401da177e4SLinus Torvalds DEFINE_RWLOCK(vmlist_lock); 2411da177e4SLinus Torvalds struct vm_struct *vmlist; 2421da177e4SLinus Torvalds 243b3bdda02SChristoph Lameter void vfree(const void *addr) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds kfree(addr); 2461da177e4SLinus Torvalds } 247b5073173SPaul Mundt EXPORT_SYMBOL(vfree); 2481da177e4SLinus Torvalds 249dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds /* 2528518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 2538518609dSRobert P. J. Day * returns only a logical address. 2541da177e4SLinus Torvalds */ 25584097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 2561da177e4SLinus Torvalds } 257b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc); 2581da177e4SLinus Torvalds 259f905bc44SPaul Mundt void *vmalloc_user(unsigned long size) 260f905bc44SPaul Mundt { 261f905bc44SPaul Mundt void *ret; 262f905bc44SPaul Mundt 263f905bc44SPaul Mundt ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 264f905bc44SPaul Mundt PAGE_KERNEL); 265f905bc44SPaul Mundt if (ret) { 266f905bc44SPaul Mundt struct vm_area_struct *vma; 267f905bc44SPaul Mundt 268f905bc44SPaul Mundt down_write(¤t->mm->mmap_sem); 269f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret); 270f905bc44SPaul Mundt if (vma) 271f905bc44SPaul Mundt vma->vm_flags |= VM_USERMAP; 272f905bc44SPaul Mundt up_write(¤t->mm->mmap_sem); 273f905bc44SPaul Mundt } 274f905bc44SPaul Mundt 275f905bc44SPaul Mundt return ret; 276f905bc44SPaul Mundt } 277f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user); 278f905bc44SPaul Mundt 279b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds return virt_to_page(addr); 2821da177e4SLinus Torvalds } 283b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page); 2841da177e4SLinus Torvalds 285b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr)); 2881da177e4SLinus Torvalds } 289b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn); 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds memcpy(buf, addr, count); 2941da177e4SLinus Torvalds return count; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds /* Don't allow overflow */ 3001da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 3011da177e4SLinus Torvalds count = -(unsigned long) addr; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds memcpy(addr, buf, count); 3041da177e4SLinus Torvalds return(count); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * vmalloc - allocate virtually continguos memory 3091da177e4SLinus Torvalds * 3101da177e4SLinus Torvalds * @size: allocation size 3111da177e4SLinus Torvalds * 3121da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 3131da177e4SLinus Torvalds * allocator and map them into continguos kernel virtual space. 3141da177e4SLinus Torvalds * 315c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 3161da177e4SLinus Torvalds * use __vmalloc() instead. 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds void *vmalloc(unsigned long size) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL); 3211da177e4SLinus Torvalds } 322f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc); 323f6138882SAndrew Morton 324f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node) 325f6138882SAndrew Morton { 326f6138882SAndrew Morton return vmalloc(size); 327f6138882SAndrew Morton } 328f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc_node); 3291da177e4SLinus Torvalds 3301af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC 3311af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL 3321af446edSPaul Mundt #endif 3331af446edSPaul Mundt 3341af446edSPaul Mundt /** 3351af446edSPaul Mundt * vmalloc_exec - allocate virtually contiguous, executable memory 3361af446edSPaul Mundt * @size: allocation size 3371af446edSPaul Mundt * 3381af446edSPaul Mundt * Kernel-internal function to allocate enough pages to cover @size 3391af446edSPaul Mundt * the page level allocator and map them into contiguous and 3401af446edSPaul Mundt * executable kernel virtual space. 3411af446edSPaul Mundt * 3421af446edSPaul Mundt * For tight control over page level allocator and protection flags 3431af446edSPaul Mundt * use __vmalloc() instead. 3441af446edSPaul Mundt */ 3451af446edSPaul Mundt 3461af446edSPaul Mundt void *vmalloc_exec(unsigned long size) 3471af446edSPaul Mundt { 3481af446edSPaul Mundt return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC); 3491af446edSPaul Mundt } 3501af446edSPaul Mundt 351b5073173SPaul Mundt /** 352b5073173SPaul Mundt * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 3531da177e4SLinus Torvalds * @size: allocation size 3541da177e4SLinus Torvalds * 3551da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 3561da177e4SLinus Torvalds * page level allocator and map them into continguos kernel virtual space. 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL); 3611da177e4SLinus Torvalds } 362b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32); 363b5073173SPaul Mundt 364b5073173SPaul Mundt /** 365b5073173SPaul Mundt * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 366b5073173SPaul Mundt * @size: allocation size 367b5073173SPaul Mundt * 368b5073173SPaul Mundt * The resulting memory area is 32bit addressable and zeroed so it can be 369b5073173SPaul Mundt * mapped to userspace without leaking data. 370f905bc44SPaul Mundt * 371f905bc44SPaul Mundt * VM_USERMAP is set on the corresponding VMA so that subsequent calls to 372f905bc44SPaul Mundt * remap_vmalloc_range() are permissible. 373b5073173SPaul Mundt */ 374b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size) 375b5073173SPaul Mundt { 376f905bc44SPaul Mundt /* 377f905bc44SPaul Mundt * We'll have to sort out the ZONE_DMA bits for 64-bit, 378f905bc44SPaul Mundt * but for now this can simply use vmalloc_user() directly. 379f905bc44SPaul Mundt */ 380f905bc44SPaul Mundt return vmalloc_user(size); 381b5073173SPaul Mundt } 382b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user); 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds BUG(); 3871da177e4SLinus Torvalds return NULL; 3881da177e4SLinus Torvalds } 389b5073173SPaul Mundt EXPORT_SYMBOL(vmap); 3901da177e4SLinus Torvalds 391b3bdda02SChristoph Lameter void vunmap(const void *addr) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds BUG(); 3941da177e4SLinus Torvalds } 395b5073173SPaul Mundt EXPORT_SYMBOL(vunmap); 3961da177e4SLinus Torvalds 397*eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 398*eb6434d9SPaul Mundt { 399*eb6434d9SPaul Mundt BUG(); 400*eb6434d9SPaul Mundt return NULL; 401*eb6434d9SPaul Mundt } 402*eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram); 403*eb6434d9SPaul Mundt 404*eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count) 405*eb6434d9SPaul Mundt { 406*eb6434d9SPaul Mundt BUG(); 407*eb6434d9SPaul Mundt } 408*eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram); 409*eb6434d9SPaul Mundt 410*eb6434d9SPaul Mundt void vm_unmap_aliases(void) 411*eb6434d9SPaul Mundt { 412*eb6434d9SPaul Mundt } 413*eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases); 414*eb6434d9SPaul Mundt 4151da177e4SLinus Torvalds /* 4161eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 4171eeb66a1SChristoph Hellwig * have one. 4181eeb66a1SChristoph Hellwig */ 4191eeb66a1SChristoph Hellwig void __attribute__((weak)) vmalloc_sync_all(void) 4201eeb66a1SChristoph Hellwig { 4211eeb66a1SChristoph Hellwig } 4221eeb66a1SChristoph Hellwig 423b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 424b5073173SPaul Mundt struct page *page) 425b5073173SPaul Mundt { 426b5073173SPaul Mundt return -EINVAL; 427b5073173SPaul Mundt } 428b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page); 429b5073173SPaul Mundt 4301eeb66a1SChristoph Hellwig /* 4311da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel 4321da177e4SLinus Torvalds * lock, except when an application is doing something nasty 4331da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped 4341da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need 4351da177e4SLinus Torvalds * to invoke file system routines that need the global lock. 4361da177e4SLinus Torvalds */ 4376a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds if (brk < mm->start_brk || brk > mm->context.end_brk) 4421da177e4SLinus Torvalds return mm->brk; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds if (mm->brk == brk) 4451da177e4SLinus Torvalds return mm->brk; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* 4481da177e4SLinus Torvalds * Always allow shrinking brk 4491da177e4SLinus Torvalds */ 4501da177e4SLinus Torvalds if (brk <= mm->brk) { 4511da177e4SLinus Torvalds mm->brk = brk; 4521da177e4SLinus Torvalds return brk; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * Ok, looks good - let it rip. 4571da177e4SLinus Torvalds */ 4581da177e4SLinus Torvalds return mm->brk = brk; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4618feae131SDavid Howells /* 4628feae131SDavid Howells * initialise the VMA and region record slabs 4638feae131SDavid Howells */ 4648feae131SDavid Howells void __init mmap_init(void) 4651da177e4SLinus Torvalds { 4668feae131SDavid Howells vm_region_jar = kmem_cache_create("vm_region_jar", 4678feae131SDavid Howells sizeof(struct vm_region), 0, 4688feae131SDavid Howells SLAB_PANIC, NULL); 4698feae131SDavid Howells vm_area_cachep = kmem_cache_create("vm_area_struct", 4708feae131SDavid Howells sizeof(struct vm_area_struct), 0, 4718feae131SDavid Howells SLAB_PANIC, NULL); 4728feae131SDavid Howells } 4731da177e4SLinus Torvalds 4748feae131SDavid Howells /* 4758feae131SDavid Howells * validate the region tree 4768feae131SDavid Howells * - the caller must hold the region lock 4778feae131SDavid Howells */ 4788feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS 4798feae131SDavid Howells static noinline void validate_nommu_regions(void) 4808feae131SDavid Howells { 4818feae131SDavid Howells struct vm_region *region, *last; 4828feae131SDavid Howells struct rb_node *p, *lastp; 4831da177e4SLinus Torvalds 4848feae131SDavid Howells lastp = rb_first(&nommu_region_tree); 4858feae131SDavid Howells if (!lastp) 4868feae131SDavid Howells return; 4878feae131SDavid Howells 4888feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 4898feae131SDavid Howells if (unlikely(last->vm_end <= last->vm_start)) 4908feae131SDavid Howells BUG(); 491dd8632a1SPaul Mundt if (unlikely(last->vm_top < last->vm_end)) 492dd8632a1SPaul Mundt BUG(); 4938feae131SDavid Howells 4948feae131SDavid Howells while ((p = rb_next(lastp))) { 4958feae131SDavid Howells region = rb_entry(p, struct vm_region, vm_rb); 4968feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 4978feae131SDavid Howells 4988feae131SDavid Howells if (unlikely(region->vm_end <= region->vm_start)) 4998feae131SDavid Howells BUG(); 500dd8632a1SPaul Mundt if (unlikely(region->vm_top < region->vm_end)) 501dd8632a1SPaul Mundt BUG(); 502dd8632a1SPaul Mundt if (unlikely(region->vm_start < last->vm_top)) 5038feae131SDavid Howells BUG(); 5048feae131SDavid Howells 5058feae131SDavid Howells lastp = p; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds } 5088feae131SDavid Howells #else 5098feae131SDavid Howells #define validate_nommu_regions() do {} while(0) 5108feae131SDavid Howells #endif 5118feae131SDavid Howells 5128feae131SDavid Howells /* 5138feae131SDavid Howells * add a region into the global tree 5148feae131SDavid Howells */ 5158feae131SDavid Howells static void add_nommu_region(struct vm_region *region) 5168feae131SDavid Howells { 5178feae131SDavid Howells struct vm_region *pregion; 5188feae131SDavid Howells struct rb_node **p, *parent; 5198feae131SDavid Howells 5208feae131SDavid Howells validate_nommu_regions(); 5218feae131SDavid Howells 5228feae131SDavid Howells BUG_ON(region->vm_start & ~PAGE_MASK); 5238feae131SDavid Howells 5248feae131SDavid Howells parent = NULL; 5258feae131SDavid Howells p = &nommu_region_tree.rb_node; 5268feae131SDavid Howells while (*p) { 5278feae131SDavid Howells parent = *p; 5288feae131SDavid Howells pregion = rb_entry(parent, struct vm_region, vm_rb); 5298feae131SDavid Howells if (region->vm_start < pregion->vm_start) 5308feae131SDavid Howells p = &(*p)->rb_left; 5318feae131SDavid Howells else if (region->vm_start > pregion->vm_start) 5328feae131SDavid Howells p = &(*p)->rb_right; 5338feae131SDavid Howells else if (pregion == region) 5348feae131SDavid Howells return; 5358feae131SDavid Howells else 5368feae131SDavid Howells BUG(); 5378feae131SDavid Howells } 5388feae131SDavid Howells 5398feae131SDavid Howells rb_link_node(®ion->vm_rb, parent, p); 5408feae131SDavid Howells rb_insert_color(®ion->vm_rb, &nommu_region_tree); 5418feae131SDavid Howells 5428feae131SDavid Howells validate_nommu_regions(); 5438feae131SDavid Howells } 5448feae131SDavid Howells 5458feae131SDavid Howells /* 5468feae131SDavid Howells * delete a region from the global tree 5478feae131SDavid Howells */ 5488feae131SDavid Howells static void delete_nommu_region(struct vm_region *region) 5498feae131SDavid Howells { 5508feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 5518feae131SDavid Howells 5528feae131SDavid Howells validate_nommu_regions(); 5538feae131SDavid Howells rb_erase(®ion->vm_rb, &nommu_region_tree); 5548feae131SDavid Howells validate_nommu_regions(); 5558feae131SDavid Howells } 5568feae131SDavid Howells 5578feae131SDavid Howells /* 5588feae131SDavid Howells * free a contiguous series of pages 5598feae131SDavid Howells */ 5608feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to) 5618feae131SDavid Howells { 5628feae131SDavid Howells for (; from < to; from += PAGE_SIZE) { 5638feae131SDavid Howells struct page *page = virt_to_page(from); 5648feae131SDavid Howells 5658feae131SDavid Howells kdebug("- free %lx", from); 5668feae131SDavid Howells atomic_dec(&mmap_pages_allocated); 5678feae131SDavid Howells if (page_count(page) != 1) 5688feae131SDavid Howells kdebug("free page %p [%d]", page, page_count(page)); 5698feae131SDavid Howells put_page(page); 5708feae131SDavid Howells } 5718feae131SDavid Howells } 5728feae131SDavid Howells 5738feae131SDavid Howells /* 5748feae131SDavid Howells * release a reference to a region 5758feae131SDavid Howells * - the caller must hold the region semaphore, which this releases 576dd8632a1SPaul Mundt * - the region may not have been added to the tree yet, in which case vm_top 5778feae131SDavid Howells * will equal vm_start 5788feae131SDavid Howells */ 5798feae131SDavid Howells static void __put_nommu_region(struct vm_region *region) 5808feae131SDavid Howells __releases(nommu_region_sem) 5818feae131SDavid Howells { 5828feae131SDavid Howells kenter("%p{%d}", region, atomic_read(®ion->vm_usage)); 5838feae131SDavid Howells 5848feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 5858feae131SDavid Howells 5868feae131SDavid Howells if (atomic_dec_and_test(®ion->vm_usage)) { 587dd8632a1SPaul Mundt if (region->vm_top > region->vm_start) 5888feae131SDavid Howells delete_nommu_region(region); 5898feae131SDavid Howells up_write(&nommu_region_sem); 5908feae131SDavid Howells 5918feae131SDavid Howells if (region->vm_file) 5928feae131SDavid Howells fput(region->vm_file); 5938feae131SDavid Howells 5948feae131SDavid Howells /* IO memory and memory shared directly out of the pagecache 5958feae131SDavid Howells * from ramfs/tmpfs mustn't be released here */ 5968feae131SDavid Howells if (region->vm_flags & VM_MAPPED_COPY) { 5978feae131SDavid Howells kdebug("free series"); 598dd8632a1SPaul Mundt free_page_series(region->vm_start, region->vm_top); 5998feae131SDavid Howells } 6008feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 6018feae131SDavid Howells } else { 6028feae131SDavid Howells up_write(&nommu_region_sem); 6038feae131SDavid Howells } 6048feae131SDavid Howells } 6058feae131SDavid Howells 6068feae131SDavid Howells /* 6078feae131SDavid Howells * release a reference to a region 6088feae131SDavid Howells */ 6098feae131SDavid Howells static void put_nommu_region(struct vm_region *region) 6108feae131SDavid Howells { 6118feae131SDavid Howells down_write(&nommu_region_sem); 6128feae131SDavid Howells __put_nommu_region(region); 6138feae131SDavid Howells } 6141da177e4SLinus Torvalds 6153034097aSDavid Howells /* 6163034097aSDavid Howells * add a VMA into a process's mm_struct in the appropriate place in the list 6178feae131SDavid Howells * and tree and add to the address space's page tree also if not an anonymous 6188feae131SDavid Howells * page 6193034097aSDavid Howells * - should be called with mm->mmap_sem held writelocked 6203034097aSDavid Howells */ 6218feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) 6223034097aSDavid Howells { 6238feae131SDavid Howells struct vm_area_struct *pvma, **pp; 6248feae131SDavid Howells struct address_space *mapping; 6258feae131SDavid Howells struct rb_node **p, *parent; 6263034097aSDavid Howells 6278feae131SDavid Howells kenter(",%p", vma); 6288feae131SDavid Howells 6298feae131SDavid Howells BUG_ON(!vma->vm_region); 6308feae131SDavid Howells 6318feae131SDavid Howells mm->map_count++; 6328feae131SDavid Howells vma->vm_mm = mm; 6338feae131SDavid Howells 6348feae131SDavid Howells /* add the VMA to the mapping */ 6358feae131SDavid Howells if (vma->vm_file) { 6368feae131SDavid Howells mapping = vma->vm_file->f_mapping; 6378feae131SDavid Howells 6388feae131SDavid Howells flush_dcache_mmap_lock(mapping); 6398feae131SDavid Howells vma_prio_tree_insert(vma, &mapping->i_mmap); 6408feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 6418feae131SDavid Howells } 6428feae131SDavid Howells 6438feae131SDavid Howells /* add the VMA to the tree */ 6448feae131SDavid Howells parent = NULL; 6458feae131SDavid Howells p = &mm->mm_rb.rb_node; 6468feae131SDavid Howells while (*p) { 6478feae131SDavid Howells parent = *p; 6488feae131SDavid Howells pvma = rb_entry(parent, struct vm_area_struct, vm_rb); 6498feae131SDavid Howells 6508feae131SDavid Howells /* sort by: start addr, end addr, VMA struct addr in that order 6518feae131SDavid Howells * (the latter is necessary as we may get identical VMAs) */ 6528feae131SDavid Howells if (vma->vm_start < pvma->vm_start) 6538feae131SDavid Howells p = &(*p)->rb_left; 6548feae131SDavid Howells else if (vma->vm_start > pvma->vm_start) 6558feae131SDavid Howells p = &(*p)->rb_right; 6568feae131SDavid Howells else if (vma->vm_end < pvma->vm_end) 6578feae131SDavid Howells p = &(*p)->rb_left; 6588feae131SDavid Howells else if (vma->vm_end > pvma->vm_end) 6598feae131SDavid Howells p = &(*p)->rb_right; 6608feae131SDavid Howells else if (vma < pvma) 6618feae131SDavid Howells p = &(*p)->rb_left; 6628feae131SDavid Howells else if (vma > pvma) 6638feae131SDavid Howells p = &(*p)->rb_right; 6648feae131SDavid Howells else 6658feae131SDavid Howells BUG(); 6668feae131SDavid Howells } 6678feae131SDavid Howells 6688feae131SDavid Howells rb_link_node(&vma->vm_rb, parent, p); 6698feae131SDavid Howells rb_insert_color(&vma->vm_rb, &mm->mm_rb); 6708feae131SDavid Howells 6718feae131SDavid Howells /* add VMA to the VMA list also */ 6728feae131SDavid Howells for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) { 6738feae131SDavid Howells if (pvma->vm_start > vma->vm_start) 6743034097aSDavid Howells break; 6758feae131SDavid Howells if (pvma->vm_start < vma->vm_start) 6768feae131SDavid Howells continue; 6778feae131SDavid Howells if (pvma->vm_end < vma->vm_end) 6788feae131SDavid Howells break; 6798feae131SDavid Howells } 6803034097aSDavid Howells 6818feae131SDavid Howells vma->vm_next = *pp; 6828feae131SDavid Howells *pp = vma; 6838feae131SDavid Howells } 6848feae131SDavid Howells 6858feae131SDavid Howells /* 6868feae131SDavid Howells * delete a VMA from its owning mm_struct and address space 6878feae131SDavid Howells */ 6888feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma) 6898feae131SDavid Howells { 6908feae131SDavid Howells struct vm_area_struct **pp; 6918feae131SDavid Howells struct address_space *mapping; 6928feae131SDavid Howells struct mm_struct *mm = vma->vm_mm; 6938feae131SDavid Howells 6948feae131SDavid Howells kenter("%p", vma); 6958feae131SDavid Howells 6968feae131SDavid Howells mm->map_count--; 6978feae131SDavid Howells if (mm->mmap_cache == vma) 6988feae131SDavid Howells mm->mmap_cache = NULL; 6998feae131SDavid Howells 7008feae131SDavid Howells /* remove the VMA from the mapping */ 7018feae131SDavid Howells if (vma->vm_file) { 7028feae131SDavid Howells mapping = vma->vm_file->f_mapping; 7038feae131SDavid Howells 7048feae131SDavid Howells flush_dcache_mmap_lock(mapping); 7058feae131SDavid Howells vma_prio_tree_remove(vma, &mapping->i_mmap); 7068feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 7078feae131SDavid Howells } 7088feae131SDavid Howells 7098feae131SDavid Howells /* remove from the MM's tree and list */ 7108feae131SDavid Howells rb_erase(&vma->vm_rb, &mm->mm_rb); 7118feae131SDavid Howells for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) { 7128feae131SDavid Howells if (*pp == vma) { 7138feae131SDavid Howells *pp = vma->vm_next; 7148feae131SDavid Howells break; 7158feae131SDavid Howells } 7168feae131SDavid Howells } 7178feae131SDavid Howells 7188feae131SDavid Howells vma->vm_mm = NULL; 7198feae131SDavid Howells } 7208feae131SDavid Howells 7218feae131SDavid Howells /* 7228feae131SDavid Howells * destroy a VMA record 7238feae131SDavid Howells */ 7248feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 7258feae131SDavid Howells { 7268feae131SDavid Howells kenter("%p", vma); 7278feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 7288feae131SDavid Howells vma->vm_ops->close(vma); 7298feae131SDavid Howells if (vma->vm_file) { 7308feae131SDavid Howells fput(vma->vm_file); 7318feae131SDavid Howells if (vma->vm_flags & VM_EXECUTABLE) 7328feae131SDavid Howells removed_exe_file_vma(mm); 7338feae131SDavid Howells } 7348feae131SDavid Howells put_nommu_region(vma->vm_region); 7358feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 7363034097aSDavid Howells } 7373034097aSDavid Howells 7383034097aSDavid Howells /* 7393034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 7403034097aSDavid Howells * - should be called with mm->mmap_sem at least held readlocked 7413034097aSDavid Howells */ 7423034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 7433034097aSDavid Howells { 7448feae131SDavid Howells struct vm_area_struct *vma; 7458feae131SDavid Howells struct rb_node *n = mm->mm_rb.rb_node; 7463034097aSDavid Howells 7478feae131SDavid Howells /* check the cache first */ 7488feae131SDavid Howells vma = mm->mmap_cache; 7498feae131SDavid Howells if (vma && vma->vm_start <= addr && vma->vm_end > addr) 7508feae131SDavid Howells return vma; 7518feae131SDavid Howells 7528feae131SDavid Howells /* trawl the tree (there may be multiple mappings in which addr 7538feae131SDavid Howells * resides) */ 7548feae131SDavid Howells for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) { 7558feae131SDavid Howells vma = rb_entry(n, struct vm_area_struct, vm_rb); 7568feae131SDavid Howells if (vma->vm_start > addr) 7578feae131SDavid Howells return NULL; 7588feae131SDavid Howells if (vma->vm_end > addr) { 7598feae131SDavid Howells mm->mmap_cache = vma; 7608feae131SDavid Howells return vma; 7613034097aSDavid Howells } 7628feae131SDavid Howells } 7633034097aSDavid Howells 7643034097aSDavid Howells return NULL; 7653034097aSDavid Howells } 7663034097aSDavid Howells EXPORT_SYMBOL(find_vma); 7673034097aSDavid Howells 7683034097aSDavid Howells /* 769930e652aSDavid Howells * find a VMA 770930e652aSDavid Howells * - we don't extend stack VMAs under NOMMU conditions 771930e652aSDavid Howells */ 772930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 773930e652aSDavid Howells { 774930e652aSDavid Howells return find_vma(mm, addr); 775930e652aSDavid Howells } 776930e652aSDavid Howells 7778feae131SDavid Howells /* 7788feae131SDavid Howells * expand a stack to a given address 7798feae131SDavid Howells * - not supported under NOMMU conditions 7808feae131SDavid Howells */ 78157c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address) 78257c8f63eSGreg Ungerer { 78357c8f63eSGreg Ungerer return -ENOMEM; 78457c8f63eSGreg Ungerer } 78557c8f63eSGreg Ungerer 786930e652aSDavid Howells /* 7876fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 7886fa5f80bSDavid Howells * - should be called with mm->mmap_sem at least held readlocked 7896fa5f80bSDavid Howells */ 7908feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 7918feae131SDavid Howells unsigned long addr, 7928feae131SDavid Howells unsigned long len) 7931da177e4SLinus Torvalds { 7941da177e4SLinus Torvalds struct vm_area_struct *vma; 7958feae131SDavid Howells struct rb_node *n = mm->mm_rb.rb_node; 7968feae131SDavid Howells unsigned long end = addr + len; 7971da177e4SLinus Torvalds 7988feae131SDavid Howells /* check the cache first */ 7998feae131SDavid Howells vma = mm->mmap_cache; 8008feae131SDavid Howells if (vma && vma->vm_start == addr && vma->vm_end == end) 8011da177e4SLinus Torvalds return vma; 8028feae131SDavid Howells 8038feae131SDavid Howells /* trawl the tree (there may be multiple mappings in which addr 8048feae131SDavid Howells * resides) */ 8058feae131SDavid Howells for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) { 8068feae131SDavid Howells vma = rb_entry(n, struct vm_area_struct, vm_rb); 8078feae131SDavid Howells if (vma->vm_start < addr) 8088feae131SDavid Howells continue; 8098feae131SDavid Howells if (vma->vm_start > addr) 8108feae131SDavid Howells return NULL; 8118feae131SDavid Howells if (vma->vm_end == end) { 8128feae131SDavid Howells mm->mmap_cache = vma; 8138feae131SDavid Howells return vma; 8148feae131SDavid Howells } 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds return NULL; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8203034097aSDavid Howells /* 8211da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 8221da177e4SLinus Torvalds * mapping we're capable of supporting 8231da177e4SLinus Torvalds */ 8241da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 8251da177e4SLinus Torvalds unsigned long addr, 8261da177e4SLinus Torvalds unsigned long len, 8271da177e4SLinus Torvalds unsigned long prot, 8281da177e4SLinus Torvalds unsigned long flags, 8291da177e4SLinus Torvalds unsigned long pgoff, 8301da177e4SLinus Torvalds unsigned long *_capabilities) 8311da177e4SLinus Torvalds { 8328feae131SDavid Howells unsigned long capabilities, rlen; 8331da177e4SLinus Torvalds unsigned long reqprot = prot; 8341da177e4SLinus Torvalds int ret; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* do the simple checks first */ 8371da177e4SLinus Torvalds if (flags & MAP_FIXED || addr) { 8381da177e4SLinus Torvalds printk(KERN_DEBUG 8391da177e4SLinus Torvalds "%d: Can't do fixed-address/overlay mmap of RAM\n", 8401da177e4SLinus Torvalds current->pid); 8411da177e4SLinus Torvalds return -EINVAL; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 8451da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 8461da177e4SLinus Torvalds return -EINVAL; 8471da177e4SLinus Torvalds 848f81cff0dSMike Frysinger if (!len) 8491da177e4SLinus Torvalds return -EINVAL; 8501da177e4SLinus Torvalds 851f81cff0dSMike Frysinger /* Careful about overflows.. */ 8528feae131SDavid Howells rlen = PAGE_ALIGN(len); 8538feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 854f81cff0dSMike Frysinger return -ENOMEM; 855f81cff0dSMike Frysinger 8561da177e4SLinus Torvalds /* offset overflow? */ 8578feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 858f81cff0dSMike Frysinger return -EOVERFLOW; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds if (file) { 8611da177e4SLinus Torvalds /* validate file mapping requests */ 8621da177e4SLinus Torvalds struct address_space *mapping; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* files must support mmap */ 8651da177e4SLinus Torvalds if (!file->f_op || !file->f_op->mmap) 8661da177e4SLinus Torvalds return -ENODEV; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 8691da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 8701da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 8711da177e4SLinus Torvalds */ 8721da177e4SLinus Torvalds mapping = file->f_mapping; 8731da177e4SLinus Torvalds if (!mapping) 874e9536ae7SJosef Sipek mapping = file->f_path.dentry->d_inode->i_mapping; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds capabilities = 0; 8771da177e4SLinus Torvalds if (mapping && mapping->backing_dev_info) 8781da177e4SLinus Torvalds capabilities = mapping->backing_dev_info->capabilities; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds if (!capabilities) { 8811da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 8821da177e4SLinus Torvalds * defaults */ 883e9536ae7SJosef Sipek switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) { 8841da177e4SLinus Torvalds case S_IFREG: 8851da177e4SLinus Torvalds case S_IFBLK: 8861da177e4SLinus Torvalds capabilities = BDI_CAP_MAP_COPY; 8871da177e4SLinus Torvalds break; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds case S_IFCHR: 8901da177e4SLinus Torvalds capabilities = 8911da177e4SLinus Torvalds BDI_CAP_MAP_DIRECT | 8921da177e4SLinus Torvalds BDI_CAP_READ_MAP | 8931da177e4SLinus Torvalds BDI_CAP_WRITE_MAP; 8941da177e4SLinus Torvalds break; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds default: 8971da177e4SLinus Torvalds return -EINVAL; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 9021da177e4SLinus Torvalds * device */ 9031da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 9041da177e4SLinus Torvalds capabilities &= ~BDI_CAP_MAP_DIRECT; 9051da177e4SLinus Torvalds if (!file->f_op->read) 9061da177e4SLinus Torvalds capabilities &= ~BDI_CAP_MAP_COPY; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds if (flags & MAP_SHARED) { 9091da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 9101da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 9111da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 9121da177e4SLinus Torvalds return -EACCES; 9131da177e4SLinus Torvalds 914e9536ae7SJosef Sipek if (IS_APPEND(file->f_path.dentry->d_inode) && 9151da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 9161da177e4SLinus Torvalds return -EACCES; 9171da177e4SLinus Torvalds 918e9536ae7SJosef Sipek if (locks_verify_locked(file->f_path.dentry->d_inode)) 9191da177e4SLinus Torvalds return -EAGAIN; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds if (!(capabilities & BDI_CAP_MAP_DIRECT)) 9221da177e4SLinus Torvalds return -ENODEV; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) || 9251da177e4SLinus Torvalds ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) || 9261da177e4SLinus Torvalds ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP)) 9271da177e4SLinus Torvalds ) { 9281da177e4SLinus Torvalds printk("MAP_SHARED not completely supported on !MMU\n"); 9291da177e4SLinus Torvalds return -EINVAL; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 9331da177e4SLinus Torvalds capabilities &= ~BDI_CAP_MAP_COPY; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds else { 9361da177e4SLinus Torvalds /* we're going to read the file into private memory we 9371da177e4SLinus Torvalds * allocate */ 9381da177e4SLinus Torvalds if (!(capabilities & BDI_CAP_MAP_COPY)) 9391da177e4SLinus Torvalds return -ENODEV; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 9421da177e4SLinus Torvalds * shared with the backing device */ 9431da177e4SLinus Torvalds if (prot & PROT_WRITE) 9441da177e4SLinus Torvalds capabilities &= ~BDI_CAP_MAP_DIRECT; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds /* handle executable mappings and implied executable 9481da177e4SLinus Torvalds * mappings */ 949e9536ae7SJosef Sipek if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { 9501da177e4SLinus Torvalds if (prot & PROT_EXEC) 9511da177e4SLinus Torvalds return -EPERM; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 9541da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 9551da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 9561da177e4SLinus Torvalds if (capabilities & BDI_CAP_EXEC_MAP) 9571da177e4SLinus Torvalds prot |= PROT_EXEC; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds else if ((prot & PROT_READ) && 9611da177e4SLinus Torvalds (prot & PROT_EXEC) && 9621da177e4SLinus Torvalds !(capabilities & BDI_CAP_EXEC_MAP) 9631da177e4SLinus Torvalds ) { 9641da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 9651da177e4SLinus Torvalds capabilities &= ~BDI_CAP_MAP_DIRECT; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds else { 9691da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 9701da177e4SLinus Torvalds * privately mapped 9711da177e4SLinus Torvalds */ 9721da177e4SLinus Torvalds capabilities = BDI_CAP_MAP_COPY; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 9751da177e4SLinus Torvalds if ((prot & PROT_READ) && 9761da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 9771da177e4SLinus Torvalds prot |= PROT_EXEC; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* allow the security API to have its say */ 981ed032189SEric Paris ret = security_file_mmap(file, reqprot, prot, flags, addr, 0); 9821da177e4SLinus Torvalds if (ret < 0) 9831da177e4SLinus Torvalds return ret; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* looks okay */ 9861da177e4SLinus Torvalds *_capabilities = capabilities; 9871da177e4SLinus Torvalds return 0; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* 9911da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 9921da177e4SLinus Torvalds * now know into VMA flags 9931da177e4SLinus Torvalds */ 9941da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 9951da177e4SLinus Torvalds unsigned long prot, 9961da177e4SLinus Torvalds unsigned long flags, 9971da177e4SLinus Torvalds unsigned long capabilities) 9981da177e4SLinus Torvalds { 9991da177e4SLinus Torvalds unsigned long vm_flags; 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags); 10021da177e4SLinus Torvalds vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 10031da177e4SLinus Torvalds /* vm_flags |= mm->def_flags; */ 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds if (!(capabilities & BDI_CAP_MAP_DIRECT)) { 10061da177e4SLinus Torvalds /* attempt to share read-only copies of mapped file chunks */ 10071da177e4SLinus Torvalds if (file && !(prot & PROT_WRITE)) 10081da177e4SLinus Torvalds vm_flags |= VM_MAYSHARE; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds else { 10111da177e4SLinus Torvalds /* overlay a shareable mapping on the backing device or inode 10121da177e4SLinus Torvalds * if possible - used for chardevs, ramfs/tmpfs/shmfs and 10131da177e4SLinus Torvalds * romfs/cramfs */ 10141da177e4SLinus Torvalds if (flags & MAP_SHARED) 10151da177e4SLinus Torvalds vm_flags |= VM_MAYSHARE | VM_SHARED; 10161da177e4SLinus Torvalds else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0) 10171da177e4SLinus Torvalds vm_flags |= VM_MAYSHARE; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds /* refuse to let anyone share private mappings with this process if 10211da177e4SLinus Torvalds * it's being traced - otherwise breakpoints set in it may interfere 10221da177e4SLinus Torvalds * with another untraced process 10231da177e4SLinus Torvalds */ 1024fa8e26ccSRoland McGrath if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current)) 10251da177e4SLinus Torvalds vm_flags &= ~VM_MAYSHARE; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds return vm_flags; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /* 10318feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 10328feae131SDavid Howells * pins the storage) 10331da177e4SLinus Torvalds */ 10348feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 10351da177e4SLinus Torvalds { 10361da177e4SLinus Torvalds int ret; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1039dd8632a1SPaul Mundt if (ret == 0) { 1040dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 1041dd8632a1SPaul Mundt return ret; 1042dd8632a1SPaul Mundt } 10431da177e4SLinus Torvalds if (ret != -ENOSYS) 10441da177e4SLinus Torvalds return ret; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 10471da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll fall 10481da177e4SLinus Torvalds * through to making a private copy of the data and mapping 10491da177e4SLinus Torvalds * that if we can */ 10501da177e4SLinus Torvalds return -ENODEV; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds /* 10541da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 10551da177e4SLinus Torvalds */ 10568feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 10578feae131SDavid Howells struct vm_region *region, 10588feae131SDavid Howells unsigned long len) 10591da177e4SLinus Torvalds { 10608feae131SDavid Howells struct page *pages; 10618feae131SDavid Howells unsigned long total, point, n, rlen; 10621da177e4SLinus Torvalds void *base; 10638feae131SDavid Howells int ret, order; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds /* invoke the file's mapping function so that it can keep track of 10661da177e4SLinus Torvalds * shared mappings on devices or memory 10671da177e4SLinus Torvalds * - VM_MAYSHARE will be set if it may attempt to share 10681da177e4SLinus Torvalds */ 10691da177e4SLinus Torvalds if (vma->vm_file) { 10701da177e4SLinus Torvalds ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1071dd8632a1SPaul Mundt if (ret == 0) { 10721da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 1073dd8632a1SPaul Mundt BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); 1074dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 1075dd8632a1SPaul Mundt return ret; 10761da177e4SLinus Torvalds } 1077dd8632a1SPaul Mundt if (ret != -ENOSYS) 1078dd8632a1SPaul Mundt return ret; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 10811da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 10821da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10858feae131SDavid Howells rlen = PAGE_ALIGN(len); 10868feae131SDavid Howells 10871da177e4SLinus Torvalds /* allocate some memory to hold the mapping 10881da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 10891da177e4SLinus Torvalds * we're allocating is smaller than a page 10901da177e4SLinus Torvalds */ 10918feae131SDavid Howells order = get_order(rlen); 10928feae131SDavid Howells kdebug("alloc order %d for %lx", order, len); 10938feae131SDavid Howells 10948feae131SDavid Howells pages = alloc_pages(GFP_KERNEL, order); 10958feae131SDavid Howells if (!pages) 10961da177e4SLinus Torvalds goto enomem; 10971da177e4SLinus Torvalds 10988feae131SDavid Howells total = 1 << order; 10998feae131SDavid Howells atomic_add(total, &mmap_pages_allocated); 11001da177e4SLinus Torvalds 11018feae131SDavid Howells point = rlen >> PAGE_SHIFT; 1102dd8632a1SPaul Mundt 1103dd8632a1SPaul Mundt /* we allocated a power-of-2 sized page set, so we may want to trim off 1104dd8632a1SPaul Mundt * the excess */ 1105dd8632a1SPaul Mundt if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) { 11068feae131SDavid Howells while (total > point) { 11078feae131SDavid Howells order = ilog2(total - point); 11088feae131SDavid Howells n = 1 << order; 11098feae131SDavid Howells kdebug("shave %lu/%lu @%lu", n, total - point, total); 11108feae131SDavid Howells atomic_sub(n, &mmap_pages_allocated); 11118feae131SDavid Howells total -= n; 11128feae131SDavid Howells set_page_refcounted(pages + total); 11138feae131SDavid Howells __free_pages(pages + total, order); 11148feae131SDavid Howells } 1115dd8632a1SPaul Mundt } 11168feae131SDavid Howells 11178feae131SDavid Howells for (point = 1; point < total; point++) 11188feae131SDavid Howells set_page_refcounted(&pages[point]); 11198feae131SDavid Howells 11208feae131SDavid Howells base = page_address(pages); 11218feae131SDavid Howells region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; 11228feae131SDavid Howells region->vm_start = (unsigned long) base; 11238feae131SDavid Howells region->vm_end = region->vm_start + rlen; 1124dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 11258feae131SDavid Howells 11268feae131SDavid Howells vma->vm_start = region->vm_start; 11278feae131SDavid Howells vma->vm_end = region->vm_start + len; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds if (vma->vm_file) { 11301da177e4SLinus Torvalds /* read the contents of a file into the copy */ 11311da177e4SLinus Torvalds mm_segment_t old_fs; 11321da177e4SLinus Torvalds loff_t fpos; 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds fpos = vma->vm_pgoff; 11351da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds old_fs = get_fs(); 11381da177e4SLinus Torvalds set_fs(KERNEL_DS); 11398feae131SDavid Howells ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos); 11401da177e4SLinus Torvalds set_fs(old_fs); 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds if (ret < 0) 11431da177e4SLinus Torvalds goto error_free; 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds /* clear the last little bit */ 11468feae131SDavid Howells if (ret < rlen) 11478feae131SDavid Howells memset(base + ret, 0, rlen - ret); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds } else { 11501da177e4SLinus Torvalds /* if it's an anonymous mapping, then just clear it */ 11518feae131SDavid Howells memset(base, 0, rlen); 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds return 0; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds error_free: 11578feae131SDavid Howells free_page_series(region->vm_start, region->vm_end); 11588feae131SDavid Howells region->vm_start = vma->vm_start = 0; 11598feae131SDavid Howells region->vm_end = vma->vm_end = 0; 1160dd8632a1SPaul Mundt region->vm_top = 0; 11611da177e4SLinus Torvalds return ret; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds enomem: 11641da177e4SLinus Torvalds printk("Allocation of length %lu from process %d failed\n", 11651da177e4SLinus Torvalds len, current->pid); 11661da177e4SLinus Torvalds show_free_areas(); 11671da177e4SLinus Torvalds return -ENOMEM; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * handle mapping creation for uClinux 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file *file, 11741da177e4SLinus Torvalds unsigned long addr, 11751da177e4SLinus Torvalds unsigned long len, 11761da177e4SLinus Torvalds unsigned long prot, 11771da177e4SLinus Torvalds unsigned long flags, 11781da177e4SLinus Torvalds unsigned long pgoff) 11791da177e4SLinus Torvalds { 11808feae131SDavid Howells struct vm_area_struct *vma; 11818feae131SDavid Howells struct vm_region *region; 11821da177e4SLinus Torvalds struct rb_node *rb; 11838feae131SDavid Howells unsigned long capabilities, vm_flags, result; 11841da177e4SLinus Torvalds int ret; 11851da177e4SLinus Torvalds 11868feae131SDavid Howells kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff); 11878feae131SDavid Howells 11887cd94146SEric Paris if (!(flags & MAP_FIXED)) 11897cd94146SEric Paris addr = round_hint_to_min(addr); 11907cd94146SEric Paris 11911da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 11921da177e4SLinus Torvalds * mapping */ 11931da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 11941da177e4SLinus Torvalds &capabilities); 11958feae131SDavid Howells if (ret < 0) { 11968feae131SDavid Howells kleave(" = %d [val]", ret); 11971da177e4SLinus Torvalds return ret; 11988feae131SDavid Howells } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 12011da177e4SLinus Torvalds * now know into VMA flags */ 12021da177e4SLinus Torvalds vm_flags = determine_vm_flags(file, prot, flags, capabilities); 12031da177e4SLinus Torvalds 12048feae131SDavid Howells /* we're going to need to record the mapping */ 12058feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 12068feae131SDavid Howells if (!region) 12078feae131SDavid Howells goto error_getting_region; 12081da177e4SLinus Torvalds 12098feae131SDavid Howells vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 12108feae131SDavid Howells if (!vma) 12118feae131SDavid Howells goto error_getting_vma; 12121da177e4SLinus Torvalds 12138feae131SDavid Howells atomic_set(®ion->vm_usage, 1); 12148feae131SDavid Howells region->vm_flags = vm_flags; 12158feae131SDavid Howells region->vm_pgoff = pgoff; 12168feae131SDavid Howells 12178feae131SDavid Howells INIT_LIST_HEAD(&vma->anon_vma_node); 12188feae131SDavid Howells vma->vm_flags = vm_flags; 12198feae131SDavid Howells vma->vm_pgoff = pgoff; 12208feae131SDavid Howells 12218feae131SDavid Howells if (file) { 12228feae131SDavid Howells region->vm_file = file; 12238feae131SDavid Howells get_file(file); 12248feae131SDavid Howells vma->vm_file = file; 12258feae131SDavid Howells get_file(file); 12268feae131SDavid Howells if (vm_flags & VM_EXECUTABLE) { 12278feae131SDavid Howells added_exe_file_vma(current->mm); 12288feae131SDavid Howells vma->vm_mm = current->mm; 12298feae131SDavid Howells } 12308feae131SDavid Howells } 12318feae131SDavid Howells 12328feae131SDavid Howells down_write(&nommu_region_sem); 12338feae131SDavid Howells 12348feae131SDavid Howells /* if we want to share, we need to check for regions created by other 12351da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 12368feae131SDavid Howells * - we can only share with a superset match on most regular files 12371da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 12381da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 12391da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 12401da177e4SLinus Torvalds * than here 12411da177e4SLinus Torvalds */ 12421da177e4SLinus Torvalds if (vm_flags & VM_MAYSHARE) { 12438feae131SDavid Howells struct vm_region *pregion; 12448feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 12451da177e4SLinus Torvalds 12468feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 12478feae131SDavid Howells pgend = pgoff + pglen; 1248165b2392SDavid Howells 12498feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 12508feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 12511da177e4SLinus Torvalds 12528feae131SDavid Howells if (!(pregion->vm_flags & VM_MAYSHARE)) 12531da177e4SLinus Torvalds continue; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 12568feae131SDavid Howells if (pregion->vm_file->f_path.dentry->d_inode != 12578feae131SDavid Howells file->f_path.dentry->d_inode) 12581da177e4SLinus Torvalds continue; 12591da177e4SLinus Torvalds 12608feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 12611da177e4SLinus Torvalds continue; 12621da177e4SLinus Torvalds 12638feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 12648feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 12658feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 12668feae131SDavid Howells if (pgoff >= rpgend) 12671da177e4SLinus Torvalds continue; 12681da177e4SLinus Torvalds 12698feae131SDavid Howells /* handle inexactly overlapping matches between 12708feae131SDavid Howells * mappings */ 12718feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 12728feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 12738feae131SDavid Howells /* new mapping is not a subset of the region */ 12741da177e4SLinus Torvalds if (!(capabilities & BDI_CAP_MAP_DIRECT)) 12751da177e4SLinus Torvalds goto sharing_violation; 12761da177e4SLinus Torvalds continue; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12798feae131SDavid Howells /* we've found a region we can share */ 12808feae131SDavid Howells atomic_inc(&pregion->vm_usage); 12818feae131SDavid Howells vma->vm_region = pregion; 12828feae131SDavid Howells start = pregion->vm_start; 12838feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 12848feae131SDavid Howells vma->vm_start = start; 12858feae131SDavid Howells vma->vm_end = start + len; 12861da177e4SLinus Torvalds 12878feae131SDavid Howells if (pregion->vm_flags & VM_MAPPED_COPY) { 12888feae131SDavid Howells kdebug("share copy"); 12898feae131SDavid Howells vma->vm_flags |= VM_MAPPED_COPY; 12908feae131SDavid Howells } else { 12918feae131SDavid Howells kdebug("share mmap"); 12928feae131SDavid Howells ret = do_mmap_shared_file(vma); 12938feae131SDavid Howells if (ret < 0) { 12948feae131SDavid Howells vma->vm_region = NULL; 12958feae131SDavid Howells vma->vm_start = 0; 12968feae131SDavid Howells vma->vm_end = 0; 12978feae131SDavid Howells atomic_dec(&pregion->vm_usage); 12988feae131SDavid Howells pregion = NULL; 12998feae131SDavid Howells goto error_just_free; 13001da177e4SLinus Torvalds } 13018feae131SDavid Howells } 13028feae131SDavid Howells fput(region->vm_file); 13038feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 13048feae131SDavid Howells region = pregion; 13058feae131SDavid Howells result = start; 13068feae131SDavid Howells goto share; 13078feae131SDavid Howells } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 13101da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 13111da177e4SLinus Torvalds * tell us the location of a shared mapping 13121da177e4SLinus Torvalds */ 13131da177e4SLinus Torvalds if (file && file->f_op->get_unmapped_area) { 13141da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 13151da177e4SLinus Torvalds pgoff, flags); 13161da177e4SLinus Torvalds if (IS_ERR((void *) addr)) { 13171da177e4SLinus Torvalds ret = addr; 13181da177e4SLinus Torvalds if (ret != (unsigned long) -ENOSYS) 13198feae131SDavid Howells goto error_just_free; 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds /* the driver refused to tell us where to site 13221da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 13231da177e4SLinus Torvalds * it */ 13241da177e4SLinus Torvalds ret = (unsigned long) -ENODEV; 13251da177e4SLinus Torvalds if (!(capabilities & BDI_CAP_MAP_COPY)) 13268feae131SDavid Howells goto error_just_free; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds capabilities &= ~BDI_CAP_MAP_DIRECT; 13298feae131SDavid Howells } else { 13308feae131SDavid Howells vma->vm_start = region->vm_start = addr; 13318feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13368feae131SDavid Howells vma->vm_region = region; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* set up the mapping */ 13391da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 13408feae131SDavid Howells ret = do_mmap_shared_file(vma); 13411da177e4SLinus Torvalds else 13428feae131SDavid Howells ret = do_mmap_private(vma, region, len); 13431da177e4SLinus Torvalds if (ret < 0) 13448feae131SDavid Howells goto error_put_region; 13458feae131SDavid Howells 13468feae131SDavid Howells add_nommu_region(region); 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 13498feae131SDavid Howells result = vma->vm_start; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 13521da177e4SLinus Torvalds 13538feae131SDavid Howells share: 13548feae131SDavid Howells add_vma_to_mm(current->mm, vma); 13551da177e4SLinus Torvalds 13568feae131SDavid Howells up_write(&nommu_region_sem); 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (prot & PROT_EXEC) 13598feae131SDavid Howells flush_icache_range(result, result + len); 13601da177e4SLinus Torvalds 13618feae131SDavid Howells kleave(" = %lx", result); 13628feae131SDavid Howells return result; 13631da177e4SLinus Torvalds 13648feae131SDavid Howells error_put_region: 13658feae131SDavid Howells __put_nommu_region(region); 13661da177e4SLinus Torvalds if (vma) { 1367925d1c40SMatt Helsley if (vma->vm_file) { 13681da177e4SLinus Torvalds fput(vma->vm_file); 1369925d1c40SMatt Helsley if (vma->vm_flags & VM_EXECUTABLE) 1370925d1c40SMatt Helsley removed_exe_file_vma(vma->vm_mm); 1371925d1c40SMatt Helsley } 13728feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 13731da177e4SLinus Torvalds } 13748feae131SDavid Howells kleave(" = %d [pr]", ret); 13758feae131SDavid Howells return ret; 13768feae131SDavid Howells 13778feae131SDavid Howells error_just_free: 13788feae131SDavid Howells up_write(&nommu_region_sem); 13798feae131SDavid Howells error: 13808feae131SDavid Howells fput(region->vm_file); 13818feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 13828feae131SDavid Howells fput(vma->vm_file); 13838feae131SDavid Howells if (vma->vm_flags & VM_EXECUTABLE) 13848feae131SDavid Howells removed_exe_file_vma(vma->vm_mm); 13858feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 13868feae131SDavid Howells kleave(" = %d", ret); 13871da177e4SLinus Torvalds return ret; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds sharing_violation: 13908feae131SDavid Howells up_write(&nommu_region_sem); 13918feae131SDavid Howells printk(KERN_WARNING "Attempt to share mismatched mappings\n"); 13928feae131SDavid Howells ret = -EINVAL; 13938feae131SDavid Howells goto error; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds error_getting_vma: 13968feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 13978feae131SDavid Howells printk(KERN_WARNING "Allocation of vma for %lu byte allocation" 13988feae131SDavid Howells " from process %d failed\n", 13991da177e4SLinus Torvalds len, current->pid); 14001da177e4SLinus Torvalds show_free_areas(); 14011da177e4SLinus Torvalds return -ENOMEM; 14021da177e4SLinus Torvalds 14038feae131SDavid Howells error_getting_region: 14048feae131SDavid Howells printk(KERN_WARNING "Allocation of vm region for %lu byte allocation" 14058feae131SDavid Howells " from process %d failed\n", 14061da177e4SLinus Torvalds len, current->pid); 14071da177e4SLinus Torvalds show_free_areas(); 14081da177e4SLinus Torvalds return -ENOMEM; 14091da177e4SLinus Torvalds } 1410b5073173SPaul Mundt EXPORT_SYMBOL(do_mmap_pgoff); 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds /* 14138feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 14148feae131SDavid Howells * for the first part or the tail. 14151da177e4SLinus Torvalds */ 14168feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 14178feae131SDavid Howells unsigned long addr, int new_below) 14181da177e4SLinus Torvalds { 14198feae131SDavid Howells struct vm_area_struct *new; 14208feae131SDavid Howells struct vm_region *region; 14218feae131SDavid Howells unsigned long npages; 14221da177e4SLinus Torvalds 14238feae131SDavid Howells kenter(""); 14241da177e4SLinus Torvalds 14258feae131SDavid Howells /* we're only permitted to split anonymous regions that have a single 14268feae131SDavid Howells * owner */ 14278feae131SDavid Howells if (vma->vm_file || 14288feae131SDavid Howells atomic_read(&vma->vm_region->vm_usage) != 1) 14298feae131SDavid Howells return -ENOMEM; 14301da177e4SLinus Torvalds 14318feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 14328feae131SDavid Howells return -ENOMEM; 14331da177e4SLinus Torvalds 14348feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 14358feae131SDavid Howells if (!region) 14368feae131SDavid Howells return -ENOMEM; 14378feae131SDavid Howells 14388feae131SDavid Howells new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 14398feae131SDavid Howells if (!new) { 14408feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 14418feae131SDavid Howells return -ENOMEM; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14448feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 14458feae131SDavid Howells *new = *vma; 14468feae131SDavid Howells *region = *vma->vm_region; 14478feae131SDavid Howells new->vm_region = region; 14488feae131SDavid Howells 14498feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 14508feae131SDavid Howells 14518feae131SDavid Howells if (new_below) { 1452dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 14538feae131SDavid Howells } else { 14548feae131SDavid Howells region->vm_start = new->vm_start = addr; 14558feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 14561da177e4SLinus Torvalds } 14578feae131SDavid Howells 14588feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 14598feae131SDavid Howells new->vm_ops->open(new); 14608feae131SDavid Howells 14618feae131SDavid Howells delete_vma_from_mm(vma); 14628feae131SDavid Howells down_write(&nommu_region_sem); 14638feae131SDavid Howells delete_nommu_region(vma->vm_region); 14648feae131SDavid Howells if (new_below) { 14658feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 14668feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 14678feae131SDavid Howells } else { 14688feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1469dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 14708feae131SDavid Howells } 14718feae131SDavid Howells add_nommu_region(vma->vm_region); 14728feae131SDavid Howells add_nommu_region(new->vm_region); 14738feae131SDavid Howells up_write(&nommu_region_sem); 14748feae131SDavid Howells add_vma_to_mm(mm, vma); 14758feae131SDavid Howells add_vma_to_mm(mm, new); 14768feae131SDavid Howells return 0; 14778feae131SDavid Howells } 14788feae131SDavid Howells 14798feae131SDavid Howells /* 14808feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 14818feae131SDavid Howells * the end 14828feae131SDavid Howells */ 14838feae131SDavid Howells static int shrink_vma(struct mm_struct *mm, 14848feae131SDavid Howells struct vm_area_struct *vma, 14858feae131SDavid Howells unsigned long from, unsigned long to) 14868feae131SDavid Howells { 14878feae131SDavid Howells struct vm_region *region; 14888feae131SDavid Howells 14898feae131SDavid Howells kenter(""); 14908feae131SDavid Howells 14918feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 14928feae131SDavid Howells * and list */ 14938feae131SDavid Howells delete_vma_from_mm(vma); 14948feae131SDavid Howells if (from > vma->vm_start) 14958feae131SDavid Howells vma->vm_end = from; 14968feae131SDavid Howells else 14978feae131SDavid Howells vma->vm_start = to; 14988feae131SDavid Howells add_vma_to_mm(mm, vma); 14998feae131SDavid Howells 15008feae131SDavid Howells /* cut the backing region down to size */ 15018feae131SDavid Howells region = vma->vm_region; 15028feae131SDavid Howells BUG_ON(atomic_read(®ion->vm_usage) != 1); 15038feae131SDavid Howells 15048feae131SDavid Howells down_write(&nommu_region_sem); 15058feae131SDavid Howells delete_nommu_region(region); 1506dd8632a1SPaul Mundt if (from > region->vm_start) { 1507dd8632a1SPaul Mundt to = region->vm_top; 1508dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1509dd8632a1SPaul Mundt } else { 15108feae131SDavid Howells region->vm_start = to; 1511dd8632a1SPaul Mundt } 15128feae131SDavid Howells add_nommu_region(region); 15138feae131SDavid Howells up_write(&nommu_region_sem); 15148feae131SDavid Howells 15158feae131SDavid Howells free_page_series(from, to); 15168feae131SDavid Howells return 0; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15193034097aSDavid Howells /* 15203034097aSDavid Howells * release a mapping 15218feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 15228feae131SDavid Howells * VMA, though it need not cover the whole VMA 15233034097aSDavid Howells */ 15248feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 15251da177e4SLinus Torvalds { 15268feae131SDavid Howells struct vm_area_struct *vma; 15278feae131SDavid Howells struct rb_node *rb; 15288feae131SDavid Howells unsigned long end = start + len; 15298feae131SDavid Howells int ret; 15301da177e4SLinus Torvalds 15318feae131SDavid Howells kenter(",%lx,%zx", start, len); 15321da177e4SLinus Torvalds 15338feae131SDavid Howells if (len == 0) 15341da177e4SLinus Torvalds return -EINVAL; 15351da177e4SLinus Torvalds 15368feae131SDavid Howells /* find the first potentially overlapping VMA */ 15378feae131SDavid Howells vma = find_vma(mm, start); 15388feae131SDavid Howells if (!vma) { 15398feae131SDavid Howells printk(KERN_WARNING 15408feae131SDavid Howells "munmap of memory not mmapped by process %d (%s):" 15418feae131SDavid Howells " 0x%lx-0x%lx\n", 15428feae131SDavid Howells current->pid, current->comm, start, start + len - 1); 15438feae131SDavid Howells return -EINVAL; 15448feae131SDavid Howells } 15451da177e4SLinus Torvalds 15468feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 15478feae131SDavid Howells if (vma->vm_file) { 15488feae131SDavid Howells do { 15498feae131SDavid Howells if (start > vma->vm_start) { 15508feae131SDavid Howells kleave(" = -EINVAL [miss]"); 15518feae131SDavid Howells return -EINVAL; 15528feae131SDavid Howells } 15538feae131SDavid Howells if (end == vma->vm_end) 15548feae131SDavid Howells goto erase_whole_vma; 15558feae131SDavid Howells rb = rb_next(&vma->vm_rb); 15568feae131SDavid Howells vma = rb_entry(rb, struct vm_area_struct, vm_rb); 15578feae131SDavid Howells } while (rb); 15588feae131SDavid Howells kleave(" = -EINVAL [split file]"); 15598feae131SDavid Howells return -EINVAL; 15608feae131SDavid Howells } else { 15618feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 15628feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 15638feae131SDavid Howells goto erase_whole_vma; 15648feae131SDavid Howells if (start < vma->vm_start || end > vma->vm_end) { 15658feae131SDavid Howells kleave(" = -EINVAL [superset]"); 15668feae131SDavid Howells return -EINVAL; 15678feae131SDavid Howells } 15688feae131SDavid Howells if (start & ~PAGE_MASK) { 15698feae131SDavid Howells kleave(" = -EINVAL [unaligned start]"); 15708feae131SDavid Howells return -EINVAL; 15718feae131SDavid Howells } 15728feae131SDavid Howells if (end != vma->vm_end && end & ~PAGE_MASK) { 15738feae131SDavid Howells kleave(" = -EINVAL [unaligned split]"); 15748feae131SDavid Howells return -EINVAL; 15758feae131SDavid Howells } 15768feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 15778feae131SDavid Howells ret = split_vma(mm, vma, start, 1); 15788feae131SDavid Howells if (ret < 0) { 15798feae131SDavid Howells kleave(" = %d [split]", ret); 15808feae131SDavid Howells return ret; 15818feae131SDavid Howells } 15828feae131SDavid Howells } 15838feae131SDavid Howells return shrink_vma(mm, vma, start, end); 15848feae131SDavid Howells } 15851da177e4SLinus Torvalds 15868feae131SDavid Howells erase_whole_vma: 15878feae131SDavid Howells delete_vma_from_mm(vma); 15888feae131SDavid Howells delete_vma(mm, vma); 15898feae131SDavid Howells kleave(" = 0"); 15901da177e4SLinus Torvalds return 0; 15911da177e4SLinus Torvalds } 1592b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap); 15931da177e4SLinus Torvalds 15946a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 15953034097aSDavid Howells { 15963034097aSDavid Howells int ret; 15973034097aSDavid Howells struct mm_struct *mm = current->mm; 15983034097aSDavid Howells 15993034097aSDavid Howells down_write(&mm->mmap_sem); 16003034097aSDavid Howells ret = do_munmap(mm, addr, len); 16013034097aSDavid Howells up_write(&mm->mmap_sem); 16023034097aSDavid Howells return ret; 16033034097aSDavid Howells } 16043034097aSDavid Howells 16053034097aSDavid Howells /* 16068feae131SDavid Howells * release all the mappings made in a process's VM space 16073034097aSDavid Howells */ 16081da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 16091da177e4SLinus Torvalds { 16108feae131SDavid Howells struct vm_area_struct *vma; 16111da177e4SLinus Torvalds 16128feae131SDavid Howells if (!mm) 16138feae131SDavid Howells return; 16148feae131SDavid Howells 16158feae131SDavid Howells kenter(""); 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds mm->total_vm = 0; 16181da177e4SLinus Torvalds 16198feae131SDavid Howells while ((vma = mm->mmap)) { 16208feae131SDavid Howells mm->mmap = vma->vm_next; 16218feae131SDavid Howells delete_vma_from_mm(vma); 16228feae131SDavid Howells delete_vma(mm, vma); 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16258feae131SDavid Howells kleave(""); 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len) 16291da177e4SLinus Torvalds { 16301da177e4SLinus Torvalds return -ENOMEM; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds /* 16346fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 16356fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 16361da177e4SLinus Torvalds * 16376fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 16388feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 16398feae131SDavid Howells * block is not shareable 16401da177e4SLinus Torvalds * 16416fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 16421da177e4SLinus Torvalds */ 16431da177e4SLinus Torvalds unsigned long do_mremap(unsigned long addr, 16441da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 16451da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 16461da177e4SLinus Torvalds { 16476fa5f80bSDavid Howells struct vm_area_struct *vma; 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds /* insanity checks first */ 16508feae131SDavid Howells if (old_len == 0 || new_len == 0) 16511da177e4SLinus Torvalds return (unsigned long) -EINVAL; 16521da177e4SLinus Torvalds 16538feae131SDavid Howells if (addr & ~PAGE_MASK) 16548feae131SDavid Howells return -EINVAL; 16558feae131SDavid Howells 16561da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 16571da177e4SLinus Torvalds return (unsigned long) -EINVAL; 16581da177e4SLinus Torvalds 16598feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 16606fa5f80bSDavid Howells if (!vma) 16611da177e4SLinus Torvalds return (unsigned long) -EINVAL; 16621da177e4SLinus Torvalds 16636fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 16641da177e4SLinus Torvalds return (unsigned long) -EFAULT; 16651da177e4SLinus Torvalds 16666fa5f80bSDavid Howells if (vma->vm_flags & VM_MAYSHARE) 16671da177e4SLinus Torvalds return (unsigned long) -EPERM; 16681da177e4SLinus Torvalds 16698feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 16701da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* all checks complete - do it */ 16736fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 16746fa5f80bSDavid Howells return vma->vm_start; 16756fa5f80bSDavid Howells } 1676b5073173SPaul Mundt EXPORT_SYMBOL(do_mremap); 16776fa5f80bSDavid Howells 16786a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 16796a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 16806a6160a7SHeiko Carstens unsigned long, new_addr) 16816fa5f80bSDavid Howells { 16826fa5f80bSDavid Howells unsigned long ret; 16836fa5f80bSDavid Howells 16846fa5f80bSDavid Howells down_write(¤t->mm->mmap_sem); 16856fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 16866fa5f80bSDavid Howells up_write(¤t->mm->mmap_sem); 16876fa5f80bSDavid Howells return ret; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16906aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1691deceb6cdSHugh Dickins unsigned int foll_flags) 16921da177e4SLinus Torvalds { 16931da177e4SLinus Torvalds return NULL; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long from, 16971da177e4SLinus Torvalds unsigned long to, unsigned long size, pgprot_t prot) 16981da177e4SLinus Torvalds { 169966aa2b4bSGreg Ungerer vma->vm_start = vma->vm_pgoff << PAGE_SHIFT; 170066aa2b4bSGreg Ungerer return 0; 17011da177e4SLinus Torvalds } 170222c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 17031da177e4SLinus Torvalds 1704f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1705f905bc44SPaul Mundt unsigned long pgoff) 1706f905bc44SPaul Mundt { 1707f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1708f905bc44SPaul Mundt 1709f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1710f905bc44SPaul Mundt return -EINVAL; 1711f905bc44SPaul Mundt 1712f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1713f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1714f905bc44SPaul Mundt 1715f905bc44SPaul Mundt return 0; 1716f905bc44SPaul Mundt } 1717f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1718f905bc44SPaul Mundt 17191da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr, 17241da177e4SLinus Torvalds unsigned long len, unsigned long pgoff, unsigned long flags) 17251da177e4SLinus Torvalds { 17261da177e4SLinus Torvalds return -ENOMEM; 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17291363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 17341da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, 17351da177e4SLinus Torvalds int even_cows) 17361da177e4SLinus Torvalds { 17371da177e4SLinus Torvalds } 173822c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range); 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds /* 1741d56e03cdSDavid Howells * ask for an unmapped area at which to create a mapping on a file 1742d56e03cdSDavid Howells */ 1743d56e03cdSDavid Howells unsigned long get_unmapped_area(struct file *file, unsigned long addr, 1744d56e03cdSDavid Howells unsigned long len, unsigned long pgoff, 1745d56e03cdSDavid Howells unsigned long flags) 1746d56e03cdSDavid Howells { 1747d56e03cdSDavid Howells unsigned long (*get_area)(struct file *, unsigned long, unsigned long, 1748d56e03cdSDavid Howells unsigned long, unsigned long); 1749d56e03cdSDavid Howells 1750d56e03cdSDavid Howells get_area = current->mm->get_unmapped_area; 1751d56e03cdSDavid Howells if (file && file->f_op && file->f_op->get_unmapped_area) 1752d56e03cdSDavid Howells get_area = file->f_op->get_unmapped_area; 1753d56e03cdSDavid Howells 1754d56e03cdSDavid Howells if (!get_area) 1755d56e03cdSDavid Howells return -ENOSYS; 1756d56e03cdSDavid Howells 1757d56e03cdSDavid Howells return get_area(file, addr, len, pgoff, flags); 1758d56e03cdSDavid Howells } 1759d56e03cdSDavid Howells EXPORT_SYMBOL(get_unmapped_area); 1760d56e03cdSDavid Howells 1761d56e03cdSDavid Howells /* 17621da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 17631da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 17641da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 17651da177e4SLinus Torvalds * 17661da177e4SLinus Torvalds * We currently support three overcommit policies, which are set via the 17671da177e4SLinus Torvalds * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 17681da177e4SLinus Torvalds * 17691da177e4SLinus Torvalds * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 17701da177e4SLinus Torvalds * Additional code 2002 Jul 20 by Robert Love. 17711da177e4SLinus Torvalds * 17721da177e4SLinus Torvalds * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 17731da177e4SLinus Torvalds * 17741da177e4SLinus Torvalds * Note this is a helper function intended to be used by LSMs which 17751da177e4SLinus Torvalds * wish to use this logic. 17761da177e4SLinus Torvalds */ 177734b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) 17781da177e4SLinus Torvalds { 17791da177e4SLinus Torvalds unsigned long free, allowed; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds vm_acct_memory(pages); 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /* 17841da177e4SLinus Torvalds * Sometimes we want to use more memory than we have 17851da177e4SLinus Torvalds */ 17861da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 17871da177e4SLinus Torvalds return 0; 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 17901da177e4SLinus Torvalds unsigned long n; 17911da177e4SLinus Torvalds 1792347ce434SChristoph Lameter free = global_page_state(NR_FILE_PAGES); 17931da177e4SLinus Torvalds free += nr_swap_pages; 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds /* 17961da177e4SLinus Torvalds * Any slabs which are created with the 17971da177e4SLinus Torvalds * SLAB_RECLAIM_ACCOUNT flag claim to have contents 17981da177e4SLinus Torvalds * which are reclaimable, under pressure. The dentry 17991da177e4SLinus Torvalds * cache and most inode caches should fall into this 18001da177e4SLinus Torvalds */ 1801972d1a7bSChristoph Lameter free += global_page_state(NR_SLAB_RECLAIMABLE); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds /* 18041da177e4SLinus Torvalds * Leave the last 3% for root 18051da177e4SLinus Torvalds */ 18061da177e4SLinus Torvalds if (!cap_sys_admin) 18071da177e4SLinus Torvalds free -= free / 32; 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds if (free > pages) 18101da177e4SLinus Torvalds return 0; 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds /* 18131da177e4SLinus Torvalds * nr_free_pages() is very expensive on large systems, 18141da177e4SLinus Torvalds * only call if we're about to fail. 18151da177e4SLinus Torvalds */ 18161da177e4SLinus Torvalds n = nr_free_pages(); 1817d5ddc79bSHideo AOKI 1818d5ddc79bSHideo AOKI /* 1819d5ddc79bSHideo AOKI * Leave reserved pages. The pages are not for anonymous pages. 1820d5ddc79bSHideo AOKI */ 1821d5ddc79bSHideo AOKI if (n <= totalreserve_pages) 1822d5ddc79bSHideo AOKI goto error; 1823d5ddc79bSHideo AOKI else 1824d5ddc79bSHideo AOKI n -= totalreserve_pages; 1825d5ddc79bSHideo AOKI 1826d5ddc79bSHideo AOKI /* 1827d5ddc79bSHideo AOKI * Leave the last 3% for root 1828d5ddc79bSHideo AOKI */ 18291da177e4SLinus Torvalds if (!cap_sys_admin) 18301da177e4SLinus Torvalds n -= n / 32; 18311da177e4SLinus Torvalds free += n; 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds if (free > pages) 18341da177e4SLinus Torvalds return 0; 1835d5ddc79bSHideo AOKI 1836d5ddc79bSHideo AOKI goto error; 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds allowed = totalram_pages * sysctl_overcommit_ratio / 100; 18401da177e4SLinus Torvalds /* 18411da177e4SLinus Torvalds * Leave the last 3% for root 18421da177e4SLinus Torvalds */ 18431da177e4SLinus Torvalds if (!cap_sys_admin) 18441da177e4SLinus Torvalds allowed -= allowed / 32; 18451da177e4SLinus Torvalds allowed += total_swap_pages; 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds /* Don't let a single process grow too big: 18481da177e4SLinus Torvalds leave 3% of the size of this process for other processes */ 1849731572d3SAlan Cox if (mm) 1850731572d3SAlan Cox allowed -= mm->total_vm / 32; 18511da177e4SLinus Torvalds 18522f60f8d3SSimon Derr /* 18532f60f8d3SSimon Derr * cast `allowed' as a signed long because vm_committed_space 18542f60f8d3SSimon Derr * sometimes has a negative value 18552f60f8d3SSimon Derr */ 185680119ef5SAlan Cox if (atomic_long_read(&vm_committed_space) < (long)allowed) 18571da177e4SLinus Torvalds return 0; 1858d5ddc79bSHideo AOKI error: 18591da177e4SLinus Torvalds vm_unacct_memory(pages); 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds return -ENOMEM; 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr) 18651da177e4SLinus Torvalds { 18661da177e4SLinus Torvalds return 0; 18671da177e4SLinus Torvalds } 1868b0e15190SDavid Howells 1869d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1870b0e15190SDavid Howells { 1871b0e15190SDavid Howells BUG(); 1872d0217ac0SNick Piggin return 0; 1873b0e15190SDavid Howells } 1874b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 18750ec76a11SDavid Howells 18760ec76a11SDavid Howells /* 18770ec76a11SDavid Howells * Access another process' address space. 18780ec76a11SDavid Howells * - source/target buffer must be kernel space 18790ec76a11SDavid Howells */ 18800ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 18810ec76a11SDavid Howells { 18820ec76a11SDavid Howells struct vm_area_struct *vma; 18830ec76a11SDavid Howells struct mm_struct *mm; 18840ec76a11SDavid Howells 18850ec76a11SDavid Howells if (addr + len < addr) 18860ec76a11SDavid Howells return 0; 18870ec76a11SDavid Howells 18880ec76a11SDavid Howells mm = get_task_mm(tsk); 18890ec76a11SDavid Howells if (!mm) 18900ec76a11SDavid Howells return 0; 18910ec76a11SDavid Howells 18920ec76a11SDavid Howells down_read(&mm->mmap_sem); 18930ec76a11SDavid Howells 18940ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 18950159b141SDavid Howells vma = find_vma(mm, addr); 18960159b141SDavid Howells if (vma) { 18970ec76a11SDavid Howells /* don't overrun this mapping */ 18980ec76a11SDavid Howells if (addr + len >= vma->vm_end) 18990ec76a11SDavid Howells len = vma->vm_end - addr; 19000ec76a11SDavid Howells 19010ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1902d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 19030ec76a11SDavid Howells len -= copy_to_user((void *) addr, buf, len); 1904d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 19050ec76a11SDavid Howells len -= copy_from_user(buf, (void *) addr, len); 19060ec76a11SDavid Howells else 19070ec76a11SDavid Howells len = 0; 19080ec76a11SDavid Howells } else { 19090ec76a11SDavid Howells len = 0; 19100ec76a11SDavid Howells } 19110ec76a11SDavid Howells 19120ec76a11SDavid Howells up_read(&mm->mmap_sem); 19130ec76a11SDavid Howells mmput(mm); 19140ec76a11SDavid Howells return len; 19150ec76a11SDavid Howells } 1916