blob: 598bc871487a89434f6da665591a512fedae4c37 [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 Mundteb6434d2009-01-21 17:45:47 +090013 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
David Howellsf2b85442007-10-29 13:15:39 +000016#include <linux/module.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>
Roland McGrathfa8e26c2008-07-25 19:45:50 -070025#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
27#include <linux/backing-dev.h>
28#include <linux/mount.h>
29#include <linux/personality.h>
30#include <linux/security.h>
31#include <linux/syscalls.h>
32
33#include <asm/uaccess.h>
34#include <asm/tlb.h>
35#include <asm/tlbflush.h>
David Howells8feae132009-01-08 12:04:47 +000036#include "internal.h"
37
38static inline __attribute__((format(printf, 1, 2)))
39void no_printk(const char *fmt, ...)
40{
41}
42
43#if 0
44#define kenter(FMT, ...) \
45 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
46#define kleave(FMT, ...) \
47 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
48#define kdebug(FMT, ...) \
49 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
50#else
51#define kenter(FMT, ...) \
52 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
53#define kleave(FMT, ...) \
54 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
55#define kdebug(FMT, ...) \
56 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
57#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Nick Pigginb291f002008-10-18 20:26:44 -070059#include "internal.h"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061void *high_memory;
62struct page *mem_map;
63unsigned long max_mapnr;
64unsigned long num_physpages;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070065struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
67int sysctl_overcommit_ratio = 50; /* default is 50% */
68int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070069int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070int heap_stack_gap = 0;
71
Paul Mundt35f2c2f2009-06-09 17:48:56 +090072/* amount of vm to protect from userspace access */
73unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
74
David Howells33e5d7692009-04-02 16:56:32 -070075atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070078EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
David Howells8feae132009-01-08 12:04:47 +000080/* list of mapped, potentially shareable regions */
81static struct kmem_cache *vm_region_jar;
82struct rb_root nommu_region_tree = RB_ROOT;
83DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85struct vm_operations_struct generic_file_vm_ops = {
86};
87
88/*
89 * Handle all mappings that got truncated by a "truncate()"
90 * system call.
91 *
92 * NOTE! We have to be ready to update the memory sharing
93 * between the file and the memory map for a potential last
94 * incomplete page. Ugly, but necessary.
95 */
96int vmtruncate(struct inode *inode, loff_t offset)
97{
98 struct address_space *mapping = inode->i_mapping;
99 unsigned long limit;
100
101 if (inode->i_size < offset)
102 goto do_expand;
103 i_size_write(inode, offset);
104
105 truncate_inode_pages(mapping, offset);
106 goto out_truncate;
107
108do_expand:
109 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
110 if (limit != RLIM_INFINITY && offset > limit)
111 goto out_sig;
112 if (offset > inode->i_sb->s_maxbytes)
113 goto out;
114 i_size_write(inode, offset);
115
116out_truncate:
Al Viroacfa4382008-12-04 10:06:33 -0500117 if (inode->i_op->truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 inode->i_op->truncate(inode);
119 return 0;
120out_sig:
121 send_sig(SIGXFSZ, current, 0);
122out:
123 return -EFBIG;
124}
125
126EXPORT_SYMBOL(vmtruncate);
127
128/*
129 * Return the total memory allocated for this pointer, not
130 * just what the caller asked for.
131 *
132 * Doesn't have to be accurate, i.e. may have races.
133 */
134unsigned int kobjsize(const void *objp)
135{
136 struct page *page;
137
Michael Hennerich4016a132008-04-28 02:13:38 -0700138 /*
139 * If the object we have should not have ksize performed on it,
140 * return size of 0
141 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900142 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700143 return 0;
144
145 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700146
147 /*
148 * If the allocator sets PageSlab, we know the pointer came from
149 * kmalloc().
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (PageSlab(page))
152 return ksize(objp);
153
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700154 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000155 * If it's not a compound page, see if we have a matching VMA
156 * region. This test is intentionally done in reverse order,
157 * so if there's no VMA, we still fall through and hand back
158 * PAGE_SIZE for 0-order pages.
159 */
160 if (!PageCompound(page)) {
161 struct vm_area_struct *vma;
162
163 vma = find_vma(current->mm, (unsigned long)objp);
164 if (vma)
165 return vma->vm_end - vma->vm_start;
166 }
167
168 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700169 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900170 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700171 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900172 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Nick Pigginb291f002008-10-18 20:26:44 -0700175int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
176 unsigned long start, int len, int flags,
177 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700179 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700180 unsigned long vm_flags;
181 int i;
Nick Pigginb291f002008-10-18 20:26:44 -0700182 int write = !!(flags & GUP_FLAGS_WRITE);
183 int force = !!(flags & GUP_FLAGS_FORCE);
184 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
David Howells7b4d5b82006-09-27 01:50:18 -0700185
186 /* calculate required read or write permissions.
187 * - if 'force' is set, we only require the "MAY" flags.
188 */
189 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
190 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 for (i = 0; i < len; i++) {
Sonic Zhang910e46d2006-09-27 01:50:17 -0700193 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700194 if (!vma)
195 goto finish_or_fault;
196
197 /* protect what we can, including chardevs */
198 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
Nick Pigginb291f002008-10-18 20:26:44 -0700199 (!ignore && !(vm_flags & vma->vm_flags)))
David Howells7b4d5b82006-09-27 01:50:18 -0700200 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (pages) {
203 pages[i] = virt_to_page(start);
204 if (pages[i])
205 page_cache_get(pages[i]);
206 }
207 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700208 vmas[i] = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 start += PAGE_SIZE;
210 }
David Howells7b4d5b82006-09-27 01:50:18 -0700211
212 return i;
213
214finish_or_fault:
215 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
Nick Pigginb291f002008-10-18 20:26:44 -0700217
218
219/*
220 * get a list of pages in an address range belonging to the specified process
221 * and indicate the VMA that covers each page
222 * - this is potentially dodgy as we may end incrementing the page count of a
223 * slab page or a secondary page from a compound page
224 * - don't permit access to VMAs that don't support it, such as I/O mappings
225 */
226int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
227 unsigned long start, int len, int write, int force,
228 struct page **pages, struct vm_area_struct **vmas)
229{
230 int flags = 0;
231
232 if (write)
233 flags |= GUP_FLAGS_WRITE;
234 if (force)
235 flags |= GUP_FLAGS_FORCE;
236
237 return __get_user_pages(tsk, mm,
238 start, len, flags,
239 pages, vmas);
240}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000241EXPORT_SYMBOL(get_user_pages);
242
Paul Mundtdfc2f912009-06-26 04:31:57 +0900243/**
244 * follow_pfn - look up PFN at a user virtual address
245 * @vma: memory mapping
246 * @address: user virtual address
247 * @pfn: location to store found PFN
248 *
249 * Only IO mappings and raw PFN mappings are allowed.
250 *
251 * Returns zero and the pfn at @pfn on success, -ve otherwise.
252 */
253int follow_pfn(struct vm_area_struct *vma, unsigned long address,
254 unsigned long *pfn)
255{
256 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
257 return -EINVAL;
258
259 *pfn = address >> PAGE_SHIFT;
260 return 0;
261}
262EXPORT_SYMBOL(follow_pfn);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264DEFINE_RWLOCK(vmlist_lock);
265struct vm_struct *vmlist;
266
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800267void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 kfree(addr);
270}
Paul Mundtb5073172007-07-21 04:37:25 -0700271EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Al Virodd0fc662005-10-07 07:46:04 +0100273void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200276 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
277 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Nick Piggin84097512006-03-22 00:08:34 -0800279 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
Paul Mundtb5073172007-07-21 04:37:25 -0700281EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Paul Mundtf905bc42008-02-04 22:29:59 -0800283void *vmalloc_user(unsigned long size)
284{
285 void *ret;
286
287 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
288 PAGE_KERNEL);
289 if (ret) {
290 struct vm_area_struct *vma;
291
292 down_write(&current->mm->mmap_sem);
293 vma = find_vma(current->mm, (unsigned long)ret);
294 if (vma)
295 vma->vm_flags |= VM_USERMAP;
296 up_write(&current->mm->mmap_sem);
297 }
298
299 return ret;
300}
301EXPORT_SYMBOL(vmalloc_user);
302
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800303struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 return virt_to_page(addr);
306}
Paul Mundtb5073172007-07-21 04:37:25 -0700307EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800309unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 return page_to_pfn(virt_to_page(addr));
312}
Paul Mundtb5073172007-07-21 04:37:25 -0700313EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315long vread(char *buf, char *addr, unsigned long count)
316{
317 memcpy(buf, addr, count);
318 return count;
319}
320
321long vwrite(char *buf, char *addr, unsigned long count)
322{
323 /* Don't allow overflow */
324 if ((unsigned long) addr + count < count)
325 count = -(unsigned long) addr;
326
327 memcpy(addr, buf, count);
328 return(count);
329}
330
331/*
332 * vmalloc - allocate virtually continguos memory
333 *
334 * @size: allocation size
335 *
336 * Allocate enough pages to cover @size from the page level
337 * allocator and map them into continguos kernel virtual space.
338 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200339 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * use __vmalloc() instead.
341 */
342void *vmalloc(unsigned long size)
343{
344 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
345}
Andrew Mortonf6138882006-02-28 16:59:18 -0800346EXPORT_SYMBOL(vmalloc);
347
348void *vmalloc_node(unsigned long size, int node)
349{
350 return vmalloc(size);
351}
352EXPORT_SYMBOL(vmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Paul Mundt1af446e2008-08-04 16:01:47 +0900354#ifndef PAGE_KERNEL_EXEC
355# define PAGE_KERNEL_EXEC PAGE_KERNEL
356#endif
357
358/**
359 * vmalloc_exec - allocate virtually contiguous, executable memory
360 * @size: allocation size
361 *
362 * Kernel-internal function to allocate enough pages to cover @size
363 * the page level allocator and map them into contiguous and
364 * executable kernel virtual space.
365 *
366 * For tight control over page level allocator and protection flags
367 * use __vmalloc() instead.
368 */
369
370void *vmalloc_exec(unsigned long size)
371{
372 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
373}
374
Paul Mundtb5073172007-07-21 04:37:25 -0700375/**
376 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * @size: allocation size
378 *
379 * Allocate enough 32bit PA addressable pages to cover @size from the
380 * page level allocator and map them into continguos kernel virtual space.
381 */
382void *vmalloc_32(unsigned long size)
383{
384 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
385}
Paul Mundtb5073172007-07-21 04:37:25 -0700386EXPORT_SYMBOL(vmalloc_32);
387
388/**
389 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
390 * @size: allocation size
391 *
392 * The resulting memory area is 32bit addressable and zeroed so it can be
393 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800394 *
395 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
396 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700397 */
398void *vmalloc_32_user(unsigned long size)
399{
Paul Mundtf905bc42008-02-04 22:29:59 -0800400 /*
401 * We'll have to sort out the ZONE_DMA bits for 64-bit,
402 * but for now this can simply use vmalloc_user() directly.
403 */
404 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700405}
406EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
409{
410 BUG();
411 return NULL;
412}
Paul Mundtb5073172007-07-21 04:37:25 -0700413EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800415void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 BUG();
418}
Paul Mundtb5073172007-07-21 04:37:25 -0700419EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Paul Mundteb6434d2009-01-21 17:45:47 +0900421void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
422{
423 BUG();
424 return NULL;
425}
426EXPORT_SYMBOL(vm_map_ram);
427
428void vm_unmap_ram(const void *mem, unsigned int count)
429{
430 BUG();
431}
432EXPORT_SYMBOL(vm_unmap_ram);
433
434void vm_unmap_aliases(void)
435{
436}
437EXPORT_SYMBOL_GPL(vm_unmap_aliases);
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700440 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
441 * have one.
442 */
443void __attribute__((weak)) vmalloc_sync_all(void)
444{
445}
446
Paul Mundtb5073172007-07-21 04:37:25 -0700447int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
448 struct page *page)
449{
450 return -EINVAL;
451}
452EXPORT_SYMBOL(vm_insert_page);
453
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * sys_brk() for the most part doesn't need the global kernel
456 * lock, except when an application is doing something nasty
457 * like trying to un-brk an area that has already been mapped
458 * to a regular file. in this case, the unmapping will need
459 * to invoke file system routines that need the global lock.
460 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100461SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct mm_struct *mm = current->mm;
464
465 if (brk < mm->start_brk || brk > mm->context.end_brk)
466 return mm->brk;
467
468 if (mm->brk == brk)
469 return mm->brk;
470
471 /*
472 * Always allow shrinking brk
473 */
474 if (brk <= mm->brk) {
475 mm->brk = brk;
476 return brk;
477 }
478
479 /*
480 * Ok, looks good - let it rip.
481 */
482 return mm->brk = brk;
483}
484
David Howells8feae132009-01-08 12:04:47 +0000485/*
486 * initialise the VMA and region record slabs
487 */
488void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700490 int ret;
491
492 ret = percpu_counter_init(&vm_committed_as, 0);
493 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700494 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000495}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
David Howells8feae132009-01-08 12:04:47 +0000497/*
498 * validate the region tree
499 * - the caller must hold the region lock
500 */
501#ifdef CONFIG_DEBUG_NOMMU_REGIONS
502static noinline void validate_nommu_regions(void)
503{
504 struct vm_region *region, *last;
505 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
David Howells8feae132009-01-08 12:04:47 +0000507 lastp = rb_first(&nommu_region_tree);
508 if (!lastp)
509 return;
510
511 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700512 BUG_ON(unlikely(last->vm_end <= last->vm_start));
513 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000514
515 while ((p = rb_next(lastp))) {
516 region = rb_entry(p, struct vm_region, vm_rb);
517 last = rb_entry(lastp, struct vm_region, vm_rb);
518
David Howells33e5d7692009-04-02 16:56:32 -0700519 BUG_ON(unlikely(region->vm_end <= region->vm_start));
520 BUG_ON(unlikely(region->vm_top < region->vm_end));
521 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000522
523 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525}
David Howells8feae132009-01-08 12:04:47 +0000526#else
David Howells33e5d7692009-04-02 16:56:32 -0700527static void validate_nommu_regions(void)
528{
529}
David Howells8feae132009-01-08 12:04:47 +0000530#endif
531
532/*
533 * add a region into the global tree
534 */
535static void add_nommu_region(struct vm_region *region)
536{
537 struct vm_region *pregion;
538 struct rb_node **p, *parent;
539
540 validate_nommu_regions();
541
David Howells8feae132009-01-08 12:04:47 +0000542 parent = NULL;
543 p = &nommu_region_tree.rb_node;
544 while (*p) {
545 parent = *p;
546 pregion = rb_entry(parent, struct vm_region, vm_rb);
547 if (region->vm_start < pregion->vm_start)
548 p = &(*p)->rb_left;
549 else if (region->vm_start > pregion->vm_start)
550 p = &(*p)->rb_right;
551 else if (pregion == region)
552 return;
553 else
554 BUG();
555 }
556
557 rb_link_node(&region->vm_rb, parent, p);
558 rb_insert_color(&region->vm_rb, &nommu_region_tree);
559
560 validate_nommu_regions();
561}
562
563/*
564 * delete a region from the global tree
565 */
566static void delete_nommu_region(struct vm_region *region)
567{
568 BUG_ON(!nommu_region_tree.rb_node);
569
570 validate_nommu_regions();
571 rb_erase(&region->vm_rb, &nommu_region_tree);
572 validate_nommu_regions();
573}
574
575/*
576 * free a contiguous series of pages
577 */
578static void free_page_series(unsigned long from, unsigned long to)
579{
580 for (; from < to; from += PAGE_SIZE) {
581 struct page *page = virt_to_page(from);
582
583 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700584 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000585 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700586 kdebug("free page %p: refcount not one: %d",
587 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000588 put_page(page);
589 }
590}
591
592/*
593 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700594 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000595 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000596 * will equal vm_start
597 */
598static void __put_nommu_region(struct vm_region *region)
599 __releases(nommu_region_sem)
600{
601 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
602
603 BUG_ON(!nommu_region_tree.rb_node);
604
605 if (atomic_dec_and_test(&region->vm_usage)) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000606 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000607 delete_nommu_region(region);
608 up_write(&nommu_region_sem);
609
610 if (region->vm_file)
611 fput(region->vm_file);
612
613 /* IO memory and memory shared directly out of the pagecache
614 * from ramfs/tmpfs mustn't be released here */
615 if (region->vm_flags & VM_MAPPED_COPY) {
616 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000617 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000618 }
619 kmem_cache_free(vm_region_jar, region);
620 } else {
621 up_write(&nommu_region_sem);
622 }
623}
624
625/*
626 * release a reference to a region
627 */
628static void put_nommu_region(struct vm_region *region)
629{
630 down_write(&nommu_region_sem);
631 __put_nommu_region(region);
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
David Howells30340972006-09-27 01:50:20 -0700634/*
635 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000636 * and tree and add to the address space's page tree also if not an anonymous
637 * page
David Howells30340972006-09-27 01:50:20 -0700638 * - should be called with mm->mmap_sem held writelocked
639 */
David Howells8feae132009-01-08 12:04:47 +0000640static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700641{
David Howells8feae132009-01-08 12:04:47 +0000642 struct vm_area_struct *pvma, **pp;
643 struct address_space *mapping;
644 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700645
David Howells8feae132009-01-08 12:04:47 +0000646 kenter(",%p", vma);
647
648 BUG_ON(!vma->vm_region);
649
650 mm->map_count++;
651 vma->vm_mm = mm;
652
653 /* add the VMA to the mapping */
654 if (vma->vm_file) {
655 mapping = vma->vm_file->f_mapping;
656
657 flush_dcache_mmap_lock(mapping);
658 vma_prio_tree_insert(vma, &mapping->i_mmap);
659 flush_dcache_mmap_unlock(mapping);
660 }
661
662 /* add the VMA to the tree */
663 parent = NULL;
664 p = &mm->mm_rb.rb_node;
665 while (*p) {
666 parent = *p;
667 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
668
669 /* sort by: start addr, end addr, VMA struct addr in that order
670 * (the latter is necessary as we may get identical VMAs) */
671 if (vma->vm_start < pvma->vm_start)
672 p = &(*p)->rb_left;
673 else if (vma->vm_start > pvma->vm_start)
674 p = &(*p)->rb_right;
675 else if (vma->vm_end < pvma->vm_end)
676 p = &(*p)->rb_left;
677 else if (vma->vm_end > pvma->vm_end)
678 p = &(*p)->rb_right;
679 else if (vma < pvma)
680 p = &(*p)->rb_left;
681 else if (vma > pvma)
682 p = &(*p)->rb_right;
683 else
684 BUG();
685 }
686
687 rb_link_node(&vma->vm_rb, parent, p);
688 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
689
690 /* add VMA to the VMA list also */
691 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
692 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700693 break;
David Howells8feae132009-01-08 12:04:47 +0000694 if (pvma->vm_start < vma->vm_start)
695 continue;
696 if (pvma->vm_end < vma->vm_end)
697 break;
698 }
David Howells30340972006-09-27 01:50:20 -0700699
David Howells8feae132009-01-08 12:04:47 +0000700 vma->vm_next = *pp;
701 *pp = vma;
702}
703
704/*
705 * delete a VMA from its owning mm_struct and address space
706 */
707static void delete_vma_from_mm(struct vm_area_struct *vma)
708{
709 struct vm_area_struct **pp;
710 struct address_space *mapping;
711 struct mm_struct *mm = vma->vm_mm;
712
713 kenter("%p", vma);
714
715 mm->map_count--;
716 if (mm->mmap_cache == vma)
717 mm->mmap_cache = NULL;
718
719 /* remove the VMA from the mapping */
720 if (vma->vm_file) {
721 mapping = vma->vm_file->f_mapping;
722
723 flush_dcache_mmap_lock(mapping);
724 vma_prio_tree_remove(vma, &mapping->i_mmap);
725 flush_dcache_mmap_unlock(mapping);
726 }
727
728 /* remove from the MM's tree and list */
729 rb_erase(&vma->vm_rb, &mm->mm_rb);
730 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
731 if (*pp == vma) {
732 *pp = vma->vm_next;
733 break;
734 }
735 }
736
737 vma->vm_mm = NULL;
738}
739
740/*
741 * destroy a VMA record
742 */
743static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
744{
745 kenter("%p", vma);
746 if (vma->vm_ops && vma->vm_ops->close)
747 vma->vm_ops->close(vma);
748 if (vma->vm_file) {
749 fput(vma->vm_file);
750 if (vma->vm_flags & VM_EXECUTABLE)
751 removed_exe_file_vma(mm);
752 }
753 put_nommu_region(vma->vm_region);
754 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700755}
756
757/*
758 * look up the first VMA in which addr resides, NULL if none
759 * - should be called with mm->mmap_sem at least held readlocked
760 */
761struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
762{
David Howells8feae132009-01-08 12:04:47 +0000763 struct vm_area_struct *vma;
764 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700765
David Howells8feae132009-01-08 12:04:47 +0000766 /* check the cache first */
767 vma = mm->mmap_cache;
768 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
769 return vma;
770
771 /* trawl the tree (there may be multiple mappings in which addr
772 * resides) */
773 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
774 vma = rb_entry(n, struct vm_area_struct, vm_rb);
775 if (vma->vm_start > addr)
776 return NULL;
777 if (vma->vm_end > addr) {
778 mm->mmap_cache = vma;
779 return vma;
780 }
David Howells30340972006-09-27 01:50:20 -0700781 }
782
David Howells30340972006-09-27 01:50:20 -0700783 return NULL;
784}
785EXPORT_SYMBOL(find_vma);
786
787/*
David Howells930e6522006-09-27 01:50:22 -0700788 * find a VMA
789 * - we don't extend stack VMAs under NOMMU conditions
790 */
791struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
792{
793 return find_vma(mm, addr);
794}
795
David Howells8feae132009-01-08 12:04:47 +0000796/*
797 * expand a stack to a given address
798 * - not supported under NOMMU conditions
799 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700800int expand_stack(struct vm_area_struct *vma, unsigned long address)
801{
802 return -ENOMEM;
803}
804
David Howells930e6522006-09-27 01:50:22 -0700805/*
David Howells6fa5f802006-09-27 01:50:21 -0700806 * look up the first VMA exactly that exactly matches addr
807 * - should be called with mm->mmap_sem at least held readlocked
808 */
David Howells8feae132009-01-08 12:04:47 +0000809static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
810 unsigned long addr,
811 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000814 struct rb_node *n = mm->mm_rb.rb_node;
815 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
David Howells8feae132009-01-08 12:04:47 +0000817 /* check the cache first */
818 vma = mm->mmap_cache;
819 if (vma && vma->vm_start == addr && vma->vm_end == end)
820 return vma;
821
822 /* trawl the tree (there may be multiple mappings in which addr
823 * resides) */
824 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000826 if (vma->vm_start < addr)
827 continue;
828 if (vma->vm_start > addr)
829 return NULL;
830 if (vma->vm_end == end) {
831 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return vma;
David Howells8feae132009-01-08 12:04:47 +0000833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 return NULL;
837}
838
David Howells30340972006-09-27 01:50:20 -0700839/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * determine whether a mapping should be permitted and, if so, what sort of
841 * mapping we're capable of supporting
842 */
843static int validate_mmap_request(struct file *file,
844 unsigned long addr,
845 unsigned long len,
846 unsigned long prot,
847 unsigned long flags,
848 unsigned long pgoff,
849 unsigned long *_capabilities)
850{
David Howells8feae132009-01-08 12:04:47 +0000851 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long reqprot = prot;
853 int ret;
854
855 /* do the simple checks first */
856 if (flags & MAP_FIXED || addr) {
857 printk(KERN_DEBUG
858 "%d: Can't do fixed-address/overlay mmap of RAM\n",
859 current->pid);
860 return -EINVAL;
861 }
862
863 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
864 (flags & MAP_TYPE) != MAP_SHARED)
865 return -EINVAL;
866
Mike Frysingerf81cff02006-12-06 12:02:59 +1000867 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -EINVAL;
869
Mike Frysingerf81cff02006-12-06 12:02:59 +1000870 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000871 rlen = PAGE_ALIGN(len);
872 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000873 return -ENOMEM;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000876 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000877 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (file) {
880 /* validate file mapping requests */
881 struct address_space *mapping;
882
883 /* files must support mmap */
884 if (!file->f_op || !file->f_op->mmap)
885 return -ENODEV;
886
887 /* work out if what we've got could possibly be shared
888 * - we support chardevs that provide their own "memory"
889 * - we support files/blockdevs that are memory backed
890 */
891 mapping = file->f_mapping;
892 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800893 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 capabilities = 0;
896 if (mapping && mapping->backing_dev_info)
897 capabilities = mapping->backing_dev_info->capabilities;
898
899 if (!capabilities) {
900 /* no explicit capabilities set, so assume some
901 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800902 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 case S_IFREG:
904 case S_IFBLK:
905 capabilities = BDI_CAP_MAP_COPY;
906 break;
907
908 case S_IFCHR:
909 capabilities =
910 BDI_CAP_MAP_DIRECT |
911 BDI_CAP_READ_MAP |
912 BDI_CAP_WRITE_MAP;
913 break;
914
915 default:
916 return -EINVAL;
917 }
918 }
919
920 /* eliminate any capabilities that we can't support on this
921 * device */
922 if (!file->f_op->get_unmapped_area)
923 capabilities &= ~BDI_CAP_MAP_DIRECT;
924 if (!file->f_op->read)
925 capabilities &= ~BDI_CAP_MAP_COPY;
926
927 if (flags & MAP_SHARED) {
928 /* do checks for writing, appending and locking */
929 if ((prot & PROT_WRITE) &&
930 !(file->f_mode & FMODE_WRITE))
931 return -EACCES;
932
Josef Sipeke9536ae2006-12-08 02:37:21 -0800933 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 (file->f_mode & FMODE_WRITE))
935 return -EACCES;
936
Josef Sipeke9536ae2006-12-08 02:37:21 -0800937 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EAGAIN;
939
940 if (!(capabilities & BDI_CAP_MAP_DIRECT))
941 return -ENODEV;
942
943 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
944 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
945 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
946 ) {
947 printk("MAP_SHARED not completely supported on !MMU\n");
948 return -EINVAL;
949 }
950
951 /* we mustn't privatise shared mappings */
952 capabilities &= ~BDI_CAP_MAP_COPY;
953 }
954 else {
955 /* we're going to read the file into private memory we
956 * allocate */
957 if (!(capabilities & BDI_CAP_MAP_COPY))
958 return -ENODEV;
959
960 /* we don't permit a private writable mapping to be
961 * shared with the backing device */
962 if (prot & PROT_WRITE)
963 capabilities &= ~BDI_CAP_MAP_DIRECT;
964 }
965
966 /* handle executable mappings and implied executable
967 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800968 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (prot & PROT_EXEC)
970 return -EPERM;
971 }
972 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
973 /* handle implication of PROT_EXEC by PROT_READ */
974 if (current->personality & READ_IMPLIES_EXEC) {
975 if (capabilities & BDI_CAP_EXEC_MAP)
976 prot |= PROT_EXEC;
977 }
978 }
979 else if ((prot & PROT_READ) &&
980 (prot & PROT_EXEC) &&
981 !(capabilities & BDI_CAP_EXEC_MAP)
982 ) {
983 /* backing file is not executable, try to copy */
984 capabilities &= ~BDI_CAP_MAP_DIRECT;
985 }
986 }
987 else {
988 /* anonymous mappings are always memory backed and can be
989 * privately mapped
990 */
991 capabilities = BDI_CAP_MAP_COPY;
992
993 /* handle PROT_EXEC implication by PROT_READ */
994 if ((prot & PROT_READ) &&
995 (current->personality & READ_IMPLIES_EXEC))
996 prot |= PROT_EXEC;
997 }
998
999 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -04001000 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (ret < 0)
1002 return ret;
1003
1004 /* looks okay */
1005 *_capabilities = capabilities;
1006 return 0;
1007}
1008
1009/*
1010 * we've determined that we can make the mapping, now translate what we
1011 * now know into VMA flags
1012 */
1013static unsigned long determine_vm_flags(struct file *file,
1014 unsigned long prot,
1015 unsigned long flags,
1016 unsigned long capabilities)
1017{
1018 unsigned long vm_flags;
1019
1020 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1021 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1022 /* vm_flags |= mm->def_flags; */
1023
1024 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1025 /* attempt to share read-only copies of mapped file chunks */
1026 if (file && !(prot & PROT_WRITE))
1027 vm_flags |= VM_MAYSHARE;
1028 }
1029 else {
1030 /* overlay a shareable mapping on the backing device or inode
1031 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1032 * romfs/cramfs */
1033 if (flags & MAP_SHARED)
1034 vm_flags |= VM_MAYSHARE | VM_SHARED;
1035 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
1036 vm_flags |= VM_MAYSHARE;
1037 }
1038
1039 /* refuse to let anyone share private mappings with this process if
1040 * it's being traced - otherwise breakpoints set in it may interfere
1041 * with another untraced process
1042 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001043 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 vm_flags &= ~VM_MAYSHARE;
1045
1046 return vm_flags;
1047}
1048
1049/*
David Howells8feae132009-01-08 12:04:47 +00001050 * set up a shared mapping on a file (the driver or filesystem provides and
1051 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
David Howells8feae132009-01-08 12:04:47 +00001053static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 int ret;
1056
1057 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001058 if (ret == 0) {
1059 vma->vm_region->vm_top = vma->vm_region->vm_end;
1060 return ret;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (ret != -ENOSYS)
1063 return ret;
1064
1065 /* getting an ENOSYS error indicates that direct mmap isn't
1066 * possible (as opposed to tried but failed) so we'll fall
1067 * through to making a private copy of the data and mapping
1068 * that if we can */
1069 return -ENODEV;
1070}
1071
1072/*
1073 * set up a private mapping or an anonymous shared mapping
1074 */
David Howells8feae132009-01-08 12:04:47 +00001075static int do_mmap_private(struct vm_area_struct *vma,
1076 struct vm_region *region,
1077 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
David Howells8feae132009-01-08 12:04:47 +00001079 struct page *pages;
1080 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 void *base;
David Howells8feae132009-01-08 12:04:47 +00001082 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /* invoke the file's mapping function so that it can keep track of
1085 * shared mappings on devices or memory
1086 * - VM_MAYSHARE will be set if it may attempt to share
1087 */
1088 if (vma->vm_file) {
1089 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001090 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001092 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1093 vma->vm_region->vm_top = vma->vm_region->vm_end;
1094 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001096 if (ret != -ENOSYS)
1097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* getting an ENOSYS error indicates that direct mmap isn't
1100 * possible (as opposed to tried but failed) so we'll try to
1101 * make a private copy of the data and map that instead */
1102 }
1103
David Howells8feae132009-01-08 12:04:47 +00001104 rlen = PAGE_ALIGN(len);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* allocate some memory to hold the mapping
1107 * - note that this may not return a page-aligned address if the object
1108 * we're allocating is smaller than a page
1109 */
David Howells8feae132009-01-08 12:04:47 +00001110 order = get_order(rlen);
1111 kdebug("alloc order %d for %lx", order, len);
1112
1113 pages = alloc_pages(GFP_KERNEL, order);
1114 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 goto enomem;
1116
David Howells8feae132009-01-08 12:04:47 +00001117 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001118 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
David Howells8feae132009-01-08 12:04:47 +00001120 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001121
1122 /* we allocated a power-of-2 sized page set, so we may want to trim off
1123 * the excess */
1124 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1125 while (total > point) {
1126 order = ilog2(total - point);
1127 n = 1 << order;
1128 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001129 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001130 total -= n;
1131 set_page_refcounted(pages + total);
1132 __free_pages(pages + total, order);
1133 }
David Howells8feae132009-01-08 12:04:47 +00001134 }
1135
David Howells8feae132009-01-08 12:04:47 +00001136 for (point = 1; point < total; point++)
1137 set_page_refcounted(&pages[point]);
1138
1139 base = page_address(pages);
1140 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1141 region->vm_start = (unsigned long) base;
1142 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001143 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001144
1145 vma->vm_start = region->vm_start;
1146 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (vma->vm_file) {
1149 /* read the contents of a file into the copy */
1150 mm_segment_t old_fs;
1151 loff_t fpos;
1152
1153 fpos = vma->vm_pgoff;
1154 fpos <<= PAGE_SHIFT;
1155
1156 old_fs = get_fs();
1157 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001158 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 set_fs(old_fs);
1160
1161 if (ret < 0)
1162 goto error_free;
1163
1164 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001165 if (ret < rlen)
1166 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 } else {
1169 /* if it's an anonymous mapping, then just clear it */
David Howells8feae132009-01-08 12:04:47 +00001170 memset(base, 0, rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173 return 0;
1174
1175error_free:
David Howells8feae132009-01-08 12:04:47 +00001176 free_page_series(region->vm_start, region->vm_end);
1177 region->vm_start = vma->vm_start = 0;
1178 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001179 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return ret;
1181
1182enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001183 printk("Allocation of length %lu from process %d (%s) failed\n",
1184 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 show_free_areas();
1186 return -ENOMEM;
1187}
1188
1189/*
1190 * handle mapping creation for uClinux
1191 */
1192unsigned long do_mmap_pgoff(struct file *file,
1193 unsigned long addr,
1194 unsigned long len,
1195 unsigned long prot,
1196 unsigned long flags,
1197 unsigned long pgoff)
1198{
David Howells8feae132009-01-08 12:04:47 +00001199 struct vm_area_struct *vma;
1200 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001202 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int ret;
1204
David Howells8feae132009-01-08 12:04:47 +00001205 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1206
Eric Paris7cd94142007-11-26 18:47:40 -05001207 if (!(flags & MAP_FIXED))
1208 addr = round_hint_to_min(addr);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* decide whether we should attempt the mapping, and if so what sort of
1211 * mapping */
1212 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1213 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001214 if (ret < 0) {
1215 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return ret;
David Howells8feae132009-01-08 12:04:47 +00001217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* we've determined that we can make the mapping, now translate what we
1220 * now know into VMA flags */
1221 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1222
David Howells8feae132009-01-08 12:04:47 +00001223 /* we're going to need to record the mapping */
1224 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1225 if (!region)
1226 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
David Howells8feae132009-01-08 12:04:47 +00001228 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1229 if (!vma)
1230 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
David Howells8feae132009-01-08 12:04:47 +00001232 atomic_set(&region->vm_usage, 1);
1233 region->vm_flags = vm_flags;
1234 region->vm_pgoff = pgoff;
1235
1236 INIT_LIST_HEAD(&vma->anon_vma_node);
1237 vma->vm_flags = vm_flags;
1238 vma->vm_pgoff = pgoff;
1239
1240 if (file) {
1241 region->vm_file = file;
1242 get_file(file);
1243 vma->vm_file = file;
1244 get_file(file);
1245 if (vm_flags & VM_EXECUTABLE) {
1246 added_exe_file_vma(current->mm);
1247 vma->vm_mm = current->mm;
1248 }
1249 }
1250
1251 down_write(&nommu_region_sem);
1252
1253 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001255 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * - shared mappings on character devices and memory backed files are
1257 * permitted to overlap inexactly as far as we are concerned for in
1258 * these cases, sharing is handled in the driver or filesystem rather
1259 * than here
1260 */
1261 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001262 struct vm_region *pregion;
1263 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
David Howells8feae132009-01-08 12:04:47 +00001265 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1266 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001267
David Howells8feae132009-01-08 12:04:47 +00001268 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1269 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
David Howells8feae132009-01-08 12:04:47 +00001271 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 continue;
1273
1274 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001275 if (pregion->vm_file->f_path.dentry->d_inode !=
1276 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 continue;
1278
David Howells8feae132009-01-08 12:04:47 +00001279 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 continue;
1281
David Howells8feae132009-01-08 12:04:47 +00001282 rpglen = pregion->vm_end - pregion->vm_start;
1283 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1284 rpgend = pregion->vm_pgoff + rpglen;
1285 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 continue;
1287
David Howells8feae132009-01-08 12:04:47 +00001288 /* handle inexactly overlapping matches between
1289 * mappings */
1290 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1291 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1292 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1294 goto sharing_violation;
1295 continue;
1296 }
1297
David Howells8feae132009-01-08 12:04:47 +00001298 /* we've found a region we can share */
1299 atomic_inc(&pregion->vm_usage);
1300 vma->vm_region = pregion;
1301 start = pregion->vm_start;
1302 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1303 vma->vm_start = start;
1304 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
David Howells8feae132009-01-08 12:04:47 +00001306 if (pregion->vm_flags & VM_MAPPED_COPY) {
1307 kdebug("share copy");
1308 vma->vm_flags |= VM_MAPPED_COPY;
1309 } else {
1310 kdebug("share mmap");
1311 ret = do_mmap_shared_file(vma);
1312 if (ret < 0) {
1313 vma->vm_region = NULL;
1314 vma->vm_start = 0;
1315 vma->vm_end = 0;
1316 atomic_dec(&pregion->vm_usage);
1317 pregion = NULL;
1318 goto error_just_free;
1319 }
1320 }
1321 fput(region->vm_file);
1322 kmem_cache_free(vm_region_jar, region);
1323 region = pregion;
1324 result = start;
1325 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* obtain the address at which to make a shared mapping
1329 * - this is the hook for quasi-memory character devices to
1330 * tell us the location of a shared mapping
1331 */
1332 if (file && file->f_op->get_unmapped_area) {
1333 addr = file->f_op->get_unmapped_area(file, addr, len,
1334 pgoff, flags);
1335 if (IS_ERR((void *) addr)) {
1336 ret = addr;
1337 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001338 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* the driver refused to tell us where to site
1341 * the mapping so we'll have to attempt to copy
1342 * it */
1343 ret = (unsigned long) -ENODEV;
1344 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001345 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001348 } else {
1349 vma->vm_start = region->vm_start = addr;
1350 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 }
1353 }
1354
David Howells8feae132009-01-08 12:04:47 +00001355 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* set up the mapping */
1358 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001359 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 else
David Howells8feae132009-01-08 12:04:47 +00001361 ret = do_mmap_private(vma, region, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001363 goto error_put_region;
1364
1365 add_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001368 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 current->mm->total_vm += len >> PAGE_SHIFT;
1371
David Howells8feae132009-01-08 12:04:47 +00001372share:
1373 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David Howells8feae132009-01-08 12:04:47 +00001375 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 if (prot & PROT_EXEC)
David Howells8feae132009-01-08 12:04:47 +00001378 flush_icache_range(result, result + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
David Howells8feae132009-01-08 12:04:47 +00001380 kleave(" = %lx", result);
1381 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
David Howells8feae132009-01-08 12:04:47 +00001383error_put_region:
1384 __put_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (vma) {
Matt Helsley925d1c42008-04-29 01:01:36 -07001386 if (vma->vm_file) {
Gavin Lambert3fcd03e2006-09-30 23:27:01 -07001387 fput(vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07001388 if (vma->vm_flags & VM_EXECUTABLE)
1389 removed_exe_file_vma(vma->vm_mm);
1390 }
David Howells8feae132009-01-08 12:04:47 +00001391 kmem_cache_free(vm_area_cachep, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
David Howells8feae132009-01-08 12:04:47 +00001393 kleave(" = %d [pr]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return ret;
1395
David Howells8feae132009-01-08 12:04:47 +00001396error_just_free:
1397 up_write(&nommu_region_sem);
1398error:
1399 fput(region->vm_file);
1400 kmem_cache_free(vm_region_jar, region);
1401 fput(vma->vm_file);
1402 if (vma->vm_flags & VM_EXECUTABLE)
1403 removed_exe_file_vma(vma->vm_mm);
1404 kmem_cache_free(vm_area_cachep, vma);
1405 kleave(" = %d", ret);
1406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
David Howells8feae132009-01-08 12:04:47 +00001408sharing_violation:
1409 up_write(&nommu_region_sem);
1410 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1411 ret = -EINVAL;
1412 goto error;
1413
1414error_getting_vma:
1415 kmem_cache_free(vm_region_jar, region);
1416 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1417 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 len, current->pid);
1419 show_free_areas();
1420 return -ENOMEM;
1421
David Howells8feae132009-01-08 12:04:47 +00001422error_getting_region:
1423 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1424 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 len, current->pid);
1426 show_free_areas();
1427 return -ENOMEM;
1428}
Paul Mundtb5073172007-07-21 04:37:25 -07001429EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431/*
David Howells8feae132009-01-08 12:04:47 +00001432 * split a vma into two pieces at address 'addr', a new vma is allocated either
1433 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
David Howells8feae132009-01-08 12:04:47 +00001435int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1436 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
David Howells8feae132009-01-08 12:04:47 +00001438 struct vm_area_struct *new;
1439 struct vm_region *region;
1440 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
David Howells8feae132009-01-08 12:04:47 +00001442 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
David Howells8feae132009-01-08 12:04:47 +00001444 /* we're only permitted to split anonymous regions that have a single
1445 * owner */
1446 if (vma->vm_file ||
1447 atomic_read(&vma->vm_region->vm_usage) != 1)
1448 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
David Howells8feae132009-01-08 12:04:47 +00001450 if (mm->map_count >= sysctl_max_map_count)
1451 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
David Howells8feae132009-01-08 12:04:47 +00001453 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1454 if (!region)
1455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
David Howells8feae132009-01-08 12:04:47 +00001457 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1458 if (!new) {
1459 kmem_cache_free(vm_region_jar, region);
1460 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
David Howells8feae132009-01-08 12:04:47 +00001462
1463 /* most fields are the same, copy all, and then fixup */
1464 *new = *vma;
1465 *region = *vma->vm_region;
1466 new->vm_region = region;
1467
1468 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1469
1470 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001471 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001472 } else {
1473 region->vm_start = new->vm_start = addr;
1474 region->vm_pgoff = new->vm_pgoff += npages;
1475 }
1476
1477 if (new->vm_ops && new->vm_ops->open)
1478 new->vm_ops->open(new);
1479
1480 delete_vma_from_mm(vma);
1481 down_write(&nommu_region_sem);
1482 delete_nommu_region(vma->vm_region);
1483 if (new_below) {
1484 vma->vm_region->vm_start = vma->vm_start = addr;
1485 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1486 } else {
1487 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001488 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001489 }
1490 add_nommu_region(vma->vm_region);
1491 add_nommu_region(new->vm_region);
1492 up_write(&nommu_region_sem);
1493 add_vma_to_mm(mm, vma);
1494 add_vma_to_mm(mm, new);
1495 return 0;
1496}
1497
1498/*
1499 * shrink a VMA by removing the specified chunk from either the beginning or
1500 * the end
1501 */
1502static int shrink_vma(struct mm_struct *mm,
1503 struct vm_area_struct *vma,
1504 unsigned long from, unsigned long to)
1505{
1506 struct vm_region *region;
1507
1508 kenter("");
1509
1510 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1511 * and list */
1512 delete_vma_from_mm(vma);
1513 if (from > vma->vm_start)
1514 vma->vm_end = from;
1515 else
1516 vma->vm_start = to;
1517 add_vma_to_mm(mm, vma);
1518
1519 /* cut the backing region down to size */
1520 region = vma->vm_region;
1521 BUG_ON(atomic_read(&region->vm_usage) != 1);
1522
1523 down_write(&nommu_region_sem);
1524 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001525 if (from > region->vm_start) {
1526 to = region->vm_top;
1527 region->vm_top = region->vm_end = from;
1528 } else {
David Howells8feae132009-01-08 12:04:47 +00001529 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001530 }
David Howells8feae132009-01-08 12:04:47 +00001531 add_nommu_region(region);
1532 up_write(&nommu_region_sem);
1533
1534 free_page_series(from, to);
1535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
David Howells30340972006-09-27 01:50:20 -07001538/*
1539 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001540 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1541 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001542 */
David Howells8feae132009-01-08 12:04:47 +00001543int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
David Howells8feae132009-01-08 12:04:47 +00001545 struct vm_area_struct *vma;
1546 struct rb_node *rb;
1547 unsigned long end = start + len;
1548 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
David Howells8feae132009-01-08 12:04:47 +00001550 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David Howells8feae132009-01-08 12:04:47 +00001552 if (len == 0)
1553 return -EINVAL;
1554
1555 /* find the first potentially overlapping VMA */
1556 vma = find_vma(mm, start);
1557 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001558 static int limit = 0;
1559 if (limit < 5) {
1560 printk(KERN_WARNING
1561 "munmap of memory not mmapped by process %d"
1562 " (%s): 0x%lx-0x%lx\n",
1563 current->pid, current->comm,
1564 start, start + len - 1);
1565 limit++;
1566 }
David Howells8feae132009-01-08 12:04:47 +00001567 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
David Howells8feae132009-01-08 12:04:47 +00001570 /* we're allowed to split an anonymous VMA but not a file-backed one */
1571 if (vma->vm_file) {
1572 do {
1573 if (start > vma->vm_start) {
1574 kleave(" = -EINVAL [miss]");
1575 return -EINVAL;
1576 }
1577 if (end == vma->vm_end)
1578 goto erase_whole_vma;
1579 rb = rb_next(&vma->vm_rb);
1580 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1581 } while (rb);
1582 kleave(" = -EINVAL [split file]");
1583 return -EINVAL;
1584 } else {
1585 /* the chunk must be a subset of the VMA found */
1586 if (start == vma->vm_start && end == vma->vm_end)
1587 goto erase_whole_vma;
1588 if (start < vma->vm_start || end > vma->vm_end) {
1589 kleave(" = -EINVAL [superset]");
1590 return -EINVAL;
1591 }
1592 if (start & ~PAGE_MASK) {
1593 kleave(" = -EINVAL [unaligned start]");
1594 return -EINVAL;
1595 }
1596 if (end != vma->vm_end && end & ~PAGE_MASK) {
1597 kleave(" = -EINVAL [unaligned split]");
1598 return -EINVAL;
1599 }
1600 if (start != vma->vm_start && end != vma->vm_end) {
1601 ret = split_vma(mm, vma, start, 1);
1602 if (ret < 0) {
1603 kleave(" = %d [split]", ret);
1604 return ret;
1605 }
1606 }
1607 return shrink_vma(mm, vma, start, end);
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
David Howells8feae132009-01-08 12:04:47 +00001610erase_whole_vma:
1611 delete_vma_from_mm(vma);
1612 delete_vma(mm, vma);
1613 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
1615}
Paul Mundtb5073172007-07-21 04:37:25 -07001616EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001618SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001619{
1620 int ret;
1621 struct mm_struct *mm = current->mm;
1622
1623 down_write(&mm->mmap_sem);
1624 ret = do_munmap(mm, addr, len);
1625 up_write(&mm->mmap_sem);
1626 return ret;
1627}
1628
1629/*
David Howells8feae132009-01-08 12:04:47 +00001630 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001631 */
David Howells8feae132009-01-08 12:04:47 +00001632void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
David Howells8feae132009-01-08 12:04:47 +00001634 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
David Howells8feae132009-01-08 12:04:47 +00001636 if (!mm)
1637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
David Howells8feae132009-01-08 12:04:47 +00001639 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David Howells8feae132009-01-08 12:04:47 +00001641 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
David Howells8feae132009-01-08 12:04:47 +00001643 while ((vma = mm->mmap)) {
1644 mm->mmap = vma->vm_next;
1645 delete_vma_from_mm(vma);
1646 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
David Howells8feae132009-01-08 12:04:47 +00001648
1649 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652unsigned long do_brk(unsigned long addr, unsigned long len)
1653{
1654 return -ENOMEM;
1655}
1656
1657/*
David Howells6fa5f802006-09-27 01:50:21 -07001658 * expand (or shrink) an existing mapping, potentially moving it at the same
1659 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 *
David Howells6fa5f802006-09-27 01:50:21 -07001661 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001662 * as long as it stays within the region allocated by do_mmap_private() and the
1663 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 *
David Howells6fa5f802006-09-27 01:50:21 -07001665 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
1667unsigned long do_mremap(unsigned long addr,
1668 unsigned long old_len, unsigned long new_len,
1669 unsigned long flags, unsigned long new_addr)
1670{
David Howells6fa5f802006-09-27 01:50:21 -07001671 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001674 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return (unsigned long) -EINVAL;
1676
David Howells8feae132009-01-08 12:04:47 +00001677 if (addr & ~PAGE_MASK)
1678 return -EINVAL;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (flags & MREMAP_FIXED && new_addr != addr)
1681 return (unsigned long) -EINVAL;
1682
David Howells8feae132009-01-08 12:04:47 +00001683 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001684 if (!vma)
1685 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
David Howells6fa5f802006-09-27 01:50:21 -07001687 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return (unsigned long) -EFAULT;
1689
David Howells6fa5f802006-09-27 01:50:21 -07001690 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return (unsigned long) -EPERM;
1692
David Howells8feae132009-01-08 12:04:47 +00001693 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return (unsigned long) -ENOMEM;
1695
1696 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001697 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001698 return vma->vm_start;
1699}
Paul Mundtb5073172007-07-21 04:37:25 -07001700EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001701
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001702SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1703 unsigned long, new_len, unsigned long, flags,
1704 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001705{
1706 unsigned long ret;
1707
1708 down_write(&current->mm->mmap_sem);
1709 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1710 up_write(&current->mm->mmap_sem);
1711 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Linus Torvalds6aab3412005-11-28 14:34:23 -08001714struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001715 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 return NULL;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1721 unsigned long to, unsigned long size, pgprot_t prot)
1722{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001723 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
Luke Yang22c4af42006-07-14 00:24:09 -07001726EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Paul Mundtf905bc42008-02-04 22:29:59 -08001728int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1729 unsigned long pgoff)
1730{
1731 unsigned int size = vma->vm_end - vma->vm_start;
1732
1733 if (!(vma->vm_flags & VM_USERMAP))
1734 return -EINVAL;
1735
1736 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1737 vma->vm_end = vma->vm_start + size;
1738
1739 return 0;
1740}
1741EXPORT_SYMBOL(remap_vmalloc_range);
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1744{
1745}
1746
1747unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1748 unsigned long len, unsigned long pgoff, unsigned long flags)
1749{
1750 return -ENOMEM;
1751}
1752
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001753void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757void unmap_mapping_range(struct address_space *mapping,
1758 loff_t const holebegin, loff_t const holelen,
1759 int even_cows)
1760{
1761}
Luke Yang22c4af42006-07-14 00:24:09 -07001762EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764/*
David Howellsd56e03c2007-03-22 00:11:23 -08001765 * ask for an unmapped area at which to create a mapping on a file
1766 */
1767unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1768 unsigned long len, unsigned long pgoff,
1769 unsigned long flags)
1770{
1771 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1772 unsigned long, unsigned long);
1773
1774 get_area = current->mm->get_unmapped_area;
1775 if (file && file->f_op && file->f_op->get_unmapped_area)
1776 get_area = file->f_op->get_unmapped_area;
1777
1778 if (!get_area)
1779 return -ENOSYS;
1780
1781 return get_area(file, addr, len, pgoff, flags);
1782}
David Howellsd56e03c2007-03-22 00:11:23 -08001783EXPORT_SYMBOL(get_unmapped_area);
1784
1785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * Check that a process has enough memory to allocate a new virtual
1787 * mapping. 0 means there is enough memory for the allocation to
1788 * succeed and -ENOMEM implies there is not.
1789 *
1790 * We currently support three overcommit policies, which are set via the
1791 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1792 *
1793 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1794 * Additional code 2002 Jul 20 by Robert Love.
1795 *
1796 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1797 *
1798 * Note this is a helper function intended to be used by LSMs which
1799 * wish to use this logic.
1800 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001801int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 unsigned long free, allowed;
1804
1805 vm_acct_memory(pages);
1806
1807 /*
1808 * Sometimes we want to use more memory than we have
1809 */
1810 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1811 return 0;
1812
1813 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1814 unsigned long n;
1815
Christoph Lameter347ce432006-06-30 01:55:35 -07001816 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 free += nr_swap_pages;
1818
1819 /*
1820 * Any slabs which are created with the
1821 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1822 * which are reclaimable, under pressure. The dentry
1823 * cache and most inode caches should fall into this
1824 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001825 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /*
1828 * Leave the last 3% for root
1829 */
1830 if (!cap_sys_admin)
1831 free -= free / 32;
1832
1833 if (free > pages)
1834 return 0;
1835
1836 /*
1837 * nr_free_pages() is very expensive on large systems,
1838 * only call if we're about to fail.
1839 */
1840 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001841
1842 /*
1843 * Leave reserved pages. The pages are not for anonymous pages.
1844 */
1845 if (n <= totalreserve_pages)
1846 goto error;
1847 else
1848 n -= totalreserve_pages;
1849
1850 /*
1851 * Leave the last 3% for root
1852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (!cap_sys_admin)
1854 n -= n / 32;
1855 free += n;
1856
1857 if (free > pages)
1858 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001859
1860 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
1863 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1864 /*
1865 * Leave the last 3% for root
1866 */
1867 if (!cap_sys_admin)
1868 allowed -= allowed / 32;
1869 allowed += total_swap_pages;
1870
1871 /* Don't let a single process grow too big:
1872 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001873 if (mm)
1874 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001876 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001878
Hideo AOKId5ddc792006-04-10 22:53:01 -07001879error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 vm_unacct_memory(pages);
1881
1882 return -ENOMEM;
1883}
1884
1885int in_gate_area_no_task(unsigned long addr)
1886{
1887 return 0;
1888}
David Howellsb0e15192006-01-06 00:11:42 -08001889
Nick Piggind0217ac2007-07-19 01:47:03 -07001890int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001891{
1892 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001893 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001894}
Paul Mundtb5073172007-07-21 04:37:25 -07001895EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001896
1897/*
1898 * Access another process' address space.
1899 * - source/target buffer must be kernel space
1900 */
1901int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1902{
David Howells0ec76a12006-09-27 01:50:15 -07001903 struct vm_area_struct *vma;
1904 struct mm_struct *mm;
1905
1906 if (addr + len < addr)
1907 return 0;
1908
1909 mm = get_task_mm(tsk);
1910 if (!mm)
1911 return 0;
1912
1913 down_read(&mm->mmap_sem);
1914
1915 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001916 vma = find_vma(mm, addr);
1917 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001918 /* don't overrun this mapping */
1919 if (addr + len >= vma->vm_end)
1920 len = vma->vm_end - addr;
1921
1922 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001923 if (write && vma->vm_flags & VM_MAYWRITE)
David Howells0ec76a12006-09-27 01:50:15 -07001924 len -= copy_to_user((void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001925 else if (!write && vma->vm_flags & VM_MAYREAD)
David Howells0ec76a12006-09-27 01:50:15 -07001926 len -= copy_from_user(buf, (void *) addr, len);
1927 else
1928 len = 0;
1929 } else {
1930 len = 0;
1931 }
1932
1933 up_read(&mm->mmap_sem);
1934 mmput(mm);
1935 return len;
1936}