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