Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/nommu.c |
| 3 | * |
| 4 | * Replacement code for mm functions to support CPU's that don't |
| 5 | * have any form of memory management unit (thus no virtual memory). |
| 6 | * |
| 7 | * See Documentation/nommu-mmap.txt |
| 8 | * |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 9 | * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com> |
| 11 | * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org> |
| 12 | * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com> |
Paul Mundt | eb6434d | 2009-01-21 17:45:47 +0900 | [diff] [blame] | 13 | * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
David Howells | f2b8544 | 2007-10-29 13:15:39 +0000 | [diff] [blame] | 16 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/mman.h> |
| 19 | #include <linux/swap.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/highmem.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/vmalloc.h> |
Roland McGrath | fa8e26c | 2008-07-25 19:45:50 -0700 | [diff] [blame] | 25 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/backing-dev.h> |
| 28 | #include <linux/mount.h> |
| 29 | #include <linux/personality.h> |
| 30 | #include <linux/security.h> |
| 31 | #include <linux/syscalls.h> |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 32 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/uaccess.h> |
| 35 | #include <asm/tlb.h> |
| 36 | #include <asm/tlbflush.h> |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 37 | #include <asm/mmu_context.h> |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 38 | #include "internal.h" |
| 39 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 40 | #if 0 |
| 41 | #define kenter(FMT, ...) \ |
| 42 | printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) |
| 43 | #define kleave(FMT, ...) \ |
| 44 | printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) |
| 45 | #define kdebug(FMT, ...) \ |
| 46 | printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__) |
| 47 | #else |
| 48 | #define kenter(FMT, ...) \ |
| 49 | no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) |
| 50 | #define kleave(FMT, ...) \ |
| 51 | no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) |
| 52 | #define kdebug(FMT, ...) \ |
| 53 | no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__) |
| 54 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | void *high_memory; |
| 57 | struct page *mem_map; |
| 58 | unsigned long max_mapnr; |
| 59 | unsigned long num_physpages; |
Hugh Dickins | 4266c97 | 2009-09-23 17:05:53 +0100 | [diff] [blame] | 60 | unsigned long highest_memmap_pfn; |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 61 | struct percpu_counter vm_committed_as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */ |
| 63 | int sysctl_overcommit_ratio = 50; /* default is 50% */ |
| 64 | int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; |
David Howells | fc4d5c2 | 2009-05-06 16:03:05 -0700 | [diff] [blame] | 65 | int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | int heap_stack_gap = 0; |
| 67 | |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 68 | atomic_long_t mmap_pages_allocated; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | EXPORT_SYMBOL(mem_map); |
Wu, Bryan | 6a04de6 | 2007-04-11 23:28:47 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL(num_physpages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 73 | /* list of mapped, potentially shareable regions */ |
| 74 | static struct kmem_cache *vm_region_jar; |
| 75 | struct rb_root nommu_region_tree = RB_ROOT; |
| 76 | DECLARE_RWSEM(nommu_region_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 78 | const struct vm_operations_struct generic_file_vm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * Return the total memory allocated for this pointer, not |
| 83 | * just what the caller asked for. |
| 84 | * |
| 85 | * Doesn't have to be accurate, i.e. may have races. |
| 86 | */ |
| 87 | unsigned int kobjsize(const void *objp) |
| 88 | { |
| 89 | struct page *page; |
| 90 | |
Michael Hennerich | 4016a13 | 2008-04-28 02:13:38 -0700 | [diff] [blame] | 91 | /* |
| 92 | * If the object we have should not have ksize performed on it, |
| 93 | * return size of 0 |
| 94 | */ |
Paul Mundt | 5a1603b | 2008-06-12 16:29:55 +0900 | [diff] [blame] | 95 | if (!objp || !virt_addr_valid(objp)) |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | |
| 98 | page = virt_to_head_page(objp); |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * If the allocator sets PageSlab, we know the pointer came from |
| 102 | * kmalloc(). |
| 103 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (PageSlab(page)) |
| 105 | return ksize(objp); |
| 106 | |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 107 | /* |
Paul Mundt | ab2e83e | 2009-01-08 12:04:48 +0000 | [diff] [blame] | 108 | * If it's not a compound page, see if we have a matching VMA |
| 109 | * region. This test is intentionally done in reverse order, |
| 110 | * so if there's no VMA, we still fall through and hand back |
| 111 | * PAGE_SIZE for 0-order pages. |
| 112 | */ |
| 113 | if (!PageCompound(page)) { |
| 114 | struct vm_area_struct *vma; |
| 115 | |
| 116 | vma = find_vma(current->mm, (unsigned long)objp); |
| 117 | if (vma) |
| 118 | return vma->vm_end - vma->vm_start; |
| 119 | } |
| 120 | |
| 121 | /* |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 122 | * The ksize() function is only guaranteed to work for pointers |
Paul Mundt | 5a1603b | 2008-06-12 16:29:55 +0900 | [diff] [blame] | 123 | * returned by kmalloc(). So handle arbitrary pointers here. |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 124 | */ |
Paul Mundt | 5a1603b | 2008-06-12 16:29:55 +0900 | [diff] [blame] | 125 | return PAGE_SIZE << compound_order(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 128 | int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
Hugh Dickins | 4266c97 | 2009-09-23 17:05:53 +0100 | [diff] [blame] | 129 | unsigned long start, int nr_pages, unsigned int foll_flags, |
Peter Zijlstra | 9d73777 | 2009-06-25 11:58:55 +0200 | [diff] [blame] | 130 | struct page **pages, struct vm_area_struct **vmas) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Sonic Zhang | 910e46d | 2006-09-27 01:50:17 -0700 | [diff] [blame] | 132 | struct vm_area_struct *vma; |
David Howells | 7b4d5b8 | 2006-09-27 01:50:18 -0700 | [diff] [blame] | 133 | unsigned long vm_flags; |
| 134 | int i; |
| 135 | |
| 136 | /* calculate required read or write permissions. |
Hugh Dickins | 58fa879 | 2009-09-21 17:03:31 -0700 | [diff] [blame] | 137 | * If FOLL_FORCE is set, we only require the "MAY" flags. |
David Howells | 7b4d5b8 | 2006-09-27 01:50:18 -0700 | [diff] [blame] | 138 | */ |
Hugh Dickins | 58fa879 | 2009-09-21 17:03:31 -0700 | [diff] [blame] | 139 | vm_flags = (foll_flags & FOLL_WRITE) ? |
| 140 | (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); |
| 141 | vm_flags &= (foll_flags & FOLL_FORCE) ? |
| 142 | (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Peter Zijlstra | 9d73777 | 2009-06-25 11:58:55 +0200 | [diff] [blame] | 144 | for (i = 0; i < nr_pages; i++) { |
David Howells | 7561e8c | 2010-03-25 16:48:38 +0000 | [diff] [blame] | 145 | vma = find_vma(mm, start); |
David Howells | 7b4d5b8 | 2006-09-27 01:50:18 -0700 | [diff] [blame] | 146 | if (!vma) |
| 147 | goto finish_or_fault; |
| 148 | |
| 149 | /* protect what we can, including chardevs */ |
Hugh Dickins | 1c3aff1 | 2009-09-21 17:03:24 -0700 | [diff] [blame] | 150 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || |
| 151 | !(vm_flags & vma->vm_flags)) |
David Howells | 7b4d5b8 | 2006-09-27 01:50:18 -0700 | [diff] [blame] | 152 | goto finish_or_fault; |
Sonic Zhang | 910e46d | 2006-09-27 01:50:17 -0700 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (pages) { |
| 155 | pages[i] = virt_to_page(start); |
| 156 | if (pages[i]) |
| 157 | page_cache_get(pages[i]); |
| 158 | } |
| 159 | if (vmas) |
Sonic Zhang | 910e46d | 2006-09-27 01:50:17 -0700 | [diff] [blame] | 160 | vmas[i] = vma; |
David Howells | e1ee65d | 2010-03-25 16:48:44 +0000 | [diff] [blame] | 161 | start = (start + PAGE_SIZE) & PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
David Howells | 7b4d5b8 | 2006-09-27 01:50:18 -0700 | [diff] [blame] | 163 | |
| 164 | return i; |
| 165 | |
| 166 | finish_or_fault: |
| 167 | return i ? : -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 169 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 170 | /* |
| 171 | * get a list of pages in an address range belonging to the specified process |
| 172 | * and indicate the VMA that covers each page |
| 173 | * - this is potentially dodgy as we may end incrementing the page count of a |
| 174 | * slab page or a secondary page from a compound page |
| 175 | * - don't permit access to VMAs that don't support it, such as I/O mappings |
| 176 | */ |
| 177 | int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
Peter Zijlstra | 9d73777 | 2009-06-25 11:58:55 +0200 | [diff] [blame] | 178 | unsigned long start, int nr_pages, int write, int force, |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 179 | struct page **pages, struct vm_area_struct **vmas) |
| 180 | { |
| 181 | int flags = 0; |
| 182 | |
| 183 | if (write) |
Hugh Dickins | 58fa879 | 2009-09-21 17:03:31 -0700 | [diff] [blame] | 184 | flags |= FOLL_WRITE; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 185 | if (force) |
Hugh Dickins | 58fa879 | 2009-09-21 17:03:31 -0700 | [diff] [blame] | 186 | flags |= FOLL_FORCE; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 187 | |
Peter Zijlstra | 9d73777 | 2009-06-25 11:58:55 +0200 | [diff] [blame] | 188 | return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 189 | } |
Greg Ungerer | 66aa2b4 | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 190 | EXPORT_SYMBOL(get_user_pages); |
| 191 | |
Paul Mundt | dfc2f91 | 2009-06-26 04:31:57 +0900 | [diff] [blame] | 192 | /** |
| 193 | * follow_pfn - look up PFN at a user virtual address |
| 194 | * @vma: memory mapping |
| 195 | * @address: user virtual address |
| 196 | * @pfn: location to store found PFN |
| 197 | * |
| 198 | * Only IO mappings and raw PFN mappings are allowed. |
| 199 | * |
| 200 | * Returns zero and the pfn at @pfn on success, -ve otherwise. |
| 201 | */ |
| 202 | int follow_pfn(struct vm_area_struct *vma, unsigned long address, |
| 203 | unsigned long *pfn) |
| 204 | { |
| 205 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) |
| 206 | return -EINVAL; |
| 207 | |
| 208 | *pfn = address >> PAGE_SHIFT; |
| 209 | return 0; |
| 210 | } |
| 211 | EXPORT_SYMBOL(follow_pfn); |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | DEFINE_RWLOCK(vmlist_lock); |
| 214 | struct vm_struct *vmlist; |
| 215 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 216 | void vfree(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
| 218 | kfree(addr); |
| 219 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 220 | EXPORT_SYMBOL(vfree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 222 | void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | /* |
Robert P. J. Day | 8518609 | 2007-10-19 23:11:38 +0200 | [diff] [blame] | 225 | * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() |
| 226 | * returns only a logical address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | */ |
Nick Piggin | 8409751 | 2006-03-22 00:08:34 -0800 | [diff] [blame] | 228 | return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 230 | EXPORT_SYMBOL(__vmalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 232 | void *vmalloc_user(unsigned long size) |
| 233 | { |
| 234 | void *ret; |
| 235 | |
| 236 | ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, |
| 237 | PAGE_KERNEL); |
| 238 | if (ret) { |
| 239 | struct vm_area_struct *vma; |
| 240 | |
| 241 | down_write(¤t->mm->mmap_sem); |
| 242 | vma = find_vma(current->mm, (unsigned long)ret); |
| 243 | if (vma) |
| 244 | vma->vm_flags |= VM_USERMAP; |
| 245 | up_write(¤t->mm->mmap_sem); |
| 246 | } |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | EXPORT_SYMBOL(vmalloc_user); |
| 251 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 252 | struct page *vmalloc_to_page(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | return virt_to_page(addr); |
| 255 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 256 | EXPORT_SYMBOL(vmalloc_to_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 258 | unsigned long vmalloc_to_pfn(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | return page_to_pfn(virt_to_page(addr)); |
| 261 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL(vmalloc_to_pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | long vread(char *buf, char *addr, unsigned long count) |
| 265 | { |
| 266 | memcpy(buf, addr, count); |
| 267 | return count; |
| 268 | } |
| 269 | |
| 270 | long vwrite(char *buf, char *addr, unsigned long count) |
| 271 | { |
| 272 | /* Don't allow overflow */ |
| 273 | if ((unsigned long) addr + count < count) |
| 274 | count = -(unsigned long) addr; |
| 275 | |
| 276 | memcpy(addr, buf, count); |
| 277 | return(count); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * vmalloc - allocate virtually continguos memory |
| 282 | * |
| 283 | * @size: allocation size |
| 284 | * |
| 285 | * Allocate enough pages to cover @size from the page level |
| 286 | * allocator and map them into continguos kernel virtual space. |
| 287 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 288 | * For tight control over page level allocator and protection flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * use __vmalloc() instead. |
| 290 | */ |
| 291 | void *vmalloc(unsigned long size) |
| 292 | { |
| 293 | return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL); |
| 294 | } |
Andrew Morton | f613888 | 2006-02-28 16:59:18 -0800 | [diff] [blame] | 295 | EXPORT_SYMBOL(vmalloc); |
| 296 | |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 297 | /* |
| 298 | * vzalloc - allocate virtually continguos memory with zero fill |
| 299 | * |
| 300 | * @size: allocation size |
| 301 | * |
| 302 | * Allocate enough pages to cover @size from the page level |
| 303 | * allocator and map them into continguos kernel virtual space. |
| 304 | * The memory allocated is set to zero. |
| 305 | * |
| 306 | * For tight control over page level allocator and protection flags |
| 307 | * use __vmalloc() instead. |
| 308 | */ |
| 309 | void *vzalloc(unsigned long size) |
| 310 | { |
| 311 | return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, |
| 312 | PAGE_KERNEL); |
| 313 | } |
| 314 | EXPORT_SYMBOL(vzalloc); |
| 315 | |
| 316 | /** |
| 317 | * vmalloc_node - allocate memory on a specific node |
| 318 | * @size: allocation size |
| 319 | * @node: numa node |
| 320 | * |
| 321 | * Allocate enough pages to cover @size from the page level |
| 322 | * allocator and map them into contiguous kernel virtual space. |
| 323 | * |
| 324 | * For tight control over page level allocator and protection flags |
| 325 | * use __vmalloc() instead. |
| 326 | */ |
Andrew Morton | f613888 | 2006-02-28 16:59:18 -0800 | [diff] [blame] | 327 | void *vmalloc_node(unsigned long size, int node) |
| 328 | { |
| 329 | return vmalloc(size); |
| 330 | } |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 331 | |
| 332 | /** |
| 333 | * vzalloc_node - allocate memory on a specific node with zero fill |
| 334 | * @size: allocation size |
| 335 | * @node: numa node |
| 336 | * |
| 337 | * Allocate enough pages to cover @size from the page level |
| 338 | * allocator and map them into contiguous kernel virtual space. |
| 339 | * The memory allocated is set to zero. |
| 340 | * |
| 341 | * For tight control over page level allocator and protection flags |
| 342 | * use __vmalloc() instead. |
| 343 | */ |
| 344 | void *vzalloc_node(unsigned long size, int node) |
| 345 | { |
| 346 | return vzalloc(size); |
| 347 | } |
| 348 | EXPORT_SYMBOL(vzalloc_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Paul Mundt | 1af446e | 2008-08-04 16:01:47 +0900 | [diff] [blame] | 350 | #ifndef PAGE_KERNEL_EXEC |
| 351 | # define PAGE_KERNEL_EXEC PAGE_KERNEL |
| 352 | #endif |
| 353 | |
| 354 | /** |
| 355 | * vmalloc_exec - allocate virtually contiguous, executable memory |
| 356 | * @size: allocation size |
| 357 | * |
| 358 | * Kernel-internal function to allocate enough pages to cover @size |
| 359 | * the page level allocator and map them into contiguous and |
| 360 | * executable kernel virtual space. |
| 361 | * |
| 362 | * For tight control over page level allocator and protection flags |
| 363 | * use __vmalloc() instead. |
| 364 | */ |
| 365 | |
| 366 | void *vmalloc_exec(unsigned long size) |
| 367 | { |
| 368 | return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC); |
| 369 | } |
| 370 | |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 371 | /** |
| 372 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * @size: allocation size |
| 374 | * |
| 375 | * Allocate enough 32bit PA addressable pages to cover @size from the |
| 376 | * page level allocator and map them into continguos kernel virtual space. |
| 377 | */ |
| 378 | void *vmalloc_32(unsigned long size) |
| 379 | { |
| 380 | return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL); |
| 381 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 382 | EXPORT_SYMBOL(vmalloc_32); |
| 383 | |
| 384 | /** |
| 385 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory |
| 386 | * @size: allocation size |
| 387 | * |
| 388 | * The resulting memory area is 32bit addressable and zeroed so it can be |
| 389 | * mapped to userspace without leaking data. |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 390 | * |
| 391 | * VM_USERMAP is set on the corresponding VMA so that subsequent calls to |
| 392 | * remap_vmalloc_range() are permissible. |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 393 | */ |
| 394 | void *vmalloc_32_user(unsigned long size) |
| 395 | { |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 396 | /* |
| 397 | * We'll have to sort out the ZONE_DMA bits for 64-bit, |
| 398 | * but for now this can simply use vmalloc_user() directly. |
| 399 | */ |
| 400 | return vmalloc_user(size); |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 401 | } |
| 402 | EXPORT_SYMBOL(vmalloc_32_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) |
| 405 | { |
| 406 | BUG(); |
| 407 | return NULL; |
| 408 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 409 | EXPORT_SYMBOL(vmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 411 | void vunmap(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | BUG(); |
| 414 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 415 | EXPORT_SYMBOL(vunmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Paul Mundt | eb6434d | 2009-01-21 17:45:47 +0900 | [diff] [blame] | 417 | void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) |
| 418 | { |
| 419 | BUG(); |
| 420 | return NULL; |
| 421 | } |
| 422 | EXPORT_SYMBOL(vm_map_ram); |
| 423 | |
| 424 | void vm_unmap_ram(const void *mem, unsigned int count) |
| 425 | { |
| 426 | BUG(); |
| 427 | } |
| 428 | EXPORT_SYMBOL(vm_unmap_ram); |
| 429 | |
| 430 | void vm_unmap_aliases(void) |
| 431 | { |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 436 | * Implement a stub for vmalloc_sync_all() if the architecture chose not to |
| 437 | * have one. |
| 438 | */ |
| 439 | void __attribute__((weak)) vmalloc_sync_all(void) |
| 440 | { |
| 441 | } |
| 442 | |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 443 | int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, |
| 444 | struct page *page) |
| 445 | { |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | EXPORT_SYMBOL(vm_insert_page); |
| 449 | |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 450 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * sys_brk() for the most part doesn't need the global kernel |
| 452 | * lock, except when an application is doing something nasty |
| 453 | * like trying to un-brk an area that has already been mapped |
| 454 | * to a regular file. in this case, the unmapping will need |
| 455 | * to invoke file system routines that need the global lock. |
| 456 | */ |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 457 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | struct mm_struct *mm = current->mm; |
| 460 | |
| 461 | if (brk < mm->start_brk || brk > mm->context.end_brk) |
| 462 | return mm->brk; |
| 463 | |
| 464 | if (mm->brk == brk) |
| 465 | return mm->brk; |
| 466 | |
| 467 | /* |
| 468 | * Always allow shrinking brk |
| 469 | */ |
| 470 | if (brk <= mm->brk) { |
| 471 | mm->brk = brk; |
| 472 | return brk; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Ok, looks good - let it rip. |
| 477 | */ |
Mike Frysinger | cfe79c0 | 2010-01-06 17:23:23 +0000 | [diff] [blame] | 478 | flush_icache_range(mm->brk, brk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return mm->brk = brk; |
| 480 | } |
| 481 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 482 | /* |
| 483 | * initialise the VMA and region record slabs |
| 484 | */ |
| 485 | void __init mmap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 487 | int ret; |
| 488 | |
| 489 | ret = percpu_counter_init(&vm_committed_as, 0); |
| 490 | VM_BUG_ON(ret); |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 491 | vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 494 | /* |
| 495 | * validate the region tree |
| 496 | * - the caller must hold the region lock |
| 497 | */ |
| 498 | #ifdef CONFIG_DEBUG_NOMMU_REGIONS |
| 499 | static noinline void validate_nommu_regions(void) |
| 500 | { |
| 501 | struct vm_region *region, *last; |
| 502 | struct rb_node *p, *lastp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 504 | lastp = rb_first(&nommu_region_tree); |
| 505 | if (!lastp) |
| 506 | return; |
| 507 | |
| 508 | last = rb_entry(lastp, struct vm_region, vm_rb); |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 509 | BUG_ON(unlikely(last->vm_end <= last->vm_start)); |
| 510 | BUG_ON(unlikely(last->vm_top < last->vm_end)); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 511 | |
| 512 | while ((p = rb_next(lastp))) { |
| 513 | region = rb_entry(p, struct vm_region, vm_rb); |
| 514 | last = rb_entry(lastp, struct vm_region, vm_rb); |
| 515 | |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 516 | BUG_ON(unlikely(region->vm_end <= region->vm_start)); |
| 517 | BUG_ON(unlikely(region->vm_top < region->vm_end)); |
| 518 | BUG_ON(unlikely(region->vm_start < last->vm_top)); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 519 | |
| 520 | lastp = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 523 | #else |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 524 | static void validate_nommu_regions(void) |
| 525 | { |
| 526 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 527 | #endif |
| 528 | |
| 529 | /* |
| 530 | * add a region into the global tree |
| 531 | */ |
| 532 | static void add_nommu_region(struct vm_region *region) |
| 533 | { |
| 534 | struct vm_region *pregion; |
| 535 | struct rb_node **p, *parent; |
| 536 | |
| 537 | validate_nommu_regions(); |
| 538 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 539 | parent = NULL; |
| 540 | p = &nommu_region_tree.rb_node; |
| 541 | while (*p) { |
| 542 | parent = *p; |
| 543 | pregion = rb_entry(parent, struct vm_region, vm_rb); |
| 544 | if (region->vm_start < pregion->vm_start) |
| 545 | p = &(*p)->rb_left; |
| 546 | else if (region->vm_start > pregion->vm_start) |
| 547 | p = &(*p)->rb_right; |
| 548 | else if (pregion == region) |
| 549 | return; |
| 550 | else |
| 551 | BUG(); |
| 552 | } |
| 553 | |
| 554 | rb_link_node(®ion->vm_rb, parent, p); |
| 555 | rb_insert_color(®ion->vm_rb, &nommu_region_tree); |
| 556 | |
| 557 | validate_nommu_regions(); |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * delete a region from the global tree |
| 562 | */ |
| 563 | static void delete_nommu_region(struct vm_region *region) |
| 564 | { |
| 565 | BUG_ON(!nommu_region_tree.rb_node); |
| 566 | |
| 567 | validate_nommu_regions(); |
| 568 | rb_erase(®ion->vm_rb, &nommu_region_tree); |
| 569 | validate_nommu_regions(); |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * free a contiguous series of pages |
| 574 | */ |
| 575 | static void free_page_series(unsigned long from, unsigned long to) |
| 576 | { |
| 577 | for (; from < to; from += PAGE_SIZE) { |
| 578 | struct page *page = virt_to_page(from); |
| 579 | |
| 580 | kdebug("- free %lx", from); |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 581 | atomic_long_dec(&mmap_pages_allocated); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 582 | if (page_count(page) != 1) |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 583 | kdebug("free page %p: refcount not one: %d", |
| 584 | page, page_count(page)); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 585 | put_page(page); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * release a reference to a region |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 591 | * - the caller must hold the region semaphore for writing, which this releases |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 592 | * - the region may not have been added to the tree yet, in which case vm_top |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 593 | * will equal vm_start |
| 594 | */ |
| 595 | static void __put_nommu_region(struct vm_region *region) |
| 596 | __releases(nommu_region_sem) |
| 597 | { |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 598 | kenter("%p{%d}", region, region->vm_usage); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 599 | |
| 600 | BUG_ON(!nommu_region_tree.rb_node); |
| 601 | |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 602 | if (--region->vm_usage == 0) { |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 603 | if (region->vm_top > region->vm_start) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 604 | delete_nommu_region(region); |
| 605 | up_write(&nommu_region_sem); |
| 606 | |
| 607 | if (region->vm_file) |
| 608 | fput(region->vm_file); |
| 609 | |
| 610 | /* IO memory and memory shared directly out of the pagecache |
| 611 | * from ramfs/tmpfs mustn't be released here */ |
| 612 | if (region->vm_flags & VM_MAPPED_COPY) { |
| 613 | kdebug("free series"); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 614 | free_page_series(region->vm_start, region->vm_top); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 615 | } |
| 616 | kmem_cache_free(vm_region_jar, region); |
| 617 | } else { |
| 618 | up_write(&nommu_region_sem); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * release a reference to a region |
| 624 | */ |
| 625 | static void put_nommu_region(struct vm_region *region) |
| 626 | { |
| 627 | down_write(&nommu_region_sem); |
| 628 | __put_nommu_region(region); |
| 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 631 | /* |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 632 | * update protection on a vma |
| 633 | */ |
| 634 | static void protect_vma(struct vm_area_struct *vma, unsigned long flags) |
| 635 | { |
| 636 | #ifdef CONFIG_MPU |
| 637 | struct mm_struct *mm = vma->vm_mm; |
| 638 | long start = vma->vm_start & PAGE_MASK; |
| 639 | while (start < vma->vm_end) { |
| 640 | protect_page(mm, start, flags); |
| 641 | start += PAGE_SIZE; |
| 642 | } |
| 643 | update_protections(mm); |
| 644 | #endif |
| 645 | } |
| 646 | |
| 647 | /* |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 648 | * add a VMA into a process's mm_struct in the appropriate place in the list |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 649 | * and tree and add to the address space's page tree also if not an anonymous |
| 650 | * page |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 651 | * - should be called with mm->mmap_sem held writelocked |
| 652 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 653 | static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 654 | { |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 655 | struct vm_area_struct *pvma, **pp, *next; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 656 | struct address_space *mapping; |
| 657 | struct rb_node **p, *parent; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 658 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 659 | kenter(",%p", vma); |
| 660 | |
| 661 | BUG_ON(!vma->vm_region); |
| 662 | |
| 663 | mm->map_count++; |
| 664 | vma->vm_mm = mm; |
| 665 | |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 666 | protect_vma(vma, vma->vm_flags); |
| 667 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 668 | /* add the VMA to the mapping */ |
| 669 | if (vma->vm_file) { |
| 670 | mapping = vma->vm_file->f_mapping; |
| 671 | |
| 672 | flush_dcache_mmap_lock(mapping); |
| 673 | vma_prio_tree_insert(vma, &mapping->i_mmap); |
| 674 | flush_dcache_mmap_unlock(mapping); |
| 675 | } |
| 676 | |
| 677 | /* add the VMA to the tree */ |
| 678 | parent = NULL; |
| 679 | p = &mm->mm_rb.rb_node; |
| 680 | while (*p) { |
| 681 | parent = *p; |
| 682 | pvma = rb_entry(parent, struct vm_area_struct, vm_rb); |
| 683 | |
| 684 | /* sort by: start addr, end addr, VMA struct addr in that order |
| 685 | * (the latter is necessary as we may get identical VMAs) */ |
| 686 | if (vma->vm_start < pvma->vm_start) |
| 687 | p = &(*p)->rb_left; |
| 688 | else if (vma->vm_start > pvma->vm_start) |
| 689 | p = &(*p)->rb_right; |
| 690 | else if (vma->vm_end < pvma->vm_end) |
| 691 | p = &(*p)->rb_left; |
| 692 | else if (vma->vm_end > pvma->vm_end) |
| 693 | p = &(*p)->rb_right; |
| 694 | else if (vma < pvma) |
| 695 | p = &(*p)->rb_left; |
| 696 | else if (vma > pvma) |
| 697 | p = &(*p)->rb_right; |
| 698 | else |
| 699 | BUG(); |
| 700 | } |
| 701 | |
| 702 | rb_link_node(&vma->vm_rb, parent, p); |
| 703 | rb_insert_color(&vma->vm_rb, &mm->mm_rb); |
| 704 | |
| 705 | /* add VMA to the VMA list also */ |
| 706 | for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) { |
| 707 | if (pvma->vm_start > vma->vm_start) |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 708 | break; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 709 | if (pvma->vm_start < vma->vm_start) |
| 710 | continue; |
| 711 | if (pvma->vm_end < vma->vm_end) |
| 712 | break; |
| 713 | } |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 714 | |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 715 | next = *pp; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 716 | *pp = vma; |
Linus Torvalds | 297c5ee | 2010-08-20 16:24:55 -0700 | [diff] [blame] | 717 | vma->vm_next = next; |
| 718 | if (next) |
| 719 | next->vm_prev = vma; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
| 723 | * delete a VMA from its owning mm_struct and address space |
| 724 | */ |
| 725 | static void delete_vma_from_mm(struct vm_area_struct *vma) |
| 726 | { |
| 727 | struct vm_area_struct **pp; |
| 728 | struct address_space *mapping; |
| 729 | struct mm_struct *mm = vma->vm_mm; |
| 730 | |
| 731 | kenter("%p", vma); |
| 732 | |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 733 | protect_vma(vma, 0); |
| 734 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 735 | mm->map_count--; |
| 736 | if (mm->mmap_cache == vma) |
| 737 | mm->mmap_cache = NULL; |
| 738 | |
| 739 | /* remove the VMA from the mapping */ |
| 740 | if (vma->vm_file) { |
| 741 | mapping = vma->vm_file->f_mapping; |
| 742 | |
| 743 | flush_dcache_mmap_lock(mapping); |
| 744 | vma_prio_tree_remove(vma, &mapping->i_mmap); |
| 745 | flush_dcache_mmap_unlock(mapping); |
| 746 | } |
| 747 | |
| 748 | /* remove from the MM's tree and list */ |
| 749 | rb_erase(&vma->vm_rb, &mm->mm_rb); |
| 750 | for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) { |
| 751 | if (*pp == vma) { |
| 752 | *pp = vma->vm_next; |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | vma->vm_mm = NULL; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * destroy a VMA record |
| 762 | */ |
| 763 | static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) |
| 764 | { |
| 765 | kenter("%p", vma); |
| 766 | if (vma->vm_ops && vma->vm_ops->close) |
| 767 | vma->vm_ops->close(vma); |
| 768 | if (vma->vm_file) { |
| 769 | fput(vma->vm_file); |
| 770 | if (vma->vm_flags & VM_EXECUTABLE) |
| 771 | removed_exe_file_vma(mm); |
| 772 | } |
| 773 | put_nommu_region(vma->vm_region); |
| 774 | kmem_cache_free(vm_area_cachep, vma); |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
| 778 | * look up the first VMA in which addr resides, NULL if none |
| 779 | * - should be called with mm->mmap_sem at least held readlocked |
| 780 | */ |
| 781 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) |
| 782 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 783 | struct vm_area_struct *vma; |
| 784 | struct rb_node *n = mm->mm_rb.rb_node; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 785 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 786 | /* check the cache first */ |
| 787 | vma = mm->mmap_cache; |
| 788 | if (vma && vma->vm_start <= addr && vma->vm_end > addr) |
| 789 | return vma; |
| 790 | |
| 791 | /* trawl the tree (there may be multiple mappings in which addr |
| 792 | * resides) */ |
| 793 | for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) { |
| 794 | vma = rb_entry(n, struct vm_area_struct, vm_rb); |
| 795 | if (vma->vm_start > addr) |
| 796 | return NULL; |
| 797 | if (vma->vm_end > addr) { |
| 798 | mm->mmap_cache = vma; |
| 799 | return vma; |
| 800 | } |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 801 | } |
| 802 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 803 | return NULL; |
| 804 | } |
| 805 | EXPORT_SYMBOL(find_vma); |
| 806 | |
| 807 | /* |
David Howells | 930e652 | 2006-09-27 01:50:22 -0700 | [diff] [blame] | 808 | * find a VMA |
| 809 | * - we don't extend stack VMAs under NOMMU conditions |
| 810 | */ |
| 811 | struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) |
| 812 | { |
David Howells | 7561e8c | 2010-03-25 16:48:38 +0000 | [diff] [blame] | 813 | return find_vma(mm, addr); |
David Howells | 930e652 | 2006-09-27 01:50:22 -0700 | [diff] [blame] | 814 | } |
| 815 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 816 | /* |
| 817 | * expand a stack to a given address |
| 818 | * - not supported under NOMMU conditions |
| 819 | */ |
Greg Ungerer | 57c8f63 | 2007-07-15 23:38:28 -0700 | [diff] [blame] | 820 | int expand_stack(struct vm_area_struct *vma, unsigned long address) |
| 821 | { |
| 822 | return -ENOMEM; |
| 823 | } |
| 824 | |
David Howells | 930e652 | 2006-09-27 01:50:22 -0700 | [diff] [blame] | 825 | /* |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 826 | * look up the first VMA exactly that exactly matches addr |
| 827 | * - should be called with mm->mmap_sem at least held readlocked |
| 828 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 829 | static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, |
| 830 | unsigned long addr, |
| 831 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
| 833 | struct vm_area_struct *vma; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 834 | struct rb_node *n = mm->mm_rb.rb_node; |
| 835 | unsigned long end = addr + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 837 | /* check the cache first */ |
| 838 | vma = mm->mmap_cache; |
| 839 | if (vma && vma->vm_start == addr && vma->vm_end == end) |
| 840 | return vma; |
| 841 | |
| 842 | /* trawl the tree (there may be multiple mappings in which addr |
| 843 | * resides) */ |
| 844 | for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | vma = rb_entry(n, struct vm_area_struct, vm_rb); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 846 | if (vma->vm_start < addr) |
| 847 | continue; |
| 848 | if (vma->vm_start > addr) |
| 849 | return NULL; |
| 850 | if (vma->vm_end == end) { |
| 851 | mm->mmap_cache = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return vma; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | return NULL; |
| 857 | } |
| 858 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 859 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | * determine whether a mapping should be permitted and, if so, what sort of |
| 861 | * mapping we're capable of supporting |
| 862 | */ |
| 863 | static int validate_mmap_request(struct file *file, |
| 864 | unsigned long addr, |
| 865 | unsigned long len, |
| 866 | unsigned long prot, |
| 867 | unsigned long flags, |
| 868 | unsigned long pgoff, |
| 869 | unsigned long *_capabilities) |
| 870 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 871 | unsigned long capabilities, rlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | unsigned long reqprot = prot; |
| 873 | int ret; |
| 874 | |
| 875 | /* do the simple checks first */ |
David Howells | 06aab5a | 2009-09-24 12:33:48 +0100 | [diff] [blame] | 876 | if (flags & MAP_FIXED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | printk(KERN_DEBUG |
| 878 | "%d: Can't do fixed-address/overlay mmap of RAM\n", |
| 879 | current->pid); |
| 880 | return -EINVAL; |
| 881 | } |
| 882 | |
| 883 | if ((flags & MAP_TYPE) != MAP_PRIVATE && |
| 884 | (flags & MAP_TYPE) != MAP_SHARED) |
| 885 | return -EINVAL; |
| 886 | |
Mike Frysinger | f81cff0 | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 887 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | return -EINVAL; |
| 889 | |
Mike Frysinger | f81cff0 | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 890 | /* Careful about overflows.. */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 891 | rlen = PAGE_ALIGN(len); |
| 892 | if (!rlen || rlen > TASK_SIZE) |
Mike Frysinger | f81cff0 | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 893 | return -ENOMEM; |
| 894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | /* offset overflow? */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 896 | if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) |
Mike Frysinger | f81cff0 | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 897 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | if (file) { |
| 900 | /* validate file mapping requests */ |
| 901 | struct address_space *mapping; |
| 902 | |
| 903 | /* files must support mmap */ |
| 904 | if (!file->f_op || !file->f_op->mmap) |
| 905 | return -ENODEV; |
| 906 | |
| 907 | /* work out if what we've got could possibly be shared |
| 908 | * - we support chardevs that provide their own "memory" |
| 909 | * - we support files/blockdevs that are memory backed |
| 910 | */ |
| 911 | mapping = file->f_mapping; |
| 912 | if (!mapping) |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 913 | mapping = file->f_path.dentry->d_inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | capabilities = 0; |
| 916 | if (mapping && mapping->backing_dev_info) |
| 917 | capabilities = mapping->backing_dev_info->capabilities; |
| 918 | |
| 919 | if (!capabilities) { |
| 920 | /* no explicit capabilities set, so assume some |
| 921 | * defaults */ |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 922 | switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | case S_IFREG: |
| 924 | case S_IFBLK: |
| 925 | capabilities = BDI_CAP_MAP_COPY; |
| 926 | break; |
| 927 | |
| 928 | case S_IFCHR: |
| 929 | capabilities = |
| 930 | BDI_CAP_MAP_DIRECT | |
| 931 | BDI_CAP_READ_MAP | |
| 932 | BDI_CAP_WRITE_MAP; |
| 933 | break; |
| 934 | |
| 935 | default: |
| 936 | return -EINVAL; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /* eliminate any capabilities that we can't support on this |
| 941 | * device */ |
| 942 | if (!file->f_op->get_unmapped_area) |
| 943 | capabilities &= ~BDI_CAP_MAP_DIRECT; |
| 944 | if (!file->f_op->read) |
| 945 | capabilities &= ~BDI_CAP_MAP_COPY; |
| 946 | |
Graff Yang | 28d7a6a | 2009-08-18 14:11:17 -0700 | [diff] [blame] | 947 | /* The file shall have been opened with read permission. */ |
| 948 | if (!(file->f_mode & FMODE_READ)) |
| 949 | return -EACCES; |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | if (flags & MAP_SHARED) { |
| 952 | /* do checks for writing, appending and locking */ |
| 953 | if ((prot & PROT_WRITE) && |
| 954 | !(file->f_mode & FMODE_WRITE)) |
| 955 | return -EACCES; |
| 956 | |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 957 | if (IS_APPEND(file->f_path.dentry->d_inode) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | (file->f_mode & FMODE_WRITE)) |
| 959 | return -EACCES; |
| 960 | |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 961 | if (locks_verify_locked(file->f_path.dentry->d_inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return -EAGAIN; |
| 963 | |
| 964 | if (!(capabilities & BDI_CAP_MAP_DIRECT)) |
| 965 | return -ENODEV; |
| 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | /* we mustn't privatise shared mappings */ |
| 968 | capabilities &= ~BDI_CAP_MAP_COPY; |
| 969 | } |
| 970 | else { |
| 971 | /* we're going to read the file into private memory we |
| 972 | * allocate */ |
| 973 | if (!(capabilities & BDI_CAP_MAP_COPY)) |
| 974 | return -ENODEV; |
| 975 | |
| 976 | /* we don't permit a private writable mapping to be |
| 977 | * shared with the backing device */ |
| 978 | if (prot & PROT_WRITE) |
| 979 | capabilities &= ~BDI_CAP_MAP_DIRECT; |
| 980 | } |
| 981 | |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 982 | if (capabilities & BDI_CAP_MAP_DIRECT) { |
| 983 | if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) || |
| 984 | ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) || |
| 985 | ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP)) |
| 986 | ) { |
| 987 | capabilities &= ~BDI_CAP_MAP_DIRECT; |
| 988 | if (flags & MAP_SHARED) { |
| 989 | printk(KERN_WARNING |
| 990 | "MAP_SHARED not completely supported on !MMU\n"); |
| 991 | return -EINVAL; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | /* handle executable mappings and implied executable |
| 997 | * mappings */ |
Josef Sipek | e9536ae | 2006-12-08 02:37:21 -0800 | [diff] [blame] | 998 | if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | if (prot & PROT_EXEC) |
| 1000 | return -EPERM; |
| 1001 | } |
| 1002 | else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { |
| 1003 | /* handle implication of PROT_EXEC by PROT_READ */ |
| 1004 | if (current->personality & READ_IMPLIES_EXEC) { |
| 1005 | if (capabilities & BDI_CAP_EXEC_MAP) |
| 1006 | prot |= PROT_EXEC; |
| 1007 | } |
| 1008 | } |
| 1009 | else if ((prot & PROT_READ) && |
| 1010 | (prot & PROT_EXEC) && |
| 1011 | !(capabilities & BDI_CAP_EXEC_MAP) |
| 1012 | ) { |
| 1013 | /* backing file is not executable, try to copy */ |
| 1014 | capabilities &= ~BDI_CAP_MAP_DIRECT; |
| 1015 | } |
| 1016 | } |
| 1017 | else { |
| 1018 | /* anonymous mappings are always memory backed and can be |
| 1019 | * privately mapped |
| 1020 | */ |
| 1021 | capabilities = BDI_CAP_MAP_COPY; |
| 1022 | |
| 1023 | /* handle PROT_EXEC implication by PROT_READ */ |
| 1024 | if ((prot & PROT_READ) && |
| 1025 | (current->personality & READ_IMPLIES_EXEC)) |
| 1026 | prot |= PROT_EXEC; |
| 1027 | } |
| 1028 | |
| 1029 | /* allow the security API to have its say */ |
Eric Paris | ed03218 | 2007-06-28 15:55:21 -0400 | [diff] [blame] | 1030 | ret = security_file_mmap(file, reqprot, prot, flags, addr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | if (ret < 0) |
| 1032 | return ret; |
| 1033 | |
| 1034 | /* looks okay */ |
| 1035 | *_capabilities = capabilities; |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * we've determined that we can make the mapping, now translate what we |
| 1041 | * now know into VMA flags |
| 1042 | */ |
| 1043 | static unsigned long determine_vm_flags(struct file *file, |
| 1044 | unsigned long prot, |
| 1045 | unsigned long flags, |
| 1046 | unsigned long capabilities) |
| 1047 | { |
| 1048 | unsigned long vm_flags; |
| 1049 | |
| 1050 | vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | /* vm_flags |= mm->def_flags; */ |
| 1052 | |
| 1053 | if (!(capabilities & BDI_CAP_MAP_DIRECT)) { |
| 1054 | /* attempt to share read-only copies of mapped file chunks */ |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 1055 | vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | if (file && !(prot & PROT_WRITE)) |
| 1057 | vm_flags |= VM_MAYSHARE; |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 1058 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | /* overlay a shareable mapping on the backing device or inode |
| 1060 | * if possible - used for chardevs, ramfs/tmpfs/shmfs and |
| 1061 | * romfs/cramfs */ |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 1062 | vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | if (flags & MAP_SHARED) |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 1064 | vm_flags |= VM_SHARED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* refuse to let anyone share private mappings with this process if |
| 1068 | * it's being traced - otherwise breakpoints set in it may interfere |
| 1069 | * with another untraced process |
| 1070 | */ |
Roland McGrath | fa8e26c | 2008-07-25 19:45:50 -0700 | [diff] [blame] | 1071 | if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | vm_flags &= ~VM_MAYSHARE; |
| 1073 | |
| 1074 | return vm_flags; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1078 | * set up a shared mapping on a file (the driver or filesystem provides and |
| 1079 | * pins the storage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1081 | static int do_mmap_shared_file(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | { |
| 1083 | int ret; |
| 1084 | |
| 1085 | ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1086 | if (ret == 0) { |
| 1087 | vma->vm_region->vm_top = vma->vm_region->vm_end; |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1088 | return 0; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1089 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | if (ret != -ENOSYS) |
| 1091 | return ret; |
| 1092 | |
David Howells | 3fa3046 | 2010-03-23 13:35:21 -0700 | [diff] [blame] | 1093 | /* getting -ENOSYS indicates that direct mmap isn't possible (as |
| 1094 | * opposed to tried but failed) so we can only give a suitable error as |
| 1095 | * it's not possible to make a private copy if MAP_SHARED was given */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | return -ENODEV; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * set up a private mapping or an anonymous shared mapping |
| 1101 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1102 | static int do_mmap_private(struct vm_area_struct *vma, |
| 1103 | struct vm_region *region, |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1104 | unsigned long len, |
| 1105 | unsigned long capabilities) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1107 | struct page *pages; |
| 1108 | unsigned long total, point, n, rlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | void *base; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1110 | int ret, order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | /* invoke the file's mapping function so that it can keep track of |
| 1113 | * shared mappings on devices or memory |
| 1114 | * - VM_MAYSHARE will be set if it may attempt to share |
| 1115 | */ |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1116 | if (capabilities & BDI_CAP_MAP_DIRECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1118 | if (ret == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* shouldn't return success if we're not sharing */ |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1120 | BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); |
| 1121 | vma->vm_region->vm_top = vma->vm_region->vm_end; |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1124 | if (ret != -ENOSYS) |
| 1125 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
| 1127 | /* getting an ENOSYS error indicates that direct mmap isn't |
| 1128 | * possible (as opposed to tried but failed) so we'll try to |
| 1129 | * make a private copy of the data and map that instead */ |
| 1130 | } |
| 1131 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1132 | rlen = PAGE_ALIGN(len); |
| 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | /* allocate some memory to hold the mapping |
| 1135 | * - note that this may not return a page-aligned address if the object |
| 1136 | * we're allocating is smaller than a page |
| 1137 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1138 | order = get_order(rlen); |
| 1139 | kdebug("alloc order %d for %lx", order, len); |
| 1140 | |
| 1141 | pages = alloc_pages(GFP_KERNEL, order); |
| 1142 | if (!pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | goto enomem; |
| 1144 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1145 | total = 1 << order; |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 1146 | atomic_long_add(total, &mmap_pages_allocated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1148 | point = rlen >> PAGE_SHIFT; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* we allocated a power-of-2 sized page set, so we may want to trim off |
| 1151 | * the excess */ |
| 1152 | if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) { |
| 1153 | while (total > point) { |
| 1154 | order = ilog2(total - point); |
| 1155 | n = 1 << order; |
| 1156 | kdebug("shave %lu/%lu @%lu", n, total - point, total); |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 1157 | atomic_long_sub(n, &mmap_pages_allocated); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1158 | total -= n; |
| 1159 | set_page_refcounted(pages + total); |
| 1160 | __free_pages(pages + total, order); |
| 1161 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1164 | for (point = 1; point < total; point++) |
| 1165 | set_page_refcounted(&pages[point]); |
| 1166 | |
| 1167 | base = page_address(pages); |
| 1168 | region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; |
| 1169 | region->vm_start = (unsigned long) base; |
| 1170 | region->vm_end = region->vm_start + rlen; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1171 | region->vm_top = region->vm_start + (total << PAGE_SHIFT); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1172 | |
| 1173 | vma->vm_start = region->vm_start; |
| 1174 | vma->vm_end = region->vm_start + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
| 1176 | if (vma->vm_file) { |
| 1177 | /* read the contents of a file into the copy */ |
| 1178 | mm_segment_t old_fs; |
| 1179 | loff_t fpos; |
| 1180 | |
| 1181 | fpos = vma->vm_pgoff; |
| 1182 | fpos <<= PAGE_SHIFT; |
| 1183 | |
| 1184 | old_fs = get_fs(); |
| 1185 | set_fs(KERNEL_DS); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1186 | ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | set_fs(old_fs); |
| 1188 | |
| 1189 | if (ret < 0) |
| 1190 | goto error_free; |
| 1191 | |
| 1192 | /* clear the last little bit */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1193 | if (ret < rlen) |
| 1194 | memset(base + ret, 0, rlen - ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | return 0; |
| 1199 | |
| 1200 | error_free: |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1201 | free_page_series(region->vm_start, region->vm_end); |
| 1202 | region->vm_start = vma->vm_start = 0; |
| 1203 | region->vm_end = vma->vm_end = 0; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1204 | region->vm_top = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | return ret; |
| 1206 | |
| 1207 | enomem: |
Greg Ungerer | 05ae6fa | 2009-01-13 17:30:22 +1000 | [diff] [blame] | 1208 | printk("Allocation of length %lu from process %d (%s) failed\n", |
| 1209 | len, current->pid, current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | show_free_areas(); |
| 1211 | return -ENOMEM; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * handle mapping creation for uClinux |
| 1216 | */ |
| 1217 | unsigned long do_mmap_pgoff(struct file *file, |
| 1218 | unsigned long addr, |
| 1219 | unsigned long len, |
| 1220 | unsigned long prot, |
| 1221 | unsigned long flags, |
| 1222 | unsigned long pgoff) |
| 1223 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1224 | struct vm_area_struct *vma; |
| 1225 | struct vm_region *region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | struct rb_node *rb; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1227 | unsigned long capabilities, vm_flags, result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | int ret; |
| 1229 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1230 | kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff); |
| 1231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | /* decide whether we should attempt the mapping, and if so what sort of |
| 1233 | * mapping */ |
| 1234 | ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, |
| 1235 | &capabilities); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1236 | if (ret < 0) { |
| 1237 | kleave(" = %d [val]", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | return ret; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
David Howells | 06aab5a | 2009-09-24 12:33:48 +0100 | [diff] [blame] | 1241 | /* we ignore the address hint */ |
| 1242 | addr = 0; |
| 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /* we've determined that we can make the mapping, now translate what we |
| 1245 | * now know into VMA flags */ |
| 1246 | vm_flags = determine_vm_flags(file, prot, flags, capabilities); |
| 1247 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1248 | /* we're going to need to record the mapping */ |
| 1249 | region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); |
| 1250 | if (!region) |
| 1251 | goto error_getting_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1253 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); |
| 1254 | if (!vma) |
| 1255 | goto error_getting_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1257 | region->vm_usage = 1; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1258 | region->vm_flags = vm_flags; |
| 1259 | region->vm_pgoff = pgoff; |
| 1260 | |
Rik van Riel | 5beb493 | 2010-03-05 13:42:07 -0800 | [diff] [blame] | 1261 | INIT_LIST_HEAD(&vma->anon_vma_chain); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1262 | vma->vm_flags = vm_flags; |
| 1263 | vma->vm_pgoff = pgoff; |
| 1264 | |
| 1265 | if (file) { |
| 1266 | region->vm_file = file; |
| 1267 | get_file(file); |
| 1268 | vma->vm_file = file; |
| 1269 | get_file(file); |
| 1270 | if (vm_flags & VM_EXECUTABLE) { |
| 1271 | added_exe_file_vma(current->mm); |
| 1272 | vma->vm_mm = current->mm; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | down_write(&nommu_region_sem); |
| 1277 | |
| 1278 | /* if we want to share, we need to check for regions created by other |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | * mmap() calls that overlap with our proposed mapping |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1280 | * - we can only share with a superset match on most regular files |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | * - shared mappings on character devices and memory backed files are |
| 1282 | * permitted to overlap inexactly as far as we are concerned for in |
| 1283 | * these cases, sharing is handled in the driver or filesystem rather |
| 1284 | * than here |
| 1285 | */ |
| 1286 | if (vm_flags & VM_MAYSHARE) { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1287 | struct vm_region *pregion; |
| 1288 | unsigned long pglen, rpglen, pgend, rpgend, start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1290 | pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1291 | pgend = pgoff + pglen; |
David Howells | 165b239 | 2007-03-22 00:11:24 -0800 | [diff] [blame] | 1292 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1293 | for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { |
| 1294 | pregion = rb_entry(rb, struct vm_region, vm_rb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1296 | if (!(pregion->vm_flags & VM_MAYSHARE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | continue; |
| 1298 | |
| 1299 | /* search for overlapping mappings on the same file */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1300 | if (pregion->vm_file->f_path.dentry->d_inode != |
| 1301 | file->f_path.dentry->d_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | continue; |
| 1303 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1304 | if (pregion->vm_pgoff >= pgend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | continue; |
| 1306 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1307 | rpglen = pregion->vm_end - pregion->vm_start; |
| 1308 | rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1309 | rpgend = pregion->vm_pgoff + rpglen; |
| 1310 | if (pgoff >= rpgend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | continue; |
| 1312 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1313 | /* handle inexactly overlapping matches between |
| 1314 | * mappings */ |
| 1315 | if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && |
| 1316 | !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { |
| 1317 | /* new mapping is not a subset of the region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | if (!(capabilities & BDI_CAP_MAP_DIRECT)) |
| 1319 | goto sharing_violation; |
| 1320 | continue; |
| 1321 | } |
| 1322 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1323 | /* we've found a region we can share */ |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1324 | pregion->vm_usage++; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1325 | vma->vm_region = pregion; |
| 1326 | start = pregion->vm_start; |
| 1327 | start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; |
| 1328 | vma->vm_start = start; |
| 1329 | vma->vm_end = start + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1331 | if (pregion->vm_flags & VM_MAPPED_COPY) { |
| 1332 | kdebug("share copy"); |
| 1333 | vma->vm_flags |= VM_MAPPED_COPY; |
| 1334 | } else { |
| 1335 | kdebug("share mmap"); |
| 1336 | ret = do_mmap_shared_file(vma); |
| 1337 | if (ret < 0) { |
| 1338 | vma->vm_region = NULL; |
| 1339 | vma->vm_start = 0; |
| 1340 | vma->vm_end = 0; |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1341 | pregion->vm_usage--; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1342 | pregion = NULL; |
| 1343 | goto error_just_free; |
| 1344 | } |
| 1345 | } |
| 1346 | fput(region->vm_file); |
| 1347 | kmem_cache_free(vm_region_jar, region); |
| 1348 | region = pregion; |
| 1349 | result = start; |
| 1350 | goto share; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | /* obtain the address at which to make a shared mapping |
| 1354 | * - this is the hook for quasi-memory character devices to |
| 1355 | * tell us the location of a shared mapping |
| 1356 | */ |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1357 | if (capabilities & BDI_CAP_MAP_DIRECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | addr = file->f_op->get_unmapped_area(file, addr, len, |
| 1359 | pgoff, flags); |
| 1360 | if (IS_ERR((void *) addr)) { |
| 1361 | ret = addr; |
| 1362 | if (ret != (unsigned long) -ENOSYS) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1363 | goto error_just_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
| 1365 | /* the driver refused to tell us where to site |
| 1366 | * the mapping so we'll have to attempt to copy |
| 1367 | * it */ |
| 1368 | ret = (unsigned long) -ENODEV; |
| 1369 | if (!(capabilities & BDI_CAP_MAP_COPY)) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1370 | goto error_just_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | capabilities &= ~BDI_CAP_MAP_DIRECT; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1373 | } else { |
| 1374 | vma->vm_start = region->vm_start = addr; |
| 1375 | vma->vm_end = region->vm_end = addr + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1380 | vma->vm_region = region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1382 | /* set up the mapping |
| 1383 | * - the region is filled in if BDI_CAP_MAP_DIRECT is still set |
| 1384 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | if (file && vma->vm_flags & VM_SHARED) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1386 | ret = do_mmap_shared_file(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | else |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1388 | ret = do_mmap_private(vma, region, len, capabilities); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | if (ret < 0) |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1390 | goto error_just_free; |
| 1391 | add_nommu_region(region); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1392 | |
Jie Zhang | ea63763 | 2009-12-14 18:00:02 -0800 | [diff] [blame] | 1393 | /* clear anonymous mappings that don't ask for uninitialized data */ |
| 1394 | if (!vma->vm_file && !(flags & MAP_UNINITIALIZED)) |
| 1395 | memset((void *)region->vm_start, 0, |
| 1396 | region->vm_end - region->vm_start); |
| 1397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | /* okay... we have a mapping; now we have to register it */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1399 | result = vma->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | current->mm->total_vm += len >> PAGE_SHIFT; |
| 1402 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1403 | share: |
| 1404 | add_vma_to_mm(current->mm, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
Mike Frysinger | cfe79c0 | 2010-01-06 17:23:23 +0000 | [diff] [blame] | 1406 | /* we flush the region from the icache only when the first executable |
| 1407 | * mapping of it is made */ |
| 1408 | if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { |
| 1409 | flush_icache_range(region->vm_start, region->vm_end); |
| 1410 | region->vm_icache_flushed = true; |
| 1411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Mike Frysinger | cfe79c0 | 2010-01-06 17:23:23 +0000 | [diff] [blame] | 1413 | up_write(&nommu_region_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1415 | kleave(" = %lx", result); |
| 1416 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1418 | error_just_free: |
| 1419 | up_write(&nommu_region_sem); |
| 1420 | error: |
David Howells | 89a8640 | 2009-10-30 13:13:26 +0000 | [diff] [blame] | 1421 | if (region->vm_file) |
| 1422 | fput(region->vm_file); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1423 | kmem_cache_free(vm_region_jar, region); |
David Howells | 89a8640 | 2009-10-30 13:13:26 +0000 | [diff] [blame] | 1424 | if (vma->vm_file) |
| 1425 | fput(vma->vm_file); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1426 | if (vma->vm_flags & VM_EXECUTABLE) |
| 1427 | removed_exe_file_vma(vma->vm_mm); |
| 1428 | kmem_cache_free(vm_area_cachep, vma); |
| 1429 | kleave(" = %d", ret); |
| 1430 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1432 | sharing_violation: |
| 1433 | up_write(&nommu_region_sem); |
| 1434 | printk(KERN_WARNING "Attempt to share mismatched mappings\n"); |
| 1435 | ret = -EINVAL; |
| 1436 | goto error; |
| 1437 | |
| 1438 | error_getting_vma: |
| 1439 | kmem_cache_free(vm_region_jar, region); |
| 1440 | printk(KERN_WARNING "Allocation of vma for %lu byte allocation" |
| 1441 | " from process %d failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | len, current->pid); |
| 1443 | show_free_areas(); |
| 1444 | return -ENOMEM; |
| 1445 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1446 | error_getting_region: |
| 1447 | printk(KERN_WARNING "Allocation of vm region for %lu byte allocation" |
| 1448 | " from process %d failed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | len, current->pid); |
| 1450 | show_free_areas(); |
| 1451 | return -ENOMEM; |
| 1452 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 1453 | EXPORT_SYMBOL(do_mmap_pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1455 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
| 1456 | unsigned long, prot, unsigned long, flags, |
| 1457 | unsigned long, fd, unsigned long, pgoff) |
| 1458 | { |
| 1459 | struct file *file = NULL; |
| 1460 | unsigned long retval = -EBADF; |
| 1461 | |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 1462 | audit_mmap_fd(fd, flags); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1463 | if (!(flags & MAP_ANONYMOUS)) { |
| 1464 | file = fget(fd); |
| 1465 | if (!file) |
| 1466 | goto out; |
| 1467 | } |
| 1468 | |
| 1469 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); |
| 1470 | |
| 1471 | down_write(¤t->mm->mmap_sem); |
| 1472 | retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
| 1473 | up_write(¤t->mm->mmap_sem); |
| 1474 | |
| 1475 | if (file) |
| 1476 | fput(file); |
| 1477 | out: |
| 1478 | return retval; |
| 1479 | } |
| 1480 | |
Christoph Hellwig | a467937 | 2010-03-10 15:21:15 -0800 | [diff] [blame] | 1481 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
| 1482 | struct mmap_arg_struct { |
| 1483 | unsigned long addr; |
| 1484 | unsigned long len; |
| 1485 | unsigned long prot; |
| 1486 | unsigned long flags; |
| 1487 | unsigned long fd; |
| 1488 | unsigned long offset; |
| 1489 | }; |
| 1490 | |
| 1491 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) |
| 1492 | { |
| 1493 | struct mmap_arg_struct a; |
| 1494 | |
| 1495 | if (copy_from_user(&a, arg, sizeof(a))) |
| 1496 | return -EFAULT; |
| 1497 | if (a.offset & ~PAGE_MASK) |
| 1498 | return -EINVAL; |
| 1499 | |
| 1500 | return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, |
| 1501 | a.offset >> PAGE_SHIFT); |
| 1502 | } |
| 1503 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ |
| 1504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1506 | * split a vma into two pieces at address 'addr', a new vma is allocated either |
| 1507 | * for the first part or the tail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1509 | int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1510 | unsigned long addr, int new_below) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1512 | struct vm_area_struct *new; |
| 1513 | struct vm_region *region; |
| 1514 | unsigned long npages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1516 | kenter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
David Howells | 779c102 | 2010-01-15 17:01:34 -0800 | [diff] [blame] | 1518 | /* we're only permitted to split anonymous regions (these should have |
| 1519 | * only a single usage on the region) */ |
| 1520 | if (vma->vm_file) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1521 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1523 | if (mm->map_count >= sysctl_max_map_count) |
| 1524 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1526 | region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); |
| 1527 | if (!region) |
| 1528 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1530 | new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
| 1531 | if (!new) { |
| 1532 | kmem_cache_free(vm_region_jar, region); |
| 1533 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1535 | |
| 1536 | /* most fields are the same, copy all, and then fixup */ |
| 1537 | *new = *vma; |
| 1538 | *region = *vma->vm_region; |
| 1539 | new->vm_region = region; |
| 1540 | |
| 1541 | npages = (addr - vma->vm_start) >> PAGE_SHIFT; |
| 1542 | |
| 1543 | if (new_below) { |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1544 | region->vm_top = region->vm_end = new->vm_end = addr; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1545 | } else { |
| 1546 | region->vm_start = new->vm_start = addr; |
| 1547 | region->vm_pgoff = new->vm_pgoff += npages; |
| 1548 | } |
| 1549 | |
| 1550 | if (new->vm_ops && new->vm_ops->open) |
| 1551 | new->vm_ops->open(new); |
| 1552 | |
| 1553 | delete_vma_from_mm(vma); |
| 1554 | down_write(&nommu_region_sem); |
| 1555 | delete_nommu_region(vma->vm_region); |
| 1556 | if (new_below) { |
| 1557 | vma->vm_region->vm_start = vma->vm_start = addr; |
| 1558 | vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; |
| 1559 | } else { |
| 1560 | vma->vm_region->vm_end = vma->vm_end = addr; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1561 | vma->vm_region->vm_top = addr; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1562 | } |
| 1563 | add_nommu_region(vma->vm_region); |
| 1564 | add_nommu_region(new->vm_region); |
| 1565 | up_write(&nommu_region_sem); |
| 1566 | add_vma_to_mm(mm, vma); |
| 1567 | add_vma_to_mm(mm, new); |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * shrink a VMA by removing the specified chunk from either the beginning or |
| 1573 | * the end |
| 1574 | */ |
| 1575 | static int shrink_vma(struct mm_struct *mm, |
| 1576 | struct vm_area_struct *vma, |
| 1577 | unsigned long from, unsigned long to) |
| 1578 | { |
| 1579 | struct vm_region *region; |
| 1580 | |
| 1581 | kenter(""); |
| 1582 | |
| 1583 | /* adjust the VMA's pointers, which may reposition it in the MM's tree |
| 1584 | * and list */ |
| 1585 | delete_vma_from_mm(vma); |
| 1586 | if (from > vma->vm_start) |
| 1587 | vma->vm_end = from; |
| 1588 | else |
| 1589 | vma->vm_start = to; |
| 1590 | add_vma_to_mm(mm, vma); |
| 1591 | |
| 1592 | /* cut the backing region down to size */ |
| 1593 | region = vma->vm_region; |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1594 | BUG_ON(region->vm_usage != 1); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1595 | |
| 1596 | down_write(&nommu_region_sem); |
| 1597 | delete_nommu_region(region); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1598 | if (from > region->vm_start) { |
| 1599 | to = region->vm_top; |
| 1600 | region->vm_top = region->vm_end = from; |
| 1601 | } else { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1602 | region->vm_start = to; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1603 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1604 | add_nommu_region(region); |
| 1605 | up_write(&nommu_region_sem); |
| 1606 | |
| 1607 | free_page_series(from, to); |
| 1608 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1611 | /* |
| 1612 | * release a mapping |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1613 | * - under NOMMU conditions the chunk to be unmapped must be backed by a single |
| 1614 | * VMA, though it need not cover the whole VMA |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1615 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1616 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1618 | struct vm_area_struct *vma; |
| 1619 | struct rb_node *rb; |
| 1620 | unsigned long end = start + len; |
| 1621 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1623 | kenter(",%lx,%zx", start, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1625 | if (len == 0) |
| 1626 | return -EINVAL; |
| 1627 | |
| 1628 | /* find the first potentially overlapping VMA */ |
| 1629 | vma = find_vma(mm, start); |
| 1630 | if (!vma) { |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 1631 | static int limit = 0; |
| 1632 | if (limit < 5) { |
| 1633 | printk(KERN_WARNING |
| 1634 | "munmap of memory not mmapped by process %d" |
| 1635 | " (%s): 0x%lx-0x%lx\n", |
| 1636 | current->pid, current->comm, |
| 1637 | start, start + len - 1); |
| 1638 | limit++; |
| 1639 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1640 | return -EINVAL; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1643 | /* we're allowed to split an anonymous VMA but not a file-backed one */ |
| 1644 | if (vma->vm_file) { |
| 1645 | do { |
| 1646 | if (start > vma->vm_start) { |
| 1647 | kleave(" = -EINVAL [miss]"); |
| 1648 | return -EINVAL; |
| 1649 | } |
| 1650 | if (end == vma->vm_end) |
| 1651 | goto erase_whole_vma; |
| 1652 | rb = rb_next(&vma->vm_rb); |
| 1653 | vma = rb_entry(rb, struct vm_area_struct, vm_rb); |
| 1654 | } while (rb); |
| 1655 | kleave(" = -EINVAL [split file]"); |
| 1656 | return -EINVAL; |
| 1657 | } else { |
| 1658 | /* the chunk must be a subset of the VMA found */ |
| 1659 | if (start == vma->vm_start && end == vma->vm_end) |
| 1660 | goto erase_whole_vma; |
| 1661 | if (start < vma->vm_start || end > vma->vm_end) { |
| 1662 | kleave(" = -EINVAL [superset]"); |
| 1663 | return -EINVAL; |
| 1664 | } |
| 1665 | if (start & ~PAGE_MASK) { |
| 1666 | kleave(" = -EINVAL [unaligned start]"); |
| 1667 | return -EINVAL; |
| 1668 | } |
| 1669 | if (end != vma->vm_end && end & ~PAGE_MASK) { |
| 1670 | kleave(" = -EINVAL [unaligned split]"); |
| 1671 | return -EINVAL; |
| 1672 | } |
| 1673 | if (start != vma->vm_start && end != vma->vm_end) { |
| 1674 | ret = split_vma(mm, vma, start, 1); |
| 1675 | if (ret < 0) { |
| 1676 | kleave(" = %d [split]", ret); |
| 1677 | return ret; |
| 1678 | } |
| 1679 | } |
| 1680 | return shrink_vma(mm, vma, start, end); |
| 1681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1683 | erase_whole_vma: |
| 1684 | delete_vma_from_mm(vma); |
| 1685 | delete_vma(mm, vma); |
| 1686 | kleave(" = 0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 1689 | EXPORT_SYMBOL(do_munmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 1691 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1692 | { |
| 1693 | int ret; |
| 1694 | struct mm_struct *mm = current->mm; |
| 1695 | |
| 1696 | down_write(&mm->mmap_sem); |
| 1697 | ret = do_munmap(mm, addr, len); |
| 1698 | up_write(&mm->mmap_sem); |
| 1699 | return ret; |
| 1700 | } |
| 1701 | |
| 1702 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1703 | * release all the mappings made in a process's VM space |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1704 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1705 | void exit_mmap(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1707 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1709 | if (!mm) |
| 1710 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1712 | kenter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1714 | mm->total_vm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1716 | while ((vma = mm->mmap)) { |
| 1717 | mm->mmap = vma->vm_next; |
| 1718 | delete_vma_from_mm(vma); |
| 1719 | delete_vma(mm, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1721 | |
| 1722 | kleave(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | unsigned long do_brk(unsigned long addr, unsigned long len) |
| 1726 | { |
| 1727 | return -ENOMEM; |
| 1728 | } |
| 1729 | |
| 1730 | /* |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1731 | * expand (or shrink) an existing mapping, potentially moving it at the same |
| 1732 | * time (controlled by the MREMAP_MAYMOVE flag and available VM space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | * |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1734 | * under NOMMU conditions, we only permit changing a mapping's size, and only |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1735 | * as long as it stays within the region allocated by do_mmap_private() and the |
| 1736 | * block is not shareable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | * |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1738 | * MREMAP_FIXED is not supported under NOMMU conditions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | */ |
| 1740 | unsigned long do_mremap(unsigned long addr, |
| 1741 | unsigned long old_len, unsigned long new_len, |
| 1742 | unsigned long flags, unsigned long new_addr) |
| 1743 | { |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1744 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* insanity checks first */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1747 | if (old_len == 0 || new_len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | return (unsigned long) -EINVAL; |
| 1749 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1750 | if (addr & ~PAGE_MASK) |
| 1751 | return -EINVAL; |
| 1752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | if (flags & MREMAP_FIXED && new_addr != addr) |
| 1754 | return (unsigned long) -EINVAL; |
| 1755 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1756 | vma = find_vma_exact(current->mm, addr, old_len); |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1757 | if (!vma) |
| 1758 | return (unsigned long) -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1760 | if (vma->vm_end != vma->vm_start + old_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | return (unsigned long) -EFAULT; |
| 1762 | |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1763 | if (vma->vm_flags & VM_MAYSHARE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | return (unsigned long) -EPERM; |
| 1765 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1766 | if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | return (unsigned long) -ENOMEM; |
| 1768 | |
| 1769 | /* all checks complete - do it */ |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1770 | vma->vm_end = vma->vm_start + new_len; |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1771 | return vma->vm_start; |
| 1772 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 1773 | EXPORT_SYMBOL(do_mremap); |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1774 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 1775 | SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, |
| 1776 | unsigned long, new_len, unsigned long, flags, |
| 1777 | unsigned long, new_addr) |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1778 | { |
| 1779 | unsigned long ret; |
| 1780 | |
| 1781 | down_write(¤t->mm->mmap_sem); |
| 1782 | ret = do_mremap(addr, old_len, new_len, flags, new_addr); |
| 1783 | up_write(¤t->mm->mmap_sem); |
| 1784 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 1787 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, |
Hugh Dickins | deceb6c | 2005-10-29 18:16:33 -0700 | [diff] [blame] | 1788 | unsigned int foll_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | { |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | int remap_pfn_range(struct vm_area_struct *vma, unsigned long from, |
| 1794 | unsigned long to, unsigned long size, pgprot_t prot) |
| 1795 | { |
Greg Ungerer | 66aa2b4 | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 1796 | vma->vm_start = vma->vm_pgoff << PAGE_SHIFT; |
| 1797 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } |
Luke Yang | 22c4af4 | 2006-07-14 00:24:09 -0700 | [diff] [blame] | 1799 | EXPORT_SYMBOL(remap_pfn_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 1801 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
| 1802 | unsigned long pgoff) |
| 1803 | { |
| 1804 | unsigned int size = vma->vm_end - vma->vm_start; |
| 1805 | |
| 1806 | if (!(vma->vm_flags & VM_USERMAP)) |
| 1807 | return -EINVAL; |
| 1808 | |
| 1809 | vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); |
| 1810 | vma->vm_end = vma->vm_start + size; |
| 1811 | |
| 1812 | return 0; |
| 1813 | } |
| 1814 | EXPORT_SYMBOL(remap_vmalloc_range); |
| 1815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) |
| 1817 | { |
| 1818 | } |
| 1819 | |
| 1820 | unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr, |
| 1821 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 1822 | { |
| 1823 | return -ENOMEM; |
| 1824 | } |
| 1825 | |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 1826 | void arch_unmap_area(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | { |
| 1828 | } |
| 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | void unmap_mapping_range(struct address_space *mapping, |
| 1831 | loff_t const holebegin, loff_t const holelen, |
| 1832 | int even_cows) |
| 1833 | { |
| 1834 | } |
Luke Yang | 22c4af4 | 2006-07-14 00:24:09 -0700 | [diff] [blame] | 1835 | EXPORT_SYMBOL(unmap_mapping_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | /* |
| 1838 | * Check that a process has enough memory to allocate a new virtual |
| 1839 | * mapping. 0 means there is enough memory for the allocation to |
| 1840 | * succeed and -ENOMEM implies there is not. |
| 1841 | * |
| 1842 | * We currently support three overcommit policies, which are set via the |
| 1843 | * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting |
| 1844 | * |
| 1845 | * Strict overcommit modes added 2002 Feb 26 by Alan Cox. |
| 1846 | * Additional code 2002 Jul 20 by Robert Love. |
| 1847 | * |
| 1848 | * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. |
| 1849 | * |
| 1850 | * Note this is a helper function intended to be used by LSMs which |
| 1851 | * wish to use this logic. |
| 1852 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 1853 | int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | { |
| 1855 | unsigned long free, allowed; |
| 1856 | |
| 1857 | vm_acct_memory(pages); |
| 1858 | |
| 1859 | /* |
| 1860 | * Sometimes we want to use more memory than we have |
| 1861 | */ |
| 1862 | if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) |
| 1863 | return 0; |
| 1864 | |
| 1865 | if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { |
| 1866 | unsigned long n; |
| 1867 | |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 1868 | free = global_page_state(NR_FILE_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | free += nr_swap_pages; |
| 1870 | |
| 1871 | /* |
| 1872 | * Any slabs which are created with the |
| 1873 | * SLAB_RECLAIM_ACCOUNT flag claim to have contents |
| 1874 | * which are reclaimable, under pressure. The dentry |
| 1875 | * cache and most inode caches should fall into this |
| 1876 | */ |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 1877 | free += global_page_state(NR_SLAB_RECLAIMABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
| 1879 | /* |
| 1880 | * Leave the last 3% for root |
| 1881 | */ |
| 1882 | if (!cap_sys_admin) |
| 1883 | free -= free / 32; |
| 1884 | |
| 1885 | if (free > pages) |
| 1886 | return 0; |
| 1887 | |
| 1888 | /* |
| 1889 | * nr_free_pages() is very expensive on large systems, |
| 1890 | * only call if we're about to fail. |
| 1891 | */ |
| 1892 | n = nr_free_pages(); |
Hideo AOKI | d5ddc79 | 2006-04-10 22:53:01 -0700 | [diff] [blame] | 1893 | |
| 1894 | /* |
| 1895 | * Leave reserved pages. The pages are not for anonymous pages. |
| 1896 | */ |
| 1897 | if (n <= totalreserve_pages) |
| 1898 | goto error; |
| 1899 | else |
| 1900 | n -= totalreserve_pages; |
| 1901 | |
| 1902 | /* |
| 1903 | * Leave the last 3% for root |
| 1904 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | if (!cap_sys_admin) |
| 1906 | n -= n / 32; |
| 1907 | free += n; |
| 1908 | |
| 1909 | if (free > pages) |
| 1910 | return 0; |
Hideo AOKI | d5ddc79 | 2006-04-10 22:53:01 -0700 | [diff] [blame] | 1911 | |
| 1912 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | allowed = totalram_pages * sysctl_overcommit_ratio / 100; |
| 1916 | /* |
| 1917 | * Leave the last 3% for root |
| 1918 | */ |
| 1919 | if (!cap_sys_admin) |
| 1920 | allowed -= allowed / 32; |
| 1921 | allowed += total_swap_pages; |
| 1922 | |
| 1923 | /* Don't let a single process grow too big: |
| 1924 | leave 3% of the size of this process for other processes */ |
Alan Cox | 731572d | 2008-10-29 14:01:20 -0700 | [diff] [blame] | 1925 | if (mm) |
| 1926 | allowed -= mm->total_vm / 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 1928 | if (percpu_counter_read_positive(&vm_committed_as) < allowed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | return 0; |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 1930 | |
Hideo AOKI | d5ddc79 | 2006-04-10 22:53:01 -0700 | [diff] [blame] | 1931 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | vm_unacct_memory(pages); |
| 1933 | |
| 1934 | return -ENOMEM; |
| 1935 | } |
| 1936 | |
| 1937 | int in_gate_area_no_task(unsigned long addr) |
| 1938 | { |
| 1939 | return 0; |
| 1940 | } |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 1941 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1942 | int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 1943 | { |
| 1944 | BUG(); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1945 | return 0; |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 1946 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 1947 | EXPORT_SYMBOL(filemap_fault); |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1948 | |
| 1949 | /* |
| 1950 | * Access another process' address space. |
| 1951 | * - source/target buffer must be kernel space |
| 1952 | */ |
| 1953 | int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) |
| 1954 | { |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1955 | struct vm_area_struct *vma; |
| 1956 | struct mm_struct *mm; |
| 1957 | |
| 1958 | if (addr + len < addr) |
| 1959 | return 0; |
| 1960 | |
| 1961 | mm = get_task_mm(tsk); |
| 1962 | if (!mm) |
| 1963 | return 0; |
| 1964 | |
| 1965 | down_read(&mm->mmap_sem); |
| 1966 | |
| 1967 | /* the access must start within one of the target process's mappings */ |
David Howells | 0159b14 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 1968 | vma = find_vma(mm, addr); |
| 1969 | if (vma) { |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1970 | /* don't overrun this mapping */ |
| 1971 | if (addr + len >= vma->vm_end) |
| 1972 | len = vma->vm_end - addr; |
| 1973 | |
| 1974 | /* only read or write mappings where it is permitted */ |
David Howells | d00c7b99 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 1975 | if (write && vma->vm_flags & VM_MAYWRITE) |
Jie Zhang | 7959722 | 2010-01-06 17:23:28 +0000 | [diff] [blame] | 1976 | copy_to_user_page(vma, NULL, addr, |
| 1977 | (void *) addr, buf, len); |
David Howells | d00c7b99 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 1978 | else if (!write && vma->vm_flags & VM_MAYREAD) |
Jie Zhang | 7959722 | 2010-01-06 17:23:28 +0000 | [diff] [blame] | 1979 | copy_from_user_page(vma, NULL, addr, |
| 1980 | buf, (void *) addr, len); |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1981 | else |
| 1982 | len = 0; |
| 1983 | } else { |
| 1984 | len = 0; |
| 1985 | } |
| 1986 | |
| 1987 | up_read(&mm->mmap_sem); |
| 1988 | mmput(mm); |
| 1989 | return len; |
| 1990 | } |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1991 | |
| 1992 | /** |
| 1993 | * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode |
| 1994 | * @inode: The inode to check |
| 1995 | * @size: The current filesize of the inode |
| 1996 | * @newsize: The proposed filesize of the inode |
| 1997 | * |
| 1998 | * Check the shared mappings on an inode on behalf of a shrinking truncate to |
| 1999 | * make sure that that any outstanding VMAs aren't broken and then shrink the |
| 2000 | * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't |
| 2001 | * automatically grant mappings that are too large. |
| 2002 | */ |
| 2003 | int nommu_shrink_inode_mappings(struct inode *inode, size_t size, |
| 2004 | size_t newsize) |
| 2005 | { |
| 2006 | struct vm_area_struct *vma; |
| 2007 | struct prio_tree_iter iter; |
| 2008 | struct vm_region *region; |
| 2009 | pgoff_t low, high; |
| 2010 | size_t r_size, r_top; |
| 2011 | |
| 2012 | low = newsize >> PAGE_SHIFT; |
| 2013 | high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 2014 | |
| 2015 | down_write(&nommu_region_sem); |
| 2016 | |
| 2017 | /* search for VMAs that fall within the dead zone */ |
| 2018 | vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap, |
| 2019 | low, high) { |
| 2020 | /* found one - only interested if it's shared out of the page |
| 2021 | * cache */ |
| 2022 | if (vma->vm_flags & VM_SHARED) { |
| 2023 | up_write(&nommu_region_sem); |
| 2024 | return -ETXTBSY; /* not quite true, but near enough */ |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | /* reduce any regions that overlap the dead zone - if in existence, |
| 2029 | * these will be pointed to by VMAs that don't overlap the dead zone |
| 2030 | * |
| 2031 | * we don't check for any regions that start beyond the EOF as there |
| 2032 | * shouldn't be any |
| 2033 | */ |
| 2034 | vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap, |
| 2035 | 0, ULONG_MAX) { |
| 2036 | if (!(vma->vm_flags & VM_SHARED)) |
| 2037 | continue; |
| 2038 | |
| 2039 | region = vma->vm_region; |
| 2040 | r_size = region->vm_top - region->vm_start; |
| 2041 | r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; |
| 2042 | |
| 2043 | if (r_top > newsize) { |
| 2044 | region->vm_top -= r_top - newsize; |
| 2045 | if (region->vm_end > region->vm_top) |
| 2046 | region->vm_end = region->vm_top; |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | up_write(&nommu_region_sem); |
| 2051 | return 0; |
| 2052 | } |