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> 36b1de0d13SMitchel Humpherys #include <linux/printk.h> 371da177e4SLinus Torvalds 387c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 391da177e4SLinus Torvalds #include <asm/tlb.h> 401da177e4SLinus Torvalds #include <asm/tlbflush.h> 41eb8cdec4SBernd Schmidt #include <asm/mmu_context.h> 428feae131SDavid Howells #include "internal.h" 438feae131SDavid Howells 441da177e4SLinus Torvalds void *high_memory; 45944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory); 461da177e4SLinus Torvalds struct page *mem_map; 471da177e4SLinus Torvalds unsigned long max_mapnr; 485b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr); 494266c97aSHugh Dickins unsigned long highest_memmap_pfn; 50fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; 511da177e4SLinus Torvalds int heap_stack_gap = 0; 521da177e4SLinus Torvalds 5333e5d769SDavid Howells atomic_long_t mmap_pages_allocated; 548feae131SDavid Howells 551da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map); 561da177e4SLinus Torvalds 578feae131SDavid Howells /* list of mapped, potentially shareable regions */ 588feae131SDavid Howells static struct kmem_cache *vm_region_jar; 598feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT; 608feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem); 611da177e4SLinus Torvalds 62f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = { 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * Return the total memory allocated for this pointer, not 671da177e4SLinus Torvalds * just what the caller asked for. 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds * Doesn't have to be accurate, i.e. may have races. 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds unsigned int kobjsize(const void *objp) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct page *page; 741da177e4SLinus Torvalds 754016a139SMichael Hennerich /* 764016a139SMichael Hennerich * If the object we have should not have ksize performed on it, 774016a139SMichael Hennerich * return size of 0 784016a139SMichael Hennerich */ 795a1603beSPaul Mundt if (!objp || !virt_addr_valid(objp)) 806cfd53fcSPaul Mundt return 0; 816cfd53fcSPaul Mundt 826cfd53fcSPaul Mundt page = virt_to_head_page(objp); 836cfd53fcSPaul Mundt 846cfd53fcSPaul Mundt /* 856cfd53fcSPaul Mundt * If the allocator sets PageSlab, we know the pointer came from 866cfd53fcSPaul Mundt * kmalloc(). 876cfd53fcSPaul Mundt */ 881da177e4SLinus Torvalds if (PageSlab(page)) 891da177e4SLinus Torvalds return ksize(objp); 901da177e4SLinus Torvalds 916cfd53fcSPaul Mundt /* 92ab2e83eaSPaul Mundt * If it's not a compound page, see if we have a matching VMA 93ab2e83eaSPaul Mundt * region. This test is intentionally done in reverse order, 94ab2e83eaSPaul Mundt * so if there's no VMA, we still fall through and hand back 95ab2e83eaSPaul Mundt * PAGE_SIZE for 0-order pages. 96ab2e83eaSPaul Mundt */ 97ab2e83eaSPaul Mundt if (!PageCompound(page)) { 98ab2e83eaSPaul Mundt struct vm_area_struct *vma; 99ab2e83eaSPaul Mundt 100ab2e83eaSPaul Mundt vma = find_vma(current->mm, (unsigned long)objp); 101ab2e83eaSPaul Mundt if (vma) 102ab2e83eaSPaul Mundt return vma->vm_end - vma->vm_start; 103ab2e83eaSPaul Mundt } 104ab2e83eaSPaul Mundt 105ab2e83eaSPaul Mundt /* 1066cfd53fcSPaul Mundt * The ksize() function is only guaranteed to work for pointers 1075a1603beSPaul Mundt * returned by kmalloc(). So handle arbitrary pointers here. 1086cfd53fcSPaul Mundt */ 1095a1603beSPaul Mundt return PAGE_SIZE << compound_order(page); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1120d731759SLorenzo Stoakes static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 11328a35716SMichel Lespinasse unsigned long start, unsigned long nr_pages, 11428a35716SMichel Lespinasse unsigned int foll_flags, struct page **pages, 11528a35716SMichel Lespinasse struct vm_area_struct **vmas, int *nonblocking) 1161da177e4SLinus Torvalds { 117910e46daSSonic Zhang struct vm_area_struct *vma; 1187b4d5b8bSDavid Howells unsigned long vm_flags; 1197b4d5b8bSDavid Howells int i; 1207b4d5b8bSDavid Howells 1217b4d5b8bSDavid Howells /* calculate required read or write permissions. 12258fa879eSHugh Dickins * If FOLL_FORCE is set, we only require the "MAY" flags. 1237b4d5b8bSDavid Howells */ 12458fa879eSHugh Dickins vm_flags = (foll_flags & FOLL_WRITE) ? 12558fa879eSHugh Dickins (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 12658fa879eSHugh Dickins vm_flags &= (foll_flags & FOLL_FORCE) ? 12758fa879eSHugh Dickins (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 1281da177e4SLinus Torvalds 1299d73777eSPeter Zijlstra for (i = 0; i < nr_pages; i++) { 1307561e8caSDavid Howells vma = find_vma(mm, start); 131910e46daSSonic Zhang if (!vma) 1327b4d5b8bSDavid Howells goto finish_or_fault; 1337b4d5b8bSDavid Howells 1347b4d5b8bSDavid Howells /* protect what we can, including chardevs */ 1351c3aff1cSHugh Dickins if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || 1361c3aff1cSHugh Dickins !(vm_flags & vma->vm_flags)) 1377b4d5b8bSDavid Howells goto finish_or_fault; 138910e46daSSonic Zhang 1391da177e4SLinus Torvalds if (pages) { 1401da177e4SLinus Torvalds pages[i] = virt_to_page(start); 1411da177e4SLinus Torvalds if (pages[i]) 14209cbfeafSKirill A. Shutemov get_page(pages[i]); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds if (vmas) 145910e46daSSonic Zhang vmas[i] = vma; 146e1ee65d8SDavid Howells start = (start + PAGE_SIZE) & PAGE_MASK; 1471da177e4SLinus Torvalds } 1487b4d5b8bSDavid Howells 1497b4d5b8bSDavid Howells return i; 1507b4d5b8bSDavid Howells 1517b4d5b8bSDavid Howells finish_or_fault: 1527b4d5b8bSDavid Howells return i ? : -EFAULT; 1531da177e4SLinus Torvalds } 154b291f000SNick Piggin 155b291f000SNick Piggin /* 156b291f000SNick Piggin * get a list of pages in an address range belonging to the specified process 157b291f000SNick Piggin * and indicate the VMA that covers each page 158b291f000SNick Piggin * - this is potentially dodgy as we may end incrementing the page count of a 159b291f000SNick Piggin * slab page or a secondary page from a compound page 160b291f000SNick Piggin * - don't permit access to VMAs that don't support it, such as I/O mappings 161b291f000SNick Piggin */ 162c12d2da5SIngo Molnar long get_user_pages(unsigned long start, unsigned long nr_pages, 163768ae309SLorenzo Stoakes unsigned int gup_flags, struct page **pages, 16428a35716SMichel Lespinasse struct vm_area_struct **vmas) 165b291f000SNick Piggin { 166768ae309SLorenzo Stoakes return __get_user_pages(current, current->mm, start, nr_pages, 167768ae309SLorenzo Stoakes gup_flags, pages, vmas, NULL); 168b291f000SNick Piggin } 169c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages); 17066aa2b4bSGreg Ungerer 171c12d2da5SIngo Molnar long get_user_pages_locked(unsigned long start, unsigned long nr_pages, 1723b913179SLorenzo Stoakes unsigned int gup_flags, struct page **pages, 173f0818f47SAndrea Arcangeli int *locked) 174f0818f47SAndrea Arcangeli { 175768ae309SLorenzo Stoakes return get_user_pages(start, nr_pages, gup_flags, pages, NULL); 176f0818f47SAndrea Arcangeli } 177c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages_locked); 178f0818f47SAndrea Arcangeli 1798b7457efSLorenzo Stoakes static long __get_user_pages_unlocked(struct task_struct *tsk, 1808b7457efSLorenzo Stoakes struct mm_struct *mm, unsigned long start, 1818b7457efSLorenzo Stoakes unsigned long nr_pages, struct page **pages, 1828b7457efSLorenzo Stoakes unsigned int gup_flags) 183f0818f47SAndrea Arcangeli { 184f0818f47SAndrea Arcangeli long ret; 185f0818f47SAndrea Arcangeli down_read(&mm->mmap_sem); 186cde70140SDave Hansen ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages, 187cde70140SDave Hansen NULL, NULL); 188f0818f47SAndrea Arcangeli up_read(&mm->mmap_sem); 189f0818f47SAndrea Arcangeli return ret; 190f0818f47SAndrea Arcangeli } 1910fd71a56SAndrea Arcangeli 192c12d2da5SIngo Molnar long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, 193c164154fSLorenzo Stoakes struct page **pages, unsigned int gup_flags) 1940fd71a56SAndrea Arcangeli { 195cde70140SDave Hansen return __get_user_pages_unlocked(current, current->mm, start, nr_pages, 196c164154fSLorenzo Stoakes pages, gup_flags); 1970fd71a56SAndrea Arcangeli } 198c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages_unlocked); 199f0818f47SAndrea Arcangeli 200dfc2f91aSPaul Mundt /** 201dfc2f91aSPaul Mundt * follow_pfn - look up PFN at a user virtual address 202dfc2f91aSPaul Mundt * @vma: memory mapping 203dfc2f91aSPaul Mundt * @address: user virtual address 204dfc2f91aSPaul Mundt * @pfn: location to store found PFN 205dfc2f91aSPaul Mundt * 206dfc2f91aSPaul Mundt * Only IO mappings and raw PFN mappings are allowed. 207dfc2f91aSPaul Mundt * 208dfc2f91aSPaul Mundt * Returns zero and the pfn at @pfn on success, -ve otherwise. 209dfc2f91aSPaul Mundt */ 210dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address, 211dfc2f91aSPaul Mundt unsigned long *pfn) 212dfc2f91aSPaul Mundt { 213dfc2f91aSPaul Mundt if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 214dfc2f91aSPaul Mundt return -EINVAL; 215dfc2f91aSPaul Mundt 216dfc2f91aSPaul Mundt *pfn = address >> PAGE_SHIFT; 217dfc2f91aSPaul Mundt return 0; 218dfc2f91aSPaul Mundt } 219dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn); 220dfc2f91aSPaul Mundt 221f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list); 2221da177e4SLinus Torvalds 223b3bdda02SChristoph Lameter void vfree(const void *addr) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds kfree(addr); 2261da177e4SLinus Torvalds } 227b5073173SPaul Mundt EXPORT_SYMBOL(vfree); 2281da177e4SLinus Torvalds 229dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds /* 2328518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 2338518609dSRobert P. J. Day * returns only a logical address. 2341da177e4SLinus Torvalds */ 23584097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 2361da177e4SLinus Torvalds } 237b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc); 2381da177e4SLinus Torvalds 239f905bc44SPaul Mundt void *vmalloc_user(unsigned long size) 240f905bc44SPaul Mundt { 241f905bc44SPaul Mundt void *ret; 242f905bc44SPaul Mundt 243f905bc44SPaul Mundt ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 244f905bc44SPaul Mundt PAGE_KERNEL); 245f905bc44SPaul Mundt if (ret) { 246f905bc44SPaul Mundt struct vm_area_struct *vma; 247f905bc44SPaul Mundt 248f905bc44SPaul Mundt down_write(¤t->mm->mmap_sem); 249f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret); 250f905bc44SPaul Mundt if (vma) 251f905bc44SPaul Mundt vma->vm_flags |= VM_USERMAP; 252f905bc44SPaul Mundt up_write(¤t->mm->mmap_sem); 253f905bc44SPaul Mundt } 254f905bc44SPaul Mundt 255f905bc44SPaul Mundt return ret; 256f905bc44SPaul Mundt } 257f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user); 258f905bc44SPaul Mundt 259b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds return virt_to_page(addr); 2621da177e4SLinus Torvalds } 263b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page); 2641da177e4SLinus Torvalds 265b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr)); 2681da177e4SLinus Torvalds } 269b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count) 2721da177e4SLinus Torvalds { 2739bde916bSChen Gang /* Don't allow overflow */ 2749bde916bSChen Gang if ((unsigned long) buf + count < count) 2759bde916bSChen Gang count = -(unsigned long) buf; 2769bde916bSChen Gang 2771da177e4SLinus Torvalds memcpy(buf, addr, count); 2781da177e4SLinus Torvalds return count; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count) 2821da177e4SLinus Torvalds { 2831da177e4SLinus Torvalds /* Don't allow overflow */ 2841da177e4SLinus Torvalds if ((unsigned long) addr + count < count) 2851da177e4SLinus Torvalds count = -(unsigned long) addr; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds memcpy(addr, buf, count); 288ac714904SChoi Gi-yong return count; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 292e1c05067SMasahiro Yamada * vmalloc - allocate virtually contiguous memory 2931da177e4SLinus Torvalds * 2941da177e4SLinus Torvalds * @size: allocation size 2951da177e4SLinus Torvalds * 2961da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level 297e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space. 2981da177e4SLinus Torvalds * 299c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags 3001da177e4SLinus Torvalds * use __vmalloc() instead. 3011da177e4SLinus Torvalds */ 3021da177e4SLinus Torvalds void *vmalloc(unsigned long size) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL); 3051da177e4SLinus Torvalds } 306f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc); 307f6138882SAndrew Morton 308e1ca7788SDave Young /* 309e1c05067SMasahiro Yamada * vzalloc - allocate virtually contiguous memory with zero fill 310e1ca7788SDave Young * 311e1ca7788SDave Young * @size: allocation size 312e1ca7788SDave Young * 313e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 314e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space. 315e1ca7788SDave Young * The memory allocated is set to zero. 316e1ca7788SDave Young * 317e1ca7788SDave Young * For tight control over page level allocator and protection flags 318e1ca7788SDave Young * use __vmalloc() instead. 319e1ca7788SDave Young */ 320e1ca7788SDave Young void *vzalloc(unsigned long size) 321e1ca7788SDave Young { 322e1ca7788SDave Young return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 323e1ca7788SDave Young PAGE_KERNEL); 324e1ca7788SDave Young } 325e1ca7788SDave Young EXPORT_SYMBOL(vzalloc); 326e1ca7788SDave Young 327e1ca7788SDave Young /** 328e1ca7788SDave Young * vmalloc_node - allocate memory on a specific node 329e1ca7788SDave Young * @size: allocation size 330e1ca7788SDave Young * @node: numa node 331e1ca7788SDave Young * 332e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 333e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 334e1ca7788SDave Young * 335e1ca7788SDave Young * For tight control over page level allocator and protection flags 336e1ca7788SDave Young * use __vmalloc() instead. 337e1ca7788SDave Young */ 338f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node) 339f6138882SAndrew Morton { 340f6138882SAndrew Morton return vmalloc(size); 341f6138882SAndrew Morton } 3429a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node); 343e1ca7788SDave Young 344e1ca7788SDave Young /** 345e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill 346e1ca7788SDave Young * @size: allocation size 347e1ca7788SDave Young * @node: numa node 348e1ca7788SDave Young * 349e1ca7788SDave Young * Allocate enough pages to cover @size from the page level 350e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space. 351e1ca7788SDave Young * The memory allocated is set to zero. 352e1ca7788SDave Young * 353e1ca7788SDave Young * For tight control over page level allocator and protection flags 354e1ca7788SDave Young * use __vmalloc() instead. 355e1ca7788SDave Young */ 356e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node) 357e1ca7788SDave Young { 358e1ca7788SDave Young return vzalloc(size); 359e1ca7788SDave Young } 360e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node); 3611da177e4SLinus Torvalds 3621af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC 3631af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL 3641af446edSPaul Mundt #endif 3651af446edSPaul Mundt 3661af446edSPaul Mundt /** 3671af446edSPaul Mundt * vmalloc_exec - allocate virtually contiguous, executable memory 3681af446edSPaul Mundt * @size: allocation size 3691af446edSPaul Mundt * 3701af446edSPaul Mundt * Kernel-internal function to allocate enough pages to cover @size 3711af446edSPaul Mundt * the page level allocator and map them into contiguous and 3721af446edSPaul Mundt * executable kernel virtual space. 3731af446edSPaul Mundt * 3741af446edSPaul Mundt * For tight control over page level allocator and protection flags 3751af446edSPaul Mundt * use __vmalloc() instead. 3761af446edSPaul Mundt */ 3771af446edSPaul Mundt 3781af446edSPaul Mundt void *vmalloc_exec(unsigned long size) 3791af446edSPaul Mundt { 3801af446edSPaul Mundt return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC); 3811af446edSPaul Mundt } 3821af446edSPaul Mundt 383b5073173SPaul Mundt /** 384b5073173SPaul Mundt * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 3851da177e4SLinus Torvalds * @size: allocation size 3861da177e4SLinus Torvalds * 3871da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the 388e1c05067SMasahiro Yamada * page level allocator and map them into contiguous kernel virtual space. 3891da177e4SLinus Torvalds */ 3901da177e4SLinus Torvalds void *vmalloc_32(unsigned long size) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL); 3931da177e4SLinus Torvalds } 394b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32); 395b5073173SPaul Mundt 396b5073173SPaul Mundt /** 397b5073173SPaul Mundt * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 398b5073173SPaul Mundt * @size: allocation size 399b5073173SPaul Mundt * 400b5073173SPaul Mundt * The resulting memory area is 32bit addressable and zeroed so it can be 401b5073173SPaul Mundt * mapped to userspace without leaking data. 402f905bc44SPaul Mundt * 403f905bc44SPaul Mundt * VM_USERMAP is set on the corresponding VMA so that subsequent calls to 404f905bc44SPaul Mundt * remap_vmalloc_range() are permissible. 405b5073173SPaul Mundt */ 406b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size) 407b5073173SPaul Mundt { 408f905bc44SPaul Mundt /* 409f905bc44SPaul Mundt * We'll have to sort out the ZONE_DMA bits for 64-bit, 410f905bc44SPaul Mundt * but for now this can simply use vmalloc_user() directly. 411f905bc44SPaul Mundt */ 412f905bc44SPaul Mundt return vmalloc_user(size); 413b5073173SPaul Mundt } 414b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user); 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds BUG(); 4191da177e4SLinus Torvalds return NULL; 4201da177e4SLinus Torvalds } 421b5073173SPaul Mundt EXPORT_SYMBOL(vmap); 4221da177e4SLinus Torvalds 423b3bdda02SChristoph Lameter void vunmap(const void *addr) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds BUG(); 4261da177e4SLinus Torvalds } 427b5073173SPaul Mundt EXPORT_SYMBOL(vunmap); 4281da177e4SLinus Torvalds 429eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 430eb6434d9SPaul Mundt { 431eb6434d9SPaul Mundt BUG(); 432eb6434d9SPaul Mundt return NULL; 433eb6434d9SPaul Mundt } 434eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram); 435eb6434d9SPaul Mundt 436eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count) 437eb6434d9SPaul Mundt { 438eb6434d9SPaul Mundt BUG(); 439eb6434d9SPaul Mundt } 440eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram); 441eb6434d9SPaul Mundt 442eb6434d9SPaul Mundt void vm_unmap_aliases(void) 443eb6434d9SPaul Mundt { 444eb6434d9SPaul Mundt } 445eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases); 446eb6434d9SPaul Mundt 4471da177e4SLinus Torvalds /* 4481eeb66a1SChristoph Hellwig * Implement a stub for vmalloc_sync_all() if the architecture chose not to 4491eeb66a1SChristoph Hellwig * have one. 4501eeb66a1SChristoph Hellwig */ 4513b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void) 4521eeb66a1SChristoph Hellwig { 4531eeb66a1SChristoph Hellwig } 4541eeb66a1SChristoph Hellwig 45529c185e5SPaul Mundt /** 45629c185e5SPaul Mundt * alloc_vm_area - allocate a range of kernel address space 45729c185e5SPaul Mundt * @size: size of the area 45829c185e5SPaul Mundt * 45929c185e5SPaul Mundt * Returns: NULL on failure, vm_struct on success 46029c185e5SPaul Mundt * 46129c185e5SPaul Mundt * This function reserves a range of kernel address space, and 46229c185e5SPaul Mundt * allocates pagetables to map that range. No actual mappings 46329c185e5SPaul Mundt * are created. If the kernel address space is not shared 46429c185e5SPaul Mundt * between processes, it syncs the pagetable across all 46529c185e5SPaul Mundt * processes. 46629c185e5SPaul Mundt */ 467cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) 46829c185e5SPaul Mundt { 46929c185e5SPaul Mundt BUG(); 47029c185e5SPaul Mundt return NULL; 47129c185e5SPaul Mundt } 47229c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area); 47329c185e5SPaul Mundt 47429c185e5SPaul Mundt void free_vm_area(struct vm_struct *area) 47529c185e5SPaul Mundt { 47629c185e5SPaul Mundt BUG(); 47729c185e5SPaul Mundt } 47829c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area); 47929c185e5SPaul Mundt 480b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 481b5073173SPaul Mundt struct page *page) 482b5073173SPaul Mundt { 483b5073173SPaul Mundt return -EINVAL; 484b5073173SPaul Mundt } 485b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page); 486b5073173SPaul Mundt 4871eeb66a1SChristoph Hellwig /* 4881da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel 4891da177e4SLinus Torvalds * lock, except when an application is doing something nasty 4901da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped 4911da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need 4921da177e4SLinus Torvalds * to invoke file system routines that need the global lock. 4931da177e4SLinus Torvalds */ 4946a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk) 4951da177e4SLinus Torvalds { 4961da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds if (brk < mm->start_brk || brk > mm->context.end_brk) 4991da177e4SLinus Torvalds return mm->brk; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (mm->brk == brk) 5021da177e4SLinus Torvalds return mm->brk; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* 5051da177e4SLinus Torvalds * Always allow shrinking brk 5061da177e4SLinus Torvalds */ 5071da177e4SLinus Torvalds if (brk <= mm->brk) { 5081da177e4SLinus Torvalds mm->brk = brk; 5091da177e4SLinus Torvalds return brk; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds * Ok, looks good - let it rip. 5141da177e4SLinus Torvalds */ 515cfe79c00SMike Frysinger flush_icache_range(mm->brk, brk); 5161da177e4SLinus Torvalds return mm->brk = brk; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5198feae131SDavid Howells /* 520*3edf41d8Sseokhoon.yoon * initialise the percpu counter for VM and region record slabs 5218feae131SDavid Howells */ 5228feae131SDavid Howells void __init mmap_init(void) 5231da177e4SLinus Torvalds { 52400a62ce9SKOSAKI Motohiro int ret; 52500a62ce9SKOSAKI Motohiro 526908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); 52700a62ce9SKOSAKI Motohiro VM_BUG_ON(ret); 5285d097056SVladimir Davydov vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT); 5298feae131SDavid Howells } 5301da177e4SLinus Torvalds 5318feae131SDavid Howells /* 5328feae131SDavid Howells * validate the region tree 5338feae131SDavid Howells * - the caller must hold the region lock 5348feae131SDavid Howells */ 5358feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS 5368feae131SDavid Howells static noinline void validate_nommu_regions(void) 5378feae131SDavid Howells { 5388feae131SDavid Howells struct vm_region *region, *last; 5398feae131SDavid Howells struct rb_node *p, *lastp; 5401da177e4SLinus Torvalds 5418feae131SDavid Howells lastp = rb_first(&nommu_region_tree); 5428feae131SDavid Howells if (!lastp) 5438feae131SDavid Howells return; 5448feae131SDavid Howells 5458feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 546c9427bc0SGeliang Tang BUG_ON(last->vm_end <= last->vm_start); 547c9427bc0SGeliang Tang BUG_ON(last->vm_top < last->vm_end); 5488feae131SDavid Howells 5498feae131SDavid Howells while ((p = rb_next(lastp))) { 5508feae131SDavid Howells region = rb_entry(p, struct vm_region, vm_rb); 5518feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb); 5528feae131SDavid Howells 553c9427bc0SGeliang Tang BUG_ON(region->vm_end <= region->vm_start); 554c9427bc0SGeliang Tang BUG_ON(region->vm_top < region->vm_end); 555c9427bc0SGeliang Tang BUG_ON(region->vm_start < last->vm_top); 5568feae131SDavid Howells 5578feae131SDavid Howells lastp = p; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds } 5608feae131SDavid Howells #else 56133e5d769SDavid Howells static void validate_nommu_regions(void) 56233e5d769SDavid Howells { 56333e5d769SDavid Howells } 5648feae131SDavid Howells #endif 5658feae131SDavid Howells 5668feae131SDavid Howells /* 5678feae131SDavid Howells * add a region into the global tree 5688feae131SDavid Howells */ 5698feae131SDavid Howells static void add_nommu_region(struct vm_region *region) 5708feae131SDavid Howells { 5718feae131SDavid Howells struct vm_region *pregion; 5728feae131SDavid Howells struct rb_node **p, *parent; 5738feae131SDavid Howells 5748feae131SDavid Howells validate_nommu_regions(); 5758feae131SDavid Howells 5768feae131SDavid Howells parent = NULL; 5778feae131SDavid Howells p = &nommu_region_tree.rb_node; 5788feae131SDavid Howells while (*p) { 5798feae131SDavid Howells parent = *p; 5808feae131SDavid Howells pregion = rb_entry(parent, struct vm_region, vm_rb); 5818feae131SDavid Howells if (region->vm_start < pregion->vm_start) 5828feae131SDavid Howells p = &(*p)->rb_left; 5838feae131SDavid Howells else if (region->vm_start > pregion->vm_start) 5848feae131SDavid Howells p = &(*p)->rb_right; 5858feae131SDavid Howells else if (pregion == region) 5868feae131SDavid Howells return; 5878feae131SDavid Howells else 5888feae131SDavid Howells BUG(); 5898feae131SDavid Howells } 5908feae131SDavid Howells 5918feae131SDavid Howells rb_link_node(®ion->vm_rb, parent, p); 5928feae131SDavid Howells rb_insert_color(®ion->vm_rb, &nommu_region_tree); 5938feae131SDavid Howells 5948feae131SDavid Howells validate_nommu_regions(); 5958feae131SDavid Howells } 5968feae131SDavid Howells 5978feae131SDavid Howells /* 5988feae131SDavid Howells * delete a region from the global tree 5998feae131SDavid Howells */ 6008feae131SDavid Howells static void delete_nommu_region(struct vm_region *region) 6018feae131SDavid Howells { 6028feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 6038feae131SDavid Howells 6048feae131SDavid Howells validate_nommu_regions(); 6058feae131SDavid Howells rb_erase(®ion->vm_rb, &nommu_region_tree); 6068feae131SDavid Howells validate_nommu_regions(); 6078feae131SDavid Howells } 6088feae131SDavid Howells 6098feae131SDavid Howells /* 6108feae131SDavid Howells * free a contiguous series of pages 6118feae131SDavid Howells */ 6128feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to) 6138feae131SDavid Howells { 6148feae131SDavid Howells for (; from < to; from += PAGE_SIZE) { 6158feae131SDavid Howells struct page *page = virt_to_page(from); 6168feae131SDavid Howells 61733e5d769SDavid Howells atomic_long_dec(&mmap_pages_allocated); 6188feae131SDavid Howells put_page(page); 6198feae131SDavid Howells } 6208feae131SDavid Howells } 6218feae131SDavid Howells 6228feae131SDavid Howells /* 6238feae131SDavid Howells * release a reference to a region 62433e5d769SDavid Howells * - the caller must hold the region semaphore for writing, which this releases 625dd8632a1SPaul Mundt * - the region may not have been added to the tree yet, in which case vm_top 6268feae131SDavid Howells * will equal vm_start 6278feae131SDavid Howells */ 6288feae131SDavid Howells static void __put_nommu_region(struct vm_region *region) 6298feae131SDavid Howells __releases(nommu_region_sem) 6308feae131SDavid Howells { 6318feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node); 6328feae131SDavid Howells 6331e2ae599SDavid Howells if (--region->vm_usage == 0) { 634dd8632a1SPaul Mundt if (region->vm_top > region->vm_start) 6358feae131SDavid Howells delete_nommu_region(region); 6368feae131SDavid Howells up_write(&nommu_region_sem); 6378feae131SDavid Howells 6388feae131SDavid Howells if (region->vm_file) 6398feae131SDavid Howells fput(region->vm_file); 6408feae131SDavid Howells 6418feae131SDavid Howells /* IO memory and memory shared directly out of the pagecache 6428feae131SDavid Howells * from ramfs/tmpfs mustn't be released here */ 64322cc877bSLeon Romanovsky if (region->vm_flags & VM_MAPPED_COPY) 644dd8632a1SPaul Mundt free_page_series(region->vm_start, region->vm_top); 6458feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 6468feae131SDavid Howells } else { 6478feae131SDavid Howells up_write(&nommu_region_sem); 6488feae131SDavid Howells } 6498feae131SDavid Howells } 6508feae131SDavid Howells 6518feae131SDavid Howells /* 6528feae131SDavid Howells * release a reference to a region 6538feae131SDavid Howells */ 6548feae131SDavid Howells static void put_nommu_region(struct vm_region *region) 6558feae131SDavid Howells { 6568feae131SDavid Howells down_write(&nommu_region_sem); 6578feae131SDavid Howells __put_nommu_region(region); 6588feae131SDavid Howells } 6591da177e4SLinus Torvalds 6603034097aSDavid Howells /* 661eb8cdec4SBernd Schmidt * update protection on a vma 662eb8cdec4SBernd Schmidt */ 663eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags) 664eb8cdec4SBernd Schmidt { 665eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU 666eb8cdec4SBernd Schmidt struct mm_struct *mm = vma->vm_mm; 667eb8cdec4SBernd Schmidt long start = vma->vm_start & PAGE_MASK; 668eb8cdec4SBernd Schmidt while (start < vma->vm_end) { 669eb8cdec4SBernd Schmidt protect_page(mm, start, flags); 670eb8cdec4SBernd Schmidt start += PAGE_SIZE; 671eb8cdec4SBernd Schmidt } 672eb8cdec4SBernd Schmidt update_protections(mm); 673eb8cdec4SBernd Schmidt #endif 674eb8cdec4SBernd Schmidt } 675eb8cdec4SBernd Schmidt 676eb8cdec4SBernd Schmidt /* 6773034097aSDavid Howells * add a VMA into a process's mm_struct in the appropriate place in the list 6788feae131SDavid Howells * and tree and add to the address space's page tree also if not an anonymous 6798feae131SDavid Howells * page 6803034097aSDavid Howells * - should be called with mm->mmap_sem held writelocked 6813034097aSDavid Howells */ 6828feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) 6833034097aSDavid Howells { 6846038def0SNamhyung Kim struct vm_area_struct *pvma, *prev; 6858feae131SDavid Howells struct address_space *mapping; 6866038def0SNamhyung Kim struct rb_node **p, *parent, *rb_prev; 6873034097aSDavid Howells 6888feae131SDavid Howells BUG_ON(!vma->vm_region); 6898feae131SDavid Howells 6908feae131SDavid Howells mm->map_count++; 6918feae131SDavid Howells vma->vm_mm = mm; 6928feae131SDavid Howells 693eb8cdec4SBernd Schmidt protect_vma(vma, vma->vm_flags); 694eb8cdec4SBernd Schmidt 6958feae131SDavid Howells /* add the VMA to the mapping */ 6968feae131SDavid Howells if (vma->vm_file) { 6978feae131SDavid Howells mapping = vma->vm_file->f_mapping; 6988feae131SDavid Howells 69983cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 7008feae131SDavid Howells flush_dcache_mmap_lock(mapping); 7016b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap); 7028feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 70383cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 7048feae131SDavid Howells } 7058feae131SDavid Howells 7068feae131SDavid Howells /* add the VMA to the tree */ 7076038def0SNamhyung Kim parent = rb_prev = NULL; 7088feae131SDavid Howells p = &mm->mm_rb.rb_node; 7098feae131SDavid Howells while (*p) { 7108feae131SDavid Howells parent = *p; 7118feae131SDavid Howells pvma = rb_entry(parent, struct vm_area_struct, vm_rb); 7128feae131SDavid Howells 7138feae131SDavid Howells /* sort by: start addr, end addr, VMA struct addr in that order 7148feae131SDavid Howells * (the latter is necessary as we may get identical VMAs) */ 7158feae131SDavid Howells if (vma->vm_start < pvma->vm_start) 7168feae131SDavid Howells p = &(*p)->rb_left; 7176038def0SNamhyung Kim else if (vma->vm_start > pvma->vm_start) { 7186038def0SNamhyung Kim rb_prev = parent; 7198feae131SDavid Howells p = &(*p)->rb_right; 7206038def0SNamhyung Kim } else if (vma->vm_end < pvma->vm_end) 7218feae131SDavid Howells p = &(*p)->rb_left; 7226038def0SNamhyung Kim else if (vma->vm_end > pvma->vm_end) { 7236038def0SNamhyung Kim rb_prev = parent; 7248feae131SDavid Howells p = &(*p)->rb_right; 7256038def0SNamhyung Kim } else if (vma < pvma) 7268feae131SDavid Howells p = &(*p)->rb_left; 7276038def0SNamhyung Kim else if (vma > pvma) { 7286038def0SNamhyung Kim rb_prev = parent; 7298feae131SDavid Howells p = &(*p)->rb_right; 7306038def0SNamhyung Kim } else 7318feae131SDavid Howells BUG(); 7328feae131SDavid Howells } 7338feae131SDavid Howells 7348feae131SDavid Howells rb_link_node(&vma->vm_rb, parent, p); 7358feae131SDavid Howells rb_insert_color(&vma->vm_rb, &mm->mm_rb); 7368feae131SDavid Howells 7378feae131SDavid Howells /* add VMA to the VMA list also */ 7386038def0SNamhyung Kim prev = NULL; 7396038def0SNamhyung Kim if (rb_prev) 7406038def0SNamhyung Kim prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); 7413034097aSDavid Howells 7426038def0SNamhyung Kim __vma_link_list(mm, vma, prev, parent); 7438feae131SDavid Howells } 7448feae131SDavid Howells 7458feae131SDavid Howells /* 7468feae131SDavid Howells * delete a VMA from its owning mm_struct and address space 7478feae131SDavid Howells */ 7488feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma) 7498feae131SDavid Howells { 750615d6e87SDavidlohr Bueso int i; 7518feae131SDavid Howells struct address_space *mapping; 7528feae131SDavid Howells struct mm_struct *mm = vma->vm_mm; 753615d6e87SDavidlohr Bueso struct task_struct *curr = current; 7548feae131SDavid Howells 755eb8cdec4SBernd Schmidt protect_vma(vma, 0); 756eb8cdec4SBernd Schmidt 7578feae131SDavid Howells mm->map_count--; 758615d6e87SDavidlohr Bueso for (i = 0; i < VMACACHE_SIZE; i++) { 759615d6e87SDavidlohr Bueso /* if the vma is cached, invalidate the entire cache */ 760615d6e87SDavidlohr Bueso if (curr->vmacache[i] == vma) { 761e020d5bdSSteven Miao vmacache_invalidate(mm); 762615d6e87SDavidlohr Bueso break; 763615d6e87SDavidlohr Bueso } 764615d6e87SDavidlohr Bueso } 7658feae131SDavid Howells 7668feae131SDavid Howells /* remove the VMA from the mapping */ 7678feae131SDavid Howells if (vma->vm_file) { 7688feae131SDavid Howells mapping = vma->vm_file->f_mapping; 7698feae131SDavid Howells 77083cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping); 7718feae131SDavid Howells flush_dcache_mmap_lock(mapping); 7726b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap); 7738feae131SDavid Howells flush_dcache_mmap_unlock(mapping); 77483cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping); 7758feae131SDavid Howells } 7768feae131SDavid Howells 7778feae131SDavid Howells /* remove from the MM's tree and list */ 7788feae131SDavid Howells rb_erase(&vma->vm_rb, &mm->mm_rb); 779b951bf2cSNamhyung Kim 780b951bf2cSNamhyung Kim if (vma->vm_prev) 781b951bf2cSNamhyung Kim vma->vm_prev->vm_next = vma->vm_next; 782b951bf2cSNamhyung Kim else 783b951bf2cSNamhyung Kim mm->mmap = vma->vm_next; 784b951bf2cSNamhyung Kim 785b951bf2cSNamhyung Kim if (vma->vm_next) 786b951bf2cSNamhyung Kim vma->vm_next->vm_prev = vma->vm_prev; 7878feae131SDavid Howells } 7888feae131SDavid Howells 7898feae131SDavid Howells /* 7908feae131SDavid Howells * destroy a VMA record 7918feae131SDavid Howells */ 7928feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 7938feae131SDavid Howells { 7948feae131SDavid Howells if (vma->vm_ops && vma->vm_ops->close) 7958feae131SDavid Howells vma->vm_ops->close(vma); 796e9714acfSKonstantin Khlebnikov if (vma->vm_file) 7978feae131SDavid Howells fput(vma->vm_file); 7988feae131SDavid Howells put_nommu_region(vma->vm_region); 7998feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 8003034097aSDavid Howells } 8013034097aSDavid Howells 8023034097aSDavid Howells /* 8033034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none 8043034097aSDavid Howells * - should be called with mm->mmap_sem at least held readlocked 8053034097aSDavid Howells */ 8063034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 8073034097aSDavid Howells { 8088feae131SDavid Howells struct vm_area_struct *vma; 8093034097aSDavid Howells 8108feae131SDavid Howells /* check the cache first */ 811615d6e87SDavidlohr Bueso vma = vmacache_find(mm, addr); 812615d6e87SDavidlohr Bueso if (likely(vma)) 8138feae131SDavid Howells return vma; 8148feae131SDavid Howells 815e922c4c5SNamhyung Kim /* trawl the list (there may be multiple mappings in which addr 8168feae131SDavid Howells * resides) */ 817e922c4c5SNamhyung Kim for (vma = mm->mmap; vma; vma = vma->vm_next) { 8188feae131SDavid Howells if (vma->vm_start > addr) 8198feae131SDavid Howells return NULL; 8208feae131SDavid Howells if (vma->vm_end > addr) { 821615d6e87SDavidlohr Bueso vmacache_update(addr, vma); 8228feae131SDavid Howells return vma; 8233034097aSDavid Howells } 8248feae131SDavid Howells } 8253034097aSDavid Howells 8263034097aSDavid Howells return NULL; 8273034097aSDavid Howells } 8283034097aSDavid Howells EXPORT_SYMBOL(find_vma); 8293034097aSDavid Howells 8303034097aSDavid Howells /* 831930e652aSDavid Howells * find a VMA 832930e652aSDavid Howells * - we don't extend stack VMAs under NOMMU conditions 833930e652aSDavid Howells */ 834930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 835930e652aSDavid Howells { 8367561e8caSDavid Howells return find_vma(mm, addr); 837930e652aSDavid Howells } 838930e652aSDavid Howells 8398feae131SDavid Howells /* 8408feae131SDavid Howells * expand a stack to a given address 8418feae131SDavid Howells * - not supported under NOMMU conditions 8428feae131SDavid Howells */ 84357c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address) 84457c8f63eSGreg Ungerer { 84557c8f63eSGreg Ungerer return -ENOMEM; 84657c8f63eSGreg Ungerer } 84757c8f63eSGreg Ungerer 848930e652aSDavid Howells /* 8496fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr 8506fa5f80bSDavid Howells * - should be called with mm->mmap_sem at least held readlocked 8516fa5f80bSDavid Howells */ 8528feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 8538feae131SDavid Howells unsigned long addr, 8548feae131SDavid Howells unsigned long len) 8551da177e4SLinus Torvalds { 8561da177e4SLinus Torvalds struct vm_area_struct *vma; 8578feae131SDavid Howells unsigned long end = addr + len; 8581da177e4SLinus Torvalds 8598feae131SDavid Howells /* check the cache first */ 860615d6e87SDavidlohr Bueso vma = vmacache_find_exact(mm, addr, end); 861615d6e87SDavidlohr Bueso if (vma) 8621da177e4SLinus Torvalds return vma; 8638feae131SDavid Howells 864e922c4c5SNamhyung Kim /* trawl the list (there may be multiple mappings in which addr 8658feae131SDavid Howells * resides) */ 866e922c4c5SNamhyung Kim for (vma = mm->mmap; vma; vma = vma->vm_next) { 8678feae131SDavid Howells if (vma->vm_start < addr) 8688feae131SDavid Howells continue; 8698feae131SDavid Howells if (vma->vm_start > addr) 8708feae131SDavid Howells return NULL; 8718feae131SDavid Howells if (vma->vm_end == end) { 872615d6e87SDavidlohr Bueso vmacache_update(addr, vma); 8738feae131SDavid Howells return vma; 8748feae131SDavid Howells } 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds return NULL; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 8803034097aSDavid Howells /* 8811da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of 8821da177e4SLinus Torvalds * mapping we're capable of supporting 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds static int validate_mmap_request(struct file *file, 8851da177e4SLinus Torvalds unsigned long addr, 8861da177e4SLinus Torvalds unsigned long len, 8871da177e4SLinus Torvalds unsigned long prot, 8881da177e4SLinus Torvalds unsigned long flags, 8891da177e4SLinus Torvalds unsigned long pgoff, 8901da177e4SLinus Torvalds unsigned long *_capabilities) 8911da177e4SLinus Torvalds { 8928feae131SDavid Howells unsigned long capabilities, rlen; 8931da177e4SLinus Torvalds int ret; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds /* do the simple checks first */ 89622cc877bSLeon Romanovsky if (flags & MAP_FIXED) 8971da177e4SLinus Torvalds return -EINVAL; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE && 9001da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED) 9011da177e4SLinus Torvalds return -EINVAL; 9021da177e4SLinus Torvalds 903f81cff0dSMike Frysinger if (!len) 9041da177e4SLinus Torvalds return -EINVAL; 9051da177e4SLinus Torvalds 906f81cff0dSMike Frysinger /* Careful about overflows.. */ 9078feae131SDavid Howells rlen = PAGE_ALIGN(len); 9088feae131SDavid Howells if (!rlen || rlen > TASK_SIZE) 909f81cff0dSMike Frysinger return -ENOMEM; 910f81cff0dSMike Frysinger 9111da177e4SLinus Torvalds /* offset overflow? */ 9128feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 913f81cff0dSMike Frysinger return -EOVERFLOW; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds if (file) { 9161da177e4SLinus Torvalds /* files must support mmap */ 91772c2d531SAl Viro if (!file->f_op->mmap) 9181da177e4SLinus Torvalds return -ENODEV; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds /* work out if what we've got could possibly be shared 9211da177e4SLinus Torvalds * - we support chardevs that provide their own "memory" 9221da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed 9231da177e4SLinus Torvalds */ 924b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) { 925b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file); 926b4caecd4SChristoph Hellwig } else { 9271da177e4SLinus Torvalds /* no explicit capabilities set, so assume some 9281da177e4SLinus Torvalds * defaults */ 929496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) { 9301da177e4SLinus Torvalds case S_IFREG: 9311da177e4SLinus Torvalds case S_IFBLK: 932b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 9331da177e4SLinus Torvalds break; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds case S_IFCHR: 9361da177e4SLinus Torvalds capabilities = 937b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT | 938b4caecd4SChristoph Hellwig NOMMU_MAP_READ | 939b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE; 9401da177e4SLinus Torvalds break; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds default: 9431da177e4SLinus Torvalds return -EINVAL; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this 9481da177e4SLinus Torvalds * device */ 9491da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area) 950b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 9516e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ)) 952b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 9531da177e4SLinus Torvalds 95428d7a6aeSGraff Yang /* The file shall have been opened with read permission. */ 95528d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ)) 95628d7a6aeSGraff Yang return -EACCES; 95728d7a6aeSGraff Yang 9581da177e4SLinus Torvalds if (flags & MAP_SHARED) { 9591da177e4SLinus Torvalds /* do checks for writing, appending and locking */ 9601da177e4SLinus Torvalds if ((prot & PROT_WRITE) && 9611da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE)) 9621da177e4SLinus Torvalds return -EACCES; 9631da177e4SLinus Torvalds 964496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) && 9651da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE)) 9661da177e4SLinus Torvalds return -EACCES; 9671da177e4SLinus Torvalds 968d7a06983SJeff Layton if (locks_verify_locked(file)) 9691da177e4SLinus Torvalds return -EAGAIN; 9701da177e4SLinus Torvalds 971b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 9721da177e4SLinus Torvalds return -ENODEV; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds /* we mustn't privatise shared mappings */ 975b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY; 976ac714904SChoi Gi-yong } else { 9771da177e4SLinus Torvalds /* we're going to read the file into private memory we 9781da177e4SLinus Torvalds * allocate */ 979b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 9801da177e4SLinus Torvalds return -ENODEV; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds /* we don't permit a private writable mapping to be 9831da177e4SLinus Torvalds * shared with the backing device */ 9841da177e4SLinus Torvalds if (prot & PROT_WRITE) 985b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 988b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 989b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || 990b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || 991b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) 9923c7b2045SBernd Schmidt ) { 993b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 9943c7b2045SBernd Schmidt if (flags & MAP_SHARED) { 99522cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n"); 9963c7b2045SBernd Schmidt return -EINVAL; 9973c7b2045SBernd Schmidt } 9983c7b2045SBernd Schmidt } 9993c7b2045SBernd Schmidt } 10003c7b2045SBernd Schmidt 10011da177e4SLinus Torvalds /* handle executable mappings and implied executable 10021da177e4SLinus Torvalds * mappings */ 100390f8572bSEric W. Biederman if (path_noexec(&file->f_path)) { 10041da177e4SLinus Torvalds if (prot & PROT_EXEC) 10051da177e4SLinus Torvalds return -EPERM; 1006ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 10071da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */ 10081da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) { 1009b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC) 10101da177e4SLinus Torvalds prot |= PROT_EXEC; 10111da177e4SLinus Torvalds } 1012ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && 10131da177e4SLinus Torvalds (prot & PROT_EXEC) && 1014b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC) 10151da177e4SLinus Torvalds ) { 10161da177e4SLinus Torvalds /* backing file is not executable, try to copy */ 1017b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 10181da177e4SLinus Torvalds } 1019ac714904SChoi Gi-yong } else { 10201da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be 10211da177e4SLinus Torvalds * privately mapped 10221da177e4SLinus Torvalds */ 1023b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */ 10261da177e4SLinus Torvalds if ((prot & PROT_READ) && 10271da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC)) 10281da177e4SLinus Torvalds prot |= PROT_EXEC; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds /* allow the security API to have its say */ 1032e5467859SAl Viro ret = security_mmap_addr(addr); 1033e5467859SAl Viro if (ret < 0) 1034e5467859SAl Viro return ret; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds /* looks okay */ 10371da177e4SLinus Torvalds *_capabilities = capabilities; 10381da177e4SLinus Torvalds return 0; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds /* 10421da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we 10431da177e4SLinus Torvalds * now know into VMA flags 10441da177e4SLinus Torvalds */ 10451da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file, 10461da177e4SLinus Torvalds unsigned long prot, 10471da177e4SLinus Torvalds unsigned long flags, 10481da177e4SLinus Torvalds unsigned long capabilities) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds unsigned long vm_flags; 10511da177e4SLinus Torvalds 1052e6bfb709SDave Hansen vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); 10531da177e4SLinus Torvalds /* vm_flags |= mm->def_flags; */ 10541da177e4SLinus Torvalds 1055b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) { 10561da177e4SLinus Torvalds /* attempt to share read-only copies of mapped file chunks */ 10573c7b2045SBernd Schmidt vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 10581da177e4SLinus Torvalds if (file && !(prot & PROT_WRITE)) 10591da177e4SLinus Torvalds vm_flags |= VM_MAYSHARE; 10603c7b2045SBernd Schmidt } else { 10611da177e4SLinus Torvalds /* overlay a shareable mapping on the backing device or inode 10621da177e4SLinus Torvalds * if possible - used for chardevs, ramfs/tmpfs/shmfs and 10631da177e4SLinus Torvalds * romfs/cramfs */ 1064b4caecd4SChristoph Hellwig vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS); 10651da177e4SLinus Torvalds if (flags & MAP_SHARED) 10663c7b2045SBernd Schmidt vm_flags |= VM_SHARED; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds /* refuse to let anyone share private mappings with this process if 10701da177e4SLinus Torvalds * it's being traced - otherwise breakpoints set in it may interfere 10711da177e4SLinus Torvalds * with another untraced process 10721da177e4SLinus Torvalds */ 1073a288eeccSTejun Heo if ((flags & MAP_PRIVATE) && current->ptrace) 10741da177e4SLinus Torvalds vm_flags &= ~VM_MAYSHARE; 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds return vm_flags; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds /* 10808feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and 10818feae131SDavid Howells * pins the storage) 10821da177e4SLinus Torvalds */ 10838feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds int ret; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1088dd8632a1SPaul Mundt if (ret == 0) { 1089dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 1090645d83c5SDavid Howells return 0; 1091dd8632a1SPaul Mundt } 10921da177e4SLinus Torvalds if (ret != -ENOSYS) 10931da177e4SLinus Torvalds return ret; 10941da177e4SLinus Torvalds 10953fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as 10963fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as 10973fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */ 10981da177e4SLinus Torvalds return -ENODEV; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds /* 11021da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping 11031da177e4SLinus Torvalds */ 11048feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma, 11058feae131SDavid Howells struct vm_region *region, 1106645d83c5SDavid Howells unsigned long len, 1107645d83c5SDavid Howells unsigned long capabilities) 11081da177e4SLinus Torvalds { 1109dbc8358cSJoonsoo Kim unsigned long total, point; 11101da177e4SLinus Torvalds void *base; 11118feae131SDavid Howells int ret, order; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* invoke the file's mapping function so that it can keep track of 11141da177e4SLinus Torvalds * shared mappings on devices or memory 11151da177e4SLinus Torvalds * - VM_MAYSHARE will be set if it may attempt to share 11161da177e4SLinus Torvalds */ 1117b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 11181da177e4SLinus Torvalds ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1119dd8632a1SPaul Mundt if (ret == 0) { 11201da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */ 1121dd8632a1SPaul Mundt BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); 1122dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end; 1123645d83c5SDavid Howells return 0; 11241da177e4SLinus Torvalds } 1125dd8632a1SPaul Mundt if (ret != -ENOSYS) 1126dd8632a1SPaul Mundt return ret; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't 11291da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to 11301da177e4SLinus Torvalds * make a private copy of the data and map that instead */ 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11338feae131SDavid Howells 11341da177e4SLinus Torvalds /* allocate some memory to hold the mapping 11351da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object 11361da177e4SLinus Torvalds * we're allocating is smaller than a page 11371da177e4SLinus Torvalds */ 1138f67d9b15SBob Liu order = get_order(len); 11398feae131SDavid Howells total = 1 << order; 1140f67d9b15SBob Liu point = len >> PAGE_SHIFT; 1141dd8632a1SPaul Mundt 1142dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */ 114322cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) 1144dbc8358cSJoonsoo Kim total = point; 11458feae131SDavid Howells 1146da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); 1147dbc8358cSJoonsoo Kim if (!base) 1148dbc8358cSJoonsoo Kim goto enomem; 11498feae131SDavid Howells 1150dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated); 1151dbc8358cSJoonsoo Kim 11528feae131SDavid Howells region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; 11538feae131SDavid Howells region->vm_start = (unsigned long) base; 1154f67d9b15SBob Liu region->vm_end = region->vm_start + len; 1155dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT); 11568feae131SDavid Howells 11578feae131SDavid Howells vma->vm_start = region->vm_start; 11588feae131SDavid Howells vma->vm_end = region->vm_start + len; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (vma->vm_file) { 11611da177e4SLinus Torvalds /* read the contents of a file into the copy */ 11621da177e4SLinus Torvalds mm_segment_t old_fs; 11631da177e4SLinus Torvalds loff_t fpos; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds fpos = vma->vm_pgoff; 11661da177e4SLinus Torvalds fpos <<= PAGE_SHIFT; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds old_fs = get_fs(); 11691da177e4SLinus Torvalds set_fs(KERNEL_DS); 11706e242a1cSAl Viro ret = __vfs_read(vma->vm_file, base, len, &fpos); 11711da177e4SLinus Torvalds set_fs(old_fs); 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds if (ret < 0) 11741da177e4SLinus Torvalds goto error_free; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* clear the last little bit */ 1177f67d9b15SBob Liu if (ret < len) 1178f67d9b15SBob Liu memset(base + ret, 0, len - ret); 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds return 0; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds error_free: 11857223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top); 11868feae131SDavid Howells region->vm_start = vma->vm_start = 0; 11878feae131SDavid Howells region->vm_end = vma->vm_end = 0; 1188dd8632a1SPaul Mundt region->vm_top = 0; 11891da177e4SLinus Torvalds return ret; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds enomem: 1192b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n", 119305ae6fa3SGreg Ungerer len, current->pid, current->comm); 11949af744d7SMichal Hocko show_free_areas(0, NULL); 11951da177e4SLinus Torvalds return -ENOMEM; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds /* 11991da177e4SLinus Torvalds * handle mapping creation for uClinux 12001da177e4SLinus Torvalds */ 12011fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file, 12021da177e4SLinus Torvalds unsigned long addr, 12031da177e4SLinus Torvalds unsigned long len, 12041da177e4SLinus Torvalds unsigned long prot, 12051da177e4SLinus Torvalds unsigned long flags, 12061fcfd8dbSOleg Nesterov vm_flags_t vm_flags, 1207bebeb3d6SMichel Lespinasse unsigned long pgoff, 120841badc15SMichel Lespinasse unsigned long *populate) 12091da177e4SLinus Torvalds { 12108feae131SDavid Howells struct vm_area_struct *vma; 12118feae131SDavid Howells struct vm_region *region; 12121da177e4SLinus Torvalds struct rb_node *rb; 12131fcfd8dbSOleg Nesterov unsigned long capabilities, result; 12141da177e4SLinus Torvalds int ret; 12151da177e4SLinus Torvalds 121641badc15SMichel Lespinasse *populate = 0; 1217bebeb3d6SMichel Lespinasse 12181da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of 12191da177e4SLinus Torvalds * mapping */ 12201da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 12211da177e4SLinus Torvalds &capabilities); 122222cc877bSLeon Romanovsky if (ret < 0) 12231da177e4SLinus Torvalds return ret; 12241da177e4SLinus Torvalds 122506aab5a3SDavid Howells /* we ignore the address hint */ 122606aab5a3SDavid Howells addr = 0; 1227f67d9b15SBob Liu len = PAGE_ALIGN(len); 122806aab5a3SDavid Howells 12291da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we 12301da177e4SLinus Torvalds * now know into VMA flags */ 12311fcfd8dbSOleg Nesterov vm_flags |= determine_vm_flags(file, prot, flags, capabilities); 12321da177e4SLinus Torvalds 12338feae131SDavid Howells /* we're going to need to record the mapping */ 12348feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 12358feae131SDavid Howells if (!region) 12368feae131SDavid Howells goto error_getting_region; 12371da177e4SLinus Torvalds 12388feae131SDavid Howells vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 12398feae131SDavid Howells if (!vma) 12408feae131SDavid Howells goto error_getting_vma; 12411da177e4SLinus Torvalds 12421e2ae599SDavid Howells region->vm_usage = 1; 12438feae131SDavid Howells region->vm_flags = vm_flags; 12448feae131SDavid Howells region->vm_pgoff = pgoff; 12458feae131SDavid Howells 12465beb4930SRik van Riel INIT_LIST_HEAD(&vma->anon_vma_chain); 12478feae131SDavid Howells vma->vm_flags = vm_flags; 12488feae131SDavid Howells vma->vm_pgoff = pgoff; 12498feae131SDavid Howells 12508feae131SDavid Howells if (file) { 1251cb0942b8SAl Viro region->vm_file = get_file(file); 1252cb0942b8SAl Viro vma->vm_file = get_file(file); 12538feae131SDavid Howells } 12548feae131SDavid Howells 12558feae131SDavid Howells down_write(&nommu_region_sem); 12568feae131SDavid Howells 12578feae131SDavid Howells /* if we want to share, we need to check for regions created by other 12581da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping 12598feae131SDavid Howells * - we can only share with a superset match on most regular files 12601da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are 12611da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in 12621da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather 12631da177e4SLinus Torvalds * than here 12641da177e4SLinus Torvalds */ 12651da177e4SLinus Torvalds if (vm_flags & VM_MAYSHARE) { 12668feae131SDavid Howells struct vm_region *pregion; 12678feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start; 12681da177e4SLinus Torvalds 12698feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 12708feae131SDavid Howells pgend = pgoff + pglen; 1271165b2392SDavid Howells 12728feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 12738feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb); 12741da177e4SLinus Torvalds 12758feae131SDavid Howells if (!(pregion->vm_flags & VM_MAYSHARE)) 12761da177e4SLinus Torvalds continue; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds /* search for overlapping mappings on the same file */ 1279496ad9aaSAl Viro if (file_inode(pregion->vm_file) != 1280496ad9aaSAl Viro file_inode(file)) 12811da177e4SLinus Torvalds continue; 12821da177e4SLinus Torvalds 12838feae131SDavid Howells if (pregion->vm_pgoff >= pgend) 12841da177e4SLinus Torvalds continue; 12851da177e4SLinus Torvalds 12868feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start; 12878feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 12888feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen; 12898feae131SDavid Howells if (pgoff >= rpgend) 12901da177e4SLinus Torvalds continue; 12911da177e4SLinus Torvalds 12928feae131SDavid Howells /* handle inexactly overlapping matches between 12938feae131SDavid Howells * mappings */ 12948feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 12958feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 12968feae131SDavid Howells /* new mapping is not a subset of the region */ 1297b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT)) 12981da177e4SLinus Torvalds goto sharing_violation; 12991da177e4SLinus Torvalds continue; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13028feae131SDavid Howells /* we've found a region we can share */ 13031e2ae599SDavid Howells pregion->vm_usage++; 13048feae131SDavid Howells vma->vm_region = pregion; 13058feae131SDavid Howells start = pregion->vm_start; 13068feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 13078feae131SDavid Howells vma->vm_start = start; 13088feae131SDavid Howells vma->vm_end = start + len; 13091da177e4SLinus Torvalds 131022cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY) 13118feae131SDavid Howells vma->vm_flags |= VM_MAPPED_COPY; 131222cc877bSLeon Romanovsky else { 13138feae131SDavid Howells ret = do_mmap_shared_file(vma); 13148feae131SDavid Howells if (ret < 0) { 13158feae131SDavid Howells vma->vm_region = NULL; 13168feae131SDavid Howells vma->vm_start = 0; 13178feae131SDavid Howells vma->vm_end = 0; 13181e2ae599SDavid Howells pregion->vm_usage--; 13198feae131SDavid Howells pregion = NULL; 13208feae131SDavid Howells goto error_just_free; 13211da177e4SLinus Torvalds } 13228feae131SDavid Howells } 13238feae131SDavid Howells fput(region->vm_file); 13248feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 13258feae131SDavid Howells region = pregion; 13268feae131SDavid Howells result = start; 13278feae131SDavid Howells goto share; 13288feae131SDavid Howells } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping 13311da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to 13321da177e4SLinus Torvalds * tell us the location of a shared mapping 13331da177e4SLinus Torvalds */ 1334b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) { 13351da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len, 13361da177e4SLinus Torvalds pgoff, flags); 1337bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) { 13381da177e4SLinus Torvalds ret = addr; 1339bb005a59SNamhyung Kim if (ret != -ENOSYS) 13408feae131SDavid Howells goto error_just_free; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds /* the driver refused to tell us where to site 13431da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy 13441da177e4SLinus Torvalds * it */ 1345bb005a59SNamhyung Kim ret = -ENODEV; 1346b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY)) 13478feae131SDavid Howells goto error_just_free; 13481da177e4SLinus Torvalds 1349b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT; 13508feae131SDavid Howells } else { 13518feae131SDavid Howells vma->vm_start = region->vm_start = addr; 13528feae131SDavid Howells vma->vm_end = region->vm_end = addr + len; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13578feae131SDavid Howells vma->vm_region = region; 13581da177e4SLinus Torvalds 1359645d83c5SDavid Howells /* set up the mapping 1360b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set 1361645d83c5SDavid Howells */ 13621da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED) 13638feae131SDavid Howells ret = do_mmap_shared_file(vma); 13641da177e4SLinus Torvalds else 1365645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities); 13661da177e4SLinus Torvalds if (ret < 0) 1367645d83c5SDavid Howells goto error_just_free; 1368645d83c5SDavid Howells add_nommu_region(region); 13698feae131SDavid Howells 1370ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */ 1371ea637639SJie Zhang if (!vma->vm_file && !(flags & MAP_UNINITIALIZED)) 1372ea637639SJie Zhang memset((void *)region->vm_start, 0, 1373ea637639SJie Zhang region->vm_end - region->vm_start); 1374ea637639SJie Zhang 13751da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */ 13768feae131SDavid Howells result = vma->vm_start; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT; 13791da177e4SLinus Torvalds 13808feae131SDavid Howells share: 13818feae131SDavid Howells add_vma_to_mm(current->mm, vma); 13821da177e4SLinus Torvalds 1383cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable 1384cfe79c00SMike Frysinger * mapping of it is made */ 1385cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1386cfe79c00SMike Frysinger flush_icache_range(region->vm_start, region->vm_end); 1387cfe79c00SMike Frysinger region->vm_icache_flushed = true; 1388cfe79c00SMike Frysinger } 13891da177e4SLinus Torvalds 1390cfe79c00SMike Frysinger up_write(&nommu_region_sem); 13911da177e4SLinus Torvalds 13928feae131SDavid Howells return result; 13931da177e4SLinus Torvalds 13948feae131SDavid Howells error_just_free: 13958feae131SDavid Howells up_write(&nommu_region_sem); 13968feae131SDavid Howells error: 139789a86402SDavid Howells if (region->vm_file) 13988feae131SDavid Howells fput(region->vm_file); 13998feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 140089a86402SDavid Howells if (vma->vm_file) 14018feae131SDavid Howells fput(vma->vm_file); 14028feae131SDavid Howells kmem_cache_free(vm_area_cachep, vma); 14031da177e4SLinus Torvalds return ret; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds sharing_violation: 14068feae131SDavid Howells up_write(&nommu_region_sem); 140722cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n"); 14088feae131SDavid Howells ret = -EINVAL; 14098feae131SDavid Howells goto error; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds error_getting_vma: 14128feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 141322cc877bSLeon Romanovsky pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", 14141da177e4SLinus Torvalds len, current->pid); 14159af744d7SMichal Hocko show_free_areas(0, NULL); 14161da177e4SLinus Torvalds return -ENOMEM; 14171da177e4SLinus Torvalds 14188feae131SDavid Howells error_getting_region: 141922cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", 14201da177e4SLinus Torvalds len, current->pid); 14219af744d7SMichal Hocko show_free_areas(0, NULL); 14221da177e4SLinus Torvalds return -ENOMEM; 14231da177e4SLinus Torvalds } 14246be5ceb0SLinus Torvalds 142566f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 142666f0dc48SHugh Dickins unsigned long, prot, unsigned long, flags, 142766f0dc48SHugh Dickins unsigned long, fd, unsigned long, pgoff) 142866f0dc48SHugh Dickins { 142966f0dc48SHugh Dickins struct file *file = NULL; 143066f0dc48SHugh Dickins unsigned long retval = -EBADF; 143166f0dc48SHugh Dickins 1432120a795dSAl Viro audit_mmap_fd(fd, flags); 143366f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) { 143466f0dc48SHugh Dickins file = fget(fd); 143566f0dc48SHugh Dickins if (!file) 143666f0dc48SHugh Dickins goto out; 143766f0dc48SHugh Dickins } 143866f0dc48SHugh Dickins 143966f0dc48SHugh Dickins flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 144066f0dc48SHugh Dickins 1441ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); 144266f0dc48SHugh Dickins 144366f0dc48SHugh Dickins if (file) 144466f0dc48SHugh Dickins fput(file); 144566f0dc48SHugh Dickins out: 144666f0dc48SHugh Dickins return retval; 144766f0dc48SHugh Dickins } 144866f0dc48SHugh Dickins 1449a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP 1450a4679373SChristoph Hellwig struct mmap_arg_struct { 1451a4679373SChristoph Hellwig unsigned long addr; 1452a4679373SChristoph Hellwig unsigned long len; 1453a4679373SChristoph Hellwig unsigned long prot; 1454a4679373SChristoph Hellwig unsigned long flags; 1455a4679373SChristoph Hellwig unsigned long fd; 1456a4679373SChristoph Hellwig unsigned long offset; 1457a4679373SChristoph Hellwig }; 1458a4679373SChristoph Hellwig 1459a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1460a4679373SChristoph Hellwig { 1461a4679373SChristoph Hellwig struct mmap_arg_struct a; 1462a4679373SChristoph Hellwig 1463a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a))) 1464a4679373SChristoph Hellwig return -EFAULT; 14651824cb75SAlexander Kuleshov if (offset_in_page(a.offset)) 1466a4679373SChristoph Hellwig return -EINVAL; 1467a4679373SChristoph Hellwig 1468a4679373SChristoph Hellwig return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1469a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT); 1470a4679373SChristoph Hellwig } 1471a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1472a4679373SChristoph Hellwig 14731da177e4SLinus Torvalds /* 14748feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either 14758feae131SDavid Howells * for the first part or the tail. 14761da177e4SLinus Torvalds */ 14778feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 14788feae131SDavid Howells unsigned long addr, int new_below) 14791da177e4SLinus Torvalds { 14808feae131SDavid Howells struct vm_area_struct *new; 14818feae131SDavid Howells struct vm_region *region; 14828feae131SDavid Howells unsigned long npages; 14831da177e4SLinus Torvalds 1484779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have 1485779c1023SDavid Howells * only a single usage on the region) */ 1486779c1023SDavid Howells if (vma->vm_file) 14878feae131SDavid Howells return -ENOMEM; 14881da177e4SLinus Torvalds 14898feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count) 14908feae131SDavid Howells return -ENOMEM; 14911da177e4SLinus Torvalds 14928feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 14938feae131SDavid Howells if (!region) 14948feae131SDavid Howells return -ENOMEM; 14958feae131SDavid Howells 14968feae131SDavid Howells new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 14978feae131SDavid Howells if (!new) { 14988feae131SDavid Howells kmem_cache_free(vm_region_jar, region); 14998feae131SDavid Howells return -ENOMEM; 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 15028feae131SDavid Howells /* most fields are the same, copy all, and then fixup */ 15038feae131SDavid Howells *new = *vma; 15048feae131SDavid Howells *region = *vma->vm_region; 15058feae131SDavid Howells new->vm_region = region; 15068feae131SDavid Howells 15078feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT; 15088feae131SDavid Howells 15098feae131SDavid Howells if (new_below) { 1510dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr; 15118feae131SDavid Howells } else { 15128feae131SDavid Howells region->vm_start = new->vm_start = addr; 15138feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages; 15141da177e4SLinus Torvalds } 15158feae131SDavid Howells 15168feae131SDavid Howells if (new->vm_ops && new->vm_ops->open) 15178feae131SDavid Howells new->vm_ops->open(new); 15188feae131SDavid Howells 15198feae131SDavid Howells delete_vma_from_mm(vma); 15208feae131SDavid Howells down_write(&nommu_region_sem); 15218feae131SDavid Howells delete_nommu_region(vma->vm_region); 15228feae131SDavid Howells if (new_below) { 15238feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr; 15248feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 15258feae131SDavid Howells } else { 15268feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr; 1527dd8632a1SPaul Mundt vma->vm_region->vm_top = addr; 15288feae131SDavid Howells } 15298feae131SDavid Howells add_nommu_region(vma->vm_region); 15308feae131SDavid Howells add_nommu_region(new->vm_region); 15318feae131SDavid Howells up_write(&nommu_region_sem); 15328feae131SDavid Howells add_vma_to_mm(mm, vma); 15338feae131SDavid Howells add_vma_to_mm(mm, new); 15348feae131SDavid Howells return 0; 15358feae131SDavid Howells } 15368feae131SDavid Howells 15378feae131SDavid Howells /* 15388feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or 15398feae131SDavid Howells * the end 15408feae131SDavid Howells */ 15418feae131SDavid Howells static int shrink_vma(struct mm_struct *mm, 15428feae131SDavid Howells struct vm_area_struct *vma, 15438feae131SDavid Howells unsigned long from, unsigned long to) 15448feae131SDavid Howells { 15458feae131SDavid Howells struct vm_region *region; 15468feae131SDavid Howells 15478feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree 15488feae131SDavid Howells * and list */ 15498feae131SDavid Howells delete_vma_from_mm(vma); 15508feae131SDavid Howells if (from > vma->vm_start) 15518feae131SDavid Howells vma->vm_end = from; 15528feae131SDavid Howells else 15538feae131SDavid Howells vma->vm_start = to; 15548feae131SDavid Howells add_vma_to_mm(mm, vma); 15558feae131SDavid Howells 15568feae131SDavid Howells /* cut the backing region down to size */ 15578feae131SDavid Howells region = vma->vm_region; 15581e2ae599SDavid Howells BUG_ON(region->vm_usage != 1); 15598feae131SDavid Howells 15608feae131SDavid Howells down_write(&nommu_region_sem); 15618feae131SDavid Howells delete_nommu_region(region); 1562dd8632a1SPaul Mundt if (from > region->vm_start) { 1563dd8632a1SPaul Mundt to = region->vm_top; 1564dd8632a1SPaul Mundt region->vm_top = region->vm_end = from; 1565dd8632a1SPaul Mundt } else { 15668feae131SDavid Howells region->vm_start = to; 1567dd8632a1SPaul Mundt } 15688feae131SDavid Howells add_nommu_region(region); 15698feae131SDavid Howells up_write(&nommu_region_sem); 15708feae131SDavid Howells 15718feae131SDavid Howells free_page_series(from, to); 15728feae131SDavid Howells return 0; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds 15753034097aSDavid Howells /* 15763034097aSDavid Howells * release a mapping 15778feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single 15788feae131SDavid Howells * VMA, though it need not cover the whole VMA 15793034097aSDavid Howells */ 15808feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 15811da177e4SLinus Torvalds { 15828feae131SDavid Howells struct vm_area_struct *vma; 1583f67d9b15SBob Liu unsigned long end; 15848feae131SDavid Howells int ret; 15851da177e4SLinus Torvalds 1586f67d9b15SBob Liu len = PAGE_ALIGN(len); 15878feae131SDavid Howells if (len == 0) 15881da177e4SLinus Torvalds return -EINVAL; 15891da177e4SLinus Torvalds 1590f67d9b15SBob Liu end = start + len; 1591f67d9b15SBob Liu 15928feae131SDavid Howells /* find the first potentially overlapping VMA */ 15938feae131SDavid Howells vma = find_vma(mm, start); 15948feae131SDavid Howells if (!vma) { 1595ac714904SChoi Gi-yong static int limit; 159633e5d769SDavid Howells if (limit < 5) { 159722cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", 159833e5d769SDavid Howells current->pid, current->comm, 159933e5d769SDavid Howells start, start + len - 1); 160033e5d769SDavid Howells limit++; 160133e5d769SDavid Howells } 16028feae131SDavid Howells return -EINVAL; 16038feae131SDavid Howells } 16041da177e4SLinus Torvalds 16058feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */ 16068feae131SDavid Howells if (vma->vm_file) { 16078feae131SDavid Howells do { 160822cc877bSLeon Romanovsky if (start > vma->vm_start) 16098feae131SDavid Howells return -EINVAL; 16108feae131SDavid Howells if (end == vma->vm_end) 16118feae131SDavid Howells goto erase_whole_vma; 1612d75a310cSNamhyung Kim vma = vma->vm_next; 1613d75a310cSNamhyung Kim } while (vma); 16148feae131SDavid Howells return -EINVAL; 16158feae131SDavid Howells } else { 16168feae131SDavid Howells /* the chunk must be a subset of the VMA found */ 16178feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end) 16188feae131SDavid Howells goto erase_whole_vma; 161922cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end) 16208feae131SDavid Howells return -EINVAL; 16211824cb75SAlexander Kuleshov if (offset_in_page(start)) 16228feae131SDavid Howells return -EINVAL; 16231824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end)) 16248feae131SDavid Howells return -EINVAL; 16258feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) { 16268feae131SDavid Howells ret = split_vma(mm, vma, start, 1); 162722cc877bSLeon Romanovsky if (ret < 0) 16288feae131SDavid Howells return ret; 16298feae131SDavid Howells } 16308feae131SDavid Howells return shrink_vma(mm, vma, start, end); 16318feae131SDavid Howells } 16321da177e4SLinus Torvalds 16338feae131SDavid Howells erase_whole_vma: 16348feae131SDavid Howells delete_vma_from_mm(vma); 16358feae131SDavid Howells delete_vma(mm, vma); 16361da177e4SLinus Torvalds return 0; 16371da177e4SLinus Torvalds } 1638b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap); 16391da177e4SLinus Torvalds 1640bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len) 16413034097aSDavid Howells { 1642bfce281cSAl Viro struct mm_struct *mm = current->mm; 16433034097aSDavid Howells int ret; 16443034097aSDavid Howells 16453034097aSDavid Howells down_write(&mm->mmap_sem); 16463034097aSDavid Howells ret = do_munmap(mm, addr, len); 16473034097aSDavid Howells up_write(&mm->mmap_sem); 16483034097aSDavid Howells return ret; 16493034097aSDavid Howells } 1650a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap); 1651a46ef99dSLinus Torvalds 1652a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1653a46ef99dSLinus Torvalds { 1654bfce281cSAl Viro return vm_munmap(addr, len); 1655a46ef99dSLinus Torvalds } 16563034097aSDavid Howells 16573034097aSDavid Howells /* 16588feae131SDavid Howells * release all the mappings made in a process's VM space 16593034097aSDavid Howells */ 16601da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm) 16611da177e4SLinus Torvalds { 16628feae131SDavid Howells struct vm_area_struct *vma; 16631da177e4SLinus Torvalds 16648feae131SDavid Howells if (!mm) 16658feae131SDavid Howells return; 16668feae131SDavid Howells 16671da177e4SLinus Torvalds mm->total_vm = 0; 16681da177e4SLinus Torvalds 16698feae131SDavid Howells while ((vma = mm->mmap)) { 16708feae131SDavid Howells mm->mmap = vma->vm_next; 16718feae131SDavid Howells delete_vma_from_mm(vma); 16728feae131SDavid Howells delete_vma(mm, vma); 167304c34961SSteven J. Magnani cond_resched(); 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16775d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len) 16781da177e4SLinus Torvalds { 16791da177e4SLinus Torvalds return -ENOMEM; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* 16836fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same 16846fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 16851da177e4SLinus Torvalds * 16866fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only 16878feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the 16888feae131SDavid Howells * block is not shareable 16891da177e4SLinus Torvalds * 16906fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions 16911da177e4SLinus Torvalds */ 16924b377babSAl Viro static unsigned long do_mremap(unsigned long addr, 16931da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len, 16941da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr) 16951da177e4SLinus Torvalds { 16966fa5f80bSDavid Howells struct vm_area_struct *vma; 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds /* insanity checks first */ 1699f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len); 1700f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len); 17018feae131SDavid Howells if (old_len == 0 || new_len == 0) 17021da177e4SLinus Torvalds return (unsigned long) -EINVAL; 17031da177e4SLinus Torvalds 17041824cb75SAlexander Kuleshov if (offset_in_page(addr)) 17058feae131SDavid Howells return -EINVAL; 17068feae131SDavid Howells 17071da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr) 17081da177e4SLinus Torvalds return (unsigned long) -EINVAL; 17091da177e4SLinus Torvalds 17108feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len); 17116fa5f80bSDavid Howells if (!vma) 17121da177e4SLinus Torvalds return (unsigned long) -EINVAL; 17131da177e4SLinus Torvalds 17146fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len) 17151da177e4SLinus Torvalds return (unsigned long) -EFAULT; 17161da177e4SLinus Torvalds 17176fa5f80bSDavid Howells if (vma->vm_flags & VM_MAYSHARE) 17181da177e4SLinus Torvalds return (unsigned long) -EPERM; 17191da177e4SLinus Torvalds 17208feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 17211da177e4SLinus Torvalds return (unsigned long) -ENOMEM; 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds /* all checks complete - do it */ 17246fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len; 17256fa5f80bSDavid Howells return vma->vm_start; 17266fa5f80bSDavid Howells } 17276fa5f80bSDavid Howells 17286a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 17296a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags, 17306a6160a7SHeiko Carstens unsigned long, new_addr) 17316fa5f80bSDavid Howells { 17326fa5f80bSDavid Howells unsigned long ret; 17336fa5f80bSDavid Howells 17346fa5f80bSDavid Howells down_write(¤t->mm->mmap_sem); 17356fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr); 17366fa5f80bSDavid Howells up_write(¤t->mm->mmap_sem); 17376fa5f80bSDavid Howells return ret; 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 1740240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma, 1741240aadeeSMichel Lespinasse unsigned long address, unsigned int flags, 1742240aadeeSMichel Lespinasse unsigned int *page_mask) 17431da177e4SLinus Torvalds { 1744240aadeeSMichel Lespinasse *page_mask = 0; 17451da177e4SLinus Torvalds return NULL; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17488f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, 17498f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot) 17501da177e4SLinus Torvalds { 17518f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT)) 17528f3b1327SBob Liu return -EINVAL; 17538f3b1327SBob Liu 1754314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 175566aa2b4bSGreg Ungerer return 0; 17561da177e4SLinus Torvalds } 175722c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range); 17581da177e4SLinus Torvalds 17593c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) 17603c0b9de6SLinus Torvalds { 17613c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT; 17623c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start; 17633c0b9de6SLinus Torvalds 17643c0b9de6SLinus Torvalds pfn += vma->vm_pgoff; 17653c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); 17663c0b9de6SLinus Torvalds } 17673c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory); 17683c0b9de6SLinus Torvalds 1769f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1770f905bc44SPaul Mundt unsigned long pgoff) 1771f905bc44SPaul Mundt { 1772f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start; 1773f905bc44SPaul Mundt 1774f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP)) 1775f905bc44SPaul Mundt return -EINVAL; 1776f905bc44SPaul Mundt 1777f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1778f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size; 1779f905bc44SPaul Mundt 1780f905bc44SPaul Mundt return 0; 1781f905bc44SPaul Mundt } 1782f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range); 1783f905bc44SPaul Mundt 17841da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr, 17851da177e4SLinus Torvalds unsigned long len, unsigned long pgoff, unsigned long flags) 17861da177e4SLinus Torvalds { 17871da177e4SLinus Torvalds return -ENOMEM; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping, 17911da177e4SLinus Torvalds loff_t const holebegin, loff_t const holelen, 17921da177e4SLinus Torvalds int even_cows) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds } 179522c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range); 17961da177e4SLinus Torvalds 179711bac800SDave Jiang int filemap_fault(struct vm_fault *vmf) 1798b0e15190SDavid Howells { 1799b0e15190SDavid Howells BUG(); 1800d0217ac0SNick Piggin return 0; 1801b0e15190SDavid Howells } 1802b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault); 18030ec76a11SDavid Howells 180482b0f8c3SJan Kara void filemap_map_pages(struct vm_fault *vmf, 1805bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff) 1806f1820361SKirill A. Shutemov { 1807f1820361SKirill A. Shutemov BUG(); 1808f1820361SKirill A. Shutemov } 1809f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages); 1810f1820361SKirill A. Shutemov 181184d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm, 1812442486ecSLorenzo Stoakes unsigned long addr, void *buf, int len, unsigned int gup_flags) 18130ec76a11SDavid Howells { 18140ec76a11SDavid Howells struct vm_area_struct *vma; 1815442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE; 18160ec76a11SDavid Howells 18170ec76a11SDavid Howells down_read(&mm->mmap_sem); 18180ec76a11SDavid Howells 18190ec76a11SDavid Howells /* the access must start within one of the target process's mappings */ 18200159b141SDavid Howells vma = find_vma(mm, addr); 18210159b141SDavid Howells if (vma) { 18220ec76a11SDavid Howells /* don't overrun this mapping */ 18230ec76a11SDavid Howells if (addr + len >= vma->vm_end) 18240ec76a11SDavid Howells len = vma->vm_end - addr; 18250ec76a11SDavid Howells 18260ec76a11SDavid Howells /* only read or write mappings where it is permitted */ 1827d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE) 18287959722bSJie Zhang copy_to_user_page(vma, NULL, addr, 18297959722bSJie Zhang (void *) addr, buf, len); 1830d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD) 18317959722bSJie Zhang copy_from_user_page(vma, NULL, addr, 18327959722bSJie Zhang buf, (void *) addr, len); 18330ec76a11SDavid Howells else 18340ec76a11SDavid Howells len = 0; 18350ec76a11SDavid Howells } else { 18360ec76a11SDavid Howells len = 0; 18370ec76a11SDavid Howells } 18380ec76a11SDavid Howells 18390ec76a11SDavid Howells up_read(&mm->mmap_sem); 1840f55f199bSMike Frysinger 1841f55f199bSMike Frysinger return len; 1842f55f199bSMike Frysinger } 1843f55f199bSMike Frysinger 1844f55f199bSMike Frysinger /** 1845f55f199bSMike Frysinger * @access_remote_vm - access another process' address space 1846f55f199bSMike Frysinger * @mm: the mm_struct of the target address space 1847f55f199bSMike Frysinger * @addr: start address to access 1848f55f199bSMike Frysinger * @buf: source or destination buffer 1849f55f199bSMike Frysinger * @len: number of bytes to transfer 18506347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour 1851f55f199bSMike Frysinger * 1852f55f199bSMike Frysinger * The caller must hold a reference on @mm. 1853f55f199bSMike Frysinger */ 1854f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr, 18556347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags) 1856f55f199bSMike Frysinger { 18576347e8d5SLorenzo Stoakes return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags); 1858f55f199bSMike Frysinger } 1859f55f199bSMike Frysinger 1860f55f199bSMike Frysinger /* 1861f55f199bSMike Frysinger * Access another process' address space. 1862f55f199bSMike Frysinger * - source/target buffer must be kernel space 1863f55f199bSMike Frysinger */ 1864f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, 1865f307ab6dSLorenzo Stoakes unsigned int gup_flags) 1866f55f199bSMike Frysinger { 1867f55f199bSMike Frysinger struct mm_struct *mm; 1868f55f199bSMike Frysinger 1869f55f199bSMike Frysinger if (addr + len < addr) 1870f55f199bSMike Frysinger return 0; 1871f55f199bSMike Frysinger 1872f55f199bSMike Frysinger mm = get_task_mm(tsk); 1873f55f199bSMike Frysinger if (!mm) 1874f55f199bSMike Frysinger return 0; 1875f55f199bSMike Frysinger 1876f307ab6dSLorenzo Stoakes len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 1877f55f199bSMike Frysinger 18780ec76a11SDavid Howells mmput(mm); 18790ec76a11SDavid Howells return len; 18800ec76a11SDavid Howells } 1881fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm); 18827e660872SDavid Howells 18837e660872SDavid Howells /** 18847e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 18857e660872SDavid Howells * @inode: The inode to check 18867e660872SDavid Howells * @size: The current filesize of the inode 18877e660872SDavid Howells * @newsize: The proposed filesize of the inode 18887e660872SDavid Howells * 18897e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to 18907e660872SDavid Howells * make sure that that any outstanding VMAs aren't broken and then shrink the 18917e660872SDavid Howells * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't 18927e660872SDavid Howells * automatically grant mappings that are too large. 18937e660872SDavid Howells */ 18947e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 18957e660872SDavid Howells size_t newsize) 18967e660872SDavid Howells { 18977e660872SDavid Howells struct vm_area_struct *vma; 18987e660872SDavid Howells struct vm_region *region; 18997e660872SDavid Howells pgoff_t low, high; 19007e660872SDavid Howells size_t r_size, r_top; 19017e660872SDavid Howells 19027e660872SDavid Howells low = newsize >> PAGE_SHIFT; 19037e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 19047e660872SDavid Howells 19057e660872SDavid Howells down_write(&nommu_region_sem); 19061acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping); 19077e660872SDavid Howells 19087e660872SDavid Howells /* search for VMAs that fall within the dead zone */ 19096b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { 19107e660872SDavid Howells /* found one - only interested if it's shared out of the page 19117e660872SDavid Howells * cache */ 19127e660872SDavid Howells if (vma->vm_flags & VM_SHARED) { 19131acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 19147e660872SDavid Howells up_write(&nommu_region_sem); 19157e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */ 19167e660872SDavid Howells } 19177e660872SDavid Howells } 19187e660872SDavid Howells 19197e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence, 19207e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone 19217e660872SDavid Howells * 19227e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there 19237e660872SDavid Howells * shouldn't be any 19247e660872SDavid Howells */ 19251acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { 19267e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED)) 19277e660872SDavid Howells continue; 19287e660872SDavid Howells 19297e660872SDavid Howells region = vma->vm_region; 19307e660872SDavid Howells r_size = region->vm_top - region->vm_start; 19317e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 19327e660872SDavid Howells 19337e660872SDavid Howells if (r_top > newsize) { 19347e660872SDavid Howells region->vm_top -= r_top - newsize; 19357e660872SDavid Howells if (region->vm_end > region->vm_top) 19367e660872SDavid Howells region->vm_end = region->vm_top; 19377e660872SDavid Howells } 19387e660872SDavid Howells } 19397e660872SDavid Howells 19401acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping); 19417e660872SDavid Howells up_write(&nommu_region_sem); 19427e660872SDavid Howells return 0; 19437e660872SDavid Howells } 1944c9b1d098SAndrew Shewmaker 1945c9b1d098SAndrew Shewmaker /* 1946c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes. 1947c9b1d098SAndrew Shewmaker * 1948c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging 1949c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER 1950c9b1d098SAndrew Shewmaker * mode. 1951c9b1d098SAndrew Shewmaker * 1952c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB) 1953c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill. 1954c9b1d098SAndrew Shewmaker */ 1955c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void) 1956c9b1d098SAndrew Shewmaker { 1957c9b1d098SAndrew Shewmaker unsigned long free_kbytes; 1958c9b1d098SAndrew Shewmaker 1959c9b1d098SAndrew Shewmaker free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 1960c9b1d098SAndrew Shewmaker 1961c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); 1962c9b1d098SAndrew Shewmaker return 0; 1963c9b1d098SAndrew Shewmaker } 1964a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve); 19654eeab4f5SAndrew Shewmaker 19664eeab4f5SAndrew Shewmaker /* 19674eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes. 19684eeab4f5SAndrew Shewmaker * 19694eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin 19704eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process. 19714eeab4f5SAndrew Shewmaker * 19724eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover 19734eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will 19744eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default. 19754eeab4f5SAndrew Shewmaker */ 19764eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void) 19774eeab4f5SAndrew Shewmaker { 19784eeab4f5SAndrew Shewmaker unsigned long free_kbytes; 19794eeab4f5SAndrew Shewmaker 19804eeab4f5SAndrew Shewmaker free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); 19814eeab4f5SAndrew Shewmaker 19824eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); 19834eeab4f5SAndrew Shewmaker return 0; 19844eeab4f5SAndrew Shewmaker } 1985a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve); 1986