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