Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/nommu.c |
| 4 | * |
| 5 | * Replacement code for mm functions to support CPU's that don't |
| 6 | * have any form of memory management unit (thus no virtual memory). |
| 7 | * |
Stephen Kitt | dd19d29 | 2020-08-12 11:22:30 +0200 | [diff] [blame] | 8 | * See Documentation/admin-guide/mm/nommu-mmap.rst |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 10 | * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com> |
| 12 | * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org> |
| 13 | * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com> |
Paul Mundt | 29c185e | 2010-12-24 12:08:30 +0900 | [diff] [blame] | 14 | * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Mitchel Humpherys | b1de0d1 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 19 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/mm.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 21 | #include <linux/sched/mm.h> |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 22 | #include <linux/vmacache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/mman.h> |
| 24 | #include <linux/swap.h> |
| 25 | #include <linux/file.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/pagemap.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Gideon Israel Dsouza | 3b32123 | 2014-04-07 15:37:26 -0700 | [diff] [blame] | 31 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/mount.h> |
| 33 | #include <linux/personality.h> |
| 34 | #include <linux/security.h> |
| 35 | #include <linux/syscalls.h> |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 36 | #include <linux/audit.h> |
Mitchel Humpherys | b1de0d1 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 37 | #include <linux/printk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 39 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/tlb.h> |
| 41 | #include <asm/tlbflush.h> |
Bernd Schmidt | eb8cdec | 2009-09-21 17:03:57 -0700 | [diff] [blame] | 42 | #include <asm/mmu_context.h> |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 43 | #include "internal.h" |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | void *high_memory; |
Arnd Bergmann | 944b687 | 2015-02-05 12:25:12 -0800 | [diff] [blame] | 46 | EXPORT_SYMBOL(high_memory); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct page *mem_map; |
| 48 | unsigned long max_mapnr; |
gchen gchen | 5b8bf30 | 2015-03-12 16:26:05 -0700 | [diff] [blame] | 49 | EXPORT_SYMBOL(max_mapnr); |
Hugh Dickins | 4266c97 | 2009-09-23 17:05:53 +0100 | [diff] [blame] | 50 | unsigned long highest_memmap_pfn; |
David Howells | fc4d5c2 | 2009-05-06 16:03:05 -0700 | [diff] [blame] | 51 | int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int heap_stack_gap = 0; |
| 53 | |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 54 | atomic_long_t mmap_pages_allocated; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | EXPORT_SYMBOL(mem_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 58 | /* list of mapped, potentially shareable regions */ |
| 59 | static struct kmem_cache *vm_region_jar; |
| 60 | struct rb_root nommu_region_tree = RB_ROOT; |
| 61 | DECLARE_RWSEM(nommu_region_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 63 | const struct vm_operations_struct generic_file_vm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * Return the total memory allocated for this pointer, not |
| 68 | * just what the caller asked for. |
| 69 | * |
| 70 | * Doesn't have to be accurate, i.e. may have races. |
| 71 | */ |
| 72 | unsigned int kobjsize(const void *objp) |
| 73 | { |
| 74 | struct page *page; |
| 75 | |
Michael Hennerich | 4016a13 | 2008-04-28 02:13:38 -0700 | [diff] [blame] | 76 | /* |
| 77 | * If the object we have should not have ksize performed on it, |
| 78 | * return size of 0 |
| 79 | */ |
Paul Mundt | 5a1603b | 2008-06-12 16:29:55 +0900 | [diff] [blame] | 80 | if (!objp || !virt_addr_valid(objp)) |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | |
| 83 | page = virt_to_head_page(objp); |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * If the allocator sets PageSlab, we know the pointer came from |
| 87 | * kmalloc(). |
| 88 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (PageSlab(page)) |
| 90 | return ksize(objp); |
| 91 | |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 92 | /* |
Paul Mundt | ab2e83e | 2009-01-08 12:04:48 +0000 | [diff] [blame] | 93 | * If it's not a compound page, see if we have a matching VMA |
| 94 | * region. This test is intentionally done in reverse order, |
| 95 | * so if there's no VMA, we still fall through and hand back |
| 96 | * PAGE_SIZE for 0-order pages. |
| 97 | */ |
| 98 | if (!PageCompound(page)) { |
| 99 | struct vm_area_struct *vma; |
| 100 | |
| 101 | vma = find_vma(current->mm, (unsigned long)objp); |
| 102 | if (vma) |
| 103 | return vma->vm_end - vma->vm_start; |
| 104 | } |
| 105 | |
| 106 | /* |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 107 | * The ksize() function is only guaranteed to work for pointers |
Paul Mundt | 5a1603b | 2008-06-12 16:29:55 +0900 | [diff] [blame] | 108 | * returned by kmalloc(). So handle arbitrary pointers here. |
Paul Mundt | 6cfd53fc | 2008-06-05 22:46:08 -0700 | [diff] [blame] | 109 | */ |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 110 | return page_size(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Paul Mundt | dfc2f91 | 2009-06-26 04:31:57 +0900 | [diff] [blame] | 113 | /** |
| 114 | * follow_pfn - look up PFN at a user virtual address |
| 115 | * @vma: memory mapping |
| 116 | * @address: user virtual address |
| 117 | * @pfn: location to store found PFN |
| 118 | * |
| 119 | * Only IO mappings and raw PFN mappings are allowed. |
| 120 | * |
| 121 | * Returns zero and the pfn at @pfn on success, -ve otherwise. |
| 122 | */ |
| 123 | int follow_pfn(struct vm_area_struct *vma, unsigned long address, |
| 124 | unsigned long *pfn) |
| 125 | { |
| 126 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | *pfn = address >> PAGE_SHIFT; |
| 130 | return 0; |
| 131 | } |
| 132 | EXPORT_SYMBOL(follow_pfn); |
| 133 | |
Joonsoo Kim | f1c4069 | 2013-04-29 15:07:37 -0700 | [diff] [blame] | 134 | LIST_HEAD(vmap_area_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 136 | void vfree(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | kfree(addr); |
| 139 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 140 | EXPORT_SYMBOL(vfree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 142 | void *__vmalloc(unsigned long size, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | /* |
Robert P. J. Day | 8518609 | 2007-10-19 23:11:38 +0200 | [diff] [blame] | 145 | * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() |
| 146 | * returns only a logical address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | */ |
Nick Piggin | 8409751 | 2006-03-22 00:08:34 -0800 | [diff] [blame] | 148 | return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 150 | EXPORT_SYMBOL(__vmalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Christoph Hellwig | 041de93 | 2020-06-01 21:52:02 -0700 | [diff] [blame] | 152 | void *__vmalloc_node_range(unsigned long size, unsigned long align, |
| 153 | unsigned long start, unsigned long end, gfp_t gfp_mask, |
| 154 | pgprot_t prot, unsigned long vm_flags, int node, |
| 155 | const void *caller) |
| 156 | { |
| 157 | return __vmalloc(size, gfp_mask); |
| 158 | } |
| 159 | |
Christoph Hellwig | 2b90594 | 2020-06-01 21:51:53 -0700 | [diff] [blame] | 160 | void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, |
| 161 | int node, const void *caller) |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 162 | { |
Christoph Hellwig | 2b90594 | 2020-06-01 21:51:53 -0700 | [diff] [blame] | 163 | return __vmalloc(size, gfp_mask); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Andrii Nakryiko | ed81745 | 2019-11-23 14:08:35 -0800 | [diff] [blame] | 166 | static void *__vmalloc_user_flags(unsigned long size, gfp_t flags) |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 167 | { |
| 168 | void *ret; |
| 169 | |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 170 | ret = __vmalloc(size, flags); |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 171 | if (ret) { |
| 172 | struct vm_area_struct *vma; |
| 173 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 174 | mmap_write_lock(current->mm); |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 175 | vma = find_vma(current->mm, (unsigned long)ret); |
| 176 | if (vma) |
| 177 | vma->vm_flags |= VM_USERMAP; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 178 | mmap_write_unlock(current->mm); |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return ret; |
| 182 | } |
Andrii Nakryiko | ed81745 | 2019-11-23 14:08:35 -0800 | [diff] [blame] | 183 | |
| 184 | void *vmalloc_user(unsigned long size) |
| 185 | { |
| 186 | return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO); |
| 187 | } |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 188 | EXPORT_SYMBOL(vmalloc_user); |
| 189 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 190 | struct page *vmalloc_to_page(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | return virt_to_page(addr); |
| 193 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 194 | EXPORT_SYMBOL(vmalloc_to_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 196 | unsigned long vmalloc_to_pfn(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | return page_to_pfn(virt_to_page(addr)); |
| 199 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 200 | EXPORT_SYMBOL(vmalloc_to_pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | long vread(char *buf, char *addr, unsigned long count) |
| 203 | { |
Chen Gang | 9bde916 | 2013-07-03 15:02:36 -0700 | [diff] [blame] | 204 | /* Don't allow overflow */ |
| 205 | if ((unsigned long) buf + count < count) |
| 206 | count = -(unsigned long) buf; |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | memcpy(buf, addr, count); |
| 209 | return count; |
| 210 | } |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* |
Masahiro Yamada | e1c0506 | 2015-07-07 10:14:59 +0900 | [diff] [blame] | 213 | * vmalloc - allocate virtually contiguous memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * |
| 215 | * @size: allocation size |
| 216 | * |
| 217 | * Allocate enough pages to cover @size from the page level |
Masahiro Yamada | e1c0506 | 2015-07-07 10:14:59 +0900 | [diff] [blame] | 218 | * allocator and map them into contiguous kernel virtual space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 220 | * For tight control over page level allocator and protection flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | * use __vmalloc() instead. |
| 222 | */ |
| 223 | void *vmalloc(unsigned long size) |
| 224 | { |
Chen Li | 176056f | 2021-06-30 18:52:14 -0700 | [diff] [blame] | 225 | return __vmalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
Andrew Morton | f613888 | 2006-02-28 16:59:18 -0800 | [diff] [blame] | 227 | EXPORT_SYMBOL(vmalloc); |
| 228 | |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 229 | /* |
Masahiro Yamada | e1c0506 | 2015-07-07 10:14:59 +0900 | [diff] [blame] | 230 | * vzalloc - allocate virtually contiguous memory with zero fill |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 231 | * |
| 232 | * @size: allocation size |
| 233 | * |
| 234 | * Allocate enough pages to cover @size from the page level |
Masahiro Yamada | e1c0506 | 2015-07-07 10:14:59 +0900 | [diff] [blame] | 235 | * allocator and map them into contiguous kernel virtual space. |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 236 | * The memory allocated is set to zero. |
| 237 | * |
| 238 | * For tight control over page level allocator and protection flags |
| 239 | * use __vmalloc() instead. |
| 240 | */ |
| 241 | void *vzalloc(unsigned long size) |
| 242 | { |
Chen Li | 176056f | 2021-06-30 18:52:14 -0700 | [diff] [blame] | 243 | return __vmalloc(size, GFP_KERNEL | __GFP_ZERO); |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 244 | } |
| 245 | EXPORT_SYMBOL(vzalloc); |
| 246 | |
| 247 | /** |
| 248 | * vmalloc_node - allocate memory on a specific node |
| 249 | * @size: allocation size |
| 250 | * @node: numa node |
| 251 | * |
| 252 | * Allocate enough pages to cover @size from the page level |
| 253 | * allocator and map them into contiguous kernel virtual space. |
| 254 | * |
| 255 | * For tight control over page level allocator and protection flags |
| 256 | * use __vmalloc() instead. |
| 257 | */ |
Andrew Morton | f613888 | 2006-02-28 16:59:18 -0800 | [diff] [blame] | 258 | void *vmalloc_node(unsigned long size, int node) |
| 259 | { |
| 260 | return vmalloc(size); |
| 261 | } |
Paul Mundt | 9a14f65 | 2010-12-24 11:50:34 +0900 | [diff] [blame] | 262 | EXPORT_SYMBOL(vmalloc_node); |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * vzalloc_node - allocate memory on a specific node with zero fill |
| 266 | * @size: allocation size |
| 267 | * @node: numa node |
| 268 | * |
| 269 | * Allocate enough pages to cover @size from the page level |
| 270 | * allocator and map them into contiguous kernel virtual space. |
| 271 | * The memory allocated is set to zero. |
| 272 | * |
| 273 | * For tight control over page level allocator and protection flags |
| 274 | * use __vmalloc() instead. |
| 275 | */ |
| 276 | void *vzalloc_node(unsigned long size, int node) |
| 277 | { |
| 278 | return vzalloc(size); |
| 279 | } |
| 280 | EXPORT_SYMBOL(vzalloc_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Paul Mundt | 1af446e | 2008-08-04 16:01:47 +0900 | [diff] [blame] | 282 | /** |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 283 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * @size: allocation size |
| 285 | * |
| 286 | * Allocate enough 32bit PA addressable pages to cover @size from the |
Masahiro Yamada | e1c0506 | 2015-07-07 10:14:59 +0900 | [diff] [blame] | 287 | * page level allocator and map them into contiguous kernel virtual space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | */ |
| 289 | void *vmalloc_32(unsigned long size) |
| 290 | { |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 291 | return __vmalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 293 | EXPORT_SYMBOL(vmalloc_32); |
| 294 | |
| 295 | /** |
| 296 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory |
| 297 | * @size: allocation size |
| 298 | * |
| 299 | * The resulting memory area is 32bit addressable and zeroed so it can be |
| 300 | * mapped to userspace without leaking data. |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 301 | * |
| 302 | * VM_USERMAP is set on the corresponding VMA so that subsequent calls to |
| 303 | * remap_vmalloc_range() are permissible. |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 304 | */ |
| 305 | void *vmalloc_32_user(unsigned long size) |
| 306 | { |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 307 | /* |
| 308 | * We'll have to sort out the ZONE_DMA bits for 64-bit, |
| 309 | * but for now this can simply use vmalloc_user() directly. |
| 310 | */ |
| 311 | return vmalloc_user(size); |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 312 | } |
| 313 | EXPORT_SYMBOL(vmalloc_32_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) |
| 316 | { |
| 317 | BUG(); |
| 318 | return NULL; |
| 319 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 320 | EXPORT_SYMBOL(vmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 322 | void vunmap(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | BUG(); |
| 325 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 326 | EXPORT_SYMBOL(vunmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Christoph Hellwig | d4efd79 | 2020-06-01 21:51:27 -0700 | [diff] [blame] | 328 | void *vm_map_ram(struct page **pages, unsigned int count, int node) |
Paul Mundt | eb6434d | 2009-01-21 17:45:47 +0900 | [diff] [blame] | 329 | { |
| 330 | BUG(); |
| 331 | return NULL; |
| 332 | } |
| 333 | EXPORT_SYMBOL(vm_map_ram); |
| 334 | |
| 335 | void vm_unmap_ram(const void *mem, unsigned int count) |
| 336 | { |
| 337 | BUG(); |
| 338 | } |
| 339 | EXPORT_SYMBOL(vm_unmap_ram); |
| 340 | |
| 341 | void vm_unmap_aliases(void) |
| 342 | { |
| 343 | } |
| 344 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); |
| 345 | |
Paul Mundt | 29c185e | 2010-12-24 12:08:30 +0900 | [diff] [blame] | 346 | void free_vm_area(struct vm_struct *area) |
| 347 | { |
| 348 | BUG(); |
| 349 | } |
| 350 | EXPORT_SYMBOL_GPL(free_vm_area); |
| 351 | |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 352 | int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, |
| 353 | struct page *page) |
| 354 | { |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | EXPORT_SYMBOL(vm_insert_page); |
| 358 | |
Souptick Joarder | a667d74 | 2019-05-13 17:21:56 -0700 | [diff] [blame] | 359 | int vm_map_pages(struct vm_area_struct *vma, struct page **pages, |
| 360 | unsigned long num) |
| 361 | { |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | EXPORT_SYMBOL(vm_map_pages); |
| 365 | |
| 366 | int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages, |
| 367 | unsigned long num) |
| 368 | { |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | EXPORT_SYMBOL(vm_map_pages_zero); |
| 372 | |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 373 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | * sys_brk() for the most part doesn't need the global kernel |
| 375 | * lock, except when an application is doing something nasty |
| 376 | * like trying to un-brk an area that has already been mapped |
| 377 | * to a regular file. in this case, the unmapping will need |
| 378 | * to invoke file system routines that need the global lock. |
| 379 | */ |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 380 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | struct mm_struct *mm = current->mm; |
| 383 | |
| 384 | if (brk < mm->start_brk || brk > mm->context.end_brk) |
| 385 | return mm->brk; |
| 386 | |
| 387 | if (mm->brk == brk) |
| 388 | return mm->brk; |
| 389 | |
| 390 | /* |
| 391 | * Always allow shrinking brk |
| 392 | */ |
| 393 | if (brk <= mm->brk) { |
| 394 | mm->brk = brk; |
| 395 | return brk; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Ok, looks good - let it rip. |
| 400 | */ |
Christoph Hellwig | a75a2df | 2020-06-07 21:42:49 -0700 | [diff] [blame] | 401 | flush_icache_user_range(mm->brk, brk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return mm->brk = brk; |
| 403 | } |
| 404 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 405 | /* |
seokhoon.yoon | 3edf41d | 2017-02-24 14:56:44 -0800 | [diff] [blame] | 406 | * initialise the percpu counter for VM and region record slabs |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 407 | */ |
| 408 | void __init mmap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 410 | int ret; |
| 411 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 412 | ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); |
KOSAKI Motohiro | 00a62ce | 2009-04-30 15:08:51 -0700 | [diff] [blame] | 413 | VM_BUG_ON(ret); |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 414 | vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 417 | /* |
| 418 | * validate the region tree |
| 419 | * - the caller must hold the region lock |
| 420 | */ |
| 421 | #ifdef CONFIG_DEBUG_NOMMU_REGIONS |
| 422 | static noinline void validate_nommu_regions(void) |
| 423 | { |
| 424 | struct vm_region *region, *last; |
| 425 | struct rb_node *p, *lastp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 427 | lastp = rb_first(&nommu_region_tree); |
| 428 | if (!lastp) |
| 429 | return; |
| 430 | |
| 431 | last = rb_entry(lastp, struct vm_region, vm_rb); |
Geliang Tang | c9427bc | 2015-11-05 18:48:38 -0800 | [diff] [blame] | 432 | BUG_ON(last->vm_end <= last->vm_start); |
| 433 | BUG_ON(last->vm_top < last->vm_end); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 434 | |
| 435 | while ((p = rb_next(lastp))) { |
| 436 | region = rb_entry(p, struct vm_region, vm_rb); |
| 437 | last = rb_entry(lastp, struct vm_region, vm_rb); |
| 438 | |
Geliang Tang | c9427bc | 2015-11-05 18:48:38 -0800 | [diff] [blame] | 439 | BUG_ON(region->vm_end <= region->vm_start); |
| 440 | BUG_ON(region->vm_top < region->vm_end); |
| 441 | BUG_ON(region->vm_start < last->vm_top); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 442 | |
| 443 | lastp = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 446 | #else |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 447 | static void validate_nommu_regions(void) |
| 448 | { |
| 449 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 450 | #endif |
| 451 | |
| 452 | /* |
| 453 | * add a region into the global tree |
| 454 | */ |
| 455 | static void add_nommu_region(struct vm_region *region) |
| 456 | { |
| 457 | struct vm_region *pregion; |
| 458 | struct rb_node **p, *parent; |
| 459 | |
| 460 | validate_nommu_regions(); |
| 461 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 462 | parent = NULL; |
| 463 | p = &nommu_region_tree.rb_node; |
| 464 | while (*p) { |
| 465 | parent = *p; |
| 466 | pregion = rb_entry(parent, struct vm_region, vm_rb); |
| 467 | if (region->vm_start < pregion->vm_start) |
| 468 | p = &(*p)->rb_left; |
| 469 | else if (region->vm_start > pregion->vm_start) |
| 470 | p = &(*p)->rb_right; |
| 471 | else if (pregion == region) |
| 472 | return; |
| 473 | else |
| 474 | BUG(); |
| 475 | } |
| 476 | |
| 477 | rb_link_node(®ion->vm_rb, parent, p); |
| 478 | rb_insert_color(®ion->vm_rb, &nommu_region_tree); |
| 479 | |
| 480 | validate_nommu_regions(); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * delete a region from the global tree |
| 485 | */ |
| 486 | static void delete_nommu_region(struct vm_region *region) |
| 487 | { |
| 488 | BUG_ON(!nommu_region_tree.rb_node); |
| 489 | |
| 490 | validate_nommu_regions(); |
| 491 | rb_erase(®ion->vm_rb, &nommu_region_tree); |
| 492 | validate_nommu_regions(); |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * free a contiguous series of pages |
| 497 | */ |
| 498 | static void free_page_series(unsigned long from, unsigned long to) |
| 499 | { |
| 500 | for (; from < to; from += PAGE_SIZE) { |
| 501 | struct page *page = virt_to_page(from); |
| 502 | |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 503 | atomic_long_dec(&mmap_pages_allocated); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 504 | put_page(page); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * release a reference to a region |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 510 | * - the caller must hold the region semaphore for writing, which this releases |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 511 | * - 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] | 512 | * will equal vm_start |
| 513 | */ |
| 514 | static void __put_nommu_region(struct vm_region *region) |
| 515 | __releases(nommu_region_sem) |
| 516 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 517 | BUG_ON(!nommu_region_tree.rb_node); |
| 518 | |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 519 | if (--region->vm_usage == 0) { |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 520 | if (region->vm_top > region->vm_start) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 521 | delete_nommu_region(region); |
| 522 | up_write(&nommu_region_sem); |
| 523 | |
| 524 | if (region->vm_file) |
| 525 | fput(region->vm_file); |
| 526 | |
| 527 | /* IO memory and memory shared directly out of the pagecache |
| 528 | * from ramfs/tmpfs mustn't be released here */ |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 529 | if (region->vm_flags & VM_MAPPED_COPY) |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 530 | free_page_series(region->vm_start, region->vm_top); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 531 | kmem_cache_free(vm_region_jar, region); |
| 532 | } else { |
| 533 | up_write(&nommu_region_sem); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * release a reference to a region |
| 539 | */ |
| 540 | static void put_nommu_region(struct vm_region *region) |
| 541 | { |
| 542 | down_write(&nommu_region_sem); |
| 543 | __put_nommu_region(region); |
| 544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 546 | /* |
| 547 | * 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] | 548 | * and tree and add to the address space's page tree also if not an anonymous |
| 549 | * page |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 550 | * - should be called with mm->mmap_lock held writelocked |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 551 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 552 | 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] | 553 | { |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 554 | struct vm_area_struct *pvma, *prev; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 555 | struct address_space *mapping; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 556 | struct rb_node **p, *parent, *rb_prev; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 557 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 558 | BUG_ON(!vma->vm_region); |
| 559 | |
| 560 | mm->map_count++; |
| 561 | vma->vm_mm = mm; |
| 562 | |
| 563 | /* add the VMA to the mapping */ |
| 564 | if (vma->vm_file) { |
| 565 | mapping = vma->vm_file->f_mapping; |
| 566 | |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 567 | i_mmap_lock_write(mapping); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 568 | flush_dcache_mmap_lock(mapping); |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 569 | vma_interval_tree_insert(vma, &mapping->i_mmap); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 570 | flush_dcache_mmap_unlock(mapping); |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 571 | i_mmap_unlock_write(mapping); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* add the VMA to the tree */ |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 575 | parent = rb_prev = NULL; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 576 | p = &mm->mm_rb.rb_node; |
| 577 | while (*p) { |
| 578 | parent = *p; |
| 579 | pvma = rb_entry(parent, struct vm_area_struct, vm_rb); |
| 580 | |
| 581 | /* sort by: start addr, end addr, VMA struct addr in that order |
| 582 | * (the latter is necessary as we may get identical VMAs) */ |
| 583 | if (vma->vm_start < pvma->vm_start) |
| 584 | p = &(*p)->rb_left; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 585 | else if (vma->vm_start > pvma->vm_start) { |
| 586 | rb_prev = parent; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 587 | p = &(*p)->rb_right; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 588 | } else if (vma->vm_end < pvma->vm_end) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 589 | p = &(*p)->rb_left; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 590 | else if (vma->vm_end > pvma->vm_end) { |
| 591 | rb_prev = parent; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 592 | p = &(*p)->rb_right; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 593 | } else if (vma < pvma) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 594 | p = &(*p)->rb_left; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 595 | else if (vma > pvma) { |
| 596 | rb_prev = parent; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 597 | p = &(*p)->rb_right; |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 598 | } else |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 599 | BUG(); |
| 600 | } |
| 601 | |
| 602 | rb_link_node(&vma->vm_rb, parent, p); |
| 603 | rb_insert_color(&vma->vm_rb, &mm->mm_rb); |
| 604 | |
| 605 | /* add VMA to the VMA list also */ |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 606 | prev = NULL; |
| 607 | if (rb_prev) |
| 608 | prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 609 | |
Wei Yang | aba6dfb | 2019-11-30 17:50:53 -0800 | [diff] [blame] | 610 | __vma_link_list(mm, vma, prev); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* |
| 614 | * delete a VMA from its owning mm_struct and address space |
| 615 | */ |
| 616 | static void delete_vma_from_mm(struct vm_area_struct *vma) |
| 617 | { |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 618 | int i; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 619 | struct address_space *mapping; |
| 620 | struct mm_struct *mm = vma->vm_mm; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 621 | struct task_struct *curr = current; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 622 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 623 | mm->map_count--; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 624 | for (i = 0; i < VMACACHE_SIZE; i++) { |
| 625 | /* if the vma is cached, invalidate the entire cache */ |
Ingo Molnar | 314ff78 | 2017-02-03 11:03:31 +0100 | [diff] [blame] | 626 | if (curr->vmacache.vmas[i] == vma) { |
Steven Miao | e020d5b | 2014-06-23 13:22:02 -0700 | [diff] [blame] | 627 | vmacache_invalidate(mm); |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 631 | |
| 632 | /* remove the VMA from the mapping */ |
| 633 | if (vma->vm_file) { |
| 634 | mapping = vma->vm_file->f_mapping; |
| 635 | |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 636 | i_mmap_lock_write(mapping); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 637 | flush_dcache_mmap_lock(mapping); |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 638 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 639 | flush_dcache_mmap_unlock(mapping); |
Davidlohr Bueso | 83cde9e | 2014-12-12 16:54:21 -0800 | [diff] [blame] | 640 | i_mmap_unlock_write(mapping); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* remove from the MM's tree and list */ |
| 644 | rb_erase(&vma->vm_rb, &mm->mm_rb); |
Namhyung Kim | b951bf2 | 2011-05-24 17:11:23 -0700 | [diff] [blame] | 645 | |
Wei Yang | 1b9fc5b2 | 2019-11-30 17:50:49 -0800 | [diff] [blame] | 646 | __vma_unlink_list(mm, vma); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /* |
| 650 | * destroy a VMA record |
| 651 | */ |
| 652 | static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) |
| 653 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 654 | if (vma->vm_ops && vma->vm_ops->close) |
| 655 | vma->vm_ops->close(vma); |
Konstantin Khlebnikov | e9714ac | 2012-10-08 16:28:54 -0700 | [diff] [blame] | 656 | if (vma->vm_file) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 657 | fput(vma->vm_file); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 658 | put_nommu_region(vma->vm_region); |
Linus Torvalds | 3928d4f | 2018-07-21 13:48:51 -0700 | [diff] [blame] | 659 | vm_area_free(vma); |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
| 663 | * look up the first VMA in which addr resides, NULL if none |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 664 | * - should be called with mm->mmap_lock at least held readlocked |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 665 | */ |
| 666 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) |
| 667 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 668 | struct vm_area_struct *vma; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 669 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 670 | /* check the cache first */ |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 671 | vma = vmacache_find(mm, addr); |
| 672 | if (likely(vma)) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 673 | return vma; |
| 674 | |
Namhyung Kim | e922c4c | 2011-05-24 17:11:24 -0700 | [diff] [blame] | 675 | /* trawl the list (there may be multiple mappings in which addr |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 676 | * resides) */ |
Namhyung Kim | e922c4c | 2011-05-24 17:11:24 -0700 | [diff] [blame] | 677 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 678 | if (vma->vm_start > addr) |
| 679 | return NULL; |
| 680 | if (vma->vm_end > addr) { |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 681 | vmacache_update(addr, vma); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 682 | return vma; |
| 683 | } |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 684 | } |
| 685 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 686 | return NULL; |
| 687 | } |
| 688 | EXPORT_SYMBOL(find_vma); |
| 689 | |
| 690 | /* |
David Howells | 930e652 | 2006-09-27 01:50:22 -0700 | [diff] [blame] | 691 | * find a VMA |
| 692 | * - we don't extend stack VMAs under NOMMU conditions |
| 693 | */ |
| 694 | struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) |
| 695 | { |
David Howells | 7561e8c | 2010-03-25 16:48:38 +0000 | [diff] [blame] | 696 | return find_vma(mm, addr); |
David Howells | 930e652 | 2006-09-27 01:50:22 -0700 | [diff] [blame] | 697 | } |
| 698 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 699 | /* |
| 700 | * expand a stack to a given address |
| 701 | * - not supported under NOMMU conditions |
| 702 | */ |
Greg Ungerer | 57c8f63 | 2007-07-15 23:38:28 -0700 | [diff] [blame] | 703 | int expand_stack(struct vm_area_struct *vma, unsigned long address) |
| 704 | { |
| 705 | return -ENOMEM; |
| 706 | } |
| 707 | |
David Howells | 930e652 | 2006-09-27 01:50:22 -0700 | [diff] [blame] | 708 | /* |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 709 | * look up the first VMA exactly that exactly matches addr |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 710 | * - should be called with mm->mmap_lock at least held readlocked |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 711 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 712 | static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, |
| 713 | unsigned long addr, |
| 714 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
| 716 | struct vm_area_struct *vma; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 717 | unsigned long end = addr + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 719 | /* check the cache first */ |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 720 | vma = vmacache_find_exact(mm, addr, end); |
| 721 | if (vma) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 722 | return vma; |
| 723 | |
Namhyung Kim | e922c4c | 2011-05-24 17:11:24 -0700 | [diff] [blame] | 724 | /* trawl the list (there may be multiple mappings in which addr |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 725 | * resides) */ |
Namhyung Kim | e922c4c | 2011-05-24 17:11:24 -0700 | [diff] [blame] | 726 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 727 | if (vma->vm_start < addr) |
| 728 | continue; |
| 729 | if (vma->vm_start > addr) |
| 730 | return NULL; |
| 731 | if (vma->vm_end == end) { |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 732 | vmacache_update(addr, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return vma; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | return NULL; |
| 738 | } |
| 739 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 740 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | * determine whether a mapping should be permitted and, if so, what sort of |
| 742 | * mapping we're capable of supporting |
| 743 | */ |
| 744 | static int validate_mmap_request(struct file *file, |
| 745 | unsigned long addr, |
| 746 | unsigned long len, |
| 747 | unsigned long prot, |
| 748 | unsigned long flags, |
| 749 | unsigned long pgoff, |
| 750 | unsigned long *_capabilities) |
| 751 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 752 | unsigned long capabilities, rlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | int ret; |
| 754 | |
| 755 | /* do the simple checks first */ |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 756 | if (flags & MAP_FIXED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | if ((flags & MAP_TYPE) != MAP_PRIVATE && |
| 760 | (flags & MAP_TYPE) != MAP_SHARED) |
| 761 | return -EINVAL; |
| 762 | |
Mike Frysinger | f81cff0d | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 763 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | return -EINVAL; |
| 765 | |
Mike Frysinger | f81cff0d | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 766 | /* Careful about overflows.. */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 767 | rlen = PAGE_ALIGN(len); |
| 768 | if (!rlen || rlen > TASK_SIZE) |
Mike Frysinger | f81cff0d | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 769 | return -ENOMEM; |
| 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | /* offset overflow? */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 772 | if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) |
Mike Frysinger | f81cff0d | 2006-12-06 12:02:59 +1000 | [diff] [blame] | 773 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | if (file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | /* files must support mmap */ |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 777 | if (!file->f_op->mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return -ENODEV; |
| 779 | |
| 780 | /* work out if what we've got could possibly be shared |
| 781 | * - we support chardevs that provide their own "memory" |
| 782 | * - we support files/blockdevs that are memory backed |
| 783 | */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 784 | if (file->f_op->mmap_capabilities) { |
| 785 | capabilities = file->f_op->mmap_capabilities(file); |
| 786 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | /* no explicit capabilities set, so assume some |
| 788 | * defaults */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 789 | switch (file_inode(file)->i_mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | case S_IFREG: |
| 791 | case S_IFBLK: |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 792 | capabilities = NOMMU_MAP_COPY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | break; |
| 794 | |
| 795 | case S_IFCHR: |
| 796 | capabilities = |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 797 | NOMMU_MAP_DIRECT | |
| 798 | NOMMU_MAP_READ | |
| 799 | NOMMU_MAP_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | break; |
| 801 | |
| 802 | default: |
| 803 | return -EINVAL; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* eliminate any capabilities that we can't support on this |
| 808 | * device */ |
| 809 | if (!file->f_op->get_unmapped_area) |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 810 | capabilities &= ~NOMMU_MAP_DIRECT; |
Al Viro | 6e242a1 | 2015-03-31 12:35:13 -0400 | [diff] [blame] | 811 | if (!(file->f_mode & FMODE_CAN_READ)) |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 812 | capabilities &= ~NOMMU_MAP_COPY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Graff Yang | 28d7a6a | 2009-08-18 14:11:17 -0700 | [diff] [blame] | 814 | /* The file shall have been opened with read permission. */ |
| 815 | if (!(file->f_mode & FMODE_READ)) |
| 816 | return -EACCES; |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | if (flags & MAP_SHARED) { |
| 819 | /* do checks for writing, appending and locking */ |
| 820 | if ((prot & PROT_WRITE) && |
| 821 | !(file->f_mode & FMODE_WRITE)) |
| 822 | return -EACCES; |
| 823 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 824 | if (IS_APPEND(file_inode(file)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | (file->f_mode & FMODE_WRITE)) |
| 826 | return -EACCES; |
| 827 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 828 | if (!(capabilities & NOMMU_MAP_DIRECT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return -ENODEV; |
| 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | /* we mustn't privatise shared mappings */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 832 | capabilities &= ~NOMMU_MAP_COPY; |
Choi Gi-yong | ac71490 | 2014-04-07 15:37:36 -0700 | [diff] [blame] | 833 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | /* we're going to read the file into private memory we |
| 835 | * allocate */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 836 | if (!(capabilities & NOMMU_MAP_COPY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return -ENODEV; |
| 838 | |
| 839 | /* we don't permit a private writable mapping to be |
| 840 | * shared with the backing device */ |
| 841 | if (prot & PROT_WRITE) |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 842 | capabilities &= ~NOMMU_MAP_DIRECT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 845 | if (capabilities & NOMMU_MAP_DIRECT) { |
| 846 | if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) || |
| 847 | ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) || |
| 848 | ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC)) |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 849 | ) { |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 850 | capabilities &= ~NOMMU_MAP_DIRECT; |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 851 | if (flags & MAP_SHARED) { |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 852 | pr_warn("MAP_SHARED not completely supported on !MMU\n"); |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 853 | return -EINVAL; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | /* handle executable mappings and implied executable |
| 859 | * mappings */ |
Eric W. Biederman | 90f8572 | 2015-06-29 14:42:03 -0500 | [diff] [blame] | 860 | if (path_noexec(&file->f_path)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | if (prot & PROT_EXEC) |
| 862 | return -EPERM; |
Choi Gi-yong | ac71490 | 2014-04-07 15:37:36 -0700 | [diff] [blame] | 863 | } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | /* handle implication of PROT_EXEC by PROT_READ */ |
| 865 | if (current->personality & READ_IMPLIES_EXEC) { |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 866 | if (capabilities & NOMMU_MAP_EXEC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | prot |= PROT_EXEC; |
| 868 | } |
Choi Gi-yong | ac71490 | 2014-04-07 15:37:36 -0700 | [diff] [blame] | 869 | } else if ((prot & PROT_READ) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | (prot & PROT_EXEC) && |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 871 | !(capabilities & NOMMU_MAP_EXEC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | ) { |
| 873 | /* backing file is not executable, try to copy */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 874 | capabilities &= ~NOMMU_MAP_DIRECT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
Choi Gi-yong | ac71490 | 2014-04-07 15:37:36 -0700 | [diff] [blame] | 876 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /* anonymous mappings are always memory backed and can be |
| 878 | * privately mapped |
| 879 | */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 880 | capabilities = NOMMU_MAP_COPY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
| 882 | /* handle PROT_EXEC implication by PROT_READ */ |
| 883 | if ((prot & PROT_READ) && |
| 884 | (current->personality & READ_IMPLIES_EXEC)) |
| 885 | prot |= PROT_EXEC; |
| 886 | } |
| 887 | |
| 888 | /* allow the security API to have its say */ |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 889 | ret = security_mmap_addr(addr); |
| 890 | if (ret < 0) |
| 891 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | /* looks okay */ |
| 894 | *_capabilities = capabilities; |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * we've determined that we can make the mapping, now translate what we |
| 900 | * now know into VMA flags |
| 901 | */ |
| 902 | static unsigned long determine_vm_flags(struct file *file, |
| 903 | unsigned long prot, |
| 904 | unsigned long flags, |
| 905 | unsigned long capabilities) |
| 906 | { |
| 907 | unsigned long vm_flags; |
| 908 | |
Dave Hansen | e6bfb70 | 2016-02-12 13:02:31 -0800 | [diff] [blame] | 909 | vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* vm_flags |= mm->def_flags; */ |
| 911 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 912 | if (!(capabilities & NOMMU_MAP_DIRECT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | /* attempt to share read-only copies of mapped file chunks */ |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 914 | vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | if (file && !(prot & PROT_WRITE)) |
| 916 | vm_flags |= VM_MAYSHARE; |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 917 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | /* overlay a shareable mapping on the backing device or inode |
| 919 | * if possible - used for chardevs, ramfs/tmpfs/shmfs and |
| 920 | * romfs/cramfs */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 921 | vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | if (flags & MAP_SHARED) |
Bernd Schmidt | 3c7b204 | 2010-05-25 23:43:00 -0700 | [diff] [blame] | 923 | vm_flags |= VM_SHARED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* refuse to let anyone share private mappings with this process if |
| 927 | * it's being traced - otherwise breakpoints set in it may interfere |
| 928 | * with another untraced process |
| 929 | */ |
Tejun Heo | a288eec | 2011-06-17 16:50:37 +0200 | [diff] [blame] | 930 | if ((flags & MAP_PRIVATE) && current->ptrace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | vm_flags &= ~VM_MAYSHARE; |
| 932 | |
| 933 | return vm_flags; |
| 934 | } |
| 935 | |
| 936 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 937 | * set up a shared mapping on a file (the driver or filesystem provides and |
| 938 | * pins the storage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 940 | static int do_mmap_shared_file(struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | { |
| 942 | int ret; |
| 943 | |
Miklos Szeredi | f74ac01 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 944 | ret = call_mmap(vma->vm_file, vma); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 945 | if (ret == 0) { |
| 946 | vma->vm_region->vm_top = vma->vm_region->vm_end; |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 947 | return 0; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (ret != -ENOSYS) |
| 950 | return ret; |
| 951 | |
David Howells | 3fa3046 | 2010-03-23 13:35:21 -0700 | [diff] [blame] | 952 | /* getting -ENOSYS indicates that direct mmap isn't possible (as |
| 953 | * opposed to tried but failed) so we can only give a suitable error as |
| 954 | * 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] | 955 | return -ENODEV; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * set up a private mapping or an anonymous shared mapping |
| 960 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 961 | static int do_mmap_private(struct vm_area_struct *vma, |
| 962 | struct vm_region *region, |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 963 | unsigned long len, |
| 964 | unsigned long capabilities) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | { |
Joonsoo Kim | dbc8358 | 2014-12-12 16:55:55 -0800 | [diff] [blame] | 966 | unsigned long total, point; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | void *base; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 968 | int ret, order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | /* invoke the file's mapping function so that it can keep track of |
| 971 | * shared mappings on devices or memory |
| 972 | * - VM_MAYSHARE will be set if it may attempt to share |
| 973 | */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 974 | if (capabilities & NOMMU_MAP_DIRECT) { |
Miklos Szeredi | f74ac01 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 975 | ret = call_mmap(vma->vm_file, vma); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 976 | if (ret == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | /* shouldn't return success if we're not sharing */ |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 978 | BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); |
| 979 | vma->vm_region->vm_top = vma->vm_region->vm_end; |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 980 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 982 | if (ret != -ENOSYS) |
| 983 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | /* getting an ENOSYS error indicates that direct mmap isn't |
| 986 | * possible (as opposed to tried but failed) so we'll try to |
| 987 | * make a private copy of the data and map that instead */ |
| 988 | } |
| 989 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | /* allocate some memory to hold the mapping |
| 992 | * - note that this may not return a page-aligned address if the object |
| 993 | * we're allocating is smaller than a page |
| 994 | */ |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 995 | order = get_order(len); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 996 | total = 1 << order; |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 997 | point = len >> PAGE_SHIFT; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 998 | |
Joonsoo Kim | dbc8358 | 2014-12-12 16:55:55 -0800 | [diff] [blame] | 999 | /* we don't want to allocate a power-of-2 sized page set */ |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1000 | if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) |
Joonsoo Kim | dbc8358 | 2014-12-12 16:55:55 -0800 | [diff] [blame] | 1001 | total = point; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1002 | |
Joonsoo Kim | da61653 | 2015-02-27 15:51:43 -0800 | [diff] [blame] | 1003 | base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL); |
Joonsoo Kim | dbc8358 | 2014-12-12 16:55:55 -0800 | [diff] [blame] | 1004 | if (!base) |
| 1005 | goto enomem; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1006 | |
Joonsoo Kim | dbc8358 | 2014-12-12 16:55:55 -0800 | [diff] [blame] | 1007 | atomic_long_add(total, &mmap_pages_allocated); |
| 1008 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1009 | region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; |
| 1010 | region->vm_start = (unsigned long) base; |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1011 | region->vm_end = region->vm_start + len; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1012 | region->vm_top = region->vm_start + (total << PAGE_SHIFT); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1013 | |
| 1014 | vma->vm_start = region->vm_start; |
| 1015 | vma->vm_end = region->vm_start + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (vma->vm_file) { |
| 1018 | /* read the contents of a file into the copy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | loff_t fpos; |
| 1020 | |
| 1021 | fpos = vma->vm_pgoff; |
| 1022 | fpos <<= PAGE_SHIFT; |
| 1023 | |
Christoph Hellwig | b4bf802 | 2017-09-01 17:39:17 +0200 | [diff] [blame] | 1024 | ret = kernel_read(vma->vm_file, base, len, &fpos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | if (ret < 0) |
| 1026 | goto error_free; |
| 1027 | |
| 1028 | /* clear the last little bit */ |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1029 | if (ret < len) |
| 1030 | memset(base + ret, 0, len - ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Kirill A. Shutemov | bfd40ea | 2018-07-26 16:37:35 -0700 | [diff] [blame] | 1032 | } else { |
| 1033 | vma_set_anonymous(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | return 0; |
| 1037 | |
| 1038 | error_free: |
Namhyung Kim | 7223bb4 | 2011-05-24 17:11:26 -0700 | [diff] [blame] | 1039 | free_page_series(region->vm_start, region->vm_top); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1040 | region->vm_start = vma->vm_start = 0; |
| 1041 | region->vm_end = vma->vm_end = 0; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1042 | region->vm_top = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | return ret; |
| 1044 | |
| 1045 | enomem: |
Mitchel Humpherys | b1de0d1 | 2014-06-06 14:38:30 -0700 | [diff] [blame] | 1046 | pr_err("Allocation of length %lu from process %d (%s) failed\n", |
Greg Ungerer | 05ae6fa | 2009-01-13 17:30:22 +1000 | [diff] [blame] | 1047 | len, current->pid, current->comm); |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 1048 | show_free_areas(0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | return -ENOMEM; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * handle mapping creation for uClinux |
| 1054 | */ |
Oleg Nesterov | 1fcfd8d | 2015-09-09 15:39:29 -0700 | [diff] [blame] | 1055 | unsigned long do_mmap(struct file *file, |
| 1056 | unsigned long addr, |
| 1057 | unsigned long len, |
| 1058 | unsigned long prot, |
| 1059 | unsigned long flags, |
Oleg Nesterov | 1fcfd8d | 2015-09-09 15:39:29 -0700 | [diff] [blame] | 1060 | unsigned long pgoff, |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 1061 | unsigned long *populate, |
| 1062 | struct list_head *uf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1064 | struct vm_area_struct *vma; |
| 1065 | struct vm_region *region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | struct rb_node *rb; |
Peter Collingbourne | 45e5530 | 2020-08-06 23:23:37 -0700 | [diff] [blame] | 1067 | vm_flags_t vm_flags; |
Oleg Nesterov | 1fcfd8d | 2015-09-09 15:39:29 -0700 | [diff] [blame] | 1068 | unsigned long capabilities, result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | int ret; |
| 1070 | |
Michel Lespinasse | 41badc1 | 2013-02-22 16:32:47 -0800 | [diff] [blame] | 1071 | *populate = 0; |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /* decide whether we should attempt the mapping, and if so what sort of |
| 1074 | * mapping */ |
| 1075 | ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, |
| 1076 | &capabilities); |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1077 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | return ret; |
| 1079 | |
David Howells | 06aab5a | 2009-09-24 12:33:48 +0100 | [diff] [blame] | 1080 | /* we ignore the address hint */ |
| 1081 | addr = 0; |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1082 | len = PAGE_ALIGN(len); |
David Howells | 06aab5a | 2009-09-24 12:33:48 +0100 | [diff] [blame] | 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | /* we've determined that we can make the mapping, now translate what we |
| 1085 | * now know into VMA flags */ |
Peter Collingbourne | 45e5530 | 2020-08-06 23:23:37 -0700 | [diff] [blame] | 1086 | vm_flags = determine_vm_flags(file, prot, flags, capabilities); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1088 | /* we're going to need to record the mapping */ |
| 1089 | region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); |
| 1090 | if (!region) |
| 1091 | goto error_getting_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
Linus Torvalds | 490fc05 | 2018-07-21 15:24:03 -0700 | [diff] [blame] | 1093 | vma = vm_area_alloc(current->mm); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1094 | if (!vma) |
| 1095 | goto error_getting_vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1097 | region->vm_usage = 1; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1098 | region->vm_flags = vm_flags; |
| 1099 | region->vm_pgoff = pgoff; |
| 1100 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1101 | vma->vm_flags = vm_flags; |
| 1102 | vma->vm_pgoff = pgoff; |
| 1103 | |
| 1104 | if (file) { |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 1105 | region->vm_file = get_file(file); |
| 1106 | vma->vm_file = get_file(file); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | down_write(&nommu_region_sem); |
| 1110 | |
| 1111 | /* 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] | 1112 | * mmap() calls that overlap with our proposed mapping |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1113 | * - we can only share with a superset match on most regular files |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | * - shared mappings on character devices and memory backed files are |
| 1115 | * permitted to overlap inexactly as far as we are concerned for in |
| 1116 | * these cases, sharing is handled in the driver or filesystem rather |
| 1117 | * than here |
| 1118 | */ |
| 1119 | if (vm_flags & VM_MAYSHARE) { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1120 | struct vm_region *pregion; |
| 1121 | unsigned long pglen, rpglen, pgend, rpgend, start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1123 | pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1124 | pgend = pgoff + pglen; |
David Howells | 165b239 | 2007-03-22 00:11:24 -0800 | [diff] [blame] | 1125 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1126 | for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { |
| 1127 | pregion = rb_entry(rb, struct vm_region, vm_rb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1129 | if (!(pregion->vm_flags & VM_MAYSHARE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | continue; |
| 1131 | |
| 1132 | /* search for overlapping mappings on the same file */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1133 | if (file_inode(pregion->vm_file) != |
| 1134 | file_inode(file)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | continue; |
| 1136 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1137 | if (pregion->vm_pgoff >= pgend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | continue; |
| 1139 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1140 | rpglen = pregion->vm_end - pregion->vm_start; |
| 1141 | rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1142 | rpgend = pregion->vm_pgoff + rpglen; |
| 1143 | if (pgoff >= rpgend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | continue; |
| 1145 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1146 | /* handle inexactly overlapping matches between |
| 1147 | * mappings */ |
| 1148 | if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && |
| 1149 | !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { |
| 1150 | /* new mapping is not a subset of the region */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1151 | if (!(capabilities & NOMMU_MAP_DIRECT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | goto sharing_violation; |
| 1153 | continue; |
| 1154 | } |
| 1155 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1156 | /* we've found a region we can share */ |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1157 | pregion->vm_usage++; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1158 | vma->vm_region = pregion; |
| 1159 | start = pregion->vm_start; |
| 1160 | start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; |
| 1161 | vma->vm_start = start; |
| 1162 | vma->vm_end = start + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1164 | if (pregion->vm_flags & VM_MAPPED_COPY) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1165 | vma->vm_flags |= VM_MAPPED_COPY; |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1166 | else { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1167 | ret = do_mmap_shared_file(vma); |
| 1168 | if (ret < 0) { |
| 1169 | vma->vm_region = NULL; |
| 1170 | vma->vm_start = 0; |
| 1171 | vma->vm_end = 0; |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1172 | pregion->vm_usage--; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1173 | pregion = NULL; |
| 1174 | goto error_just_free; |
| 1175 | } |
| 1176 | } |
| 1177 | fput(region->vm_file); |
| 1178 | kmem_cache_free(vm_region_jar, region); |
| 1179 | region = pregion; |
| 1180 | result = start; |
| 1181 | goto share; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | /* obtain the address at which to make a shared mapping |
| 1185 | * - this is the hook for quasi-memory character devices to |
| 1186 | * tell us the location of a shared mapping |
| 1187 | */ |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1188 | if (capabilities & NOMMU_MAP_DIRECT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | addr = file->f_op->get_unmapped_area(file, addr, len, |
| 1190 | pgoff, flags); |
Namhyung Kim | bb005a5 | 2011-05-24 17:11:27 -0700 | [diff] [blame] | 1191 | if (IS_ERR_VALUE(addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | ret = addr; |
Namhyung Kim | bb005a5 | 2011-05-24 17:11:27 -0700 | [diff] [blame] | 1193 | if (ret != -ENOSYS) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1194 | goto error_just_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
| 1196 | /* the driver refused to tell us where to site |
| 1197 | * the mapping so we'll have to attempt to copy |
| 1198 | * it */ |
Namhyung Kim | bb005a5 | 2011-05-24 17:11:27 -0700 | [diff] [blame] | 1199 | ret = -ENODEV; |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1200 | if (!(capabilities & NOMMU_MAP_COPY)) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1201 | goto error_just_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1203 | capabilities &= ~NOMMU_MAP_DIRECT; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1204 | } else { |
| 1205 | vma->vm_start = region->vm_start = addr; |
| 1206 | vma->vm_end = region->vm_end = addr + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1211 | vma->vm_region = region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1213 | /* set up the mapping |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1214 | * - the region is filled in if NOMMU_MAP_DIRECT is still set |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1215 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | if (file && vma->vm_flags & VM_SHARED) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1217 | ret = do_mmap_shared_file(vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | else |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1219 | ret = do_mmap_private(vma, region, len, capabilities); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | if (ret < 0) |
David Howells | 645d83c | 2009-09-24 15:13:10 +0100 | [diff] [blame] | 1221 | goto error_just_free; |
| 1222 | add_nommu_region(region); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1223 | |
Jie Zhang | ea63763 | 2009-12-14 18:00:02 -0800 | [diff] [blame] | 1224 | /* clear anonymous mappings that don't ask for uninitialized data */ |
Christoph Hellwig | 0bf5f94 | 2019-07-16 16:26:27 -0700 | [diff] [blame] | 1225 | if (!vma->vm_file && |
| 1226 | (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) || |
| 1227 | !(flags & MAP_UNINITIALIZED))) |
Jie Zhang | ea63763 | 2009-12-14 18:00:02 -0800 | [diff] [blame] | 1228 | memset((void *)region->vm_start, 0, |
| 1229 | region->vm_end - region->vm_start); |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | /* okay... we have a mapping; now we have to register it */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1232 | result = vma->vm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | current->mm->total_vm += len >> PAGE_SHIFT; |
| 1235 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1236 | share: |
| 1237 | add_vma_to_mm(current->mm, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
Mike Frysinger | cfe79c0 | 2010-01-06 17:23:23 +0000 | [diff] [blame] | 1239 | /* we flush the region from the icache only when the first executable |
| 1240 | * mapping of it is made */ |
| 1241 | if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { |
Christoph Hellwig | a75a2df | 2020-06-07 21:42:49 -0700 | [diff] [blame] | 1242 | flush_icache_user_range(region->vm_start, region->vm_end); |
Mike Frysinger | cfe79c0 | 2010-01-06 17:23:23 +0000 | [diff] [blame] | 1243 | region->vm_icache_flushed = true; |
| 1244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
Mike Frysinger | cfe79c0 | 2010-01-06 17:23:23 +0000 | [diff] [blame] | 1246 | up_write(&nommu_region_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1248 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1250 | error_just_free: |
| 1251 | up_write(&nommu_region_sem); |
| 1252 | error: |
David Howells | 89a8640 | 2009-10-30 13:13:26 +0000 | [diff] [blame] | 1253 | if (region->vm_file) |
| 1254 | fput(region->vm_file); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1255 | kmem_cache_free(vm_region_jar, region); |
David Howells | 89a8640 | 2009-10-30 13:13:26 +0000 | [diff] [blame] | 1256 | if (vma->vm_file) |
| 1257 | fput(vma->vm_file); |
Linus Torvalds | 3928d4f | 2018-07-21 13:48:51 -0700 | [diff] [blame] | 1258 | vm_area_free(vma); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1259 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1261 | sharing_violation: |
| 1262 | up_write(&nommu_region_sem); |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1263 | pr_warn("Attempt to share mismatched mappings\n"); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1264 | ret = -EINVAL; |
| 1265 | goto error; |
| 1266 | |
| 1267 | error_getting_vma: |
| 1268 | kmem_cache_free(vm_region_jar, region); |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1269 | pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n", |
| 1270 | len, current->pid); |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 1271 | show_free_areas(0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | return -ENOMEM; |
| 1273 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1274 | error_getting_region: |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1275 | pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n", |
| 1276 | len, current->pid); |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 1277 | show_free_areas(0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | return -ENOMEM; |
| 1279 | } |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1280 | |
Dominik Brodowski | a90f590 | 2018-03-11 11:34:46 +0100 | [diff] [blame] | 1281 | unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, |
| 1282 | unsigned long prot, unsigned long flags, |
| 1283 | unsigned long fd, unsigned long pgoff) |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1284 | { |
| 1285 | struct file *file = NULL; |
| 1286 | unsigned long retval = -EBADF; |
| 1287 | |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 1288 | audit_mmap_fd(fd, flags); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1289 | if (!(flags & MAP_ANONYMOUS)) { |
| 1290 | file = fget(fd); |
| 1291 | if (!file) |
| 1292 | goto out; |
| 1293 | } |
| 1294 | |
Greg Ungerer | ad1ed29 | 2012-06-04 14:29:59 +1000 | [diff] [blame] | 1295 | retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
Hugh Dickins | 66f0dc4 | 2009-12-30 20:17:34 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (file) |
| 1298 | fput(file); |
| 1299 | out: |
| 1300 | return retval; |
| 1301 | } |
| 1302 | |
Dominik Brodowski | a90f590 | 2018-03-11 11:34:46 +0100 | [diff] [blame] | 1303 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
| 1304 | unsigned long, prot, unsigned long, flags, |
| 1305 | unsigned long, fd, unsigned long, pgoff) |
| 1306 | { |
| 1307 | return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); |
| 1308 | } |
| 1309 | |
Christoph Hellwig | a467937 | 2010-03-10 15:21:15 -0800 | [diff] [blame] | 1310 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
| 1311 | struct mmap_arg_struct { |
| 1312 | unsigned long addr; |
| 1313 | unsigned long len; |
| 1314 | unsigned long prot; |
| 1315 | unsigned long flags; |
| 1316 | unsigned long fd; |
| 1317 | unsigned long offset; |
| 1318 | }; |
| 1319 | |
| 1320 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) |
| 1321 | { |
| 1322 | struct mmap_arg_struct a; |
| 1323 | |
| 1324 | if (copy_from_user(&a, arg, sizeof(a))) |
| 1325 | return -EFAULT; |
Alexander Kuleshov | 1824cb7 | 2015-11-05 18:46:35 -0800 | [diff] [blame] | 1326 | if (offset_in_page(a.offset)) |
Christoph Hellwig | a467937 | 2010-03-10 15:21:15 -0800 | [diff] [blame] | 1327 | return -EINVAL; |
| 1328 | |
Dominik Brodowski | a90f590 | 2018-03-11 11:34:46 +0100 | [diff] [blame] | 1329 | return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, |
| 1330 | a.offset >> PAGE_SHIFT); |
Christoph Hellwig | a467937 | 2010-03-10 15:21:15 -0800 | [diff] [blame] | 1331 | } |
| 1332 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ |
| 1333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1335 | * split a vma into two pieces at address 'addr', a new vma is allocated either |
| 1336 | * for the first part or the tail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1338 | int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1339 | unsigned long addr, int new_below) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1341 | struct vm_area_struct *new; |
| 1342 | struct vm_region *region; |
| 1343 | unsigned long npages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
David Howells | 779c102 | 2010-01-15 17:01:34 -0800 | [diff] [blame] | 1345 | /* we're only permitted to split anonymous regions (these should have |
| 1346 | * only a single usage on the region) */ |
| 1347 | if (vma->vm_file) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1348 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1350 | if (mm->map_count >= sysctl_max_map_count) |
| 1351 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1353 | region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); |
| 1354 | if (!region) |
| 1355 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
Linus Torvalds | 3928d4f | 2018-07-21 13:48:51 -0700 | [diff] [blame] | 1357 | new = vm_area_dup(vma); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1358 | if (!new) { |
| 1359 | kmem_cache_free(vm_region_jar, region); |
| 1360 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* most fields are the same, copy all, and then fixup */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1364 | *region = *vma->vm_region; |
| 1365 | new->vm_region = region; |
| 1366 | |
| 1367 | npages = (addr - vma->vm_start) >> PAGE_SHIFT; |
| 1368 | |
| 1369 | if (new_below) { |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1370 | region->vm_top = region->vm_end = new->vm_end = addr; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1371 | } else { |
| 1372 | region->vm_start = new->vm_start = addr; |
| 1373 | region->vm_pgoff = new->vm_pgoff += npages; |
| 1374 | } |
| 1375 | |
| 1376 | if (new->vm_ops && new->vm_ops->open) |
| 1377 | new->vm_ops->open(new); |
| 1378 | |
| 1379 | delete_vma_from_mm(vma); |
| 1380 | down_write(&nommu_region_sem); |
| 1381 | delete_nommu_region(vma->vm_region); |
| 1382 | if (new_below) { |
| 1383 | vma->vm_region->vm_start = vma->vm_start = addr; |
| 1384 | vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; |
| 1385 | } else { |
| 1386 | vma->vm_region->vm_end = vma->vm_end = addr; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1387 | vma->vm_region->vm_top = addr; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1388 | } |
| 1389 | add_nommu_region(vma->vm_region); |
| 1390 | add_nommu_region(new->vm_region); |
| 1391 | up_write(&nommu_region_sem); |
| 1392 | add_vma_to_mm(mm, vma); |
| 1393 | add_vma_to_mm(mm, new); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * shrink a VMA by removing the specified chunk from either the beginning or |
| 1399 | * the end |
| 1400 | */ |
| 1401 | static int shrink_vma(struct mm_struct *mm, |
| 1402 | struct vm_area_struct *vma, |
| 1403 | unsigned long from, unsigned long to) |
| 1404 | { |
| 1405 | struct vm_region *region; |
| 1406 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1407 | /* adjust the VMA's pointers, which may reposition it in the MM's tree |
| 1408 | * and list */ |
| 1409 | delete_vma_from_mm(vma); |
| 1410 | if (from > vma->vm_start) |
| 1411 | vma->vm_end = from; |
| 1412 | else |
| 1413 | vma->vm_start = to; |
| 1414 | add_vma_to_mm(mm, vma); |
| 1415 | |
| 1416 | /* cut the backing region down to size */ |
| 1417 | region = vma->vm_region; |
David Howells | 1e2ae59 | 2010-01-15 17:01:33 -0800 | [diff] [blame] | 1418 | BUG_ON(region->vm_usage != 1); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1419 | |
| 1420 | down_write(&nommu_region_sem); |
| 1421 | delete_nommu_region(region); |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1422 | if (from > region->vm_start) { |
| 1423 | to = region->vm_top; |
| 1424 | region->vm_top = region->vm_end = from; |
| 1425 | } else { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1426 | region->vm_start = to; |
Paul Mundt | dd8632a | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1427 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1428 | add_nommu_region(region); |
| 1429 | up_write(&nommu_region_sem); |
| 1430 | |
| 1431 | free_page_series(from, to); |
| 1432 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1435 | /* |
| 1436 | * release a mapping |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1437 | * - under NOMMU conditions the chunk to be unmapped must be backed by a single |
| 1438 | * VMA, though it need not cover the whole VMA |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1439 | */ |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 1440 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1442 | struct vm_area_struct *vma; |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1443 | unsigned long end; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1444 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1446 | len = PAGE_ALIGN(len); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1447 | if (len == 0) |
| 1448 | return -EINVAL; |
| 1449 | |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1450 | end = start + len; |
| 1451 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1452 | /* find the first potentially overlapping VMA */ |
| 1453 | vma = find_vma(mm, start); |
| 1454 | if (!vma) { |
Choi Gi-yong | ac71490 | 2014-04-07 15:37:36 -0700 | [diff] [blame] | 1455 | static int limit; |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 1456 | if (limit < 5) { |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1457 | pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n", |
| 1458 | current->pid, current->comm, |
| 1459 | start, start + len - 1); |
David Howells | 33e5d769 | 2009-04-02 16:56:32 -0700 | [diff] [blame] | 1460 | limit++; |
| 1461 | } |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1462 | return -EINVAL; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1465 | /* we're allowed to split an anonymous VMA but not a file-backed one */ |
| 1466 | if (vma->vm_file) { |
| 1467 | do { |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1468 | if (start > vma->vm_start) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1469 | return -EINVAL; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1470 | if (end == vma->vm_end) |
| 1471 | goto erase_whole_vma; |
Namhyung Kim | d75a310 | 2011-05-24 17:11:25 -0700 | [diff] [blame] | 1472 | vma = vma->vm_next; |
| 1473 | } while (vma); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1474 | return -EINVAL; |
| 1475 | } else { |
| 1476 | /* the chunk must be a subset of the VMA found */ |
| 1477 | if (start == vma->vm_start && end == vma->vm_end) |
| 1478 | goto erase_whole_vma; |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1479 | if (start < vma->vm_start || end > vma->vm_end) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1480 | return -EINVAL; |
Alexander Kuleshov | 1824cb7 | 2015-11-05 18:46:35 -0800 | [diff] [blame] | 1481 | if (offset_in_page(start)) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1482 | return -EINVAL; |
Alexander Kuleshov | 1824cb7 | 2015-11-05 18:46:35 -0800 | [diff] [blame] | 1483 | if (end != vma->vm_end && offset_in_page(end)) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1484 | return -EINVAL; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1485 | if (start != vma->vm_start && end != vma->vm_end) { |
| 1486 | ret = split_vma(mm, vma, start, 1); |
Leon Romanovsky | 22cc877 | 2015-06-24 16:57:47 -0700 | [diff] [blame] | 1487 | if (ret < 0) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1488 | return ret; |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1489 | } |
| 1490 | return shrink_vma(mm, vma, start, end); |
| 1491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1493 | erase_whole_vma: |
| 1494 | delete_vma_from_mm(vma); |
| 1495 | delete_vma(mm, vma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | return 0; |
| 1497 | } |
| 1498 | |
Al Viro | bfce281 | 2012-04-20 21:57:04 -0400 | [diff] [blame] | 1499 | int vm_munmap(unsigned long addr, size_t len) |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1500 | { |
Al Viro | bfce281 | 2012-04-20 21:57:04 -0400 | [diff] [blame] | 1501 | struct mm_struct *mm = current->mm; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1502 | int ret; |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1503 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1504 | mmap_write_lock(mm); |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 1505 | ret = do_munmap(mm, addr, len, NULL); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1506 | mmap_write_unlock(mm); |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1507 | return ret; |
| 1508 | } |
Linus Torvalds | a46ef99 | 2012-04-20 16:20:01 -0700 | [diff] [blame] | 1509 | EXPORT_SYMBOL(vm_munmap); |
| 1510 | |
| 1511 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) |
| 1512 | { |
Al Viro | bfce281 | 2012-04-20 21:57:04 -0400 | [diff] [blame] | 1513 | return vm_munmap(addr, len); |
Linus Torvalds | a46ef99 | 2012-04-20 16:20:01 -0700 | [diff] [blame] | 1514 | } |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1515 | |
| 1516 | /* |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1517 | * release all the mappings made in a process's VM space |
David Howells | 3034097 | 2006-09-27 01:50:20 -0700 | [diff] [blame] | 1518 | */ |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1519 | void exit_mmap(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | { |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1521 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1523 | if (!mm) |
| 1524 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1526 | mm->total_vm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1528 | while ((vma = mm->mmap)) { |
| 1529 | mm->mmap = vma->vm_next; |
| 1530 | delete_vma_from_mm(vma); |
| 1531 | delete_vma(mm, vma); |
Steven J. Magnani | 04c3496 | 2010-11-24 12:56:54 -0800 | [diff] [blame] | 1532 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Linus Torvalds | 5d22fc2 | 2016-05-27 15:57:31 -0700 | [diff] [blame] | 1536 | int vm_brk(unsigned long addr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | { |
| 1538 | return -ENOMEM; |
| 1539 | } |
| 1540 | |
| 1541 | /* |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1542 | * expand (or shrink) an existing mapping, potentially moving it at the same |
| 1543 | * time (controlled by the MREMAP_MAYMOVE flag and available VM space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | * |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1545 | * 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] | 1546 | * as long as it stays within the region allocated by do_mmap_private() and the |
| 1547 | * block is not shareable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | * |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1549 | * MREMAP_FIXED is not supported under NOMMU conditions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | */ |
Al Viro | 4b377ba | 2013-03-04 10:47:59 -0500 | [diff] [blame] | 1551 | static unsigned long do_mremap(unsigned long addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | unsigned long old_len, unsigned long new_len, |
| 1553 | unsigned long flags, unsigned long new_addr) |
| 1554 | { |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1555 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | |
| 1557 | /* insanity checks first */ |
Bob Liu | f67d9b1 | 2011-05-24 17:12:56 -0700 | [diff] [blame] | 1558 | old_len = PAGE_ALIGN(old_len); |
| 1559 | new_len = PAGE_ALIGN(new_len); |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1560 | if (old_len == 0 || new_len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | return (unsigned long) -EINVAL; |
| 1562 | |
Alexander Kuleshov | 1824cb7 | 2015-11-05 18:46:35 -0800 | [diff] [blame] | 1563 | if (offset_in_page(addr)) |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1564 | return -EINVAL; |
| 1565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | if (flags & MREMAP_FIXED && new_addr != addr) |
| 1567 | return (unsigned long) -EINVAL; |
| 1568 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1569 | vma = find_vma_exact(current->mm, addr, old_len); |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1570 | if (!vma) |
| 1571 | return (unsigned long) -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1573 | if (vma->vm_end != vma->vm_start + old_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | return (unsigned long) -EFAULT; |
| 1575 | |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1576 | if (vma->vm_flags & VM_MAYSHARE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | return (unsigned long) -EPERM; |
| 1578 | |
David Howells | 8feae13 | 2009-01-08 12:04:47 +0000 | [diff] [blame] | 1579 | 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] | 1580 | return (unsigned long) -ENOMEM; |
| 1581 | |
| 1582 | /* all checks complete - do it */ |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1583 | vma->vm_end = vma->vm_start + new_len; |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1584 | return vma->vm_start; |
| 1585 | } |
| 1586 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 1587 | SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, |
| 1588 | unsigned long, new_len, unsigned long, flags, |
| 1589 | unsigned long, new_addr) |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1590 | { |
| 1591 | unsigned long ret; |
| 1592 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1593 | mmap_write_lock(current->mm); |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1594 | ret = do_mremap(addr, old_len, new_len, flags, new_addr); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1595 | mmap_write_unlock(current->mm); |
David Howells | 6fa5f80 | 2006-09-27 01:50:21 -0700 | [diff] [blame] | 1596 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 1599 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, |
| 1600 | unsigned int foll_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | { |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | |
Bob Liu | 8f3b132 | 2011-07-08 15:39:46 -0700 | [diff] [blame] | 1605 | int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, |
| 1606 | unsigned long pfn, unsigned long size, pgprot_t prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | { |
Bob Liu | 8f3b132 | 2011-07-08 15:39:46 -0700 | [diff] [blame] | 1608 | if (addr != (pfn << PAGE_SHIFT)) |
| 1609 | return -EINVAL; |
| 1610 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 1611 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
Greg Ungerer | 66aa2b4 | 2005-09-12 11:18:10 +1000 | [diff] [blame] | 1612 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
Luke Yang | 22c4af4 | 2006-07-14 00:24:09 -0700 | [diff] [blame] | 1614 | EXPORT_SYMBOL(remap_pfn_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
Linus Torvalds | 3c0b9de | 2013-04-27 13:25:38 -0700 | [diff] [blame] | 1616 | int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len) |
| 1617 | { |
| 1618 | unsigned long pfn = start >> PAGE_SHIFT; |
| 1619 | unsigned long vm_len = vma->vm_end - vma->vm_start; |
| 1620 | |
| 1621 | pfn += vma->vm_pgoff; |
| 1622 | return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot); |
| 1623 | } |
| 1624 | EXPORT_SYMBOL(vm_iomap_memory); |
| 1625 | |
Paul Mundt | f905bc4 | 2008-02-04 22:29:59 -0800 | [diff] [blame] | 1626 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
| 1627 | unsigned long pgoff) |
| 1628 | { |
| 1629 | unsigned int size = vma->vm_end - vma->vm_start; |
| 1630 | |
| 1631 | if (!(vma->vm_flags & VM_USERMAP)) |
| 1632 | return -EINVAL; |
| 1633 | |
| 1634 | vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); |
| 1635 | vma->vm_end = vma->vm_start + size; |
| 1636 | |
| 1637 | return 0; |
| 1638 | } |
| 1639 | EXPORT_SYMBOL(remap_vmalloc_range); |
| 1640 | |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 1641 | vm_fault_t filemap_fault(struct vm_fault *vmf) |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 1642 | { |
| 1643 | BUG(); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1644 | return 0; |
David Howells | b0e1519 | 2006-01-06 00:11:42 -0800 | [diff] [blame] | 1645 | } |
Paul Mundt | b507317 | 2007-07-21 04:37:25 -0700 | [diff] [blame] | 1646 | EXPORT_SYMBOL(filemap_fault); |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1647 | |
Geert Uytterhoeven | 3f98a28 | 2021-01-28 11:06:26 +0100 | [diff] [blame] | 1648 | vm_fault_t filemap_map_pages(struct vm_fault *vmf, |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 1649 | pgoff_t start_pgoff, pgoff_t end_pgoff) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 1650 | { |
| 1651 | BUG(); |
Geert Uytterhoeven | 3f98a28 | 2021-01-28 11:06:26 +0100 | [diff] [blame] | 1652 | return 0; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 1653 | } |
| 1654 | EXPORT_SYMBOL(filemap_map_pages); |
| 1655 | |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 1656 | int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, |
| 1657 | int len, unsigned int gup_flags) |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1658 | { |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1659 | struct vm_area_struct *vma; |
Lorenzo Stoakes | 442486e | 2016-10-13 01:20:18 +0100 | [diff] [blame] | 1660 | int write = gup_flags & FOLL_WRITE; |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1661 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1662 | if (mmap_read_lock_killable(mm)) |
Konstantin Khlebnikov | 1e426fe | 2019-07-11 21:00:07 -0700 | [diff] [blame] | 1663 | return 0; |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1664 | |
| 1665 | /* the access must start within one of the target process's mappings */ |
David Howells | 0159b14 | 2006-09-27 01:50:16 -0700 | [diff] [blame] | 1666 | vma = find_vma(mm, addr); |
| 1667 | if (vma) { |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1668 | /* don't overrun this mapping */ |
| 1669 | if (addr + len >= vma->vm_end) |
| 1670 | len = vma->vm_end - addr; |
| 1671 | |
| 1672 | /* only read or write mappings where it is permitted */ |
David Howells | d00c7b99 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 1673 | if (write && vma->vm_flags & VM_MAYWRITE) |
Jie Zhang | 7959722 | 2010-01-06 17:23:28 +0000 | [diff] [blame] | 1674 | copy_to_user_page(vma, NULL, addr, |
| 1675 | (void *) addr, buf, len); |
David Howells | d00c7b99 | 2006-09-27 01:50:19 -0700 | [diff] [blame] | 1676 | else if (!write && vma->vm_flags & VM_MAYREAD) |
Jie Zhang | 7959722 | 2010-01-06 17:23:28 +0000 | [diff] [blame] | 1677 | copy_from_user_page(vma, NULL, addr, |
| 1678 | buf, (void *) addr, len); |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1679 | else |
| 1680 | len = 0; |
| 1681 | } else { |
| 1682 | len = 0; |
| 1683 | } |
| 1684 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1685 | mmap_read_unlock(mm); |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1686 | |
| 1687 | return len; |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 1691 | * access_remote_vm - access another process' address space |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1692 | * @mm: the mm_struct of the target address space |
| 1693 | * @addr: start address to access |
| 1694 | * @buf: source or destination buffer |
| 1695 | * @len: number of bytes to transfer |
Lorenzo Stoakes | 6347e8d | 2016-10-13 01:20:19 +0100 | [diff] [blame] | 1696 | * @gup_flags: flags modifying lookup behaviour |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1697 | * |
| 1698 | * The caller must hold a reference on @mm. |
| 1699 | */ |
| 1700 | int access_remote_vm(struct mm_struct *mm, unsigned long addr, |
Lorenzo Stoakes | 6347e8d | 2016-10-13 01:20:19 +0100 | [diff] [blame] | 1701 | void *buf, int len, unsigned int gup_flags) |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1702 | { |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 1703 | return __access_remote_vm(mm, addr, buf, len, gup_flags); |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * Access another process' address space. |
| 1708 | * - source/target buffer must be kernel space |
| 1709 | */ |
Lorenzo Stoakes | f307ab6 | 2016-10-13 01:20:20 +0100 | [diff] [blame] | 1710 | int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, |
| 1711 | unsigned int gup_flags) |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1712 | { |
| 1713 | struct mm_struct *mm; |
| 1714 | |
| 1715 | if (addr + len < addr) |
| 1716 | return 0; |
| 1717 | |
| 1718 | mm = get_task_mm(tsk); |
| 1719 | if (!mm) |
| 1720 | return 0; |
| 1721 | |
John Hubbard | d3f5ffc | 2020-12-14 19:07:45 -0800 | [diff] [blame] | 1722 | len = __access_remote_vm(mm, addr, buf, len, gup_flags); |
Mike Frysinger | f55f199 | 2011-03-29 14:05:12 +0100 | [diff] [blame] | 1723 | |
David Howells | 0ec76a1 | 2006-09-27 01:50:15 -0700 | [diff] [blame] | 1724 | mmput(mm); |
| 1725 | return len; |
| 1726 | } |
Catalin Marinas | fcd3585 | 2016-11-01 14:43:25 -0700 | [diff] [blame] | 1727 | EXPORT_SYMBOL_GPL(access_process_vm); |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1728 | |
| 1729 | /** |
| 1730 | * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode |
| 1731 | * @inode: The inode to check |
| 1732 | * @size: The current filesize of the inode |
| 1733 | * @newsize: The proposed filesize of the inode |
| 1734 | * |
| 1735 | * Check the shared mappings on an inode on behalf of a shrinking truncate to |
Randy Dunlap | c08b342 | 2020-08-11 18:33:11 -0700 | [diff] [blame] | 1736 | * make sure that any outstanding VMAs aren't broken and then shrink the |
| 1737 | * vm_regions that extend beyond so that do_mmap() doesn't |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1738 | * automatically grant mappings that are too large. |
| 1739 | */ |
| 1740 | int nommu_shrink_inode_mappings(struct inode *inode, size_t size, |
| 1741 | size_t newsize) |
| 1742 | { |
| 1743 | struct vm_area_struct *vma; |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1744 | struct vm_region *region; |
| 1745 | pgoff_t low, high; |
| 1746 | size_t r_size, r_top; |
| 1747 | |
| 1748 | low = newsize >> PAGE_SHIFT; |
| 1749 | high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1750 | |
| 1751 | down_write(&nommu_region_sem); |
Davidlohr Bueso | 1acf2e0 | 2014-12-12 16:54:39 -0800 | [diff] [blame] | 1752 | i_mmap_lock_read(inode->i_mapping); |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1753 | |
| 1754 | /* search for VMAs that fall within the dead zone */ |
Michel Lespinasse | 6b2dbba | 2012-10-08 16:31:25 -0700 | [diff] [blame] | 1755 | vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1756 | /* found one - only interested if it's shared out of the page |
| 1757 | * cache */ |
| 1758 | if (vma->vm_flags & VM_SHARED) { |
Davidlohr Bueso | 1acf2e0 | 2014-12-12 16:54:39 -0800 | [diff] [blame] | 1759 | i_mmap_unlock_read(inode->i_mapping); |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1760 | up_write(&nommu_region_sem); |
| 1761 | return -ETXTBSY; /* not quite true, but near enough */ |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | /* reduce any regions that overlap the dead zone - if in existence, |
| 1766 | * these will be pointed to by VMAs that don't overlap the dead zone |
| 1767 | * |
| 1768 | * we don't check for any regions that start beyond the EOF as there |
| 1769 | * shouldn't be any |
| 1770 | */ |
Davidlohr Bueso | 1acf2e0 | 2014-12-12 16:54:39 -0800 | [diff] [blame] | 1771 | vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1772 | if (!(vma->vm_flags & VM_SHARED)) |
| 1773 | continue; |
| 1774 | |
| 1775 | region = vma->vm_region; |
| 1776 | r_size = region->vm_top - region->vm_start; |
| 1777 | r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; |
| 1778 | |
| 1779 | if (r_top > newsize) { |
| 1780 | region->vm_top -= r_top - newsize; |
| 1781 | if (region->vm_end > region->vm_top) |
| 1782 | region->vm_end = region->vm_top; |
| 1783 | } |
| 1784 | } |
| 1785 | |
Davidlohr Bueso | 1acf2e0 | 2014-12-12 16:54:39 -0800 | [diff] [blame] | 1786 | i_mmap_unlock_read(inode->i_mapping); |
David Howells | 7e66087 | 2010-01-15 17:01:39 -0800 | [diff] [blame] | 1787 | up_write(&nommu_region_sem); |
| 1788 | return 0; |
| 1789 | } |
Andrew Shewmaker | c9b1d09 | 2013-04-29 15:08:10 -0700 | [diff] [blame] | 1790 | |
| 1791 | /* |
| 1792 | * Initialise sysctl_user_reserve_kbytes. |
| 1793 | * |
| 1794 | * This is intended to prevent a user from starting a single memory hogging |
| 1795 | * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER |
| 1796 | * mode. |
| 1797 | * |
| 1798 | * The default value is min(3% of free memory, 128MB) |
| 1799 | * 128MB is enough to recover with sshd/login, bash, and top/kill. |
| 1800 | */ |
| 1801 | static int __meminit init_user_reserve(void) |
| 1802 | { |
| 1803 | unsigned long free_kbytes; |
| 1804 | |
Michal Hocko | c41f012 | 2017-09-06 16:23:36 -0700 | [diff] [blame] | 1805 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
Andrew Shewmaker | c9b1d09 | 2013-04-29 15:08:10 -0700 | [diff] [blame] | 1806 | |
| 1807 | sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); |
| 1808 | return 0; |
| 1809 | } |
Paul Gortmaker | a4bc6fc | 2015-05-01 20:08:20 -0400 | [diff] [blame] | 1810 | subsys_initcall(init_user_reserve); |
Andrew Shewmaker | 4eeab4f | 2013-04-29 15:08:11 -0700 | [diff] [blame] | 1811 | |
| 1812 | /* |
| 1813 | * Initialise sysctl_admin_reserve_kbytes. |
| 1814 | * |
| 1815 | * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin |
| 1816 | * to log in and kill a memory hogging process. |
| 1817 | * |
| 1818 | * Systems with more than 256MB will reserve 8MB, enough to recover |
| 1819 | * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will |
| 1820 | * only reserve 3% of free pages by default. |
| 1821 | */ |
| 1822 | static int __meminit init_admin_reserve(void) |
| 1823 | { |
| 1824 | unsigned long free_kbytes; |
| 1825 | |
Michal Hocko | c41f012 | 2017-09-06 16:23:36 -0700 | [diff] [blame] | 1826 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
Andrew Shewmaker | 4eeab4f | 2013-04-29 15:08:11 -0700 | [diff] [blame] | 1827 | |
| 1828 | sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); |
| 1829 | return 0; |
| 1830 | } |
Paul Gortmaker | a4bc6fc | 2015-05-01 20:08:20 -0400 | [diff] [blame] | 1831 | subsys_initcall(init_admin_reserve); |