blob: 0d363dfcf10e1de316ba3071dfa28f3d1c9ce8c8 [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 Mundtf905bc42008-02-04 22:29:59 -080013 * Copyright (c) 2007 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;
Alan Cox80119ef2008-05-23 13:04:31 -070065atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
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;
69int heap_stack_gap = 0;
70
David Howells8feae132009-01-08 12:04:47 +000071atomic_t mmap_pages_allocated;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070074EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David Howells8feae132009-01-08 12:04:47 +000076/* list of mapped, potentially shareable regions */
77static struct kmem_cache *vm_region_jar;
78struct rb_root nommu_region_tree = RB_ROOT;
79DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81struct vm_operations_struct generic_file_vm_ops = {
82};
83
84/*
85 * Handle all mappings that got truncated by a "truncate()"
86 * system call.
87 *
88 * NOTE! We have to be ready to update the memory sharing
89 * between the file and the memory map for a potential last
90 * incomplete page. Ugly, but necessary.
91 */
92int vmtruncate(struct inode *inode, loff_t offset)
93{
94 struct address_space *mapping = inode->i_mapping;
95 unsigned long limit;
96
97 if (inode->i_size < offset)
98 goto do_expand;
99 i_size_write(inode, offset);
100
101 truncate_inode_pages(mapping, offset);
102 goto out_truncate;
103
104do_expand:
105 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
106 if (limit != RLIM_INFINITY && offset > limit)
107 goto out_sig;
108 if (offset > inode->i_sb->s_maxbytes)
109 goto out;
110 i_size_write(inode, offset);
111
112out_truncate:
Al Viroacfa4382008-12-04 10:06:33 -0500113 if (inode->i_op->truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 inode->i_op->truncate(inode);
115 return 0;
116out_sig:
117 send_sig(SIGXFSZ, current, 0);
118out:
119 return -EFBIG;
120}
121
122EXPORT_SYMBOL(vmtruncate);
123
124/*
125 * Return the total memory allocated for this pointer, not
126 * just what the caller asked for.
127 *
128 * Doesn't have to be accurate, i.e. may have races.
129 */
130unsigned int kobjsize(const void *objp)
131{
132 struct page *page;
133
Michael Hennerich4016a132008-04-28 02:13:38 -0700134 /*
135 * If the object we have should not have ksize performed on it,
136 * return size of 0
137 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900138 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700139 return 0;
140
141 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700142
143 /*
144 * If the allocator sets PageSlab, we know the pointer came from
145 * kmalloc().
146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (PageSlab(page))
148 return ksize(objp);
149
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700150 /*
151 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900152 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700153 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900154 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Nick Pigginb291f002008-10-18 20:26:44 -0700157int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
158 unsigned long start, int len, int flags,
159 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700161 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700162 unsigned long vm_flags;
163 int i;
Nick Pigginb291f002008-10-18 20:26:44 -0700164 int write = !!(flags & GUP_FLAGS_WRITE);
165 int force = !!(flags & GUP_FLAGS_FORCE);
166 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
David Howells7b4d5b82006-09-27 01:50:18 -0700167
168 /* calculate required read or write permissions.
169 * - if 'force' is set, we only require the "MAY" flags.
170 */
171 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
172 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 for (i = 0; i < len; i++) {
Sonic Zhang910e46d2006-09-27 01:50:17 -0700175 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700176 if (!vma)
177 goto finish_or_fault;
178
179 /* protect what we can, including chardevs */
180 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
Nick Pigginb291f002008-10-18 20:26:44 -0700181 (!ignore && !(vm_flags & vma->vm_flags)))
David Howells7b4d5b82006-09-27 01:50:18 -0700182 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (pages) {
185 pages[i] = virt_to_page(start);
186 if (pages[i])
187 page_cache_get(pages[i]);
188 }
189 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700190 vmas[i] = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 start += PAGE_SIZE;
192 }
David Howells7b4d5b82006-09-27 01:50:18 -0700193
194 return i;
195
196finish_or_fault:
197 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Nick Pigginb291f002008-10-18 20:26:44 -0700199
200
201/*
202 * get a list of pages in an address range belonging to the specified process
203 * and indicate the VMA that covers each page
204 * - this is potentially dodgy as we may end incrementing the page count of a
205 * slab page or a secondary page from a compound page
206 * - don't permit access to VMAs that don't support it, such as I/O mappings
207 */
208int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
209 unsigned long start, int len, int write, int force,
210 struct page **pages, struct vm_area_struct **vmas)
211{
212 int flags = 0;
213
214 if (write)
215 flags |= GUP_FLAGS_WRITE;
216 if (force)
217 flags |= GUP_FLAGS_FORCE;
218
219 return __get_user_pages(tsk, mm,
220 start, len, flags,
221 pages, vmas);
222}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000223EXPORT_SYMBOL(get_user_pages);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225DEFINE_RWLOCK(vmlist_lock);
226struct vm_struct *vmlist;
227
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800228void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 kfree(addr);
231}
Paul Mundtb5073172007-07-21 04:37:25 -0700232EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Al Virodd0fc662005-10-07 07:46:04 +0100234void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200237 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
238 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
Nick Piggin84097512006-03-22 00:08:34 -0800240 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
Paul Mundtb5073172007-07-21 04:37:25 -0700242EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Paul Mundtf905bc42008-02-04 22:29:59 -0800244void *vmalloc_user(unsigned long size)
245{
246 void *ret;
247
248 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
249 PAGE_KERNEL);
250 if (ret) {
251 struct vm_area_struct *vma;
252
253 down_write(&current->mm->mmap_sem);
254 vma = find_vma(current->mm, (unsigned long)ret);
255 if (vma)
256 vma->vm_flags |= VM_USERMAP;
257 up_write(&current->mm->mmap_sem);
258 }
259
260 return ret;
261}
262EXPORT_SYMBOL(vmalloc_user);
263
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800264struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 return virt_to_page(addr);
267}
Paul Mundtb5073172007-07-21 04:37:25 -0700268EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800270unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 return page_to_pfn(virt_to_page(addr));
273}
Paul Mundtb5073172007-07-21 04:37:25 -0700274EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276long vread(char *buf, char *addr, unsigned long count)
277{
278 memcpy(buf, addr, count);
279 return count;
280}
281
282long vwrite(char *buf, char *addr, unsigned long count)
283{
284 /* Don't allow overflow */
285 if ((unsigned long) addr + count < count)
286 count = -(unsigned long) addr;
287
288 memcpy(addr, buf, count);
289 return(count);
290}
291
292/*
293 * vmalloc - allocate virtually continguos memory
294 *
295 * @size: allocation size
296 *
297 * Allocate enough pages to cover @size from the page level
298 * allocator and map them into continguos kernel virtual space.
299 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200300 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * use __vmalloc() instead.
302 */
303void *vmalloc(unsigned long size)
304{
305 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
306}
Andrew Mortonf6138882006-02-28 16:59:18 -0800307EXPORT_SYMBOL(vmalloc);
308
309void *vmalloc_node(unsigned long size, int node)
310{
311 return vmalloc(size);
312}
313EXPORT_SYMBOL(vmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Paul Mundt1af446e2008-08-04 16:01:47 +0900315#ifndef PAGE_KERNEL_EXEC
316# define PAGE_KERNEL_EXEC PAGE_KERNEL
317#endif
318
319/**
320 * vmalloc_exec - allocate virtually contiguous, executable memory
321 * @size: allocation size
322 *
323 * Kernel-internal function to allocate enough pages to cover @size
324 * the page level allocator and map them into contiguous and
325 * executable kernel virtual space.
326 *
327 * For tight control over page level allocator and protection flags
328 * use __vmalloc() instead.
329 */
330
331void *vmalloc_exec(unsigned long size)
332{
333 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
334}
335
Paul Mundtb5073172007-07-21 04:37:25 -0700336/**
337 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * @size: allocation size
339 *
340 * Allocate enough 32bit PA addressable pages to cover @size from the
341 * page level allocator and map them into continguos kernel virtual space.
342 */
343void *vmalloc_32(unsigned long size)
344{
345 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
346}
Paul Mundtb5073172007-07-21 04:37:25 -0700347EXPORT_SYMBOL(vmalloc_32);
348
349/**
350 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
351 * @size: allocation size
352 *
353 * The resulting memory area is 32bit addressable and zeroed so it can be
354 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800355 *
356 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
357 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700358 */
359void *vmalloc_32_user(unsigned long size)
360{
Paul Mundtf905bc42008-02-04 22:29:59 -0800361 /*
362 * We'll have to sort out the ZONE_DMA bits for 64-bit,
363 * but for now this can simply use vmalloc_user() directly.
364 */
365 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700366}
367EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
370{
371 BUG();
372 return NULL;
373}
Paul Mundtb5073172007-07-21 04:37:25 -0700374EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800376void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 BUG();
379}
Paul Mundtb5073172007-07-21 04:37:25 -0700380EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700383 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
384 * have one.
385 */
386void __attribute__((weak)) vmalloc_sync_all(void)
387{
388}
389
Paul Mundtb5073172007-07-21 04:37:25 -0700390int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
391 struct page *page)
392{
393 return -EINVAL;
394}
395EXPORT_SYMBOL(vm_insert_page);
396
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * sys_brk() for the most part doesn't need the global kernel
399 * lock, except when an application is doing something nasty
400 * like trying to un-brk an area that has already been mapped
401 * to a regular file. in this case, the unmapping will need
402 * to invoke file system routines that need the global lock.
403 */
404asmlinkage unsigned long sys_brk(unsigned long brk)
405{
406 struct mm_struct *mm = current->mm;
407
408 if (brk < mm->start_brk || brk > mm->context.end_brk)
409 return mm->brk;
410
411 if (mm->brk == brk)
412 return mm->brk;
413
414 /*
415 * Always allow shrinking brk
416 */
417 if (brk <= mm->brk) {
418 mm->brk = brk;
419 return brk;
420 }
421
422 /*
423 * Ok, looks good - let it rip.
424 */
425 return mm->brk = brk;
426}
427
David Howells8feae132009-01-08 12:04:47 +0000428/*
429 * initialise the VMA and region record slabs
430 */
431void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
David Howells8feae132009-01-08 12:04:47 +0000433 vm_region_jar = kmem_cache_create("vm_region_jar",
434 sizeof(struct vm_region), 0,
435 SLAB_PANIC, NULL);
436 vm_area_cachep = kmem_cache_create("vm_area_struct",
437 sizeof(struct vm_area_struct), 0,
438 SLAB_PANIC, NULL);
439}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
David Howells8feae132009-01-08 12:04:47 +0000441/*
442 * validate the region tree
443 * - the caller must hold the region lock
444 */
445#ifdef CONFIG_DEBUG_NOMMU_REGIONS
446static noinline void validate_nommu_regions(void)
447{
448 struct vm_region *region, *last;
449 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
David Howells8feae132009-01-08 12:04:47 +0000451 lastp = rb_first(&nommu_region_tree);
452 if (!lastp)
453 return;
454
455 last = rb_entry(lastp, struct vm_region, vm_rb);
456 if (unlikely(last->vm_end <= last->vm_start))
457 BUG();
458
459 while ((p = rb_next(lastp))) {
460 region = rb_entry(p, struct vm_region, vm_rb);
461 last = rb_entry(lastp, struct vm_region, vm_rb);
462
463 if (unlikely(region->vm_end <= region->vm_start))
464 BUG();
465 if (unlikely(region->vm_start < last->vm_end))
466 BUG();
467
468 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470}
David Howells8feae132009-01-08 12:04:47 +0000471#else
472#define validate_nommu_regions() do {} while(0)
473#endif
474
475/*
476 * add a region into the global tree
477 */
478static void add_nommu_region(struct vm_region *region)
479{
480 struct vm_region *pregion;
481 struct rb_node **p, *parent;
482
483 validate_nommu_regions();
484
485 BUG_ON(region->vm_start & ~PAGE_MASK);
486
487 parent = NULL;
488 p = &nommu_region_tree.rb_node;
489 while (*p) {
490 parent = *p;
491 pregion = rb_entry(parent, struct vm_region, vm_rb);
492 if (region->vm_start < pregion->vm_start)
493 p = &(*p)->rb_left;
494 else if (region->vm_start > pregion->vm_start)
495 p = &(*p)->rb_right;
496 else if (pregion == region)
497 return;
498 else
499 BUG();
500 }
501
502 rb_link_node(&region->vm_rb, parent, p);
503 rb_insert_color(&region->vm_rb, &nommu_region_tree);
504
505 validate_nommu_regions();
506}
507
508/*
509 * delete a region from the global tree
510 */
511static void delete_nommu_region(struct vm_region *region)
512{
513 BUG_ON(!nommu_region_tree.rb_node);
514
515 validate_nommu_regions();
516 rb_erase(&region->vm_rb, &nommu_region_tree);
517 validate_nommu_regions();
518}
519
520/*
521 * free a contiguous series of pages
522 */
523static void free_page_series(unsigned long from, unsigned long to)
524{
525 for (; from < to; from += PAGE_SIZE) {
526 struct page *page = virt_to_page(from);
527
528 kdebug("- free %lx", from);
529 atomic_dec(&mmap_pages_allocated);
530 if (page_count(page) != 1)
531 kdebug("free page %p [%d]", page, page_count(page));
532 put_page(page);
533 }
534}
535
536/*
537 * release a reference to a region
538 * - the caller must hold the region semaphore, which this releases
539 * - the region may not have been added to the tree yet, in which case vm_end
540 * will equal vm_start
541 */
542static void __put_nommu_region(struct vm_region *region)
543 __releases(nommu_region_sem)
544{
545 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
546
547 BUG_ON(!nommu_region_tree.rb_node);
548
549 if (atomic_dec_and_test(&region->vm_usage)) {
550 if (region->vm_end > region->vm_start)
551 delete_nommu_region(region);
552 up_write(&nommu_region_sem);
553
554 if (region->vm_file)
555 fput(region->vm_file);
556
557 /* IO memory and memory shared directly out of the pagecache
558 * from ramfs/tmpfs mustn't be released here */
559 if (region->vm_flags & VM_MAPPED_COPY) {
560 kdebug("free series");
561 free_page_series(region->vm_start, region->vm_end);
562 }
563 kmem_cache_free(vm_region_jar, region);
564 } else {
565 up_write(&nommu_region_sem);
566 }
567}
568
569/*
570 * release a reference to a region
571 */
572static void put_nommu_region(struct vm_region *region)
573{
574 down_write(&nommu_region_sem);
575 __put_nommu_region(region);
576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
David Howells30340972006-09-27 01:50:20 -0700578/*
579 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000580 * and tree and add to the address space's page tree also if not an anonymous
581 * page
David Howells30340972006-09-27 01:50:20 -0700582 * - should be called with mm->mmap_sem held writelocked
583 */
David Howells8feae132009-01-08 12:04:47 +0000584static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700585{
David Howells8feae132009-01-08 12:04:47 +0000586 struct vm_area_struct *pvma, **pp;
587 struct address_space *mapping;
588 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700589
David Howells8feae132009-01-08 12:04:47 +0000590 kenter(",%p", vma);
591
592 BUG_ON(!vma->vm_region);
593
594 mm->map_count++;
595 vma->vm_mm = mm;
596
597 /* add the VMA to the mapping */
598 if (vma->vm_file) {
599 mapping = vma->vm_file->f_mapping;
600
601 flush_dcache_mmap_lock(mapping);
602 vma_prio_tree_insert(vma, &mapping->i_mmap);
603 flush_dcache_mmap_unlock(mapping);
604 }
605
606 /* add the VMA to the tree */
607 parent = NULL;
608 p = &mm->mm_rb.rb_node;
609 while (*p) {
610 parent = *p;
611 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
612
613 /* sort by: start addr, end addr, VMA struct addr in that order
614 * (the latter is necessary as we may get identical VMAs) */
615 if (vma->vm_start < pvma->vm_start)
616 p = &(*p)->rb_left;
617 else if (vma->vm_start > pvma->vm_start)
618 p = &(*p)->rb_right;
619 else if (vma->vm_end < pvma->vm_end)
620 p = &(*p)->rb_left;
621 else if (vma->vm_end > pvma->vm_end)
622 p = &(*p)->rb_right;
623 else if (vma < pvma)
624 p = &(*p)->rb_left;
625 else if (vma > pvma)
626 p = &(*p)->rb_right;
627 else
628 BUG();
629 }
630
631 rb_link_node(&vma->vm_rb, parent, p);
632 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
633
634 /* add VMA to the VMA list also */
635 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
636 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700637 break;
David Howells8feae132009-01-08 12:04:47 +0000638 if (pvma->vm_start < vma->vm_start)
639 continue;
640 if (pvma->vm_end < vma->vm_end)
641 break;
642 }
David Howells30340972006-09-27 01:50:20 -0700643
David Howells8feae132009-01-08 12:04:47 +0000644 vma->vm_next = *pp;
645 *pp = vma;
646}
647
648/*
649 * delete a VMA from its owning mm_struct and address space
650 */
651static void delete_vma_from_mm(struct vm_area_struct *vma)
652{
653 struct vm_area_struct **pp;
654 struct address_space *mapping;
655 struct mm_struct *mm = vma->vm_mm;
656
657 kenter("%p", vma);
658
659 mm->map_count--;
660 if (mm->mmap_cache == vma)
661 mm->mmap_cache = NULL;
662
663 /* remove the VMA from the mapping */
664 if (vma->vm_file) {
665 mapping = vma->vm_file->f_mapping;
666
667 flush_dcache_mmap_lock(mapping);
668 vma_prio_tree_remove(vma, &mapping->i_mmap);
669 flush_dcache_mmap_unlock(mapping);
670 }
671
672 /* remove from the MM's tree and list */
673 rb_erase(&vma->vm_rb, &mm->mm_rb);
674 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
675 if (*pp == vma) {
676 *pp = vma->vm_next;
677 break;
678 }
679 }
680
681 vma->vm_mm = NULL;
682}
683
684/*
685 * destroy a VMA record
686 */
687static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
688{
689 kenter("%p", vma);
690 if (vma->vm_ops && vma->vm_ops->close)
691 vma->vm_ops->close(vma);
692 if (vma->vm_file) {
693 fput(vma->vm_file);
694 if (vma->vm_flags & VM_EXECUTABLE)
695 removed_exe_file_vma(mm);
696 }
697 put_nommu_region(vma->vm_region);
698 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700699}
700
701/*
702 * look up the first VMA in which addr resides, NULL if none
703 * - should be called with mm->mmap_sem at least held readlocked
704 */
705struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
706{
David Howells8feae132009-01-08 12:04:47 +0000707 struct vm_area_struct *vma;
708 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700709
David Howells8feae132009-01-08 12:04:47 +0000710 /* check the cache first */
711 vma = mm->mmap_cache;
712 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
713 return vma;
714
715 /* trawl the tree (there may be multiple mappings in which addr
716 * resides) */
717 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
718 vma = rb_entry(n, struct vm_area_struct, vm_rb);
719 if (vma->vm_start > addr)
720 return NULL;
721 if (vma->vm_end > addr) {
722 mm->mmap_cache = vma;
723 return vma;
724 }
David Howells30340972006-09-27 01:50:20 -0700725 }
726
David Howells30340972006-09-27 01:50:20 -0700727 return NULL;
728}
729EXPORT_SYMBOL(find_vma);
730
731/*
David Howells930e6522006-09-27 01:50:22 -0700732 * find a VMA
733 * - we don't extend stack VMAs under NOMMU conditions
734 */
735struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
736{
737 return find_vma(mm, addr);
738}
739
David Howells8feae132009-01-08 12:04:47 +0000740/*
741 * expand a stack to a given address
742 * - not supported under NOMMU conditions
743 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700744int expand_stack(struct vm_area_struct *vma, unsigned long address)
745{
746 return -ENOMEM;
747}
748
David Howells930e6522006-09-27 01:50:22 -0700749/*
David Howells6fa5f802006-09-27 01:50:21 -0700750 * look up the first VMA exactly that exactly matches addr
751 * - should be called with mm->mmap_sem at least held readlocked
752 */
David Howells8feae132009-01-08 12:04:47 +0000753static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
754 unsigned long addr,
755 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000758 struct rb_node *n = mm->mm_rb.rb_node;
759 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
David Howells8feae132009-01-08 12:04:47 +0000761 /* check the cache first */
762 vma = mm->mmap_cache;
763 if (vma && vma->vm_start == addr && vma->vm_end == end)
764 return vma;
765
766 /* trawl the tree (there may be multiple mappings in which addr
767 * resides) */
768 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000770 if (vma->vm_start < addr)
771 continue;
772 if (vma->vm_start > addr)
773 return NULL;
774 if (vma->vm_end == end) {
775 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return vma;
David Howells8feae132009-01-08 12:04:47 +0000777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
780 return NULL;
781}
782
David Howells30340972006-09-27 01:50:20 -0700783/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * determine whether a mapping should be permitted and, if so, what sort of
785 * mapping we're capable of supporting
786 */
787static int validate_mmap_request(struct file *file,
788 unsigned long addr,
789 unsigned long len,
790 unsigned long prot,
791 unsigned long flags,
792 unsigned long pgoff,
793 unsigned long *_capabilities)
794{
David Howells8feae132009-01-08 12:04:47 +0000795 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned long reqprot = prot;
797 int ret;
798
799 /* do the simple checks first */
800 if (flags & MAP_FIXED || addr) {
801 printk(KERN_DEBUG
802 "%d: Can't do fixed-address/overlay mmap of RAM\n",
803 current->pid);
804 return -EINVAL;
805 }
806
807 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
808 (flags & MAP_TYPE) != MAP_SHARED)
809 return -EINVAL;
810
Mike Frysingerf81cff02006-12-06 12:02:59 +1000811 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EINVAL;
813
Mike Frysingerf81cff02006-12-06 12:02:59 +1000814 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000815 rlen = PAGE_ALIGN(len);
816 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000817 return -ENOMEM;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000820 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000821 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (file) {
824 /* validate file mapping requests */
825 struct address_space *mapping;
826
827 /* files must support mmap */
828 if (!file->f_op || !file->f_op->mmap)
829 return -ENODEV;
830
831 /* work out if what we've got could possibly be shared
832 * - we support chardevs that provide their own "memory"
833 * - we support files/blockdevs that are memory backed
834 */
835 mapping = file->f_mapping;
836 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800837 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 capabilities = 0;
840 if (mapping && mapping->backing_dev_info)
841 capabilities = mapping->backing_dev_info->capabilities;
842
843 if (!capabilities) {
844 /* no explicit capabilities set, so assume some
845 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800846 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 case S_IFREG:
848 case S_IFBLK:
849 capabilities = BDI_CAP_MAP_COPY;
850 break;
851
852 case S_IFCHR:
853 capabilities =
854 BDI_CAP_MAP_DIRECT |
855 BDI_CAP_READ_MAP |
856 BDI_CAP_WRITE_MAP;
857 break;
858
859 default:
860 return -EINVAL;
861 }
862 }
863
864 /* eliminate any capabilities that we can't support on this
865 * device */
866 if (!file->f_op->get_unmapped_area)
867 capabilities &= ~BDI_CAP_MAP_DIRECT;
868 if (!file->f_op->read)
869 capabilities &= ~BDI_CAP_MAP_COPY;
870
871 if (flags & MAP_SHARED) {
872 /* do checks for writing, appending and locking */
873 if ((prot & PROT_WRITE) &&
874 !(file->f_mode & FMODE_WRITE))
875 return -EACCES;
876
Josef Sipeke9536ae2006-12-08 02:37:21 -0800877 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 (file->f_mode & FMODE_WRITE))
879 return -EACCES;
880
Josef Sipeke9536ae2006-12-08 02:37:21 -0800881 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -EAGAIN;
883
884 if (!(capabilities & BDI_CAP_MAP_DIRECT))
885 return -ENODEV;
886
887 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
888 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
889 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
890 ) {
891 printk("MAP_SHARED not completely supported on !MMU\n");
892 return -EINVAL;
893 }
894
895 /* we mustn't privatise shared mappings */
896 capabilities &= ~BDI_CAP_MAP_COPY;
897 }
898 else {
899 /* we're going to read the file into private memory we
900 * allocate */
901 if (!(capabilities & BDI_CAP_MAP_COPY))
902 return -ENODEV;
903
904 /* we don't permit a private writable mapping to be
905 * shared with the backing device */
906 if (prot & PROT_WRITE)
907 capabilities &= ~BDI_CAP_MAP_DIRECT;
908 }
909
910 /* handle executable mappings and implied executable
911 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800912 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (prot & PROT_EXEC)
914 return -EPERM;
915 }
916 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
917 /* handle implication of PROT_EXEC by PROT_READ */
918 if (current->personality & READ_IMPLIES_EXEC) {
919 if (capabilities & BDI_CAP_EXEC_MAP)
920 prot |= PROT_EXEC;
921 }
922 }
923 else if ((prot & PROT_READ) &&
924 (prot & PROT_EXEC) &&
925 !(capabilities & BDI_CAP_EXEC_MAP)
926 ) {
927 /* backing file is not executable, try to copy */
928 capabilities &= ~BDI_CAP_MAP_DIRECT;
929 }
930 }
931 else {
932 /* anonymous mappings are always memory backed and can be
933 * privately mapped
934 */
935 capabilities = BDI_CAP_MAP_COPY;
936
937 /* handle PROT_EXEC implication by PROT_READ */
938 if ((prot & PROT_READ) &&
939 (current->personality & READ_IMPLIES_EXEC))
940 prot |= PROT_EXEC;
941 }
942
943 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -0400944 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (ret < 0)
946 return ret;
947
948 /* looks okay */
949 *_capabilities = capabilities;
950 return 0;
951}
952
953/*
954 * we've determined that we can make the mapping, now translate what we
955 * now know into VMA flags
956 */
957static unsigned long determine_vm_flags(struct file *file,
958 unsigned long prot,
959 unsigned long flags,
960 unsigned long capabilities)
961{
962 unsigned long vm_flags;
963
964 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
965 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
966 /* vm_flags |= mm->def_flags; */
967
968 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
969 /* attempt to share read-only copies of mapped file chunks */
970 if (file && !(prot & PROT_WRITE))
971 vm_flags |= VM_MAYSHARE;
972 }
973 else {
974 /* overlay a shareable mapping on the backing device or inode
975 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
976 * romfs/cramfs */
977 if (flags & MAP_SHARED)
978 vm_flags |= VM_MAYSHARE | VM_SHARED;
979 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
980 vm_flags |= VM_MAYSHARE;
981 }
982
983 /* refuse to let anyone share private mappings with this process if
984 * it's being traced - otherwise breakpoints set in it may interfere
985 * with another untraced process
986 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -0700987 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 vm_flags &= ~VM_MAYSHARE;
989
990 return vm_flags;
991}
992
993/*
David Howells8feae132009-01-08 12:04:47 +0000994 * set up a shared mapping on a file (the driver or filesystem provides and
995 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
David Howells8feae132009-01-08 12:04:47 +0000997static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 int ret;
1000
1001 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1002 if (ret != -ENOSYS)
1003 return ret;
1004
1005 /* getting an ENOSYS error indicates that direct mmap isn't
1006 * possible (as opposed to tried but failed) so we'll fall
1007 * through to making a private copy of the data and mapping
1008 * that if we can */
1009 return -ENODEV;
1010}
1011
1012/*
1013 * set up a private mapping or an anonymous shared mapping
1014 */
David Howells8feae132009-01-08 12:04:47 +00001015static int do_mmap_private(struct vm_area_struct *vma,
1016 struct vm_region *region,
1017 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
David Howells8feae132009-01-08 12:04:47 +00001019 struct page *pages;
1020 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 void *base;
David Howells8feae132009-01-08 12:04:47 +00001022 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* invoke the file's mapping function so that it can keep track of
1025 * shared mappings on devices or memory
1026 * - VM_MAYSHARE will be set if it may attempt to share
1027 */
1028 if (vma->vm_file) {
1029 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1030 if (ret != -ENOSYS) {
1031 /* shouldn't return success if we're not sharing */
1032 BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
1033 return ret; /* success or a real error */
1034 }
1035
1036 /* getting an ENOSYS error indicates that direct mmap isn't
1037 * possible (as opposed to tried but failed) so we'll try to
1038 * make a private copy of the data and map that instead */
1039 }
1040
David Howells8feae132009-01-08 12:04:47 +00001041 rlen = PAGE_ALIGN(len);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* allocate some memory to hold the mapping
1044 * - note that this may not return a page-aligned address if the object
1045 * we're allocating is smaller than a page
1046 */
David Howells8feae132009-01-08 12:04:47 +00001047 order = get_order(rlen);
1048 kdebug("alloc order %d for %lx", order, len);
1049
1050 pages = alloc_pages(GFP_KERNEL, order);
1051 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto enomem;
1053
David Howells8feae132009-01-08 12:04:47 +00001054 /* we allocated a power-of-2 sized page set, so we need to trim off the
1055 * excess */
1056 total = 1 << order;
1057 atomic_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
David Howells8feae132009-01-08 12:04:47 +00001059 point = rlen >> PAGE_SHIFT;
1060 while (total > point) {
1061 order = ilog2(total - point);
1062 n = 1 << order;
1063 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1064 atomic_sub(n, &mmap_pages_allocated);
1065 total -= n;
1066 set_page_refcounted(pages + total);
1067 __free_pages(pages + total, order);
1068 }
1069
1070 total = rlen >> PAGE_SHIFT;
1071 for (point = 1; point < total; point++)
1072 set_page_refcounted(&pages[point]);
1073
1074 base = page_address(pages);
1075 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1076 region->vm_start = (unsigned long) base;
1077 region->vm_end = region->vm_start + rlen;
1078
1079 vma->vm_start = region->vm_start;
1080 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (vma->vm_file) {
1083 /* read the contents of a file into the copy */
1084 mm_segment_t old_fs;
1085 loff_t fpos;
1086
1087 fpos = vma->vm_pgoff;
1088 fpos <<= PAGE_SHIFT;
1089
1090 old_fs = get_fs();
1091 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001092 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 set_fs(old_fs);
1094
1095 if (ret < 0)
1096 goto error_free;
1097
1098 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001099 if (ret < rlen)
1100 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 } else {
1103 /* if it's an anonymous mapping, then just clear it */
David Howells8feae132009-01-08 12:04:47 +00001104 memset(base, 0, rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
1107 return 0;
1108
1109error_free:
David Howells8feae132009-01-08 12:04:47 +00001110 free_page_series(region->vm_start, region->vm_end);
1111 region->vm_start = vma->vm_start = 0;
1112 region->vm_end = vma->vm_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return ret;
1114
1115enomem:
1116 printk("Allocation of length %lu from process %d failed\n",
1117 len, current->pid);
1118 show_free_areas();
1119 return -ENOMEM;
1120}
1121
1122/*
1123 * handle mapping creation for uClinux
1124 */
1125unsigned long do_mmap_pgoff(struct file *file,
1126 unsigned long addr,
1127 unsigned long len,
1128 unsigned long prot,
1129 unsigned long flags,
1130 unsigned long pgoff)
1131{
David Howells8feae132009-01-08 12:04:47 +00001132 struct vm_area_struct *vma;
1133 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001135 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int ret;
1137
David Howells8feae132009-01-08 12:04:47 +00001138 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1139
Eric Paris7cd94142007-11-26 18:47:40 -05001140 if (!(flags & MAP_FIXED))
1141 addr = round_hint_to_min(addr);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /* decide whether we should attempt the mapping, and if so what sort of
1144 * mapping */
1145 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1146 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001147 if (ret < 0) {
1148 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return ret;
David Howells8feae132009-01-08 12:04:47 +00001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* we've determined that we can make the mapping, now translate what we
1153 * now know into VMA flags */
1154 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1155
David Howells8feae132009-01-08 12:04:47 +00001156 /* we're going to need to record the mapping */
1157 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1158 if (!region)
1159 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
David Howells8feae132009-01-08 12:04:47 +00001161 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1162 if (!vma)
1163 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
David Howells8feae132009-01-08 12:04:47 +00001165 atomic_set(&region->vm_usage, 1);
1166 region->vm_flags = vm_flags;
1167 region->vm_pgoff = pgoff;
1168
1169 INIT_LIST_HEAD(&vma->anon_vma_node);
1170 vma->vm_flags = vm_flags;
1171 vma->vm_pgoff = pgoff;
1172
1173 if (file) {
1174 region->vm_file = file;
1175 get_file(file);
1176 vma->vm_file = file;
1177 get_file(file);
1178 if (vm_flags & VM_EXECUTABLE) {
1179 added_exe_file_vma(current->mm);
1180 vma->vm_mm = current->mm;
1181 }
1182 }
1183
1184 down_write(&nommu_region_sem);
1185
1186 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001188 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * - shared mappings on character devices and memory backed files are
1190 * permitted to overlap inexactly as far as we are concerned for in
1191 * these cases, sharing is handled in the driver or filesystem rather
1192 * than here
1193 */
1194 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001195 struct vm_region *pregion;
1196 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
David Howells8feae132009-01-08 12:04:47 +00001198 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1199 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001200
David Howells8feae132009-01-08 12:04:47 +00001201 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1202 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
David Howells8feae132009-01-08 12:04:47 +00001204 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 continue;
1206
1207 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001208 if (pregion->vm_file->f_path.dentry->d_inode !=
1209 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 continue;
1211
David Howells8feae132009-01-08 12:04:47 +00001212 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 continue;
1214
David Howells8feae132009-01-08 12:04:47 +00001215 rpglen = pregion->vm_end - pregion->vm_start;
1216 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1217 rpgend = pregion->vm_pgoff + rpglen;
1218 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 continue;
1220
David Howells8feae132009-01-08 12:04:47 +00001221 /* handle inexactly overlapping matches between
1222 * mappings */
1223 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1224 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1225 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1227 goto sharing_violation;
1228 continue;
1229 }
1230
David Howells8feae132009-01-08 12:04:47 +00001231 /* we've found a region we can share */
1232 atomic_inc(&pregion->vm_usage);
1233 vma->vm_region = pregion;
1234 start = pregion->vm_start;
1235 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1236 vma->vm_start = start;
1237 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
David Howells8feae132009-01-08 12:04:47 +00001239 if (pregion->vm_flags & VM_MAPPED_COPY) {
1240 kdebug("share copy");
1241 vma->vm_flags |= VM_MAPPED_COPY;
1242 } else {
1243 kdebug("share mmap");
1244 ret = do_mmap_shared_file(vma);
1245 if (ret < 0) {
1246 vma->vm_region = NULL;
1247 vma->vm_start = 0;
1248 vma->vm_end = 0;
1249 atomic_dec(&pregion->vm_usage);
1250 pregion = NULL;
1251 goto error_just_free;
1252 }
1253 }
1254 fput(region->vm_file);
1255 kmem_cache_free(vm_region_jar, region);
1256 region = pregion;
1257 result = start;
1258 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /* obtain the address at which to make a shared mapping
1262 * - this is the hook for quasi-memory character devices to
1263 * tell us the location of a shared mapping
1264 */
1265 if (file && file->f_op->get_unmapped_area) {
1266 addr = file->f_op->get_unmapped_area(file, addr, len,
1267 pgoff, flags);
1268 if (IS_ERR((void *) addr)) {
1269 ret = addr;
1270 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001271 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /* the driver refused to tell us where to site
1274 * the mapping so we'll have to attempt to copy
1275 * it */
1276 ret = (unsigned long) -ENODEV;
1277 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001278 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001281 } else {
1282 vma->vm_start = region->vm_start = addr;
1283 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 }
1286 }
1287
David Howells8feae132009-01-08 12:04:47 +00001288 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /* set up the mapping */
1291 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001292 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 else
David Howells8feae132009-01-08 12:04:47 +00001294 ret = do_mmap_private(vma, region, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001296 goto error_put_region;
1297
1298 add_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001301 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 current->mm->total_vm += len >> PAGE_SHIFT;
1304
David Howells8feae132009-01-08 12:04:47 +00001305share:
1306 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
David Howells8feae132009-01-08 12:04:47 +00001308 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (prot & PROT_EXEC)
David Howells8feae132009-01-08 12:04:47 +00001311 flush_icache_range(result, result + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
David Howells8feae132009-01-08 12:04:47 +00001313 kleave(" = %lx", result);
1314 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
David Howells8feae132009-01-08 12:04:47 +00001316error_put_region:
1317 __put_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (vma) {
Matt Helsley925d1c42008-04-29 01:01:36 -07001319 if (vma->vm_file) {
Gavin Lambert3fcd03e2006-09-30 23:27:01 -07001320 fput(vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07001321 if (vma->vm_flags & VM_EXECUTABLE)
1322 removed_exe_file_vma(vma->vm_mm);
1323 }
David Howells8feae132009-01-08 12:04:47 +00001324 kmem_cache_free(vm_area_cachep, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
David Howells8feae132009-01-08 12:04:47 +00001326 kleave(" = %d [pr]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return ret;
1328
David Howells8feae132009-01-08 12:04:47 +00001329error_just_free:
1330 up_write(&nommu_region_sem);
1331error:
1332 fput(region->vm_file);
1333 kmem_cache_free(vm_region_jar, region);
1334 fput(vma->vm_file);
1335 if (vma->vm_flags & VM_EXECUTABLE)
1336 removed_exe_file_vma(vma->vm_mm);
1337 kmem_cache_free(vm_area_cachep, vma);
1338 kleave(" = %d", ret);
1339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
David Howells8feae132009-01-08 12:04:47 +00001341sharing_violation:
1342 up_write(&nommu_region_sem);
1343 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1344 ret = -EINVAL;
1345 goto error;
1346
1347error_getting_vma:
1348 kmem_cache_free(vm_region_jar, region);
1349 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1350 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 len, current->pid);
1352 show_free_areas();
1353 return -ENOMEM;
1354
David Howells8feae132009-01-08 12:04:47 +00001355error_getting_region:
1356 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1357 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 len, current->pid);
1359 show_free_areas();
1360 return -ENOMEM;
1361}
Paul Mundtb5073172007-07-21 04:37:25 -07001362EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364/*
David Howells8feae132009-01-08 12:04:47 +00001365 * split a vma into two pieces at address 'addr', a new vma is allocated either
1366 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
David Howells8feae132009-01-08 12:04:47 +00001368int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1369 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
David Howells8feae132009-01-08 12:04:47 +00001371 struct vm_area_struct *new;
1372 struct vm_region *region;
1373 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David Howells8feae132009-01-08 12:04:47 +00001375 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
David Howells8feae132009-01-08 12:04:47 +00001377 /* we're only permitted to split anonymous regions that have a single
1378 * owner */
1379 if (vma->vm_file ||
1380 atomic_read(&vma->vm_region->vm_usage) != 1)
1381 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
David Howells8feae132009-01-08 12:04:47 +00001383 if (mm->map_count >= sysctl_max_map_count)
1384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
David Howells8feae132009-01-08 12:04:47 +00001386 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1387 if (!region)
1388 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
David Howells8feae132009-01-08 12:04:47 +00001390 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1391 if (!new) {
1392 kmem_cache_free(vm_region_jar, region);
1393 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
David Howells8feae132009-01-08 12:04:47 +00001395
1396 /* most fields are the same, copy all, and then fixup */
1397 *new = *vma;
1398 *region = *vma->vm_region;
1399 new->vm_region = region;
1400
1401 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1402
1403 if (new_below) {
1404 region->vm_end = new->vm_end = addr;
1405 } else {
1406 region->vm_start = new->vm_start = addr;
1407 region->vm_pgoff = new->vm_pgoff += npages;
1408 }
1409
1410 if (new->vm_ops && new->vm_ops->open)
1411 new->vm_ops->open(new);
1412
1413 delete_vma_from_mm(vma);
1414 down_write(&nommu_region_sem);
1415 delete_nommu_region(vma->vm_region);
1416 if (new_below) {
1417 vma->vm_region->vm_start = vma->vm_start = addr;
1418 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1419 } else {
1420 vma->vm_region->vm_end = vma->vm_end = addr;
1421 }
1422 add_nommu_region(vma->vm_region);
1423 add_nommu_region(new->vm_region);
1424 up_write(&nommu_region_sem);
1425 add_vma_to_mm(mm, vma);
1426 add_vma_to_mm(mm, new);
1427 return 0;
1428}
1429
1430/*
1431 * shrink a VMA by removing the specified chunk from either the beginning or
1432 * the end
1433 */
1434static int shrink_vma(struct mm_struct *mm,
1435 struct vm_area_struct *vma,
1436 unsigned long from, unsigned long to)
1437{
1438 struct vm_region *region;
1439
1440 kenter("");
1441
1442 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1443 * and list */
1444 delete_vma_from_mm(vma);
1445 if (from > vma->vm_start)
1446 vma->vm_end = from;
1447 else
1448 vma->vm_start = to;
1449 add_vma_to_mm(mm, vma);
1450
1451 /* cut the backing region down to size */
1452 region = vma->vm_region;
1453 BUG_ON(atomic_read(&region->vm_usage) != 1);
1454
1455 down_write(&nommu_region_sem);
1456 delete_nommu_region(region);
1457 if (from > region->vm_start)
1458 region->vm_end = from;
1459 else
1460 region->vm_start = to;
1461 add_nommu_region(region);
1462 up_write(&nommu_region_sem);
1463
1464 free_page_series(from, to);
1465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
David Howells30340972006-09-27 01:50:20 -07001468/*
1469 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001470 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1471 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001472 */
David Howells8feae132009-01-08 12:04:47 +00001473int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
David Howells8feae132009-01-08 12:04:47 +00001475 struct vm_area_struct *vma;
1476 struct rb_node *rb;
1477 unsigned long end = start + len;
1478 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
David Howells8feae132009-01-08 12:04:47 +00001480 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
David Howells8feae132009-01-08 12:04:47 +00001482 if (len == 0)
1483 return -EINVAL;
1484
1485 /* find the first potentially overlapping VMA */
1486 vma = find_vma(mm, start);
1487 if (!vma) {
1488 printk(KERN_WARNING
1489 "munmap of memory not mmapped by process %d (%s):"
1490 " 0x%lx-0x%lx\n",
1491 current->pid, current->comm, start, start + len - 1);
1492 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
David Howells8feae132009-01-08 12:04:47 +00001495 /* we're allowed to split an anonymous VMA but not a file-backed one */
1496 if (vma->vm_file) {
1497 do {
1498 if (start > vma->vm_start) {
1499 kleave(" = -EINVAL [miss]");
1500 return -EINVAL;
1501 }
1502 if (end == vma->vm_end)
1503 goto erase_whole_vma;
1504 rb = rb_next(&vma->vm_rb);
1505 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1506 } while (rb);
1507 kleave(" = -EINVAL [split file]");
1508 return -EINVAL;
1509 } else {
1510 /* the chunk must be a subset of the VMA found */
1511 if (start == vma->vm_start && end == vma->vm_end)
1512 goto erase_whole_vma;
1513 if (start < vma->vm_start || end > vma->vm_end) {
1514 kleave(" = -EINVAL [superset]");
1515 return -EINVAL;
1516 }
1517 if (start & ~PAGE_MASK) {
1518 kleave(" = -EINVAL [unaligned start]");
1519 return -EINVAL;
1520 }
1521 if (end != vma->vm_end && end & ~PAGE_MASK) {
1522 kleave(" = -EINVAL [unaligned split]");
1523 return -EINVAL;
1524 }
1525 if (start != vma->vm_start && end != vma->vm_end) {
1526 ret = split_vma(mm, vma, start, 1);
1527 if (ret < 0) {
1528 kleave(" = %d [split]", ret);
1529 return ret;
1530 }
1531 }
1532 return shrink_vma(mm, vma, start, end);
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
David Howells8feae132009-01-08 12:04:47 +00001535erase_whole_vma:
1536 delete_vma_from_mm(vma);
1537 delete_vma(mm, vma);
1538 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 0;
1540}
Paul Mundtb5073172007-07-21 04:37:25 -07001541EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
David Howells30340972006-09-27 01:50:20 -07001543asmlinkage long sys_munmap(unsigned long addr, size_t len)
1544{
1545 int ret;
1546 struct mm_struct *mm = current->mm;
1547
1548 down_write(&mm->mmap_sem);
1549 ret = do_munmap(mm, addr, len);
1550 up_write(&mm->mmap_sem);
1551 return ret;
1552}
1553
1554/*
David Howells8feae132009-01-08 12:04:47 +00001555 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001556 */
David Howells8feae132009-01-08 12:04:47 +00001557void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
David Howells8feae132009-01-08 12:04:47 +00001559 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
David Howells8feae132009-01-08 12:04:47 +00001561 if (!mm)
1562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Howells8feae132009-01-08 12:04:47 +00001564 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
David Howells8feae132009-01-08 12:04:47 +00001566 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
David Howells8feae132009-01-08 12:04:47 +00001568 while ((vma = mm->mmap)) {
1569 mm->mmap = vma->vm_next;
1570 delete_vma_from_mm(vma);
1571 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
David Howells8feae132009-01-08 12:04:47 +00001573
1574 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577unsigned long do_brk(unsigned long addr, unsigned long len)
1578{
1579 return -ENOMEM;
1580}
1581
1582/*
David Howells6fa5f802006-09-27 01:50:21 -07001583 * expand (or shrink) an existing mapping, potentially moving it at the same
1584 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *
David Howells6fa5f802006-09-27 01:50:21 -07001586 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001587 * as long as it stays within the region allocated by do_mmap_private() and the
1588 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *
David Howells6fa5f802006-09-27 01:50:21 -07001590 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 */
1592unsigned long do_mremap(unsigned long addr,
1593 unsigned long old_len, unsigned long new_len,
1594 unsigned long flags, unsigned long new_addr)
1595{
David Howells6fa5f802006-09-27 01:50:21 -07001596 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001599 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return (unsigned long) -EINVAL;
1601
David Howells8feae132009-01-08 12:04:47 +00001602 if (addr & ~PAGE_MASK)
1603 return -EINVAL;
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (flags & MREMAP_FIXED && new_addr != addr)
1606 return (unsigned long) -EINVAL;
1607
David Howells8feae132009-01-08 12:04:47 +00001608 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001609 if (!vma)
1610 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
David Howells6fa5f802006-09-27 01:50:21 -07001612 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return (unsigned long) -EFAULT;
1614
David Howells6fa5f802006-09-27 01:50:21 -07001615 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return (unsigned long) -EPERM;
1617
David Howells8feae132009-01-08 12:04:47 +00001618 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return (unsigned long) -ENOMEM;
1620
1621 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001622 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001623 return vma->vm_start;
1624}
Paul Mundtb5073172007-07-21 04:37:25 -07001625EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001626
David Howells8feae132009-01-08 12:04:47 +00001627asmlinkage
1628unsigned long sys_mremap(unsigned long addr,
1629 unsigned long old_len, unsigned long new_len,
1630 unsigned long flags, unsigned long new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001631{
1632 unsigned long ret;
1633
1634 down_write(&current->mm->mmap_sem);
1635 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1636 up_write(&current->mm->mmap_sem);
1637 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Linus Torvalds6aab3412005-11-28 14:34:23 -08001640struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001641 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 return NULL;
1644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1647 unsigned long to, unsigned long size, pgprot_t prot)
1648{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001649 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
Luke Yang22c4af42006-07-14 00:24:09 -07001652EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Paul Mundtf905bc42008-02-04 22:29:59 -08001654int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1655 unsigned long pgoff)
1656{
1657 unsigned int size = vma->vm_end - vma->vm_start;
1658
1659 if (!(vma->vm_flags & VM_USERMAP))
1660 return -EINVAL;
1661
1662 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1663 vma->vm_end = vma->vm_start + size;
1664
1665 return 0;
1666}
1667EXPORT_SYMBOL(remap_vmalloc_range);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1670{
1671}
1672
1673unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1674 unsigned long len, unsigned long pgoff, unsigned long flags)
1675{
1676 return -ENOMEM;
1677}
1678
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001679void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683void unmap_mapping_range(struct address_space *mapping,
1684 loff_t const holebegin, loff_t const holelen,
1685 int even_cows)
1686{
1687}
Luke Yang22c4af42006-07-14 00:24:09 -07001688EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690/*
David Howellsd56e03c2007-03-22 00:11:23 -08001691 * ask for an unmapped area at which to create a mapping on a file
1692 */
1693unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1694 unsigned long len, unsigned long pgoff,
1695 unsigned long flags)
1696{
1697 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1698 unsigned long, unsigned long);
1699
1700 get_area = current->mm->get_unmapped_area;
1701 if (file && file->f_op && file->f_op->get_unmapped_area)
1702 get_area = file->f_op->get_unmapped_area;
1703
1704 if (!get_area)
1705 return -ENOSYS;
1706
1707 return get_area(file, addr, len, pgoff, flags);
1708}
David Howellsd56e03c2007-03-22 00:11:23 -08001709EXPORT_SYMBOL(get_unmapped_area);
1710
1711/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * Check that a process has enough memory to allocate a new virtual
1713 * mapping. 0 means there is enough memory for the allocation to
1714 * succeed and -ENOMEM implies there is not.
1715 *
1716 * We currently support three overcommit policies, which are set via the
1717 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1718 *
1719 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1720 * Additional code 2002 Jul 20 by Robert Love.
1721 *
1722 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1723 *
1724 * Note this is a helper function intended to be used by LSMs which
1725 * wish to use this logic.
1726 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001727int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 unsigned long free, allowed;
1730
1731 vm_acct_memory(pages);
1732
1733 /*
1734 * Sometimes we want to use more memory than we have
1735 */
1736 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1737 return 0;
1738
1739 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1740 unsigned long n;
1741
Christoph Lameter347ce432006-06-30 01:55:35 -07001742 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 free += nr_swap_pages;
1744
1745 /*
1746 * Any slabs which are created with the
1747 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1748 * which are reclaimable, under pressure. The dentry
1749 * cache and most inode caches should fall into this
1750 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001751 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 /*
1754 * Leave the last 3% for root
1755 */
1756 if (!cap_sys_admin)
1757 free -= free / 32;
1758
1759 if (free > pages)
1760 return 0;
1761
1762 /*
1763 * nr_free_pages() is very expensive on large systems,
1764 * only call if we're about to fail.
1765 */
1766 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001767
1768 /*
1769 * Leave reserved pages. The pages are not for anonymous pages.
1770 */
1771 if (n <= totalreserve_pages)
1772 goto error;
1773 else
1774 n -= totalreserve_pages;
1775
1776 /*
1777 * Leave the last 3% for root
1778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (!cap_sys_admin)
1780 n -= n / 32;
1781 free += n;
1782
1783 if (free > pages)
1784 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001785
1786 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
1789 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1790 /*
1791 * Leave the last 3% for root
1792 */
1793 if (!cap_sys_admin)
1794 allowed -= allowed / 32;
1795 allowed += total_swap_pages;
1796
1797 /* Don't let a single process grow too big:
1798 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001799 if (mm)
1800 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Simon Derr2f60f8d2005-08-04 19:52:03 -07001802 /*
1803 * cast `allowed' as a signed long because vm_committed_space
1804 * sometimes has a negative value
1805 */
Alan Cox80119ef2008-05-23 13:04:31 -07001806 if (atomic_long_read(&vm_committed_space) < (long)allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001808error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 vm_unacct_memory(pages);
1810
1811 return -ENOMEM;
1812}
1813
1814int in_gate_area_no_task(unsigned long addr)
1815{
1816 return 0;
1817}
David Howellsb0e15192006-01-06 00:11:42 -08001818
Nick Piggind0217ac2007-07-19 01:47:03 -07001819int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001820{
1821 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001822 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001823}
Paul Mundtb5073172007-07-21 04:37:25 -07001824EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001825
1826/*
1827 * Access another process' address space.
1828 * - source/target buffer must be kernel space
1829 */
1830int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1831{
David Howells0ec76a12006-09-27 01:50:15 -07001832 struct vm_area_struct *vma;
1833 struct mm_struct *mm;
1834
1835 if (addr + len < addr)
1836 return 0;
1837
1838 mm = get_task_mm(tsk);
1839 if (!mm)
1840 return 0;
1841
1842 down_read(&mm->mmap_sem);
1843
1844 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001845 vma = find_vma(mm, addr);
1846 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001847 /* don't overrun this mapping */
1848 if (addr + len >= vma->vm_end)
1849 len = vma->vm_end - addr;
1850
1851 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001852 if (write && vma->vm_flags & VM_MAYWRITE)
David Howells0ec76a12006-09-27 01:50:15 -07001853 len -= copy_to_user((void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001854 else if (!write && vma->vm_flags & VM_MAYREAD)
David Howells0ec76a12006-09-27 01:50:15 -07001855 len -= copy_from_user(buf, (void *) addr, len);
1856 else
1857 len = 0;
1858 } else {
1859 len = 0;
1860 }
1861
1862 up_read(&mm->mmap_sem);
1863 mmput(mm);
1864 return len;
1865}