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