blob: e19482533ce33b69a33e43bff6c26a72c81c4172 [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;
Hugh Dickins4266c972009-09-23 17:05:53 +010059unsigned long highest_memmap_pfn;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070060struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
62int sysctl_overcommit_ratio = 50; /* default is 50% */
Jerome Marchand49f0ce52014-01-21 15:49:14 -080063unsigned long sysctl_overcommit_kbytes __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064int 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David Howells8feae132009-01-08 12:04:47 +000089/* list of mapped, potentially shareable regions */
90static struct kmem_cache *vm_region_jar;
91struct rb_root nommu_region_tree = RB_ROOT;
92DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +040094const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * Return the total memory allocated for this pointer, not
99 * just what the caller asked for.
100 *
101 * Doesn't have to be accurate, i.e. may have races.
102 */
103unsigned int kobjsize(const void *objp)
104{
105 struct page *page;
106
Michael Hennerich4016a132008-04-28 02:13:38 -0700107 /*
108 * If the object we have should not have ksize performed on it,
109 * return size of 0
110 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900111 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700112 return 0;
113
114 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700115
116 /*
117 * If the allocator sets PageSlab, we know the pointer came from
118 * kmalloc().
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (PageSlab(page))
121 return ksize(objp);
122
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700123 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000124 * If it's not a compound page, see if we have a matching VMA
125 * region. This test is intentionally done in reverse order,
126 * so if there's no VMA, we still fall through and hand back
127 * PAGE_SIZE for 0-order pages.
128 */
129 if (!PageCompound(page)) {
130 struct vm_area_struct *vma;
131
132 vma = find_vma(current->mm, (unsigned long)objp);
133 if (vma)
134 return vma->vm_end - vma->vm_start;
135 }
136
137 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700138 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900139 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700140 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900141 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Michel Lespinasse28a35712013-02-22 16:35:55 -0800144long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
145 unsigned long start, unsigned long nr_pages,
146 unsigned int foll_flags, struct page **pages,
147 struct vm_area_struct **vmas, int *nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700149 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700150 unsigned long vm_flags;
151 int i;
152
153 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700154 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700155 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700156 vm_flags = (foll_flags & FOLL_WRITE) ?
157 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
158 vm_flags &= (foll_flags & FOLL_FORCE) ?
159 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Peter Zijlstra9d737772009-06-25 11:58:55 +0200161 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000162 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700163 if (!vma)
164 goto finish_or_fault;
165
166 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700167 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
168 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700169 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (pages) {
172 pages[i] = virt_to_page(start);
173 if (pages[i])
174 page_cache_get(pages[i]);
175 }
176 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700177 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000178 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
David Howells7b4d5b82006-09-27 01:50:18 -0700180
181 return i;
182
183finish_or_fault:
184 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
Nick Pigginb291f002008-10-18 20:26:44 -0700186
Nick Pigginb291f002008-10-18 20:26:44 -0700187/*
188 * get a list of pages in an address range belonging to the specified process
189 * and indicate the VMA that covers each page
190 * - this is potentially dodgy as we may end incrementing the page count of a
191 * slab page or a secondary page from a compound page
192 * - don't permit access to VMAs that don't support it, such as I/O mappings
193 */
Michel Lespinasse28a35712013-02-22 16:35:55 -0800194long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
195 unsigned long start, unsigned long nr_pages,
196 int write, int force, struct page **pages,
197 struct vm_area_struct **vmas)
Nick Pigginb291f002008-10-18 20:26:44 -0700198{
199 int flags = 0;
200
201 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700202 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700203 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700204 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700205
Michel Lespinasse53a77062011-01-13 15:46:14 -0800206 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
207 NULL);
Nick Pigginb291f002008-10-18 20:26:44 -0700208}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000209EXPORT_SYMBOL(get_user_pages);
210
Paul Mundtdfc2f912009-06-26 04:31:57 +0900211/**
212 * follow_pfn - look up PFN at a user virtual address
213 * @vma: memory mapping
214 * @address: user virtual address
215 * @pfn: location to store found PFN
216 *
217 * Only IO mappings and raw PFN mappings are allowed.
218 *
219 * Returns zero and the pfn at @pfn on success, -ve otherwise.
220 */
221int follow_pfn(struct vm_area_struct *vma, unsigned long address,
222 unsigned long *pfn)
223{
224 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
225 return -EINVAL;
226
227 *pfn = address >> PAGE_SHIFT;
228 return 0;
229}
230EXPORT_SYMBOL(follow_pfn);
231
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700232LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800234void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 kfree(addr);
237}
Paul Mundtb5073172007-07-21 04:37:25 -0700238EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Al Virodd0fc662005-10-07 07:46:04 +0100240void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200243 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
244 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Nick Piggin84097512006-03-22 00:08:34 -0800246 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
Paul Mundtb5073172007-07-21 04:37:25 -0700248EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Paul Mundtf905bc42008-02-04 22:29:59 -0800250void *vmalloc_user(unsigned long size)
251{
252 void *ret;
253
254 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
255 PAGE_KERNEL);
256 if (ret) {
257 struct vm_area_struct *vma;
258
259 down_write(&current->mm->mmap_sem);
260 vma = find_vma(current->mm, (unsigned long)ret);
261 if (vma)
262 vma->vm_flags |= VM_USERMAP;
263 up_write(&current->mm->mmap_sem);
264 }
265
266 return ret;
267}
268EXPORT_SYMBOL(vmalloc_user);
269
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800270struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 return virt_to_page(addr);
273}
Paul Mundtb5073172007-07-21 04:37:25 -0700274EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800276unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 return page_to_pfn(virt_to_page(addr));
279}
Paul Mundtb5073172007-07-21 04:37:25 -0700280EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282long vread(char *buf, char *addr, unsigned long count)
283{
Chen Gang9bde9162013-07-03 15:02:36 -0700284 /* Don't allow overflow */
285 if ((unsigned long) buf + count < count)
286 count = -(unsigned long) buf;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 memcpy(buf, addr, count);
289 return count;
290}
291
292long vwrite(char *buf, char *addr, unsigned long count)
293{
294 /* Don't allow overflow */
295 if ((unsigned long) addr + count < count)
296 count = -(unsigned long) addr;
297
298 memcpy(addr, buf, count);
299 return(count);
300}
301
302/*
303 * vmalloc - allocate virtually continguos memory
304 *
305 * @size: allocation size
306 *
307 * Allocate enough pages to cover @size from the page level
308 * allocator and map them into continguos kernel virtual space.
309 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200310 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * use __vmalloc() instead.
312 */
313void *vmalloc(unsigned long size)
314{
315 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
316}
Andrew Mortonf6138882006-02-28 16:59:18 -0800317EXPORT_SYMBOL(vmalloc);
318
Dave Younge1ca7782010-10-26 14:22:06 -0700319/*
320 * vzalloc - allocate virtually continguos memory with zero fill
321 *
322 * @size: allocation size
323 *
324 * Allocate enough pages to cover @size from the page level
325 * allocator and map them into continguos kernel virtual space.
326 * The memory allocated is set to zero.
327 *
328 * For tight control over page level allocator and protection flags
329 * use __vmalloc() instead.
330 */
331void *vzalloc(unsigned long size)
332{
333 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
334 PAGE_KERNEL);
335}
336EXPORT_SYMBOL(vzalloc);
337
338/**
339 * vmalloc_node - allocate memory on a specific node
340 * @size: allocation size
341 * @node: numa node
342 *
343 * Allocate enough pages to cover @size from the page level
344 * allocator and map them into contiguous kernel virtual space.
345 *
346 * For tight control over page level allocator and protection flags
347 * use __vmalloc() instead.
348 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800349void *vmalloc_node(unsigned long size, int node)
350{
351 return vmalloc(size);
352}
Paul Mundt9a14f652010-12-24 11:50:34 +0900353EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700354
355/**
356 * vzalloc_node - allocate memory on a specific node with zero fill
357 * @size: allocation size
358 * @node: numa node
359 *
360 * Allocate enough pages to cover @size from the page level
361 * allocator and map them into contiguous kernel virtual space.
362 * The memory allocated is set to zero.
363 *
364 * For tight control over page level allocator and protection flags
365 * use __vmalloc() instead.
366 */
367void *vzalloc_node(unsigned long size, int node)
368{
369 return vzalloc(size);
370}
371EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Paul Mundt1af446e2008-08-04 16:01:47 +0900373#ifndef PAGE_KERNEL_EXEC
374# define PAGE_KERNEL_EXEC PAGE_KERNEL
375#endif
376
377/**
378 * vmalloc_exec - allocate virtually contiguous, executable memory
379 * @size: allocation size
380 *
381 * Kernel-internal function to allocate enough pages to cover @size
382 * the page level allocator and map them into contiguous and
383 * executable kernel virtual space.
384 *
385 * For tight control over page level allocator and protection flags
386 * use __vmalloc() instead.
387 */
388
389void *vmalloc_exec(unsigned long size)
390{
391 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
392}
393
Paul Mundtb5073172007-07-21 04:37:25 -0700394/**
395 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * @size: allocation size
397 *
398 * Allocate enough 32bit PA addressable pages to cover @size from the
399 * page level allocator and map them into continguos kernel virtual space.
400 */
401void *vmalloc_32(unsigned long size)
402{
403 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
404}
Paul Mundtb5073172007-07-21 04:37:25 -0700405EXPORT_SYMBOL(vmalloc_32);
406
407/**
408 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
409 * @size: allocation size
410 *
411 * The resulting memory area is 32bit addressable and zeroed so it can be
412 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800413 *
414 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
415 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700416 */
417void *vmalloc_32_user(unsigned long size)
418{
Paul Mundtf905bc42008-02-04 22:29:59 -0800419 /*
420 * We'll have to sort out the ZONE_DMA bits for 64-bit,
421 * but for now this can simply use vmalloc_user() directly.
422 */
423 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700424}
425EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
428{
429 BUG();
430 return NULL;
431}
Paul Mundtb5073172007-07-21 04:37:25 -0700432EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800434void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 BUG();
437}
Paul Mundtb5073172007-07-21 04:37:25 -0700438EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Paul Mundteb6434d2009-01-21 17:45:47 +0900440void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
441{
442 BUG();
443 return NULL;
444}
445EXPORT_SYMBOL(vm_map_ram);
446
447void vm_unmap_ram(const void *mem, unsigned int count)
448{
449 BUG();
450}
451EXPORT_SYMBOL(vm_unmap_ram);
452
453void vm_unmap_aliases(void)
454{
455}
456EXPORT_SYMBOL_GPL(vm_unmap_aliases);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700459 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
460 * have one.
461 */
462void __attribute__((weak)) vmalloc_sync_all(void)
463{
464}
465
Paul Mundt29c185e2010-12-24 12:08:30 +0900466/**
467 * alloc_vm_area - allocate a range of kernel address space
468 * @size: size of the area
469 *
470 * Returns: NULL on failure, vm_struct on success
471 *
472 * This function reserves a range of kernel address space, and
473 * allocates pagetables to map that range. No actual mappings
474 * are created. If the kernel address space is not shared
475 * between processes, it syncs the pagetable across all
476 * processes.
477 */
David Vrabelcd129092011-09-29 16:53:32 +0100478struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900479{
480 BUG();
481 return NULL;
482}
483EXPORT_SYMBOL_GPL(alloc_vm_area);
484
485void free_vm_area(struct vm_struct *area)
486{
487 BUG();
488}
489EXPORT_SYMBOL_GPL(free_vm_area);
490
Paul Mundtb5073172007-07-21 04:37:25 -0700491int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
492 struct page *page)
493{
494 return -EINVAL;
495}
496EXPORT_SYMBOL(vm_insert_page);
497
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * sys_brk() for the most part doesn't need the global kernel
500 * lock, except when an application is doing something nasty
501 * like trying to un-brk an area that has already been mapped
502 * to a regular file. in this case, the unmapping will need
503 * to invoke file system routines that need the global lock.
504 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100505SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct mm_struct *mm = current->mm;
508
509 if (brk < mm->start_brk || brk > mm->context.end_brk)
510 return mm->brk;
511
512 if (mm->brk == brk)
513 return mm->brk;
514
515 /*
516 * Always allow shrinking brk
517 */
518 if (brk <= mm->brk) {
519 mm->brk = brk;
520 return brk;
521 }
522
523 /*
524 * Ok, looks good - let it rip.
525 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000526 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return mm->brk = brk;
528}
529
David Howells8feae132009-01-08 12:04:47 +0000530/*
531 * initialise the VMA and region record slabs
532 */
533void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700535 int ret;
536
537 ret = percpu_counter_init(&vm_committed_as, 0);
538 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700539 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
David Howells8feae132009-01-08 12:04:47 +0000542/*
543 * validate the region tree
544 * - the caller must hold the region lock
545 */
546#ifdef CONFIG_DEBUG_NOMMU_REGIONS
547static noinline void validate_nommu_regions(void)
548{
549 struct vm_region *region, *last;
550 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
David Howells8feae132009-01-08 12:04:47 +0000552 lastp = rb_first(&nommu_region_tree);
553 if (!lastp)
554 return;
555
556 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700557 BUG_ON(unlikely(last->vm_end <= last->vm_start));
558 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000559
560 while ((p = rb_next(lastp))) {
561 region = rb_entry(p, struct vm_region, vm_rb);
562 last = rb_entry(lastp, struct vm_region, vm_rb);
563
David Howells33e5d7692009-04-02 16:56:32 -0700564 BUG_ON(unlikely(region->vm_end <= region->vm_start));
565 BUG_ON(unlikely(region->vm_top < region->vm_end));
566 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000567
568 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570}
David Howells8feae132009-01-08 12:04:47 +0000571#else
David Howells33e5d7692009-04-02 16:56:32 -0700572static void validate_nommu_regions(void)
573{
574}
David Howells8feae132009-01-08 12:04:47 +0000575#endif
576
577/*
578 * add a region into the global tree
579 */
580static void add_nommu_region(struct vm_region *region)
581{
582 struct vm_region *pregion;
583 struct rb_node **p, *parent;
584
585 validate_nommu_regions();
586
David Howells8feae132009-01-08 12:04:47 +0000587 parent = NULL;
588 p = &nommu_region_tree.rb_node;
589 while (*p) {
590 parent = *p;
591 pregion = rb_entry(parent, struct vm_region, vm_rb);
592 if (region->vm_start < pregion->vm_start)
593 p = &(*p)->rb_left;
594 else if (region->vm_start > pregion->vm_start)
595 p = &(*p)->rb_right;
596 else if (pregion == region)
597 return;
598 else
599 BUG();
600 }
601
602 rb_link_node(&region->vm_rb, parent, p);
603 rb_insert_color(&region->vm_rb, &nommu_region_tree);
604
605 validate_nommu_regions();
606}
607
608/*
609 * delete a region from the global tree
610 */
611static void delete_nommu_region(struct vm_region *region)
612{
613 BUG_ON(!nommu_region_tree.rb_node);
614
615 validate_nommu_regions();
616 rb_erase(&region->vm_rb, &nommu_region_tree);
617 validate_nommu_regions();
618}
619
620/*
621 * free a contiguous series of pages
622 */
623static void free_page_series(unsigned long from, unsigned long to)
624{
625 for (; from < to; from += PAGE_SIZE) {
626 struct page *page = virt_to_page(from);
627
628 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700629 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000630 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700631 kdebug("free page %p: refcount not one: %d",
632 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000633 put_page(page);
634 }
635}
636
637/*
638 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700639 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000640 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000641 * will equal vm_start
642 */
643static void __put_nommu_region(struct vm_region *region)
644 __releases(nommu_region_sem)
645{
David Howells1e2ae592010-01-15 17:01:33 -0800646 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000647
648 BUG_ON(!nommu_region_tree.rb_node);
649
David Howells1e2ae592010-01-15 17:01:33 -0800650 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000651 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000652 delete_nommu_region(region);
653 up_write(&nommu_region_sem);
654
655 if (region->vm_file)
656 fput(region->vm_file);
657
658 /* IO memory and memory shared directly out of the pagecache
659 * from ramfs/tmpfs mustn't be released here */
660 if (region->vm_flags & VM_MAPPED_COPY) {
661 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000662 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000663 }
664 kmem_cache_free(vm_region_jar, region);
665 } else {
666 up_write(&nommu_region_sem);
667 }
668}
669
670/*
671 * release a reference to a region
672 */
673static void put_nommu_region(struct vm_region *region)
674{
675 down_write(&nommu_region_sem);
676 __put_nommu_region(region);
677}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
David Howells30340972006-09-27 01:50:20 -0700679/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700680 * update protection on a vma
681 */
682static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
683{
684#ifdef CONFIG_MPU
685 struct mm_struct *mm = vma->vm_mm;
686 long start = vma->vm_start & PAGE_MASK;
687 while (start < vma->vm_end) {
688 protect_page(mm, start, flags);
689 start += PAGE_SIZE;
690 }
691 update_protections(mm);
692#endif
693}
694
695/*
David Howells30340972006-09-27 01:50:20 -0700696 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000697 * and tree and add to the address space's page tree also if not an anonymous
698 * page
David Howells30340972006-09-27 01:50:20 -0700699 * - should be called with mm->mmap_sem held writelocked
700 */
David Howells8feae132009-01-08 12:04:47 +0000701static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700702{
Namhyung Kim6038def2011-05-24 17:11:22 -0700703 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000704 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700705 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700706
David Howells8feae132009-01-08 12:04:47 +0000707 kenter(",%p", vma);
708
709 BUG_ON(!vma->vm_region);
710
711 mm->map_count++;
712 vma->vm_mm = mm;
713
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700714 protect_vma(vma, vma->vm_flags);
715
David Howells8feae132009-01-08 12:04:47 +0000716 /* add the VMA to the mapping */
717 if (vma->vm_file) {
718 mapping = vma->vm_file->f_mapping;
719
David Howells918e5562012-02-23 13:50:35 +0000720 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000721 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700722 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000723 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000724 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000725 }
726
727 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700728 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000729 p = &mm->mm_rb.rb_node;
730 while (*p) {
731 parent = *p;
732 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
733
734 /* sort by: start addr, end addr, VMA struct addr in that order
735 * (the latter is necessary as we may get identical VMAs) */
736 if (vma->vm_start < pvma->vm_start)
737 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700738 else if (vma->vm_start > pvma->vm_start) {
739 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000740 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700741 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000742 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700743 else if (vma->vm_end > pvma->vm_end) {
744 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000745 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700746 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000747 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700748 else if (vma > pvma) {
749 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000750 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700751 } else
David Howells8feae132009-01-08 12:04:47 +0000752 BUG();
753 }
754
755 rb_link_node(&vma->vm_rb, parent, p);
756 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
757
758 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700759 prev = NULL;
760 if (rb_prev)
761 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700762
Namhyung Kim6038def2011-05-24 17:11:22 -0700763 __vma_link_list(mm, vma, prev, parent);
David Howells8feae132009-01-08 12:04:47 +0000764}
765
766/*
767 * delete a VMA from its owning mm_struct and address space
768 */
769static void delete_vma_from_mm(struct vm_area_struct *vma)
770{
David Howells8feae132009-01-08 12:04:47 +0000771 struct address_space *mapping;
772 struct mm_struct *mm = vma->vm_mm;
773
774 kenter("%p", vma);
775
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700776 protect_vma(vma, 0);
777
David Howells8feae132009-01-08 12:04:47 +0000778 mm->map_count--;
779 if (mm->mmap_cache == vma)
780 mm->mmap_cache = NULL;
781
782 /* remove the VMA from the mapping */
783 if (vma->vm_file) {
784 mapping = vma->vm_file->f_mapping;
785
David Howells918e5562012-02-23 13:50:35 +0000786 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000787 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700788 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000789 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000790 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000791 }
792
793 /* remove from the MM's tree and list */
794 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700795
796 if (vma->vm_prev)
797 vma->vm_prev->vm_next = vma->vm_next;
798 else
799 mm->mmap = vma->vm_next;
800
801 if (vma->vm_next)
802 vma->vm_next->vm_prev = vma->vm_prev;
David Howells8feae132009-01-08 12:04:47 +0000803}
804
805/*
806 * destroy a VMA record
807 */
808static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
809{
810 kenter("%p", vma);
811 if (vma->vm_ops && vma->vm_ops->close)
812 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700813 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000814 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000815 put_nommu_region(vma->vm_region);
816 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700817}
818
819/*
820 * look up the first VMA in which addr resides, NULL if none
821 * - should be called with mm->mmap_sem at least held readlocked
822 */
823struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
824{
David Howells8feae132009-01-08 12:04:47 +0000825 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700826
David Howells8feae132009-01-08 12:04:47 +0000827 /* check the cache first */
Jan Stancekb6a9b7f2013-04-04 11:35:10 -0700828 vma = ACCESS_ONCE(mm->mmap_cache);
David Howells8feae132009-01-08 12:04:47 +0000829 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
830 return vma;
831
Namhyung Kime922c4c2011-05-24 17:11:24 -0700832 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000833 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700834 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000835 if (vma->vm_start > addr)
836 return NULL;
837 if (vma->vm_end > addr) {
838 mm->mmap_cache = vma;
839 return vma;
840 }
David Howells30340972006-09-27 01:50:20 -0700841 }
842
David Howells30340972006-09-27 01:50:20 -0700843 return NULL;
844}
845EXPORT_SYMBOL(find_vma);
846
847/*
David Howells930e6522006-09-27 01:50:22 -0700848 * find a VMA
849 * - we don't extend stack VMAs under NOMMU conditions
850 */
851struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
852{
David Howells7561e8c2010-03-25 16:48:38 +0000853 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700854}
855
David Howells8feae132009-01-08 12:04:47 +0000856/*
857 * expand a stack to a given address
858 * - not supported under NOMMU conditions
859 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700860int expand_stack(struct vm_area_struct *vma, unsigned long address)
861{
862 return -ENOMEM;
863}
864
David Howells930e6522006-09-27 01:50:22 -0700865/*
David Howells6fa5f802006-09-27 01:50:21 -0700866 * look up the first VMA exactly that exactly matches addr
867 * - should be called with mm->mmap_sem at least held readlocked
868 */
David Howells8feae132009-01-08 12:04:47 +0000869static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
870 unsigned long addr,
871 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000874 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
David Howells8feae132009-01-08 12:04:47 +0000876 /* check the cache first */
877 vma = mm->mmap_cache;
878 if (vma && vma->vm_start == addr && vma->vm_end == end)
879 return vma;
880
Namhyung Kime922c4c2011-05-24 17:11:24 -0700881 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000882 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700883 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000884 if (vma->vm_start < addr)
885 continue;
886 if (vma->vm_start > addr)
887 return NULL;
888 if (vma->vm_end == end) {
889 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return vma;
David Howells8feae132009-01-08 12:04:47 +0000891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 return NULL;
895}
896
David Howells30340972006-09-27 01:50:20 -0700897/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * determine whether a mapping should be permitted and, if so, what sort of
899 * mapping we're capable of supporting
900 */
901static int validate_mmap_request(struct file *file,
902 unsigned long addr,
903 unsigned long len,
904 unsigned long prot,
905 unsigned long flags,
906 unsigned long pgoff,
907 unsigned long *_capabilities)
908{
David Howells8feae132009-01-08 12:04:47 +0000909 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int ret;
911
912 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100913 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 printk(KERN_DEBUG
915 "%d: Can't do fixed-address/overlay mmap of RAM\n",
916 current->pid);
917 return -EINVAL;
918 }
919
920 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
921 (flags & MAP_TYPE) != MAP_SHARED)
922 return -EINVAL;
923
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000924 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EINVAL;
926
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000927 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000928 rlen = PAGE_ALIGN(len);
929 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000930 return -ENOMEM;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000933 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000934 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (file) {
937 /* validate file mapping requests */
938 struct address_space *mapping;
939
940 /* files must support mmap */
Al Viro72c2d532013-09-22 16:27:52 -0400941 if (!file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -ENODEV;
943
944 /* work out if what we've got could possibly be shared
945 * - we support chardevs that provide their own "memory"
946 * - we support files/blockdevs that are memory backed
947 */
948 mapping = file->f_mapping;
949 if (!mapping)
Al Viro496ad9a2013-01-23 17:07:38 -0500950 mapping = file_inode(file)->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 capabilities = 0;
953 if (mapping && mapping->backing_dev_info)
954 capabilities = mapping->backing_dev_info->capabilities;
955
956 if (!capabilities) {
957 /* no explicit capabilities set, so assume some
958 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500959 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 case S_IFREG:
961 case S_IFBLK:
962 capabilities = BDI_CAP_MAP_COPY;
963 break;
964
965 case S_IFCHR:
966 capabilities =
967 BDI_CAP_MAP_DIRECT |
968 BDI_CAP_READ_MAP |
969 BDI_CAP_WRITE_MAP;
970 break;
971
972 default:
973 return -EINVAL;
974 }
975 }
976
977 /* eliminate any capabilities that we can't support on this
978 * device */
979 if (!file->f_op->get_unmapped_area)
980 capabilities &= ~BDI_CAP_MAP_DIRECT;
981 if (!file->f_op->read)
982 capabilities &= ~BDI_CAP_MAP_COPY;
983
Graff Yang28d7a6a2009-08-18 14:11:17 -0700984 /* The file shall have been opened with read permission. */
985 if (!(file->f_mode & FMODE_READ))
986 return -EACCES;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (flags & MAP_SHARED) {
989 /* do checks for writing, appending and locking */
990 if ((prot & PROT_WRITE) &&
991 !(file->f_mode & FMODE_WRITE))
992 return -EACCES;
993
Al Viro496ad9a2013-01-23 17:07:38 -0500994 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 (file->f_mode & FMODE_WRITE))
996 return -EACCES;
997
Jeff Laytond7a06982014-03-10 09:54:15 -0400998 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -EAGAIN;
1000
1001 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1002 return -ENODEV;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* we mustn't privatise shared mappings */
1005 capabilities &= ~BDI_CAP_MAP_COPY;
1006 }
1007 else {
1008 /* we're going to read the file into private memory we
1009 * allocate */
1010 if (!(capabilities & BDI_CAP_MAP_COPY))
1011 return -ENODEV;
1012
1013 /* we don't permit a private writable mapping to be
1014 * shared with the backing device */
1015 if (prot & PROT_WRITE)
1016 capabilities &= ~BDI_CAP_MAP_DIRECT;
1017 }
1018
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001019 if (capabilities & BDI_CAP_MAP_DIRECT) {
1020 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1021 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1022 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1023 ) {
1024 capabilities &= ~BDI_CAP_MAP_DIRECT;
1025 if (flags & MAP_SHARED) {
1026 printk(KERN_WARNING
1027 "MAP_SHARED not completely supported on !MMU\n");
1028 return -EINVAL;
1029 }
1030 }
1031 }
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* handle executable mappings and implied executable
1034 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -08001035 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (prot & PROT_EXEC)
1037 return -EPERM;
1038 }
1039 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1040 /* handle implication of PROT_EXEC by PROT_READ */
1041 if (current->personality & READ_IMPLIES_EXEC) {
1042 if (capabilities & BDI_CAP_EXEC_MAP)
1043 prot |= PROT_EXEC;
1044 }
1045 }
1046 else if ((prot & PROT_READ) &&
1047 (prot & PROT_EXEC) &&
1048 !(capabilities & BDI_CAP_EXEC_MAP)
1049 ) {
1050 /* backing file is not executable, try to copy */
1051 capabilities &= ~BDI_CAP_MAP_DIRECT;
1052 }
1053 }
1054 else {
1055 /* anonymous mappings are always memory backed and can be
1056 * privately mapped
1057 */
1058 capabilities = BDI_CAP_MAP_COPY;
1059
1060 /* handle PROT_EXEC implication by PROT_READ */
1061 if ((prot & PROT_READ) &&
1062 (current->personality & READ_IMPLIES_EXEC))
1063 prot |= PROT_EXEC;
1064 }
1065
1066 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -04001067 ret = security_mmap_addr(addr);
1068 if (ret < 0)
1069 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* looks okay */
1072 *_capabilities = capabilities;
1073 return 0;
1074}
1075
1076/*
1077 * we've determined that we can make the mapping, now translate what we
1078 * now know into VMA flags
1079 */
1080static unsigned long determine_vm_flags(struct file *file,
1081 unsigned long prot,
1082 unsigned long flags,
1083 unsigned long capabilities)
1084{
1085 unsigned long vm_flags;
1086
1087 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* vm_flags |= mm->def_flags; */
1089
1090 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1091 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001092 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (file && !(prot & PROT_WRITE))
1094 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001095 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* overlay a shareable mapping on the backing device or inode
1097 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1098 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001099 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001101 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
1104 /* refuse to let anyone share private mappings with this process if
1105 * it's being traced - otherwise breakpoints set in it may interfere
1106 * with another untraced process
1107 */
Tejun Heoa288eec2011-06-17 16:50:37 +02001108 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 vm_flags &= ~VM_MAYSHARE;
1110
1111 return vm_flags;
1112}
1113
1114/*
David Howells8feae132009-01-08 12:04:47 +00001115 * set up a shared mapping on a file (the driver or filesystem provides and
1116 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
David Howells8feae132009-01-08 12:04:47 +00001118static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int ret;
1121
1122 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001123 if (ret == 0) {
1124 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001125 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (ret != -ENOSYS)
1128 return ret;
1129
David Howells3fa30462010-03-23 13:35:21 -07001130 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1131 * opposed to tried but failed) so we can only give a suitable error as
1132 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return -ENODEV;
1134}
1135
1136/*
1137 * set up a private mapping or an anonymous shared mapping
1138 */
David Howells8feae132009-01-08 12:04:47 +00001139static int do_mmap_private(struct vm_area_struct *vma,
1140 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001141 unsigned long len,
1142 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
David Howells8feae132009-01-08 12:04:47 +00001144 struct page *pages;
Bob Liuf67d9b12011-05-24 17:12:56 -07001145 unsigned long total, point, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 void *base;
David Howells8feae132009-01-08 12:04:47 +00001147 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /* invoke the file's mapping function so that it can keep track of
1150 * shared mappings on devices or memory
1151 * - VM_MAYSHARE will be set if it may attempt to share
1152 */
David Howells645d83c2009-09-24 15:13:10 +01001153 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001155 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001157 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1158 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001161 if (ret != -ENOSYS)
1162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* getting an ENOSYS error indicates that direct mmap isn't
1165 * possible (as opposed to tried but failed) so we'll try to
1166 * make a private copy of the data and map that instead */
1167 }
1168
David Howells8feae132009-01-08 12:04:47 +00001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* allocate some memory to hold the mapping
1171 * - note that this may not return a page-aligned address if the object
1172 * we're allocating is smaller than a page
1173 */
Bob Liuf67d9b12011-05-24 17:12:56 -07001174 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +00001175 kdebug("alloc order %d for %lx", order, len);
1176
1177 pages = alloc_pages(GFP_KERNEL, order);
1178 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto enomem;
1180
David Howells8feae132009-01-08 12:04:47 +00001181 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001182 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Bob Liuf67d9b12011-05-24 17:12:56 -07001184 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001185
1186 /* we allocated a power-of-2 sized page set, so we may want to trim off
1187 * the excess */
1188 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1189 while (total > point) {
1190 order = ilog2(total - point);
1191 n = 1 << order;
1192 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001193 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001194 total -= n;
1195 set_page_refcounted(pages + total);
1196 __free_pages(pages + total, order);
1197 }
David Howells8feae132009-01-08 12:04:47 +00001198 }
1199
David Howells8feae132009-01-08 12:04:47 +00001200 for (point = 1; point < total; point++)
1201 set_page_refcounted(&pages[point]);
1202
1203 base = page_address(pages);
1204 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1205 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001206 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001207 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001208
1209 vma->vm_start = region->vm_start;
1210 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (vma->vm_file) {
1213 /* read the contents of a file into the copy */
1214 mm_segment_t old_fs;
1215 loff_t fpos;
1216
1217 fpos = vma->vm_pgoff;
1218 fpos <<= PAGE_SHIFT;
1219
1220 old_fs = get_fs();
1221 set_fs(KERNEL_DS);
Bob Liuf67d9b12011-05-24 17:12:56 -07001222 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 set_fs(old_fs);
1224
1225 if (ret < 0)
1226 goto error_free;
1227
1228 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001229 if (ret < len)
1230 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
1234 return 0;
1235
1236error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001237 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001238 region->vm_start = vma->vm_start = 0;
1239 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001240 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return ret;
1242
1243enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001244 printk("Allocation of length %lu from process %d (%s) failed\n",
1245 len, current->pid, current->comm);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001246 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ENOMEM;
1248}
1249
1250/*
1251 * handle mapping creation for uClinux
1252 */
Al Viroe3fc6292012-05-30 20:08:42 -04001253unsigned long do_mmap_pgoff(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 unsigned long addr,
1255 unsigned long len,
1256 unsigned long prot,
1257 unsigned long flags,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001258 unsigned long pgoff,
Michel Lespinasse41badc12013-02-22 16:32:47 -08001259 unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
David Howells8feae132009-01-08 12:04:47 +00001261 struct vm_area_struct *vma;
1262 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001264 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int ret;
1266
David Howells8feae132009-01-08 12:04:47 +00001267 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1268
Michel Lespinasse41badc12013-02-22 16:32:47 -08001269 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* decide whether we should attempt the mapping, and if so what sort of
1272 * mapping */
1273 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1274 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001275 if (ret < 0) {
1276 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return ret;
David Howells8feae132009-01-08 12:04:47 +00001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
David Howells06aab5a2009-09-24 12:33:48 +01001280 /* we ignore the address hint */
1281 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001282 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* we've determined that we can make the mapping, now translate what we
1285 * now know into VMA flags */
1286 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1287
David Howells8feae132009-01-08 12:04:47 +00001288 /* we're going to need to record the mapping */
1289 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1290 if (!region)
1291 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
David Howells8feae132009-01-08 12:04:47 +00001293 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1294 if (!vma)
1295 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
David Howells1e2ae592010-01-15 17:01:33 -08001297 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001298 region->vm_flags = vm_flags;
1299 region->vm_pgoff = pgoff;
1300
Rik van Riel5beb4932010-03-05 13:42:07 -08001301 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001302 vma->vm_flags = vm_flags;
1303 vma->vm_pgoff = pgoff;
1304
1305 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001306 region->vm_file = get_file(file);
1307 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001308 }
1309
1310 down_write(&nommu_region_sem);
1311
1312 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001314 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 * - shared mappings on character devices and memory backed files are
1316 * permitted to overlap inexactly as far as we are concerned for in
1317 * these cases, sharing is handled in the driver or filesystem rather
1318 * than here
1319 */
1320 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001321 struct vm_region *pregion;
1322 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
David Howells8feae132009-01-08 12:04:47 +00001324 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1325 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001326
David Howells8feae132009-01-08 12:04:47 +00001327 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1328 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
David Howells8feae132009-01-08 12:04:47 +00001330 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 continue;
1332
1333 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001334 if (file_inode(pregion->vm_file) !=
1335 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 continue;
1337
David Howells8feae132009-01-08 12:04:47 +00001338 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 continue;
1340
David Howells8feae132009-01-08 12:04:47 +00001341 rpglen = pregion->vm_end - pregion->vm_start;
1342 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1343 rpgend = pregion->vm_pgoff + rpglen;
1344 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 continue;
1346
David Howells8feae132009-01-08 12:04:47 +00001347 /* handle inexactly overlapping matches between
1348 * mappings */
1349 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1350 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1351 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1353 goto sharing_violation;
1354 continue;
1355 }
1356
David Howells8feae132009-01-08 12:04:47 +00001357 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001358 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001359 vma->vm_region = pregion;
1360 start = pregion->vm_start;
1361 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1362 vma->vm_start = start;
1363 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
David Howells8feae132009-01-08 12:04:47 +00001365 if (pregion->vm_flags & VM_MAPPED_COPY) {
1366 kdebug("share copy");
1367 vma->vm_flags |= VM_MAPPED_COPY;
1368 } else {
1369 kdebug("share mmap");
1370 ret = do_mmap_shared_file(vma);
1371 if (ret < 0) {
1372 vma->vm_region = NULL;
1373 vma->vm_start = 0;
1374 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001375 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001376 pregion = NULL;
1377 goto error_just_free;
1378 }
1379 }
1380 fput(region->vm_file);
1381 kmem_cache_free(vm_region_jar, region);
1382 region = pregion;
1383 result = start;
1384 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* obtain the address at which to make a shared mapping
1388 * - this is the hook for quasi-memory character devices to
1389 * tell us the location of a shared mapping
1390 */
David Howells645d83c2009-09-24 15:13:10 +01001391 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 addr = file->f_op->get_unmapped_area(file, addr, len,
1393 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001394 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001396 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001397 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 /* the driver refused to tell us where to site
1400 * the mapping so we'll have to attempt to copy
1401 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001402 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001404 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001407 } else {
1408 vma->vm_start = region->vm_start = addr;
1409 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 }
1412 }
1413
David Howells8feae132009-01-08 12:04:47 +00001414 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
David Howells645d83c2009-09-24 15:13:10 +01001416 /* set up the mapping
1417 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001420 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 else
David Howells645d83c2009-09-24 15:13:10 +01001422 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001424 goto error_just_free;
1425 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001426
Jie Zhangea637632009-12-14 18:00:02 -08001427 /* clear anonymous mappings that don't ask for uninitialized data */
1428 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1429 memset((void *)region->vm_start, 0,
1430 region->vm_end - region->vm_start);
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001433 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 current->mm->total_vm += len >> PAGE_SHIFT;
1436
David Howells8feae132009-01-08 12:04:47 +00001437share:
1438 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Mike Frysingercfe79c02010-01-06 17:23:23 +00001440 /* we flush the region from the icache only when the first executable
1441 * mapping of it is made */
1442 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1443 flush_icache_range(region->vm_start, region->vm_end);
1444 region->vm_icache_flushed = true;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Mike Frysingercfe79c02010-01-06 17:23:23 +00001447 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
David Howells8feae132009-01-08 12:04:47 +00001449 kleave(" = %lx", result);
1450 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
David Howells8feae132009-01-08 12:04:47 +00001452error_just_free:
1453 up_write(&nommu_region_sem);
1454error:
David Howells89a86402009-10-30 13:13:26 +00001455 if (region->vm_file)
1456 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001457 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001458 if (vma->vm_file)
1459 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001460 kmem_cache_free(vm_area_cachep, vma);
1461 kleave(" = %d", ret);
1462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
David Howells8feae132009-01-08 12:04:47 +00001464sharing_violation:
1465 up_write(&nommu_region_sem);
1466 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1467 ret = -EINVAL;
1468 goto error;
1469
1470error_getting_vma:
1471 kmem_cache_free(vm_region_jar, region);
1472 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1473 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001475 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -ENOMEM;
1477
David Howells8feae132009-01-08 12:04:47 +00001478error_getting_region:
1479 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1480 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001482 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -ENOMEM;
1484}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001485
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001486SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1487 unsigned long, prot, unsigned long, flags,
1488 unsigned long, fd, unsigned long, pgoff)
1489{
1490 struct file *file = NULL;
1491 unsigned long retval = -EBADF;
1492
Al Viro120a7952010-10-30 02:54:44 -04001493 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001494 if (!(flags & MAP_ANONYMOUS)) {
1495 file = fget(fd);
1496 if (!file)
1497 goto out;
1498 }
1499
1500 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1501
Greg Ungererad1ed292012-06-04 14:29:59 +10001502 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001503
1504 if (file)
1505 fput(file);
1506out:
1507 return retval;
1508}
1509
Christoph Hellwiga4679372010-03-10 15:21:15 -08001510#ifdef __ARCH_WANT_SYS_OLD_MMAP
1511struct mmap_arg_struct {
1512 unsigned long addr;
1513 unsigned long len;
1514 unsigned long prot;
1515 unsigned long flags;
1516 unsigned long fd;
1517 unsigned long offset;
1518};
1519
1520SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1521{
1522 struct mmap_arg_struct a;
1523
1524 if (copy_from_user(&a, arg, sizeof(a)))
1525 return -EFAULT;
1526 if (a.offset & ~PAGE_MASK)
1527 return -EINVAL;
1528
1529 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1530 a.offset >> PAGE_SHIFT);
1531}
1532#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/*
David Howells8feae132009-01-08 12:04:47 +00001535 * split a vma into two pieces at address 'addr', a new vma is allocated either
1536 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
David Howells8feae132009-01-08 12:04:47 +00001538int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1539 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
David Howells8feae132009-01-08 12:04:47 +00001541 struct vm_area_struct *new;
1542 struct vm_region *region;
1543 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
David Howells8feae132009-01-08 12:04:47 +00001545 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
David Howells779c1022010-01-15 17:01:34 -08001547 /* we're only permitted to split anonymous regions (these should have
1548 * only a single usage on the region) */
1549 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David Howells8feae132009-01-08 12:04:47 +00001552 if (mm->map_count >= sysctl_max_map_count)
1553 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
David Howells8feae132009-01-08 12:04:47 +00001555 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1556 if (!region)
1557 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
David Howells8feae132009-01-08 12:04:47 +00001559 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1560 if (!new) {
1561 kmem_cache_free(vm_region_jar, region);
1562 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
David Howells8feae132009-01-08 12:04:47 +00001564
1565 /* most fields are the same, copy all, and then fixup */
1566 *new = *vma;
1567 *region = *vma->vm_region;
1568 new->vm_region = region;
1569
1570 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1571
1572 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001573 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001574 } else {
1575 region->vm_start = new->vm_start = addr;
1576 region->vm_pgoff = new->vm_pgoff += npages;
1577 }
1578
1579 if (new->vm_ops && new->vm_ops->open)
1580 new->vm_ops->open(new);
1581
1582 delete_vma_from_mm(vma);
1583 down_write(&nommu_region_sem);
1584 delete_nommu_region(vma->vm_region);
1585 if (new_below) {
1586 vma->vm_region->vm_start = vma->vm_start = addr;
1587 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1588 } else {
1589 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001590 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001591 }
1592 add_nommu_region(vma->vm_region);
1593 add_nommu_region(new->vm_region);
1594 up_write(&nommu_region_sem);
1595 add_vma_to_mm(mm, vma);
1596 add_vma_to_mm(mm, new);
1597 return 0;
1598}
1599
1600/*
1601 * shrink a VMA by removing the specified chunk from either the beginning or
1602 * the end
1603 */
1604static int shrink_vma(struct mm_struct *mm,
1605 struct vm_area_struct *vma,
1606 unsigned long from, unsigned long to)
1607{
1608 struct vm_region *region;
1609
1610 kenter("");
1611
1612 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1613 * and list */
1614 delete_vma_from_mm(vma);
1615 if (from > vma->vm_start)
1616 vma->vm_end = from;
1617 else
1618 vma->vm_start = to;
1619 add_vma_to_mm(mm, vma);
1620
1621 /* cut the backing region down to size */
1622 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001623 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001624
1625 down_write(&nommu_region_sem);
1626 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001627 if (from > region->vm_start) {
1628 to = region->vm_top;
1629 region->vm_top = region->vm_end = from;
1630 } else {
David Howells8feae132009-01-08 12:04:47 +00001631 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001632 }
David Howells8feae132009-01-08 12:04:47 +00001633 add_nommu_region(region);
1634 up_write(&nommu_region_sem);
1635
1636 free_page_series(from, to);
1637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
David Howells30340972006-09-27 01:50:20 -07001640/*
1641 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001642 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1643 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001644 */
David Howells8feae132009-01-08 12:04:47 +00001645int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
David Howells8feae132009-01-08 12:04:47 +00001647 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001648 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001649 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
David Howells8feae132009-01-08 12:04:47 +00001651 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Bob Liuf67d9b12011-05-24 17:12:56 -07001653 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001654 if (len == 0)
1655 return -EINVAL;
1656
Bob Liuf67d9b12011-05-24 17:12:56 -07001657 end = start + len;
1658
David Howells8feae132009-01-08 12:04:47 +00001659 /* find the first potentially overlapping VMA */
1660 vma = find_vma(mm, start);
1661 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001662 static int limit = 0;
1663 if (limit < 5) {
1664 printk(KERN_WARNING
1665 "munmap of memory not mmapped by process %d"
1666 " (%s): 0x%lx-0x%lx\n",
1667 current->pid, current->comm,
1668 start, start + len - 1);
1669 limit++;
1670 }
David Howells8feae132009-01-08 12:04:47 +00001671 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David Howells8feae132009-01-08 12:04:47 +00001674 /* we're allowed to split an anonymous VMA but not a file-backed one */
1675 if (vma->vm_file) {
1676 do {
1677 if (start > vma->vm_start) {
1678 kleave(" = -EINVAL [miss]");
1679 return -EINVAL;
1680 }
1681 if (end == vma->vm_end)
1682 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001683 vma = vma->vm_next;
1684 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001685 kleave(" = -EINVAL [split file]");
1686 return -EINVAL;
1687 } else {
1688 /* the chunk must be a subset of the VMA found */
1689 if (start == vma->vm_start && end == vma->vm_end)
1690 goto erase_whole_vma;
1691 if (start < vma->vm_start || end > vma->vm_end) {
1692 kleave(" = -EINVAL [superset]");
1693 return -EINVAL;
1694 }
1695 if (start & ~PAGE_MASK) {
1696 kleave(" = -EINVAL [unaligned start]");
1697 return -EINVAL;
1698 }
1699 if (end != vma->vm_end && end & ~PAGE_MASK) {
1700 kleave(" = -EINVAL [unaligned split]");
1701 return -EINVAL;
1702 }
1703 if (start != vma->vm_start && end != vma->vm_end) {
1704 ret = split_vma(mm, vma, start, 1);
1705 if (ret < 0) {
1706 kleave(" = %d [split]", ret);
1707 return ret;
1708 }
1709 }
1710 return shrink_vma(mm, vma, start, end);
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
David Howells8feae132009-01-08 12:04:47 +00001713erase_whole_vma:
1714 delete_vma_from_mm(vma);
1715 delete_vma(mm, vma);
1716 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718}
Paul Mundtb5073172007-07-21 04:37:25 -07001719EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Al Virobfce2812012-04-20 21:57:04 -04001721int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001722{
Al Virobfce2812012-04-20 21:57:04 -04001723 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001724 int ret;
David Howells30340972006-09-27 01:50:20 -07001725
1726 down_write(&mm->mmap_sem);
1727 ret = do_munmap(mm, addr, len);
1728 up_write(&mm->mmap_sem);
1729 return ret;
1730}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001731EXPORT_SYMBOL(vm_munmap);
1732
1733SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1734{
Al Virobfce2812012-04-20 21:57:04 -04001735 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001736}
David Howells30340972006-09-27 01:50:20 -07001737
1738/*
David Howells8feae132009-01-08 12:04:47 +00001739 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001740 */
David Howells8feae132009-01-08 12:04:47 +00001741void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
David Howells8feae132009-01-08 12:04:47 +00001743 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
David Howells8feae132009-01-08 12:04:47 +00001745 if (!mm)
1746 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
David Howells8feae132009-01-08 12:04:47 +00001748 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
David Howells8feae132009-01-08 12:04:47 +00001750 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
David Howells8feae132009-01-08 12:04:47 +00001752 while ((vma = mm->mmap)) {
1753 mm->mmap = vma->vm_next;
1754 delete_vma_from_mm(vma);
1755 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001756 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
David Howells8feae132009-01-08 12:04:47 +00001758
1759 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07001762unsigned long vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 return -ENOMEM;
1765}
1766
1767/*
David Howells6fa5f802006-09-27 01:50:21 -07001768 * expand (or shrink) an existing mapping, potentially moving it at the same
1769 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 *
David Howells6fa5f802006-09-27 01:50:21 -07001771 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001772 * as long as it stays within the region allocated by do_mmap_private() and the
1773 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
David Howells6fa5f802006-09-27 01:50:21 -07001775 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Al Viro4b377ba2013-03-04 10:47:59 -05001777static unsigned long do_mremap(unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 unsigned long old_len, unsigned long new_len,
1779 unsigned long flags, unsigned long new_addr)
1780{
David Howells6fa5f802006-09-27 01:50:21 -07001781 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001784 old_len = PAGE_ALIGN(old_len);
1785 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001786 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return (unsigned long) -EINVAL;
1788
David Howells8feae132009-01-08 12:04:47 +00001789 if (addr & ~PAGE_MASK)
1790 return -EINVAL;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (flags & MREMAP_FIXED && new_addr != addr)
1793 return (unsigned long) -EINVAL;
1794
David Howells8feae132009-01-08 12:04:47 +00001795 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001796 if (!vma)
1797 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
David Howells6fa5f802006-09-27 01:50:21 -07001799 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return (unsigned long) -EFAULT;
1801
David Howells6fa5f802006-09-27 01:50:21 -07001802 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return (unsigned long) -EPERM;
1804
David Howells8feae132009-01-08 12:04:47 +00001805 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return (unsigned long) -ENOMEM;
1807
1808 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001809 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001810 return vma->vm_start;
1811}
1812
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001813SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1814 unsigned long, new_len, unsigned long, flags,
1815 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001816{
1817 unsigned long ret;
1818
1819 down_write(&current->mm->mmap_sem);
1820 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1821 up_write(&current->mm->mmap_sem);
1822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Michel Lespinasse240aade2013-02-22 16:35:56 -08001825struct page *follow_page_mask(struct vm_area_struct *vma,
1826 unsigned long address, unsigned int flags,
1827 unsigned int *page_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Michel Lespinasse240aade2013-02-22 16:35:56 -08001829 *page_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return NULL;
1831}
1832
Bob Liu8f3b1322011-07-08 15:39:46 -07001833int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1834 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Bob Liu8f3b1322011-07-08 15:39:46 -07001836 if (addr != (pfn << PAGE_SHIFT))
1837 return -EINVAL;
1838
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001839 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
Luke Yang22c4af42006-07-14 00:24:09 -07001842EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001844int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1845{
1846 unsigned long pfn = start >> PAGE_SHIFT;
1847 unsigned long vm_len = vma->vm_end - vma->vm_start;
1848
1849 pfn += vma->vm_pgoff;
1850 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1851}
1852EXPORT_SYMBOL(vm_iomap_memory);
1853
Paul Mundtf905bc42008-02-04 22:29:59 -08001854int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1855 unsigned long pgoff)
1856{
1857 unsigned int size = vma->vm_end - vma->vm_start;
1858
1859 if (!(vma->vm_flags & VM_USERMAP))
1860 return -EINVAL;
1861
1862 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1863 vma->vm_end = vma->vm_start + size;
1864
1865 return 0;
1866}
1867EXPORT_SYMBOL(remap_vmalloc_range);
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1870 unsigned long len, unsigned long pgoff, unsigned long flags)
1871{
1872 return -ENOMEM;
1873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875void unmap_mapping_range(struct address_space *mapping,
1876 loff_t const holebegin, loff_t const holelen,
1877 int even_cows)
1878{
1879}
Luke Yang22c4af42006-07-14 00:24:09 -07001880EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882/*
1883 * Check that a process has enough memory to allocate a new virtual
1884 * mapping. 0 means there is enough memory for the allocation to
1885 * succeed and -ENOMEM implies there is not.
1886 *
1887 * We currently support three overcommit policies, which are set via the
1888 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1889 *
1890 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1891 * Additional code 2002 Jul 20 by Robert Love.
1892 *
1893 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1894 *
1895 * Note this is a helper function intended to be used by LSMs which
1896 * wish to use this logic.
1897 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001898int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001900 unsigned long free, allowed, reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 vm_acct_memory(pages);
1903
1904 /*
1905 * Sometimes we want to use more memory than we have
1906 */
1907 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1908 return 0;
1909
1910 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -07001911 free = global_page_state(NR_FREE_PAGES);
1912 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Dmitry Finkc15bef32011-07-25 17:12:19 -07001914 /*
1915 * shmem pages shouldn't be counted as free in this
1916 * case, they can't be purged, only swapped out, and
1917 * that won't affect the overall amount of available
1918 * memory in the system.
1919 */
1920 free -= global_page_state(NR_SHMEM);
1921
Shaohua Liec8acf22013-02-22 16:34:38 -08001922 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /*
1925 * Any slabs which are created with the
1926 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1927 * which are reclaimable, under pressure. The dentry
1928 * cache and most inode caches should fall into this
1929 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001930 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -07001933 * Leave reserved pages. The pages are not for anonymous pages.
1934 */
1935 if (free <= totalreserve_pages)
1936 goto error;
1937 else
1938 free -= totalreserve_pages;
1939
1940 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001941 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
1943 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001944 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (free > pages)
1947 return 0;
1948
Hideo AOKId5ddc792006-04-10 22:53:01 -07001949 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
Jerome Marchand00619bc2013-11-12 15:08:31 -08001952 allowed = vm_commit_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001954 * Reserve some 3% for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
1956 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001957 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001959 /*
1960 * Don't let a single process grow so big a user can't recover
1961 */
1962 if (mm) {
1963 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1964 allowed -= min(mm->total_vm / 32, reserve);
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001967 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001969
Hideo AOKId5ddc792006-04-10 22:53:01 -07001970error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 vm_unacct_memory(pages);
1972
1973 return -ENOMEM;
1974}
1975
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001976int in_gate_area_no_mm(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 return 0;
1979}
David Howellsb0e15192006-01-06 00:11:42 -08001980
Nick Piggind0217ac2007-07-19 01:47:03 -07001981int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001982{
1983 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001984 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001985}
Paul Mundtb5073172007-07-21 04:37:25 -07001986EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001987
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07001988void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
1989{
1990 BUG();
1991}
1992EXPORT_SYMBOL(filemap_map_pages);
1993
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001994int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1995 unsigned long size, pgoff_t pgoff)
1996{
1997 BUG();
1998 return 0;
1999}
2000EXPORT_SYMBOL(generic_file_remap_pages);
2001
Mike Frysingerf55f1992011-03-29 14:05:12 +01002002static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2003 unsigned long addr, void *buf, int len, int write)
David Howells0ec76a12006-09-27 01:50:15 -07002004{
David Howells0ec76a12006-09-27 01:50:15 -07002005 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07002006
2007 down_read(&mm->mmap_sem);
2008
2009 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07002010 vma = find_vma(mm, addr);
2011 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07002012 /* don't overrun this mapping */
2013 if (addr + len >= vma->vm_end)
2014 len = vma->vm_end - addr;
2015
2016 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07002017 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00002018 copy_to_user_page(vma, NULL, addr,
2019 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07002020 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00002021 copy_from_user_page(vma, NULL, addr,
2022 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07002023 else
2024 len = 0;
2025 } else {
2026 len = 0;
2027 }
2028
2029 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01002030
2031 return len;
2032}
2033
2034/**
2035 * @access_remote_vm - access another process' address space
2036 * @mm: the mm_struct of the target address space
2037 * @addr: start address to access
2038 * @buf: source or destination buffer
2039 * @len: number of bytes to transfer
2040 * @write: whether the access is a write
2041 *
2042 * The caller must hold a reference on @mm.
2043 */
2044int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2045 void *buf, int len, int write)
2046{
2047 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2048}
2049
2050/*
2051 * Access another process' address space.
2052 * - source/target buffer must be kernel space
2053 */
2054int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2055{
2056 struct mm_struct *mm;
2057
2058 if (addr + len < addr)
2059 return 0;
2060
2061 mm = get_task_mm(tsk);
2062 if (!mm)
2063 return 0;
2064
2065 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2066
David Howells0ec76a12006-09-27 01:50:15 -07002067 mmput(mm);
2068 return len;
2069}
David Howells7e660872010-01-15 17:01:39 -08002070
2071/**
2072 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2073 * @inode: The inode to check
2074 * @size: The current filesize of the inode
2075 * @newsize: The proposed filesize of the inode
2076 *
2077 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2078 * make sure that that any outstanding VMAs aren't broken and then shrink the
2079 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2080 * automatically grant mappings that are too large.
2081 */
2082int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2083 size_t newsize)
2084{
2085 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08002086 struct vm_region *region;
2087 pgoff_t low, high;
2088 size_t r_size, r_top;
2089
2090 low = newsize >> PAGE_SHIFT;
2091 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2092
2093 down_write(&nommu_region_sem);
David Howells918e5562012-02-23 13:50:35 +00002094 mutex_lock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002095
2096 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002097 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08002098 /* found one - only interested if it's shared out of the page
2099 * cache */
2100 if (vma->vm_flags & VM_SHARED) {
David Howells918e5562012-02-23 13:50:35 +00002101 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002102 up_write(&nommu_region_sem);
2103 return -ETXTBSY; /* not quite true, but near enough */
2104 }
2105 }
2106
2107 /* reduce any regions that overlap the dead zone - if in existence,
2108 * these will be pointed to by VMAs that don't overlap the dead zone
2109 *
2110 * we don't check for any regions that start beyond the EOF as there
2111 * shouldn't be any
2112 */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002113 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2114 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08002115 if (!(vma->vm_flags & VM_SHARED))
2116 continue;
2117
2118 region = vma->vm_region;
2119 r_size = region->vm_top - region->vm_start;
2120 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2121
2122 if (r_top > newsize) {
2123 region->vm_top -= r_top - newsize;
2124 if (region->vm_end > region->vm_top)
2125 region->vm_end = region->vm_top;
2126 }
2127 }
2128
David Howells918e5562012-02-23 13:50:35 +00002129 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002130 up_write(&nommu_region_sem);
2131 return 0;
2132}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07002133
2134/*
2135 * Initialise sysctl_user_reserve_kbytes.
2136 *
2137 * This is intended to prevent a user from starting a single memory hogging
2138 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2139 * mode.
2140 *
2141 * The default value is min(3% of free memory, 128MB)
2142 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2143 */
2144static int __meminit init_user_reserve(void)
2145{
2146 unsigned long free_kbytes;
2147
2148 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2149
2150 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2151 return 0;
2152}
2153module_init(init_user_reserve)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07002154
2155/*
2156 * Initialise sysctl_admin_reserve_kbytes.
2157 *
2158 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2159 * to log in and kill a memory hogging process.
2160 *
2161 * Systems with more than 256MB will reserve 8MB, enough to recover
2162 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2163 * only reserve 3% of free pages by default.
2164 */
2165static int __meminit init_admin_reserve(void)
2166{
2167 unsigned long free_kbytes;
2168
2169 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2170
2171 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2172 return 0;
2173}
2174module_init(init_admin_reserve)