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> 1329c185e5SPaul Mundt * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org> 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 16b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17b1de0d13SMitchel Humpherys 18b95f1b31SPaul Gortmaker #include <linux/export.h> 191da177e4SLinus Torvalds #include <linux/mm.h> 20615d6e87SDavidlohr Bueso #include <linux/vmacache.h> 211da177e4SLinus Torvalds #include <linux/mman.h> 221da177e4SLinus Torvalds #include <linux/swap.h> 231da177e4SLinus Torvalds #include <linux/file.h> 241da177e4SLinus Torvalds #include <linux/highmem.h> 251da177e4SLinus Torvalds #include <linux/pagemap.h> 261da177e4SLinus Torvalds #include <linux/slab.h> 271da177e4SLinus Torvalds #include <linux/vmalloc.h> 281da177e4SLinus Torvalds #include <linux/blkdev.h> 291da177e4SLinus Torvalds #include <linux/backing-dev.h> 303b32123dSGideon Israel Dsouza #include <linux/compiler.h> 311da177e4SLinus Torvalds #include <linux/mount.h> 321da177e4SLinus Torvalds #include <linux/personality.h> 331da177e4SLinus Torvalds #include <linux/security.h> 341da177e4SLinus Torvalds #include <linux/syscalls.h> 35120a795dSAl Viro #include <linux/audit.h> 36cf4aebc2SClark Williams #include <linux/sched/sysctl.h> 37b1de0d13SMitchel Humpherys #include <linux/printk.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <asm/uaccess.h> 401da177e4SLinus Torvalds #include <asm/tlb.h> 411da177e4SLinus Torvalds #include <asm/tlbflush.h> 42eb8cdec4SBernd Schmidt #include <asm/mmu_context.h> 438feae131SDavid Howells #include "internal.h" 448feae131SDavid Howells 451da177e4SLinus Torvalds void *high_memory; 46944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory); 471da177e4SLinus Torvalds struct page *mem_map; 481da177e4SLinus Torvalds unsigned long max_mapnr; 495b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr); 504266c97aSHugh Dickins unsigned long highest_memmap_pfn; 5100a62ce9SKOSAKI Motohiro struct percpu_counter vm_committed_as; 521da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */ 531da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */ 5449f0ce5fSJerome Marchand unsigned long sysctl_overcommit_kbytes __read_mostly; 551da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; 56fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; 57c9b1d098SAndrew Shewmaker unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */ 584eeab4f5SAndrew Shewmaker unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */ 591da177e4SLinus Torvalds int heap_stack_gap = 0; 601da177e4SLinus Torvalds 6133e5d769SDavid Howells atomic_long_t mmap_pages_allocated; 628feae131SDavid Howells 63997071bcSK. Y. Srinivasan /* 64997071bcSK. Y. Srinivasan * The global memory commitment made in the system can be a metric 65997071bcSK. Y. Srinivasan * that can be used to drive ballooning decisions when Linux is hosted 66997071bcSK. Y. Srinivasan * as a guest. On Hyper-V, the host implements a policy engine for dynamically 67997071bcSK. Y. Srinivasan * balancing memory across competing virtual machines that are hosted. 68997071bcSK. Y. Srinivasan * Several metrics drive this policy engine including the guest reported 69997071bcSK. Y. Srinivasan * memory commitment. 70997071bcSK. Y. Srinivasan */ 71997071bcSK. Y. Srinivasan unsigned long vm_memory_committed(void) 72997071bcSK. Y. Srinivasan { 73997071bcSK. Y. Srinivasan return percpu_counter_read_positive(&vm_committed_as); 74997071bcSK. Y. Srinivasan } 75997071bcSK. Y. Srinivasan 76997071bcSK. Y. Srinivasan EXPORT_SYMBOL_GPL(vm_memory_committed); 77997071bcSK. Y. Srinivasan 781da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 791da177e4SLinus Torvalds 808feae131SDavid Howells /* list of mapped, potentially shareable regions */ 818feae131SDavid Howells static struct kmem_cache *vm_region_jar; 828feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT; 838feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem); 841da177e4SLinus Torvalds 85f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = { 861da177e4SLinus Torvalds }; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* 891da177e4SLinus Torvalds * Return the total memory allocated for this pointer, not 901da177e4SLinus Torvalds * just what the caller asked for. 911da177e4SLinus Torvalds * 921da177e4SLinus Torvalds * Doesn't have to be accurate, i.e. may have races. 931da177e4SLinus Torvalds */ 941da177e4SLinus Torvalds unsigned int kobjsize(const void *objp) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds struct page *page; 971da177e4SLinus Torvalds 984016a139SMichael Hennerich /* 994016a139SMichael Hennerich * If the object we have should not have ksize performed on it, 1004016a139SMichael Hennerich * return size of 0 1014016a139SMichael Hennerich */ 1025a1603beSPaul Mundt if (!objp || !virt_addr_valid(objp)) 1036cfd53fcSPaul Mundt return 0; 1046cfd53fcSPaul Mundt 1056cfd53fcSPaul Mundt page = virt_to_head_page(objp); 1066cfd53fcSPaul Mundt 1076cfd53fcSPaul Mundt /* 1086cfd53fcSPaul Mundt * If the allocator sets PageSlab, we know the pointer came from 1096cfd53fcSPaul Mundt * kmalloc(). 1106cfd53fcSPaul Mundt */ 1111da177e4SLinus Torvalds if (PageSlab(page)) 1121da177e4SLinus Torvalds return ksize(objp); 1131da177e4SLinus Torvalds 1146cfd53fcSPaul Mundt /* 115ab2e83eaSPaul Mundt * If it's not a compound page, see if we have a matching VMA 116ab2e83eaSPaul Mundt * region. This test is intentionally done in reverse order, 117ab2e83eaSPaul Mundt * so if there's no VMA, we still fall through and hand back 118ab2e83eaSPaul Mundt * PAGE_SIZE for 0-order pages. 119ab2e83eaSPaul Mundt */ 120ab2e83eaSPaul Mundt if (!PageCompound(page)) { 121ab2e83eaSPaul Mundt struct vm_area_struct *vma; 122ab2e83eaSPaul Mundt 123ab2e83eaSPaul Mundt vma = find_vma(current->mm, (unsigned long)objp); 124ab2e83eaSPaul Mundt if (vma) 125ab2e83eaSPaul Mundt return vma->vm_end - vma->vm_start; 126ab2e83eaSPaul Mundt } 127ab2e83eaSPaul Mundt 128ab2e83eaSPaul Mundt /* 1296cfd53fcSPaul Mundt * The ksize() function is only guaranteed to work for pointers 1305a1603beSPaul Mundt * returned by kmalloc(). So handle arbitrary pointers here. 1316cfd53fcSPaul Mundt */ 1325a1603beSPaul Mundt return PAGE_SIZE << compound_order(page); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 13528a35716SMichel Lespinasse long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 13628a35716SMichel Lespinasse unsigned long start, unsigned long nr_pages, 13728a35716SMichel Lespinasse unsigned int foll_flags, struct page **pages, 13828a35716SMichel Lespinasse struct vm_area_struct **vmas, int *nonblocking) 1391da177e4SLinus Torvalds { 140910e46daSSonic Zhang struct vm_area_struct *vma; 1417b4d5b8bSDavid Howells unsigned long vm_flags; 1427b4d5b8bSDavid Howells int i; 1437b4d5b8bSDavid Howells 1447b4d5b8bSDavid Howells /* calculate required read or write permissions. 14558fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 1467b4d5b8bSDavid Howells */ 14758fa879eSHugh Dickins vm_flags = (foll_flags & FOLL_WRITE) ? 14858fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 14958fa879eSHugh Dickins vm_flags &= (foll_flags & FOLL_FORCE) ? 15058fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 1511da177e4SLinus Torvalds 1529d73777eSPeter Zijlstra for (i = 0; i < nr_pages; i++) { 1537561e8caSDavid Howells vma = find_vma(mm, start); 154910e46daSSonic Zhang if (!vma) 1557b4d5b8bSDavid Howells goto finish_or_fault; 1567b4d5b8bSDavid Howells 1577b4d5b8bSDavid Howells /* protect what we can, including chardevs */ 1581c3aff1cSHugh Dickins if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || 1591c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 1607b4d5b8bSDavid Howells goto finish_or_fault; 161910e46daSSonic Zhang 1621da177e4SLinus Torvalds if (pages) { 1631da177e4SLinus Torvalds pages[i] = virt_to_page(start); 1641da177e4SLinus Torvalds if (pages[i]) 1651da177e4SLinus Torvalds page_cache_get(pages[i]); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds if (vmas) 168910e46daSSonic Zhang vmas[i] = vma; 169e1ee65d8SDavid Howells start = (start + PAGE_SIZE) & PAGE_MASK; 1701da177e4SLinus Torvalds } 1717b4d5b8bSDavid Howells 1727b4d5b8bSDavid Howells return i; 1737b4d5b8bSDavid Howells 1747b4d5b8bSDavid Howells finish_or_fault: 1757b4d5b8bSDavid Howells return i ? : -EFAULT; 1761da177e4SLinus Torvalds } 177b291f000SNick Piggin 178b291f000SNick Piggin /* 179b291f000SNick Piggin * get a list of pages in an address range belonging to the specified process 180b291f000SNick Piggin * and indicate the VMA that covers each page 181b291f000SNick Piggin * - this is potentially dodgy as we may end incrementing the page count of a 182b291f000SNick Piggin * slab page or a secondary page from a compound page 183b291f000SNick Piggin * - don't permit access to VMAs that don't support it, such as I/O mappings 184b291f000SNick Piggin */ 18528a35716SMichel Lespinasse long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 18628a35716SMichel Lespinasse unsigned long start, unsigned long nr_pages, 18728a35716SMichel Lespinasse int write, int force, struct page **pages, 18828a35716SMichel Lespinasse struct vm_area_struct **vmas) 189b291f000SNick Piggin { 190b291f000SNick Piggin int flags = 0; 191b291f000SNick Piggin 192b291f000SNick Piggin if (write) 19358fa879eSHugh Dickins flags |= FOLL_WRITE; 194b291f000SNick Piggin if (force) 19558fa879eSHugh Dickins flags |= FOLL_FORCE; 196b291f000SNick Piggin 19753a7706dSMichel Lespinasse return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, 19853a7706dSMichel Lespinasse NULL); 199b291f000SNick Piggin } 20066aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages); 20166aa2b4bSGreg Ungerer 202f0818f47SAndrea Arcangeli long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm, 203f0818f47SAndrea Arcangeli unsigned long start, unsigned long nr_pages, 204f0818f47SAndrea Arcangeli int write, int force, struct page **pages, 205f0818f47SAndrea Arcangeli int *locked) 206f0818f47SAndrea Arcangeli { 207f0818f47SAndrea Arcangeli return get_user_pages(tsk, mm, start, nr_pages, write, force, 208f0818f47SAndrea Arcangeli pages, NULL); 209f0818f47SAndrea Arcangeli } 210f0818f47SAndrea Arcangeli EXPORT_SYMBOL(get_user_pages_locked); 211f0818f47SAndrea Arcangeli 2120fd71a56SAndrea Arcangeli long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, 213f0818f47SAndrea Arcangeli unsigned long start, unsigned long nr_pages, 2140fd71a56SAndrea Arcangeli int write, int force, struct page **pages, 2150fd71a56SAndrea Arcangeli unsigned int gup_flags) 216f0818f47SAndrea Arcangeli { 217f0818f47SAndrea Arcangeli long ret; 218f0818f47SAndrea Arcangeli down_read(&mm->mmap_sem); 219f0818f47SAndrea Arcangeli ret = get_user_pages(tsk, mm, start, nr_pages, write, force, 220f0818f47SAndrea Arcangeli pages, NULL); 221f0818f47SAndrea Arcangeli up_read(&mm->mmap_sem); 222f0818f47SAndrea Arcangeli return ret; 223f0818f47SAndrea Arcangeli } 2240fd71a56SAndrea Arcangeli EXPORT_SYMBOL(__get_user_pages_unlocked); 2250fd71a56SAndrea Arcangeli 2260fd71a56SAndrea Arcangeli long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm, 2270fd71a56SAndrea Arcangeli unsigned long start, unsigned long nr_pages, 2280fd71a56SAndrea Arcangeli int write, int force, struct page **pages) 2290fd71a56SAndrea Arcangeli { 2300fd71a56SAndrea Arcangeli return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write, 2310fd71a56SAndrea Arcangeli force, pages, 0); 2320fd71a56SAndrea Arcangeli } 233f0818f47SAndrea Arcangeli EXPORT_SYMBOL(get_user_pages_unlocked); 234f0818f47SAndrea Arcangeli 235dfc2f91aSPaul Mundt /** 236dfc2f91aSPaul Mundt * follow_pfn - look up PFN at a user virtual address 237dfc2f91aSPaul Mundt * @vma: memory mapping 238dfc2f91aSPaul Mundt * @address: user virtual address 239dfc2f91aSPaul Mundt * @pfn: location to store found PFN 240dfc2f91aSPaul Mundt * 241dfc2f91aSPaul Mundt * Only IO mappings and raw PFN mappings are allowed. 242dfc2f91aSPaul Mundt * 243dfc2f91aSPaul Mundt * Returns zero and the pfn at @pfn on success, -ve otherwise. 244dfc2f91aSPaul Mundt */ 245dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address, 246dfc2f91aSPaul Mundt unsigned long *pfn) 247dfc2f91aSPaul Mundt { 248dfc2f91aSPaul Mundt if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 249dfc2f91aSPaul Mundt return -EINVAL; 250dfc2f91aSPaul Mundt 251dfc2f91aSPaul Mundt *pfn = address >> PAGE_SHIFT; 252dfc2f91aSPaul Mundt return 0; 253dfc2f91aSPaul Mundt } 254dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn); 255dfc2f91aSPaul Mundt 256f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 2571da177e4SLinus Torvalds 258b3bdda02SChristoph Lameter void vfree(const void *addr) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds kfree(addr); 2611da177e4SLinus Torvalds } 262b5073173SPaul Mundt EXPORT_SYMBOL(vfree); 2631da177e4SLinus Torvalds 264dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds /* 2678518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 2688518609dSRobert P. J. Day * returns only a logical address. 2691da177e4SLinus Torvalds */ 27084097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 2711da177e4SLinus Torvalds } 272b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc); 2731da177e4SLinus Torvalds 274f905bc44SPaul Mundt void *vmalloc_user(unsigned long size) 275f905bc44SPaul Mundt { 276f905bc44SPaul Mundt void *ret; 277f905bc44SPaul Mundt 278f905bc44SPaul Mundt ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 279f905bc44SPaul Mundt PAGE_KERNEL); 280f905bc44SPaul Mundt if (ret) { 281f905bc44SPaul Mundt struct vm_area_struct *vma; 282f905bc44SPaul Mundt 283f905bc44SPaul Mundt down_write(¤t->mm->mmap_sem); 284f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret); 285f905bc44SPaul Mundt if (vma) 286f905bc44SPaul Mundt vma->vm_flags |= VM_USERMAP; 287f905bc44SPaul Mundt up_write(¤t->mm->mmap_sem); 288f905bc44SPaul Mundt } 289f905bc44SPaul Mundt 290f905bc44SPaul Mundt return ret; 291f905bc44SPaul Mundt } 292f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user); 293f905bc44SPaul Mundt 294b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds return virt_to_page(addr); 2971da177e4SLinus Torvalds } 298b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page); 2991da177e4SLinus Torvalds 300b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr) 3011da177e4SLinus Torvalds { 3021da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr)); 3031da177e4SLinus Torvalds } 304b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 3071da177e4SLinus Torvalds { 3089bde916bSChen Gang /* Don't allow overflow */ 3099bde916bSChen Gang if ((unsigned long) buf + count < count) 3109bde916bSChen Gang count = -(unsigned long) buf; 3119bde916bSChen Gang 3121da177e4SLinus Torvalds memcpy(buf, addr, count); 3131da177e4SLinus Torvalds return count; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 3171da177e4SLinus Torvalds { 3181da177e4SLinus Torvalds /* Don't allow overflow */ 3191da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 3201da177e4SLinus Torvalds count = -(unsigned long) addr; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds memcpy(addr, buf, count); 323ac714904SChoi Gi-yong return count; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* 3271da177e4SLinus Torvalds * vmalloc - allocate virtually continguos memory 3281da177e4SLinus Torvalds * 3291da177e4SLinus Torvalds * @size: allocation size 3301da177e4SLinus Torvalds * 3311da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 3321da177e4SLinus Torvalds * allocator and map them into continguos kernel virtual space. 3331da177e4SLinus Torvalds * 334c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 3351da177e4SLinus Torvalds * use __vmalloc() instead. 3361da177e4SLinus Torvalds */ 3371da177e4SLinus Torvalds void *vmalloc(unsigned long size) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL); 3401da177e4SLinus Torvalds } 341f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc); 342f6138882SAndrew Morton 343e1ca7788SDave Young /* 344e1ca7788SDave Young * vzalloc - allocate virtually continguos memory with zero fill 345e1ca7788SDave Young * 346e1ca7788SDave Young * @size: allocation size 347e1ca7788SDave Young * 348e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 349e1ca7788SDave Young * allocator and map them into continguos kernel virtual space. 350e1ca7788SDave Young * The memory allocated is set to zero. 351e1ca7788SDave Young * 352e1ca7788SDave Young * For tight control over page level allocator and protection flags 353e1ca7788SDave Young * use __vmalloc() instead. 354e1ca7788SDave Young */ 355e1ca7788SDave Young void *vzalloc(unsigned long size) 356e1ca7788SDave Young { 357e1ca7788SDave Young return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 358e1ca7788SDave Young PAGE_KERNEL); 359e1ca7788SDave Young } 360e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 361e1ca7788SDave Young 362e1ca7788SDave Young /** 363e1ca7788SDave Young * vmalloc_node - allocate memory on a specific node 364e1ca7788SDave Young * @size: allocation size 365e1ca7788SDave Young * @node: numa node 366e1ca7788SDave Young * 367e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 368e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 369e1ca7788SDave Young * 370e1ca7788SDave Young * For tight control over page level allocator and protection flags 371e1ca7788SDave Young * use __vmalloc() instead. 372e1ca7788SDave Young */ 373f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node) 374f6138882SAndrew Morton { 375f6138882SAndrew Morton return vmalloc(size); 376f6138882SAndrew Morton } 3779a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node); 378e1ca7788SDave Young 379e1ca7788SDave Young /** 380e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 381e1ca7788SDave Young * @size: allocation size 382e1ca7788SDave Young * @node: numa node 383e1ca7788SDave Young * 384e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 385e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 386e1ca7788SDave Young * The memory allocated is set to zero. 387e1ca7788SDave Young * 388e1ca7788SDave Young * For tight control over page level allocator and protection flags 389e1ca7788SDave Young * use __vmalloc() instead. 390e1ca7788SDave Young */ 391e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 392e1ca7788SDave Young { 393e1ca7788SDave Young return vzalloc(size); 394e1ca7788SDave Young } 395e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 3961da177e4SLinus Torvalds 3971af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC 3981af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL 3991af446edSPaul Mundt #endif 4001af446edSPaul Mundt 4011af446edSPaul Mundt /** 4021af446edSPaul Mundt * vmalloc_exec - allocate virtually contiguous, executable memory 4031af446edSPaul Mundt * @size: allocation size 4041af446edSPaul Mundt * 4051af446edSPaul Mundt * Kernel-internal function to allocate enough pages to cover @size 4061af446edSPaul Mundt * the page level allocator and map them into contiguous and 4071af446edSPaul Mundt * executable kernel virtual space. 4081af446edSPaul Mundt * 4091af446edSPaul Mundt * For tight control over page level allocator and protection flags 4101af446edSPaul Mundt * use __vmalloc() instead. 4111af446edSPaul Mundt */ 4121af446edSPaul Mundt 4131af446edSPaul Mundt void *vmalloc_exec(unsigned long size) 4141af446edSPaul Mundt { 4151af446edSPaul Mundt return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC); 4161af446edSPaul Mundt } 4171af446edSPaul Mundt 418b5073173SPaul Mundt /** 419b5073173SPaul Mundt * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 4201da177e4SLinus Torvalds * @size: allocation size 4211da177e4SLinus Torvalds * 4221da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 4231da177e4SLinus Torvalds * page level allocator and map them into continguos kernel virtual space. 4241da177e4SLinus Torvalds */ 4251da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL); 4281da177e4SLinus Torvalds } 429b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32); 430b5073173SPaul Mundt 431b5073173SPaul Mundt /** 432b5073173SPaul Mundt * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 433b5073173SPaul Mundt * @size: allocation size 434b5073173SPaul Mundt * 435b5073173SPaul Mundt * The resulting memory area is 32bit addressable and zeroed so it can be 436b5073173SPaul Mundt * mapped to userspace without leaking data. 437f905bc44SPaul Mundt * 438f905bc44SPaul Mundt * VM_USERMAP is set on the corresponding VMA so that subsequent calls to 439f905bc44SPaul Mundt * remap_vmalloc_range() are permissible. 440b5073173SPaul Mundt */ 441b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size) 442b5073173SPaul Mundt { 443f905bc44SPaul Mundt /* 444f905bc44SPaul Mundt * We'll have to sort out the ZONE_DMA bits for 64-bit, 445f905bc44SPaul Mundt * but for now this can simply use vmalloc_user() directly. 446f905bc44SPaul Mundt */ 447f905bc44SPaul Mundt return vmalloc_user(size); 448b5073173SPaul Mundt } 449b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) 4521da177e4SLinus Torvalds { 4531da177e4SLinus Torvalds BUG(); 4541da177e4SLinus Torvalds return NULL; 4551da177e4SLinus Torvalds } 456b5073173SPaul Mundt EXPORT_SYMBOL(vmap); 4571da177e4SLinus Torvalds 458b3bdda02SChristoph Lameter void vunmap(const void *addr) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds BUG(); 4611da177e4SLinus Torvalds } 462b5073173SPaul Mundt EXPORT_SYMBOL(vunmap); 4631da177e4SLinus Torvalds 464eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 465eb6434d9SPaul Mundt { 466eb6434d9SPaul Mundt BUG(); 467eb6434d9SPaul Mundt return NULL; 468eb6434d9SPaul Mundt } 469eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram); 470eb6434d9SPaul Mundt 471eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count) 472eb6434d9SPaul Mundt { 473eb6434d9SPaul Mundt BUG(); 474eb6434d9SPaul Mundt } 475eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram); 476eb6434d9SPaul Mundt 477eb6434d9SPaul Mundt void vm_unmap_aliases(void) 478eb6434d9SPaul Mundt { 479eb6434d9SPaul Mundt } 480eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases); 481eb6434d9SPaul Mundt 4821da177e4SLinus Torvalds /* 4831eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 4841eeb66a1SChristoph Hellwig * have one. 4851eeb66a1SChristoph Hellwig */ 4863b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void) 4871eeb66a1SChristoph Hellwig { 4881eeb66a1SChristoph Hellwig } 4891eeb66a1SChristoph Hellwig 49029c185e5SPaul Mundt /** 49129c185e5SPaul Mundt * alloc_vm_area - allocate a range of kernel address space 49229c185e5SPaul Mundt * @size: size of the area 49329c185e5SPaul Mundt * 49429c185e5SPaul Mundt * Returns: NULL on failure, vm_struct on success 49529c185e5SPaul Mundt * 49629c185e5SPaul Mundt * This function reserves a range of kernel address space, and 49729c185e5SPaul Mundt * allocates pagetables to map that range. No actual mappings 49829c185e5SPaul Mundt * are created. If the kernel address space is not shared 49929c185e5SPaul Mundt * between processes, it syncs the pagetable across all 50029c185e5SPaul Mundt * processes. 50129c185e5SPaul Mundt */ 502cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) 50329c185e5SPaul Mundt { 50429c185e5SPaul Mundt BUG(); 50529c185e5SPaul Mundt return NULL; 50629c185e5SPaul Mundt } 50729c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area); 50829c185e5SPaul Mundt 50929c185e5SPaul Mundt void free_vm_area(struct vm_struct *area) 51029c185e5SPaul Mundt { 51129c185e5SPaul Mundt BUG(); 51229c185e5SPaul Mundt } 51329c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area); 51429c185e5SPaul Mundt 515b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 516b5073173SPaul Mundt struct page *page) 517b5073173SPaul Mundt { 518b5073173SPaul Mundt return -EINVAL; 519b5073173SPaul Mundt } 520b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page); 521b5073173SPaul Mundt 5221eeb66a1SChristoph Hellwig /* 5231da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel 5241da177e4SLinus Torvalds * lock, except when an application is doing something nasty 5251da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped 5261da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need 5271da177e4SLinus Torvalds * to invoke file system routines that need the global lock. 5281da177e4SLinus Torvalds */ 5296a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 5301da177e4SLinus Torvalds { 5311da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds if (brk < mm->start_brk || brk > mm->context.end_brk) 5341da177e4SLinus Torvalds return mm->brk; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (mm->brk == brk) 5371da177e4SLinus Torvalds return mm->brk; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* 5401da177e4SLinus Torvalds * Always allow shrinking brk 5411da177e4SLinus Torvalds */ 5421da177e4SLinus Torvalds if (brk <= mm->brk) { 5431da177e4SLinus Torvalds mm->brk = brk; 5441da177e4SLinus Torvalds return brk; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* 5481da177e4SLinus Torvalds * Ok, looks good - let it rip. 5491da177e4SLinus Torvalds */ 550cfe79c00SMike Frysinger flush_icache_range(mm->brk, brk); 5511da177e4SLinus Torvalds return mm->brk = brk; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5548feae131SDavid Howells /* 5558feae131SDavid Howells * initialise the VMA and region record slabs 5568feae131SDavid Howells */ 5578feae131SDavid Howells void __init mmap_init(void) 5581da177e4SLinus Torvalds { 55900a62ce9SKOSAKI Motohiro int ret; 56000a62ce9SKOSAKI Motohiro 561908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 56200a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 56333e5d769SDavid Howells vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC); 5648feae131SDavid Howells } 5651da177e4SLinus Torvalds 5668feae131SDavid Howells /* 5678feae131SDavid Howells * validate the region tree 5688feae131SDavid Howells * - the caller must hold the region lock 5698feae131SDavid Howells */ 5708feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS 5718feae131SDavid Howells static noinline void validate_nommu_regions(void) 5728feae131SDavid Howells { 5738feae131SDavid Howells struct vm_region *region, *last; 5748feae131SDavid Howells struct rb_node *p, *lastp; 5751da177e4SLinus Torvalds 5768feae131SDavid Howells lastp = rb_first(&nommu_region_tree); 5778feae131SDavid Howells if (!lastp) 5788feae131SDavid Howells return; 5798feae131SDavid Howells 5808feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 58133e5d769SDavid Howells BUG_ON(unlikely(last->vm_end <= last->vm_start)); 58233e5d769SDavid Howells BUG_ON(unlikely(last->vm_top < last->vm_end)); 5838feae131SDavid Howells 5848feae131SDavid Howells while ((p = rb_next(lastp))) { 5858feae131SDavid Howells region = rb_entry(p, struct vm_region, vm_rb); 5868feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 5878feae131SDavid Howells 58833e5d769SDavid Howells BUG_ON(unlikely(region->vm_end <= region->vm_start)); 58933e5d769SDavid Howells BUG_ON(unlikely(region->vm_top < region->vm_end)); 59033e5d769SDavid Howells BUG_ON(unlikely(region->vm_start < last->vm_top)); 5918feae131SDavid Howells 5928feae131SDavid Howells lastp = p; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds } 5958feae131SDavid Howells #else 59633e5d769SDavid Howells static void validate_nommu_regions(void) 59733e5d769SDavid Howells { 59833e5d769SDavid Howells } 5998feae131SDavid Howells #endif 6008feae131SDavid Howells 6018feae131SDavid Howells /* 6028feae131SDavid Howells * add a region into the global tree 6038feae131SDavid Howells */ 6048feae131SDavid Howells static void add_nommu_region(struct vm_region *region) 6058feae131SDavid Howells { 6068feae131SDavid Howells struct vm_region *pregion; 6078feae131SDavid Howells struct rb_node **p, *parent; 6088feae131SDavid Howells 6098feae131SDavid Howells validate_nommu_regions(); 6108feae131SDavid Howells 6118feae131SDavid Howells parent = NULL; 6128feae131SDavid Howells p = &nommu_region_tree.rb_node; 6138feae131SDavid Howells while (*p) { 6148feae131SDavid Howells parent = *p; 6158feae131SDavid Howells pregion = rb_entry(parent, struct vm_region, vm_rb); 6168feae131SDavid Howells if (region->vm_start < pregion->vm_start) 6178feae131SDavid Howells p = &(*p)->rb_left; 6188feae131SDavid Howells else if (region->vm_start > pregion->vm_start) 6198feae131SDavid Howells p = &(*p)->rb_right; 6208feae131SDavid Howells else if (pregion == region) 6218feae131SDavid Howells return; 6228feae131SDavid Howells else 6238feae131SDavid Howells BUG(); 6248feae131SDavid Howells } 6258feae131SDavid Howells 6268feae131SDavid Howells rb_link_node(®ion->vm_rb, parent, p); 6278feae131SDavid Howells rb_insert_color(®ion->vm_rb, &nommu_region_tree); 6288feae131SDavid Howells 6298feae131SDavid Howells validate_nommu_regions(); 6308feae131SDavid Howells } 6318feae131SDavid Howells 6328feae131SDavid Howells /* 6338feae131SDavid Howells * delete a region from the global tree 6348feae131SDavid Howells */ 6358feae131SDavid Howells static void delete_nommu_region(struct vm_region *region) 6368feae131SDavid Howells { 6378feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 6388feae131SDavid Howells 6398feae131SDavid Howells validate_nommu_regions(); 6408feae131SDavid Howells rb_erase(®ion->vm_rb, &nommu_region_tree); 6418feae131SDavid Howells validate_nommu_regions(); 6428feae131SDavid Howells } 6438feae131SDavid Howells 6448feae131SDavid Howells /* 6458feae131SDavid Howells * free a contiguous series of pages 6468feae131SDavid Howells */ 6478feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to) 6488feae131SDavid Howells { 6498feae131SDavid Howells for (; from < to; from += PAGE_SIZE) { 6508feae131SDavid Howells struct page *page = virt_to_page(from); 6518feae131SDavid Howells 65233e5d769SDavid Howells atomic_long_dec(&mmap_pages_allocated); 6538feae131SDavid Howells put_page(page); 6548feae131SDavid Howells } 6558feae131SDavid Howells } 6568feae131SDavid Howells 6578feae131SDavid Howells /* 6588feae131SDavid Howells * release a reference to a region 65933e5d769SDavid Howells * - the caller must hold the region semaphore for writing, which this releases 660dd8632a1SPaul Mundt * - the region may not have been added to the tree yet, in which case vm_top 6618feae131SDavid Howells * will equal vm_start 6628feae131SDavid Howells */ 6638feae131SDavid Howells static void __put_nommu_region(struct vm_region *region) 6648feae131SDavid Howells __releases(nommu_region_sem) 6658feae131SDavid Howells { 6668feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 6678feae131SDavid Howells 6681e2ae599SDavid Howells if (--region->vm_usage == 0) { 669dd8632a1SPaul Mundt if (region->vm_top > region->vm_start) 6708feae131SDavid Howells delete_nommu_region(region); 6718feae131SDavid Howells up_write(&nommu_region_sem); 6728feae131SDavid Howells 6738feae131SDavid Howells if (region->vm_file) 6748feae131SDavid Howells fput(region->vm_file); 6758feae131SDavid Howells 6768feae131SDavid Howells /* IO memory and memory shared directly out of the pagecache 6778feae131SDavid Howells * from ramfs/tmpfs mustn't be released here */ 678*22cc877bSLeon Romanovsky if (region->vm_flags & VM_MAPPED_COPY) 679dd8632a1SPaul Mundt free_page_series(region->vm_start, region->vm_top); 6808feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 6818feae131SDavid Howells } else { 6828feae131SDavid Howells up_write(&nommu_region_sem); 6838feae131SDavid Howells } 6848feae131SDavid Howells } 6858feae131SDavid Howells 6868feae131SDavid Howells /* 6878feae131SDavid Howells * release a reference to a region 6888feae131SDavid Howells */ 6898feae131SDavid Howells static void put_nommu_region(struct vm_region *region) 6908feae131SDavid Howells { 6918feae131SDavid Howells down_write(&nommu_region_sem); 6928feae131SDavid Howells __put_nommu_region(region); 6938feae131SDavid Howells } 6941da177e4SLinus Torvalds 6953034097aSDavid Howells /* 696eb8cdec4SBernd Schmidt * update protection on a vma 697eb8cdec4SBernd Schmidt */ 698eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags) 699eb8cdec4SBernd Schmidt { 700eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU 701eb8cdec4SBernd Schmidt struct mm_struct *mm = vma->vm_mm; 702eb8cdec4SBernd Schmidt long start = vma->vm_start & PAGE_MASK; 703eb8cdec4SBernd Schmidt while (start < vma->vm_end) { 704eb8cdec4SBernd Schmidt protect_page(mm, start, flags); 705eb8cdec4SBernd Schmidt start += PAGE_SIZE; 706eb8cdec4SBernd Schmidt } 707eb8cdec4SBernd Schmidt update_protections(mm); 708eb8cdec4SBernd Schmidt #endif 709eb8cdec4SBernd Schmidt } 710eb8cdec4SBernd Schmidt 711eb8cdec4SBernd Schmidt /* 7123034097aSDavid Howells * add a VMA into a process's mm_struct in the appropriate place in the list 7138feae131SDavid Howells * and tree and add to the address space's page tree also if not an anonymous 7148feae131SDavid Howells * page 7153034097aSDavid Howells * - should be called with mm->mmap_sem held writelocked 7163034097aSDavid Howells */ 7178feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) 7183034097aSDavid Howells { 7196038def0SNamhyung Kim struct vm_area_struct *pvma, *prev; 7208feae131SDavid Howells struct address_space *mapping; 7216038def0SNamhyung Kim struct rb_node **p, *parent, *rb_prev; 7223034097aSDavid Howells 7238feae131SDavid Howells BUG_ON(!vma->vm_region); 7248feae131SDavid Howells 7258feae131SDavid Howells mm->map_count++; 7268feae131SDavid Howells vma->vm_mm = mm; 7278feae131SDavid Howells 728eb8cdec4SBernd Schmidt protect_vma(vma, vma->vm_flags); 729eb8cdec4SBernd Schmidt 7308feae131SDavid Howells /* add the VMA to the mapping */ 7318feae131SDavid Howells if (vma->vm_file) { 7328feae131SDavid Howells mapping = vma->vm_file->f_mapping; 7338feae131SDavid Howells 73483cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 7358feae131SDavid Howells flush_dcache_mmap_lock(mapping); 7366b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 7378feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 73883cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 7398feae131SDavid Howells } 7408feae131SDavid Howells 7418feae131SDavid Howells /* add the VMA to the tree */ 7426038def0SNamhyung Kim parent = rb_prev = NULL; 7438feae131SDavid Howells p = &mm->mm_rb.rb_node; 7448feae131SDavid Howells while (*p) { 7458feae131SDavid Howells parent = *p; 7468feae131SDavid Howells pvma = rb_entry(parent, struct vm_area_struct, vm_rb); 7478feae131SDavid Howells 7488feae131SDavid Howells /* sort by: start addr, end addr, VMA struct addr in that order 7498feae131SDavid Howells * (the latter is necessary as we may get identical VMAs) */ 7508feae131SDavid Howells if (vma->vm_start < pvma->vm_start) 7518feae131SDavid Howells p = &(*p)->rb_left; 7526038def0SNamhyung Kim else if (vma->vm_start > pvma->vm_start) { 7536038def0SNamhyung Kim rb_prev = parent; 7548feae131SDavid Howells p = &(*p)->rb_right; 7556038def0SNamhyung Kim } else if (vma->vm_end < pvma->vm_end) 7568feae131SDavid Howells p = &(*p)->rb_left; 7576038def0SNamhyung Kim else if (vma->vm_end > pvma->vm_end) { 7586038def0SNamhyung Kim rb_prev = parent; 7598feae131SDavid Howells p = &(*p)->rb_right; 7606038def0SNamhyung Kim } else if (vma < pvma) 7618feae131SDavid Howells p = &(*p)->rb_left; 7626038def0SNamhyung Kim else if (vma > pvma) { 7636038def0SNamhyung Kim rb_prev = parent; 7648feae131SDavid Howells p = &(*p)->rb_right; 7656038def0SNamhyung Kim } else 7668feae131SDavid Howells BUG(); 7678feae131SDavid Howells } 7688feae131SDavid Howells 7698feae131SDavid Howells rb_link_node(&vma->vm_rb, parent, p); 7708feae131SDavid Howells rb_insert_color(&vma->vm_rb, &mm->mm_rb); 7718feae131SDavid Howells 7728feae131SDavid Howells /* add VMA to the VMA list also */ 7736038def0SNamhyung Kim prev = NULL; 7746038def0SNamhyung Kim if (rb_prev) 7756038def0SNamhyung Kim prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); 7763034097aSDavid Howells 7776038def0SNamhyung Kim __vma_link_list(mm, vma, prev, parent); 7788feae131SDavid Howells } 7798feae131SDavid Howells 7808feae131SDavid Howells /* 7818feae131SDavid Howells * delete a VMA from its owning mm_struct and address space 7828feae131SDavid Howells */ 7838feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma) 7848feae131SDavid Howells { 785615d6e87SDavidlohr Bueso int i; 7868feae131SDavid Howells struct address_space *mapping; 7878feae131SDavid Howells struct mm_struct *mm = vma->vm_mm; 788615d6e87SDavidlohr Bueso struct task_struct *curr = current; 7898feae131SDavid Howells 790eb8cdec4SBernd Schmidt protect_vma(vma, 0); 791eb8cdec4SBernd Schmidt 7928feae131SDavid Howells mm->map_count--; 793615d6e87SDavidlohr Bueso for (i = 0; i < VMACACHE_SIZE; i++) { 794615d6e87SDavidlohr Bueso /* if the vma is cached, invalidate the entire cache */ 795615d6e87SDavidlohr Bueso if (curr->vmacache[i] == vma) { 796e020d5bdSSteven Miao vmacache_invalidate(mm); 797615d6e87SDavidlohr Bueso break; 798615d6e87SDavidlohr Bueso } 799615d6e87SDavidlohr Bueso } 8008feae131SDavid Howells 8018feae131SDavid Howells /* remove the VMA from the mapping */ 8028feae131SDavid Howells if (vma->vm_file) { 8038feae131SDavid Howells mapping = vma->vm_file->f_mapping; 8048feae131SDavid Howells 80583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 8068feae131SDavid Howells flush_dcache_mmap_lock(mapping); 8076b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 8088feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 80983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 8108feae131SDavid Howells } 8118feae131SDavid Howells 8128feae131SDavid Howells /* remove from the MM's tree and list */ 8138feae131SDavid Howells rb_erase(&vma->vm_rb, &mm->mm_rb); 814b951bf2cSNamhyung Kim 815b951bf2cSNamhyung Kim if (vma->vm_prev) 816b951bf2cSNamhyung Kim vma->vm_prev->vm_next = vma->vm_next; 817b951bf2cSNamhyung Kim else 818b951bf2cSNamhyung Kim mm->mmap = vma->vm_next; 819b951bf2cSNamhyung Kim 820b951bf2cSNamhyung Kim if (vma->vm_next) 821b951bf2cSNamhyung Kim vma->vm_next->vm_prev = vma->vm_prev; 8228feae131SDavid Howells } 8238feae131SDavid Howells 8248feae131SDavid Howells /* 8258feae131SDavid Howells * destroy a VMA record 8268feae131SDavid Howells */ 8278feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 8288feae131SDavid Howells { 8298feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 8308feae131SDavid Howells vma->vm_ops->close(vma); 831e9714acfSKonstantin Khlebnikov if (vma->vm_file) 8328feae131SDavid Howells fput(vma->vm_file); 8338feae131SDavid Howells put_nommu_region(vma->vm_region); 8348feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 8353034097aSDavid Howells } 8363034097aSDavid Howells 8373034097aSDavid Howells /* 8383034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 8393034097aSDavid Howells * - should be called with mm->mmap_sem at least held readlocked 8403034097aSDavid Howells */ 8413034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 8423034097aSDavid Howells { 8438feae131SDavid Howells struct vm_area_struct *vma; 8443034097aSDavid Howells 8458feae131SDavid Howells /* check the cache first */ 846615d6e87SDavidlohr Bueso vma = vmacache_find(mm, addr); 847615d6e87SDavidlohr Bueso if (likely(vma)) 8488feae131SDavid Howells return vma; 8498feae131SDavid Howells 850e922c4c5SNamhyung Kim /* trawl the list (there may be multiple mappings in which addr 8518feae131SDavid Howells * resides) */ 852e922c4c5SNamhyung Kim for (vma = mm->mmap; vma; vma = vma->vm_next) { 8538feae131SDavid Howells if (vma->vm_start > addr) 8548feae131SDavid Howells return NULL; 8558feae131SDavid Howells if (vma->vm_end > addr) { 856615d6e87SDavidlohr Bueso vmacache_update(addr, vma); 8578feae131SDavid Howells return vma; 8583034097aSDavid Howells } 8598feae131SDavid Howells } 8603034097aSDavid Howells 8613034097aSDavid Howells return NULL; 8623034097aSDavid Howells } 8633034097aSDavid Howells EXPORT_SYMBOL(find_vma); 8643034097aSDavid Howells 8653034097aSDavid Howells /* 866930e652aSDavid Howells * find a VMA 867930e652aSDavid Howells * - we don't extend stack VMAs under NOMMU conditions 868930e652aSDavid Howells */ 869930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 870930e652aSDavid Howells { 8717561e8caSDavid Howells return find_vma(mm, addr); 872930e652aSDavid Howells } 873930e652aSDavid Howells 8748feae131SDavid Howells /* 8758feae131SDavid Howells * expand a stack to a given address 8768feae131SDavid Howells * - not supported under NOMMU conditions 8778feae131SDavid Howells */ 87857c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address) 87957c8f63eSGreg Ungerer { 88057c8f63eSGreg Ungerer return -ENOMEM; 88157c8f63eSGreg Ungerer } 88257c8f63eSGreg Ungerer 883930e652aSDavid Howells /* 8846fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 8856fa5f80bSDavid Howells * - should be called with mm->mmap_sem at least held readlocked 8866fa5f80bSDavid Howells */ 8878feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 8888feae131SDavid Howells unsigned long addr, 8898feae131SDavid Howells unsigned long len) 8901da177e4SLinus Torvalds { 8911da177e4SLinus Torvalds struct vm_area_struct *vma; 8928feae131SDavid Howells unsigned long end = addr + len; 8931da177e4SLinus Torvalds 8948feae131SDavid Howells /* check the cache first */ 895615d6e87SDavidlohr Bueso vma = vmacache_find_exact(mm, addr, end); 896615d6e87SDavidlohr Bueso if (vma) 8971da177e4SLinus Torvalds return vma; 8988feae131SDavid Howells 899e922c4c5SNamhyung Kim /* trawl the list (there may be multiple mappings in which addr 9008feae131SDavid Howells * resides) */ 901e922c4c5SNamhyung Kim for (vma = mm->mmap; vma; vma = vma->vm_next) { 9028feae131SDavid Howells if (vma->vm_start < addr) 9038feae131SDavid Howells continue; 9048feae131SDavid Howells if (vma->vm_start > addr) 9058feae131SDavid Howells return NULL; 9068feae131SDavid Howells if (vma->vm_end == end) { 907615d6e87SDavidlohr Bueso vmacache_update(addr, vma); 9088feae131SDavid Howells return vma; 9098feae131SDavid Howells } 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds return NULL; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9153034097aSDavid Howells /* 9161da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 9171da177e4SLinus Torvalds * mapping we're capable of supporting 9181da177e4SLinus Torvalds */ 9191da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 9201da177e4SLinus Torvalds unsigned long addr, 9211da177e4SLinus Torvalds unsigned long len, 9221da177e4SLinus Torvalds unsigned long prot, 9231da177e4SLinus Torvalds unsigned long flags, 9241da177e4SLinus Torvalds unsigned long pgoff, 9251da177e4SLinus Torvalds unsigned long *_capabilities) 9261da177e4SLinus Torvalds { 9278feae131SDavid Howells unsigned long capabilities, rlen; 9281da177e4SLinus Torvalds int ret; 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds /* do the simple checks first */ 931*22cc877bSLeon Romanovsky if (flags & MAP_FIXED) 9321da177e4SLinus Torvalds return -EINVAL; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 9351da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 9361da177e4SLinus Torvalds return -EINVAL; 9371da177e4SLinus Torvalds 938f81cff0dSMike Frysinger if (!len) 9391da177e4SLinus Torvalds return -EINVAL; 9401da177e4SLinus Torvalds 941f81cff0dSMike Frysinger /* Careful about overflows.. */ 9428feae131SDavid Howells rlen = PAGE_ALIGN(len); 9438feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 944f81cff0dSMike Frysinger return -ENOMEM; 945f81cff0dSMike Frysinger 9461da177e4SLinus Torvalds /* offset overflow? */ 9478feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 948f81cff0dSMike Frysinger return -EOVERFLOW; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds if (file) { 9511da177e4SLinus Torvalds /* files must support mmap */ 95272c2d531SAl Viro if (!file->f_op->mmap) 9531da177e4SLinus Torvalds return -ENODEV; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 9561da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 9571da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 9581da177e4SLinus Torvalds */ 959b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 960b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 961b4caecd4SChristoph Hellwig } else { 9621da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 9631da177e4SLinus Torvalds * defaults */ 964496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 9651da177e4SLinus Torvalds case S_IFREG: 9661da177e4SLinus Torvalds case S_IFBLK: 967b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 9681da177e4SLinus Torvalds break; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds case S_IFCHR: 9711da177e4SLinus Torvalds capabilities = 972b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 973b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 974b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 9751da177e4SLinus Torvalds break; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds default: 9781da177e4SLinus Torvalds return -EINVAL; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 9831da177e4SLinus Torvalds * device */ 9841da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 985b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 9866e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 987b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 9881da177e4SLinus Torvalds 98928d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 99028d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 99128d7a6aeSGraff Yang return -EACCES; 99228d7a6aeSGraff Yang 9931da177e4SLinus Torvalds if (flags & MAP_SHARED) { 9941da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 9951da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 9961da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 9971da177e4SLinus Torvalds return -EACCES; 9981da177e4SLinus Torvalds 999496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 10001da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 10011da177e4SLinus Torvalds return -EACCES; 10021da177e4SLinus Torvalds 1003d7a06983SJeff Layton if (locks_verify_locked(file)) 10041da177e4SLinus Torvalds return -EAGAIN; 10051da177e4SLinus Torvalds 1006b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 10071da177e4SLinus Torvalds return -ENODEV; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 1010b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 1011ac714904SChoi Gi-yong } else { 10121da177e4SLinus Torvalds /* we're going to read the file into private memory we 10131da177e4SLinus Torvalds * allocate */ 1014b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 10151da177e4SLinus Torvalds return -ENODEV; 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 10181da177e4SLinus Torvalds * shared with the backing device */ 10191da177e4SLinus Torvalds if (prot & PROT_WRITE) 1020b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 1023b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 1024b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 1025b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 1026b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 10273c7b2045SBernd Schmidt ) { 1028b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 10293c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 1030*22cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 10313c7b2045SBernd Schmidt return -EINVAL; 10323c7b2045SBernd Schmidt } 10333c7b2045SBernd Schmidt } 10343c7b2045SBernd Schmidt } 10353c7b2045SBernd Schmidt 10361da177e4SLinus Torvalds /* handle executable mappings and implied executable 10371da177e4SLinus Torvalds * mappings */ 1038e9536ae7SJosef Sipek if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { 10391da177e4SLinus Torvalds if (prot & PROT_EXEC) 10401da177e4SLinus Torvalds return -EPERM; 1041ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 10421da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 10431da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 1044b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 10451da177e4SLinus Torvalds prot |= PROT_EXEC; 10461da177e4SLinus Torvalds } 1047ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 10481da177e4SLinus Torvalds (prot & PROT_EXEC) && 1049b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 10501da177e4SLinus Torvalds ) { 10511da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 1052b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 10531da177e4SLinus Torvalds } 1054ac714904SChoi Gi-yong } else { 10551da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 10561da177e4SLinus Torvalds * privately mapped 10571da177e4SLinus Torvalds */ 1058b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 10611da177e4SLinus Torvalds if ((prot & PROT_READ) && 10621da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 10631da177e4SLinus Torvalds prot |= PROT_EXEC; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds /* allow the security API to have its say */ 1067e5467859SAl Viro ret = security_mmap_addr(addr); 1068e5467859SAl Viro if (ret < 0) 1069e5467859SAl Viro return ret; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /* looks okay */ 10721da177e4SLinus Torvalds *_capabilities = capabilities; 10731da177e4SLinus Torvalds return 0; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* 10771da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 10781da177e4SLinus Torvalds * now know into VMA flags 10791da177e4SLinus Torvalds */ 10801da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 10811da177e4SLinus Torvalds unsigned long prot, 10821da177e4SLinus Torvalds unsigned long flags, 10831da177e4SLinus Torvalds unsigned long capabilities) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds unsigned long vm_flags; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags); 10881da177e4SLinus Torvalds /* vm_flags |= mm->def_flags; */ 10891da177e4SLinus Torvalds 1090b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) { 10911da177e4SLinus Torvalds /* attempt to share read-only copies of mapped file chunks */ 10923c7b2045SBernd Schmidt vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 10931da177e4SLinus Torvalds if (file && !(prot & PROT_WRITE)) 10941da177e4SLinus Torvalds vm_flags |= VM_MAYSHARE; 10953c7b2045SBernd Schmidt } else { 10961da177e4SLinus Torvalds /* overlay a shareable mapping on the backing device or inode 10971da177e4SLinus Torvalds * if possible - used for chardevs, ramfs/tmpfs/shmfs and 10981da177e4SLinus Torvalds * romfs/cramfs */ 1099b4caecd4SChristoph Hellwig vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS); 11001da177e4SLinus Torvalds if (flags & MAP_SHARED) 11013c7b2045SBernd Schmidt vm_flags |= VM_SHARED; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds /* refuse to let anyone share private mappings with this process if 11051da177e4SLinus Torvalds * it's being traced - otherwise breakpoints set in it may interfere 11061da177e4SLinus Torvalds * with another untraced process 11071da177e4SLinus Torvalds */ 1108a288eeccSTejun Heo if ((flags & MAP_PRIVATE) && current->ptrace) 11091da177e4SLinus Torvalds vm_flags &= ~VM_MAYSHARE; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds return vm_flags; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* 11158feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 11168feae131SDavid Howells * pins the storage) 11171da177e4SLinus Torvalds */ 11188feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 11191da177e4SLinus Torvalds { 11201da177e4SLinus Torvalds int ret; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1123dd8632a1SPaul Mundt if (ret == 0) { 1124dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 1125645d83c5SDavid Howells return 0; 1126dd8632a1SPaul Mundt } 11271da177e4SLinus Torvalds if (ret != -ENOSYS) 11281da177e4SLinus Torvalds return ret; 11291da177e4SLinus Torvalds 11303fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 11313fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 11323fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 11331da177e4SLinus Torvalds return -ENODEV; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds /* 11371da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 11381da177e4SLinus Torvalds */ 11398feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 11408feae131SDavid Howells struct vm_region *region, 1141645d83c5SDavid Howells unsigned long len, 1142645d83c5SDavid Howells unsigned long capabilities) 11431da177e4SLinus Torvalds { 1144dbc8358cSJoonsoo Kim unsigned long total, point; 11451da177e4SLinus Torvalds void *base; 11468feae131SDavid Howells int ret, order; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* invoke the file's mapping function so that it can keep track of 11491da177e4SLinus Torvalds * shared mappings on devices or memory 11501da177e4SLinus Torvalds * - VM_MAYSHARE will be set if it may attempt to share 11511da177e4SLinus Torvalds */ 1152b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11531da177e4SLinus Torvalds ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1154dd8632a1SPaul Mundt if (ret == 0) { 11551da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 1156dd8632a1SPaul Mundt BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); 1157dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 1158645d83c5SDavid Howells return 0; 11591da177e4SLinus Torvalds } 1160dd8632a1SPaul Mundt if (ret != -ENOSYS) 1161dd8632a1SPaul Mundt return ret; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 11641da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 11651da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 11688feae131SDavid Howells 11691da177e4SLinus Torvalds /* allocate some memory to hold the mapping 11701da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 11711da177e4SLinus Torvalds * we're allocating is smaller than a page 11721da177e4SLinus Torvalds */ 1173f67d9b15SBob Liu order = get_order(len); 11748feae131SDavid Howells total = 1 << order; 1175f67d9b15SBob Liu point = len >> PAGE_SHIFT; 1176dd8632a1SPaul Mundt 1177dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 1178*22cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 1179dbc8358cSJoonsoo Kim total = point; 11808feae131SDavid Howells 1181da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 1182dbc8358cSJoonsoo Kim if (!base) 1183dbc8358cSJoonsoo Kim goto enomem; 11848feae131SDavid Howells 1185dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 1186dbc8358cSJoonsoo Kim 11878feae131SDavid Howells region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; 11888feae131SDavid Howells region->vm_start = (unsigned long) base; 1189f67d9b15SBob Liu region->vm_end = region->vm_start + len; 1190dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 11918feae131SDavid Howells 11928feae131SDavid Howells vma->vm_start = region->vm_start; 11938feae131SDavid Howells vma->vm_end = region->vm_start + len; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds if (vma->vm_file) { 11961da177e4SLinus Torvalds /* read the contents of a file into the copy */ 11971da177e4SLinus Torvalds mm_segment_t old_fs; 11981da177e4SLinus Torvalds loff_t fpos; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds fpos = vma->vm_pgoff; 12011da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds old_fs = get_fs(); 12041da177e4SLinus Torvalds set_fs(KERNEL_DS); 12056e242a1cSAl Viro ret = __vfs_read(vma->vm_file, base, len, &fpos); 12061da177e4SLinus Torvalds set_fs(old_fs); 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds if (ret < 0) 12091da177e4SLinus Torvalds goto error_free; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds /* clear the last little bit */ 1212f67d9b15SBob Liu if (ret < len) 1213f67d9b15SBob Liu memset(base + ret, 0, len - ret); 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds return 0; 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds error_free: 12207223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 12218feae131SDavid Howells region->vm_start = vma->vm_start = 0; 12228feae131SDavid Howells region->vm_end = vma->vm_end = 0; 1223dd8632a1SPaul Mundt region->vm_top = 0; 12241da177e4SLinus Torvalds return ret; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds enomem: 1227b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 122805ae6fa3SGreg Ungerer len, current->pid, current->comm); 12297bf02ea2SDavid Rientjes show_free_areas(0); 12301da177e4SLinus Torvalds return -ENOMEM; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds /* 12341da177e4SLinus Torvalds * handle mapping creation for uClinux 12351da177e4SLinus Torvalds */ 1236e3fc629dSAl Viro unsigned long do_mmap_pgoff(struct file *file, 12371da177e4SLinus Torvalds unsigned long addr, 12381da177e4SLinus Torvalds unsigned long len, 12391da177e4SLinus Torvalds unsigned long prot, 12401da177e4SLinus Torvalds unsigned long flags, 1241bebeb3d6SMichel Lespinasse unsigned long pgoff, 124241badc15SMichel Lespinasse unsigned long *populate) 12431da177e4SLinus Torvalds { 12448feae131SDavid Howells struct vm_area_struct *vma; 12458feae131SDavid Howells struct vm_region *region; 12461da177e4SLinus Torvalds struct rb_node *rb; 12478feae131SDavid Howells unsigned long capabilities, vm_flags, result; 12481da177e4SLinus Torvalds int ret; 12491da177e4SLinus Torvalds 125041badc15SMichel Lespinasse *populate = 0; 1251bebeb3d6SMichel Lespinasse 12521da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 12531da177e4SLinus Torvalds * mapping */ 12541da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 12551da177e4SLinus Torvalds &capabilities); 1256*22cc877bSLeon Romanovsky if (ret < 0) 12571da177e4SLinus Torvalds return ret; 12581da177e4SLinus Torvalds 125906aab5a3SDavid Howells /* we ignore the address hint */ 126006aab5a3SDavid Howells addr = 0; 1261f67d9b15SBob Liu len = PAGE_ALIGN(len); 126206aab5a3SDavid Howells 12631da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 12641da177e4SLinus Torvalds * now know into VMA flags */ 12651da177e4SLinus Torvalds vm_flags = determine_vm_flags(file, prot, flags, capabilities); 12661da177e4SLinus Torvalds 12678feae131SDavid Howells /* we're going to need to record the mapping */ 12688feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 12698feae131SDavid Howells if (!region) 12708feae131SDavid Howells goto error_getting_region; 12711da177e4SLinus Torvalds 12728feae131SDavid Howells vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 12738feae131SDavid Howells if (!vma) 12748feae131SDavid Howells goto error_getting_vma; 12751da177e4SLinus Torvalds 12761e2ae599SDavid Howells region->vm_usage = 1; 12778feae131SDavid Howells region->vm_flags = vm_flags; 12788feae131SDavid Howells region->vm_pgoff = pgoff; 12798feae131SDavid Howells 12805beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 12818feae131SDavid Howells vma->vm_flags = vm_flags; 12828feae131SDavid Howells vma->vm_pgoff = pgoff; 12838feae131SDavid Howells 12848feae131SDavid Howells if (file) { 1285cb0942b8SAl Viro region->vm_file = get_file(file); 1286cb0942b8SAl Viro vma->vm_file = get_file(file); 12878feae131SDavid Howells } 12888feae131SDavid Howells 12898feae131SDavid Howells down_write(&nommu_region_sem); 12908feae131SDavid Howells 12918feae131SDavid Howells /* if we want to share, we need to check for regions created by other 12921da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 12938feae131SDavid Howells * - we can only share with a superset match on most regular files 12941da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 12951da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 12961da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 12971da177e4SLinus Torvalds * than here 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds if (vm_flags & VM_MAYSHARE) { 13008feae131SDavid Howells struct vm_region *pregion; 13018feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 13021da177e4SLinus Torvalds 13038feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 13048feae131SDavid Howells pgend = pgoff + pglen; 1305165b2392SDavid Howells 13068feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 13078feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 13081da177e4SLinus Torvalds 13098feae131SDavid Howells if (!(pregion->vm_flags & VM_MAYSHARE)) 13101da177e4SLinus Torvalds continue; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1313496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1314496ad9aaSAl Viro file_inode(file)) 13151da177e4SLinus Torvalds continue; 13161da177e4SLinus Torvalds 13178feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 13181da177e4SLinus Torvalds continue; 13191da177e4SLinus Torvalds 13208feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 13218feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 13228feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 13238feae131SDavid Howells if (pgoff >= rpgend) 13241da177e4SLinus Torvalds continue; 13251da177e4SLinus Torvalds 13268feae131SDavid Howells /* handle inexactly overlapping matches between 13278feae131SDavid Howells * mappings */ 13288feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 13298feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 13308feae131SDavid Howells /* new mapping is not a subset of the region */ 1331b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 13321da177e4SLinus Torvalds goto sharing_violation; 13331da177e4SLinus Torvalds continue; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13368feae131SDavid Howells /* we've found a region we can share */ 13371e2ae599SDavid Howells pregion->vm_usage++; 13388feae131SDavid Howells vma->vm_region = pregion; 13398feae131SDavid Howells start = pregion->vm_start; 13408feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 13418feae131SDavid Howells vma->vm_start = start; 13428feae131SDavid Howells vma->vm_end = start + len; 13431da177e4SLinus Torvalds 1344*22cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 13458feae131SDavid Howells vma->vm_flags |= VM_MAPPED_COPY; 1346*22cc877bSLeon Romanovsky else { 13478feae131SDavid Howells ret = do_mmap_shared_file(vma); 13488feae131SDavid Howells if (ret < 0) { 13498feae131SDavid Howells vma->vm_region = NULL; 13508feae131SDavid Howells vma->vm_start = 0; 13518feae131SDavid Howells vma->vm_end = 0; 13521e2ae599SDavid Howells pregion->vm_usage--; 13538feae131SDavid Howells pregion = NULL; 13548feae131SDavid Howells goto error_just_free; 13551da177e4SLinus Torvalds } 13568feae131SDavid Howells } 13578feae131SDavid Howells fput(region->vm_file); 13588feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 13598feae131SDavid Howells region = pregion; 13608feae131SDavid Howells result = start; 13618feae131SDavid Howells goto share; 13628feae131SDavid Howells } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 13651da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 13661da177e4SLinus Torvalds * tell us the location of a shared mapping 13671da177e4SLinus Torvalds */ 1368b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 13691da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 13701da177e4SLinus Torvalds pgoff, flags); 1371bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 13721da177e4SLinus Torvalds ret = addr; 1373bb005a59SNamhyung Kim if (ret != -ENOSYS) 13748feae131SDavid Howells goto error_just_free; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* the driver refused to tell us where to site 13771da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 13781da177e4SLinus Torvalds * it */ 1379bb005a59SNamhyung Kim ret = -ENODEV; 1380b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 13818feae131SDavid Howells goto error_just_free; 13821da177e4SLinus Torvalds 1383b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 13848feae131SDavid Howells } else { 13858feae131SDavid Howells vma->vm_start = region->vm_start = addr; 13868feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13918feae131SDavid Howells vma->vm_region = region; 13921da177e4SLinus Torvalds 1393645d83c5SDavid Howells /* set up the mapping 1394b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1395645d83c5SDavid Howells */ 13961da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 13978feae131SDavid Howells ret = do_mmap_shared_file(vma); 13981da177e4SLinus Torvalds else 1399645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 14001da177e4SLinus Torvalds if (ret < 0) 1401645d83c5SDavid Howells goto error_just_free; 1402645d83c5SDavid Howells add_nommu_region(region); 14038feae131SDavid Howells 1404ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 1405ea637639SJie Zhang if (!vma->vm_file && !(flags & MAP_UNINITIALIZED)) 1406ea637639SJie Zhang memset((void *)region->vm_start, 0, 1407ea637639SJie Zhang region->vm_end - region->vm_start); 1408ea637639SJie Zhang 14091da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 14108feae131SDavid Howells result = vma->vm_start; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 14131da177e4SLinus Torvalds 14148feae131SDavid Howells share: 14158feae131SDavid Howells add_vma_to_mm(current->mm, vma); 14161da177e4SLinus Torvalds 1417cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable 1418cfe79c00SMike Frysinger * mapping of it is made */ 1419cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1420cfe79c00SMike Frysinger flush_icache_range(region->vm_start, region->vm_end); 1421cfe79c00SMike Frysinger region->vm_icache_flushed = true; 1422cfe79c00SMike Frysinger } 14231da177e4SLinus Torvalds 1424cfe79c00SMike Frysinger up_write(&nommu_region_sem); 14251da177e4SLinus Torvalds 14268feae131SDavid Howells return result; 14271da177e4SLinus Torvalds 14288feae131SDavid Howells error_just_free: 14298feae131SDavid Howells up_write(&nommu_region_sem); 14308feae131SDavid Howells error: 143189a86402SDavid Howells if (region->vm_file) 14328feae131SDavid Howells fput(region->vm_file); 14338feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 143489a86402SDavid Howells if (vma->vm_file) 14358feae131SDavid Howells fput(vma->vm_file); 14368feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 14371da177e4SLinus Torvalds return ret; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds sharing_violation: 14408feae131SDavid Howells up_write(&nommu_region_sem); 1441*22cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n"); 14428feae131SDavid Howells ret = -EINVAL; 14438feae131SDavid Howells goto error; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds error_getting_vma: 14468feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 1447*22cc877bSLeon Romanovsky pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", 14481da177e4SLinus Torvalds len, current->pid); 14497bf02ea2SDavid Rientjes show_free_areas(0); 14501da177e4SLinus Torvalds return -ENOMEM; 14511da177e4SLinus Torvalds 14528feae131SDavid Howells error_getting_region: 1453*22cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", 14541da177e4SLinus Torvalds len, current->pid); 14557bf02ea2SDavid Rientjes show_free_areas(0); 14561da177e4SLinus Torvalds return -ENOMEM; 14571da177e4SLinus Torvalds } 14586be5ceb0SLinus Torvalds 145966f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 146066f0dc48SHugh Dickins unsigned long, prot, unsigned long, flags, 146166f0dc48SHugh Dickins unsigned long, fd, unsigned long, pgoff) 146266f0dc48SHugh Dickins { 146366f0dc48SHugh Dickins struct file *file = NULL; 146466f0dc48SHugh Dickins unsigned long retval = -EBADF; 146566f0dc48SHugh Dickins 1466120a795dSAl Viro audit_mmap_fd(fd, flags); 146766f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 146866f0dc48SHugh Dickins file = fget(fd); 146966f0dc48SHugh Dickins if (!file) 147066f0dc48SHugh Dickins goto out; 147166f0dc48SHugh Dickins } 147266f0dc48SHugh Dickins 147366f0dc48SHugh Dickins flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 147466f0dc48SHugh Dickins 1475ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 147666f0dc48SHugh Dickins 147766f0dc48SHugh Dickins if (file) 147866f0dc48SHugh Dickins fput(file); 147966f0dc48SHugh Dickins out: 148066f0dc48SHugh Dickins return retval; 148166f0dc48SHugh Dickins } 148266f0dc48SHugh Dickins 1483a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1484a4679373SChristoph Hellwig struct mmap_arg_struct { 1485a4679373SChristoph Hellwig unsigned long addr; 1486a4679373SChristoph Hellwig unsigned long len; 1487a4679373SChristoph Hellwig unsigned long prot; 1488a4679373SChristoph Hellwig unsigned long flags; 1489a4679373SChristoph Hellwig unsigned long fd; 1490a4679373SChristoph Hellwig unsigned long offset; 1491a4679373SChristoph Hellwig }; 1492a4679373SChristoph Hellwig 1493a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1494a4679373SChristoph Hellwig { 1495a4679373SChristoph Hellwig struct mmap_arg_struct a; 1496a4679373SChristoph Hellwig 1497a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1498a4679373SChristoph Hellwig return -EFAULT; 1499a4679373SChristoph Hellwig if (a.offset & ~PAGE_MASK) 1500a4679373SChristoph Hellwig return -EINVAL; 1501a4679373SChristoph Hellwig 1502a4679373SChristoph Hellwig return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1503a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1504a4679373SChristoph Hellwig } 1505a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1506a4679373SChristoph Hellwig 15071da177e4SLinus Torvalds /* 15088feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 15098feae131SDavid Howells * for the first part or the tail. 15101da177e4SLinus Torvalds */ 15118feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 15128feae131SDavid Howells unsigned long addr, int new_below) 15131da177e4SLinus Torvalds { 15148feae131SDavid Howells struct vm_area_struct *new; 15158feae131SDavid Howells struct vm_region *region; 15168feae131SDavid Howells unsigned long npages; 15171da177e4SLinus Torvalds 1518779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1519779c1023SDavid Howells * only a single usage on the region) */ 1520779c1023SDavid Howells if (vma->vm_file) 15218feae131SDavid Howells return -ENOMEM; 15221da177e4SLinus Torvalds 15238feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 15248feae131SDavid Howells return -ENOMEM; 15251da177e4SLinus Torvalds 15268feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 15278feae131SDavid Howells if (!region) 15288feae131SDavid Howells return -ENOMEM; 15298feae131SDavid Howells 15308feae131SDavid Howells new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 15318feae131SDavid Howells if (!new) { 15328feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 15338feae131SDavid Howells return -ENOMEM; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 15368feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 15378feae131SDavid Howells *new = *vma; 15388feae131SDavid Howells *region = *vma->vm_region; 15398feae131SDavid Howells new->vm_region = region; 15408feae131SDavid Howells 15418feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 15428feae131SDavid Howells 15438feae131SDavid Howells if (new_below) { 1544dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 15458feae131SDavid Howells } else { 15468feae131SDavid Howells region->vm_start = new->vm_start = addr; 15478feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 15481da177e4SLinus Torvalds } 15498feae131SDavid Howells 15508feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 15518feae131SDavid Howells new->vm_ops->open(new); 15528feae131SDavid Howells 15538feae131SDavid Howells delete_vma_from_mm(vma); 15548feae131SDavid Howells down_write(&nommu_region_sem); 15558feae131SDavid Howells delete_nommu_region(vma->vm_region); 15568feae131SDavid Howells if (new_below) { 15578feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 15588feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 15598feae131SDavid Howells } else { 15608feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1561dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 15628feae131SDavid Howells } 15638feae131SDavid Howells add_nommu_region(vma->vm_region); 15648feae131SDavid Howells add_nommu_region(new->vm_region); 15658feae131SDavid Howells up_write(&nommu_region_sem); 15668feae131SDavid Howells add_vma_to_mm(mm, vma); 15678feae131SDavid Howells add_vma_to_mm(mm, new); 15688feae131SDavid Howells return 0; 15698feae131SDavid Howells } 15708feae131SDavid Howells 15718feae131SDavid Howells /* 15728feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 15738feae131SDavid Howells * the end 15748feae131SDavid Howells */ 15758feae131SDavid Howells static int shrink_vma(struct mm_struct *mm, 15768feae131SDavid Howells struct vm_area_struct *vma, 15778feae131SDavid Howells unsigned long from, unsigned long to) 15788feae131SDavid Howells { 15798feae131SDavid Howells struct vm_region *region; 15808feae131SDavid Howells 15818feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 15828feae131SDavid Howells * and list */ 15838feae131SDavid Howells delete_vma_from_mm(vma); 15848feae131SDavid Howells if (from > vma->vm_start) 15858feae131SDavid Howells vma->vm_end = from; 15868feae131SDavid Howells else 15878feae131SDavid Howells vma->vm_start = to; 15888feae131SDavid Howells add_vma_to_mm(mm, vma); 15898feae131SDavid Howells 15908feae131SDavid Howells /* cut the backing region down to size */ 15918feae131SDavid Howells region = vma->vm_region; 15921e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 15938feae131SDavid Howells 15948feae131SDavid Howells down_write(&nommu_region_sem); 15958feae131SDavid Howells delete_nommu_region(region); 1596dd8632a1SPaul Mundt if (from > region->vm_start) { 1597dd8632a1SPaul Mundt to = region->vm_top; 1598dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1599dd8632a1SPaul Mundt } else { 16008feae131SDavid Howells region->vm_start = to; 1601dd8632a1SPaul Mundt } 16028feae131SDavid Howells add_nommu_region(region); 16038feae131SDavid Howells up_write(&nommu_region_sem); 16048feae131SDavid Howells 16058feae131SDavid Howells free_page_series(from, to); 16068feae131SDavid Howells return 0; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 16093034097aSDavid Howells /* 16103034097aSDavid Howells * release a mapping 16118feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 16128feae131SDavid Howells * VMA, though it need not cover the whole VMA 16133034097aSDavid Howells */ 16148feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 16151da177e4SLinus Torvalds { 16168feae131SDavid Howells struct vm_area_struct *vma; 1617f67d9b15SBob Liu unsigned long end; 16188feae131SDavid Howells int ret; 16191da177e4SLinus Torvalds 1620f67d9b15SBob Liu len = PAGE_ALIGN(len); 16218feae131SDavid Howells if (len == 0) 16221da177e4SLinus Torvalds return -EINVAL; 16231da177e4SLinus Torvalds 1624f67d9b15SBob Liu end = start + len; 1625f67d9b15SBob Liu 16268feae131SDavid Howells /* find the first potentially overlapping VMA */ 16278feae131SDavid Howells vma = find_vma(mm, start); 16288feae131SDavid Howells if (!vma) { 1629ac714904SChoi Gi-yong static int limit; 163033e5d769SDavid Howells if (limit < 5) { 1631*22cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 163233e5d769SDavid Howells current->pid, current->comm, 163333e5d769SDavid Howells start, start + len - 1); 163433e5d769SDavid Howells limit++; 163533e5d769SDavid Howells } 16368feae131SDavid Howells return -EINVAL; 16378feae131SDavid Howells } 16381da177e4SLinus Torvalds 16398feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 16408feae131SDavid Howells if (vma->vm_file) { 16418feae131SDavid Howells do { 1642*22cc877bSLeon Romanovsky if (start > vma->vm_start) 16438feae131SDavid Howells return -EINVAL; 16448feae131SDavid Howells if (end == vma->vm_end) 16458feae131SDavid Howells goto erase_whole_vma; 1646d75a310cSNamhyung Kim vma = vma->vm_next; 1647d75a310cSNamhyung Kim } while (vma); 16488feae131SDavid Howells return -EINVAL; 16498feae131SDavid Howells } else { 16508feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 16518feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 16528feae131SDavid Howells goto erase_whole_vma; 1653*22cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 16548feae131SDavid Howells return -EINVAL; 1655*22cc877bSLeon Romanovsky if (start & ~PAGE_MASK) 16568feae131SDavid Howells return -EINVAL; 1657*22cc877bSLeon Romanovsky if (end != vma->vm_end && end & ~PAGE_MASK) 16588feae131SDavid Howells return -EINVAL; 16598feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 16608feae131SDavid Howells ret = split_vma(mm, vma, start, 1); 1661*22cc877bSLeon Romanovsky if (ret < 0) 16628feae131SDavid Howells return ret; 16638feae131SDavid Howells } 16648feae131SDavid Howells return shrink_vma(mm, vma, start, end); 16658feae131SDavid Howells } 16661da177e4SLinus Torvalds 16678feae131SDavid Howells erase_whole_vma: 16688feae131SDavid Howells delete_vma_from_mm(vma); 16698feae131SDavid Howells delete_vma(mm, vma); 16701da177e4SLinus Torvalds return 0; 16711da177e4SLinus Torvalds } 1672b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap); 16731da177e4SLinus Torvalds 1674bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 16753034097aSDavid Howells { 1676bfce281cSAl Viro struct mm_struct *mm = current->mm; 16773034097aSDavid Howells int ret; 16783034097aSDavid Howells 16793034097aSDavid Howells down_write(&mm->mmap_sem); 16803034097aSDavid Howells ret = do_munmap(mm, addr, len); 16813034097aSDavid Howells up_write(&mm->mmap_sem); 16823034097aSDavid Howells return ret; 16833034097aSDavid Howells } 1684a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1685a46ef99dSLinus Torvalds 1686a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1687a46ef99dSLinus Torvalds { 1688bfce281cSAl Viro return vm_munmap(addr, len); 1689a46ef99dSLinus Torvalds } 16903034097aSDavid Howells 16913034097aSDavid Howells /* 16928feae131SDavid Howells * release all the mappings made in a process's VM space 16933034097aSDavid Howells */ 16941da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 16951da177e4SLinus Torvalds { 16968feae131SDavid Howells struct vm_area_struct *vma; 16971da177e4SLinus Torvalds 16988feae131SDavid Howells if (!mm) 16998feae131SDavid Howells return; 17008feae131SDavid Howells 17011da177e4SLinus Torvalds mm->total_vm = 0; 17021da177e4SLinus Torvalds 17038feae131SDavid Howells while ((vma = mm->mmap)) { 17048feae131SDavid Howells mm->mmap = vma->vm_next; 17058feae131SDavid Howells delete_vma_from_mm(vma); 17068feae131SDavid Howells delete_vma(mm, vma); 170704c34961SSteven J. Magnani cond_resched(); 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 1711e4eb1ff6SLinus Torvalds unsigned long vm_brk(unsigned long addr, unsigned long len) 17121da177e4SLinus Torvalds { 17131da177e4SLinus Torvalds return -ENOMEM; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* 17176fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 17186fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 17191da177e4SLinus Torvalds * 17206fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 17218feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 17228feae131SDavid Howells * block is not shareable 17231da177e4SLinus Torvalds * 17246fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 17251da177e4SLinus Torvalds */ 17264b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 17271da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 17281da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 17291da177e4SLinus Torvalds { 17306fa5f80bSDavid Howells struct vm_area_struct *vma; 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds /* insanity checks first */ 1733f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1734f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 17358feae131SDavid Howells if (old_len == 0 || new_len == 0) 17361da177e4SLinus Torvalds return (unsigned long) -EINVAL; 17371da177e4SLinus Torvalds 17388feae131SDavid Howells if (addr & ~PAGE_MASK) 17398feae131SDavid Howells return -EINVAL; 17408feae131SDavid Howells 17411da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 17421da177e4SLinus Torvalds return (unsigned long) -EINVAL; 17431da177e4SLinus Torvalds 17448feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 17456fa5f80bSDavid Howells if (!vma) 17461da177e4SLinus Torvalds return (unsigned long) -EINVAL; 17471da177e4SLinus Torvalds 17486fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 17491da177e4SLinus Torvalds return (unsigned long) -EFAULT; 17501da177e4SLinus Torvalds 17516fa5f80bSDavid Howells if (vma->vm_flags & VM_MAYSHARE) 17521da177e4SLinus Torvalds return (unsigned long) -EPERM; 17531da177e4SLinus Torvalds 17548feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 17551da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds /* all checks complete - do it */ 17586fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 17596fa5f80bSDavid Howells return vma->vm_start; 17606fa5f80bSDavid Howells } 17616fa5f80bSDavid Howells 17626a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 17636a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 17646a6160a7SHeiko Carstens unsigned long, new_addr) 17656fa5f80bSDavid Howells { 17666fa5f80bSDavid Howells unsigned long ret; 17676fa5f80bSDavid Howells 17686fa5f80bSDavid Howells down_write(¤t->mm->mmap_sem); 17696fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 17706fa5f80bSDavid Howells up_write(¤t->mm->mmap_sem); 17716fa5f80bSDavid Howells return ret; 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 1774240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma, 1775240aadeeSMichel Lespinasse unsigned long address, unsigned int flags, 1776240aadeeSMichel Lespinasse unsigned int *page_mask) 17771da177e4SLinus Torvalds { 1778240aadeeSMichel Lespinasse *page_mask = 0; 17791da177e4SLinus Torvalds return NULL; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17828f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 17838f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 17841da177e4SLinus Torvalds { 17858f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 17868f3b1327SBob Liu return -EINVAL; 17878f3b1327SBob Liu 1788314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 178966aa2b4bSGreg Ungerer return 0; 17901da177e4SLinus Torvalds } 179122c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 17921da177e4SLinus Torvalds 17933c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 17943c0b9de6SLinus Torvalds { 17953c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 17963c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 17973c0b9de6SLinus Torvalds 17983c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 17993c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 18003c0b9de6SLinus Torvalds } 18013c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 18023c0b9de6SLinus Torvalds 1803f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1804f905bc44SPaul Mundt unsigned long pgoff) 1805f905bc44SPaul Mundt { 1806f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1807f905bc44SPaul Mundt 1808f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1809f905bc44SPaul Mundt return -EINVAL; 1810f905bc44SPaul Mundt 1811f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1812f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1813f905bc44SPaul Mundt 1814f905bc44SPaul Mundt return 0; 1815f905bc44SPaul Mundt } 1816f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1817f905bc44SPaul Mundt 18181da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr, 18191da177e4SLinus Torvalds unsigned long len, unsigned long pgoff, unsigned long flags) 18201da177e4SLinus Torvalds { 18211da177e4SLinus Torvalds return -ENOMEM; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 18251da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, 18261da177e4SLinus Torvalds int even_cows) 18271da177e4SLinus Torvalds { 18281da177e4SLinus Torvalds } 182922c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range); 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /* 18321da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 18331da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 18341da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 18351da177e4SLinus Torvalds * 18361da177e4SLinus Torvalds * We currently support three overcommit policies, which are set via the 18371da177e4SLinus Torvalds * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 18381da177e4SLinus Torvalds * 18391da177e4SLinus Torvalds * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 18401da177e4SLinus Torvalds * Additional code 2002 Jul 20 by Robert Love. 18411da177e4SLinus Torvalds * 18421da177e4SLinus Torvalds * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 18431da177e4SLinus Torvalds * 18441da177e4SLinus Torvalds * Note this is a helper function intended to be used by LSMs which 18451da177e4SLinus Torvalds * wish to use this logic. 18461da177e4SLinus Torvalds */ 184734b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) 18481da177e4SLinus Torvalds { 18498138a67aSRoman Gushchin long free, allowed, reserve; 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds vm_acct_memory(pages); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds /* 18541da177e4SLinus Torvalds * Sometimes we want to use more memory than we have 18551da177e4SLinus Torvalds */ 18561da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 18571da177e4SLinus Torvalds return 0; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 1860c15bef30SDmitry Fink free = global_page_state(NR_FREE_PAGES); 1861c15bef30SDmitry Fink free += global_page_state(NR_FILE_PAGES); 18621da177e4SLinus Torvalds 1863c15bef30SDmitry Fink /* 1864c15bef30SDmitry Fink * shmem pages shouldn't be counted as free in this 1865c15bef30SDmitry Fink * case, they can't be purged, only swapped out, and 1866c15bef30SDmitry Fink * that won't affect the overall amount of available 1867c15bef30SDmitry Fink * memory in the system. 1868c15bef30SDmitry Fink */ 1869c15bef30SDmitry Fink free -= global_page_state(NR_SHMEM); 1870c15bef30SDmitry Fink 1871ec8acf20SShaohua Li free += get_nr_swap_pages(); 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* 18741da177e4SLinus Torvalds * Any slabs which are created with the 18751da177e4SLinus Torvalds * SLAB_RECLAIM_ACCOUNT flag claim to have contents 18761da177e4SLinus Torvalds * which are reclaimable, under pressure. The dentry 18771da177e4SLinus Torvalds * cache and most inode caches should fall into this 18781da177e4SLinus Torvalds */ 1879972d1a7bSChristoph Lameter free += global_page_state(NR_SLAB_RECLAIMABLE); 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* 1882c15bef30SDmitry Fink * Leave reserved pages. The pages are not for anonymous pages. 1883c15bef30SDmitry Fink */ 1884c15bef30SDmitry Fink if (free <= totalreserve_pages) 1885c15bef30SDmitry Fink goto error; 1886c15bef30SDmitry Fink else 1887c15bef30SDmitry Fink free -= totalreserve_pages; 1888c15bef30SDmitry Fink 1889c15bef30SDmitry Fink /* 18904eeab4f5SAndrew Shewmaker * Reserve some for root 18911da177e4SLinus Torvalds */ 18921da177e4SLinus Torvalds if (!cap_sys_admin) 18934eeab4f5SAndrew Shewmaker free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds if (free > pages) 18961da177e4SLinus Torvalds return 0; 18971da177e4SLinus Torvalds 1898d5ddc79bSHideo AOKI goto error; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 190100619bccSJerome Marchand allowed = vm_commit_limit(); 19021da177e4SLinus Torvalds /* 19034eeab4f5SAndrew Shewmaker * Reserve some 3% for root 19041da177e4SLinus Torvalds */ 19051da177e4SLinus Torvalds if (!cap_sys_admin) 19064eeab4f5SAndrew Shewmaker allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); 19071da177e4SLinus Torvalds 1908c9b1d098SAndrew Shewmaker /* 1909c9b1d098SAndrew Shewmaker * Don't let a single process grow so big a user can't recover 1910c9b1d098SAndrew Shewmaker */ 1911c9b1d098SAndrew Shewmaker if (mm) { 1912c9b1d098SAndrew Shewmaker reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10); 19138138a67aSRoman Gushchin allowed -= min_t(long, mm->total_vm / 32, reserve); 1914c9b1d098SAndrew Shewmaker } 19151da177e4SLinus Torvalds 191600a62ce9SKOSAKI Motohiro if (percpu_counter_read_positive(&vm_committed_as) < allowed) 19171da177e4SLinus Torvalds return 0; 191800a62ce9SKOSAKI Motohiro 1919d5ddc79bSHideo AOKI error: 19201da177e4SLinus Torvalds vm_unacct_memory(pages); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds return -ENOMEM; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 1925d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1926b0e15190SDavid Howells { 1927b0e15190SDavid Howells BUG(); 1928d0217ac0SNick Piggin return 0; 1929b0e15190SDavid Howells } 1930b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 19310ec76a11SDavid Howells 1932f1820361SKirill A. Shutemov void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf) 1933f1820361SKirill A. Shutemov { 1934f1820361SKirill A. Shutemov BUG(); 1935f1820361SKirill A. Shutemov } 1936f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1937f1820361SKirill A. Shutemov 1938f55f199bSMike Frysinger static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 1939f55f199bSMike Frysinger unsigned long addr, void *buf, int len, int write) 19400ec76a11SDavid Howells { 19410ec76a11SDavid Howells struct vm_area_struct *vma; 19420ec76a11SDavid Howells 19430ec76a11SDavid Howells down_read(&mm->mmap_sem); 19440ec76a11SDavid Howells 19450ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 19460159b141SDavid Howells vma = find_vma(mm, addr); 19470159b141SDavid Howells if (vma) { 19480ec76a11SDavid Howells /* don't overrun this mapping */ 19490ec76a11SDavid Howells if (addr + len >= vma->vm_end) 19500ec76a11SDavid Howells len = vma->vm_end - addr; 19510ec76a11SDavid Howells 19520ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1953d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 19547959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 19557959722bSJie Zhang (void *) addr, buf, len); 1956d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 19577959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 19587959722bSJie Zhang buf, (void *) addr, len); 19590ec76a11SDavid Howells else 19600ec76a11SDavid Howells len = 0; 19610ec76a11SDavid Howells } else { 19620ec76a11SDavid Howells len = 0; 19630ec76a11SDavid Howells } 19640ec76a11SDavid Howells 19650ec76a11SDavid Howells up_read(&mm->mmap_sem); 1966f55f199bSMike Frysinger 1967f55f199bSMike Frysinger return len; 1968f55f199bSMike Frysinger } 1969f55f199bSMike Frysinger 1970f55f199bSMike Frysinger /** 1971f55f199bSMike Frysinger * @access_remote_vm - access another process' address space 1972f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1973f55f199bSMike Frysinger * @addr: start address to access 1974f55f199bSMike Frysinger * @buf: source or destination buffer 1975f55f199bSMike Frysinger * @len: number of bytes to transfer 1976f55f199bSMike Frysinger * @write: whether the access is a write 1977f55f199bSMike Frysinger * 1978f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1979f55f199bSMike Frysinger */ 1980f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 1981f55f199bSMike Frysinger void *buf, int len, int write) 1982f55f199bSMike Frysinger { 1983f55f199bSMike Frysinger return __access_remote_vm(NULL, mm, addr, buf, len, write); 1984f55f199bSMike Frysinger } 1985f55f199bSMike Frysinger 1986f55f199bSMike Frysinger /* 1987f55f199bSMike Frysinger * Access another process' address space. 1988f55f199bSMike Frysinger * - source/target buffer must be kernel space 1989f55f199bSMike Frysinger */ 1990f55f199bSMike Frysinger int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 1991f55f199bSMike Frysinger { 1992f55f199bSMike Frysinger struct mm_struct *mm; 1993f55f199bSMike Frysinger 1994f55f199bSMike Frysinger if (addr + len < addr) 1995f55f199bSMike Frysinger return 0; 1996f55f199bSMike Frysinger 1997f55f199bSMike Frysinger mm = get_task_mm(tsk); 1998f55f199bSMike Frysinger if (!mm) 1999f55f199bSMike Frysinger return 0; 2000f55f199bSMike Frysinger 2001f55f199bSMike Frysinger len = __access_remote_vm(tsk, mm, addr, buf, len, write); 2002f55f199bSMike Frysinger 20030ec76a11SDavid Howells mmput(mm); 20040ec76a11SDavid Howells return len; 20050ec76a11SDavid Howells } 20067e660872SDavid Howells 20077e660872SDavid Howells /** 20087e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 20097e660872SDavid Howells * @inode: The inode to check 20107e660872SDavid Howells * @size: The current filesize of the inode 20117e660872SDavid Howells * @newsize: The proposed filesize of the inode 20127e660872SDavid Howells * 20137e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 20147e660872SDavid Howells * make sure that that any outstanding VMAs aren't broken and then shrink the 20157e660872SDavid Howells * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't 20167e660872SDavid Howells * automatically grant mappings that are too large. 20177e660872SDavid Howells */ 20187e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 20197e660872SDavid Howells size_t newsize) 20207e660872SDavid Howells { 20217e660872SDavid Howells struct vm_area_struct *vma; 20227e660872SDavid Howells struct vm_region *region; 20237e660872SDavid Howells pgoff_t low, high; 20247e660872SDavid Howells size_t r_size, r_top; 20257e660872SDavid Howells 20267e660872SDavid Howells low = newsize >> PAGE_SHIFT; 20277e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 20287e660872SDavid Howells 20297e660872SDavid Howells down_write(&nommu_region_sem); 20301acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 20317e660872SDavid Howells 20327e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 20336b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 20347e660872SDavid Howells /* found one - only interested if it's shared out of the page 20357e660872SDavid Howells * cache */ 20367e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 20371acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 20387e660872SDavid Howells up_write(&nommu_region_sem); 20397e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 20407e660872SDavid Howells } 20417e660872SDavid Howells } 20427e660872SDavid Howells 20437e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 20447e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 20457e660872SDavid Howells * 20467e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 20477e660872SDavid Howells * shouldn't be any 20487e660872SDavid Howells */ 20491acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 20507e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 20517e660872SDavid Howells continue; 20527e660872SDavid Howells 20537e660872SDavid Howells region = vma->vm_region; 20547e660872SDavid Howells r_size = region->vm_top - region->vm_start; 20557e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 20567e660872SDavid Howells 20577e660872SDavid Howells if (r_top > newsize) { 20587e660872SDavid Howells region->vm_top -= r_top - newsize; 20597e660872SDavid Howells if (region->vm_end > region->vm_top) 20607e660872SDavid Howells region->vm_end = region->vm_top; 20617e660872SDavid Howells } 20627e660872SDavid Howells } 20637e660872SDavid Howells 20641acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 20657e660872SDavid Howells up_write(&nommu_region_sem); 20667e660872SDavid Howells return 0; 20677e660872SDavid Howells } 2068c9b1d098SAndrew Shewmaker 2069c9b1d098SAndrew Shewmaker /* 2070c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 2071c9b1d098SAndrew Shewmaker * 2072c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 2073c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 2074c9b1d098SAndrew Shewmaker * mode. 2075c9b1d098SAndrew Shewmaker * 2076c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 2077c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 2078c9b1d098SAndrew Shewmaker */ 2079c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 2080c9b1d098SAndrew Shewmaker { 2081c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 2082c9b1d098SAndrew Shewmaker 2083c9b1d098SAndrew Shewmaker free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 2084c9b1d098SAndrew Shewmaker 2085c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 2086c9b1d098SAndrew Shewmaker return 0; 2087c9b1d098SAndrew Shewmaker } 2088c9b1d098SAndrew Shewmaker module_init(init_user_reserve) 20894eeab4f5SAndrew Shewmaker 20904eeab4f5SAndrew Shewmaker /* 20914eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 20924eeab4f5SAndrew Shewmaker * 20934eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 20944eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 20954eeab4f5SAndrew Shewmaker * 20964eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 20974eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 20984eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 20994eeab4f5SAndrew Shewmaker */ 21004eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 21014eeab4f5SAndrew Shewmaker { 21024eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 21034eeab4f5SAndrew Shewmaker 21044eeab4f5SAndrew Shewmaker free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 21054eeab4f5SAndrew Shewmaker 21064eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 21074eeab4f5SAndrew Shewmaker return 0; 21084eeab4f5SAndrew Shewmaker } 21094eeab4f5SAndrew Shewmaker module_init(init_admin_reserve) 2110