blob: fbe3e2f317eb4d39e24ab4330c0ccbc01a291b74 [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
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/swap.h>
20#include <linux/file.h>
21#include <linux/highmem.h>
22#include <linux/pagemap.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/blkdev.h>
26#include <linux/backing-dev.h>
27#include <linux/mount.h>
28#include <linux/personality.h>
29#include <linux/security.h>
30#include <linux/syscalls.h>
Al Viro120a7952010-10-30 02:54:44 -040031#include <linux/audit.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060032#include <linux/sched/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/uaccess.h>
35#include <asm/tlb.h>
36#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070037#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000038#include "internal.h"
39
David Howells8feae132009-01-08 12:04:47 +000040#if 0
41#define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43#define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45#define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47#else
48#define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50#define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52#define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56void *high_memory;
57struct page *mem_map;
58unsigned long max_mapnr;
59unsigned long num_physpages;
Hugh Dickins4266c972009-09-23 17:05:53 +010060unsigned long highest_memmap_pfn;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070061struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
63int sysctl_overcommit_ratio = 50; /* default is 50% */
64int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070065int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -070066unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -070067unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068int heap_stack_gap = 0;
69
David Howells33e5d7692009-04-02 16:56:32 -070070atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000071
K. Y. Srinivasan997071b2012-11-15 14:34:42 -080072/*
73 * The global memory commitment made in the system can be a metric
74 * that can be used to drive ballooning decisions when Linux is hosted
75 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
76 * balancing memory across competing virtual machines that are hosted.
77 * Several metrics drive this policy engine including the guest reported
78 * memory commitment.
79 */
80unsigned long vm_memory_committed(void)
81{
82 return percpu_counter_read_positive(&vm_committed_as);
83}
84
85EXPORT_SYMBOL_GPL(vm_memory_committed);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070088EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David Howells8feae132009-01-08 12:04:47 +000090/* list of mapped, potentially shareable regions */
91static struct kmem_cache *vm_region_jar;
92struct rb_root nommu_region_tree = RB_ROOT;
93DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040095const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * Return the total memory allocated for this pointer, not
100 * just what the caller asked for.
101 *
102 * Doesn't have to be accurate, i.e. may have races.
103 */
104unsigned int kobjsize(const void *objp)
105{
106 struct page *page;
107
Michael Hennerich4016a132008-04-28 02:13:38 -0700108 /*
109 * If the object we have should not have ksize performed on it,
110 * return size of 0
111 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900112 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700113 return 0;
114
115 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700116
117 /*
118 * If the allocator sets PageSlab, we know the pointer came from
119 * kmalloc().
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (PageSlab(page))
122 return ksize(objp);
123
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700124 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000125 * If it's not a compound page, see if we have a matching VMA
126 * region. This test is intentionally done in reverse order,
127 * so if there's no VMA, we still fall through and hand back
128 * PAGE_SIZE for 0-order pages.
129 */
130 if (!PageCompound(page)) {
131 struct vm_area_struct *vma;
132
133 vma = find_vma(current->mm, (unsigned long)objp);
134 if (vma)
135 return vma->vm_end - vma->vm_start;
136 }
137
138 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700139 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900140 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700141 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900142 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Michel Lespinasse28a35712013-02-22 16:35:55 -0800145long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
146 unsigned long start, unsigned long nr_pages,
147 unsigned int foll_flags, struct page **pages,
148 struct vm_area_struct **vmas, int *nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700150 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700151 unsigned long vm_flags;
152 int i;
153
154 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700155 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700156 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700157 vm_flags = (foll_flags & FOLL_WRITE) ?
158 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
159 vm_flags &= (foll_flags & FOLL_FORCE) ?
160 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Peter Zijlstra9d737772009-06-25 11:58:55 +0200162 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000163 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700164 if (!vma)
165 goto finish_or_fault;
166
167 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700168 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
169 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700170 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (pages) {
173 pages[i] = virt_to_page(start);
174 if (pages[i])
175 page_cache_get(pages[i]);
176 }
177 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700178 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000179 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
David Howells7b4d5b82006-09-27 01:50:18 -0700181
182 return i;
183
184finish_or_fault:
185 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Nick Pigginb291f002008-10-18 20:26:44 -0700187
Nick Pigginb291f002008-10-18 20:26:44 -0700188/*
189 * get a list of pages in an address range belonging to the specified process
190 * and indicate the VMA that covers each page
191 * - this is potentially dodgy as we may end incrementing the page count of a
192 * slab page or a secondary page from a compound page
193 * - don't permit access to VMAs that don't support it, such as I/O mappings
194 */
Michel Lespinasse28a35712013-02-22 16:35:55 -0800195long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
196 unsigned long start, unsigned long nr_pages,
197 int write, int force, struct page **pages,
198 struct vm_area_struct **vmas)
Nick Pigginb291f002008-10-18 20:26:44 -0700199{
200 int flags = 0;
201
202 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700203 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700204 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700205 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700206
Michel Lespinasse53a77062011-01-13 15:46:14 -0800207 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
208 NULL);
Nick Pigginb291f002008-10-18 20:26:44 -0700209}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000210EXPORT_SYMBOL(get_user_pages);
211
Paul Mundtdfc2f912009-06-26 04:31:57 +0900212/**
213 * follow_pfn - look up PFN at a user virtual address
214 * @vma: memory mapping
215 * @address: user virtual address
216 * @pfn: location to store found PFN
217 *
218 * Only IO mappings and raw PFN mappings are allowed.
219 *
220 * Returns zero and the pfn at @pfn on success, -ve otherwise.
221 */
222int follow_pfn(struct vm_area_struct *vma, unsigned long address,
223 unsigned long *pfn)
224{
225 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
226 return -EINVAL;
227
228 *pfn = address >> PAGE_SHIFT;
229 return 0;
230}
231EXPORT_SYMBOL(follow_pfn);
232
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700233LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800235void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 kfree(addr);
238}
Paul Mundtb5073172007-07-21 04:37:25 -0700239EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Al Virodd0fc662005-10-07 07:46:04 +0100241void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200244 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
245 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Nick Piggin84097512006-03-22 00:08:34 -0800247 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Paul Mundtb5073172007-07-21 04:37:25 -0700249EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Paul Mundtf905bc42008-02-04 22:29:59 -0800251void *vmalloc_user(unsigned long size)
252{
253 void *ret;
254
255 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
256 PAGE_KERNEL);
257 if (ret) {
258 struct vm_area_struct *vma;
259
260 down_write(&current->mm->mmap_sem);
261 vma = find_vma(current->mm, (unsigned long)ret);
262 if (vma)
263 vma->vm_flags |= VM_USERMAP;
264 up_write(&current->mm->mmap_sem);
265 }
266
267 return ret;
268}
269EXPORT_SYMBOL(vmalloc_user);
270
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800271struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 return virt_to_page(addr);
274}
Paul Mundtb5073172007-07-21 04:37:25 -0700275EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800277unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 return page_to_pfn(virt_to_page(addr));
280}
Paul Mundtb5073172007-07-21 04:37:25 -0700281EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283long vread(char *buf, char *addr, unsigned long count)
284{
285 memcpy(buf, addr, count);
286 return count;
287}
288
289long vwrite(char *buf, char *addr, unsigned long count)
290{
291 /* Don't allow overflow */
292 if ((unsigned long) addr + count < count)
293 count = -(unsigned long) addr;
294
295 memcpy(addr, buf, count);
296 return(count);
297}
298
299/*
300 * vmalloc - allocate virtually continguos memory
301 *
302 * @size: allocation size
303 *
304 * Allocate enough pages to cover @size from the page level
305 * allocator and map them into continguos kernel virtual space.
306 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200307 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * use __vmalloc() instead.
309 */
310void *vmalloc(unsigned long size)
311{
312 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
313}
Andrew Mortonf6138882006-02-28 16:59:18 -0800314EXPORT_SYMBOL(vmalloc);
315
Dave Younge1ca7782010-10-26 14:22:06 -0700316/*
317 * vzalloc - allocate virtually continguos memory with zero fill
318 *
319 * @size: allocation size
320 *
321 * Allocate enough pages to cover @size from the page level
322 * allocator and map them into continguos kernel virtual space.
323 * The memory allocated is set to zero.
324 *
325 * For tight control over page level allocator and protection flags
326 * use __vmalloc() instead.
327 */
328void *vzalloc(unsigned long size)
329{
330 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
331 PAGE_KERNEL);
332}
333EXPORT_SYMBOL(vzalloc);
334
335/**
336 * vmalloc_node - allocate memory on a specific node
337 * @size: allocation size
338 * @node: numa node
339 *
340 * Allocate enough pages to cover @size from the page level
341 * allocator and map them into contiguous kernel virtual space.
342 *
343 * For tight control over page level allocator and protection flags
344 * use __vmalloc() instead.
345 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800346void *vmalloc_node(unsigned long size, int node)
347{
348 return vmalloc(size);
349}
Paul Mundt9a14f652010-12-24 11:50:34 +0900350EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700351
352/**
353 * vzalloc_node - allocate memory on a specific node with zero fill
354 * @size: allocation size
355 * @node: numa node
356 *
357 * Allocate enough pages to cover @size from the page level
358 * allocator and map them into contiguous kernel virtual space.
359 * The memory allocated is set to zero.
360 *
361 * For tight control over page level allocator and protection flags
362 * use __vmalloc() instead.
363 */
364void *vzalloc_node(unsigned long size, int node)
365{
366 return vzalloc(size);
367}
368EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Paul Mundt1af446e2008-08-04 16:01:47 +0900370#ifndef PAGE_KERNEL_EXEC
371# define PAGE_KERNEL_EXEC PAGE_KERNEL
372#endif
373
374/**
375 * vmalloc_exec - allocate virtually contiguous, executable memory
376 * @size: allocation size
377 *
378 * Kernel-internal function to allocate enough pages to cover @size
379 * the page level allocator and map them into contiguous and
380 * executable kernel virtual space.
381 *
382 * For tight control over page level allocator and protection flags
383 * use __vmalloc() instead.
384 */
385
386void *vmalloc_exec(unsigned long size)
387{
388 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
389}
390
Paul Mundtb5073172007-07-21 04:37:25 -0700391/**
392 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * @size: allocation size
394 *
395 * Allocate enough 32bit PA addressable pages to cover @size from the
396 * page level allocator and map them into continguos kernel virtual space.
397 */
398void *vmalloc_32(unsigned long size)
399{
400 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
401}
Paul Mundtb5073172007-07-21 04:37:25 -0700402EXPORT_SYMBOL(vmalloc_32);
403
404/**
405 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
406 * @size: allocation size
407 *
408 * The resulting memory area is 32bit addressable and zeroed so it can be
409 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800410 *
411 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
412 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700413 */
414void *vmalloc_32_user(unsigned long size)
415{
Paul Mundtf905bc42008-02-04 22:29:59 -0800416 /*
417 * We'll have to sort out the ZONE_DMA bits for 64-bit,
418 * but for now this can simply use vmalloc_user() directly.
419 */
420 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700421}
422EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
425{
426 BUG();
427 return NULL;
428}
Paul Mundtb5073172007-07-21 04:37:25 -0700429EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800431void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 BUG();
434}
Paul Mundtb5073172007-07-21 04:37:25 -0700435EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Paul Mundteb6434d2009-01-21 17:45:47 +0900437void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
438{
439 BUG();
440 return NULL;
441}
442EXPORT_SYMBOL(vm_map_ram);
443
444void vm_unmap_ram(const void *mem, unsigned int count)
445{
446 BUG();
447}
448EXPORT_SYMBOL(vm_unmap_ram);
449
450void vm_unmap_aliases(void)
451{
452}
453EXPORT_SYMBOL_GPL(vm_unmap_aliases);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700456 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
457 * have one.
458 */
459void __attribute__((weak)) vmalloc_sync_all(void)
460{
461}
462
Paul Mundt29c185e2010-12-24 12:08:30 +0900463/**
464 * alloc_vm_area - allocate a range of kernel address space
465 * @size: size of the area
466 *
467 * Returns: NULL on failure, vm_struct on success
468 *
469 * This function reserves a range of kernel address space, and
470 * allocates pagetables to map that range. No actual mappings
471 * are created. If the kernel address space is not shared
472 * between processes, it syncs the pagetable across all
473 * processes.
474 */
David Vrabelcd129092011-09-29 16:53:32 +0100475struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900476{
477 BUG();
478 return NULL;
479}
480EXPORT_SYMBOL_GPL(alloc_vm_area);
481
482void free_vm_area(struct vm_struct *area)
483{
484 BUG();
485}
486EXPORT_SYMBOL_GPL(free_vm_area);
487
Paul Mundtb5073172007-07-21 04:37:25 -0700488int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
489 struct page *page)
490{
491 return -EINVAL;
492}
493EXPORT_SYMBOL(vm_insert_page);
494
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * sys_brk() for the most part doesn't need the global kernel
497 * lock, except when an application is doing something nasty
498 * like trying to un-brk an area that has already been mapped
499 * to a regular file. in this case, the unmapping will need
500 * to invoke file system routines that need the global lock.
501 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100502SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct mm_struct *mm = current->mm;
505
506 if (brk < mm->start_brk || brk > mm->context.end_brk)
507 return mm->brk;
508
509 if (mm->brk == brk)
510 return mm->brk;
511
512 /*
513 * Always allow shrinking brk
514 */
515 if (brk <= mm->brk) {
516 mm->brk = brk;
517 return brk;
518 }
519
520 /*
521 * Ok, looks good - let it rip.
522 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000523 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return mm->brk = brk;
525}
526
David Howells8feae132009-01-08 12:04:47 +0000527/*
528 * initialise the VMA and region record slabs
529 */
530void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700532 int ret;
533
534 ret = percpu_counter_init(&vm_committed_as, 0);
535 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700536 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Howells8feae132009-01-08 12:04:47 +0000539/*
540 * validate the region tree
541 * - the caller must hold the region lock
542 */
543#ifdef CONFIG_DEBUG_NOMMU_REGIONS
544static noinline void validate_nommu_regions(void)
545{
546 struct vm_region *region, *last;
547 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
David Howells8feae132009-01-08 12:04:47 +0000549 lastp = rb_first(&nommu_region_tree);
550 if (!lastp)
551 return;
552
553 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700554 BUG_ON(unlikely(last->vm_end <= last->vm_start));
555 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000556
557 while ((p = rb_next(lastp))) {
558 region = rb_entry(p, struct vm_region, vm_rb);
559 last = rb_entry(lastp, struct vm_region, vm_rb);
560
David Howells33e5d7692009-04-02 16:56:32 -0700561 BUG_ON(unlikely(region->vm_end <= region->vm_start));
562 BUG_ON(unlikely(region->vm_top < region->vm_end));
563 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000564
565 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567}
David Howells8feae132009-01-08 12:04:47 +0000568#else
David Howells33e5d7692009-04-02 16:56:32 -0700569static void validate_nommu_regions(void)
570{
571}
David Howells8feae132009-01-08 12:04:47 +0000572#endif
573
574/*
575 * add a region into the global tree
576 */
577static void add_nommu_region(struct vm_region *region)
578{
579 struct vm_region *pregion;
580 struct rb_node **p, *parent;
581
582 validate_nommu_regions();
583
David Howells8feae132009-01-08 12:04:47 +0000584 parent = NULL;
585 p = &nommu_region_tree.rb_node;
586 while (*p) {
587 parent = *p;
588 pregion = rb_entry(parent, struct vm_region, vm_rb);
589 if (region->vm_start < pregion->vm_start)
590 p = &(*p)->rb_left;
591 else if (region->vm_start > pregion->vm_start)
592 p = &(*p)->rb_right;
593 else if (pregion == region)
594 return;
595 else
596 BUG();
597 }
598
599 rb_link_node(&region->vm_rb, parent, p);
600 rb_insert_color(&region->vm_rb, &nommu_region_tree);
601
602 validate_nommu_regions();
603}
604
605/*
606 * delete a region from the global tree
607 */
608static void delete_nommu_region(struct vm_region *region)
609{
610 BUG_ON(!nommu_region_tree.rb_node);
611
612 validate_nommu_regions();
613 rb_erase(&region->vm_rb, &nommu_region_tree);
614 validate_nommu_regions();
615}
616
617/*
618 * free a contiguous series of pages
619 */
620static void free_page_series(unsigned long from, unsigned long to)
621{
622 for (; from < to; from += PAGE_SIZE) {
623 struct page *page = virt_to_page(from);
624
625 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700626 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000627 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700628 kdebug("free page %p: refcount not one: %d",
629 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000630 put_page(page);
631 }
632}
633
634/*
635 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700636 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000637 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000638 * will equal vm_start
639 */
640static void __put_nommu_region(struct vm_region *region)
641 __releases(nommu_region_sem)
642{
David Howells1e2ae592010-01-15 17:01:33 -0800643 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000644
645 BUG_ON(!nommu_region_tree.rb_node);
646
David Howells1e2ae592010-01-15 17:01:33 -0800647 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000648 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000649 delete_nommu_region(region);
650 up_write(&nommu_region_sem);
651
652 if (region->vm_file)
653 fput(region->vm_file);
654
655 /* IO memory and memory shared directly out of the pagecache
656 * from ramfs/tmpfs mustn't be released here */
657 if (region->vm_flags & VM_MAPPED_COPY) {
658 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000659 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000660 }
661 kmem_cache_free(vm_region_jar, region);
662 } else {
663 up_write(&nommu_region_sem);
664 }
665}
666
667/*
668 * release a reference to a region
669 */
670static void put_nommu_region(struct vm_region *region)
671{
672 down_write(&nommu_region_sem);
673 __put_nommu_region(region);
674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
David Howells30340972006-09-27 01:50:20 -0700676/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700677 * update protection on a vma
678 */
679static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
680{
681#ifdef CONFIG_MPU
682 struct mm_struct *mm = vma->vm_mm;
683 long start = vma->vm_start & PAGE_MASK;
684 while (start < vma->vm_end) {
685 protect_page(mm, start, flags);
686 start += PAGE_SIZE;
687 }
688 update_protections(mm);
689#endif
690}
691
692/*
David Howells30340972006-09-27 01:50:20 -0700693 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000694 * and tree and add to the address space's page tree also if not an anonymous
695 * page
David Howells30340972006-09-27 01:50:20 -0700696 * - should be called with mm->mmap_sem held writelocked
697 */
David Howells8feae132009-01-08 12:04:47 +0000698static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700699{
Namhyung Kim6038def2011-05-24 17:11:22 -0700700 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000701 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700702 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700703
David Howells8feae132009-01-08 12:04:47 +0000704 kenter(",%p", vma);
705
706 BUG_ON(!vma->vm_region);
707
708 mm->map_count++;
709 vma->vm_mm = mm;
710
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700711 protect_vma(vma, vma->vm_flags);
712
David Howells8feae132009-01-08 12:04:47 +0000713 /* add the VMA to the mapping */
714 if (vma->vm_file) {
715 mapping = vma->vm_file->f_mapping;
716
David Howells918e5562012-02-23 13:50:35 +0000717 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000718 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700719 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000720 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000721 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000722 }
723
724 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700725 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000726 p = &mm->mm_rb.rb_node;
727 while (*p) {
728 parent = *p;
729 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
730
731 /* sort by: start addr, end addr, VMA struct addr in that order
732 * (the latter is necessary as we may get identical VMAs) */
733 if (vma->vm_start < pvma->vm_start)
734 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700735 else if (vma->vm_start > pvma->vm_start) {
736 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000737 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700738 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000739 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700740 else if (vma->vm_end > pvma->vm_end) {
741 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000742 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700743 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000744 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700745 else if (vma > pvma) {
746 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000747 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700748 } else
David Howells8feae132009-01-08 12:04:47 +0000749 BUG();
750 }
751
752 rb_link_node(&vma->vm_rb, parent, p);
753 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
754
755 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700756 prev = NULL;
757 if (rb_prev)
758 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700759
Namhyung Kim6038def2011-05-24 17:11:22 -0700760 __vma_link_list(mm, vma, prev, parent);
David Howells8feae132009-01-08 12:04:47 +0000761}
762
763/*
764 * delete a VMA from its owning mm_struct and address space
765 */
766static void delete_vma_from_mm(struct vm_area_struct *vma)
767{
David Howells8feae132009-01-08 12:04:47 +0000768 struct address_space *mapping;
769 struct mm_struct *mm = vma->vm_mm;
770
771 kenter("%p", vma);
772
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700773 protect_vma(vma, 0);
774
David Howells8feae132009-01-08 12:04:47 +0000775 mm->map_count--;
776 if (mm->mmap_cache == vma)
777 mm->mmap_cache = NULL;
778
779 /* remove the VMA from the mapping */
780 if (vma->vm_file) {
781 mapping = vma->vm_file->f_mapping;
782
David Howells918e5562012-02-23 13:50:35 +0000783 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000784 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700785 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000786 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000787 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000788 }
789
790 /* remove from the MM's tree and list */
791 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700792
793 if (vma->vm_prev)
794 vma->vm_prev->vm_next = vma->vm_next;
795 else
796 mm->mmap = vma->vm_next;
797
798 if (vma->vm_next)
799 vma->vm_next->vm_prev = vma->vm_prev;
David Howells8feae132009-01-08 12:04:47 +0000800}
801
802/*
803 * destroy a VMA record
804 */
805static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
806{
807 kenter("%p", vma);
808 if (vma->vm_ops && vma->vm_ops->close)
809 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700810 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000811 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000812 put_nommu_region(vma->vm_region);
813 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700814}
815
816/*
817 * look up the first VMA in which addr resides, NULL if none
818 * - should be called with mm->mmap_sem at least held readlocked
819 */
820struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
821{
David Howells8feae132009-01-08 12:04:47 +0000822 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700823
David Howells8feae132009-01-08 12:04:47 +0000824 /* check the cache first */
Jan Stancekb6a9b7f2013-04-04 11:35:10 -0700825 vma = ACCESS_ONCE(mm->mmap_cache);
David Howells8feae132009-01-08 12:04:47 +0000826 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
827 return vma;
828
Namhyung Kime922c4c2011-05-24 17:11:24 -0700829 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000830 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700831 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000832 if (vma->vm_start > addr)
833 return NULL;
834 if (vma->vm_end > addr) {
835 mm->mmap_cache = vma;
836 return vma;
837 }
David Howells30340972006-09-27 01:50:20 -0700838 }
839
David Howells30340972006-09-27 01:50:20 -0700840 return NULL;
841}
842EXPORT_SYMBOL(find_vma);
843
844/*
David Howells930e6522006-09-27 01:50:22 -0700845 * find a VMA
846 * - we don't extend stack VMAs under NOMMU conditions
847 */
848struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
849{
David Howells7561e8c2010-03-25 16:48:38 +0000850 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700851}
852
David Howells8feae132009-01-08 12:04:47 +0000853/*
854 * expand a stack to a given address
855 * - not supported under NOMMU conditions
856 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700857int expand_stack(struct vm_area_struct *vma, unsigned long address)
858{
859 return -ENOMEM;
860}
861
David Howells930e6522006-09-27 01:50:22 -0700862/*
David Howells6fa5f802006-09-27 01:50:21 -0700863 * look up the first VMA exactly that exactly matches addr
864 * - should be called with mm->mmap_sem at least held readlocked
865 */
David Howells8feae132009-01-08 12:04:47 +0000866static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
867 unsigned long addr,
868 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000871 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
David Howells8feae132009-01-08 12:04:47 +0000873 /* check the cache first */
874 vma = mm->mmap_cache;
875 if (vma && vma->vm_start == addr && vma->vm_end == end)
876 return vma;
877
Namhyung Kime922c4c2011-05-24 17:11:24 -0700878 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000879 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700880 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000881 if (vma->vm_start < addr)
882 continue;
883 if (vma->vm_start > addr)
884 return NULL;
885 if (vma->vm_end == end) {
886 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return vma;
David Howells8feae132009-01-08 12:04:47 +0000888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 return NULL;
892}
893
David Howells30340972006-09-27 01:50:20 -0700894/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * determine whether a mapping should be permitted and, if so, what sort of
896 * mapping we're capable of supporting
897 */
898static int validate_mmap_request(struct file *file,
899 unsigned long addr,
900 unsigned long len,
901 unsigned long prot,
902 unsigned long flags,
903 unsigned long pgoff,
904 unsigned long *_capabilities)
905{
David Howells8feae132009-01-08 12:04:47 +0000906 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int ret;
908
909 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100910 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 printk(KERN_DEBUG
912 "%d: Can't do fixed-address/overlay mmap of RAM\n",
913 current->pid);
914 return -EINVAL;
915 }
916
917 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
918 (flags & MAP_TYPE) != MAP_SHARED)
919 return -EINVAL;
920
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000921 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -EINVAL;
923
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000924 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000925 rlen = PAGE_ALIGN(len);
926 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000927 return -ENOMEM;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000930 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000931 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (file) {
934 /* validate file mapping requests */
935 struct address_space *mapping;
936
937 /* files must support mmap */
938 if (!file->f_op || !file->f_op->mmap)
939 return -ENODEV;
940
941 /* work out if what we've got could possibly be shared
942 * - we support chardevs that provide their own "memory"
943 * - we support files/blockdevs that are memory backed
944 */
945 mapping = file->f_mapping;
946 if (!mapping)
Al Viro496ad9a2013-01-23 17:07:38 -0500947 mapping = file_inode(file)->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 capabilities = 0;
950 if (mapping && mapping->backing_dev_info)
951 capabilities = mapping->backing_dev_info->capabilities;
952
953 if (!capabilities) {
954 /* no explicit capabilities set, so assume some
955 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500956 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 case S_IFREG:
958 case S_IFBLK:
959 capabilities = BDI_CAP_MAP_COPY;
960 break;
961
962 case S_IFCHR:
963 capabilities =
964 BDI_CAP_MAP_DIRECT |
965 BDI_CAP_READ_MAP |
966 BDI_CAP_WRITE_MAP;
967 break;
968
969 default:
970 return -EINVAL;
971 }
972 }
973
974 /* eliminate any capabilities that we can't support on this
975 * device */
976 if (!file->f_op->get_unmapped_area)
977 capabilities &= ~BDI_CAP_MAP_DIRECT;
978 if (!file->f_op->read)
979 capabilities &= ~BDI_CAP_MAP_COPY;
980
Graff Yang28d7a6a2009-08-18 14:11:17 -0700981 /* The file shall have been opened with read permission. */
982 if (!(file->f_mode & FMODE_READ))
983 return -EACCES;
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (flags & MAP_SHARED) {
986 /* do checks for writing, appending and locking */
987 if ((prot & PROT_WRITE) &&
988 !(file->f_mode & FMODE_WRITE))
989 return -EACCES;
990
Al Viro496ad9a2013-01-23 17:07:38 -0500991 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 (file->f_mode & FMODE_WRITE))
993 return -EACCES;
994
Al Viro496ad9a2013-01-23 17:07:38 -0500995 if (locks_verify_locked(file_inode(file)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -EAGAIN;
997
998 if (!(capabilities & BDI_CAP_MAP_DIRECT))
999 return -ENODEV;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* we mustn't privatise shared mappings */
1002 capabilities &= ~BDI_CAP_MAP_COPY;
1003 }
1004 else {
1005 /* we're going to read the file into private memory we
1006 * allocate */
1007 if (!(capabilities & BDI_CAP_MAP_COPY))
1008 return -ENODEV;
1009
1010 /* we don't permit a private writable mapping to be
1011 * shared with the backing device */
1012 if (prot & PROT_WRITE)
1013 capabilities &= ~BDI_CAP_MAP_DIRECT;
1014 }
1015
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001016 if (capabilities & BDI_CAP_MAP_DIRECT) {
1017 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1018 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1019 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1020 ) {
1021 capabilities &= ~BDI_CAP_MAP_DIRECT;
1022 if (flags & MAP_SHARED) {
1023 printk(KERN_WARNING
1024 "MAP_SHARED not completely supported on !MMU\n");
1025 return -EINVAL;
1026 }
1027 }
1028 }
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* handle executable mappings and implied executable
1031 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -08001032 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (prot & PROT_EXEC)
1034 return -EPERM;
1035 }
1036 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1037 /* handle implication of PROT_EXEC by PROT_READ */
1038 if (current->personality & READ_IMPLIES_EXEC) {
1039 if (capabilities & BDI_CAP_EXEC_MAP)
1040 prot |= PROT_EXEC;
1041 }
1042 }
1043 else if ((prot & PROT_READ) &&
1044 (prot & PROT_EXEC) &&
1045 !(capabilities & BDI_CAP_EXEC_MAP)
1046 ) {
1047 /* backing file is not executable, try to copy */
1048 capabilities &= ~BDI_CAP_MAP_DIRECT;
1049 }
1050 }
1051 else {
1052 /* anonymous mappings are always memory backed and can be
1053 * privately mapped
1054 */
1055 capabilities = BDI_CAP_MAP_COPY;
1056
1057 /* handle PROT_EXEC implication by PROT_READ */
1058 if ((prot & PROT_READ) &&
1059 (current->personality & READ_IMPLIES_EXEC))
1060 prot |= PROT_EXEC;
1061 }
1062
1063 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -04001064 ret = security_mmap_addr(addr);
1065 if (ret < 0)
1066 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /* looks okay */
1069 *_capabilities = capabilities;
1070 return 0;
1071}
1072
1073/*
1074 * we've determined that we can make the mapping, now translate what we
1075 * now know into VMA flags
1076 */
1077static unsigned long determine_vm_flags(struct file *file,
1078 unsigned long prot,
1079 unsigned long flags,
1080 unsigned long capabilities)
1081{
1082 unsigned long vm_flags;
1083
1084 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* vm_flags |= mm->def_flags; */
1086
1087 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1088 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001089 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (file && !(prot & PROT_WRITE))
1091 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001092 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* overlay a shareable mapping on the backing device or inode
1094 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1095 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001096 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001098 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 /* refuse to let anyone share private mappings with this process if
1102 * it's being traced - otherwise breakpoints set in it may interfere
1103 * with another untraced process
1104 */
Tejun Heoa288eec2011-06-17 16:50:37 +02001105 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 vm_flags &= ~VM_MAYSHARE;
1107
1108 return vm_flags;
1109}
1110
1111/*
David Howells8feae132009-01-08 12:04:47 +00001112 * set up a shared mapping on a file (the driver or filesystem provides and
1113 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
David Howells8feae132009-01-08 12:04:47 +00001115static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 int ret;
1118
1119 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001120 if (ret == 0) {
1121 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001122 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (ret != -ENOSYS)
1125 return ret;
1126
David Howells3fa30462010-03-23 13:35:21 -07001127 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1128 * opposed to tried but failed) so we can only give a suitable error as
1129 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return -ENODEV;
1131}
1132
1133/*
1134 * set up a private mapping or an anonymous shared mapping
1135 */
David Howells8feae132009-01-08 12:04:47 +00001136static int do_mmap_private(struct vm_area_struct *vma,
1137 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001138 unsigned long len,
1139 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
David Howells8feae132009-01-08 12:04:47 +00001141 struct page *pages;
Bob Liuf67d9b12011-05-24 17:12:56 -07001142 unsigned long total, point, n;
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 */
David Howells645d83c2009-09-24 15:13:10 +01001150 if (capabilities & BDI_CAP_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 kdebug("alloc order %d for %lx", order, len);
1173
1174 pages = alloc_pages(GFP_KERNEL, order);
1175 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto enomem;
1177
David Howells8feae132009-01-08 12:04:47 +00001178 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001179 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Bob Liuf67d9b12011-05-24 17:12:56 -07001181 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001182
1183 /* we allocated a power-of-2 sized page set, so we may want to trim off
1184 * the excess */
1185 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1186 while (total > point) {
1187 order = ilog2(total - point);
1188 n = 1 << order;
1189 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001190 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001191 total -= n;
1192 set_page_refcounted(pages + total);
1193 __free_pages(pages + total, order);
1194 }
David Howells8feae132009-01-08 12:04:47 +00001195 }
1196
David Howells8feae132009-01-08 12:04:47 +00001197 for (point = 1; point < total; point++)
1198 set_page_refcounted(&pages[point]);
1199
1200 base = page_address(pages);
1201 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1202 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001203 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001204 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001205
1206 vma->vm_start = region->vm_start;
1207 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (vma->vm_file) {
1210 /* read the contents of a file into the copy */
1211 mm_segment_t old_fs;
1212 loff_t fpos;
1213
1214 fpos = vma->vm_pgoff;
1215 fpos <<= PAGE_SHIFT;
1216
1217 old_fs = get_fs();
1218 set_fs(KERNEL_DS);
Bob Liuf67d9b12011-05-24 17:12:56 -07001219 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 set_fs(old_fs);
1221
1222 if (ret < 0)
1223 goto error_free;
1224
1225 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001226 if (ret < len)
1227 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
1231 return 0;
1232
1233error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001234 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001235 region->vm_start = vma->vm_start = 0;
1236 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001237 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
1239
1240enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001241 printk("Allocation of length %lu from process %d (%s) failed\n",
1242 len, current->pid, current->comm);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001243 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -ENOMEM;
1245}
1246
1247/*
1248 * handle mapping creation for uClinux
1249 */
Al Viroe3fc6292012-05-30 20:08:42 -04001250unsigned long do_mmap_pgoff(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 unsigned long addr,
1252 unsigned long len,
1253 unsigned long prot,
1254 unsigned long flags,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001255 unsigned long pgoff,
Michel Lespinasse41badc12013-02-22 16:32:47 -08001256 unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
David Howells8feae132009-01-08 12:04:47 +00001258 struct vm_area_struct *vma;
1259 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001261 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int ret;
1263
David Howells8feae132009-01-08 12:04:47 +00001264 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1265
Michel Lespinasse41badc12013-02-22 16:32:47 -08001266 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* decide whether we should attempt the mapping, and if so what sort of
1269 * mapping */
1270 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1271 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001272 if (ret < 0) {
1273 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return ret;
David Howells8feae132009-01-08 12:04:47 +00001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
David Howells06aab5a2009-09-24 12:33:48 +01001277 /* we ignore the address hint */
1278 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001279 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* we've determined that we can make the mapping, now translate what we
1282 * now know into VMA flags */
1283 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1284
David Howells8feae132009-01-08 12:04:47 +00001285 /* we're going to need to record the mapping */
1286 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1287 if (!region)
1288 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David Howells8feae132009-01-08 12:04:47 +00001290 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1291 if (!vma)
1292 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
David Howells1e2ae592010-01-15 17:01:33 -08001294 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001295 region->vm_flags = vm_flags;
1296 region->vm_pgoff = pgoff;
1297
Rik van Riel5beb4932010-03-05 13:42:07 -08001298 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001299 vma->vm_flags = vm_flags;
1300 vma->vm_pgoff = pgoff;
1301
1302 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001303 region->vm_file = get_file(file);
1304 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001305 }
1306
1307 down_write(&nommu_region_sem);
1308
1309 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001311 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * - shared mappings on character devices and memory backed files are
1313 * permitted to overlap inexactly as far as we are concerned for in
1314 * these cases, sharing is handled in the driver or filesystem rather
1315 * than here
1316 */
1317 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001318 struct vm_region *pregion;
1319 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
David Howells8feae132009-01-08 12:04:47 +00001321 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1322 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001323
David Howells8feae132009-01-08 12:04:47 +00001324 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1325 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
David Howells8feae132009-01-08 12:04:47 +00001327 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 continue;
1329
1330 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001331 if (file_inode(pregion->vm_file) !=
1332 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 continue;
1334
David Howells8feae132009-01-08 12:04:47 +00001335 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 continue;
1337
David Howells8feae132009-01-08 12:04:47 +00001338 rpglen = pregion->vm_end - pregion->vm_start;
1339 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1340 rpgend = pregion->vm_pgoff + rpglen;
1341 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 continue;
1343
David Howells8feae132009-01-08 12:04:47 +00001344 /* handle inexactly overlapping matches between
1345 * mappings */
1346 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1347 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1348 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1350 goto sharing_violation;
1351 continue;
1352 }
1353
David Howells8feae132009-01-08 12:04:47 +00001354 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001355 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001356 vma->vm_region = pregion;
1357 start = pregion->vm_start;
1358 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1359 vma->vm_start = start;
1360 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
David Howells8feae132009-01-08 12:04:47 +00001362 if (pregion->vm_flags & VM_MAPPED_COPY) {
1363 kdebug("share copy");
1364 vma->vm_flags |= VM_MAPPED_COPY;
1365 } else {
1366 kdebug("share mmap");
1367 ret = do_mmap_shared_file(vma);
1368 if (ret < 0) {
1369 vma->vm_region = NULL;
1370 vma->vm_start = 0;
1371 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001372 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001373 pregion = NULL;
1374 goto error_just_free;
1375 }
1376 }
1377 fput(region->vm_file);
1378 kmem_cache_free(vm_region_jar, region);
1379 region = pregion;
1380 result = start;
1381 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* obtain the address at which to make a shared mapping
1385 * - this is the hook for quasi-memory character devices to
1386 * tell us the location of a shared mapping
1387 */
David Howells645d83c2009-09-24 15:13:10 +01001388 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 addr = file->f_op->get_unmapped_area(file, addr, len,
1390 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001391 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001393 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001394 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /* the driver refused to tell us where to site
1397 * the mapping so we'll have to attempt to copy
1398 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001399 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001401 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001404 } else {
1405 vma->vm_start = region->vm_start = addr;
1406 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 }
1409 }
1410
David Howells8feae132009-01-08 12:04:47 +00001411 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
David Howells645d83c2009-09-24 15:13:10 +01001413 /* set up the mapping
1414 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001417 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 else
David Howells645d83c2009-09-24 15:13:10 +01001419 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001421 goto error_just_free;
1422 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001423
Jie Zhangea637632009-12-14 18:00:02 -08001424 /* clear anonymous mappings that don't ask for uninitialized data */
1425 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1426 memset((void *)region->vm_start, 0,
1427 region->vm_end - region->vm_start);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001430 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 current->mm->total_vm += len >> PAGE_SHIFT;
1433
David Howells8feae132009-01-08 12:04:47 +00001434share:
1435 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Mike Frysingercfe79c02010-01-06 17:23:23 +00001437 /* we flush the region from the icache only when the first executable
1438 * mapping of it is made */
1439 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1440 flush_icache_range(region->vm_start, region->vm_end);
1441 region->vm_icache_flushed = true;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Mike Frysingercfe79c02010-01-06 17:23:23 +00001444 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
David Howells8feae132009-01-08 12:04:47 +00001446 kleave(" = %lx", result);
1447 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
David Howells8feae132009-01-08 12:04:47 +00001449error_just_free:
1450 up_write(&nommu_region_sem);
1451error:
David Howells89a86402009-10-30 13:13:26 +00001452 if (region->vm_file)
1453 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001454 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001455 if (vma->vm_file)
1456 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001457 kmem_cache_free(vm_area_cachep, vma);
1458 kleave(" = %d", ret);
1459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
David Howells8feae132009-01-08 12:04:47 +00001461sharing_violation:
1462 up_write(&nommu_region_sem);
1463 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1464 ret = -EINVAL;
1465 goto error;
1466
1467error_getting_vma:
1468 kmem_cache_free(vm_region_jar, region);
1469 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1470 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001472 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -ENOMEM;
1474
David Howells8feae132009-01-08 12:04:47 +00001475error_getting_region:
1476 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1477 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001479 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -ENOMEM;
1481}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001482
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001483SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1484 unsigned long, prot, unsigned long, flags,
1485 unsigned long, fd, unsigned long, pgoff)
1486{
1487 struct file *file = NULL;
1488 unsigned long retval = -EBADF;
1489
Al Viro120a7952010-10-30 02:54:44 -04001490 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001491 if (!(flags & MAP_ANONYMOUS)) {
1492 file = fget(fd);
1493 if (!file)
1494 goto out;
1495 }
1496
1497 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1498
Greg Ungererad1ed292012-06-04 14:29:59 +10001499 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001500
1501 if (file)
1502 fput(file);
1503out:
1504 return retval;
1505}
1506
Christoph Hellwiga4679372010-03-10 15:21:15 -08001507#ifdef __ARCH_WANT_SYS_OLD_MMAP
1508struct mmap_arg_struct {
1509 unsigned long addr;
1510 unsigned long len;
1511 unsigned long prot;
1512 unsigned long flags;
1513 unsigned long fd;
1514 unsigned long offset;
1515};
1516
1517SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1518{
1519 struct mmap_arg_struct a;
1520
1521 if (copy_from_user(&a, arg, sizeof(a)))
1522 return -EFAULT;
1523 if (a.offset & ~PAGE_MASK)
1524 return -EINVAL;
1525
1526 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1527 a.offset >> PAGE_SHIFT);
1528}
1529#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/*
David Howells8feae132009-01-08 12:04:47 +00001532 * split a vma into two pieces at address 'addr', a new vma is allocated either
1533 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
David Howells8feae132009-01-08 12:04:47 +00001535int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1536 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
David Howells8feae132009-01-08 12:04:47 +00001538 struct vm_area_struct *new;
1539 struct vm_region *region;
1540 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
David Howells8feae132009-01-08 12:04:47 +00001542 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
David Howells779c1022010-01-15 17:01:34 -08001544 /* we're only permitted to split anonymous regions (these should have
1545 * only a single usage on the region) */
1546 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001547 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
David Howells8feae132009-01-08 12:04:47 +00001549 if (mm->map_count >= sysctl_max_map_count)
1550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David Howells8feae132009-01-08 12:04:47 +00001552 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1553 if (!region)
1554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
David Howells8feae132009-01-08 12:04:47 +00001556 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1557 if (!new) {
1558 kmem_cache_free(vm_region_jar, region);
1559 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
David Howells8feae132009-01-08 12:04:47 +00001561
1562 /* most fields are the same, copy all, and then fixup */
1563 *new = *vma;
1564 *region = *vma->vm_region;
1565 new->vm_region = region;
1566
1567 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1568
1569 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001570 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001571 } else {
1572 region->vm_start = new->vm_start = addr;
1573 region->vm_pgoff = new->vm_pgoff += npages;
1574 }
1575
1576 if (new->vm_ops && new->vm_ops->open)
1577 new->vm_ops->open(new);
1578
1579 delete_vma_from_mm(vma);
1580 down_write(&nommu_region_sem);
1581 delete_nommu_region(vma->vm_region);
1582 if (new_below) {
1583 vma->vm_region->vm_start = vma->vm_start = addr;
1584 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1585 } else {
1586 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001587 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001588 }
1589 add_nommu_region(vma->vm_region);
1590 add_nommu_region(new->vm_region);
1591 up_write(&nommu_region_sem);
1592 add_vma_to_mm(mm, vma);
1593 add_vma_to_mm(mm, new);
1594 return 0;
1595}
1596
1597/*
1598 * shrink a VMA by removing the specified chunk from either the beginning or
1599 * the end
1600 */
1601static int shrink_vma(struct mm_struct *mm,
1602 struct vm_area_struct *vma,
1603 unsigned long from, unsigned long to)
1604{
1605 struct vm_region *region;
1606
1607 kenter("");
1608
1609 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1610 * and list */
1611 delete_vma_from_mm(vma);
1612 if (from > vma->vm_start)
1613 vma->vm_end = from;
1614 else
1615 vma->vm_start = to;
1616 add_vma_to_mm(mm, vma);
1617
1618 /* cut the backing region down to size */
1619 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001620 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001621
1622 down_write(&nommu_region_sem);
1623 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001624 if (from > region->vm_start) {
1625 to = region->vm_top;
1626 region->vm_top = region->vm_end = from;
1627 } else {
David Howells8feae132009-01-08 12:04:47 +00001628 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001629 }
David Howells8feae132009-01-08 12:04:47 +00001630 add_nommu_region(region);
1631 up_write(&nommu_region_sem);
1632
1633 free_page_series(from, to);
1634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
David Howells30340972006-09-27 01:50:20 -07001637/*
1638 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001639 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1640 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001641 */
David Howells8feae132009-01-08 12:04:47 +00001642int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
David Howells8feae132009-01-08 12:04:47 +00001644 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001645 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001646 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
David Howells8feae132009-01-08 12:04:47 +00001648 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Bob Liuf67d9b12011-05-24 17:12:56 -07001650 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001651 if (len == 0)
1652 return -EINVAL;
1653
Bob Liuf67d9b12011-05-24 17:12:56 -07001654 end = start + len;
1655
David Howells8feae132009-01-08 12:04:47 +00001656 /* find the first potentially overlapping VMA */
1657 vma = find_vma(mm, start);
1658 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001659 static int limit = 0;
1660 if (limit < 5) {
1661 printk(KERN_WARNING
1662 "munmap of memory not mmapped by process %d"
1663 " (%s): 0x%lx-0x%lx\n",
1664 current->pid, current->comm,
1665 start, start + len - 1);
1666 limit++;
1667 }
David Howells8feae132009-01-08 12:04:47 +00001668 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
David Howells8feae132009-01-08 12:04:47 +00001671 /* we're allowed to split an anonymous VMA but not a file-backed one */
1672 if (vma->vm_file) {
1673 do {
1674 if (start > vma->vm_start) {
1675 kleave(" = -EINVAL [miss]");
1676 return -EINVAL;
1677 }
1678 if (end == vma->vm_end)
1679 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001680 vma = vma->vm_next;
1681 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001682 kleave(" = -EINVAL [split file]");
1683 return -EINVAL;
1684 } else {
1685 /* the chunk must be a subset of the VMA found */
1686 if (start == vma->vm_start && end == vma->vm_end)
1687 goto erase_whole_vma;
1688 if (start < vma->vm_start || end > vma->vm_end) {
1689 kleave(" = -EINVAL [superset]");
1690 return -EINVAL;
1691 }
1692 if (start & ~PAGE_MASK) {
1693 kleave(" = -EINVAL [unaligned start]");
1694 return -EINVAL;
1695 }
1696 if (end != vma->vm_end && end & ~PAGE_MASK) {
1697 kleave(" = -EINVAL [unaligned split]");
1698 return -EINVAL;
1699 }
1700 if (start != vma->vm_start && end != vma->vm_end) {
1701 ret = split_vma(mm, vma, start, 1);
1702 if (ret < 0) {
1703 kleave(" = %d [split]", ret);
1704 return ret;
1705 }
1706 }
1707 return shrink_vma(mm, vma, start, end);
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
David Howells8feae132009-01-08 12:04:47 +00001710erase_whole_vma:
1711 delete_vma_from_mm(vma);
1712 delete_vma(mm, vma);
1713 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715}
Paul Mundtb5073172007-07-21 04:37:25 -07001716EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Al Virobfce2812012-04-20 21:57:04 -04001718int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001719{
Al Virobfce2812012-04-20 21:57:04 -04001720 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001721 int ret;
David Howells30340972006-09-27 01:50:20 -07001722
1723 down_write(&mm->mmap_sem);
1724 ret = do_munmap(mm, addr, len);
1725 up_write(&mm->mmap_sem);
1726 return ret;
1727}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001728EXPORT_SYMBOL(vm_munmap);
1729
1730SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1731{
Al Virobfce2812012-04-20 21:57:04 -04001732 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001733}
David Howells30340972006-09-27 01:50:20 -07001734
1735/*
David Howells8feae132009-01-08 12:04:47 +00001736 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001737 */
David Howells8feae132009-01-08 12:04:47 +00001738void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
David Howells8feae132009-01-08 12:04:47 +00001740 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
David Howells8feae132009-01-08 12:04:47 +00001742 if (!mm)
1743 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
David Howells8feae132009-01-08 12:04:47 +00001745 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
David Howells8feae132009-01-08 12:04:47 +00001747 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Howells8feae132009-01-08 12:04:47 +00001749 while ((vma = mm->mmap)) {
1750 mm->mmap = vma->vm_next;
1751 delete_vma_from_mm(vma);
1752 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001753 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
David Howells8feae132009-01-08 12:04:47 +00001755
1756 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07001759unsigned long vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 return -ENOMEM;
1762}
1763
1764/*
David Howells6fa5f802006-09-27 01:50:21 -07001765 * expand (or shrink) an existing mapping, potentially moving it at the same
1766 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 *
David Howells6fa5f802006-09-27 01:50:21 -07001768 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001769 * as long as it stays within the region allocated by do_mmap_private() and the
1770 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
David Howells6fa5f802006-09-27 01:50:21 -07001772 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
1774unsigned long do_mremap(unsigned long addr,
1775 unsigned long old_len, unsigned long new_len,
1776 unsigned long flags, unsigned long new_addr)
1777{
David Howells6fa5f802006-09-27 01:50:21 -07001778 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001781 old_len = PAGE_ALIGN(old_len);
1782 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001783 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return (unsigned long) -EINVAL;
1785
David Howells8feae132009-01-08 12:04:47 +00001786 if (addr & ~PAGE_MASK)
1787 return -EINVAL;
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (flags & MREMAP_FIXED && new_addr != addr)
1790 return (unsigned long) -EINVAL;
1791
David Howells8feae132009-01-08 12:04:47 +00001792 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001793 if (!vma)
1794 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
David Howells6fa5f802006-09-27 01:50:21 -07001796 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return (unsigned long) -EFAULT;
1798
David Howells6fa5f802006-09-27 01:50:21 -07001799 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return (unsigned long) -EPERM;
1801
David Howells8feae132009-01-08 12:04:47 +00001802 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return (unsigned long) -ENOMEM;
1804
1805 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001806 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001807 return vma->vm_start;
1808}
Paul Mundtb5073172007-07-21 04:37:25 -07001809EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001810
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001811SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1812 unsigned long, new_len, unsigned long, flags,
1813 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001814{
1815 unsigned long ret;
1816
1817 down_write(&current->mm->mmap_sem);
1818 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1819 up_write(&current->mm->mmap_sem);
1820 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Michel Lespinasse240aade2013-02-22 16:35:56 -08001823struct page *follow_page_mask(struct vm_area_struct *vma,
1824 unsigned long address, unsigned int flags,
1825 unsigned int *page_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Michel Lespinasse240aade2013-02-22 16:35:56 -08001827 *page_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return NULL;
1829}
1830
Bob Liu8f3b1322011-07-08 15:39:46 -07001831int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1832 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Bob Liu8f3b1322011-07-08 15:39:46 -07001834 if (addr != (pfn << PAGE_SHIFT))
1835 return -EINVAL;
1836
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001837 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
Luke Yang22c4af42006-07-14 00:24:09 -07001840EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001842int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1843{
1844 unsigned long pfn = start >> PAGE_SHIFT;
1845 unsigned long vm_len = vma->vm_end - vma->vm_start;
1846
1847 pfn += vma->vm_pgoff;
1848 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1849}
1850EXPORT_SYMBOL(vm_iomap_memory);
1851
Paul Mundtf905bc42008-02-04 22:29:59 -08001852int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1853 unsigned long pgoff)
1854{
1855 unsigned int size = vma->vm_end - vma->vm_start;
1856
1857 if (!(vma->vm_flags & VM_USERMAP))
1858 return -EINVAL;
1859
1860 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1861 vma->vm_end = vma->vm_start + size;
1862
1863 return 0;
1864}
1865EXPORT_SYMBOL(remap_vmalloc_range);
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1868 unsigned long len, unsigned long pgoff, unsigned long flags)
1869{
1870 return -ENOMEM;
1871}
1872
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001873void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877void unmap_mapping_range(struct address_space *mapping,
1878 loff_t const holebegin, loff_t const holelen,
1879 int even_cows)
1880{
1881}
Luke Yang22c4af42006-07-14 00:24:09 -07001882EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884/*
1885 * Check that a process has enough memory to allocate a new virtual
1886 * mapping. 0 means there is enough memory for the allocation to
1887 * succeed and -ENOMEM implies there is not.
1888 *
1889 * We currently support three overcommit policies, which are set via the
1890 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1891 *
1892 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1893 * Additional code 2002 Jul 20 by Robert Love.
1894 *
1895 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1896 *
1897 * Note this is a helper function intended to be used by LSMs which
1898 * wish to use this logic.
1899 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001900int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001902 unsigned long free, allowed, reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 vm_acct_memory(pages);
1905
1906 /*
1907 * Sometimes we want to use more memory than we have
1908 */
1909 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1910 return 0;
1911
1912 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -07001913 free = global_page_state(NR_FREE_PAGES);
1914 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Dmitry Finkc15bef32011-07-25 17:12:19 -07001916 /*
1917 * shmem pages shouldn't be counted as free in this
1918 * case, they can't be purged, only swapped out, and
1919 * that won't affect the overall amount of available
1920 * memory in the system.
1921 */
1922 free -= global_page_state(NR_SHMEM);
1923
Shaohua Liec8acf22013-02-22 16:34:38 -08001924 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /*
1927 * Any slabs which are created with the
1928 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1929 * which are reclaimable, under pressure. The dentry
1930 * cache and most inode caches should fall into this
1931 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001932 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -07001935 * Leave reserved pages. The pages are not for anonymous pages.
1936 */
1937 if (free <= totalreserve_pages)
1938 goto error;
1939 else
1940 free -= totalreserve_pages;
1941
1942 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001943 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 */
1945 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001946 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 if (free > pages)
1949 return 0;
1950
Hideo AOKId5ddc792006-04-10 22:53:01 -07001951 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
1954 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1955 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001956 * Reserve some 3% for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
1958 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001959 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 allowed += total_swap_pages;
1961
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001962 /*
1963 * Don't let a single process grow so big a user can't recover
1964 */
1965 if (mm) {
1966 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1967 allowed -= min(mm->total_vm / 32, reserve);
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001970 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001972
Hideo AOKId5ddc792006-04-10 22:53:01 -07001973error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 vm_unacct_memory(pages);
1975
1976 return -ENOMEM;
1977}
1978
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001979int in_gate_area_no_mm(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 return 0;
1982}
David Howellsb0e15192006-01-06 00:11:42 -08001983
Nick Piggind0217ac2007-07-19 01:47:03 -07001984int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001985{
1986 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001987 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001988}
Paul Mundtb5073172007-07-21 04:37:25 -07001989EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001990
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001991int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1992 unsigned long size, pgoff_t pgoff)
1993{
1994 BUG();
1995 return 0;
1996}
1997EXPORT_SYMBOL(generic_file_remap_pages);
1998
Mike Frysingerf55f1992011-03-29 14:05:12 +01001999static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2000 unsigned long addr, void *buf, int len, int write)
David Howells0ec76a12006-09-27 01:50:15 -07002001{
David Howells0ec76a12006-09-27 01:50:15 -07002002 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07002003
2004 down_read(&mm->mmap_sem);
2005
2006 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07002007 vma = find_vma(mm, addr);
2008 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07002009 /* don't overrun this mapping */
2010 if (addr + len >= vma->vm_end)
2011 len = vma->vm_end - addr;
2012
2013 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07002014 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00002015 copy_to_user_page(vma, NULL, addr,
2016 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07002017 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00002018 copy_from_user_page(vma, NULL, addr,
2019 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07002020 else
2021 len = 0;
2022 } else {
2023 len = 0;
2024 }
2025
2026 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01002027
2028 return len;
2029}
2030
2031/**
2032 * @access_remote_vm - access another process' address space
2033 * @mm: the mm_struct of the target address space
2034 * @addr: start address to access
2035 * @buf: source or destination buffer
2036 * @len: number of bytes to transfer
2037 * @write: whether the access is a write
2038 *
2039 * The caller must hold a reference on @mm.
2040 */
2041int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2042 void *buf, int len, int write)
2043{
2044 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2045}
2046
2047/*
2048 * Access another process' address space.
2049 * - source/target buffer must be kernel space
2050 */
2051int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2052{
2053 struct mm_struct *mm;
2054
2055 if (addr + len < addr)
2056 return 0;
2057
2058 mm = get_task_mm(tsk);
2059 if (!mm)
2060 return 0;
2061
2062 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2063
David Howells0ec76a12006-09-27 01:50:15 -07002064 mmput(mm);
2065 return len;
2066}
David Howells7e660872010-01-15 17:01:39 -08002067
2068/**
2069 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2070 * @inode: The inode to check
2071 * @size: The current filesize of the inode
2072 * @newsize: The proposed filesize of the inode
2073 *
2074 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2075 * make sure that that any outstanding VMAs aren't broken and then shrink the
2076 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2077 * automatically grant mappings that are too large.
2078 */
2079int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2080 size_t newsize)
2081{
2082 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08002083 struct vm_region *region;
2084 pgoff_t low, high;
2085 size_t r_size, r_top;
2086
2087 low = newsize >> PAGE_SHIFT;
2088 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2089
2090 down_write(&nommu_region_sem);
David Howells918e5562012-02-23 13:50:35 +00002091 mutex_lock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002092
2093 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002094 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08002095 /* found one - only interested if it's shared out of the page
2096 * cache */
2097 if (vma->vm_flags & VM_SHARED) {
David Howells918e5562012-02-23 13:50:35 +00002098 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002099 up_write(&nommu_region_sem);
2100 return -ETXTBSY; /* not quite true, but near enough */
2101 }
2102 }
2103
2104 /* reduce any regions that overlap the dead zone - if in existence,
2105 * these will be pointed to by VMAs that don't overlap the dead zone
2106 *
2107 * we don't check for any regions that start beyond the EOF as there
2108 * shouldn't be any
2109 */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002110 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2111 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08002112 if (!(vma->vm_flags & VM_SHARED))
2113 continue;
2114
2115 region = vma->vm_region;
2116 r_size = region->vm_top - region->vm_start;
2117 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2118
2119 if (r_top > newsize) {
2120 region->vm_top -= r_top - newsize;
2121 if (region->vm_end > region->vm_top)
2122 region->vm_end = region->vm_top;
2123 }
2124 }
2125
David Howells918e5562012-02-23 13:50:35 +00002126 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002127 up_write(&nommu_region_sem);
2128 return 0;
2129}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07002130
2131/*
2132 * Initialise sysctl_user_reserve_kbytes.
2133 *
2134 * This is intended to prevent a user from starting a single memory hogging
2135 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2136 * mode.
2137 *
2138 * The default value is min(3% of free memory, 128MB)
2139 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2140 */
2141static int __meminit init_user_reserve(void)
2142{
2143 unsigned long free_kbytes;
2144
2145 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2146
2147 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2148 return 0;
2149}
2150module_init(init_user_reserve)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07002151
2152/*
2153 * Initialise sysctl_admin_reserve_kbytes.
2154 *
2155 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2156 * to log in and kill a memory hogging process.
2157 *
2158 * Systems with more than 256MB will reserve 8MB, enough to recover
2159 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2160 * only reserve 3% of free pages by default.
2161 */
2162static int __meminit init_admin_reserve(void)
2163{
2164 unsigned long free_kbytes;
2165
2166 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2167
2168 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2169 return 0;
2170}
2171module_init(init_admin_reserve)