blob: 55a9e48a7a02681667f33e038bb209c13b1ccb3c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Kittdd19d292020-08-12 11:22:30 +02008 * See Documentation/admin-guide/mm/nommu-mmap.rst
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
David Howells8feae132009-01-08 12:04:47 +000010 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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 Mundt29c185e2010-12-24 12:08:30 +090014 * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010021#include <linux/sched/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070022#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/mman.h>
24#include <linux/swap.h>
25#include <linux/file.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/slab.h>
29#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/backing-dev.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070031#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/mount.h>
33#include <linux/personality.h>
34#include <linux/security.h>
35#include <linux/syscalls.h>
Al Viro120a7952010-10-30 02:54:44 -040036#include <linux/audit.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070037#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/tlb.h>
41#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070042#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000043#include "internal.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045void *high_memory;
Arnd Bergmann944b6872015-02-05 12:25:12 -080046EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct page *mem_map;
48unsigned long max_mapnr;
gchen gchen5b8bf302015-03-12 16:26:05 -070049EXPORT_SYMBOL(max_mapnr);
Hugh Dickins4266c972009-09-23 17:05:53 +010050unsigned long highest_memmap_pfn;
David Howellsfc4d5c22009-05-06 16:03:05 -070051int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int heap_stack_gap = 0;
53
David Howells33e5d7692009-04-02 16:56:32 -070054atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056EXPORT_SYMBOL(mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David Howells8feae132009-01-08 12:04:47 +000058/* list of mapped, potentially shareable regions */
59static struct kmem_cache *vm_region_jar;
60struct rb_root nommu_region_tree = RB_ROOT;
61DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +040063const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * 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 */
72unsigned int kobjsize(const void *objp)
73{
74 struct page *page;
75
Michael Hennerich4016a132008-04-28 02:13:38 -070076 /*
77 * If the object we have should not have ksize performed on it,
78 * return size of 0
79 */
Paul Mundt5a1603b2008-06-12 16:29:55 +090080 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070081 return 0;
82
83 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070084
85 /*
86 * If the allocator sets PageSlab, we know the pointer came from
87 * kmalloc().
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (PageSlab(page))
90 return ksize(objp);
91
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070092 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +000093 * 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 Mundt6cfd53fc2008-06-05 22:46:08 -0700107 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900108 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700109 */
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700110 return page_size(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Paul Mundtdfc2f912009-06-26 04:31:57 +0900113/**
114 * follow_pfn - look up PFN at a user virtual address
115 * @vma: memory mapping
116 * @address: user virtual address
117 * @pfn: location to store found PFN
118 *
119 * Only IO mappings and raw PFN mappings are allowed.
120 *
121 * Returns zero and the pfn at @pfn on success, -ve otherwise.
122 */
123int follow_pfn(struct vm_area_struct *vma, unsigned long address,
124 unsigned long *pfn)
125{
126 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
127 return -EINVAL;
128
129 *pfn = address >> PAGE_SHIFT;
130 return 0;
131}
132EXPORT_SYMBOL(follow_pfn);
133
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700134LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800136void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 kfree(addr);
139}
Paul Mundtb5073172007-07-21 04:37:25 -0700140EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700142void *__vmalloc(unsigned long size, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200145 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
146 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
Nick Piggin84097512006-03-22 00:08:34 -0800148 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Paul Mundtb5073172007-07-21 04:37:25 -0700150EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Christoph Hellwig041de932020-06-01 21:52:02 -0700152void *__vmalloc_node_range(unsigned long size, unsigned long align,
153 unsigned long start, unsigned long end, gfp_t gfp_mask,
154 pgprot_t prot, unsigned long vm_flags, int node,
155 const void *caller)
156{
157 return __vmalloc(size, gfp_mask);
158}
159
Christoph Hellwig2b905942020-06-01 21:51:53 -0700160void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
161 int node, const void *caller)
Michal Hockoa7c3e902017-05-08 15:57:09 -0700162{
Christoph Hellwig2b905942020-06-01 21:51:53 -0700163 return __vmalloc(size, gfp_mask);
Michal Hockoa7c3e902017-05-08 15:57:09 -0700164}
165
Andrii Nakryikoed817452019-11-23 14:08:35 -0800166static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
Paul Mundtf905bc42008-02-04 22:29:59 -0800167{
168 void *ret;
169
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700170 ret = __vmalloc(size, flags);
Paul Mundtf905bc42008-02-04 22:29:59 -0800171 if (ret) {
172 struct vm_area_struct *vma;
173
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700174 mmap_write_lock(current->mm);
Paul Mundtf905bc42008-02-04 22:29:59 -0800175 vma = find_vma(current->mm, (unsigned long)ret);
176 if (vma)
177 vma->vm_flags |= VM_USERMAP;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700178 mmap_write_unlock(current->mm);
Paul Mundtf905bc42008-02-04 22:29:59 -0800179 }
180
181 return ret;
182}
Andrii Nakryikoed817452019-11-23 14:08:35 -0800183
184void *vmalloc_user(unsigned long size)
185{
186 return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
187}
Paul Mundtf905bc42008-02-04 22:29:59 -0800188EXPORT_SYMBOL(vmalloc_user);
189
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800190struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 return virt_to_page(addr);
193}
Paul Mundtb5073172007-07-21 04:37:25 -0700194EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800196unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 return page_to_pfn(virt_to_page(addr));
199}
Paul Mundtb5073172007-07-21 04:37:25 -0700200EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202long vread(char *buf, char *addr, unsigned long count)
203{
Chen Gang9bde9162013-07-03 15:02:36 -0700204 /* Don't allow overflow */
205 if ((unsigned long) buf + count < count)
206 count = -(unsigned long) buf;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 memcpy(buf, addr, count);
209 return count;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900213 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
215 * @size: allocation size
216 *
217 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900218 * allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200220 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * use __vmalloc() instead.
222 */
223void *vmalloc(unsigned long size)
224{
Chen Li176056f2021-06-30 18:52:14 -0700225 return __vmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Andrew Mortonf6138882006-02-28 16:59:18 -0800227EXPORT_SYMBOL(vmalloc);
228
Dave Younge1ca7782010-10-26 14:22:06 -0700229/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900230 * vzalloc - allocate virtually contiguous memory with zero fill
Dave Younge1ca7782010-10-26 14:22:06 -0700231 *
232 * @size: allocation size
233 *
234 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900235 * allocator and map them into contiguous kernel virtual space.
Dave Younge1ca7782010-10-26 14:22:06 -0700236 * The memory allocated is set to zero.
237 *
238 * For tight control over page level allocator and protection flags
239 * use __vmalloc() instead.
240 */
241void *vzalloc(unsigned long size)
242{
Chen Li176056f2021-06-30 18:52:14 -0700243 return __vmalloc(size, GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -0700244}
245EXPORT_SYMBOL(vzalloc);
246
247/**
248 * vmalloc_node - allocate memory on a specific node
249 * @size: allocation size
250 * @node: numa node
251 *
252 * Allocate enough pages to cover @size from the page level
253 * allocator and map them into contiguous kernel virtual space.
254 *
255 * For tight control over page level allocator and protection flags
256 * use __vmalloc() instead.
257 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800258void *vmalloc_node(unsigned long size, int node)
259{
260 return vmalloc(size);
261}
Paul Mundt9a14f652010-12-24 11:50:34 +0900262EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700263
264/**
265 * vzalloc_node - allocate memory on a specific node with zero fill
266 * @size: allocation size
267 * @node: numa node
268 *
269 * Allocate enough pages to cover @size from the page level
270 * allocator and map them into contiguous kernel virtual space.
271 * The memory allocated is set to zero.
272 *
273 * For tight control over page level allocator and protection flags
274 * use __vmalloc() instead.
275 */
276void *vzalloc_node(unsigned long size, int node)
277{
278 return vzalloc(size);
279}
280EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Paul Mundt1af446e2008-08-04 16:01:47 +0900282/**
Paul Mundtb5073172007-07-21 04:37:25 -0700283 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * @size: allocation size
285 *
286 * Allocate enough 32bit PA addressable pages to cover @size from the
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900287 * page level allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289void *vmalloc_32(unsigned long size)
290{
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700291 return __vmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Paul Mundtb5073172007-07-21 04:37:25 -0700293EXPORT_SYMBOL(vmalloc_32);
294
295/**
296 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
297 * @size: allocation size
298 *
299 * The resulting memory area is 32bit addressable and zeroed so it can be
300 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800301 *
302 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
303 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700304 */
305void *vmalloc_32_user(unsigned long size)
306{
Paul Mundtf905bc42008-02-04 22:29:59 -0800307 /*
308 * We'll have to sort out the ZONE_DMA bits for 64-bit,
309 * but for now this can simply use vmalloc_user() directly.
310 */
311 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700312}
313EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
316{
317 BUG();
318 return NULL;
319}
Paul Mundtb5073172007-07-21 04:37:25 -0700320EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800322void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 BUG();
325}
Paul Mundtb5073172007-07-21 04:37:25 -0700326EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Christoph Hellwigd4efd792020-06-01 21:51:27 -0700328void *vm_map_ram(struct page **pages, unsigned int count, int node)
Paul Mundteb6434d2009-01-21 17:45:47 +0900329{
330 BUG();
331 return NULL;
332}
333EXPORT_SYMBOL(vm_map_ram);
334
335void vm_unmap_ram(const void *mem, unsigned int count)
336{
337 BUG();
338}
339EXPORT_SYMBOL(vm_unmap_ram);
340
341void vm_unmap_aliases(void)
342{
343}
344EXPORT_SYMBOL_GPL(vm_unmap_aliases);
345
Paul Mundt29c185e2010-12-24 12:08:30 +0900346void free_vm_area(struct vm_struct *area)
347{
348 BUG();
349}
350EXPORT_SYMBOL_GPL(free_vm_area);
351
Paul Mundtb5073172007-07-21 04:37:25 -0700352int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
353 struct page *page)
354{
355 return -EINVAL;
356}
357EXPORT_SYMBOL(vm_insert_page);
358
Souptick Joardera667d742019-05-13 17:21:56 -0700359int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
360 unsigned long num)
361{
362 return -EINVAL;
363}
364EXPORT_SYMBOL(vm_map_pages);
365
366int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
367 unsigned long num)
368{
369 return -EINVAL;
370}
371EXPORT_SYMBOL(vm_map_pages_zero);
372
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * sys_brk() for the most part doesn't need the global kernel
375 * lock, except when an application is doing something nasty
376 * like trying to un-brk an area that has already been mapped
377 * to a regular file. in this case, the unmapping will need
378 * to invoke file system routines that need the global lock.
379 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100380SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct mm_struct *mm = current->mm;
383
384 if (brk < mm->start_brk || brk > mm->context.end_brk)
385 return mm->brk;
386
387 if (mm->brk == brk)
388 return mm->brk;
389
390 /*
391 * Always allow shrinking brk
392 */
393 if (brk <= mm->brk) {
394 mm->brk = brk;
395 return brk;
396 }
397
398 /*
399 * Ok, looks good - let it rip.
400 */
Christoph Hellwiga75a2df2020-06-07 21:42:49 -0700401 flush_icache_user_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return mm->brk = brk;
403}
404
David Howells8feae132009-01-08 12:04:47 +0000405/*
seokhoon.yoon3edf41d2017-02-24 14:56:44 -0800406 * initialise the percpu counter for VM and region record slabs
David Howells8feae132009-01-08 12:04:47 +0000407 */
408void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700410 int ret;
411
Tejun Heo908c7f12014-09-08 09:51:29 +0900412 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700413 VM_BUG_ON(ret);
Vladimir Davydov5d097052016-01-14 15:18:21 -0800414 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
David Howells8feae132009-01-08 12:04:47 +0000415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells8feae132009-01-08 12:04:47 +0000417/*
418 * validate the region tree
419 * - the caller must hold the region lock
420 */
421#ifdef CONFIG_DEBUG_NOMMU_REGIONS
422static noinline void validate_nommu_regions(void)
423{
424 struct vm_region *region, *last;
425 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
David Howells8feae132009-01-08 12:04:47 +0000427 lastp = rb_first(&nommu_region_tree);
428 if (!lastp)
429 return;
430
431 last = rb_entry(lastp, struct vm_region, vm_rb);
Geliang Tangc9427bc2015-11-05 18:48:38 -0800432 BUG_ON(last->vm_end <= last->vm_start);
433 BUG_ON(last->vm_top < last->vm_end);
David Howells8feae132009-01-08 12:04:47 +0000434
435 while ((p = rb_next(lastp))) {
436 region = rb_entry(p, struct vm_region, vm_rb);
437 last = rb_entry(lastp, struct vm_region, vm_rb);
438
Geliang Tangc9427bc2015-11-05 18:48:38 -0800439 BUG_ON(region->vm_end <= region->vm_start);
440 BUG_ON(region->vm_top < region->vm_end);
441 BUG_ON(region->vm_start < last->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000442
443 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445}
David Howells8feae132009-01-08 12:04:47 +0000446#else
David Howells33e5d7692009-04-02 16:56:32 -0700447static void validate_nommu_regions(void)
448{
449}
David Howells8feae132009-01-08 12:04:47 +0000450#endif
451
452/*
453 * add a region into the global tree
454 */
455static void add_nommu_region(struct vm_region *region)
456{
457 struct vm_region *pregion;
458 struct rb_node **p, *parent;
459
460 validate_nommu_regions();
461
David Howells8feae132009-01-08 12:04:47 +0000462 parent = NULL;
463 p = &nommu_region_tree.rb_node;
464 while (*p) {
465 parent = *p;
466 pregion = rb_entry(parent, struct vm_region, vm_rb);
467 if (region->vm_start < pregion->vm_start)
468 p = &(*p)->rb_left;
469 else if (region->vm_start > pregion->vm_start)
470 p = &(*p)->rb_right;
471 else if (pregion == region)
472 return;
473 else
474 BUG();
475 }
476
477 rb_link_node(&region->vm_rb, parent, p);
478 rb_insert_color(&region->vm_rb, &nommu_region_tree);
479
480 validate_nommu_regions();
481}
482
483/*
484 * delete a region from the global tree
485 */
486static void delete_nommu_region(struct vm_region *region)
487{
488 BUG_ON(!nommu_region_tree.rb_node);
489
490 validate_nommu_regions();
491 rb_erase(&region->vm_rb, &nommu_region_tree);
492 validate_nommu_regions();
493}
494
495/*
496 * free a contiguous series of pages
497 */
498static void free_page_series(unsigned long from, unsigned long to)
499{
500 for (; from < to; from += PAGE_SIZE) {
501 struct page *page = virt_to_page(from);
502
David Howells33e5d7692009-04-02 16:56:32 -0700503 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000504 put_page(page);
505 }
506}
507
508/*
509 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700510 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000511 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000512 * will equal vm_start
513 */
514static void __put_nommu_region(struct vm_region *region)
515 __releases(nommu_region_sem)
516{
David Howells8feae132009-01-08 12:04:47 +0000517 BUG_ON(!nommu_region_tree.rb_node);
518
David Howells1e2ae592010-01-15 17:01:33 -0800519 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000520 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000521 delete_nommu_region(region);
522 up_write(&nommu_region_sem);
523
524 if (region->vm_file)
525 fput(region->vm_file);
526
527 /* IO memory and memory shared directly out of the pagecache
528 * from ramfs/tmpfs mustn't be released here */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700529 if (region->vm_flags & VM_MAPPED_COPY)
Paul Mundtdd8632a2009-01-08 12:04:47 +0000530 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000531 kmem_cache_free(vm_region_jar, region);
532 } else {
533 up_write(&nommu_region_sem);
534 }
535}
536
537/*
538 * release a reference to a region
539 */
540static void put_nommu_region(struct vm_region *region)
541{
542 down_write(&nommu_region_sem);
543 __put_nommu_region(region);
544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
David Howells30340972006-09-27 01:50:20 -0700546/*
547 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000548 * and tree and add to the address space's page tree also if not an anonymous
549 * page
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700550 * - should be called with mm->mmap_lock held writelocked
David Howells30340972006-09-27 01:50:20 -0700551 */
David Howells8feae132009-01-08 12:04:47 +0000552static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700553{
Namhyung Kim6038def2011-05-24 17:11:22 -0700554 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000555 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700556 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700557
David Howells8feae132009-01-08 12:04:47 +0000558 BUG_ON(!vma->vm_region);
559
560 mm->map_count++;
561 vma->vm_mm = mm;
562
563 /* add the VMA to the mapping */
564 if (vma->vm_file) {
565 mapping = vma->vm_file->f_mapping;
566
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800567 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000568 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700569 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000570 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800571 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000572 }
573
574 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700575 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000576 p = &mm->mm_rb.rb_node;
577 while (*p) {
578 parent = *p;
579 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
580
581 /* sort by: start addr, end addr, VMA struct addr in that order
582 * (the latter is necessary as we may get identical VMAs) */
583 if (vma->vm_start < pvma->vm_start)
584 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700585 else if (vma->vm_start > pvma->vm_start) {
586 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000587 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700588 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000589 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700590 else if (vma->vm_end > pvma->vm_end) {
591 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000592 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700593 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000594 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700595 else if (vma > pvma) {
596 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000597 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700598 } else
David Howells8feae132009-01-08 12:04:47 +0000599 BUG();
600 }
601
602 rb_link_node(&vma->vm_rb, parent, p);
603 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
604
605 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700606 prev = NULL;
607 if (rb_prev)
608 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700609
Wei Yangaba6dfb2019-11-30 17:50:53 -0800610 __vma_link_list(mm, vma, prev);
David Howells8feae132009-01-08 12:04:47 +0000611}
612
613/*
614 * delete a VMA from its owning mm_struct and address space
615 */
616static void delete_vma_from_mm(struct vm_area_struct *vma)
617{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700618 int i;
David Howells8feae132009-01-08 12:04:47 +0000619 struct address_space *mapping;
620 struct mm_struct *mm = vma->vm_mm;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700621 struct task_struct *curr = current;
David Howells8feae132009-01-08 12:04:47 +0000622
David Howells8feae132009-01-08 12:04:47 +0000623 mm->map_count--;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700624 for (i = 0; i < VMACACHE_SIZE; i++) {
625 /* if the vma is cached, invalidate the entire cache */
Ingo Molnar314ff782017-02-03 11:03:31 +0100626 if (curr->vmacache.vmas[i] == vma) {
Steven Miaoe020d5b2014-06-23 13:22:02 -0700627 vmacache_invalidate(mm);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700628 break;
629 }
630 }
David Howells8feae132009-01-08 12:04:47 +0000631
632 /* remove the VMA from the mapping */
633 if (vma->vm_file) {
634 mapping = vma->vm_file->f_mapping;
635
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800636 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000637 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700638 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000639 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800640 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000641 }
642
643 /* remove from the MM's tree and list */
644 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700645
Wei Yang1b9fc5b22019-11-30 17:50:49 -0800646 __vma_unlink_list(mm, vma);
David Howells8feae132009-01-08 12:04:47 +0000647}
648
649/*
650 * destroy a VMA record
651 */
652static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
653{
David Howells8feae132009-01-08 12:04:47 +0000654 if (vma->vm_ops && vma->vm_ops->close)
655 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700656 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000657 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000658 put_nommu_region(vma->vm_region);
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700659 vm_area_free(vma);
David Howells30340972006-09-27 01:50:20 -0700660}
661
662/*
663 * look up the first VMA in which addr resides, NULL if none
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700664 * - should be called with mm->mmap_lock at least held readlocked
David Howells30340972006-09-27 01:50:20 -0700665 */
666struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
667{
David Howells8feae132009-01-08 12:04:47 +0000668 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700669
David Howells8feae132009-01-08 12:04:47 +0000670 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700671 vma = vmacache_find(mm, addr);
672 if (likely(vma))
David Howells8feae132009-01-08 12:04:47 +0000673 return vma;
674
Namhyung Kime922c4c2011-05-24 17:11:24 -0700675 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000676 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700677 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000678 if (vma->vm_start > addr)
679 return NULL;
680 if (vma->vm_end > addr) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700681 vmacache_update(addr, vma);
David Howells8feae132009-01-08 12:04:47 +0000682 return vma;
683 }
David Howells30340972006-09-27 01:50:20 -0700684 }
685
David Howells30340972006-09-27 01:50:20 -0700686 return NULL;
687}
688EXPORT_SYMBOL(find_vma);
689
690/*
David Howells930e6522006-09-27 01:50:22 -0700691 * find a VMA
692 * - we don't extend stack VMAs under NOMMU conditions
693 */
694struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
695{
David Howells7561e8c2010-03-25 16:48:38 +0000696 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700697}
698
David Howells8feae132009-01-08 12:04:47 +0000699/*
700 * expand a stack to a given address
701 * - not supported under NOMMU conditions
702 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700703int expand_stack(struct vm_area_struct *vma, unsigned long address)
704{
705 return -ENOMEM;
706}
707
David Howells930e6522006-09-27 01:50:22 -0700708/*
David Howells6fa5f802006-09-27 01:50:21 -0700709 * look up the first VMA exactly that exactly matches addr
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700710 * - should be called with mm->mmap_lock at least held readlocked
David Howells6fa5f802006-09-27 01:50:21 -0700711 */
David Howells8feae132009-01-08 12:04:47 +0000712static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
713 unsigned long addr,
714 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000717 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
David Howells8feae132009-01-08 12:04:47 +0000719 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700720 vma = vmacache_find_exact(mm, addr, end);
721 if (vma)
David Howells8feae132009-01-08 12:04:47 +0000722 return vma;
723
Namhyung Kime922c4c2011-05-24 17:11:24 -0700724 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000725 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700726 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000727 if (vma->vm_start < addr)
728 continue;
729 if (vma->vm_start > addr)
730 return NULL;
731 if (vma->vm_end == end) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700732 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return vma;
David Howells8feae132009-01-08 12:04:47 +0000734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
737 return NULL;
738}
739
David Howells30340972006-09-27 01:50:20 -0700740/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * determine whether a mapping should be permitted and, if so, what sort of
742 * mapping we're capable of supporting
743 */
744static int validate_mmap_request(struct file *file,
745 unsigned long addr,
746 unsigned long len,
747 unsigned long prot,
748 unsigned long flags,
749 unsigned long pgoff,
750 unsigned long *_capabilities)
751{
David Howells8feae132009-01-08 12:04:47 +0000752 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int ret;
754
755 /* do the simple checks first */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700756 if (flags & MAP_FIXED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
760 (flags & MAP_TYPE) != MAP_SHARED)
761 return -EINVAL;
762
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000763 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -EINVAL;
765
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000766 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000767 rlen = PAGE_ALIGN(len);
768 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000769 return -ENOMEM;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000772 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000773 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* files must support mmap */
Al Viro72c2d532013-09-22 16:27:52 -0400777 if (!file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -ENODEV;
779
780 /* work out if what we've got could possibly be shared
781 * - we support chardevs that provide their own "memory"
782 * - we support files/blockdevs that are memory backed
783 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100784 if (file->f_op->mmap_capabilities) {
785 capabilities = file->f_op->mmap_capabilities(file);
786 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* no explicit capabilities set, so assume some
788 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500789 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case S_IFREG:
791 case S_IFBLK:
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100792 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794
795 case S_IFCHR:
796 capabilities =
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100797 NOMMU_MAP_DIRECT |
798 NOMMU_MAP_READ |
799 NOMMU_MAP_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 break;
801
802 default:
803 return -EINVAL;
804 }
805 }
806
807 /* eliminate any capabilities that we can't support on this
808 * device */
809 if (!file->f_op->get_unmapped_area)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100810 capabilities &= ~NOMMU_MAP_DIRECT;
Al Viro6e242a12015-03-31 12:35:13 -0400811 if (!(file->f_mode & FMODE_CAN_READ))
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100812 capabilities &= ~NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Graff Yang28d7a6a2009-08-18 14:11:17 -0700814 /* The file shall have been opened with read permission. */
815 if (!(file->f_mode & FMODE_READ))
816 return -EACCES;
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (flags & MAP_SHARED) {
819 /* do checks for writing, appending and locking */
820 if ((prot & PROT_WRITE) &&
821 !(file->f_mode & FMODE_WRITE))
822 return -EACCES;
823
Al Viro496ad9a2013-01-23 17:07:38 -0500824 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 (file->f_mode & FMODE_WRITE))
826 return -EACCES;
827
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100828 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -ENODEV;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* we mustn't privatise shared mappings */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100832 capabilities &= ~NOMMU_MAP_COPY;
Choi Gi-yongac714902014-04-07 15:37:36 -0700833 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* we're going to read the file into private memory we
835 * allocate */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100836 if (!(capabilities & NOMMU_MAP_COPY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -ENODEV;
838
839 /* we don't permit a private writable mapping to be
840 * shared with the backing device */
841 if (prot & PROT_WRITE)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100842 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100845 if (capabilities & NOMMU_MAP_DIRECT) {
846 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
847 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
848 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700849 ) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100850 capabilities &= ~NOMMU_MAP_DIRECT;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700851 if (flags & MAP_SHARED) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700852 pr_warn("MAP_SHARED not completely supported on !MMU\n");
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700853 return -EINVAL;
854 }
855 }
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* handle executable mappings and implied executable
859 * mappings */
Eric W. Biederman90f85722015-06-29 14:42:03 -0500860 if (path_noexec(&file->f_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (prot & PROT_EXEC)
862 return -EPERM;
Choi Gi-yongac714902014-04-07 15:37:36 -0700863 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* handle implication of PROT_EXEC by PROT_READ */
865 if (current->personality & READ_IMPLIES_EXEC) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100866 if (capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 prot |= PROT_EXEC;
868 }
Choi Gi-yongac714902014-04-07 15:37:36 -0700869 } else if ((prot & PROT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 (prot & PROT_EXEC) &&
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100871 !(capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ) {
873 /* backing file is not executable, try to copy */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100874 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Choi Gi-yongac714902014-04-07 15:37:36 -0700876 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* anonymous mappings are always memory backed and can be
878 * privately mapped
879 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100880 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* handle PROT_EXEC implication by PROT_READ */
883 if ((prot & PROT_READ) &&
884 (current->personality & READ_IMPLIES_EXEC))
885 prot |= PROT_EXEC;
886 }
887
888 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -0400889 ret = security_mmap_addr(addr);
890 if (ret < 0)
891 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* looks okay */
894 *_capabilities = capabilities;
895 return 0;
896}
897
898/*
899 * we've determined that we can make the mapping, now translate what we
900 * now know into VMA flags
901 */
902static unsigned long determine_vm_flags(struct file *file,
903 unsigned long prot,
904 unsigned long flags,
905 unsigned long capabilities)
906{
907 unsigned long vm_flags;
908
Dave Hansene6bfb702016-02-12 13:02:31 -0800909 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* vm_flags |= mm->def_flags; */
911
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100912 if (!(capabilities & NOMMU_MAP_DIRECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700914 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (file && !(prot & PROT_WRITE))
916 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700917 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* overlay a shareable mapping on the backing device or inode
919 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
920 * romfs/cramfs */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100921 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700923 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
926 /* refuse to let anyone share private mappings with this process if
927 * it's being traced - otherwise breakpoints set in it may interfere
928 * with another untraced process
929 */
Tejun Heoa288eec2011-06-17 16:50:37 +0200930 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 vm_flags &= ~VM_MAYSHARE;
932
933 return vm_flags;
934}
935
936/*
David Howells8feae132009-01-08 12:04:47 +0000937 * set up a shared mapping on a file (the driver or filesystem provides and
938 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
David Howells8feae132009-01-08 12:04:47 +0000940static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 int ret;
943
Miklos Szeredif74ac012017-02-20 16:51:23 +0100944 ret = call_mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +0000945 if (ret == 0) {
946 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +0100947 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +0000948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (ret != -ENOSYS)
950 return ret;
951
David Howells3fa30462010-03-23 13:35:21 -0700952 /* getting -ENOSYS indicates that direct mmap isn't possible (as
953 * opposed to tried but failed) so we can only give a suitable error as
954 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENODEV;
956}
957
958/*
959 * set up a private mapping or an anonymous shared mapping
960 */
David Howells8feae132009-01-08 12:04:47 +0000961static int do_mmap_private(struct vm_area_struct *vma,
962 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +0100963 unsigned long len,
964 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Joonsoo Kimdbc83582014-12-12 16:55:55 -0800966 unsigned long total, point;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 void *base;
David Howells8feae132009-01-08 12:04:47 +0000968 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* invoke the file's mapping function so that it can keep track of
971 * shared mappings on devices or memory
972 * - VM_MAYSHARE will be set if it may attempt to share
973 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100974 if (capabilities & NOMMU_MAP_DIRECT) {
Miklos Szeredif74ac012017-02-20 16:51:23 +0100975 ret = call_mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +0000976 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +0000978 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
979 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +0100980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Paul Mundtdd8632a2009-01-08 12:04:47 +0000982 if (ret != -ENOSYS)
983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /* getting an ENOSYS error indicates that direct mmap isn't
986 * possible (as opposed to tried but failed) so we'll try to
987 * make a private copy of the data and map that instead */
988 }
989
David Howells8feae132009-01-08 12:04:47 +0000990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* allocate some memory to hold the mapping
992 * - note that this may not return a page-aligned address if the object
993 * we're allocating is smaller than a page
994 */
Bob Liuf67d9b12011-05-24 17:12:56 -0700995 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +0000996 total = 1 << order;
Bob Liuf67d9b12011-05-24 17:12:56 -0700997 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +0000998
Joonsoo Kimdbc83582014-12-12 16:55:55 -0800999 /* we don't want to allocate a power-of-2 sized page set */
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001000 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001001 total = point;
David Howells8feae132009-01-08 12:04:47 +00001002
Joonsoo Kimda616532015-02-27 15:51:43 -08001003 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001004 if (!base)
1005 goto enomem;
David Howells8feae132009-01-08 12:04:47 +00001006
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001007 atomic_long_add(total, &mmap_pages_allocated);
1008
David Howells8feae132009-01-08 12:04:47 +00001009 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1010 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001011 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001012 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001013
1014 vma->vm_start = region->vm_start;
1015 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (vma->vm_file) {
1018 /* read the contents of a file into the copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 loff_t fpos;
1020
1021 fpos = vma->vm_pgoff;
1022 fpos <<= PAGE_SHIFT;
1023
Christoph Hellwigb4bf8022017-09-01 17:39:17 +02001024 ret = kernel_read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (ret < 0)
1026 goto error_free;
1027
1028 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001029 if (ret < len)
1030 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -07001032 } else {
1033 vma_set_anonymous(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 return 0;
1037
1038error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001039 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001040 region->vm_start = vma->vm_start = 0;
1041 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001042 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return ret;
1044
1045enomem:
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07001046 pr_err("Allocation of length %lu from process %d (%s) failed\n",
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001047 len, current->pid, current->comm);
Michal Hocko9af744d2017-02-22 15:46:16 -08001048 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -ENOMEM;
1050}
1051
1052/*
1053 * handle mapping creation for uClinux
1054 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001055unsigned long do_mmap(struct file *file,
1056 unsigned long addr,
1057 unsigned long len,
1058 unsigned long prot,
1059 unsigned long flags,
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001060 unsigned long pgoff,
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001061 unsigned long *populate,
1062 struct list_head *uf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
David Howells8feae132009-01-08 12:04:47 +00001064 struct vm_area_struct *vma;
1065 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct rb_node *rb;
Peter Collingbourne45e55302020-08-06 23:23:37 -07001067 vm_flags_t vm_flags;
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001068 unsigned long capabilities, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 int ret;
1070
Michel Lespinasse41badc12013-02-22 16:32:47 -08001071 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* decide whether we should attempt the mapping, and if so what sort of
1074 * mapping */
1075 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1076 &capabilities);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001077 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return ret;
1079
David Howells06aab5a2009-09-24 12:33:48 +01001080 /* we ignore the address hint */
1081 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001082 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* we've determined that we can make the mapping, now translate what we
1085 * now know into VMA flags */
Peter Collingbourne45e55302020-08-06 23:23:37 -07001086 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
David Howells8feae132009-01-08 12:04:47 +00001088 /* we're going to need to record the mapping */
1089 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1090 if (!region)
1091 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds490fc052018-07-21 15:24:03 -07001093 vma = vm_area_alloc(current->mm);
David Howells8feae132009-01-08 12:04:47 +00001094 if (!vma)
1095 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
David Howells1e2ae592010-01-15 17:01:33 -08001097 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001098 region->vm_flags = vm_flags;
1099 region->vm_pgoff = pgoff;
1100
David Howells8feae132009-01-08 12:04:47 +00001101 vma->vm_flags = vm_flags;
1102 vma->vm_pgoff = pgoff;
1103
1104 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001105 region->vm_file = get_file(file);
1106 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001107 }
1108
1109 down_write(&nommu_region_sem);
1110
1111 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001113 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * - shared mappings on character devices and memory backed files are
1115 * permitted to overlap inexactly as far as we are concerned for in
1116 * these cases, sharing is handled in the driver or filesystem rather
1117 * than here
1118 */
1119 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001120 struct vm_region *pregion;
1121 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
David Howells8feae132009-01-08 12:04:47 +00001123 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1124 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001125
David Howells8feae132009-01-08 12:04:47 +00001126 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1127 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
David Howells8feae132009-01-08 12:04:47 +00001129 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 continue;
1131
1132 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001133 if (file_inode(pregion->vm_file) !=
1134 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 continue;
1136
David Howells8feae132009-01-08 12:04:47 +00001137 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 continue;
1139
David Howells8feae132009-01-08 12:04:47 +00001140 rpglen = pregion->vm_end - pregion->vm_start;
1141 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1142 rpgend = pregion->vm_pgoff + rpglen;
1143 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 continue;
1145
David Howells8feae132009-01-08 12:04:47 +00001146 /* handle inexactly overlapping matches between
1147 * mappings */
1148 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1149 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1150 /* new mapping is not a subset of the region */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001151 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto sharing_violation;
1153 continue;
1154 }
1155
David Howells8feae132009-01-08 12:04:47 +00001156 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001157 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001158 vma->vm_region = pregion;
1159 start = pregion->vm_start;
1160 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1161 vma->vm_start = start;
1162 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001164 if (pregion->vm_flags & VM_MAPPED_COPY)
David Howells8feae132009-01-08 12:04:47 +00001165 vma->vm_flags |= VM_MAPPED_COPY;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001166 else {
David Howells8feae132009-01-08 12:04:47 +00001167 ret = do_mmap_shared_file(vma);
1168 if (ret < 0) {
1169 vma->vm_region = NULL;
1170 vma->vm_start = 0;
1171 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001172 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001173 pregion = NULL;
1174 goto error_just_free;
1175 }
1176 }
1177 fput(region->vm_file);
1178 kmem_cache_free(vm_region_jar, region);
1179 region = pregion;
1180 result = start;
1181 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* obtain the address at which to make a shared mapping
1185 * - this is the hook for quasi-memory character devices to
1186 * tell us the location of a shared mapping
1187 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001188 if (capabilities & NOMMU_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 addr = file->f_op->get_unmapped_area(file, addr, len,
1190 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001191 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001193 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001194 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 /* the driver refused to tell us where to site
1197 * the mapping so we'll have to attempt to copy
1198 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001199 ret = -ENODEV;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001200 if (!(capabilities & NOMMU_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001201 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001203 capabilities &= ~NOMMU_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001204 } else {
1205 vma->vm_start = region->vm_start = addr;
1206 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208 }
1209 }
1210
David Howells8feae132009-01-08 12:04:47 +00001211 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David Howells645d83c2009-09-24 15:13:10 +01001213 /* set up the mapping
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001214 * - the region is filled in if NOMMU_MAP_DIRECT is still set
David Howells645d83c2009-09-24 15:13:10 +01001215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001217 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 else
David Howells645d83c2009-09-24 15:13:10 +01001219 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001221 goto error_just_free;
1222 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001223
Jie Zhangea637632009-12-14 18:00:02 -08001224 /* clear anonymous mappings that don't ask for uninitialized data */
Christoph Hellwig0bf5f942019-07-16 16:26:27 -07001225 if (!vma->vm_file &&
1226 (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
1227 !(flags & MAP_UNINITIALIZED)))
Jie Zhangea637632009-12-14 18:00:02 -08001228 memset((void *)region->vm_start, 0,
1229 region->vm_end - region->vm_start);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001232 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 current->mm->total_vm += len >> PAGE_SHIFT;
1235
David Howells8feae132009-01-08 12:04:47 +00001236share:
1237 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Mike Frysingercfe79c02010-01-06 17:23:23 +00001239 /* we flush the region from the icache only when the first executable
1240 * mapping of it is made */
1241 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
Christoph Hellwiga75a2df2020-06-07 21:42:49 -07001242 flush_icache_user_range(region->vm_start, region->vm_end);
Mike Frysingercfe79c02010-01-06 17:23:23 +00001243 region->vm_icache_flushed = true;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Mike Frysingercfe79c02010-01-06 17:23:23 +00001246 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
David Howells8feae132009-01-08 12:04:47 +00001248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
David Howells8feae132009-01-08 12:04:47 +00001250error_just_free:
1251 up_write(&nommu_region_sem);
1252error:
David Howells89a86402009-10-30 13:13:26 +00001253 if (region->vm_file)
1254 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001255 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001256 if (vma->vm_file)
1257 fput(vma->vm_file);
Linus Torvalds3928d4f2018-07-21 13:48:51 -07001258 vm_area_free(vma);
David Howells8feae132009-01-08 12:04:47 +00001259 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
David Howells8feae132009-01-08 12:04:47 +00001261sharing_violation:
1262 up_write(&nommu_region_sem);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001263 pr_warn("Attempt to share mismatched mappings\n");
David Howells8feae132009-01-08 12:04:47 +00001264 ret = -EINVAL;
1265 goto error;
1266
1267error_getting_vma:
1268 kmem_cache_free(vm_region_jar, region);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001269 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1270 len, current->pid);
Michal Hocko9af744d2017-02-22 15:46:16 -08001271 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -ENOMEM;
1273
David Howells8feae132009-01-08 12:04:47 +00001274error_getting_region:
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001275 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1276 len, current->pid);
Michal Hocko9af744d2017-02-22 15:46:16 -08001277 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -ENOMEM;
1279}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001280
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001281unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1282 unsigned long prot, unsigned long flags,
1283 unsigned long fd, unsigned long pgoff)
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001284{
1285 struct file *file = NULL;
1286 unsigned long retval = -EBADF;
1287
Al Viro120a7952010-10-30 02:54:44 -04001288 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001289 if (!(flags & MAP_ANONYMOUS)) {
1290 file = fget(fd);
1291 if (!file)
1292 goto out;
1293 }
1294
Greg Ungererad1ed292012-06-04 14:29:59 +10001295 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001296
1297 if (file)
1298 fput(file);
1299out:
1300 return retval;
1301}
1302
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001303SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1304 unsigned long, prot, unsigned long, flags,
1305 unsigned long, fd, unsigned long, pgoff)
1306{
1307 return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1308}
1309
Christoph Hellwiga4679372010-03-10 15:21:15 -08001310#ifdef __ARCH_WANT_SYS_OLD_MMAP
1311struct mmap_arg_struct {
1312 unsigned long addr;
1313 unsigned long len;
1314 unsigned long prot;
1315 unsigned long flags;
1316 unsigned long fd;
1317 unsigned long offset;
1318};
1319
1320SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1321{
1322 struct mmap_arg_struct a;
1323
1324 if (copy_from_user(&a, arg, sizeof(a)))
1325 return -EFAULT;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001326 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001327 return -EINVAL;
1328
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001329 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1330 a.offset >> PAGE_SHIFT);
Christoph Hellwiga4679372010-03-10 15:21:15 -08001331}
1332#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/*
David Howells8feae132009-01-08 12:04:47 +00001335 * split a vma into two pieces at address 'addr', a new vma is allocated either
1336 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
David Howells8feae132009-01-08 12:04:47 +00001338int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1339 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
David Howells8feae132009-01-08 12:04:47 +00001341 struct vm_area_struct *new;
1342 struct vm_region *region;
1343 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
David Howells779c1022010-01-15 17:01:34 -08001345 /* we're only permitted to split anonymous regions (these should have
1346 * only a single usage on the region) */
1347 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001348 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
David Howells8feae132009-01-08 12:04:47 +00001350 if (mm->map_count >= sysctl_max_map_count)
1351 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
David Howells8feae132009-01-08 12:04:47 +00001353 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1354 if (!region)
1355 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds3928d4f2018-07-21 13:48:51 -07001357 new = vm_area_dup(vma);
David Howells8feae132009-01-08 12:04:47 +00001358 if (!new) {
1359 kmem_cache_free(vm_region_jar, region);
1360 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
David Howells8feae132009-01-08 12:04:47 +00001362
1363 /* most fields are the same, copy all, and then fixup */
David Howells8feae132009-01-08 12:04:47 +00001364 *region = *vma->vm_region;
1365 new->vm_region = region;
1366
1367 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1368
1369 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001370 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001371 } else {
1372 region->vm_start = new->vm_start = addr;
1373 region->vm_pgoff = new->vm_pgoff += npages;
1374 }
1375
1376 if (new->vm_ops && new->vm_ops->open)
1377 new->vm_ops->open(new);
1378
1379 delete_vma_from_mm(vma);
1380 down_write(&nommu_region_sem);
1381 delete_nommu_region(vma->vm_region);
1382 if (new_below) {
1383 vma->vm_region->vm_start = vma->vm_start = addr;
1384 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1385 } else {
1386 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001387 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001388 }
1389 add_nommu_region(vma->vm_region);
1390 add_nommu_region(new->vm_region);
1391 up_write(&nommu_region_sem);
1392 add_vma_to_mm(mm, vma);
1393 add_vma_to_mm(mm, new);
1394 return 0;
1395}
1396
1397/*
1398 * shrink a VMA by removing the specified chunk from either the beginning or
1399 * the end
1400 */
1401static int shrink_vma(struct mm_struct *mm,
1402 struct vm_area_struct *vma,
1403 unsigned long from, unsigned long to)
1404{
1405 struct vm_region *region;
1406
David Howells8feae132009-01-08 12:04:47 +00001407 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1408 * and list */
1409 delete_vma_from_mm(vma);
1410 if (from > vma->vm_start)
1411 vma->vm_end = from;
1412 else
1413 vma->vm_start = to;
1414 add_vma_to_mm(mm, vma);
1415
1416 /* cut the backing region down to size */
1417 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001418 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001419
1420 down_write(&nommu_region_sem);
1421 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001422 if (from > region->vm_start) {
1423 to = region->vm_top;
1424 region->vm_top = region->vm_end = from;
1425 } else {
David Howells8feae132009-01-08 12:04:47 +00001426 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001427 }
David Howells8feae132009-01-08 12:04:47 +00001428 add_nommu_region(region);
1429 up_write(&nommu_region_sem);
1430
1431 free_page_series(from, to);
1432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
David Howells30340972006-09-27 01:50:20 -07001435/*
1436 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001437 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1438 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001439 */
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001440int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
David Howells8feae132009-01-08 12:04:47 +00001442 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001443 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001444 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Bob Liuf67d9b12011-05-24 17:12:56 -07001446 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001447 if (len == 0)
1448 return -EINVAL;
1449
Bob Liuf67d9b12011-05-24 17:12:56 -07001450 end = start + len;
1451
David Howells8feae132009-01-08 12:04:47 +00001452 /* find the first potentially overlapping VMA */
1453 vma = find_vma(mm, start);
1454 if (!vma) {
Choi Gi-yongac714902014-04-07 15:37:36 -07001455 static int limit;
David Howells33e5d7692009-04-02 16:56:32 -07001456 if (limit < 5) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001457 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1458 current->pid, current->comm,
1459 start, start + len - 1);
David Howells33e5d7692009-04-02 16:56:32 -07001460 limit++;
1461 }
David Howells8feae132009-01-08 12:04:47 +00001462 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
David Howells8feae132009-01-08 12:04:47 +00001465 /* we're allowed to split an anonymous VMA but not a file-backed one */
1466 if (vma->vm_file) {
1467 do {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001468 if (start > vma->vm_start)
David Howells8feae132009-01-08 12:04:47 +00001469 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001470 if (end == vma->vm_end)
1471 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001472 vma = vma->vm_next;
1473 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001474 return -EINVAL;
1475 } else {
1476 /* the chunk must be a subset of the VMA found */
1477 if (start == vma->vm_start && end == vma->vm_end)
1478 goto erase_whole_vma;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001479 if (start < vma->vm_start || end > vma->vm_end)
David Howells8feae132009-01-08 12:04:47 +00001480 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001481 if (offset_in_page(start))
David Howells8feae132009-01-08 12:04:47 +00001482 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001483 if (end != vma->vm_end && offset_in_page(end))
David Howells8feae132009-01-08 12:04:47 +00001484 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001485 if (start != vma->vm_start && end != vma->vm_end) {
1486 ret = split_vma(mm, vma, start, 1);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001487 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001488 return ret;
David Howells8feae132009-01-08 12:04:47 +00001489 }
1490 return shrink_vma(mm, vma, start, end);
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
David Howells8feae132009-01-08 12:04:47 +00001493erase_whole_vma:
1494 delete_vma_from_mm(vma);
1495 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return 0;
1497}
1498
Al Virobfce2812012-04-20 21:57:04 -04001499int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001500{
Al Virobfce2812012-04-20 21:57:04 -04001501 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001502 int ret;
David Howells30340972006-09-27 01:50:20 -07001503
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001504 mmap_write_lock(mm);
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001505 ret = do_munmap(mm, addr, len, NULL);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001506 mmap_write_unlock(mm);
David Howells30340972006-09-27 01:50:20 -07001507 return ret;
1508}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001509EXPORT_SYMBOL(vm_munmap);
1510
1511SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1512{
Al Virobfce2812012-04-20 21:57:04 -04001513 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001514}
David Howells30340972006-09-27 01:50:20 -07001515
1516/*
David Howells8feae132009-01-08 12:04:47 +00001517 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001518 */
David Howells8feae132009-01-08 12:04:47 +00001519void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
David Howells8feae132009-01-08 12:04:47 +00001521 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
David Howells8feae132009-01-08 12:04:47 +00001523 if (!mm)
1524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
David Howells8feae132009-01-08 12:04:47 +00001526 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
David Howells8feae132009-01-08 12:04:47 +00001528 while ((vma = mm->mmap)) {
1529 mm->mmap = vma->vm_next;
1530 delete_vma_from_mm(vma);
1531 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001532 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534}
1535
Linus Torvalds5d22fc22016-05-27 15:57:31 -07001536int vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 return -ENOMEM;
1539}
1540
1541/*
David Howells6fa5f802006-09-27 01:50:21 -07001542 * expand (or shrink) an existing mapping, potentially moving it at the same
1543 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *
David Howells6fa5f802006-09-27 01:50:21 -07001545 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001546 * as long as it stays within the region allocated by do_mmap_private() and the
1547 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 *
David Howells6fa5f802006-09-27 01:50:21 -07001549 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
Al Viro4b377ba2013-03-04 10:47:59 -05001551static unsigned long do_mremap(unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 unsigned long old_len, unsigned long new_len,
1553 unsigned long flags, unsigned long new_addr)
1554{
David Howells6fa5f802006-09-27 01:50:21 -07001555 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001558 old_len = PAGE_ALIGN(old_len);
1559 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001560 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return (unsigned long) -EINVAL;
1562
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001563 if (offset_in_page(addr))
David Howells8feae132009-01-08 12:04:47 +00001564 return -EINVAL;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (flags & MREMAP_FIXED && new_addr != addr)
1567 return (unsigned long) -EINVAL;
1568
David Howells8feae132009-01-08 12:04:47 +00001569 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001570 if (!vma)
1571 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
David Howells6fa5f802006-09-27 01:50:21 -07001573 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return (unsigned long) -EFAULT;
1575
David Howells6fa5f802006-09-27 01:50:21 -07001576 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return (unsigned long) -EPERM;
1578
David Howells8feae132009-01-08 12:04:47 +00001579 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return (unsigned long) -ENOMEM;
1581
1582 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001583 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001584 return vma->vm_start;
1585}
1586
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001587SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1588 unsigned long, new_len, unsigned long, flags,
1589 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001590{
1591 unsigned long ret;
1592
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001593 mmap_write_lock(current->mm);
David Howells6fa5f802006-09-27 01:50:21 -07001594 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001595 mmap_write_unlock(current->mm);
David Howells6fa5f802006-09-27 01:50:21 -07001596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Keith Buschdf06b372018-10-26 15:10:28 -07001599struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1600 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 return NULL;
1603}
1604
Bob Liu8f3b1322011-07-08 15:39:46 -07001605int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1606 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Bob Liu8f3b1322011-07-08 15:39:46 -07001608 if (addr != (pfn << PAGE_SHIFT))
1609 return -EINVAL;
1610
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001611 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
Luke Yang22c4af42006-07-14 00:24:09 -07001614EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001616int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1617{
1618 unsigned long pfn = start >> PAGE_SHIFT;
1619 unsigned long vm_len = vma->vm_end - vma->vm_start;
1620
1621 pfn += vma->vm_pgoff;
1622 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1623}
1624EXPORT_SYMBOL(vm_iomap_memory);
1625
Paul Mundtf905bc42008-02-04 22:29:59 -08001626int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1627 unsigned long pgoff)
1628{
1629 unsigned int size = vma->vm_end - vma->vm_start;
1630
1631 if (!(vma->vm_flags & VM_USERMAP))
1632 return -EINVAL;
1633
1634 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1635 vma->vm_end = vma->vm_start + size;
1636
1637 return 0;
1638}
1639EXPORT_SYMBOL(remap_vmalloc_range);
1640
Souptick Joarder2bcd6452018-06-07 17:08:00 -07001641vm_fault_t filemap_fault(struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001642{
1643 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001644 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001645}
Paul Mundtb5073172007-07-21 04:37:25 -07001646EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001647
Geert Uytterhoeven3f98a282021-01-28 11:06:26 +01001648vm_fault_t filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001649 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07001650{
1651 BUG();
Geert Uytterhoeven3f98a282021-01-28 11:06:26 +01001652 return 0;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07001653}
1654EXPORT_SYMBOL(filemap_map_pages);
1655
John Hubbardd3f5ffc2020-12-14 19:07:45 -08001656int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
1657 int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07001658{
David Howells0ec76a12006-09-27 01:50:15 -07001659 struct vm_area_struct *vma;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01001660 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07001661
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001662 if (mmap_read_lock_killable(mm))
Konstantin Khlebnikov1e426fe2019-07-11 21:00:07 -07001663 return 0;
David Howells0ec76a12006-09-27 01:50:15 -07001664
1665 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001666 vma = find_vma(mm, addr);
1667 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001668 /* don't overrun this mapping */
1669 if (addr + len >= vma->vm_end)
1670 len = vma->vm_end - addr;
1671
1672 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001673 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001674 copy_to_user_page(vma, NULL, addr,
1675 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001676 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001677 copy_from_user_page(vma, NULL, addr,
1678 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001679 else
1680 len = 0;
1681 } else {
1682 len = 0;
1683 }
1684
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001685 mmap_read_unlock(mm);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001686
1687 return len;
1688}
1689
1690/**
Mike Rapoportb7701a52018-02-06 15:42:13 -08001691 * access_remote_vm - access another process' address space
Mike Frysingerf55f1992011-03-29 14:05:12 +01001692 * @mm: the mm_struct of the target address space
1693 * @addr: start address to access
1694 * @buf: source or destination buffer
1695 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001696 * @gup_flags: flags modifying lookup behaviour
Mike Frysingerf55f1992011-03-29 14:05:12 +01001697 *
1698 * The caller must hold a reference on @mm.
1699 */
1700int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001701 void *buf, int len, unsigned int gup_flags)
Mike Frysingerf55f1992011-03-29 14:05:12 +01001702{
John Hubbardd3f5ffc2020-12-14 19:07:45 -08001703 return __access_remote_vm(mm, addr, buf, len, gup_flags);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001704}
1705
1706/*
1707 * Access another process' address space.
1708 * - source/target buffer must be kernel space
1709 */
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001710int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1711 unsigned int gup_flags)
Mike Frysingerf55f1992011-03-29 14:05:12 +01001712{
1713 struct mm_struct *mm;
1714
1715 if (addr + len < addr)
1716 return 0;
1717
1718 mm = get_task_mm(tsk);
1719 if (!mm)
1720 return 0;
1721
John Hubbardd3f5ffc2020-12-14 19:07:45 -08001722 len = __access_remote_vm(mm, addr, buf, len, gup_flags);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001723
David Howells0ec76a12006-09-27 01:50:15 -07001724 mmput(mm);
1725 return len;
1726}
Catalin Marinasfcd35852016-11-01 14:43:25 -07001727EXPORT_SYMBOL_GPL(access_process_vm);
David Howells7e660872010-01-15 17:01:39 -08001728
1729/**
1730 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1731 * @inode: The inode to check
1732 * @size: The current filesize of the inode
1733 * @newsize: The proposed filesize of the inode
1734 *
1735 * Check the shared mappings on an inode on behalf of a shrinking truncate to
Randy Dunlapc08b3422020-08-11 18:33:11 -07001736 * make sure that any outstanding VMAs aren't broken and then shrink the
1737 * vm_regions that extend beyond so that do_mmap() doesn't
David Howells7e660872010-01-15 17:01:39 -08001738 * automatically grant mappings that are too large.
1739 */
1740int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1741 size_t newsize)
1742{
1743 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08001744 struct vm_region *region;
1745 pgoff_t low, high;
1746 size_t r_size, r_top;
1747
1748 low = newsize >> PAGE_SHIFT;
1749 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1750
1751 down_write(&nommu_region_sem);
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001752 i_mmap_lock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001753
1754 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07001755 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08001756 /* found one - only interested if it's shared out of the page
1757 * cache */
1758 if (vma->vm_flags & VM_SHARED) {
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001759 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001760 up_write(&nommu_region_sem);
1761 return -ETXTBSY; /* not quite true, but near enough */
1762 }
1763 }
1764
1765 /* reduce any regions that overlap the dead zone - if in existence,
1766 * these will be pointed to by VMAs that don't overlap the dead zone
1767 *
1768 * we don't check for any regions that start beyond the EOF as there
1769 * shouldn't be any
1770 */
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001771 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08001772 if (!(vma->vm_flags & VM_SHARED))
1773 continue;
1774
1775 region = vma->vm_region;
1776 r_size = region->vm_top - region->vm_start;
1777 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1778
1779 if (r_top > newsize) {
1780 region->vm_top -= r_top - newsize;
1781 if (region->vm_end > region->vm_top)
1782 region->vm_end = region->vm_top;
1783 }
1784 }
1785
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001786 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001787 up_write(&nommu_region_sem);
1788 return 0;
1789}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001790
1791/*
1792 * Initialise sysctl_user_reserve_kbytes.
1793 *
1794 * This is intended to prevent a user from starting a single memory hogging
1795 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1796 * mode.
1797 *
1798 * The default value is min(3% of free memory, 128MB)
1799 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1800 */
1801static int __meminit init_user_reserve(void)
1802{
1803 unsigned long free_kbytes;
1804
Michal Hockoc41f0122017-09-06 16:23:36 -07001805 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001806
1807 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1808 return 0;
1809}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04001810subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001811
1812/*
1813 * Initialise sysctl_admin_reserve_kbytes.
1814 *
1815 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1816 * to log in and kill a memory hogging process.
1817 *
1818 * Systems with more than 256MB will reserve 8MB, enough to recover
1819 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1820 * only reserve 3% of free pages by default.
1821 */
1822static int __meminit init_admin_reserve(void)
1823{
1824 unsigned long free_kbytes;
1825
Michal Hockoc41f0122017-09-06 16:23:36 -07001826 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001827
1828 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
1829 return 0;
1830}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04001831subsys_initcall(init_admin_reserve);