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