blob: 5ba39b82f241e84dac496729dc7e00c075093f36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/nommu.c
3 *
4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
6 *
7 * See Documentation/nommu-mmap.txt
8 *
David Howells8feae132009-01-08 12:04:47 +00009 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
Paul Mundt29c185e2010-12-24 12:08:30 +090013 * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Dave Hansencde70142016-02-12 13:01:55 -080018#define __DISABLE_GUP_DEPRECATED
19
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040020#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/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>
Clark Williamscf4aebc22013-02-07 09:46:59 -060038#include <linux/sched/sysctl.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070039#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/uaccess.h>
42#include <asm/tlb.h>
43#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070044#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000045#include "internal.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047void *high_memory;
Arnd Bergmann944b6872015-02-05 12:25:12 -080048EXPORT_SYMBOL(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct page *mem_map;
50unsigned long max_mapnr;
gchen gchen5b8bf302015-03-12 16:26:05 -070051EXPORT_SYMBOL(max_mapnr);
Hugh Dickins4266c972009-09-23 17:05:53 +010052unsigned long highest_memmap_pfn;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070053struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
55int sysctl_overcommit_ratio = 50; /* default is 50% */
Jerome Marchand49f0ce52014-01-21 15:49:14 -080056unsigned long sysctl_overcommit_kbytes __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070058int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -070059unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -070060unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int heap_stack_gap = 0;
62
David Howells33e5d7692009-04-02 16:56:32 -070063atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000064
K. Y. Srinivasan997071b2012-11-15 14:34:42 -080065/*
66 * The global memory commitment made in the system can be a metric
67 * that can be used to drive ballooning decisions when Linux is hosted
68 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
69 * balancing memory across competing virtual machines that are hosted.
70 * Several metrics drive this policy engine including the guest reported
71 * memory commitment.
72 */
73unsigned long vm_memory_committed(void)
74{
75 return percpu_counter_read_positive(&vm_committed_as);
76}
77
78EXPORT_SYMBOL_GPL(vm_memory_committed);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080EXPORT_SYMBOL(mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Howells8feae132009-01-08 12:04:47 +000082/* list of mapped, potentially shareable regions */
83static struct kmem_cache *vm_region_jar;
84struct rb_root nommu_region_tree = RB_ROOT;
85DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040087const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Return the total memory allocated for this pointer, not
92 * just what the caller asked for.
93 *
94 * Doesn't have to be accurate, i.e. may have races.
95 */
96unsigned int kobjsize(const void *objp)
97{
98 struct page *page;
99
Michael Hennerich4016a132008-04-28 02:13:38 -0700100 /*
101 * If the object we have should not have ksize performed on it,
102 * return size of 0
103 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900104 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700105 return 0;
106
107 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700108
109 /*
110 * If the allocator sets PageSlab, we know the pointer came from
111 * kmalloc().
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (PageSlab(page))
114 return ksize(objp);
115
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700116 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000117 * If it's not a compound page, see if we have a matching VMA
118 * region. This test is intentionally done in reverse order,
119 * so if there's no VMA, we still fall through and hand back
120 * PAGE_SIZE for 0-order pages.
121 */
122 if (!PageCompound(page)) {
123 struct vm_area_struct *vma;
124
125 vma = find_vma(current->mm, (unsigned long)objp);
126 if (vma)
127 return vma->vm_end - vma->vm_start;
128 }
129
130 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700131 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900132 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700133 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900134 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Michel Lespinasse28a35712013-02-22 16:35:55 -0800137long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
138 unsigned long start, unsigned long nr_pages,
139 unsigned int foll_flags, struct page **pages,
140 struct vm_area_struct **vmas, int *nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700142 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700143 unsigned long vm_flags;
144 int i;
145
146 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700147 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700148 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700149 vm_flags = (foll_flags & FOLL_WRITE) ?
150 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
151 vm_flags &= (foll_flags & FOLL_FORCE) ?
152 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Peter Zijlstra9d737772009-06-25 11:58:55 +0200154 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000155 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700156 if (!vma)
157 goto finish_or_fault;
158
159 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700160 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
161 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700162 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (pages) {
165 pages[i] = virt_to_page(start);
166 if (pages[i])
167 page_cache_get(pages[i]);
168 }
169 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700170 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000171 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
David Howells7b4d5b82006-09-27 01:50:18 -0700173
174 return i;
175
176finish_or_fault:
177 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
Nick Pigginb291f002008-10-18 20:26:44 -0700179
Nick Pigginb291f002008-10-18 20:26:44 -0700180/*
181 * get a list of pages in an address range belonging to the specified process
182 * and indicate the VMA that covers each page
183 * - this is potentially dodgy as we may end incrementing the page count of a
184 * slab page or a secondary page from a compound page
185 * - don't permit access to VMAs that don't support it, such as I/O mappings
186 */
Dave Hansencde70142016-02-12 13:01:55 -0800187long get_user_pages6(unsigned long start, unsigned long nr_pages,
Michel Lespinasse28a35712013-02-22 16:35:55 -0800188 int write, int force, struct page **pages,
189 struct vm_area_struct **vmas)
Nick Pigginb291f002008-10-18 20:26:44 -0700190{
191 int flags = 0;
192
193 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700194 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700195 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700196 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700197
Dave Hansencde70142016-02-12 13:01:55 -0800198 return __get_user_pages(current, current->mm, start, nr_pages, flags,
199 pages, vmas, NULL);
Nick Pigginb291f002008-10-18 20:26:44 -0700200}
Dave Hansencde70142016-02-12 13:01:55 -0800201EXPORT_SYMBOL(get_user_pages6);
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000202
Dave Hansencde70142016-02-12 13:01:55 -0800203long get_user_pages_locked6(unsigned long start, unsigned long nr_pages,
204 int write, int force, struct page **pages,
205 int *locked)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800206{
Dave Hansencde70142016-02-12 13:01:55 -0800207 return get_user_pages6(start, nr_pages, write, force, pages, NULL);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800208}
Dave Hansencde70142016-02-12 13:01:55 -0800209EXPORT_SYMBOL(get_user_pages_locked6);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800210
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800211long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
212 unsigned long start, unsigned long nr_pages,
213 int write, int force, struct page **pages,
214 unsigned int gup_flags)
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800215{
216 long ret;
217 down_read(&mm->mmap_sem);
Dave Hansencde70142016-02-12 13:01:55 -0800218 ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages,
219 NULL, NULL);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800220 up_read(&mm->mmap_sem);
221 return ret;
222}
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800223EXPORT_SYMBOL(__get_user_pages_unlocked);
224
Dave Hansencde70142016-02-12 13:01:55 -0800225long get_user_pages_unlocked5(unsigned long start, unsigned long nr_pages,
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800226 int write, int force, struct page **pages)
227{
Dave Hansencde70142016-02-12 13:01:55 -0800228 return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
229 write, force, pages, 0);
Andrea Arcangeli0fd71a52015-02-11 15:27:20 -0800230}
Dave Hansencde70142016-02-12 13:01:55 -0800231EXPORT_SYMBOL(get_user_pages_unlocked5);
Andrea Arcangelif0818f42015-02-11 15:27:17 -0800232
Paul Mundtdfc2f912009-06-26 04:31:57 +0900233/**
234 * follow_pfn - look up PFN at a user virtual address
235 * @vma: memory mapping
236 * @address: user virtual address
237 * @pfn: location to store found PFN
238 *
239 * Only IO mappings and raw PFN mappings are allowed.
240 *
241 * Returns zero and the pfn at @pfn on success, -ve otherwise.
242 */
243int follow_pfn(struct vm_area_struct *vma, unsigned long address,
244 unsigned long *pfn)
245{
246 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
247 return -EINVAL;
248
249 *pfn = address >> PAGE_SHIFT;
250 return 0;
251}
252EXPORT_SYMBOL(follow_pfn);
253
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700254LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800256void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 kfree(addr);
259}
Paul Mundtb5073172007-07-21 04:37:25 -0700260EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Al Virodd0fc662005-10-07 07:46:04 +0100262void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200265 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
266 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Nick Piggin84097512006-03-22 00:08:34 -0800268 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Paul Mundtb5073172007-07-21 04:37:25 -0700270EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Paul Mundtf905bc42008-02-04 22:29:59 -0800272void *vmalloc_user(unsigned long size)
273{
274 void *ret;
275
276 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
277 PAGE_KERNEL);
278 if (ret) {
279 struct vm_area_struct *vma;
280
281 down_write(&current->mm->mmap_sem);
282 vma = find_vma(current->mm, (unsigned long)ret);
283 if (vma)
284 vma->vm_flags |= VM_USERMAP;
285 up_write(&current->mm->mmap_sem);
286 }
287
288 return ret;
289}
290EXPORT_SYMBOL(vmalloc_user);
291
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800292struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 return virt_to_page(addr);
295}
Paul Mundtb5073172007-07-21 04:37:25 -0700296EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800298unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 return page_to_pfn(virt_to_page(addr));
301}
Paul Mundtb5073172007-07-21 04:37:25 -0700302EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304long vread(char *buf, char *addr, unsigned long count)
305{
Chen Gang9bde9162013-07-03 15:02:36 -0700306 /* Don't allow overflow */
307 if ((unsigned long) buf + count < count)
308 count = -(unsigned long) buf;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 memcpy(buf, addr, count);
311 return count;
312}
313
314long vwrite(char *buf, char *addr, unsigned long count)
315{
316 /* Don't allow overflow */
317 if ((unsigned long) addr + count < count)
318 count = -(unsigned long) addr;
319
320 memcpy(addr, buf, count);
Choi Gi-yongac714902014-04-07 15:37:36 -0700321 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900325 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
327 * @size: allocation size
328 *
329 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900330 * allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200332 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * use __vmalloc() instead.
334 */
335void *vmalloc(unsigned long size)
336{
337 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
338}
Andrew Mortonf6138882006-02-28 16:59:18 -0800339EXPORT_SYMBOL(vmalloc);
340
Dave Younge1ca7782010-10-26 14:22:06 -0700341/*
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900342 * vzalloc - allocate virtually contiguous memory with zero fill
Dave Younge1ca7782010-10-26 14:22:06 -0700343 *
344 * @size: allocation size
345 *
346 * Allocate enough pages to cover @size from the page level
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900347 * allocator and map them into contiguous kernel virtual space.
Dave Younge1ca7782010-10-26 14:22:06 -0700348 * The memory allocated is set to zero.
349 *
350 * For tight control over page level allocator and protection flags
351 * use __vmalloc() instead.
352 */
353void *vzalloc(unsigned long size)
354{
355 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
356 PAGE_KERNEL);
357}
358EXPORT_SYMBOL(vzalloc);
359
360/**
361 * vmalloc_node - allocate memory on a specific node
362 * @size: allocation size
363 * @node: numa node
364 *
365 * Allocate enough pages to cover @size from the page level
366 * allocator and map them into contiguous kernel virtual space.
367 *
368 * For tight control over page level allocator and protection flags
369 * use __vmalloc() instead.
370 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800371void *vmalloc_node(unsigned long size, int node)
372{
373 return vmalloc(size);
374}
Paul Mundt9a14f652010-12-24 11:50:34 +0900375EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700376
377/**
378 * vzalloc_node - allocate memory on a specific node with zero fill
379 * @size: allocation size
380 * @node: numa node
381 *
382 * Allocate enough pages to cover @size from the page level
383 * allocator and map them into contiguous kernel virtual space.
384 * The memory allocated is set to zero.
385 *
386 * For tight control over page level allocator and protection flags
387 * use __vmalloc() instead.
388 */
389void *vzalloc_node(unsigned long size, int node)
390{
391 return vzalloc(size);
392}
393EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Paul Mundt1af446e2008-08-04 16:01:47 +0900395#ifndef PAGE_KERNEL_EXEC
396# define PAGE_KERNEL_EXEC PAGE_KERNEL
397#endif
398
399/**
400 * vmalloc_exec - allocate virtually contiguous, executable memory
401 * @size: allocation size
402 *
403 * Kernel-internal function to allocate enough pages to cover @size
404 * the page level allocator and map them into contiguous and
405 * executable kernel virtual space.
406 *
407 * For tight control over page level allocator and protection flags
408 * use __vmalloc() instead.
409 */
410
411void *vmalloc_exec(unsigned long size)
412{
413 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
414}
415
Paul Mundtb5073172007-07-21 04:37:25 -0700416/**
417 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * @size: allocation size
419 *
420 * Allocate enough 32bit PA addressable pages to cover @size from the
Masahiro Yamadae1c05062015-07-07 10:14:59 +0900421 * page level allocator and map them into contiguous kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
423void *vmalloc_32(unsigned long size)
424{
425 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
426}
Paul Mundtb5073172007-07-21 04:37:25 -0700427EXPORT_SYMBOL(vmalloc_32);
428
429/**
430 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
431 * @size: allocation size
432 *
433 * The resulting memory area is 32bit addressable and zeroed so it can be
434 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800435 *
436 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
437 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700438 */
439void *vmalloc_32_user(unsigned long size)
440{
Paul Mundtf905bc42008-02-04 22:29:59 -0800441 /*
442 * We'll have to sort out the ZONE_DMA bits for 64-bit,
443 * but for now this can simply use vmalloc_user() directly.
444 */
445 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700446}
447EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
450{
451 BUG();
452 return NULL;
453}
Paul Mundtb5073172007-07-21 04:37:25 -0700454EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800456void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 BUG();
459}
Paul Mundtb5073172007-07-21 04:37:25 -0700460EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Paul Mundteb6434d2009-01-21 17:45:47 +0900462void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
463{
464 BUG();
465 return NULL;
466}
467EXPORT_SYMBOL(vm_map_ram);
468
469void vm_unmap_ram(const void *mem, unsigned int count)
470{
471 BUG();
472}
473EXPORT_SYMBOL(vm_unmap_ram);
474
475void vm_unmap_aliases(void)
476{
477}
478EXPORT_SYMBOL_GPL(vm_unmap_aliases);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700481 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
482 * have one.
483 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700484void __weak vmalloc_sync_all(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700485{
486}
487
Paul Mundt29c185e2010-12-24 12:08:30 +0900488/**
489 * alloc_vm_area - allocate a range of kernel address space
490 * @size: size of the area
491 *
492 * Returns: NULL on failure, vm_struct on success
493 *
494 * This function reserves a range of kernel address space, and
495 * allocates pagetables to map that range. No actual mappings
496 * are created. If the kernel address space is not shared
497 * between processes, it syncs the pagetable across all
498 * processes.
499 */
David Vrabelcd129092011-09-29 16:53:32 +0100500struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900501{
502 BUG();
503 return NULL;
504}
505EXPORT_SYMBOL_GPL(alloc_vm_area);
506
507void free_vm_area(struct vm_struct *area)
508{
509 BUG();
510}
511EXPORT_SYMBOL_GPL(free_vm_area);
512
Paul Mundtb5073172007-07-21 04:37:25 -0700513int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
514 struct page *page)
515{
516 return -EINVAL;
517}
518EXPORT_SYMBOL(vm_insert_page);
519
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700520/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * sys_brk() for the most part doesn't need the global kernel
522 * lock, except when an application is doing something nasty
523 * like trying to un-brk an area that has already been mapped
524 * to a regular file. in this case, the unmapping will need
525 * to invoke file system routines that need the global lock.
526 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100527SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct mm_struct *mm = current->mm;
530
531 if (brk < mm->start_brk || brk > mm->context.end_brk)
532 return mm->brk;
533
534 if (mm->brk == brk)
535 return mm->brk;
536
537 /*
538 * Always allow shrinking brk
539 */
540 if (brk <= mm->brk) {
541 mm->brk = brk;
542 return brk;
543 }
544
545 /*
546 * Ok, looks good - let it rip.
547 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000548 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return mm->brk = brk;
550}
551
David Howells8feae132009-01-08 12:04:47 +0000552/*
553 * initialise the VMA and region record slabs
554 */
555void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700557 int ret;
558
Tejun Heo908c7f12014-09-08 09:51:29 +0900559 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700560 VM_BUG_ON(ret);
Vladimir Davydov5d097052016-01-14 15:18:21 -0800561 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
David Howells8feae132009-01-08 12:04:47 +0000562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
David Howells8feae132009-01-08 12:04:47 +0000564/*
565 * validate the region tree
566 * - the caller must hold the region lock
567 */
568#ifdef CONFIG_DEBUG_NOMMU_REGIONS
569static noinline void validate_nommu_regions(void)
570{
571 struct vm_region *region, *last;
572 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
David Howells8feae132009-01-08 12:04:47 +0000574 lastp = rb_first(&nommu_region_tree);
575 if (!lastp)
576 return;
577
578 last = rb_entry(lastp, struct vm_region, vm_rb);
Geliang Tangc9427bc2015-11-05 18:48:38 -0800579 BUG_ON(last->vm_end <= last->vm_start);
580 BUG_ON(last->vm_top < last->vm_end);
David Howells8feae132009-01-08 12:04:47 +0000581
582 while ((p = rb_next(lastp))) {
583 region = rb_entry(p, struct vm_region, vm_rb);
584 last = rb_entry(lastp, struct vm_region, vm_rb);
585
Geliang Tangc9427bc2015-11-05 18:48:38 -0800586 BUG_ON(region->vm_end <= region->vm_start);
587 BUG_ON(region->vm_top < region->vm_end);
588 BUG_ON(region->vm_start < last->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000589
590 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592}
David Howells8feae132009-01-08 12:04:47 +0000593#else
David Howells33e5d7692009-04-02 16:56:32 -0700594static void validate_nommu_regions(void)
595{
596}
David Howells8feae132009-01-08 12:04:47 +0000597#endif
598
599/*
600 * add a region into the global tree
601 */
602static void add_nommu_region(struct vm_region *region)
603{
604 struct vm_region *pregion;
605 struct rb_node **p, *parent;
606
607 validate_nommu_regions();
608
David Howells8feae132009-01-08 12:04:47 +0000609 parent = NULL;
610 p = &nommu_region_tree.rb_node;
611 while (*p) {
612 parent = *p;
613 pregion = rb_entry(parent, struct vm_region, vm_rb);
614 if (region->vm_start < pregion->vm_start)
615 p = &(*p)->rb_left;
616 else if (region->vm_start > pregion->vm_start)
617 p = &(*p)->rb_right;
618 else if (pregion == region)
619 return;
620 else
621 BUG();
622 }
623
624 rb_link_node(&region->vm_rb, parent, p);
625 rb_insert_color(&region->vm_rb, &nommu_region_tree);
626
627 validate_nommu_regions();
628}
629
630/*
631 * delete a region from the global tree
632 */
633static void delete_nommu_region(struct vm_region *region)
634{
635 BUG_ON(!nommu_region_tree.rb_node);
636
637 validate_nommu_regions();
638 rb_erase(&region->vm_rb, &nommu_region_tree);
639 validate_nommu_regions();
640}
641
642/*
643 * free a contiguous series of pages
644 */
645static void free_page_series(unsigned long from, unsigned long to)
646{
647 for (; from < to; from += PAGE_SIZE) {
648 struct page *page = virt_to_page(from);
649
David Howells33e5d7692009-04-02 16:56:32 -0700650 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000651 put_page(page);
652 }
653}
654
655/*
656 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700657 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000658 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000659 * will equal vm_start
660 */
661static void __put_nommu_region(struct vm_region *region)
662 __releases(nommu_region_sem)
663{
David Howells8feae132009-01-08 12:04:47 +0000664 BUG_ON(!nommu_region_tree.rb_node);
665
David Howells1e2ae592010-01-15 17:01:33 -0800666 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000667 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000668 delete_nommu_region(region);
669 up_write(&nommu_region_sem);
670
671 if (region->vm_file)
672 fput(region->vm_file);
673
674 /* IO memory and memory shared directly out of the pagecache
675 * from ramfs/tmpfs mustn't be released here */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700676 if (region->vm_flags & VM_MAPPED_COPY)
Paul Mundtdd8632a2009-01-08 12:04:47 +0000677 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000678 kmem_cache_free(vm_region_jar, region);
679 } else {
680 up_write(&nommu_region_sem);
681 }
682}
683
684/*
685 * release a reference to a region
686 */
687static void put_nommu_region(struct vm_region *region)
688{
689 down_write(&nommu_region_sem);
690 __put_nommu_region(region);
691}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
David Howells30340972006-09-27 01:50:20 -0700693/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700694 * update protection on a vma
695 */
696static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
697{
698#ifdef CONFIG_MPU
699 struct mm_struct *mm = vma->vm_mm;
700 long start = vma->vm_start & PAGE_MASK;
701 while (start < vma->vm_end) {
702 protect_page(mm, start, flags);
703 start += PAGE_SIZE;
704 }
705 update_protections(mm);
706#endif
707}
708
709/*
David Howells30340972006-09-27 01:50:20 -0700710 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000711 * and tree and add to the address space's page tree also if not an anonymous
712 * page
David Howells30340972006-09-27 01:50:20 -0700713 * - should be called with mm->mmap_sem held writelocked
714 */
David Howells8feae132009-01-08 12:04:47 +0000715static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700716{
Namhyung Kim6038def2011-05-24 17:11:22 -0700717 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000718 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700719 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700720
David Howells8feae132009-01-08 12:04:47 +0000721 BUG_ON(!vma->vm_region);
722
723 mm->map_count++;
724 vma->vm_mm = mm;
725
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700726 protect_vma(vma, vma->vm_flags);
727
David Howells8feae132009-01-08 12:04:47 +0000728 /* add the VMA to the mapping */
729 if (vma->vm_file) {
730 mapping = vma->vm_file->f_mapping;
731
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800732 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000733 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700734 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000735 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800736 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000737 }
738
739 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700740 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000741 p = &mm->mm_rb.rb_node;
742 while (*p) {
743 parent = *p;
744 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
745
746 /* sort by: start addr, end addr, VMA struct addr in that order
747 * (the latter is necessary as we may get identical VMAs) */
748 if (vma->vm_start < pvma->vm_start)
749 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700750 else if (vma->vm_start > pvma->vm_start) {
751 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000752 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700753 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000754 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700755 else if (vma->vm_end > pvma->vm_end) {
756 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000757 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700758 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000759 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700760 else if (vma > pvma) {
761 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000762 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700763 } else
David Howells8feae132009-01-08 12:04:47 +0000764 BUG();
765 }
766
767 rb_link_node(&vma->vm_rb, parent, p);
768 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
769
770 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700771 prev = NULL;
772 if (rb_prev)
773 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700774
Namhyung Kim6038def2011-05-24 17:11:22 -0700775 __vma_link_list(mm, vma, prev, parent);
David Howells8feae132009-01-08 12:04:47 +0000776}
777
778/*
779 * delete a VMA from its owning mm_struct and address space
780 */
781static void delete_vma_from_mm(struct vm_area_struct *vma)
782{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700783 int i;
David Howells8feae132009-01-08 12:04:47 +0000784 struct address_space *mapping;
785 struct mm_struct *mm = vma->vm_mm;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700786 struct task_struct *curr = current;
David Howells8feae132009-01-08 12:04:47 +0000787
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700788 protect_vma(vma, 0);
789
David Howells8feae132009-01-08 12:04:47 +0000790 mm->map_count--;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700791 for (i = 0; i < VMACACHE_SIZE; i++) {
792 /* if the vma is cached, invalidate the entire cache */
793 if (curr->vmacache[i] == vma) {
Steven Miaoe020d5b2014-06-23 13:22:02 -0700794 vmacache_invalidate(mm);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700795 break;
796 }
797 }
David Howells8feae132009-01-08 12:04:47 +0000798
799 /* remove the VMA from the mapping */
800 if (vma->vm_file) {
801 mapping = vma->vm_file->f_mapping;
802
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800803 i_mmap_lock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000804 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700805 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000806 flush_dcache_mmap_unlock(mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800807 i_mmap_unlock_write(mapping);
David Howells8feae132009-01-08 12:04:47 +0000808 }
809
810 /* remove from the MM's tree and list */
811 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700812
813 if (vma->vm_prev)
814 vma->vm_prev->vm_next = vma->vm_next;
815 else
816 mm->mmap = vma->vm_next;
817
818 if (vma->vm_next)
819 vma->vm_next->vm_prev = vma->vm_prev;
David Howells8feae132009-01-08 12:04:47 +0000820}
821
822/*
823 * destroy a VMA record
824 */
825static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
826{
David Howells8feae132009-01-08 12:04:47 +0000827 if (vma->vm_ops && vma->vm_ops->close)
828 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700829 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000830 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000831 put_nommu_region(vma->vm_region);
832 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700833}
834
835/*
836 * look up the first VMA in which addr resides, NULL if none
837 * - should be called with mm->mmap_sem at least held readlocked
838 */
839struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
840{
David Howells8feae132009-01-08 12:04:47 +0000841 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700842
David Howells8feae132009-01-08 12:04:47 +0000843 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700844 vma = vmacache_find(mm, addr);
845 if (likely(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 return NULL;
853 if (vma->vm_end > addr) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700854 vmacache_update(addr, vma);
David Howells8feae132009-01-08 12:04:47 +0000855 return vma;
856 }
David Howells30340972006-09-27 01:50:20 -0700857 }
858
David Howells30340972006-09-27 01:50:20 -0700859 return NULL;
860}
861EXPORT_SYMBOL(find_vma);
862
863/*
David Howells930e6522006-09-27 01:50:22 -0700864 * find a VMA
865 * - we don't extend stack VMAs under NOMMU conditions
866 */
867struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
868{
David Howells7561e8c2010-03-25 16:48:38 +0000869 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700870}
871
David Howells8feae132009-01-08 12:04:47 +0000872/*
873 * expand a stack to a given address
874 * - not supported under NOMMU conditions
875 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700876int expand_stack(struct vm_area_struct *vma, unsigned long address)
877{
878 return -ENOMEM;
879}
880
David Howells930e6522006-09-27 01:50:22 -0700881/*
David Howells6fa5f802006-09-27 01:50:21 -0700882 * look up the first VMA exactly that exactly matches addr
883 * - should be called with mm->mmap_sem at least held readlocked
884 */
David Howells8feae132009-01-08 12:04:47 +0000885static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
886 unsigned long addr,
887 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000890 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
David Howells8feae132009-01-08 12:04:47 +0000892 /* check the cache first */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700893 vma = vmacache_find_exact(mm, addr, end);
894 if (vma)
David Howells8feae132009-01-08 12:04:47 +0000895 return vma;
896
Namhyung Kime922c4c2011-05-24 17:11:24 -0700897 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000898 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700899 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000900 if (vma->vm_start < addr)
901 continue;
902 if (vma->vm_start > addr)
903 return NULL;
904 if (vma->vm_end == end) {
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700905 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return vma;
David Howells8feae132009-01-08 12:04:47 +0000907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
910 return NULL;
911}
912
David Howells30340972006-09-27 01:50:20 -0700913/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * determine whether a mapping should be permitted and, if so, what sort of
915 * mapping we're capable of supporting
916 */
917static int validate_mmap_request(struct file *file,
918 unsigned long addr,
919 unsigned long len,
920 unsigned long prot,
921 unsigned long flags,
922 unsigned long pgoff,
923 unsigned long *_capabilities)
924{
David Howells8feae132009-01-08 12:04:47 +0000925 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int ret;
927
928 /* do the simple checks first */
Leon Romanovsky22cc8772015-06-24 16:57:47 -0700929 if (flags & MAP_FIXED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
933 (flags & MAP_TYPE) != MAP_SHARED)
934 return -EINVAL;
935
Mike Frysingerf81cff02006-12-06 12:02:59 +1000936 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -EINVAL;
938
Mike Frysingerf81cff02006-12-06 12:02:59 +1000939 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000940 rlen = PAGE_ALIGN(len);
941 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000942 return -ENOMEM;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000945 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000946 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* files must support mmap */
Al Viro72c2d532013-09-22 16:27:52 -0400950 if (!file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -ENODEV;
952
953 /* work out if what we've got could possibly be shared
954 * - we support chardevs that provide their own "memory"
955 * - we support files/blockdevs that are memory backed
956 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100957 if (file->f_op->mmap_capabilities) {
958 capabilities = file->f_op->mmap_capabilities(file);
959 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* no explicit capabilities set, so assume some
961 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500962 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 case S_IFREG:
964 case S_IFBLK:
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100965 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
967
968 case S_IFCHR:
969 capabilities =
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100970 NOMMU_MAP_DIRECT |
971 NOMMU_MAP_READ |
972 NOMMU_MAP_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 break;
974
975 default:
976 return -EINVAL;
977 }
978 }
979
980 /* eliminate any capabilities that we can't support on this
981 * device */
982 if (!file->f_op->get_unmapped_area)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100983 capabilities &= ~NOMMU_MAP_DIRECT;
Al Viro6e242a12015-03-31 12:35:13 -0400984 if (!(file->f_mode & FMODE_CAN_READ))
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100985 capabilities &= ~NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Graff Yang28d7a6a2009-08-18 14:11:17 -0700987 /* The file shall have been opened with read permission. */
988 if (!(file->f_mode & FMODE_READ))
989 return -EACCES;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (flags & MAP_SHARED) {
992 /* do checks for writing, appending and locking */
993 if ((prot & PROT_WRITE) &&
994 !(file->f_mode & FMODE_WRITE))
995 return -EACCES;
996
Al Viro496ad9a2013-01-23 17:07:38 -0500997 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 (file->f_mode & FMODE_WRITE))
999 return -EACCES;
1000
Jeff Laytond7a06982014-03-10 09:54:15 -04001001 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return -EAGAIN;
1003
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001004 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -ENODEV;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /* we mustn't privatise shared mappings */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001008 capabilities &= ~NOMMU_MAP_COPY;
Choi Gi-yongac714902014-04-07 15:37:36 -07001009 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* we're going to read the file into private memory we
1011 * allocate */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001012 if (!(capabilities & NOMMU_MAP_COPY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -ENODEV;
1014
1015 /* we don't permit a private writable mapping to be
1016 * shared with the backing device */
1017 if (prot & PROT_WRITE)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001018 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001021 if (capabilities & NOMMU_MAP_DIRECT) {
1022 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
1023 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
1024 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001025 ) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001026 capabilities &= ~NOMMU_MAP_DIRECT;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001027 if (flags & MAP_SHARED) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001028 pr_warn("MAP_SHARED not completely supported on !MMU\n");
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001029 return -EINVAL;
1030 }
1031 }
1032 }
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* handle executable mappings and implied executable
1035 * mappings */
Eric W. Biederman90f85722015-06-29 14:42:03 -05001036 if (path_noexec(&file->f_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (prot & PROT_EXEC)
1038 return -EPERM;
Choi Gi-yongac714902014-04-07 15:37:36 -07001039 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* handle implication of PROT_EXEC by PROT_READ */
1041 if (current->personality & READ_IMPLIES_EXEC) {
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001042 if (capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 prot |= PROT_EXEC;
1044 }
Choi Gi-yongac714902014-04-07 15:37:36 -07001045 } else if ((prot & PROT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 (prot & PROT_EXEC) &&
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001047 !(capabilities & NOMMU_MAP_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ) {
1049 /* backing file is not executable, try to copy */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001050 capabilities &= ~NOMMU_MAP_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Choi Gi-yongac714902014-04-07 15:37:36 -07001052 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* anonymous mappings are always memory backed and can be
1054 * privately mapped
1055 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001056 capabilities = NOMMU_MAP_COPY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 /* handle PROT_EXEC implication by PROT_READ */
1059 if ((prot & PROT_READ) &&
1060 (current->personality & READ_IMPLIES_EXEC))
1061 prot |= PROT_EXEC;
1062 }
1063
1064 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -04001065 ret = security_mmap_addr(addr);
1066 if (ret < 0)
1067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* looks okay */
1070 *_capabilities = capabilities;
1071 return 0;
1072}
1073
1074/*
1075 * we've determined that we can make the mapping, now translate what we
1076 * now know into VMA flags
1077 */
1078static unsigned long determine_vm_flags(struct file *file,
1079 unsigned long prot,
1080 unsigned long flags,
1081 unsigned long capabilities)
1082{
1083 unsigned long vm_flags;
1084
Dave Hansene6bfb702016-02-12 13:02:31 -08001085 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* vm_flags |= mm->def_flags; */
1087
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001088 if (!(capabilities & NOMMU_MAP_DIRECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001090 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (file && !(prot & PROT_WRITE))
1092 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* overlay a shareable mapping on the backing device or inode
1095 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1096 * romfs/cramfs */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001097 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001099 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
1102 /* refuse to let anyone share private mappings with this process if
1103 * it's being traced - otherwise breakpoints set in it may interfere
1104 * with another untraced process
1105 */
Tejun Heoa288eec2011-06-17 16:50:37 +02001106 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 vm_flags &= ~VM_MAYSHARE;
1108
1109 return vm_flags;
1110}
1111
1112/*
David Howells8feae132009-01-08 12:04:47 +00001113 * set up a shared mapping on a file (the driver or filesystem provides and
1114 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
David Howells8feae132009-01-08 12:04:47 +00001116static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 int ret;
1119
1120 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001121 if (ret == 0) {
1122 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001123 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (ret != -ENOSYS)
1126 return ret;
1127
David Howells3fa30462010-03-23 13:35:21 -07001128 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1129 * opposed to tried but failed) so we can only give a suitable error as
1130 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -ENODEV;
1132}
1133
1134/*
1135 * set up a private mapping or an anonymous shared mapping
1136 */
David Howells8feae132009-01-08 12:04:47 +00001137static int do_mmap_private(struct vm_area_struct *vma,
1138 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001139 unsigned long len,
1140 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001142 unsigned long total, point;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 void *base;
David Howells8feae132009-01-08 12:04:47 +00001144 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* invoke the file's mapping function so that it can keep track of
1147 * shared mappings on devices or memory
1148 * - VM_MAYSHARE will be set if it may attempt to share
1149 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001150 if (capabilities & NOMMU_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001152 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001154 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1155 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001158 if (ret != -ENOSYS)
1159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /* getting an ENOSYS error indicates that direct mmap isn't
1162 * possible (as opposed to tried but failed) so we'll try to
1163 * make a private copy of the data and map that instead */
1164 }
1165
David Howells8feae132009-01-08 12:04:47 +00001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /* allocate some memory to hold the mapping
1168 * - note that this may not return a page-aligned address if the object
1169 * we're allocating is smaller than a page
1170 */
Bob Liuf67d9b12011-05-24 17:12:56 -07001171 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +00001172 total = 1 << order;
Bob Liuf67d9b12011-05-24 17:12:56 -07001173 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001174
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001175 /* we don't want to allocate a power-of-2 sized page set */
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001176 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001177 total = point;
David Howells8feae132009-01-08 12:04:47 +00001178
Joonsoo Kimda616532015-02-27 15:51:43 -08001179 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001180 if (!base)
1181 goto enomem;
David Howells8feae132009-01-08 12:04:47 +00001182
Joonsoo Kimdbc83582014-12-12 16:55:55 -08001183 atomic_long_add(total, &mmap_pages_allocated);
1184
David Howells8feae132009-01-08 12:04:47 +00001185 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1186 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001187 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001188 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001189
1190 vma->vm_start = region->vm_start;
1191 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (vma->vm_file) {
1194 /* read the contents of a file into the copy */
1195 mm_segment_t old_fs;
1196 loff_t fpos;
1197
1198 fpos = vma->vm_pgoff;
1199 fpos <<= PAGE_SHIFT;
1200
1201 old_fs = get_fs();
1202 set_fs(KERNEL_DS);
Al Viro6e242a12015-03-31 12:35:13 -04001203 ret = __vfs_read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 set_fs(old_fs);
1205
1206 if (ret < 0)
1207 goto error_free;
1208
1209 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001210 if (ret < len)
1211 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
1215 return 0;
1216
1217error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001218 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001219 region->vm_start = vma->vm_start = 0;
1220 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001221 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return ret;
1223
1224enomem:
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07001225 pr_err("Allocation of length %lu from process %d (%s) failed\n",
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001226 len, current->pid, current->comm);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001227 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -ENOMEM;
1229}
1230
1231/*
1232 * handle mapping creation for uClinux
1233 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001234unsigned long do_mmap(struct file *file,
1235 unsigned long addr,
1236 unsigned long len,
1237 unsigned long prot,
1238 unsigned long flags,
1239 vm_flags_t vm_flags,
1240 unsigned long pgoff,
1241 unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
David Howells8feae132009-01-08 12:04:47 +00001243 struct vm_area_struct *vma;
1244 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct rb_node *rb;
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001246 unsigned long capabilities, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 int ret;
1248
Michel Lespinasse41badc12013-02-22 16:32:47 -08001249 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* decide whether we should attempt the mapping, and if so what sort of
1252 * mapping */
1253 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1254 &capabilities);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001255 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return ret;
1257
David Howells06aab5a2009-09-24 12:33:48 +01001258 /* we ignore the address hint */
1259 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001260 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /* we've determined that we can make the mapping, now translate what we
1263 * now know into VMA flags */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001264 vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
David Howells8feae132009-01-08 12:04:47 +00001266 /* we're going to need to record the mapping */
1267 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1268 if (!region)
1269 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
David Howells8feae132009-01-08 12:04:47 +00001271 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1272 if (!vma)
1273 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
David Howells1e2ae592010-01-15 17:01:33 -08001275 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001276 region->vm_flags = vm_flags;
1277 region->vm_pgoff = pgoff;
1278
Rik van Riel5beb4932010-03-05 13:42:07 -08001279 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001280 vma->vm_flags = vm_flags;
1281 vma->vm_pgoff = pgoff;
1282
1283 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001284 region->vm_file = get_file(file);
1285 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001286 }
1287
1288 down_write(&nommu_region_sem);
1289
1290 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001292 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * - shared mappings on character devices and memory backed files are
1294 * permitted to overlap inexactly as far as we are concerned for in
1295 * these cases, sharing is handled in the driver or filesystem rather
1296 * than here
1297 */
1298 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001299 struct vm_region *pregion;
1300 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
David Howells8feae132009-01-08 12:04:47 +00001302 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1303 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001304
David Howells8feae132009-01-08 12:04:47 +00001305 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1306 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
David Howells8feae132009-01-08 12:04:47 +00001308 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 continue;
1310
1311 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001312 if (file_inode(pregion->vm_file) !=
1313 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 continue;
1315
David Howells8feae132009-01-08 12:04:47 +00001316 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 continue;
1318
David Howells8feae132009-01-08 12:04:47 +00001319 rpglen = pregion->vm_end - pregion->vm_start;
1320 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1321 rpgend = pregion->vm_pgoff + rpglen;
1322 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 continue;
1324
David Howells8feae132009-01-08 12:04:47 +00001325 /* handle inexactly overlapping matches between
1326 * mappings */
1327 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1328 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1329 /* new mapping is not a subset of the region */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001330 if (!(capabilities & NOMMU_MAP_DIRECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto sharing_violation;
1332 continue;
1333 }
1334
David Howells8feae132009-01-08 12:04:47 +00001335 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001336 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001337 vma->vm_region = pregion;
1338 start = pregion->vm_start;
1339 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1340 vma->vm_start = start;
1341 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001343 if (pregion->vm_flags & VM_MAPPED_COPY)
David Howells8feae132009-01-08 12:04:47 +00001344 vma->vm_flags |= VM_MAPPED_COPY;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001345 else {
David Howells8feae132009-01-08 12:04:47 +00001346 ret = do_mmap_shared_file(vma);
1347 if (ret < 0) {
1348 vma->vm_region = NULL;
1349 vma->vm_start = 0;
1350 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001351 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001352 pregion = NULL;
1353 goto error_just_free;
1354 }
1355 }
1356 fput(region->vm_file);
1357 kmem_cache_free(vm_region_jar, region);
1358 region = pregion;
1359 result = start;
1360 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* obtain the address at which to make a shared mapping
1364 * - this is the hook for quasi-memory character devices to
1365 * tell us the location of a shared mapping
1366 */
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001367 if (capabilities & NOMMU_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 addr = file->f_op->get_unmapped_area(file, addr, len,
1369 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001370 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001372 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001373 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* the driver refused to tell us where to site
1376 * the mapping so we'll have to attempt to copy
1377 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001378 ret = -ENODEV;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001379 if (!(capabilities & NOMMU_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001380 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001382 capabilities &= ~NOMMU_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001383 } else {
1384 vma->vm_start = region->vm_start = addr;
1385 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387 }
1388 }
1389
David Howells8feae132009-01-08 12:04:47 +00001390 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
David Howells645d83c2009-09-24 15:13:10 +01001392 /* set up the mapping
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001393 * - the region is filled in if NOMMU_MAP_DIRECT is still set
David Howells645d83c2009-09-24 15:13:10 +01001394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001396 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 else
David Howells645d83c2009-09-24 15:13:10 +01001398 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001400 goto error_just_free;
1401 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001402
Jie Zhangea637632009-12-14 18:00:02 -08001403 /* clear anonymous mappings that don't ask for uninitialized data */
1404 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1405 memset((void *)region->vm_start, 0,
1406 region->vm_end - region->vm_start);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001409 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 current->mm->total_vm += len >> PAGE_SHIFT;
1412
David Howells8feae132009-01-08 12:04:47 +00001413share:
1414 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Mike Frysingercfe79c02010-01-06 17:23:23 +00001416 /* we flush the region from the icache only when the first executable
1417 * mapping of it is made */
1418 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1419 flush_icache_range(region->vm_start, region->vm_end);
1420 region->vm_icache_flushed = true;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Mike Frysingercfe79c02010-01-06 17:23:23 +00001423 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
David Howells8feae132009-01-08 12:04:47 +00001425 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
David Howells8feae132009-01-08 12:04:47 +00001427error_just_free:
1428 up_write(&nommu_region_sem);
1429error:
David Howells89a86402009-10-30 13:13:26 +00001430 if (region->vm_file)
1431 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001432 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001433 if (vma->vm_file)
1434 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001435 kmem_cache_free(vm_area_cachep, vma);
David Howells8feae132009-01-08 12:04:47 +00001436 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
David Howells8feae132009-01-08 12:04:47 +00001438sharing_violation:
1439 up_write(&nommu_region_sem);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001440 pr_warn("Attempt to share mismatched mappings\n");
David Howells8feae132009-01-08 12:04:47 +00001441 ret = -EINVAL;
1442 goto error;
1443
1444error_getting_vma:
1445 kmem_cache_free(vm_region_jar, region);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001446 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1447 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001448 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -ENOMEM;
1450
David Howells8feae132009-01-08 12:04:47 +00001451error_getting_region:
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001452 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1453 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001454 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -ENOMEM;
1456}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001457
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001458SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1459 unsigned long, prot, unsigned long, flags,
1460 unsigned long, fd, unsigned long, pgoff)
1461{
1462 struct file *file = NULL;
1463 unsigned long retval = -EBADF;
1464
Al Viro120a7952010-10-30 02:54:44 -04001465 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001466 if (!(flags & MAP_ANONYMOUS)) {
1467 file = fget(fd);
1468 if (!file)
1469 goto out;
1470 }
1471
1472 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1473
Greg Ungererad1ed292012-06-04 14:29:59 +10001474 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001475
1476 if (file)
1477 fput(file);
1478out:
1479 return retval;
1480}
1481
Christoph Hellwiga4679372010-03-10 15:21:15 -08001482#ifdef __ARCH_WANT_SYS_OLD_MMAP
1483struct mmap_arg_struct {
1484 unsigned long addr;
1485 unsigned long len;
1486 unsigned long prot;
1487 unsigned long flags;
1488 unsigned long fd;
1489 unsigned long offset;
1490};
1491
1492SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1493{
1494 struct mmap_arg_struct a;
1495
1496 if (copy_from_user(&a, arg, sizeof(a)))
1497 return -EFAULT;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001498 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001499 return -EINVAL;
1500
1501 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1502 a.offset >> PAGE_SHIFT);
1503}
1504#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/*
David Howells8feae132009-01-08 12:04:47 +00001507 * split a vma into two pieces at address 'addr', a new vma is allocated either
1508 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
David Howells8feae132009-01-08 12:04:47 +00001510int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1511 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
David Howells8feae132009-01-08 12:04:47 +00001513 struct vm_area_struct *new;
1514 struct vm_region *region;
1515 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
David Howells779c1022010-01-15 17:01:34 -08001517 /* we're only permitted to split anonymous regions (these should have
1518 * only a single usage on the region) */
1519 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001520 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
David Howells8feae132009-01-08 12:04:47 +00001522 if (mm->map_count >= sysctl_max_map_count)
1523 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Howells8feae132009-01-08 12:04:47 +00001525 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1526 if (!region)
1527 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
David Howells8feae132009-01-08 12:04:47 +00001529 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1530 if (!new) {
1531 kmem_cache_free(vm_region_jar, region);
1532 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
David Howells8feae132009-01-08 12:04:47 +00001534
1535 /* most fields are the same, copy all, and then fixup */
1536 *new = *vma;
1537 *region = *vma->vm_region;
1538 new->vm_region = region;
1539
1540 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1541
1542 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001543 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001544 } else {
1545 region->vm_start = new->vm_start = addr;
1546 region->vm_pgoff = new->vm_pgoff += npages;
1547 }
1548
1549 if (new->vm_ops && new->vm_ops->open)
1550 new->vm_ops->open(new);
1551
1552 delete_vma_from_mm(vma);
1553 down_write(&nommu_region_sem);
1554 delete_nommu_region(vma->vm_region);
1555 if (new_below) {
1556 vma->vm_region->vm_start = vma->vm_start = addr;
1557 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1558 } else {
1559 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001560 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001561 }
1562 add_nommu_region(vma->vm_region);
1563 add_nommu_region(new->vm_region);
1564 up_write(&nommu_region_sem);
1565 add_vma_to_mm(mm, vma);
1566 add_vma_to_mm(mm, new);
1567 return 0;
1568}
1569
1570/*
1571 * shrink a VMA by removing the specified chunk from either the beginning or
1572 * the end
1573 */
1574static int shrink_vma(struct mm_struct *mm,
1575 struct vm_area_struct *vma,
1576 unsigned long from, unsigned long to)
1577{
1578 struct vm_region *region;
1579
David Howells8feae132009-01-08 12:04:47 +00001580 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1581 * and list */
1582 delete_vma_from_mm(vma);
1583 if (from > vma->vm_start)
1584 vma->vm_end = from;
1585 else
1586 vma->vm_start = to;
1587 add_vma_to_mm(mm, vma);
1588
1589 /* cut the backing region down to size */
1590 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001591 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001592
1593 down_write(&nommu_region_sem);
1594 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001595 if (from > region->vm_start) {
1596 to = region->vm_top;
1597 region->vm_top = region->vm_end = from;
1598 } else {
David Howells8feae132009-01-08 12:04:47 +00001599 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001600 }
David Howells8feae132009-01-08 12:04:47 +00001601 add_nommu_region(region);
1602 up_write(&nommu_region_sem);
1603
1604 free_page_series(from, to);
1605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
David Howells30340972006-09-27 01:50:20 -07001608/*
1609 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001610 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1611 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001612 */
David Howells8feae132009-01-08 12:04:47 +00001613int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
David Howells8feae132009-01-08 12:04:47 +00001615 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001616 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001617 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Bob Liuf67d9b12011-05-24 17:12:56 -07001619 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001620 if (len == 0)
1621 return -EINVAL;
1622
Bob Liuf67d9b12011-05-24 17:12:56 -07001623 end = start + len;
1624
David Howells8feae132009-01-08 12:04:47 +00001625 /* find the first potentially overlapping VMA */
1626 vma = find_vma(mm, start);
1627 if (!vma) {
Choi Gi-yongac714902014-04-07 15:37:36 -07001628 static int limit;
David Howells33e5d7692009-04-02 16:56:32 -07001629 if (limit < 5) {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001630 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1631 current->pid, current->comm,
1632 start, start + len - 1);
David Howells33e5d7692009-04-02 16:56:32 -07001633 limit++;
1634 }
David Howells8feae132009-01-08 12:04:47 +00001635 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
David Howells8feae132009-01-08 12:04:47 +00001638 /* we're allowed to split an anonymous VMA but not a file-backed one */
1639 if (vma->vm_file) {
1640 do {
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001641 if (start > vma->vm_start)
David Howells8feae132009-01-08 12:04:47 +00001642 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001643 if (end == vma->vm_end)
1644 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001645 vma = vma->vm_next;
1646 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001647 return -EINVAL;
1648 } else {
1649 /* the chunk must be a subset of the VMA found */
1650 if (start == vma->vm_start && end == vma->vm_end)
1651 goto erase_whole_vma;
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001652 if (start < vma->vm_start || end > vma->vm_end)
David Howells8feae132009-01-08 12:04:47 +00001653 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001654 if (offset_in_page(start))
David Howells8feae132009-01-08 12:04:47 +00001655 return -EINVAL;
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001656 if (end != vma->vm_end && offset_in_page(end))
David Howells8feae132009-01-08 12:04:47 +00001657 return -EINVAL;
David Howells8feae132009-01-08 12:04:47 +00001658 if (start != vma->vm_start && end != vma->vm_end) {
1659 ret = split_vma(mm, vma, start, 1);
Leon Romanovsky22cc8772015-06-24 16:57:47 -07001660 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001661 return ret;
David Howells8feae132009-01-08 12:04:47 +00001662 }
1663 return shrink_vma(mm, vma, start, end);
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
David Howells8feae132009-01-08 12:04:47 +00001666erase_whole_vma:
1667 delete_vma_from_mm(vma);
1668 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670}
Paul Mundtb5073172007-07-21 04:37:25 -07001671EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Al Virobfce2812012-04-20 21:57:04 -04001673int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001674{
Al Virobfce2812012-04-20 21:57:04 -04001675 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001676 int ret;
David Howells30340972006-09-27 01:50:20 -07001677
1678 down_write(&mm->mmap_sem);
1679 ret = do_munmap(mm, addr, len);
1680 up_write(&mm->mmap_sem);
1681 return ret;
1682}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001683EXPORT_SYMBOL(vm_munmap);
1684
1685SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1686{
Al Virobfce2812012-04-20 21:57:04 -04001687 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001688}
David Howells30340972006-09-27 01:50:20 -07001689
1690/*
David Howells8feae132009-01-08 12:04:47 +00001691 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001692 */
David Howells8feae132009-01-08 12:04:47 +00001693void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
David Howells8feae132009-01-08 12:04:47 +00001695 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
David Howells8feae132009-01-08 12:04:47 +00001697 if (!mm)
1698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
David Howells8feae132009-01-08 12:04:47 +00001700 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
David Howells8feae132009-01-08 12:04:47 +00001702 while ((vma = mm->mmap)) {
1703 mm->mmap = vma->vm_next;
1704 delete_vma_from_mm(vma);
1705 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001706 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708}
1709
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07001710unsigned long vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 return -ENOMEM;
1713}
1714
1715/*
David Howells6fa5f802006-09-27 01:50:21 -07001716 * expand (or shrink) an existing mapping, potentially moving it at the same
1717 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *
David Howells6fa5f802006-09-27 01:50:21 -07001719 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001720 * as long as it stays within the region allocated by do_mmap_private() and the
1721 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 *
David Howells6fa5f802006-09-27 01:50:21 -07001723 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Al Viro4b377ba2013-03-04 10:47:59 -05001725static unsigned long do_mremap(unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 unsigned long old_len, unsigned long new_len,
1727 unsigned long flags, unsigned long new_addr)
1728{
David Howells6fa5f802006-09-27 01:50:21 -07001729 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001732 old_len = PAGE_ALIGN(old_len);
1733 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001734 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return (unsigned long) -EINVAL;
1736
Alexander Kuleshov1824cb72015-11-05 18:46:35 -08001737 if (offset_in_page(addr))
David Howells8feae132009-01-08 12:04:47 +00001738 return -EINVAL;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (flags & MREMAP_FIXED && new_addr != addr)
1741 return (unsigned long) -EINVAL;
1742
David Howells8feae132009-01-08 12:04:47 +00001743 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001744 if (!vma)
1745 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
David Howells6fa5f802006-09-27 01:50:21 -07001747 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return (unsigned long) -EFAULT;
1749
David Howells6fa5f802006-09-27 01:50:21 -07001750 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return (unsigned long) -EPERM;
1752
David Howells8feae132009-01-08 12:04:47 +00001753 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return (unsigned long) -ENOMEM;
1755
1756 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001757 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001758 return vma->vm_start;
1759}
1760
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001761SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1762 unsigned long, new_len, unsigned long, flags,
1763 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001764{
1765 unsigned long ret;
1766
1767 down_write(&current->mm->mmap_sem);
1768 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1769 up_write(&current->mm->mmap_sem);
1770 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Michel Lespinasse240aade2013-02-22 16:35:56 -08001773struct page *follow_page_mask(struct vm_area_struct *vma,
1774 unsigned long address, unsigned int flags,
1775 unsigned int *page_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Michel Lespinasse240aade2013-02-22 16:35:56 -08001777 *page_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return NULL;
1779}
1780
Bob Liu8f3b1322011-07-08 15:39:46 -07001781int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1782 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Bob Liu8f3b1322011-07-08 15:39:46 -07001784 if (addr != (pfn << PAGE_SHIFT))
1785 return -EINVAL;
1786
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001787 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
Luke Yang22c4af42006-07-14 00:24:09 -07001790EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001792int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1793{
1794 unsigned long pfn = start >> PAGE_SHIFT;
1795 unsigned long vm_len = vma->vm_end - vma->vm_start;
1796
1797 pfn += vma->vm_pgoff;
1798 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1799}
1800EXPORT_SYMBOL(vm_iomap_memory);
1801
Paul Mundtf905bc42008-02-04 22:29:59 -08001802int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1803 unsigned long pgoff)
1804{
1805 unsigned int size = vma->vm_end - vma->vm_start;
1806
1807 if (!(vma->vm_flags & VM_USERMAP))
1808 return -EINVAL;
1809
1810 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1811 vma->vm_end = vma->vm_start + size;
1812
1813 return 0;
1814}
1815EXPORT_SYMBOL(remap_vmalloc_range);
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1818 unsigned long len, unsigned long pgoff, unsigned long flags)
1819{
1820 return -ENOMEM;
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823void unmap_mapping_range(struct address_space *mapping,
1824 loff_t const holebegin, loff_t const holelen,
1825 int even_cows)
1826{
1827}
Luke Yang22c4af42006-07-14 00:24:09 -07001828EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830/*
1831 * Check that a process has enough memory to allocate a new virtual
1832 * mapping. 0 means there is enough memory for the allocation to
1833 * succeed and -ENOMEM implies there is not.
1834 *
1835 * We currently support three overcommit policies, which are set via the
1836 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1837 *
1838 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1839 * Additional code 2002 Jul 20 by Robert Love.
1840 *
1841 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1842 *
1843 * Note this is a helper function intended to be used by LSMs which
1844 * wish to use this logic.
1845 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001846int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Roman Gushchin8138a672015-02-11 15:28:42 -08001848 long free, allowed, reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 vm_acct_memory(pages);
1851
1852 /*
1853 * Sometimes we want to use more memory than we have
1854 */
1855 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1856 return 0;
1857
1858 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -07001859 free = global_page_state(NR_FREE_PAGES);
1860 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Dmitry Finkc15bef32011-07-25 17:12:19 -07001862 /*
1863 * shmem pages shouldn't be counted as free in this
1864 * case, they can't be purged, only swapped out, and
1865 * that won't affect the overall amount of available
1866 * memory in the system.
1867 */
1868 free -= global_page_state(NR_SHMEM);
1869
Shaohua Liec8acf22013-02-22 16:34:38 -08001870 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /*
1873 * Any slabs which are created with the
1874 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1875 * which are reclaimable, under pressure. The dentry
1876 * cache and most inode caches should fall into this
1877 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001878 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -07001881 * Leave reserved pages. The pages are not for anonymous pages.
1882 */
1883 if (free <= totalreserve_pages)
1884 goto error;
1885 else
1886 free -= totalreserve_pages;
1887
1888 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001889 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
1891 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001892 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 if (free > pages)
1895 return 0;
1896
Hideo AOKId5ddc792006-04-10 22:53:01 -07001897 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
Jerome Marchand00619bc2013-11-12 15:08:31 -08001900 allowed = vm_commit_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001902 * Reserve some 3% for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 */
1904 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001905 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001907 /*
1908 * Don't let a single process grow so big a user can't recover
1909 */
1910 if (mm) {
1911 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
Roman Gushchin8138a672015-02-11 15:28:42 -08001912 allowed -= min_t(long, mm->total_vm / 32, reserve);
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001915 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001917
Hideo AOKId5ddc792006-04-10 22:53:01 -07001918error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 vm_unacct_memory(pages);
1920
1921 return -ENOMEM;
1922}
1923
Nick Piggind0217ac2007-07-19 01:47:03 -07001924int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001925{
1926 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001927 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001928}
Paul Mundtb5073172007-07-21 04:37:25 -07001929EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001930
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07001931void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
1932{
1933 BUG();
1934}
1935EXPORT_SYMBOL(filemap_map_pages);
1936
Mike Frysingerf55f1992011-03-29 14:05:12 +01001937static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1938 unsigned long addr, void *buf, int len, int write)
David Howells0ec76a12006-09-27 01:50:15 -07001939{
David Howells0ec76a12006-09-27 01:50:15 -07001940 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07001941
1942 down_read(&mm->mmap_sem);
1943
1944 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001945 vma = find_vma(mm, addr);
1946 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001947 /* don't overrun this mapping */
1948 if (addr + len >= vma->vm_end)
1949 len = vma->vm_end - addr;
1950
1951 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001952 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001953 copy_to_user_page(vma, NULL, addr,
1954 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001955 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001956 copy_from_user_page(vma, NULL, addr,
1957 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001958 else
1959 len = 0;
1960 } else {
1961 len = 0;
1962 }
1963
1964 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01001965
1966 return len;
1967}
1968
1969/**
1970 * @access_remote_vm - access another process' address space
1971 * @mm: the mm_struct of the target address space
1972 * @addr: start address to access
1973 * @buf: source or destination buffer
1974 * @len: number of bytes to transfer
1975 * @write: whether the access is a write
1976 *
1977 * The caller must hold a reference on @mm.
1978 */
1979int access_remote_vm(struct mm_struct *mm, unsigned long addr,
1980 void *buf, int len, int write)
1981{
1982 return __access_remote_vm(NULL, mm, addr, buf, len, write);
1983}
1984
1985/*
1986 * Access another process' address space.
1987 * - source/target buffer must be kernel space
1988 */
1989int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1990{
1991 struct mm_struct *mm;
1992
1993 if (addr + len < addr)
1994 return 0;
1995
1996 mm = get_task_mm(tsk);
1997 if (!mm)
1998 return 0;
1999
2000 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2001
David Howells0ec76a12006-09-27 01:50:15 -07002002 mmput(mm);
2003 return len;
2004}
David Howells7e660872010-01-15 17:01:39 -08002005
2006/**
2007 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2008 * @inode: The inode to check
2009 * @size: The current filesize of the inode
2010 * @newsize: The proposed filesize of the inode
2011 *
2012 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2013 * make sure that that any outstanding VMAs aren't broken and then shrink the
2014 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2015 * automatically grant mappings that are too large.
2016 */
2017int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2018 size_t newsize)
2019{
2020 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08002021 struct vm_region *region;
2022 pgoff_t low, high;
2023 size_t r_size, r_top;
2024
2025 low = newsize >> PAGE_SHIFT;
2026 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2027
2028 down_write(&nommu_region_sem);
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08002029 i_mmap_lock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08002030
2031 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002032 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08002033 /* found one - only interested if it's shared out of the page
2034 * cache */
2035 if (vma->vm_flags & VM_SHARED) {
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08002036 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08002037 up_write(&nommu_region_sem);
2038 return -ETXTBSY; /* not quite true, but near enough */
2039 }
2040 }
2041
2042 /* reduce any regions that overlap the dead zone - if in existence,
2043 * these will be pointed to by VMAs that don't overlap the dead zone
2044 *
2045 * we don't check for any regions that start beyond the EOF as there
2046 * shouldn't be any
2047 */
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08002048 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08002049 if (!(vma->vm_flags & VM_SHARED))
2050 continue;
2051
2052 region = vma->vm_region;
2053 r_size = region->vm_top - region->vm_start;
2054 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2055
2056 if (r_top > newsize) {
2057 region->vm_top -= r_top - newsize;
2058 if (region->vm_end > region->vm_top)
2059 region->vm_end = region->vm_top;
2060 }
2061 }
2062
Davidlohr Bueso1acf2e02014-12-12 16:54:39 -08002063 i_mmap_unlock_read(inode->i_mapping);
David Howells7e660872010-01-15 17:01:39 -08002064 up_write(&nommu_region_sem);
2065 return 0;
2066}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07002067
2068/*
2069 * Initialise sysctl_user_reserve_kbytes.
2070 *
2071 * This is intended to prevent a user from starting a single memory hogging
2072 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2073 * mode.
2074 *
2075 * The default value is min(3% of free memory, 128MB)
2076 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2077 */
2078static int __meminit init_user_reserve(void)
2079{
2080 unsigned long free_kbytes;
2081
2082 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2083
2084 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2085 return 0;
2086}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04002087subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07002088
2089/*
2090 * Initialise sysctl_admin_reserve_kbytes.
2091 *
2092 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2093 * to log in and kill a memory hogging process.
2094 *
2095 * Systems with more than 256MB will reserve 8MB, enough to recover
2096 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2097 * only reserve 3% of free pages by default.
2098 */
2099static int __meminit init_admin_reserve(void)
2100{
2101 unsigned long free_kbytes;
2102
2103 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2104
2105 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2106 return 0;
2107}
Paul Gortmakera4bc6fc2015-05-01 20:08:20 -04002108subsys_initcall(init_admin_reserve);
Dave Hansencde70142016-02-12 13:01:55 -08002109
2110long get_user_pages8(struct task_struct *tsk, struct mm_struct *mm,
2111 unsigned long start, unsigned long nr_pages,
2112 int write, int force, struct page **pages,
2113 struct vm_area_struct **vmas)
2114{
2115 return get_user_pages6(start, nr_pages, write, force, pages, vmas);
2116}
2117EXPORT_SYMBOL(get_user_pages8);
2118
2119long get_user_pages_locked8(struct task_struct *tsk, struct mm_struct *mm,
2120 unsigned long start, unsigned long nr_pages,
2121 int write, int force, struct page **pages,
2122 int *locked)
2123{
2124 return get_user_pages_locked6(start, nr_pages, write,
2125 force, pages, locked);
2126}
2127EXPORT_SYMBOL(get_user_pages_locked8);
2128
2129long get_user_pages_unlocked7(struct task_struct *tsk, struct mm_struct *mm,
2130 unsigned long start, unsigned long nr_pages,
2131 int write, int force, struct page **pages)
2132{
2133 return get_user_pages_unlocked5(start, nr_pages, write, force, pages);
2134}
2135EXPORT_SYMBOL(get_user_pages_unlocked7);
2136