blob: d8c02fbe03b587835f4f08537eda9d70d47a982d [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 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900111 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Lorenzo Stoakes0d731752016-10-24 10:57:25 +0100114static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Michel Lespinasse28a35712013-02-22 16:35:55 -0800115 unsigned long start, unsigned long nr_pages,
116 unsigned int foll_flags, struct page **pages,
117 struct vm_area_struct **vmas, int *nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700119 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700120 unsigned long vm_flags;
121 int i;
122
123 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700124 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700125 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700126 vm_flags = (foll_flags & FOLL_WRITE) ?
127 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
128 vm_flags &= (foll_flags & FOLL_FORCE) ?
129 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Peter Zijlstra9d737772009-06-25 11:58:55 +0200131 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000132 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700133 if (!vma)
134 goto finish_or_fault;
135
136 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700137 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
138 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700139 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (pages) {
142 pages[i] = virt_to_page(start);
143 if (pages[i])
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300144 get_page(pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700147 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000148 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
David Howells7b4d5b82006-09-27 01:50:18 -0700150
151 return i;
152
153finish_or_fault:
154 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
Nick Pigginb291f002008-10-18 20:26:44 -0700156
Nick Pigginb291f002008-10-18 20:26:44 -0700157/*
158 * get a list of pages in an address range belonging to the specified process
159 * and indicate the VMA that covers each page
160 * - this is potentially dodgy as we may end incrementing the page count of a
161 * slab page or a secondary page from a compound page
162 * - don't permit access to VMAs that don't support it, such as I/O mappings
163 */
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200164long get_user_pages(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100165 unsigned int gup_flags, struct page **pages,
Michel Lespinasse28a35712013-02-22 16:35:55 -0800166 struct vm_area_struct **vmas)
Nick Pigginb291f002008-10-18 20:26:44 -0700167{
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100168 return __get_user_pages(current, current->mm, start, nr_pages,
169 gup_flags, pages, vmas, NULL);
Nick Pigginb291f002008-10-18 20:26:44 -0700170}
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200171EXPORT_SYMBOL(get_user_pages);
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000172
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200173long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakes3b913172016-10-13 01:20:14 +0100174 unsigned int gup_flags, struct page **pages,
Dave Hansencde70142016-02-12 13:01:55 -0800175 int *locked)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800176{
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100177 return get_user_pages(start, nr_pages, gup_flags, pages, NULL);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800178}
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200179EXPORT_SYMBOL(get_user_pages_locked);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800180
Lorenzo Stoakes8b7457e2016-12-14 15:06:55 -0800181static long __get_user_pages_unlocked(struct task_struct *tsk,
182 struct mm_struct *mm, unsigned long start,
183 unsigned long nr_pages, struct page **pages,
184 unsigned int gup_flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800185{
186 long ret;
187 down_read(&mm->mmap_sem);
Dave Hansencde70142016-02-12 13:01:55 -0800188 ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages,
189 NULL, NULL);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800190 up_read(&mm->mmap_sem);
191 return ret;
192}
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800193
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200194long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
Lorenzo Stoakesc1641542016-10-13 01:20:13 +0100195 struct page **pages, unsigned int gup_flags)
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800196{
Dave Hansencde70142016-02-12 13:01:55 -0800197 return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
Lorenzo Stoakesc1641542016-10-13 01:20:13 +0100198 pages, gup_flags);
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800199}
Ingo Molnarc12d2da2016-04-04 10:24:58 +0200200EXPORT_SYMBOL(get_user_pages_unlocked);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800201
Paul Mundtdfc2f912009-06-26 04:31:57 +0900202/**
203 * follow_pfn - look up PFN at a user virtual address
204 * @vma: memory mapping
205 * @address: user virtual address
206 * @pfn: location to store found PFN
207 *
208 * Only IO mappings and raw PFN mappings are allowed.
209 *
210 * Returns zero and the pfn at @pfn on success, -ve otherwise.
211 */
212int follow_pfn(struct vm_area_struct *vma, unsigned long address,
213 unsigned long *pfn)
214{
215 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
216 return -EINVAL;
217
218 *pfn = address >> PAGE_SHIFT;
219 return 0;
220}
221EXPORT_SYMBOL(follow_pfn);
222
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700223LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800225void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 kfree(addr);
228}
Paul Mundtb5073172007-07-21 04:37:25 -0700229EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Al Virodd0fc662005-10-07 07:46:04 +0100231void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200234 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
235 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Nick Piggin84097512006-03-22 00:08:34 -0800237 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Paul Mundtb5073172007-07-21 04:37:25 -0700239EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Michal Hockoa7c3e902017-05-08 15:57:09 -0700241void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
242{
243 return __vmalloc(size, flags, PAGE_KERNEL);
244}
245
Paul Mundtf905bc42008-02-04 22:29:59 -0800246void *vmalloc_user(unsigned long size)
247{
248 void *ret;
249
Michal Hocko19809c22017-05-08 15:57:44 -0700250 ret = __vmalloc(size, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL);
Paul Mundtf905bc42008-02-04 22:29:59 -0800251 if (ret) {
252 struct vm_area_struct *vma;
253
254 down_write(&current->mm->mmap_sem);
255 vma = find_vma(current->mm, (unsigned long)ret);
256 if (vma)
257 vma->vm_flags |= VM_USERMAP;
258 up_write(&current->mm->mmap_sem);
259 }
260
261 return ret;
262}
263EXPORT_SYMBOL(vmalloc_user);
264
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800265struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 return virt_to_page(addr);
268}
Paul Mundtb5073172007-07-21 04:37:25 -0700269EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800271unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 return page_to_pfn(virt_to_page(addr));
274}
Paul Mundtb5073172007-07-21 04:37:25 -0700275EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277long vread(char *buf, char *addr, unsigned long count)
278{
Chen Gang9bde9162013-07-03 15:02:36 -0700279 /* Don't allow overflow */
280 if ((unsigned long) buf + count < count)
281 count = -(unsigned long) buf;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(buf, addr, count);
284 return count;
285}
286
287long vwrite(char *buf, char *addr, unsigned long count)
288{
289 /* Don't allow overflow */
290 if ((unsigned long) addr + count < count)
291 count = -(unsigned long) addr;
292
293 memcpy(addr, buf, count);
Choi Gi-yongac714902014-04-07 15:37:36 -0700294 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900298 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *
300 * @size: allocation size
301 *
302 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900303 * allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200305 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * use __vmalloc() instead.
307 */
308void *vmalloc(unsigned long size)
309{
310 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
311}
Andrew Mortonf6138882006-02-28 16:59:18 -0800312EXPORT_SYMBOL(vmalloc);
313
Dave Younge1ca7782010-10-26 14:22:06 -0700314/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900315 * vzalloc - allocate virtually contiguous memory with zero fill
Dave Younge1ca7782010-10-26 14:22:06 -0700316 *
317 * @size: allocation size
318 *
319 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900320 * allocator and map them into contiguous kernel virtual space.
Dave Younge1ca7782010-10-26 14:22:06 -0700321 * The memory allocated is set to zero.
322 *
323 * For tight control over page level allocator and protection flags
324 * use __vmalloc() instead.
325 */
326void *vzalloc(unsigned long size)
327{
328 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
329 PAGE_KERNEL);
330}
331EXPORT_SYMBOL(vzalloc);
332
333/**
334 * vmalloc_node - allocate memory on a specific node
335 * @size: allocation size
336 * @node: numa node
337 *
338 * Allocate enough pages to cover @size from the page level
339 * allocator and map them into contiguous kernel virtual space.
340 *
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
343 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800344void *vmalloc_node(unsigned long size, int node)
345{
346 return vmalloc(size);
347}
Paul Mundt9a14f652010-12-24 11:50:34 +0900348EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700349
350/**
351 * vzalloc_node - allocate memory on a specific node with zero fill
352 * @size: allocation size
353 * @node: numa node
354 *
355 * Allocate enough pages to cover @size from the page level
356 * allocator and map them into contiguous kernel virtual space.
357 * The memory allocated is set to zero.
358 *
359 * For tight control over page level allocator and protection flags
360 * use __vmalloc() instead.
361 */
362void *vzalloc_node(unsigned long size, int node)
363{
364 return vzalloc(size);
365}
366EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Paul Mundt1af446e2008-08-04 16:01:47 +0900368/**
369 * vmalloc_exec - allocate virtually contiguous, executable memory
370 * @size: allocation size
371 *
372 * Kernel-internal function to allocate enough pages to cover @size
373 * the page level allocator and map them into contiguous and
374 * executable kernel virtual space.
375 *
376 * For tight control over page level allocator and protection flags
377 * use __vmalloc() instead.
378 */
379
380void *vmalloc_exec(unsigned long size)
381{
382 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
383}
384
Paul Mundtb5073172007-07-21 04:37:25 -0700385/**
386 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * @size: allocation size
388 *
389 * Allocate enough 32bit PA addressable pages to cover @size from the
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900390 * page level allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392void *vmalloc_32(unsigned long size)
393{
394 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
395}
Paul Mundtb5073172007-07-21 04:37:25 -0700396EXPORT_SYMBOL(vmalloc_32);
397
398/**
399 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
400 * @size: allocation size
401 *
402 * The resulting memory area is 32bit addressable and zeroed so it can be
403 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800404 *
405 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
406 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700407 */
408void *vmalloc_32_user(unsigned long size)
409{
Paul Mundtf905bc42008-02-04 22:29:59 -0800410 /*
411 * We'll have to sort out the ZONE_DMA bits for 64-bit,
412 * but for now this can simply use vmalloc_user() directly.
413 */
414 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700415}
416EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
419{
420 BUG();
421 return NULL;
422}
Paul Mundtb5073172007-07-21 04:37:25 -0700423EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800425void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 BUG();
428}
Paul Mundtb5073172007-07-21 04:37:25 -0700429EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Paul Mundteb6434d2009-01-21 17:45:47 +0900431void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
432{
433 BUG();
434 return NULL;
435}
436EXPORT_SYMBOL(vm_map_ram);
437
438void vm_unmap_ram(const void *mem, unsigned int count)
439{
440 BUG();
441}
442EXPORT_SYMBOL(vm_unmap_ram);
443
444void vm_unmap_aliases(void)
445{
446}
447EXPORT_SYMBOL_GPL(vm_unmap_aliases);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700450 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
451 * have one.
452 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700453void __weak vmalloc_sync_all(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700454{
455}
456
David Vrabelcd129092011-09-29 16:53:32 +0100457struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900458{
459 BUG();
460 return NULL;
461}
462EXPORT_SYMBOL_GPL(alloc_vm_area);
463
464void free_vm_area(struct vm_struct *area)
465{
466 BUG();
467}
468EXPORT_SYMBOL_GPL(free_vm_area);
469
Paul Mundtb5073172007-07-21 04:37:25 -0700470int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
471 struct page *page)
472{
473 return -EINVAL;
474}
475EXPORT_SYMBOL(vm_insert_page);
476
Souptick Joardera667d742019-05-13 17:21:56 -0700477int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
478 unsigned long num)
479{
480 return -EINVAL;
481}
482EXPORT_SYMBOL(vm_map_pages);
483
484int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
485 unsigned long num)
486{
487 return -EINVAL;
488}
489EXPORT_SYMBOL(vm_map_pages_zero);
490
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700491/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * sys_brk() for the most part doesn't need the global kernel
493 * lock, except when an application is doing something nasty
494 * like trying to un-brk an area that has already been mapped
495 * to a regular file. in this case, the unmapping will need
496 * to invoke file system routines that need the global lock.
497 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100498SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct mm_struct *mm = current->mm;
501
502 if (brk < mm->start_brk || brk > mm->context.end_brk)
503 return mm->brk;
504
505 if (mm->brk == brk)
506 return mm->brk;
507
508 /*
509 * Always allow shrinking brk
510 */
511 if (brk <= mm->brk) {
512 mm->brk = brk;
513 return brk;
514 }
515
516 /*
517 * Ok, looks good - let it rip.
518 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000519 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return mm->brk = brk;
521}
522
David Howells8feae132009-01-08 12:04:47 +0000523/*
seokhoon.yoon3edf41d2017-02-24 14:56:44 -0800524 * initialise the percpu counter for VM and region record slabs
David Howells8feae132009-01-08 12:04:47 +0000525 */
526void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700528 int ret;
529
Tejun Heo908c7f12014-09-08 09:51:29 +0900530 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700531 VM_BUG_ON(ret);
Vladimir Davydov5d097052016-01-14 15:18:21 -0800532 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
David Howells8feae132009-01-08 12:04:47 +0000533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
David Howells8feae132009-01-08 12:04:47 +0000535/*
536 * validate the region tree
537 * - the caller must hold the region lock
538 */
539#ifdef CONFIG_DEBUG_NOMMU_REGIONS
540static noinline void validate_nommu_regions(void)
541{
542 struct vm_region *region, *last;
543 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David Howells8feae132009-01-08 12:04:47 +0000545 lastp = rb_first(&nommu_region_tree);
546 if (!lastp)
547 return;
548
549 last = rb_entry(lastp, struct vm_region, vm_rb);
Geliang Tangc9427bc2015-11-05 18:48:38 -0800550 BUG_ON(last->vm_end <= last->vm_start);
551 BUG_ON(last->vm_top < last->vm_end);
David Howells8feae132009-01-08 12:04:47 +0000552
553 while ((p = rb_next(lastp))) {
554 region = rb_entry(p, struct vm_region, vm_rb);
555 last = rb_entry(lastp, struct vm_region, vm_rb);
556
Geliang Tangc9427bc2015-11-05 18:48:38 -0800557 BUG_ON(region->vm_end <= region->vm_start);
558 BUG_ON(region->vm_top < region->vm_end);
559 BUG_ON(region->vm_start < last->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000560
561 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563}
David Howells8feae132009-01-08 12:04:47 +0000564#else
David Howells33e5d7692009-04-02 16:56:32 -0700565static void validate_nommu_regions(void)
566{
567}
David Howells8feae132009-01-08 12:04:47 +0000568#endif
569
570/*
571 * add a region into the global tree
572 */
573static void add_nommu_region(struct vm_region *region)
574{
575 struct vm_region *pregion;
576 struct rb_node **p, *parent;
577
578 validate_nommu_regions();
579
David Howells8feae132009-01-08 12:04:47 +0000580 parent = NULL;
581 p = &nommu_region_tree.rb_node;
582 while (*p) {
583 parent = *p;
584 pregion = rb_entry(parent, struct vm_region, vm_rb);
585 if (region->vm_start < pregion->vm_start)
586 p = &(*p)->rb_left;
587 else if (region->vm_start > pregion->vm_start)
588 p = &(*p)->rb_right;
589 else if (pregion == region)
590 return;
591 else
592 BUG();
593 }
594
595 rb_link_node(&region->vm_rb, parent, p);
596 rb_insert_color(&region->vm_rb, &nommu_region_tree);
597
598 validate_nommu_regions();
599}
600
601/*
602 * delete a region from the global tree
603 */
604static void delete_nommu_region(struct vm_region *region)
605{
606 BUG_ON(!nommu_region_tree.rb_node);
607
608 validate_nommu_regions();
609 rb_erase(&region->vm_rb, &nommu_region_tree);
610 validate_nommu_regions();
611}
612
613/*
614 * free a contiguous series of pages
615 */
616static void free_page_series(unsigned long from, unsigned long to)
617{
618 for (; from < to; from += PAGE_SIZE) {
619 struct page *page = virt_to_page(from);
620
David Howells33e5d7692009-04-02 16:56:32 -0700621 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000622 put_page(page);
623 }
624}
625
626/*
627 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700628 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000629 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000630 * will equal vm_start
631 */
632static void __put_nommu_region(struct vm_region *region)
633 __releases(nommu_region_sem)
634{
David Howells8feae132009-01-08 12:04:47 +0000635 BUG_ON(!nommu_region_tree.rb_node);
636
David Howells1e2ae592010-01-15 17:01:33 -0800637 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000638 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000639 delete_nommu_region(region);
640 up_write(&nommu_region_sem);
641
642 if (region->vm_file)
643 fput(region->vm_file);
644
645 /* IO memory and memory shared directly out of the pagecache
646 * from ramfs/tmpfs mustn't be released here */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700647 if (region->vm_flags & VM_MAPPED_COPY)
Paul Mundtdd8632a2009-01-08 12:04:47 +0000648 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000649 kmem_cache_free(vm_region_jar, region);
650 } else {
651 up_write(&nommu_region_sem);
652 }
653}
654
655/*
656 * release a reference to a region
657 */
658static void put_nommu_region(struct vm_region *region)
659{
660 down_write(&nommu_region_sem);
661 __put_nommu_region(region);
662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
David Howells30340972006-09-27 01:50:20 -0700664/*
665 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000666 * and tree and add to the address space's page tree also if not an anonymous
667 * page
David Howells30340972006-09-27 01:50:20 -0700668 * - should be called with mm->mmap_sem held writelocked
669 */
David Howells8feae132009-01-08 12:04:47 +0000670static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700671{
Namhyung Kim6038def2011-05-24 17:11:22 -0700672 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000673 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700674 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700675
David Howells8feae132009-01-08 12:04:47 +0000676 BUG_ON(!vma->vm_region);
677
678 mm->map_count++;
679 vma->vm_mm = mm;
680
681 /* add the VMA to the mapping */
682 if (vma->vm_file) {
683 mapping = vma->vm_file->f_mapping;
684
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800685 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000686 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700687 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000688 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800689 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000690 }
691
692 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700693 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000694 p = &mm->mm_rb.rb_node;
695 while (*p) {
696 parent = *p;
697 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
698
699 /* sort by: start addr, end addr, VMA struct addr in that order
700 * (the latter is necessary as we may get identical VMAs) */
701 if (vma->vm_start < pvma->vm_start)
702 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700703 else if (vma->vm_start > pvma->vm_start) {
704 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000705 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700706 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000707 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700708 else if (vma->vm_end > pvma->vm_end) {
709 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000710 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700711 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000712 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700713 else if (vma > pvma) {
714 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000715 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700716 } else
David Howells8feae132009-01-08 12:04:47 +0000717 BUG();
718 }
719
720 rb_link_node(&vma->vm_rb, parent, p);
721 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
722
723 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700724 prev = NULL;
725 if (rb_prev)
726 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700727
Namhyung Kim6038def2011-05-24 17:11:22 -0700728 __vma_link_list(mm, vma, prev, parent);
David Howells8feae132009-01-08 12:04:47 +0000729}
730
731/*
732 * delete a VMA from its owning mm_struct and address space
733 */
734static void delete_vma_from_mm(struct vm_area_struct *vma)
735{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700736 int i;
David Howells8feae132009-01-08 12:04:47 +0000737 struct address_space *mapping;
738 struct mm_struct *mm = vma->vm_mm;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700739 struct task_struct *curr = current;
David Howells8feae132009-01-08 12:04:47 +0000740
David Howells8feae132009-01-08 12:04:47 +0000741 mm->map_count--;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700742 for (i = 0; i < VMACACHE_SIZE; i++) {
743 /* if the vma is cached, invalidate the entire cache */
Ingo Molnar314ff782017-02-03 11:03:31 +0100744 if (curr->vmacache.vmas[i] == vma) {
Steven Miaoe020d5b2014-06-23 13:22:02 -0700745 vmacache_invalidate(mm);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700746 break;
747 }
748 }
David Howells8feae132009-01-08 12:04:47 +0000749
750 /* remove the VMA from the mapping */
751 if (vma->vm_file) {
752 mapping = vma->vm_file->f_mapping;
753
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800754 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000755 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700756 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000757 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800758 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000759 }
760
761 /* remove from the MM's tree and list */
762 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700763
764 if (vma->vm_prev)
765 vma->vm_prev->vm_next = vma->vm_next;
766 else
767 mm->mmap = vma->vm_next;
768
769 if (vma->vm_next)
770 vma->vm_next->vm_prev = vma->vm_prev;
David Howells8feae132009-01-08 12:04:47 +0000771}
772
773/*
774 * destroy a VMA record
775 */
776static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
777{
David Howells8feae132009-01-08 12:04:47 +0000778 if (vma->vm_ops && vma->vm_ops->close)
779 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700780 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000781 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000782 put_nommu_region(vma->vm_region);
Linus Torvalds3928d4f2018-07-21 13:48:51 -0700783 vm_area_free(vma);
David Howells30340972006-09-27 01:50:20 -0700784}
785
786/*
787 * look up the first VMA in which addr resides, NULL if none
788 * - should be called with mm->mmap_sem at least held readlocked
789 */
790struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
791{
David Howells8feae132009-01-08 12:04:47 +0000792 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700793
David Howells8feae132009-01-08 12:04:47 +0000794 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700795 vma = vmacache_find(mm, addr);
796 if (likely(vma))
David Howells8feae132009-01-08 12:04:47 +0000797 return vma;
798
Namhyung Kime922c4c2011-05-24 17:11:24 -0700799 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000800 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700801 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000802 if (vma->vm_start > addr)
803 return NULL;
804 if (vma->vm_end > addr) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700805 vmacache_update(addr, vma);
David Howells8feae132009-01-08 12:04:47 +0000806 return vma;
807 }
David Howells30340972006-09-27 01:50:20 -0700808 }
809
David Howells30340972006-09-27 01:50:20 -0700810 return NULL;
811}
812EXPORT_SYMBOL(find_vma);
813
814/*
David Howells930e6522006-09-27 01:50:22 -0700815 * find a VMA
816 * - we don't extend stack VMAs under NOMMU conditions
817 */
818struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
819{
David Howells7561e8c2010-03-25 16:48:38 +0000820 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700821}
822
David Howells8feae132009-01-08 12:04:47 +0000823/*
824 * expand a stack to a given address
825 * - not supported under NOMMU conditions
826 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700827int expand_stack(struct vm_area_struct *vma, unsigned long address)
828{
829 return -ENOMEM;
830}
831
David Howells930e6522006-09-27 01:50:22 -0700832/*
David Howells6fa5f802006-09-27 01:50:21 -0700833 * look up the first VMA exactly that exactly matches addr
834 * - should be called with mm->mmap_sem at least held readlocked
835 */
David Howells8feae132009-01-08 12:04:47 +0000836static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
837 unsigned long addr,
838 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000841 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
David Howells8feae132009-01-08 12:04:47 +0000843 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700844 vma = vmacache_find_exact(mm, addr, end);
845 if (vma)
David Howells8feae132009-01-08 12:04:47 +0000846 return vma;
847
Namhyung Kime922c4c2011-05-24 17:11:24 -0700848 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000849 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700850 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000851 if (vma->vm_start < addr)
852 continue;
853 if (vma->vm_start > addr)
854 return NULL;
855 if (vma->vm_end == end) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700856 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return vma;
David Howells8feae132009-01-08 12:04:47 +0000858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 return NULL;
862}
863
David Howells30340972006-09-27 01:50:20 -0700864/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * determine whether a mapping should be permitted and, if so, what sort of
866 * mapping we're capable of supporting
867 */
868static int validate_mmap_request(struct file *file,
869 unsigned long addr,
870 unsigned long len,
871 unsigned long prot,
872 unsigned long flags,
873 unsigned long pgoff,
874 unsigned long *_capabilities)
875{
David Howells8feae132009-01-08 12:04:47 +0000876 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int ret;
878
879 /* do the simple checks first */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700880 if (flags & MAP_FIXED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
884 (flags & MAP_TYPE) != MAP_SHARED)
885 return -EINVAL;
886
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000887 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EINVAL;
889
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000890 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000891 rlen = PAGE_ALIGN(len);
892 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000893 return -ENOMEM;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000896 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000897 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* files must support mmap */
Al Viro72c2d532013-09-22 16:27:52 -0400901 if (!file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -ENODEV;
903
904 /* work out if what we've got could possibly be shared
905 * - we support chardevs that provide their own "memory"
906 * - we support files/blockdevs that are memory backed
907 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100908 if (file->f_op->mmap_capabilities) {
909 capabilities = file->f_op->mmap_capabilities(file);
910 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* no explicit capabilities set, so assume some
912 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500913 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 case S_IFREG:
915 case S_IFBLK:
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100916 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918
919 case S_IFCHR:
920 capabilities =
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100921 NOMMU_MAP_DIRECT |
922 NOMMU_MAP_READ |
923 NOMMU_MAP_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925
926 default:
927 return -EINVAL;
928 }
929 }
930
931 /* eliminate any capabilities that we can't support on this
932 * device */
933 if (!file->f_op->get_unmapped_area)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100934 capabilities &= ~NOMMU_MAP_DIRECT;
Al Viro6e242a12015-03-31 12:35:13 -0400935 if (!(file->f_mode & FMODE_CAN_READ))
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100936 capabilities &= ~NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Graff Yang28d7a6a2009-08-18 14:11:17 -0700938 /* The file shall have been opened with read permission. */
939 if (!(file->f_mode & FMODE_READ))
940 return -EACCES;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (flags & MAP_SHARED) {
943 /* do checks for writing, appending and locking */
944 if ((prot & PROT_WRITE) &&
945 !(file->f_mode & FMODE_WRITE))
946 return -EACCES;
947
Al Viro496ad9a2013-01-23 17:07:38 -0500948 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 (file->f_mode & FMODE_WRITE))
950 return -EACCES;
951
Jeff Laytond7a06982014-03-10 09:54:15 -0400952 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EAGAIN;
954
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100955 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -ENODEV;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* we mustn't privatise shared mappings */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100959 capabilities &= ~NOMMU_MAP_COPY;
Choi Gi-yongac714902014-04-07 15:37:36 -0700960 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* we're going to read the file into private memory we
962 * allocate */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100963 if (!(capabilities & NOMMU_MAP_COPY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -ENODEV;
965
966 /* we don't permit a private writable mapping to be
967 * shared with the backing device */
968 if (prot & PROT_WRITE)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100969 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100972 if (capabilities & NOMMU_MAP_DIRECT) {
973 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
974 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
975 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700976 ) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100977 capabilities &= ~NOMMU_MAP_DIRECT;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700978 if (flags & MAP_SHARED) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700979 pr_warn("MAP_SHARED not completely supported on !MMU\n");
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700980 return -EINVAL;
981 }
982 }
983 }
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* handle executable mappings and implied executable
986 * mappings */
Eric W. Biederman90f85722015-06-29 14:42:03 -0500987 if (path_noexec(&file->f_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (prot & PROT_EXEC)
989 return -EPERM;
Choi Gi-yongac714902014-04-07 15:37:36 -0700990 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* handle implication of PROT_EXEC by PROT_READ */
992 if (current->personality & READ_IMPLIES_EXEC) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100993 if (capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 prot |= PROT_EXEC;
995 }
Choi Gi-yongac714902014-04-07 15:37:36 -0700996 } else if ((prot & PROT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 (prot & PROT_EXEC) &&
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100998 !(capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 ) {
1000 /* backing file is not executable, try to copy */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001001 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Choi Gi-yongac714902014-04-07 15:37:36 -07001003 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* anonymous mappings are always memory backed and can be
1005 * privately mapped
1006 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001007 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 /* handle PROT_EXEC implication by PROT_READ */
1010 if ((prot & PROT_READ) &&
1011 (current->personality & READ_IMPLIES_EXEC))
1012 prot |= PROT_EXEC;
1013 }
1014
1015 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -04001016 ret = security_mmap_addr(addr);
1017 if (ret < 0)
1018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /* looks okay */
1021 *_capabilities = capabilities;
1022 return 0;
1023}
1024
1025/*
1026 * we've determined that we can make the mapping, now translate what we
1027 * now know into VMA flags
1028 */
1029static unsigned long determine_vm_flags(struct file *file,
1030 unsigned long prot,
1031 unsigned long flags,
1032 unsigned long capabilities)
1033{
1034 unsigned long vm_flags;
1035
Dave Hansene6bfb702016-02-12 13:02:31 -08001036 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* vm_flags |= mm->def_flags; */
1038
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001039 if (!(capabilities & NOMMU_MAP_DIRECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001041 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (file && !(prot & PROT_WRITE))
1043 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001044 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* overlay a shareable mapping on the backing device or inode
1046 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1047 * romfs/cramfs */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001048 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001050 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
1053 /* refuse to let anyone share private mappings with this process if
1054 * it's being traced - otherwise breakpoints set in it may interfere
1055 * with another untraced process
1056 */
Tejun Heoa288eec2011-06-17 16:50:37 +02001057 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 vm_flags &= ~VM_MAYSHARE;
1059
1060 return vm_flags;
1061}
1062
1063/*
David Howells8feae132009-01-08 12:04:47 +00001064 * set up a shared mapping on a file (the driver or filesystem provides and
1065 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
David Howells8feae132009-01-08 12:04:47 +00001067static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 int ret;
1070
Miklos Szeredif74ac012017-02-20 16:51:23 +01001071 ret = call_mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001072 if (ret == 0) {
1073 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001074 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (ret != -ENOSYS)
1077 return ret;
1078
David Howells3fa30462010-03-23 13:35:21 -07001079 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1080 * opposed to tried but failed) so we can only give a suitable error as
1081 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -ENODEV;
1083}
1084
1085/*
1086 * set up a private mapping or an anonymous shared mapping
1087 */
David Howells8feae132009-01-08 12:04:47 +00001088static int do_mmap_private(struct vm_area_struct *vma,
1089 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001090 unsigned long len,
1091 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001093 unsigned long total, point;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 void *base;
David Howells8feae132009-01-08 12:04:47 +00001095 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* invoke the file's mapping function so that it can keep track of
1098 * shared mappings on devices or memory
1099 * - VM_MAYSHARE will be set if it may attempt to share
1100 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001101 if (capabilities & NOMMU_MAP_DIRECT) {
Miklos Szeredif74ac012017-02-20 16:51:23 +01001102 ret = call_mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001103 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001105 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1106 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001109 if (ret != -ENOSYS)
1110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* getting an ENOSYS error indicates that direct mmap isn't
1113 * possible (as opposed to tried but failed) so we'll try to
1114 * make a private copy of the data and map that instead */
1115 }
1116
David Howells8feae132009-01-08 12:04:47 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* allocate some memory to hold the mapping
1119 * - note that this may not return a page-aligned address if the object
1120 * we're allocating is smaller than a page
1121 */
Bob Liuf67d9b12011-05-24 17:12:56 -07001122 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +00001123 total = 1 << order;
Bob Liuf67d9b12011-05-24 17:12:56 -07001124 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001125
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001126 /* we don't want to allocate a power-of-2 sized page set */
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001127 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001128 total = point;
David Howells8feae132009-01-08 12:04:47 +00001129
Joonsoo Kimda616532015-02-27 15:51:43 -08001130 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001131 if (!base)
1132 goto enomem;
David Howells8feae132009-01-08 12:04:47 +00001133
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001134 atomic_long_add(total, &mmap_pages_allocated);
1135
David Howells8feae132009-01-08 12:04:47 +00001136 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1137 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001138 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001139 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001140
1141 vma->vm_start = region->vm_start;
1142 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (vma->vm_file) {
1145 /* read the contents of a file into the copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 loff_t fpos;
1147
1148 fpos = vma->vm_pgoff;
1149 fpos <<= PAGE_SHIFT;
1150
Christoph Hellwigb4bf8022017-09-01 17:39:17 +02001151 ret = kernel_read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (ret < 0)
1153 goto error_free;
1154
1155 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001156 if (ret < len)
1157 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Kirill A. Shutemovbfd40ea2018-07-26 16:37:35 -07001159 } else {
1160 vma_set_anonymous(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 return 0;
1164
1165error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001166 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001167 region->vm_start = vma->vm_start = 0;
1168 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001169 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return ret;
1171
1172enomem:
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07001173 pr_err("Allocation of length %lu from process %d (%s) failed\n",
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001174 len, current->pid, current->comm);
Michal Hocko9af744d2017-02-22 15:46:16 -08001175 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -ENOMEM;
1177}
1178
1179/*
1180 * handle mapping creation for uClinux
1181 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001182unsigned long do_mmap(struct file *file,
1183 unsigned long addr,
1184 unsigned long len,
1185 unsigned long prot,
1186 unsigned long flags,
1187 vm_flags_t vm_flags,
1188 unsigned long pgoff,
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001189 unsigned long *populate,
1190 struct list_head *uf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
David Howells8feae132009-01-08 12:04:47 +00001192 struct vm_area_struct *vma;
1193 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct rb_node *rb;
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001195 unsigned long capabilities, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int ret;
1197
Michel Lespinasse41badc12013-02-22 16:32:47 -08001198 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* decide whether we should attempt the mapping, and if so what sort of
1201 * mapping */
1202 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1203 &capabilities);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001204 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return ret;
1206
David Howells06aab5a2009-09-24 12:33:48 +01001207 /* we ignore the address hint */
1208 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001209 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* we've determined that we can make the mapping, now translate what we
1212 * now know into VMA flags */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001213 vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
David Howells8feae132009-01-08 12:04:47 +00001215 /* we're going to need to record the mapping */
1216 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1217 if (!region)
1218 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds490fc052018-07-21 15:24:03 -07001220 vma = vm_area_alloc(current->mm);
David Howells8feae132009-01-08 12:04:47 +00001221 if (!vma)
1222 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
David Howells1e2ae592010-01-15 17:01:33 -08001224 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001225 region->vm_flags = vm_flags;
1226 region->vm_pgoff = pgoff;
1227
David Howells8feae132009-01-08 12:04:47 +00001228 vma->vm_flags = vm_flags;
1229 vma->vm_pgoff = pgoff;
1230
1231 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001232 region->vm_file = get_file(file);
1233 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001234 }
1235
1236 down_write(&nommu_region_sem);
1237
1238 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001240 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * - shared mappings on character devices and memory backed files are
1242 * permitted to overlap inexactly as far as we are concerned for in
1243 * these cases, sharing is handled in the driver or filesystem rather
1244 * than here
1245 */
1246 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001247 struct vm_region *pregion;
1248 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
David Howells8feae132009-01-08 12:04:47 +00001250 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1251 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001252
David Howells8feae132009-01-08 12:04:47 +00001253 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1254 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
David Howells8feae132009-01-08 12:04:47 +00001256 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 continue;
1258
1259 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001260 if (file_inode(pregion->vm_file) !=
1261 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 continue;
1263
David Howells8feae132009-01-08 12:04:47 +00001264 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 continue;
1266
David Howells8feae132009-01-08 12:04:47 +00001267 rpglen = pregion->vm_end - pregion->vm_start;
1268 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1269 rpgend = pregion->vm_pgoff + rpglen;
1270 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 continue;
1272
David Howells8feae132009-01-08 12:04:47 +00001273 /* handle inexactly overlapping matches between
1274 * mappings */
1275 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1276 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1277 /* new mapping is not a subset of the region */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001278 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 goto sharing_violation;
1280 continue;
1281 }
1282
David Howells8feae132009-01-08 12:04:47 +00001283 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001284 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001285 vma->vm_region = pregion;
1286 start = pregion->vm_start;
1287 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1288 vma->vm_start = start;
1289 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001291 if (pregion->vm_flags & VM_MAPPED_COPY)
David Howells8feae132009-01-08 12:04:47 +00001292 vma->vm_flags |= VM_MAPPED_COPY;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001293 else {
David Howells8feae132009-01-08 12:04:47 +00001294 ret = do_mmap_shared_file(vma);
1295 if (ret < 0) {
1296 vma->vm_region = NULL;
1297 vma->vm_start = 0;
1298 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001299 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001300 pregion = NULL;
1301 goto error_just_free;
1302 }
1303 }
1304 fput(region->vm_file);
1305 kmem_cache_free(vm_region_jar, region);
1306 region = pregion;
1307 result = start;
1308 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* obtain the address at which to make a shared mapping
1312 * - this is the hook for quasi-memory character devices to
1313 * tell us the location of a shared mapping
1314 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001315 if (capabilities & NOMMU_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 addr = file->f_op->get_unmapped_area(file, addr, len,
1317 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001318 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001320 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001321 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /* the driver refused to tell us where to site
1324 * the mapping so we'll have to attempt to copy
1325 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001326 ret = -ENODEV;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001327 if (!(capabilities & NOMMU_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001328 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001330 capabilities &= ~NOMMU_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001331 } else {
1332 vma->vm_start = region->vm_start = addr;
1333 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 }
1336 }
1337
David Howells8feae132009-01-08 12:04:47 +00001338 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
David Howells645d83c2009-09-24 15:13:10 +01001340 /* set up the mapping
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001341 * - the region is filled in if NOMMU_MAP_DIRECT is still set
David Howells645d83c2009-09-24 15:13:10 +01001342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001344 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 else
David Howells645d83c2009-09-24 15:13:10 +01001346 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001348 goto error_just_free;
1349 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001350
Jie Zhangea637632009-12-14 18:00:02 -08001351 /* clear anonymous mappings that don't ask for uninitialized data */
1352 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1353 memset((void *)region->vm_start, 0,
1354 region->vm_end - region->vm_start);
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001357 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 current->mm->total_vm += len >> PAGE_SHIFT;
1360
David Howells8feae132009-01-08 12:04:47 +00001361share:
1362 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Mike Frysingercfe79c02010-01-06 17:23:23 +00001364 /* we flush the region from the icache only when the first executable
1365 * mapping of it is made */
1366 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1367 flush_icache_range(region->vm_start, region->vm_end);
1368 region->vm_icache_flushed = true;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Mike Frysingercfe79c02010-01-06 17:23:23 +00001371 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
David Howells8feae132009-01-08 12:04:47 +00001373 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David Howells8feae132009-01-08 12:04:47 +00001375error_just_free:
1376 up_write(&nommu_region_sem);
1377error:
David Howells89a86402009-10-30 13:13:26 +00001378 if (region->vm_file)
1379 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001380 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001381 if (vma->vm_file)
1382 fput(vma->vm_file);
Linus Torvalds3928d4f2018-07-21 13:48:51 -07001383 vm_area_free(vma);
David Howells8feae132009-01-08 12:04:47 +00001384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
David Howells8feae132009-01-08 12:04:47 +00001386sharing_violation:
1387 up_write(&nommu_region_sem);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001388 pr_warn("Attempt to share mismatched mappings\n");
David Howells8feae132009-01-08 12:04:47 +00001389 ret = -EINVAL;
1390 goto error;
1391
1392error_getting_vma:
1393 kmem_cache_free(vm_region_jar, region);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001394 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1395 len, current->pid);
Michal Hocko9af744d2017-02-22 15:46:16 -08001396 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -ENOMEM;
1398
David Howells8feae132009-01-08 12:04:47 +00001399error_getting_region:
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001400 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1401 len, current->pid);
Michal Hocko9af744d2017-02-22 15:46:16 -08001402 show_free_areas(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -ENOMEM;
1404}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001405
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001406unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1407 unsigned long prot, unsigned long flags,
1408 unsigned long fd, unsigned long pgoff)
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001409{
1410 struct file *file = NULL;
1411 unsigned long retval = -EBADF;
1412
Al Viro120a7952010-10-30 02:54:44 -04001413 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001414 if (!(flags & MAP_ANONYMOUS)) {
1415 file = fget(fd);
1416 if (!file)
1417 goto out;
1418 }
1419
1420 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1421
Greg Ungererad1ed292012-06-04 14:29:59 +10001422 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001423
1424 if (file)
1425 fput(file);
1426out:
1427 return retval;
1428}
1429
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001430SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1431 unsigned long, prot, unsigned long, flags,
1432 unsigned long, fd, unsigned long, pgoff)
1433{
1434 return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1435}
1436
Christoph Hellwiga4679372010-03-10 15:21:15 -08001437#ifdef __ARCH_WANT_SYS_OLD_MMAP
1438struct mmap_arg_struct {
1439 unsigned long addr;
1440 unsigned long len;
1441 unsigned long prot;
1442 unsigned long flags;
1443 unsigned long fd;
1444 unsigned long offset;
1445};
1446
1447SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1448{
1449 struct mmap_arg_struct a;
1450
1451 if (copy_from_user(&a, arg, sizeof(a)))
1452 return -EFAULT;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001453 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001454 return -EINVAL;
1455
Dominik Brodowskia90f5902018-03-11 11:34:46 +01001456 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1457 a.offset >> PAGE_SHIFT);
Christoph Hellwiga4679372010-03-10 15:21:15 -08001458}
1459#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/*
David Howells8feae132009-01-08 12:04:47 +00001462 * split a vma into two pieces at address 'addr', a new vma is allocated either
1463 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
David Howells8feae132009-01-08 12:04:47 +00001465int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1466 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
David Howells8feae132009-01-08 12:04:47 +00001468 struct vm_area_struct *new;
1469 struct vm_region *region;
1470 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
David Howells779c1022010-01-15 17:01:34 -08001472 /* we're only permitted to split anonymous regions (these should have
1473 * only a single usage on the region) */
1474 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001475 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
David Howells8feae132009-01-08 12:04:47 +00001477 if (mm->map_count >= sysctl_max_map_count)
1478 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
David Howells8feae132009-01-08 12:04:47 +00001480 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1481 if (!region)
1482 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Linus Torvalds3928d4f2018-07-21 13:48:51 -07001484 new = vm_area_dup(vma);
David Howells8feae132009-01-08 12:04:47 +00001485 if (!new) {
1486 kmem_cache_free(vm_region_jar, region);
1487 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
David Howells8feae132009-01-08 12:04:47 +00001489
1490 /* most fields are the same, copy all, and then fixup */
David Howells8feae132009-01-08 12:04:47 +00001491 *region = *vma->vm_region;
1492 new->vm_region = region;
1493
1494 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1495
1496 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001497 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001498 } else {
1499 region->vm_start = new->vm_start = addr;
1500 region->vm_pgoff = new->vm_pgoff += npages;
1501 }
1502
1503 if (new->vm_ops && new->vm_ops->open)
1504 new->vm_ops->open(new);
1505
1506 delete_vma_from_mm(vma);
1507 down_write(&nommu_region_sem);
1508 delete_nommu_region(vma->vm_region);
1509 if (new_below) {
1510 vma->vm_region->vm_start = vma->vm_start = addr;
1511 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1512 } else {
1513 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001514 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001515 }
1516 add_nommu_region(vma->vm_region);
1517 add_nommu_region(new->vm_region);
1518 up_write(&nommu_region_sem);
1519 add_vma_to_mm(mm, vma);
1520 add_vma_to_mm(mm, new);
1521 return 0;
1522}
1523
1524/*
1525 * shrink a VMA by removing the specified chunk from either the beginning or
1526 * the end
1527 */
1528static int shrink_vma(struct mm_struct *mm,
1529 struct vm_area_struct *vma,
1530 unsigned long from, unsigned long to)
1531{
1532 struct vm_region *region;
1533
David Howells8feae132009-01-08 12:04:47 +00001534 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1535 * and list */
1536 delete_vma_from_mm(vma);
1537 if (from > vma->vm_start)
1538 vma->vm_end = from;
1539 else
1540 vma->vm_start = to;
1541 add_vma_to_mm(mm, vma);
1542
1543 /* cut the backing region down to size */
1544 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001545 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001546
1547 down_write(&nommu_region_sem);
1548 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001549 if (from > region->vm_start) {
1550 to = region->vm_top;
1551 region->vm_top = region->vm_end = from;
1552 } else {
David Howells8feae132009-01-08 12:04:47 +00001553 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001554 }
David Howells8feae132009-01-08 12:04:47 +00001555 add_nommu_region(region);
1556 up_write(&nommu_region_sem);
1557
1558 free_page_series(from, to);
1559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
David Howells30340972006-09-27 01:50:20 -07001562/*
1563 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001564 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1565 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001566 */
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001567int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
David Howells8feae132009-01-08 12:04:47 +00001569 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001570 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001571 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Bob Liuf67d9b12011-05-24 17:12:56 -07001573 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001574 if (len == 0)
1575 return -EINVAL;
1576
Bob Liuf67d9b12011-05-24 17:12:56 -07001577 end = start + len;
1578
David Howells8feae132009-01-08 12:04:47 +00001579 /* find the first potentially overlapping VMA */
1580 vma = find_vma(mm, start);
1581 if (!vma) {
Choi Gi-yongac714902014-04-07 15:37:36 -07001582 static int limit;
David Howells33e5d7692009-04-02 16:56:32 -07001583 if (limit < 5) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001584 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1585 current->pid, current->comm,
1586 start, start + len - 1);
David Howells33e5d7692009-04-02 16:56:32 -07001587 limit++;
1588 }
David Howells8feae132009-01-08 12:04:47 +00001589 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
David Howells8feae132009-01-08 12:04:47 +00001592 /* we're allowed to split an anonymous VMA but not a file-backed one */
1593 if (vma->vm_file) {
1594 do {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001595 if (start > vma->vm_start)
David Howells8feae132009-01-08 12:04:47 +00001596 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001597 if (end == vma->vm_end)
1598 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001599 vma = vma->vm_next;
1600 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001601 return -EINVAL;
1602 } else {
1603 /* the chunk must be a subset of the VMA found */
1604 if (start == vma->vm_start && end == vma->vm_end)
1605 goto erase_whole_vma;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001606 if (start < vma->vm_start || end > vma->vm_end)
David Howells8feae132009-01-08 12:04:47 +00001607 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001608 if (offset_in_page(start))
David Howells8feae132009-01-08 12:04:47 +00001609 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001610 if (end != vma->vm_end && offset_in_page(end))
David Howells8feae132009-01-08 12:04:47 +00001611 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001612 if (start != vma->vm_start && end != vma->vm_end) {
1613 ret = split_vma(mm, vma, start, 1);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001614 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001615 return ret;
David Howells8feae132009-01-08 12:04:47 +00001616 }
1617 return shrink_vma(mm, vma, start, end);
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
David Howells8feae132009-01-08 12:04:47 +00001620erase_whole_vma:
1621 delete_vma_from_mm(vma);
1622 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624}
Paul Mundtb5073172007-07-21 04:37:25 -07001625EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Al Virobfce2812012-04-20 21:57:04 -04001627int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001628{
Al Virobfce2812012-04-20 21:57:04 -04001629 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001630 int ret;
David Howells30340972006-09-27 01:50:20 -07001631
1632 down_write(&mm->mmap_sem);
Mike Rapoport897ab3e2017-02-24 14:58:22 -08001633 ret = do_munmap(mm, addr, len, NULL);
David Howells30340972006-09-27 01:50:20 -07001634 up_write(&mm->mmap_sem);
1635 return ret;
1636}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001637EXPORT_SYMBOL(vm_munmap);
1638
1639SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1640{
Al Virobfce2812012-04-20 21:57:04 -04001641 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001642}
David Howells30340972006-09-27 01:50:20 -07001643
1644/*
David Howells8feae132009-01-08 12:04:47 +00001645 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001646 */
David Howells8feae132009-01-08 12:04:47 +00001647void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
David Howells8feae132009-01-08 12:04:47 +00001649 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
David Howells8feae132009-01-08 12:04:47 +00001651 if (!mm)
1652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
David Howells8feae132009-01-08 12:04:47 +00001654 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
David Howells8feae132009-01-08 12:04:47 +00001656 while ((vma = mm->mmap)) {
1657 mm->mmap = vma->vm_next;
1658 delete_vma_from_mm(vma);
1659 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001660 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662}
1663
Linus Torvalds5d22fc22016-05-27 15:57:31 -07001664int vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 return -ENOMEM;
1667}
1668
1669/*
David Howells6fa5f802006-09-27 01:50:21 -07001670 * expand (or shrink) an existing mapping, potentially moving it at the same
1671 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 *
David Howells6fa5f802006-09-27 01:50:21 -07001673 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001674 * as long as it stays within the region allocated by do_mmap_private() and the
1675 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 *
David Howells6fa5f802006-09-27 01:50:21 -07001677 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
Al Viro4b377ba2013-03-04 10:47:59 -05001679static unsigned long do_mremap(unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 unsigned long old_len, unsigned long new_len,
1681 unsigned long flags, unsigned long new_addr)
1682{
David Howells6fa5f802006-09-27 01:50:21 -07001683 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001686 old_len = PAGE_ALIGN(old_len);
1687 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001688 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return (unsigned long) -EINVAL;
1690
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001691 if (offset_in_page(addr))
David Howells8feae132009-01-08 12:04:47 +00001692 return -EINVAL;
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (flags & MREMAP_FIXED && new_addr != addr)
1695 return (unsigned long) -EINVAL;
1696
David Howells8feae132009-01-08 12:04:47 +00001697 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001698 if (!vma)
1699 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
David Howells6fa5f802006-09-27 01:50:21 -07001701 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return (unsigned long) -EFAULT;
1703
David Howells6fa5f802006-09-27 01:50:21 -07001704 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return (unsigned long) -EPERM;
1706
David Howells8feae132009-01-08 12:04:47 +00001707 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return (unsigned long) -ENOMEM;
1709
1710 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001711 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001712 return vma->vm_start;
1713}
1714
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001715SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1716 unsigned long, new_len, unsigned long, flags,
1717 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001718{
1719 unsigned long ret;
1720
1721 down_write(&current->mm->mmap_sem);
1722 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1723 up_write(&current->mm->mmap_sem);
1724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Keith Buschdf06b372018-10-26 15:10:28 -07001727struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1728 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 return NULL;
1731}
1732
Bob Liu8f3b1322011-07-08 15:39:46 -07001733int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1734 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Bob Liu8f3b1322011-07-08 15:39:46 -07001736 if (addr != (pfn << PAGE_SHIFT))
1737 return -EINVAL;
1738
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001739 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
Luke Yang22c4af42006-07-14 00:24:09 -07001742EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001744int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1745{
1746 unsigned long pfn = start >> PAGE_SHIFT;
1747 unsigned long vm_len = vma->vm_end - vma->vm_start;
1748
1749 pfn += vma->vm_pgoff;
1750 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1751}
1752EXPORT_SYMBOL(vm_iomap_memory);
1753
Paul Mundtf905bc42008-02-04 22:29:59 -08001754int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1755 unsigned long pgoff)
1756{
1757 unsigned int size = vma->vm_end - vma->vm_start;
1758
1759 if (!(vma->vm_flags & VM_USERMAP))
1760 return -EINVAL;
1761
1762 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1763 vma->vm_end = vma->vm_start + size;
1764
1765 return 0;
1766}
1767EXPORT_SYMBOL(remap_vmalloc_range);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1770 unsigned long len, unsigned long pgoff, unsigned long flags)
1771{
1772 return -ENOMEM;
1773}
1774
Souptick Joarder2bcd6452018-06-07 17:08:00 -07001775vm_fault_t filemap_fault(struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001776{
1777 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001778 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001779}
Paul Mundtb5073172007-07-21 04:37:25 -07001780EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001781
Jan Kara82b0f8c2016-12-14 15:06:58 -08001782void filemap_map_pages(struct vm_fault *vmf,
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001783 pgoff_t start_pgoff, pgoff_t end_pgoff)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07001784{
1785 BUG();
1786}
1787EXPORT_SYMBOL(filemap_map_pages);
1788
Eric W. Biederman84d77d32016-11-22 12:06:50 -06001789int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01001790 unsigned long addr, void *buf, int len, unsigned int gup_flags)
David Howells0ec76a12006-09-27 01:50:15 -07001791{
David Howells0ec76a12006-09-27 01:50:15 -07001792 struct vm_area_struct *vma;
Lorenzo Stoakes442486e2016-10-13 01:20:18 +01001793 int write = gup_flags & FOLL_WRITE;
David Howells0ec76a12006-09-27 01:50:15 -07001794
1795 down_read(&mm->mmap_sem);
1796
1797 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001798 vma = find_vma(mm, addr);
1799 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001800 /* don't overrun this mapping */
1801 if (addr + len >= vma->vm_end)
1802 len = vma->vm_end - addr;
1803
1804 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001805 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001806 copy_to_user_page(vma, NULL, addr,
1807 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001808 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001809 copy_from_user_page(vma, NULL, addr,
1810 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001811 else
1812 len = 0;
1813 } else {
1814 len = 0;
1815 }
1816
1817 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001818
1819 return len;
1820}
1821
1822/**
Mike Rapoportb7701a52018-02-06 15:42:13 -08001823 * access_remote_vm - access another process' address space
Mike Frysingerf55f1992011-03-29 14:05:12 +01001824 * @mm: the mm_struct of the target address space
1825 * @addr: start address to access
1826 * @buf: source or destination buffer
1827 * @len: number of bytes to transfer
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001828 * @gup_flags: flags modifying lookup behaviour
Mike Frysingerf55f1992011-03-29 14:05:12 +01001829 *
1830 * The caller must hold a reference on @mm.
1831 */
1832int access_remote_vm(struct mm_struct *mm, unsigned long addr,
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001833 void *buf, int len, unsigned int gup_flags)
Mike Frysingerf55f1992011-03-29 14:05:12 +01001834{
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +01001835 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001836}
1837
1838/*
1839 * Access another process' address space.
1840 * - source/target buffer must be kernel space
1841 */
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001842int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1843 unsigned int gup_flags)
Mike Frysingerf55f1992011-03-29 14:05:12 +01001844{
1845 struct mm_struct *mm;
1846
1847 if (addr + len < addr)
1848 return 0;
1849
1850 mm = get_task_mm(tsk);
1851 if (!mm)
1852 return 0;
1853
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001854 len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001855
David Howells0ec76a12006-09-27 01:50:15 -07001856 mmput(mm);
1857 return len;
1858}
Catalin Marinasfcd35852016-11-01 14:43:25 -07001859EXPORT_SYMBOL_GPL(access_process_vm);
David Howells7e660872010-01-15 17:01:39 -08001860
1861/**
1862 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1863 * @inode: The inode to check
1864 * @size: The current filesize of the inode
1865 * @newsize: The proposed filesize of the inode
1866 *
1867 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1868 * make sure that that any outstanding VMAs aren't broken and then shrink the
1869 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1870 * automatically grant mappings that are too large.
1871 */
1872int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1873 size_t newsize)
1874{
1875 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08001876 struct vm_region *region;
1877 pgoff_t low, high;
1878 size_t r_size, r_top;
1879
1880 low = newsize >> PAGE_SHIFT;
1881 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1882
1883 down_write(&nommu_region_sem);
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001884 i_mmap_lock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001885
1886 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07001887 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08001888 /* found one - only interested if it's shared out of the page
1889 * cache */
1890 if (vma->vm_flags & VM_SHARED) {
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001891 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001892 up_write(&nommu_region_sem);
1893 return -ETXTBSY; /* not quite true, but near enough */
1894 }
1895 }
1896
1897 /* reduce any regions that overlap the dead zone - if in existence,
1898 * these will be pointed to by VMAs that don't overlap the dead zone
1899 *
1900 * we don't check for any regions that start beyond the EOF as there
1901 * shouldn't be any
1902 */
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001903 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08001904 if (!(vma->vm_flags & VM_SHARED))
1905 continue;
1906
1907 region = vma->vm_region;
1908 r_size = region->vm_top - region->vm_start;
1909 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1910
1911 if (r_top > newsize) {
1912 region->vm_top -= r_top - newsize;
1913 if (region->vm_end > region->vm_top)
1914 region->vm_end = region->vm_top;
1915 }
1916 }
1917
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08001918 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08001919 up_write(&nommu_region_sem);
1920 return 0;
1921}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001922
1923/*
1924 * Initialise sysctl_user_reserve_kbytes.
1925 *
1926 * This is intended to prevent a user from starting a single memory hogging
1927 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1928 * mode.
1929 *
1930 * The default value is min(3% of free memory, 128MB)
1931 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1932 */
1933static int __meminit init_user_reserve(void)
1934{
1935 unsigned long free_kbytes;
1936
Michal Hockoc41f0122017-09-06 16:23:36 -07001937 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001938
1939 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1940 return 0;
1941}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04001942subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001943
1944/*
1945 * Initialise sysctl_admin_reserve_kbytes.
1946 *
1947 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1948 * to log in and kill a memory hogging process.
1949 *
1950 * Systems with more than 256MB will reserve 8MB, enough to recover
1951 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1952 * only reserve 3% of free pages by default.
1953 */
1954static int __meminit init_admin_reserve(void)
1955{
1956 unsigned long free_kbytes;
1957
Michal Hockoc41f0122017-09-06 16:23:36 -07001958 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001959
1960 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
1961 return 0;
1962}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04001963subsys_initcall(init_admin_reserve);