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