blob: bd2b4e5ef14457eab36c1905ee558eea5e33f213 [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 *
8 * See Documentation/nommu-mmap.txt
9 *
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/blkdev.h>
31#include <linux/backing-dev.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070032#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/mount.h>
34#include <linux/personality.h>
35#include <linux/security.h>
36#include <linux/syscalls.h>
Al Viro120a7952010-10-30 02:54:44 -040037#include <linux/audit.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070038#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/tlb.h>
42#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070043#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000044#include "internal.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046void *high_memory;
Arnd Bergmann944b6872015-02-05 12:25:12 -080047EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct page *mem_map;
49unsigned long max_mapnr;
gchen gchen5b8bf302015-03-12 16:26:05 -070050EXPORT_SYMBOL(max_mapnr);
Hugh Dickins4266c972009-09-23 17:05:53 +010051unsigned long highest_memmap_pfn;
David Howellsfc4d5c22009-05-06 16:03:05 -070052int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053int heap_stack_gap = 0;
54
David Howells33e5d7692009-04-02 16:56:32 -070055atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057EXPORT_SYMBOL(mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Howells8feae132009-01-08 12:04:47 +000059/* list of mapped, potentially shareable regions */
60static struct kmem_cache *vm_region_jar;
61struct rb_root nommu_region_tree = RB_ROOT;
62DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +040064const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * Return the total memory allocated for this pointer, not
69 * just what the caller asked for.
70 *
71 * Doesn't have to be accurate, i.e. may have races.
72 */
73unsigned int kobjsize(const void *objp)
74{
75 struct page *page;
76
Michael Hennerich4016a132008-04-28 02:13:38 -070077 /*
78 * If the object we have should not have ksize performed on it,
79 * return size of 0
80 */
Paul Mundt5a1603b2008-06-12 16:29:55 +090081 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070082 return 0;
83
84 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070085
86 /*
87 * If the allocator sets PageSlab, we know the pointer came from
88 * kmalloc().
89 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (PageSlab(page))
91 return ksize(objp);
92
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070093 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +000094 * If it's not a compound page, see if we have a matching VMA
95 * region. This test is intentionally done in reverse order,
96 * so if there's no VMA, we still fall through and hand back
97 * PAGE_SIZE for 0-order pages.
98 */
99 if (!PageCompound(page)) {
100 struct vm_area_struct *vma;
101
102 vma = find_vma(current->mm, (unsigned long)objp);
103 if (vma)
104 return vma->vm_end - vma->vm_start;
105 }
106
107 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700108 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900109 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700110 */
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700111 return page_size(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Paul Mundtdfc2f912009-06-26 04:31:57 +0900114/**
115 * follow_pfn - look up PFN at a user virtual address
116 * @vma: memory mapping
117 * @address: user virtual address
118 * @pfn: location to store found PFN
119 *
120 * Only IO mappings and raw PFN mappings are allowed.
121 *
122 * Returns zero and the pfn at @pfn on success, -ve otherwise.
123 */
124int follow_pfn(struct vm_area_struct *vma, unsigned long address,
125 unsigned long *pfn)
126{
127 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
128 return -EINVAL;
129
130 *pfn = address >> PAGE_SHIFT;
131 return 0;
132}
133EXPORT_SYMBOL(follow_pfn);
134
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700135LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800137void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 kfree(addr);
140}
Paul Mundtb5073172007-07-21 04:37:25 -0700141EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Al Virodd0fc662005-10-07 07:46:04 +0100143void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200146 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
147 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
Nick Piggin84097512006-03-22 00:08:34 -0800149 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
Paul Mundtb5073172007-07-21 04:37:25 -0700151EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Michal Hockoa7c3e902017-05-08 15:57:09 -0700153void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
154{
155 return __vmalloc(size, flags, PAGE_KERNEL);
156}
157
Andrii Nakryikoed817452019-11-23 14:08:35 -0800158static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
Paul Mundtf905bc42008-02-04 22:29:59 -0800159{
160 void *ret;
161
Andrii Nakryikoed817452019-11-23 14:08:35 -0800162 ret = __vmalloc(size, flags, PAGE_KERNEL);
Paul Mundtf905bc42008-02-04 22:29:59 -0800163 if (ret) {
164 struct vm_area_struct *vma;
165
166 down_write(&current->mm->mmap_sem);
167 vma = find_vma(current->mm, (unsigned long)ret);
168 if (vma)
169 vma->vm_flags |= VM_USERMAP;
170 up_write(&current->mm->mmap_sem);
171 }
172
173 return ret;
174}
Andrii Nakryikoed817452019-11-23 14:08:35 -0800175
176void *vmalloc_user(unsigned long size)
177{
178 return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
179}
Paul Mundtf905bc42008-02-04 22:29:59 -0800180EXPORT_SYMBOL(vmalloc_user);
181
Andrii Nakryikoed817452019-11-23 14:08:35 -0800182void *vmalloc_user_node_flags(unsigned long size, int node, gfp_t flags)
183{
184 return __vmalloc_user_flags(size, flags | __GFP_ZERO);
185}
186EXPORT_SYMBOL(vmalloc_user_node_flags);
187
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800188struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 return virt_to_page(addr);
191}
Paul Mundtb5073172007-07-21 04:37:25 -0700192EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800194unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 return page_to_pfn(virt_to_page(addr));
197}
Paul Mundtb5073172007-07-21 04:37:25 -0700198EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200long vread(char *buf, char *addr, unsigned long count)
201{
Chen Gang9bde9162013-07-03 15:02:36 -0700202 /* Don't allow overflow */
203 if ((unsigned long) buf + count < count)
204 count = -(unsigned long) buf;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 memcpy(buf, addr, count);
207 return count;
208}
209
210long vwrite(char *buf, char *addr, unsigned long count)
211{
212 /* Don't allow overflow */
213 if ((unsigned long) addr + count < count)
214 count = -(unsigned long) addr;
215
216 memcpy(addr, buf, count);
Choi Gi-yongac714902014-04-07 15:37:36 -0700217 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900221 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
223 * @size: allocation size
224 *
225 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900226 * allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200228 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * use __vmalloc() instead.
230 */
231void *vmalloc(unsigned long size)
232{
233 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
234}
Andrew Mortonf6138882006-02-28 16:59:18 -0800235EXPORT_SYMBOL(vmalloc);
236
Dave Younge1ca7782010-10-26 14:22:06 -0700237/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900238 * vzalloc - allocate virtually contiguous memory with zero fill
Dave Younge1ca7782010-10-26 14:22:06 -0700239 *
240 * @size: allocation size
241 *
242 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900243 * allocator and map them into contiguous kernel virtual space.
Dave Younge1ca7782010-10-26 14:22:06 -0700244 * The memory allocated is set to zero.
245 *
246 * For tight control over page level allocator and protection flags
247 * use __vmalloc() instead.
248 */
249void *vzalloc(unsigned long size)
250{
251 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
252 PAGE_KERNEL);
253}
254EXPORT_SYMBOL(vzalloc);
255
256/**
257 * vmalloc_node - allocate memory on a specific node
258 * @size: allocation size
259 * @node: numa node
260 *
261 * Allocate enough pages to cover @size from the page level
262 * allocator and map them into contiguous kernel virtual space.
263 *
264 * For tight control over page level allocator and protection flags
265 * use __vmalloc() instead.
266 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800267void *vmalloc_node(unsigned long size, int node)
268{
269 return vmalloc(size);
270}
Paul Mundt9a14f652010-12-24 11:50:34 +0900271EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700272
273/**
274 * vzalloc_node - allocate memory on a specific node with zero fill
275 * @size: allocation size
276 * @node: numa node
277 *
278 * Allocate enough pages to cover @size from the page level
279 * allocator and map them into contiguous kernel virtual space.
280 * The memory allocated is set to zero.
281 *
282 * For tight control over page level allocator and protection flags
283 * use __vmalloc() instead.
284 */
285void *vzalloc_node(unsigned long size, int node)
286{
287 return vzalloc(size);
288}
289EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Paul Mundt1af446e2008-08-04 16:01:47 +0900291/**
292 * vmalloc_exec - allocate virtually contiguous, executable memory
293 * @size: allocation size
294 *
295 * Kernel-internal function to allocate enough pages to cover @size
296 * the page level allocator and map them into contiguous and
297 * executable kernel virtual space.
298 *
299 * For tight control over page level allocator and protection flags
300 * use __vmalloc() instead.
301 */
302
303void *vmalloc_exec(unsigned long size)
304{
305 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
306}
307
Paul Mundtb5073172007-07-21 04:37:25 -0700308/**
309 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * @size: allocation size
311 *
312 * Allocate enough 32bit PA addressable pages to cover @size from the
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900313 * page level allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315void *vmalloc_32(unsigned long size)
316{
317 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
318}
Paul Mundtb5073172007-07-21 04:37:25 -0700319EXPORT_SYMBOL(vmalloc_32);
320
321/**
322 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
323 * @size: allocation size
324 *
325 * The resulting memory area is 32bit addressable and zeroed so it can be
326 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800327 *
328 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
329 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700330 */
331void *vmalloc_32_user(unsigned long size)
332{
Paul Mundtf905bc42008-02-04 22:29:59 -0800333 /*
334 * We'll have to sort out the ZONE_DMA bits for 64-bit,
335 * but for now this can simply use vmalloc_user() directly.
336 */
337 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700338}
339EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
342{
343 BUG();
344 return NULL;
345}
Paul Mundtb5073172007-07-21 04:37:25 -0700346EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800348void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 BUG();
351}
Paul Mundtb5073172007-07-21 04:37:25 -0700352EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Paul Mundteb6434d2009-01-21 17:45:47 +0900354void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
355{
356 BUG();
357 return NULL;
358}
359EXPORT_SYMBOL(vm_map_ram);
360
361void vm_unmap_ram(const void *mem, unsigned int count)
362{
363 BUG();
364}
365EXPORT_SYMBOL(vm_unmap_ram);
366
367void vm_unmap_aliases(void)
368{
369}
370EXPORT_SYMBOL_GPL(vm_unmap_aliases);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700373 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
374 * have one.
375 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700376void __weak vmalloc_sync_all(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700377{
378}
379
David Vrabelcd129092011-09-29 16:53:32 +0100380struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900381{
382 BUG();
383 return NULL;
384}
385EXPORT_SYMBOL_GPL(alloc_vm_area);
386
387void free_vm_area(struct vm_struct *area)
388{
389 BUG();
390}
391EXPORT_SYMBOL_GPL(free_vm_area);
392
Paul Mundtb5073172007-07-21 04:37:25 -0700393int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
394 struct page *page)
395{
396 return -EINVAL;
397}
398EXPORT_SYMBOL(vm_insert_page);
399
Souptick Joardera667d742019-05-13 17:21:56 -0700400int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
401 unsigned long num)
402{
403 return -EINVAL;
404}
405EXPORT_SYMBOL(vm_map_pages);
406
407int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
408 unsigned long num)
409{
410 return -EINVAL;
411}
412EXPORT_SYMBOL(vm_map_pages_zero);
413
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700414/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * sys_brk() for the most part doesn't need the global kernel
416 * lock, except when an application is doing something nasty
417 * like trying to un-brk an area that has already been mapped
418 * to a regular file. in this case, the unmapping will need
419 * to invoke file system routines that need the global lock.
420 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100421SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct mm_struct *mm = current->mm;
424
425 if (brk < mm->start_brk || brk > mm->context.end_brk)
426 return mm->brk;
427
428 if (mm->brk == brk)
429 return mm->brk;
430
431 /*
432 * Always allow shrinking brk
433 */
434 if (brk <= mm->brk) {
435 mm->brk = brk;
436 return brk;
437 }
438
439 /*
440 * Ok, looks good - let it rip.
441 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000442 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return mm->brk = brk;
444}
445
David Howells8feae132009-01-08 12:04:47 +0000446/*
seokhoon.yoon3edf41d2017-02-24 14:56:44 -0800447 * initialise the percpu counter for VM and region record slabs
David Howells8feae132009-01-08 12:04:47 +0000448 */
449void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700451 int ret;
452
Tejun Heo908c7f12014-09-08 09:51:29 +0900453 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700454 VM_BUG_ON(ret);
Vladimir Davydov5d097052016-01-14 15:18:21 -0800455 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
David Howells8feae132009-01-08 12:04:47 +0000456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
David Howells8feae132009-01-08 12:04:47 +0000458/*
459 * validate the region tree
460 * - the caller must hold the region lock
461 */
462#ifdef CONFIG_DEBUG_NOMMU_REGIONS
463static noinline void validate_nommu_regions(void)
464{
465 struct vm_region *region, *last;
466 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David Howells8feae132009-01-08 12:04:47 +0000468 lastp = rb_first(&nommu_region_tree);
469 if (!lastp)
470 return;
471
472 last = rb_entry(lastp, struct vm_region, vm_rb);
Geliang Tangc9427bc2015-11-05 18:48:38 -0800473 BUG_ON(last->vm_end <= last->vm_start);
474 BUG_ON(last->vm_top < last->vm_end);
David Howells8feae132009-01-08 12:04:47 +0000475
476 while ((p = rb_next(lastp))) {
477 region = rb_entry(p, struct vm_region, vm_rb);
478 last = rb_entry(lastp, struct vm_region, vm_rb);
479
Geliang Tangc9427bc2015-11-05 18:48:38 -0800480 BUG_ON(region->vm_end <= region->vm_start);
481 BUG_ON(region->vm_top < region->vm_end);
482 BUG_ON(region->vm_start < last->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000483
484 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486}
David Howells8feae132009-01-08 12:04:47 +0000487#else
David Howells33e5d7692009-04-02 16:56:32 -0700488static void validate_nommu_regions(void)
489{
490}
David Howells8feae132009-01-08 12:04:47 +0000491#endif
492
493/*
494 * add a region into the global tree
495 */
496static void add_nommu_region(struct vm_region *region)
497{
498 struct vm_region *pregion;
499 struct rb_node **p, *parent;
500
501 validate_nommu_regions();
502
David Howells8feae132009-01-08 12:04:47 +0000503 parent = NULL;
504 p = &nommu_region_tree.rb_node;
505 while (*p) {
506 parent = *p;
507 pregion = rb_entry(parent, struct vm_region, vm_rb);
508 if (region->vm_start < pregion->vm_start)
509 p = &(*p)->rb_left;
510 else if (region->vm_start > pregion->vm_start)
511 p = &(*p)->rb_right;
512 else if (pregion == region)
513 return;
514 else
515 BUG();
516 }
517
518 rb_link_node(&region->vm_rb, parent, p);
519 rb_insert_color(&region->vm_rb, &nommu_region_tree);
520
521 validate_nommu_regions();
522}
523
524/*
525 * delete a region from the global tree
526 */
527static void delete_nommu_region(struct vm_region *region)
528{
529 BUG_ON(!nommu_region_tree.rb_node);
530
531 validate_nommu_regions();
532 rb_erase(&region->vm_rb, &nommu_region_tree);
533 validate_nommu_regions();
534}
535
536/*
537 * free a contiguous series of pages
538 */
539static void free_page_series(unsigned long from, unsigned long to)
540{
541 for (; from < to; from += PAGE_SIZE) {
542 struct page *page = virt_to_page(from);
543
David Howells33e5d7692009-04-02 16:56:32 -0700544 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000545 put_page(page);
546 }
547}
548
549/*
550 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700551 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000552 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000553 * will equal vm_start
554 */
555static void __put_nommu_region(struct vm_region *region)
556 __releases(nommu_region_sem)
557{
David Howells8feae132009-01-08 12:04:47 +0000558 BUG_ON(!nommu_region_tree.rb_node);
559
David Howells1e2ae592010-01-15 17:01:33 -0800560 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000561 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000562 delete_nommu_region(region);
563 up_write(&nommu_region_sem);
564
565 if (region->vm_file)
566 fput(region->vm_file);
567
568 /* IO memory and memory shared directly out of the pagecache
569 * from ramfs/tmpfs mustn't be released here */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700570 if (region->vm_flags & VM_MAPPED_COPY)
Paul Mundtdd8632a2009-01-08 12:04:47 +0000571 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000572 kmem_cache_free(vm_region_jar, region);
573 } else {
574 up_write(&nommu_region_sem);
575 }
576}
577
578/*
579 * release a reference to a region
580 */
581static void put_nommu_region(struct vm_region *region)
582{
583 down_write(&nommu_region_sem);
584 __put_nommu_region(region);
585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Howells30340972006-09-27 01:50:20 -0700587/*
588 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000589 * and tree and add to the address space's page tree also if not an anonymous
590 * page
David Howells30340972006-09-27 01:50:20 -0700591 * - should be called with mm->mmap_sem held writelocked
592 */
David Howells8feae132009-01-08 12:04:47 +0000593static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700594{
Namhyung Kim6038def2011-05-24 17:11:22 -0700595 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000596 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700597 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700598
David Howells8feae132009-01-08 12:04:47 +0000599 BUG_ON(!vma->vm_region);
600
601 mm->map_count++;
602 vma->vm_mm = mm;
603
604 /* add the VMA to the mapping */
605 if (vma->vm_file) {
606 mapping = vma->vm_file->f_mapping;
607
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800608 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000609 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700610 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000611 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800612 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000613 }
614
615 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700616 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000617 p = &mm->mm_rb.rb_node;
618 while (*p) {
619 parent = *p;
620 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
621
622 /* sort by: start addr, end addr, VMA struct addr in that order
623 * (the latter is necessary as we may get identical VMAs) */
624 if (vma->vm_start < pvma->vm_start)
625 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700626 else if (vma->vm_start > pvma->vm_start) {
627 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000628 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700629 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000630 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700631 else if (vma->vm_end > pvma->vm_end) {
632 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000633 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700634 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000635 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700636 else if (vma > pvma) {
637 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000638 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700639 } else
David Howells8feae132009-01-08 12:04:47 +0000640 BUG();
641 }
642
643 rb_link_node(&vma->vm_rb, parent, p);
644 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
645
646 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700647 prev = NULL;
648 if (rb_prev)
649 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700650
Wei Yangaba6dfb2019-11-30 17:50:53 -0800651 __vma_link_list(mm, vma, prev);
David Howells8feae132009-01-08 12:04:47 +0000652}
653
654/*
655 * delete a VMA from its owning mm_struct and address space
656 */
657static void delete_vma_from_mm(struct vm_area_struct *vma)
658{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700659 int i;
David Howells8feae132009-01-08 12:04:47 +0000660 struct address_space *mapping;
661 struct mm_struct *mm = vma->vm_mm;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700662 struct task_struct *curr = current;
David Howells8feae132009-01-08 12:04:47 +0000663
David Howells8feae132009-01-08 12:04:47 +0000664 mm->map_count--;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700665 for (i = 0; i < VMACACHE_SIZE; i++) {
666 /* if the vma is cached, invalidate the entire cache */
Ingo Molnar314ff782017-02-03 11:03:31 +0100667 if (curr->vmacache.vmas[i] == vma) {
Steven Miaoe020d5b2014-06-23 13:22:02 -0700668 vmacache_invalidate(mm);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700669 break;
670 }
671 }
David Howells8feae132009-01-08 12:04:47 +0000672
673 /* remove the VMA from the mapping */
674 if (vma->vm_file) {
675 mapping = vma->vm_file->f_mapping;
676
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800677 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000678 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700679 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000680 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800681 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000682 }
683
684 /* remove from the MM's tree and list */
685 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700686
Wei Yang1b9fc5b22019-11-30 17:50:49 -0800687 __vma_unlink_list(mm, vma);
David Howells8feae132009-01-08 12:04:47 +0000688}
689
690/*
691 * destroy a VMA record
692 */
693static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
694{
David Howells8feae132009-01-08 12:04:47 +0000695 if (vma->vm_ops && vma->vm_ops->close)
696 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700697 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000698 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000699 put_nommu_region(vma->vm_region);
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700700 vm_area_free(vma);
David Howells30340972006-09-27 01:50:20 -0700701}
702
703/*
704 * look up the first VMA in which addr resides, NULL if none
705 * - should be called with mm->mmap_sem at least held readlocked
706 */
707struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
708{
David Howells8feae132009-01-08 12:04:47 +0000709 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700710
David Howells8feae132009-01-08 12:04:47 +0000711 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700712 vma = vmacache_find(mm, addr);
713 if (likely(vma))
David Howells8feae132009-01-08 12:04:47 +0000714 return vma;
715
Namhyung Kime922c4c2011-05-24 17:11:24 -0700716 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000717 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700718 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000719 if (vma->vm_start > addr)
720 return NULL;
721 if (vma->vm_end > addr) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700722 vmacache_update(addr, vma);
David Howells8feae132009-01-08 12:04:47 +0000723 return vma;
724 }
David Howells30340972006-09-27 01:50:20 -0700725 }
726
David Howells30340972006-09-27 01:50:20 -0700727 return NULL;
728}
729EXPORT_SYMBOL(find_vma);
730
731/*
David Howells930e6522006-09-27 01:50:22 -0700732 * find a VMA
733 * - we don't extend stack VMAs under NOMMU conditions
734 */
735struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
736{
David Howells7561e8c2010-03-25 16:48:38 +0000737 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700738}
739
David Howells8feae132009-01-08 12:04:47 +0000740/*
741 * expand a stack to a given address
742 * - not supported under NOMMU conditions
743 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700744int expand_stack(struct vm_area_struct *vma, unsigned long address)
745{
746 return -ENOMEM;
747}
748
David Howells930e6522006-09-27 01:50:22 -0700749/*
David Howells6fa5f802006-09-27 01:50:21 -0700750 * look up the first VMA exactly that exactly matches addr
751 * - should be called with mm->mmap_sem at least held readlocked
752 */
David Howells8feae132009-01-08 12:04:47 +0000753static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
754 unsigned long addr,
755 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000758 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David Howells8feae132009-01-08 12:04:47 +0000760 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700761 vma = vmacache_find_exact(mm, addr, end);
762 if (vma)
David Howells8feae132009-01-08 12:04:47 +0000763 return vma;
764
Namhyung Kime922c4c2011-05-24 17:11:24 -0700765 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000766 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700767 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000768 if (vma->vm_start < addr)
769 continue;
770 if (vma->vm_start > addr)
771 return NULL;
772 if (vma->vm_end == end) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700773 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return vma;
David Howells8feae132009-01-08 12:04:47 +0000775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 return NULL;
779}
780
David Howells30340972006-09-27 01:50:20 -0700781/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * determine whether a mapping should be permitted and, if so, what sort of
783 * mapping we're capable of supporting
784 */
785static int validate_mmap_request(struct file *file,
786 unsigned long addr,
787 unsigned long len,
788 unsigned long prot,
789 unsigned long flags,
790 unsigned long pgoff,
791 unsigned long *_capabilities)
792{
David Howells8feae132009-01-08 12:04:47 +0000793 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int ret;
795
796 /* do the simple checks first */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700797 if (flags & MAP_FIXED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
801 (flags & MAP_TYPE) != MAP_SHARED)
802 return -EINVAL;
803
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000804 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -EINVAL;
806
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000807 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000808 rlen = PAGE_ALIGN(len);
809 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000810 return -ENOMEM;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000813 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000814 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* files must support mmap */
Al Viro72c2d532013-09-22 16:27:52 -0400818 if (!file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -ENODEV;
820
821 /* work out if what we've got could possibly be shared
822 * - we support chardevs that provide their own "memory"
823 * - we support files/blockdevs that are memory backed
824 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100825 if (file->f_op->mmap_capabilities) {
826 capabilities = file->f_op->mmap_capabilities(file);
827 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* no explicit capabilities set, so assume some
829 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500830 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 case S_IFREG:
832 case S_IFBLK:
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100833 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835
836 case S_IFCHR:
837 capabilities =
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100838 NOMMU_MAP_DIRECT |
839 NOMMU_MAP_READ |
840 NOMMU_MAP_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
842
843 default:
844 return -EINVAL;
845 }
846 }
847
848 /* eliminate any capabilities that we can't support on this
849 * device */
850 if (!file->f_op->get_unmapped_area)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100851 capabilities &= ~NOMMU_MAP_DIRECT;
Al Viro6e242a12015-03-31 12:35:13 -0400852 if (!(file->f_mode & FMODE_CAN_READ))
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100853 capabilities &= ~NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Graff Yang28d7a6a2009-08-18 14:11:17 -0700855 /* The file shall have been opened with read permission. */
856 if (!(file->f_mode & FMODE_READ))
857 return -EACCES;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (flags & MAP_SHARED) {
860 /* do checks for writing, appending and locking */
861 if ((prot & PROT_WRITE) &&
862 !(file->f_mode & FMODE_WRITE))
863 return -EACCES;
864
Al Viro496ad9a2013-01-23 17:07:38 -0500865 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 (file->f_mode & FMODE_WRITE))
867 return -EACCES;
868
Jeff Laytond7a06982014-03-10 09:54:15 -0400869 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -EAGAIN;
871
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100872 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -ENODEV;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* we mustn't privatise shared mappings */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100876 capabilities &= ~NOMMU_MAP_COPY;
Choi Gi-yongac714902014-04-07 15:37:36 -0700877 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* we're going to read the file into private memory we
879 * allocate */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100880 if (!(capabilities & NOMMU_MAP_COPY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -ENODEV;
882
883 /* we don't permit a private writable mapping to be
884 * shared with the backing device */
885 if (prot & PROT_WRITE)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100886 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100889 if (capabilities & NOMMU_MAP_DIRECT) {
890 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
891 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
892 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700893 ) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100894 capabilities &= ~NOMMU_MAP_DIRECT;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700895 if (flags & MAP_SHARED) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700896 pr_warn("MAP_SHARED not completely supported on !MMU\n");
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700897 return -EINVAL;
898 }
899 }
900 }
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* handle executable mappings and implied executable
903 * mappings */
Eric W. Biederman90f85722015-06-29 14:42:03 -0500904 if (path_noexec(&file->f_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (prot & PROT_EXEC)
906 return -EPERM;
Choi Gi-yongac714902014-04-07 15:37:36 -0700907 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* handle implication of PROT_EXEC by PROT_READ */
909 if (current->personality & READ_IMPLIES_EXEC) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100910 if (capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 prot |= PROT_EXEC;
912 }
Choi Gi-yongac714902014-04-07 15:37:36 -0700913 } else if ((prot & PROT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 (prot & PROT_EXEC) &&
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100915 !(capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ) {
917 /* backing file is not executable, try to copy */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100918 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Choi Gi-yongac714902014-04-07 15:37:36 -0700920 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* anonymous mappings are always memory backed and can be
922 * privately mapped
923 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100924 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* handle PROT_EXEC implication by PROT_READ */
927 if ((prot & PROT_READ) &&
928 (current->personality & READ_IMPLIES_EXEC))
929 prot |= PROT_EXEC;
930 }
931
932 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -0400933 ret = security_mmap_addr(addr);
934 if (ret < 0)
935 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /* looks okay */
938 *_capabilities = capabilities;
939 return 0;
940}
941
942/*
943 * we've determined that we can make the mapping, now translate what we
944 * now know into VMA flags
945 */
946static unsigned long determine_vm_flags(struct file *file,
947 unsigned long prot,
948 unsigned long flags,
949 unsigned long capabilities)
950{
951 unsigned long vm_flags;
952
Dave Hansene6bfb702016-02-12 13:02:31 -0800953 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* vm_flags |= mm->def_flags; */
955
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100956 if (!(capabilities & NOMMU_MAP_DIRECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700958 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (file && !(prot & PROT_WRITE))
960 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700961 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* overlay a shareable mapping on the backing device or inode
963 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
964 * romfs/cramfs */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100965 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700967 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
970 /* refuse to let anyone share private mappings with this process if
971 * it's being traced - otherwise breakpoints set in it may interfere
972 * with another untraced process
973 */
Tejun Heoa288eec2011-06-17 16:50:37 +0200974 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 vm_flags &= ~VM_MAYSHARE;
976
977 return vm_flags;
978}
979
980/*
David Howells8feae132009-01-08 12:04:47 +0000981 * set up a shared mapping on a file (the driver or filesystem provides and
982 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
David Howells8feae132009-01-08 12:04:47 +0000984static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int ret;
987
Miklos Szeredif74ac012017-02-20 16:51:23 +0100988 ret = call_mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +0000989 if (ret == 0) {
990 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +0100991 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +0000992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (ret != -ENOSYS)
994 return ret;
995
David Howells3fa30462010-03-23 13:35:21 -0700996 /* getting -ENOSYS indicates that direct mmap isn't possible (as
997 * opposed to tried but failed) so we can only give a suitable error as
998 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -ENODEV;
1000}
1001
1002/*
1003 * set up a private mapping or an anonymous shared mapping
1004 */
David Howells8feae132009-01-08 12:04:47 +00001005static int do_mmap_private(struct vm_area_struct *vma,
1006 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001007 unsigned long len,
1008 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001010 unsigned long total, point;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 void *base;
David Howells8feae132009-01-08 12:04:47 +00001012 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /* invoke the file's mapping function so that it can keep track of
1015 * shared mappings on devices or memory
1016 * - VM_MAYSHARE will be set if it may attempt to share
1017 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001018 if (capabilities & NOMMU_MAP_DIRECT) {
Miklos Szeredif74ac012017-02-20 16:51:23 +01001019 ret = call_mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001020 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001022 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1023 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001026 if (ret != -ENOSYS)
1027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /* getting an ENOSYS error indicates that direct mmap isn't
1030 * possible (as opposed to tried but failed) so we'll try to
1031 * make a private copy of the data and map that instead */
1032 }
1033
David Howells8feae132009-01-08 12:04:47 +00001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* allocate some memory to hold the mapping
1036 * - note that this may not return a page-aligned address if the object
1037 * we're allocating is smaller than a page
1038 */
Bob Liuf67d9b12011-05-24 17:12:56 -07001039 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +00001040 total = 1 << order;
Bob Liuf67d9b12011-05-24 17:12:56 -07001041 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001042
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001043 /* we don't want to allocate a power-of-2 sized page set */
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001044 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001045 total = point;
David Howells8feae132009-01-08 12:04:47 +00001046
Joonsoo Kimda616532015-02-27 15:51:43 -08001047 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001048 if (!base)
1049 goto enomem;
David Howells8feae132009-01-08 12:04:47 +00001050
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001051 atomic_long_add(total, &mmap_pages_allocated);
1052
David Howells8feae132009-01-08 12:04:47 +00001053 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1054 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001055 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001056 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001057
1058 vma->vm_start = region->vm_start;
1059 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (vma->vm_file) {
1062 /* read the contents of a file into the copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 loff_t fpos;
1064
1065 fpos = vma->vm_pgoff;
1066 fpos <<= PAGE_SHIFT;
1067
Christoph Hellwigb4bf8022017-09-01 17:39:17 +02001068 ret = kernel_read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (ret < 0)
1070 goto error_free;
1071
1072 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001073 if (ret < len)
1074 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -07001076 } else {
1077 vma_set_anonymous(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
1080 return 0;
1081
1082error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001083 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001084 region->vm_start = vma->vm_start = 0;
1085 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001086 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return ret;
1088
1089enomem:
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07001090 pr_err("Allocation of length %lu from process %d (%s) failed\n",
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001091 len, current->pid, current->comm);
Michal Hocko9af744d2017-02-22 15:46:16 -08001092 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -ENOMEM;
1094}
1095
1096/*
1097 * handle mapping creation for uClinux
1098 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001099unsigned long do_mmap(struct file *file,
1100 unsigned long addr,
1101 unsigned long len,
1102 unsigned long prot,
1103 unsigned long flags,
1104 vm_flags_t vm_flags,
1105 unsigned long pgoff,
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001106 unsigned long *populate,
1107 struct list_head *uf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
David Howells8feae132009-01-08 12:04:47 +00001109 struct vm_area_struct *vma;
1110 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct rb_node *rb;
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001112 unsigned long capabilities, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int ret;
1114
Michel Lespinasse41badc12013-02-22 16:32:47 -08001115 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* decide whether we should attempt the mapping, and if so what sort of
1118 * mapping */
1119 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1120 &capabilities);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001121 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return ret;
1123
David Howells06aab5a2009-09-24 12:33:48 +01001124 /* we ignore the address hint */
1125 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001126 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* we've determined that we can make the mapping, now translate what we
1129 * now know into VMA flags */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001130 vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
David Howells8feae132009-01-08 12:04:47 +00001132 /* we're going to need to record the mapping */
1133 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1134 if (!region)
1135 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvalds490fc052018-07-21 15:24:03 -07001137 vma = vm_area_alloc(current->mm);
David Howells8feae132009-01-08 12:04:47 +00001138 if (!vma)
1139 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
David Howells1e2ae592010-01-15 17:01:33 -08001141 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001142 region->vm_flags = vm_flags;
1143 region->vm_pgoff = pgoff;
1144
David Howells8feae132009-01-08 12:04:47 +00001145 vma->vm_flags = vm_flags;
1146 vma->vm_pgoff = pgoff;
1147
1148 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001149 region->vm_file = get_file(file);
1150 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001151 }
1152
1153 down_write(&nommu_region_sem);
1154
1155 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001157 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 * - shared mappings on character devices and memory backed files are
1159 * permitted to overlap inexactly as far as we are concerned for in
1160 * these cases, sharing is handled in the driver or filesystem rather
1161 * than here
1162 */
1163 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001164 struct vm_region *pregion;
1165 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
David Howells8feae132009-01-08 12:04:47 +00001167 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1168 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001169
David Howells8feae132009-01-08 12:04:47 +00001170 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1171 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
David Howells8feae132009-01-08 12:04:47 +00001173 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 continue;
1175
1176 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001177 if (file_inode(pregion->vm_file) !=
1178 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 continue;
1180
David Howells8feae132009-01-08 12:04:47 +00001181 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 continue;
1183
David Howells8feae132009-01-08 12:04:47 +00001184 rpglen = pregion->vm_end - pregion->vm_start;
1185 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1186 rpgend = pregion->vm_pgoff + rpglen;
1187 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 continue;
1189
David Howells8feae132009-01-08 12:04:47 +00001190 /* handle inexactly overlapping matches between
1191 * mappings */
1192 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1193 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1194 /* new mapping is not a subset of the region */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001195 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto sharing_violation;
1197 continue;
1198 }
1199
David Howells8feae132009-01-08 12:04:47 +00001200 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001201 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001202 vma->vm_region = pregion;
1203 start = pregion->vm_start;
1204 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1205 vma->vm_start = start;
1206 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001208 if (pregion->vm_flags & VM_MAPPED_COPY)
David Howells8feae132009-01-08 12:04:47 +00001209 vma->vm_flags |= VM_MAPPED_COPY;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001210 else {
David Howells8feae132009-01-08 12:04:47 +00001211 ret = do_mmap_shared_file(vma);
1212 if (ret < 0) {
1213 vma->vm_region = NULL;
1214 vma->vm_start = 0;
1215 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001216 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001217 pregion = NULL;
1218 goto error_just_free;
1219 }
1220 }
1221 fput(region->vm_file);
1222 kmem_cache_free(vm_region_jar, region);
1223 region = pregion;
1224 result = start;
1225 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* obtain the address at which to make a shared mapping
1229 * - this is the hook for quasi-memory character devices to
1230 * tell us the location of a shared mapping
1231 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001232 if (capabilities & NOMMU_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 addr = file->f_op->get_unmapped_area(file, addr, len,
1234 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001235 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001237 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001238 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* the driver refused to tell us where to site
1241 * the mapping so we'll have to attempt to copy
1242 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001243 ret = -ENODEV;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001244 if (!(capabilities & NOMMU_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001245 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001247 capabilities &= ~NOMMU_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001248 } else {
1249 vma->vm_start = region->vm_start = addr;
1250 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 }
1253 }
1254
David Howells8feae132009-01-08 12:04:47 +00001255 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
David Howells645d83c2009-09-24 15:13:10 +01001257 /* set up the mapping
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001258 * - the region is filled in if NOMMU_MAP_DIRECT is still set
David Howells645d83c2009-09-24 15:13:10 +01001259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001261 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
David Howells645d83c2009-09-24 15:13:10 +01001263 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001265 goto error_just_free;
1266 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001267
Jie Zhangea637632009-12-14 18:00:02 -08001268 /* clear anonymous mappings that don't ask for uninitialized data */
Christoph Hellwig0bf5f942019-07-16 16:26:27 -07001269 if (!vma->vm_file &&
1270 (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
1271 !(flags & MAP_UNINITIALIZED)))
Jie Zhangea637632009-12-14 18:00:02 -08001272 memset((void *)region->vm_start, 0,
1273 region->vm_end - region->vm_start);
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001276 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 current->mm->total_vm += len >> PAGE_SHIFT;
1279
David Howells8feae132009-01-08 12:04:47 +00001280share:
1281 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Mike Frysingercfe79c02010-01-06 17:23:23 +00001283 /* we flush the region from the icache only when the first executable
1284 * mapping of it is made */
1285 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1286 flush_icache_range(region->vm_start, region->vm_end);
1287 region->vm_icache_flushed = true;
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Mike Frysingercfe79c02010-01-06 17:23:23 +00001290 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
David Howells8feae132009-01-08 12:04:47 +00001292 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
David Howells8feae132009-01-08 12:04:47 +00001294error_just_free:
1295 up_write(&nommu_region_sem);
1296error:
David Howells89a86402009-10-30 13:13:26 +00001297 if (region->vm_file)
1298 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001299 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001300 if (vma->vm_file)
1301 fput(vma->vm_file);
Linus Torvalds3928d4f2018-07-21 13:48:51 -07001302 vm_area_free(vma);
David Howells8feae132009-01-08 12:04:47 +00001303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
David Howells8feae132009-01-08 12:04:47 +00001305sharing_violation:
1306 up_write(&nommu_region_sem);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001307 pr_warn("Attempt to share mismatched mappings\n");
David Howells8feae132009-01-08 12:04:47 +00001308 ret = -EINVAL;
1309 goto error;
1310
1311error_getting_vma:
1312 kmem_cache_free(vm_region_jar, region);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001313 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1314 len, current->pid);
Michal Hocko9af744d2017-02-22 15:46:16 -08001315 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -ENOMEM;
1317
David Howells8feae132009-01-08 12:04:47 +00001318error_getting_region:
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001319 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1320 len, current->pid);
Michal Hocko9af744d2017-02-22 15:46:16 -08001321 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -ENOMEM;
1323}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001324
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001325unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1326 unsigned long prot, unsigned long flags,
1327 unsigned long fd, unsigned long pgoff)
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001328{
1329 struct file *file = NULL;
1330 unsigned long retval = -EBADF;
1331
Al Viro120a7952010-10-30 02:54:44 -04001332 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001333 if (!(flags & MAP_ANONYMOUS)) {
1334 file = fget(fd);
1335 if (!file)
1336 goto out;
1337 }
1338
1339 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1340
Greg Ungererad1ed292012-06-04 14:29:59 +10001341 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001342
1343 if (file)
1344 fput(file);
1345out:
1346 return retval;
1347}
1348
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001349SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1350 unsigned long, prot, unsigned long, flags,
1351 unsigned long, fd, unsigned long, pgoff)
1352{
1353 return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1354}
1355
Christoph Hellwiga4679372010-03-10 15:21:15 -08001356#ifdef __ARCH_WANT_SYS_OLD_MMAP
1357struct mmap_arg_struct {
1358 unsigned long addr;
1359 unsigned long len;
1360 unsigned long prot;
1361 unsigned long flags;
1362 unsigned long fd;
1363 unsigned long offset;
1364};
1365
1366SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1367{
1368 struct mmap_arg_struct a;
1369
1370 if (copy_from_user(&a, arg, sizeof(a)))
1371 return -EFAULT;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001372 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001373 return -EINVAL;
1374
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001375 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1376 a.offset >> PAGE_SHIFT);
Christoph Hellwiga4679372010-03-10 15:21:15 -08001377}
1378#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/*
David Howells8feae132009-01-08 12:04:47 +00001381 * split a vma into two pieces at address 'addr', a new vma is allocated either
1382 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
David Howells8feae132009-01-08 12:04:47 +00001384int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1385 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
David Howells8feae132009-01-08 12:04:47 +00001387 struct vm_area_struct *new;
1388 struct vm_region *region;
1389 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David Howells779c1022010-01-15 17:01:34 -08001391 /* we're only permitted to split anonymous regions (these should have
1392 * only a single usage on the region) */
1393 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001394 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
David Howells8feae132009-01-08 12:04:47 +00001396 if (mm->map_count >= sysctl_max_map_count)
1397 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
David Howells8feae132009-01-08 12:04:47 +00001399 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1400 if (!region)
1401 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds3928d4f2018-07-21 13:48:51 -07001403 new = vm_area_dup(vma);
David Howells8feae132009-01-08 12:04:47 +00001404 if (!new) {
1405 kmem_cache_free(vm_region_jar, region);
1406 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
David Howells8feae132009-01-08 12:04:47 +00001408
1409 /* most fields are the same, copy all, and then fixup */
David Howells8feae132009-01-08 12:04:47 +00001410 *region = *vma->vm_region;
1411 new->vm_region = region;
1412
1413 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1414
1415 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001416 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001417 } else {
1418 region->vm_start = new->vm_start = addr;
1419 region->vm_pgoff = new->vm_pgoff += npages;
1420 }
1421
1422 if (new->vm_ops && new->vm_ops->open)
1423 new->vm_ops->open(new);
1424
1425 delete_vma_from_mm(vma);
1426 down_write(&nommu_region_sem);
1427 delete_nommu_region(vma->vm_region);
1428 if (new_below) {
1429 vma->vm_region->vm_start = vma->vm_start = addr;
1430 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1431 } else {
1432 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001433 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001434 }
1435 add_nommu_region(vma->vm_region);
1436 add_nommu_region(new->vm_region);
1437 up_write(&nommu_region_sem);
1438 add_vma_to_mm(mm, vma);
1439 add_vma_to_mm(mm, new);
1440 return 0;
1441}
1442
1443/*
1444 * shrink a VMA by removing the specified chunk from either the beginning or
1445 * the end
1446 */
1447static int shrink_vma(struct mm_struct *mm,
1448 struct vm_area_struct *vma,
1449 unsigned long from, unsigned long to)
1450{
1451 struct vm_region *region;
1452
David Howells8feae132009-01-08 12:04:47 +00001453 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1454 * and list */
1455 delete_vma_from_mm(vma);
1456 if (from > vma->vm_start)
1457 vma->vm_end = from;
1458 else
1459 vma->vm_start = to;
1460 add_vma_to_mm(mm, vma);
1461
1462 /* cut the backing region down to size */
1463 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001464 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001465
1466 down_write(&nommu_region_sem);
1467 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001468 if (from > region->vm_start) {
1469 to = region->vm_top;
1470 region->vm_top = region->vm_end = from;
1471 } else {
David Howells8feae132009-01-08 12:04:47 +00001472 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001473 }
David Howells8feae132009-01-08 12:04:47 +00001474 add_nommu_region(region);
1475 up_write(&nommu_region_sem);
1476
1477 free_page_series(from, to);
1478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
David Howells30340972006-09-27 01:50:20 -07001481/*
1482 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001483 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1484 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001485 */
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001486int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
David Howells8feae132009-01-08 12:04:47 +00001488 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001489 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001490 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Bob Liuf67d9b12011-05-24 17:12:56 -07001492 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001493 if (len == 0)
1494 return -EINVAL;
1495
Bob Liuf67d9b12011-05-24 17:12:56 -07001496 end = start + len;
1497
David Howells8feae132009-01-08 12:04:47 +00001498 /* find the first potentially overlapping VMA */
1499 vma = find_vma(mm, start);
1500 if (!vma) {
Choi Gi-yongac714902014-04-07 15:37:36 -07001501 static int limit;
David Howells33e5d7692009-04-02 16:56:32 -07001502 if (limit < 5) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001503 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1504 current->pid, current->comm,
1505 start, start + len - 1);
David Howells33e5d7692009-04-02 16:56:32 -07001506 limit++;
1507 }
David Howells8feae132009-01-08 12:04:47 +00001508 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
David Howells8feae132009-01-08 12:04:47 +00001511 /* we're allowed to split an anonymous VMA but not a file-backed one */
1512 if (vma->vm_file) {
1513 do {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001514 if (start > vma->vm_start)
David Howells8feae132009-01-08 12:04:47 +00001515 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001516 if (end == vma->vm_end)
1517 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001518 vma = vma->vm_next;
1519 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001520 return -EINVAL;
1521 } else {
1522 /* the chunk must be a subset of the VMA found */
1523 if (start == vma->vm_start && end == vma->vm_end)
1524 goto erase_whole_vma;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001525 if (start < vma->vm_start || end > vma->vm_end)
David Howells8feae132009-01-08 12:04:47 +00001526 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001527 if (offset_in_page(start))
David Howells8feae132009-01-08 12:04:47 +00001528 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001529 if (end != vma->vm_end && offset_in_page(end))
David Howells8feae132009-01-08 12:04:47 +00001530 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001531 if (start != vma->vm_start && end != vma->vm_end) {
1532 ret = split_vma(mm, vma, start, 1);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001533 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001534 return ret;
David Howells8feae132009-01-08 12:04:47 +00001535 }
1536 return shrink_vma(mm, vma, start, end);
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
David Howells8feae132009-01-08 12:04:47 +00001539erase_whole_vma:
1540 delete_vma_from_mm(vma);
1541 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 0;
1543}
Paul Mundtb5073172007-07-21 04:37:25 -07001544EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Al Virobfce2812012-04-20 21:57:04 -04001546int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001547{
Al Virobfce2812012-04-20 21:57:04 -04001548 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001549 int ret;
David Howells30340972006-09-27 01:50:20 -07001550
1551 down_write(&mm->mmap_sem);
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001552 ret = do_munmap(mm, addr, len, NULL);
David Howells30340972006-09-27 01:50:20 -07001553 up_write(&mm->mmap_sem);
1554 return ret;
1555}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001556EXPORT_SYMBOL(vm_munmap);
1557
1558SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1559{
Al Virobfce2812012-04-20 21:57:04 -04001560 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001561}
David Howells30340972006-09-27 01:50:20 -07001562
1563/*
David Howells8feae132009-01-08 12:04:47 +00001564 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001565 */
David Howells8feae132009-01-08 12:04:47 +00001566void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
David Howells8feae132009-01-08 12:04:47 +00001568 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
David Howells8feae132009-01-08 12:04:47 +00001570 if (!mm)
1571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
David Howells8feae132009-01-08 12:04:47 +00001573 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
David Howells8feae132009-01-08 12:04:47 +00001575 while ((vma = mm->mmap)) {
1576 mm->mmap = vma->vm_next;
1577 delete_vma_from_mm(vma);
1578 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001579 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581}
1582
Linus Torvalds5d22fc22016-05-27 15:57:31 -07001583int vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 return -ENOMEM;
1586}
1587
1588/*
David Howells6fa5f802006-09-27 01:50:21 -07001589 * expand (or shrink) an existing mapping, potentially moving it at the same
1590 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 *
David Howells6fa5f802006-09-27 01:50:21 -07001592 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001593 * as long as it stays within the region allocated by do_mmap_private() and the
1594 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *
David Howells6fa5f802006-09-27 01:50:21 -07001596 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Al Viro4b377ba2013-03-04 10:47:59 -05001598static unsigned long do_mremap(unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 unsigned long old_len, unsigned long new_len,
1600 unsigned long flags, unsigned long new_addr)
1601{
David Howells6fa5f802006-09-27 01:50:21 -07001602 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001605 old_len = PAGE_ALIGN(old_len);
1606 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001607 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return (unsigned long) -EINVAL;
1609
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001610 if (offset_in_page(addr))
David Howells8feae132009-01-08 12:04:47 +00001611 return -EINVAL;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (flags & MREMAP_FIXED && new_addr != addr)
1614 return (unsigned long) -EINVAL;
1615
David Howells8feae132009-01-08 12:04:47 +00001616 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001617 if (!vma)
1618 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
David Howells6fa5f802006-09-27 01:50:21 -07001620 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return (unsigned long) -EFAULT;
1622
David Howells6fa5f802006-09-27 01:50:21 -07001623 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return (unsigned long) -EPERM;
1625
David Howells8feae132009-01-08 12:04:47 +00001626 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return (unsigned long) -ENOMEM;
1628
1629 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001630 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001631 return vma->vm_start;
1632}
1633
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001634SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1635 unsigned long, new_len, unsigned long, flags,
1636 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001637{
1638 unsigned long ret;
1639
1640 down_write(&current->mm->mmap_sem);
1641 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1642 up_write(&current->mm->mmap_sem);
1643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Keith Buschdf06b372018-10-26 15:10:28 -07001646struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1647 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 return NULL;
1650}
1651
Bob Liu8f3b1322011-07-08 15:39:46 -07001652int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1653 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Bob Liu8f3b1322011-07-08 15:39:46 -07001655 if (addr != (pfn << PAGE_SHIFT))
1656 return -EINVAL;
1657
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001658 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
Luke Yang22c4af42006-07-14 00:24:09 -07001661EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001663int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1664{
1665 unsigned long pfn = start >> PAGE_SHIFT;
1666 unsigned long vm_len = vma->vm_end - vma->vm_start;
1667
1668 pfn += vma->vm_pgoff;
1669 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1670}
1671EXPORT_SYMBOL(vm_iomap_memory);
1672
Paul Mundtf905bc42008-02-04 22:29:59 -08001673int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1674 unsigned long pgoff)
1675{
1676 unsigned int size = vma->vm_end - vma->vm_start;
1677
1678 if (!(vma->vm_flags & VM_USERMAP))
1679 return -EINVAL;
1680
1681 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1682 vma->vm_end = vma->vm_start + size;
1683
1684 return 0;
1685}
1686EXPORT_SYMBOL(remap_vmalloc_range);
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1689 unsigned long len, unsigned long pgoff, unsigned long flags)
1690{
1691 return -ENOMEM;
1692}
1693
Souptick Joarder2bcd6452018-06-07 17:08:00 -07001694vm_fault_t filemap_fault(struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001695{
1696 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001697 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001698}
Paul Mundtb5073172007-07-21 04:37:25 -07001699EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001700
Jan Kara82b0f8c2016-12-14 15:06:58 -08001701void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001702 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07001703{
1704 BUG();
1705}
1706EXPORT_SYMBOL(filemap_map_pages);
1707
Eric W. Biederman84d77d32016-11-22 12:06:50 -06001708int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01001709 unsigned long addr, void *buf, int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07001710{
David Howells0ec76a12006-09-27 01:50:15 -07001711 struct vm_area_struct *vma;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01001712 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07001713
Konstantin Khlebnikov1e426fe2019-07-11 21:00:07 -07001714 if (down_read_killable(&mm->mmap_sem))
1715 return 0;
David Howells0ec76a12006-09-27 01:50:15 -07001716
1717 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001718 vma = find_vma(mm, addr);
1719 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001720 /* don't overrun this mapping */
1721 if (addr + len >= vma->vm_end)
1722 len = vma->vm_end - addr;
1723
1724 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001725 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001726 copy_to_user_page(vma, NULL, addr,
1727 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001728 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001729 copy_from_user_page(vma, NULL, addr,
1730 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001731 else
1732 len = 0;
1733 } else {
1734 len = 0;
1735 }
1736
1737 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001738
1739 return len;
1740}
1741
1742/**
Mike Rapoportb7701a52018-02-06 15:42:13 -08001743 * access_remote_vm - access another process' address space
Mike Frysingerf55f1992011-03-29 14:05:12 +01001744 * @mm: the mm_struct of the target address space
1745 * @addr: start address to access
1746 * @buf: source or destination buffer
1747 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001748 * @gup_flags: flags modifying lookup behaviour
Mike Frysingerf55f1992011-03-29 14:05:12 +01001749 *
1750 * The caller must hold a reference on @mm.
1751 */
1752int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001753 void *buf, int len, unsigned int gup_flags)
Mike Frysingerf55f1992011-03-29 14:05:12 +01001754{
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001755 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001756}
1757
1758/*
1759 * Access another process' address space.
1760 * - source/target buffer must be kernel space
1761 */
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001762int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1763 unsigned int gup_flags)
Mike Frysingerf55f1992011-03-29 14:05:12 +01001764{
1765 struct mm_struct *mm;
1766
1767 if (addr + len < addr)
1768 return 0;
1769
1770 mm = get_task_mm(tsk);
1771 if (!mm)
1772 return 0;
1773
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001774 len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001775
David Howells0ec76a12006-09-27 01:50:15 -07001776 mmput(mm);
1777 return len;
1778}
Catalin Marinasfcd35852016-11-01 14:43:25 -07001779EXPORT_SYMBOL_GPL(access_process_vm);
David Howells7e660872010-01-15 17:01:39 -08001780
1781/**
1782 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1783 * @inode: The inode to check
1784 * @size: The current filesize of the inode
1785 * @newsize: The proposed filesize of the inode
1786 *
1787 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1788 * make sure that that any outstanding VMAs aren't broken and then shrink the
1789 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1790 * automatically grant mappings that are too large.
1791 */
1792int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1793 size_t newsize)
1794{
1795 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08001796 struct vm_region *region;
1797 pgoff_t low, high;
1798 size_t r_size, r_top;
1799
1800 low = newsize >> PAGE_SHIFT;
1801 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1802
1803 down_write(&nommu_region_sem);
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001804 i_mmap_lock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001805
1806 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07001807 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08001808 /* found one - only interested if it's shared out of the page
1809 * cache */
1810 if (vma->vm_flags & VM_SHARED) {
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001811 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001812 up_write(&nommu_region_sem);
1813 return -ETXTBSY; /* not quite true, but near enough */
1814 }
1815 }
1816
1817 /* reduce any regions that overlap the dead zone - if in existence,
1818 * these will be pointed to by VMAs that don't overlap the dead zone
1819 *
1820 * we don't check for any regions that start beyond the EOF as there
1821 * shouldn't be any
1822 */
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001823 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08001824 if (!(vma->vm_flags & VM_SHARED))
1825 continue;
1826
1827 region = vma->vm_region;
1828 r_size = region->vm_top - region->vm_start;
1829 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1830
1831 if (r_top > newsize) {
1832 region->vm_top -= r_top - newsize;
1833 if (region->vm_end > region->vm_top)
1834 region->vm_end = region->vm_top;
1835 }
1836 }
1837
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001838 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001839 up_write(&nommu_region_sem);
1840 return 0;
1841}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001842
1843/*
1844 * Initialise sysctl_user_reserve_kbytes.
1845 *
1846 * This is intended to prevent a user from starting a single memory hogging
1847 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1848 * mode.
1849 *
1850 * The default value is min(3% of free memory, 128MB)
1851 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1852 */
1853static int __meminit init_user_reserve(void)
1854{
1855 unsigned long free_kbytes;
1856
Michal Hockoc41f0122017-09-06 16:23:36 -07001857 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001858
1859 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1860 return 0;
1861}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04001862subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001863
1864/*
1865 * Initialise sysctl_admin_reserve_kbytes.
1866 *
1867 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1868 * to log in and kill a memory hogging process.
1869 *
1870 * Systems with more than 256MB will reserve 8MB, enough to recover
1871 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1872 * only reserve 3% of free pages by default.
1873 */
1874static int __meminit init_admin_reserve(void)
1875{
1876 unsigned long free_kbytes;
1877
Michal Hockoc41f0122017-09-06 16:23:36 -07001878 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001879
1880 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
1881 return 0;
1882}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04001883subsys_initcall(init_admin_reserve);