blob: a6e8ccfbd40013df6dcb71899e25deaaf7d5c706 [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 Mundtdd8632a2009-01-08 12:04:47 +000013 * Copyright (c) 2007-2008 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;
Paul Mundtdd8632a2009-01-08 12:04:47 +000069int sysctl_nr_trim_pages = 1; /* page trimming behaviour */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070int heap_stack_gap = 0;
71
David Howells8feae132009-01-08 12:04:47 +000072atomic_t mmap_pages_allocated;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070075EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
David Howells8feae132009-01-08 12:04:47 +000077/* list of mapped, potentially shareable regions */
78static struct kmem_cache *vm_region_jar;
79struct rb_root nommu_region_tree = RB_ROOT;
80DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82struct vm_operations_struct generic_file_vm_ops = {
83};
84
85/*
86 * Handle all mappings that got truncated by a "truncate()"
87 * system call.
88 *
89 * NOTE! We have to be ready to update the memory sharing
90 * between the file and the memory map for a potential last
91 * incomplete page. Ugly, but necessary.
92 */
93int vmtruncate(struct inode *inode, loff_t offset)
94{
95 struct address_space *mapping = inode->i_mapping;
96 unsigned long limit;
97
98 if (inode->i_size < offset)
99 goto do_expand;
100 i_size_write(inode, offset);
101
102 truncate_inode_pages(mapping, offset);
103 goto out_truncate;
104
105do_expand:
106 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
107 if (limit != RLIM_INFINITY && offset > limit)
108 goto out_sig;
109 if (offset > inode->i_sb->s_maxbytes)
110 goto out;
111 i_size_write(inode, offset);
112
113out_truncate:
Al Viroacfa4382008-12-04 10:06:33 -0500114 if (inode->i_op->truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 inode->i_op->truncate(inode);
116 return 0;
117out_sig:
118 send_sig(SIGXFSZ, current, 0);
119out:
120 return -EFBIG;
121}
122
123EXPORT_SYMBOL(vmtruncate);
124
125/*
126 * Return the total memory allocated for this pointer, not
127 * just what the caller asked for.
128 *
129 * Doesn't have to be accurate, i.e. may have races.
130 */
131unsigned int kobjsize(const void *objp)
132{
133 struct page *page;
134
Michael Hennerich4016a132008-04-28 02:13:38 -0700135 /*
136 * If the object we have should not have ksize performed on it,
137 * return size of 0
138 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900139 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700140 return 0;
141
142 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700143
144 /*
145 * If the allocator sets PageSlab, we know the pointer came from
146 * kmalloc().
147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (PageSlab(page))
149 return ksize(objp);
150
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700151 /*
152 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900153 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700154 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900155 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Nick Pigginb291f002008-10-18 20:26:44 -0700158int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
159 unsigned long start, int len, int flags,
160 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700162 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700163 unsigned long vm_flags;
164 int i;
Nick Pigginb291f002008-10-18 20:26:44 -0700165 int write = !!(flags & GUP_FLAGS_WRITE);
166 int force = !!(flags & GUP_FLAGS_FORCE);
167 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
David Howells7b4d5b82006-09-27 01:50:18 -0700168
169 /* calculate required read or write permissions.
170 * - if 'force' is set, we only require the "MAY" flags.
171 */
172 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
173 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 for (i = 0; i < len; i++) {
Sonic Zhang910e46d2006-09-27 01:50:17 -0700176 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700177 if (!vma)
178 goto finish_or_fault;
179
180 /* protect what we can, including chardevs */
181 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
Nick Pigginb291f002008-10-18 20:26:44 -0700182 (!ignore && !(vm_flags & vma->vm_flags)))
David Howells7b4d5b82006-09-27 01:50:18 -0700183 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (pages) {
186 pages[i] = virt_to_page(start);
187 if (pages[i])
188 page_cache_get(pages[i]);
189 }
190 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700191 vmas[i] = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 start += PAGE_SIZE;
193 }
David Howells7b4d5b82006-09-27 01:50:18 -0700194
195 return i;
196
197finish_or_fault:
198 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Nick Pigginb291f002008-10-18 20:26:44 -0700200
201
202/*
203 * get a list of pages in an address range belonging to the specified process
204 * and indicate the VMA that covers each page
205 * - this is potentially dodgy as we may end incrementing the page count of a
206 * slab page or a secondary page from a compound page
207 * - don't permit access to VMAs that don't support it, such as I/O mappings
208 */
209int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
210 unsigned long start, int len, int write, int force,
211 struct page **pages, struct vm_area_struct **vmas)
212{
213 int flags = 0;
214
215 if (write)
216 flags |= GUP_FLAGS_WRITE;
217 if (force)
218 flags |= GUP_FLAGS_FORCE;
219
220 return __get_user_pages(tsk, mm,
221 start, len, flags,
222 pages, vmas);
223}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000224EXPORT_SYMBOL(get_user_pages);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226DEFINE_RWLOCK(vmlist_lock);
227struct vm_struct *vmlist;
228
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800229void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 kfree(addr);
232}
Paul Mundtb5073172007-07-21 04:37:25 -0700233EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Al Virodd0fc662005-10-07 07:46:04 +0100235void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200238 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
239 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
Nick Piggin84097512006-03-22 00:08:34 -0800241 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Paul Mundtb5073172007-07-21 04:37:25 -0700243EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Paul Mundtf905bc42008-02-04 22:29:59 -0800245void *vmalloc_user(unsigned long size)
246{
247 void *ret;
248
249 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
250 PAGE_KERNEL);
251 if (ret) {
252 struct vm_area_struct *vma;
253
254 down_write(&current->mm->mmap_sem);
255 vma = find_vma(current->mm, (unsigned long)ret);
256 if (vma)
257 vma->vm_flags |= VM_USERMAP;
258 up_write(&current->mm->mmap_sem);
259 }
260
261 return ret;
262}
263EXPORT_SYMBOL(vmalloc_user);
264
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800265struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 return virt_to_page(addr);
268}
Paul Mundtb5073172007-07-21 04:37:25 -0700269EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800271unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 return page_to_pfn(virt_to_page(addr));
274}
Paul Mundtb5073172007-07-21 04:37:25 -0700275EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277long vread(char *buf, char *addr, unsigned long count)
278{
279 memcpy(buf, addr, count);
280 return count;
281}
282
283long vwrite(char *buf, char *addr, unsigned long count)
284{
285 /* Don't allow overflow */
286 if ((unsigned long) addr + count < count)
287 count = -(unsigned long) addr;
288
289 memcpy(addr, buf, count);
290 return(count);
291}
292
293/*
294 * vmalloc - allocate virtually continguos memory
295 *
296 * @size: allocation size
297 *
298 * Allocate enough pages to cover @size from the page level
299 * allocator and map them into continguos kernel virtual space.
300 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200301 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * use __vmalloc() instead.
303 */
304void *vmalloc(unsigned long size)
305{
306 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
307}
Andrew Mortonf6138882006-02-28 16:59:18 -0800308EXPORT_SYMBOL(vmalloc);
309
310void *vmalloc_node(unsigned long size, int node)
311{
312 return vmalloc(size);
313}
314EXPORT_SYMBOL(vmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Paul Mundt1af446e2008-08-04 16:01:47 +0900316#ifndef PAGE_KERNEL_EXEC
317# define PAGE_KERNEL_EXEC PAGE_KERNEL
318#endif
319
320/**
321 * vmalloc_exec - allocate virtually contiguous, executable memory
322 * @size: allocation size
323 *
324 * Kernel-internal function to allocate enough pages to cover @size
325 * the page level allocator and map them into contiguous and
326 * executable kernel virtual space.
327 *
328 * For tight control over page level allocator and protection flags
329 * use __vmalloc() instead.
330 */
331
332void *vmalloc_exec(unsigned long size)
333{
334 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
335}
336
Paul Mundtb5073172007-07-21 04:37:25 -0700337/**
338 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * @size: allocation size
340 *
341 * Allocate enough 32bit PA addressable pages to cover @size from the
342 * page level allocator and map them into continguos kernel virtual space.
343 */
344void *vmalloc_32(unsigned long size)
345{
346 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
347}
Paul Mundtb5073172007-07-21 04:37:25 -0700348EXPORT_SYMBOL(vmalloc_32);
349
350/**
351 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
352 * @size: allocation size
353 *
354 * The resulting memory area is 32bit addressable and zeroed so it can be
355 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800356 *
357 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
358 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700359 */
360void *vmalloc_32_user(unsigned long size)
361{
Paul Mundtf905bc42008-02-04 22:29:59 -0800362 /*
363 * We'll have to sort out the ZONE_DMA bits for 64-bit,
364 * but for now this can simply use vmalloc_user() directly.
365 */
366 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700367}
368EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
371{
372 BUG();
373 return NULL;
374}
Paul Mundtb5073172007-07-21 04:37:25 -0700375EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800377void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 BUG();
380}
Paul Mundtb5073172007-07-21 04:37:25 -0700381EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700384 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
385 * have one.
386 */
387void __attribute__((weak)) vmalloc_sync_all(void)
388{
389}
390
Paul Mundtb5073172007-07-21 04:37:25 -0700391int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
392 struct page *page)
393{
394 return -EINVAL;
395}
396EXPORT_SYMBOL(vm_insert_page);
397
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700398/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * sys_brk() for the most part doesn't need the global kernel
400 * lock, except when an application is doing something nasty
401 * like trying to un-brk an area that has already been mapped
402 * to a regular file. in this case, the unmapping will need
403 * to invoke file system routines that need the global lock.
404 */
405asmlinkage unsigned long sys_brk(unsigned long brk)
406{
407 struct mm_struct *mm = current->mm;
408
409 if (brk < mm->start_brk || brk > mm->context.end_brk)
410 return mm->brk;
411
412 if (mm->brk == brk)
413 return mm->brk;
414
415 /*
416 * Always allow shrinking brk
417 */
418 if (brk <= mm->brk) {
419 mm->brk = brk;
420 return brk;
421 }
422
423 /*
424 * Ok, looks good - let it rip.
425 */
426 return mm->brk = brk;
427}
428
David Howells8feae132009-01-08 12:04:47 +0000429/*
430 * initialise the VMA and region record slabs
431 */
432void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
David Howells8feae132009-01-08 12:04:47 +0000434 vm_region_jar = kmem_cache_create("vm_region_jar",
435 sizeof(struct vm_region), 0,
436 SLAB_PANIC, NULL);
437 vm_area_cachep = kmem_cache_create("vm_area_struct",
438 sizeof(struct vm_area_struct), 0,
439 SLAB_PANIC, NULL);
440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
David Howells8feae132009-01-08 12:04:47 +0000442/*
443 * validate the region tree
444 * - the caller must hold the region lock
445 */
446#ifdef CONFIG_DEBUG_NOMMU_REGIONS
447static noinline void validate_nommu_regions(void)
448{
449 struct vm_region *region, *last;
450 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
David Howells8feae132009-01-08 12:04:47 +0000452 lastp = rb_first(&nommu_region_tree);
453 if (!lastp)
454 return;
455
456 last = rb_entry(lastp, struct vm_region, vm_rb);
457 if (unlikely(last->vm_end <= last->vm_start))
458 BUG();
Paul Mundtdd8632a2009-01-08 12:04:47 +0000459 if (unlikely(last->vm_top < last->vm_end))
460 BUG();
David Howells8feae132009-01-08 12:04:47 +0000461
462 while ((p = rb_next(lastp))) {
463 region = rb_entry(p, struct vm_region, vm_rb);
464 last = rb_entry(lastp, struct vm_region, vm_rb);
465
466 if (unlikely(region->vm_end <= region->vm_start))
467 BUG();
Paul Mundtdd8632a2009-01-08 12:04:47 +0000468 if (unlikely(region->vm_top < region->vm_end))
469 BUG();
470 if (unlikely(region->vm_start < last->vm_top))
David Howells8feae132009-01-08 12:04:47 +0000471 BUG();
472
473 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475}
David Howells8feae132009-01-08 12:04:47 +0000476#else
477#define validate_nommu_regions() do {} while(0)
478#endif
479
480/*
481 * add a region into the global tree
482 */
483static void add_nommu_region(struct vm_region *region)
484{
485 struct vm_region *pregion;
486 struct rb_node **p, *parent;
487
488 validate_nommu_regions();
489
490 BUG_ON(region->vm_start & ~PAGE_MASK);
491
492 parent = NULL;
493 p = &nommu_region_tree.rb_node;
494 while (*p) {
495 parent = *p;
496 pregion = rb_entry(parent, struct vm_region, vm_rb);
497 if (region->vm_start < pregion->vm_start)
498 p = &(*p)->rb_left;
499 else if (region->vm_start > pregion->vm_start)
500 p = &(*p)->rb_right;
501 else if (pregion == region)
502 return;
503 else
504 BUG();
505 }
506
507 rb_link_node(&region->vm_rb, parent, p);
508 rb_insert_color(&region->vm_rb, &nommu_region_tree);
509
510 validate_nommu_regions();
511}
512
513/*
514 * delete a region from the global tree
515 */
516static void delete_nommu_region(struct vm_region *region)
517{
518 BUG_ON(!nommu_region_tree.rb_node);
519
520 validate_nommu_regions();
521 rb_erase(&region->vm_rb, &nommu_region_tree);
522 validate_nommu_regions();
523}
524
525/*
526 * free a contiguous series of pages
527 */
528static void free_page_series(unsigned long from, unsigned long to)
529{
530 for (; from < to; from += PAGE_SIZE) {
531 struct page *page = virt_to_page(from);
532
533 kdebug("- free %lx", from);
534 atomic_dec(&mmap_pages_allocated);
535 if (page_count(page) != 1)
536 kdebug("free page %p [%d]", page, page_count(page));
537 put_page(page);
538 }
539}
540
541/*
542 * release a reference to a region
543 * - the caller must hold the region semaphore, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000544 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000545 * will equal vm_start
546 */
547static void __put_nommu_region(struct vm_region *region)
548 __releases(nommu_region_sem)
549{
550 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
551
552 BUG_ON(!nommu_region_tree.rb_node);
553
554 if (atomic_dec_and_test(&region->vm_usage)) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000555 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000556 delete_nommu_region(region);
557 up_write(&nommu_region_sem);
558
559 if (region->vm_file)
560 fput(region->vm_file);
561
562 /* IO memory and memory shared directly out of the pagecache
563 * from ramfs/tmpfs mustn't be released here */
564 if (region->vm_flags & VM_MAPPED_COPY) {
565 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000566 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000567 }
568 kmem_cache_free(vm_region_jar, region);
569 } else {
570 up_write(&nommu_region_sem);
571 }
572}
573
574/*
575 * release a reference to a region
576 */
577static void put_nommu_region(struct vm_region *region)
578{
579 down_write(&nommu_region_sem);
580 __put_nommu_region(region);
581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Howells30340972006-09-27 01:50:20 -0700583/*
584 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000585 * and tree and add to the address space's page tree also if not an anonymous
586 * page
David Howells30340972006-09-27 01:50:20 -0700587 * - should be called with mm->mmap_sem held writelocked
588 */
David Howells8feae132009-01-08 12:04:47 +0000589static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700590{
David Howells8feae132009-01-08 12:04:47 +0000591 struct vm_area_struct *pvma, **pp;
592 struct address_space *mapping;
593 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700594
David Howells8feae132009-01-08 12:04:47 +0000595 kenter(",%p", vma);
596
597 BUG_ON(!vma->vm_region);
598
599 mm->map_count++;
600 vma->vm_mm = mm;
601
602 /* add the VMA to the mapping */
603 if (vma->vm_file) {
604 mapping = vma->vm_file->f_mapping;
605
606 flush_dcache_mmap_lock(mapping);
607 vma_prio_tree_insert(vma, &mapping->i_mmap);
608 flush_dcache_mmap_unlock(mapping);
609 }
610
611 /* add the VMA to the tree */
612 parent = NULL;
613 p = &mm->mm_rb.rb_node;
614 while (*p) {
615 parent = *p;
616 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
617
618 /* sort by: start addr, end addr, VMA struct addr in that order
619 * (the latter is necessary as we may get identical VMAs) */
620 if (vma->vm_start < pvma->vm_start)
621 p = &(*p)->rb_left;
622 else if (vma->vm_start > pvma->vm_start)
623 p = &(*p)->rb_right;
624 else if (vma->vm_end < pvma->vm_end)
625 p = &(*p)->rb_left;
626 else if (vma->vm_end > pvma->vm_end)
627 p = &(*p)->rb_right;
628 else if (vma < pvma)
629 p = &(*p)->rb_left;
630 else if (vma > pvma)
631 p = &(*p)->rb_right;
632 else
633 BUG();
634 }
635
636 rb_link_node(&vma->vm_rb, parent, p);
637 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
638
639 /* add VMA to the VMA list also */
640 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
641 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700642 break;
David Howells8feae132009-01-08 12:04:47 +0000643 if (pvma->vm_start < vma->vm_start)
644 continue;
645 if (pvma->vm_end < vma->vm_end)
646 break;
647 }
David Howells30340972006-09-27 01:50:20 -0700648
David Howells8feae132009-01-08 12:04:47 +0000649 vma->vm_next = *pp;
650 *pp = vma;
651}
652
653/*
654 * delete a VMA from its owning mm_struct and address space
655 */
656static void delete_vma_from_mm(struct vm_area_struct *vma)
657{
658 struct vm_area_struct **pp;
659 struct address_space *mapping;
660 struct mm_struct *mm = vma->vm_mm;
661
662 kenter("%p", vma);
663
664 mm->map_count--;
665 if (mm->mmap_cache == vma)
666 mm->mmap_cache = NULL;
667
668 /* remove the VMA from the mapping */
669 if (vma->vm_file) {
670 mapping = vma->vm_file->f_mapping;
671
672 flush_dcache_mmap_lock(mapping);
673 vma_prio_tree_remove(vma, &mapping->i_mmap);
674 flush_dcache_mmap_unlock(mapping);
675 }
676
677 /* remove from the MM's tree and list */
678 rb_erase(&vma->vm_rb, &mm->mm_rb);
679 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
680 if (*pp == vma) {
681 *pp = vma->vm_next;
682 break;
683 }
684 }
685
686 vma->vm_mm = NULL;
687}
688
689/*
690 * destroy a VMA record
691 */
692static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
693{
694 kenter("%p", vma);
695 if (vma->vm_ops && vma->vm_ops->close)
696 vma->vm_ops->close(vma);
697 if (vma->vm_file) {
698 fput(vma->vm_file);
699 if (vma->vm_flags & VM_EXECUTABLE)
700 removed_exe_file_vma(mm);
701 }
702 put_nommu_region(vma->vm_region);
703 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700704}
705
706/*
707 * look up the first VMA in which addr resides, NULL if none
708 * - should be called with mm->mmap_sem at least held readlocked
709 */
710struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
711{
David Howells8feae132009-01-08 12:04:47 +0000712 struct vm_area_struct *vma;
713 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700714
David Howells8feae132009-01-08 12:04:47 +0000715 /* check the cache first */
716 vma = mm->mmap_cache;
717 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
718 return vma;
719
720 /* trawl the tree (there may be multiple mappings in which addr
721 * resides) */
722 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
723 vma = rb_entry(n, struct vm_area_struct, vm_rb);
724 if (vma->vm_start > addr)
725 return NULL;
726 if (vma->vm_end > addr) {
727 mm->mmap_cache = vma;
728 return vma;
729 }
David Howells30340972006-09-27 01:50:20 -0700730 }
731
David Howells30340972006-09-27 01:50:20 -0700732 return NULL;
733}
734EXPORT_SYMBOL(find_vma);
735
736/*
David Howells930e6522006-09-27 01:50:22 -0700737 * find a VMA
738 * - we don't extend stack VMAs under NOMMU conditions
739 */
740struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
741{
742 return find_vma(mm, addr);
743}
744
David Howells8feae132009-01-08 12:04:47 +0000745/*
746 * expand a stack to a given address
747 * - not supported under NOMMU conditions
748 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700749int expand_stack(struct vm_area_struct *vma, unsigned long address)
750{
751 return -ENOMEM;
752}
753
David Howells930e6522006-09-27 01:50:22 -0700754/*
David Howells6fa5f802006-09-27 01:50:21 -0700755 * look up the first VMA exactly that exactly matches addr
756 * - should be called with mm->mmap_sem at least held readlocked
757 */
David Howells8feae132009-01-08 12:04:47 +0000758static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
759 unsigned long addr,
760 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000763 struct rb_node *n = mm->mm_rb.rb_node;
764 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -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 == end)
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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000775 if (vma->vm_start < addr)
776 continue;
777 if (vma->vm_start > addr)
778 return NULL;
779 if (vma->vm_end == end) {
780 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return vma;
David Howells8feae132009-01-08 12:04:47 +0000782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 return NULL;
786}
787
David Howells30340972006-09-27 01:50:20 -0700788/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * determine whether a mapping should be permitted and, if so, what sort of
790 * mapping we're capable of supporting
791 */
792static int validate_mmap_request(struct file *file,
793 unsigned long addr,
794 unsigned long len,
795 unsigned long prot,
796 unsigned long flags,
797 unsigned long pgoff,
798 unsigned long *_capabilities)
799{
David Howells8feae132009-01-08 12:04:47 +0000800 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned long reqprot = prot;
802 int ret;
803
804 /* do the simple checks first */
805 if (flags & MAP_FIXED || addr) {
806 printk(KERN_DEBUG
807 "%d: Can't do fixed-address/overlay mmap of RAM\n",
808 current->pid);
809 return -EINVAL;
810 }
811
812 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
813 (flags & MAP_TYPE) != MAP_SHARED)
814 return -EINVAL;
815
Mike Frysingerf81cff02006-12-06 12:02:59 +1000816 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -EINVAL;
818
Mike Frysingerf81cff02006-12-06 12:02:59 +1000819 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000820 rlen = PAGE_ALIGN(len);
821 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000822 return -ENOMEM;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000825 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000826 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (file) {
829 /* validate file mapping requests */
830 struct address_space *mapping;
831
832 /* files must support mmap */
833 if (!file->f_op || !file->f_op->mmap)
834 return -ENODEV;
835
836 /* work out if what we've got could possibly be shared
837 * - we support chardevs that provide their own "memory"
838 * - we support files/blockdevs that are memory backed
839 */
840 mapping = file->f_mapping;
841 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800842 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 capabilities = 0;
845 if (mapping && mapping->backing_dev_info)
846 capabilities = mapping->backing_dev_info->capabilities;
847
848 if (!capabilities) {
849 /* no explicit capabilities set, so assume some
850 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800851 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case S_IFREG:
853 case S_IFBLK:
854 capabilities = BDI_CAP_MAP_COPY;
855 break;
856
857 case S_IFCHR:
858 capabilities =
859 BDI_CAP_MAP_DIRECT |
860 BDI_CAP_READ_MAP |
861 BDI_CAP_WRITE_MAP;
862 break;
863
864 default:
865 return -EINVAL;
866 }
867 }
868
869 /* eliminate any capabilities that we can't support on this
870 * device */
871 if (!file->f_op->get_unmapped_area)
872 capabilities &= ~BDI_CAP_MAP_DIRECT;
873 if (!file->f_op->read)
874 capabilities &= ~BDI_CAP_MAP_COPY;
875
876 if (flags & MAP_SHARED) {
877 /* do checks for writing, appending and locking */
878 if ((prot & PROT_WRITE) &&
879 !(file->f_mode & FMODE_WRITE))
880 return -EACCES;
881
Josef Sipeke9536ae2006-12-08 02:37:21 -0800882 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 (file->f_mode & FMODE_WRITE))
884 return -EACCES;
885
Josef Sipeke9536ae2006-12-08 02:37:21 -0800886 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EAGAIN;
888
889 if (!(capabilities & BDI_CAP_MAP_DIRECT))
890 return -ENODEV;
891
892 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
893 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
894 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
895 ) {
896 printk("MAP_SHARED not completely supported on !MMU\n");
897 return -EINVAL;
898 }
899
900 /* we mustn't privatise shared mappings */
901 capabilities &= ~BDI_CAP_MAP_COPY;
902 }
903 else {
904 /* we're going to read the file into private memory we
905 * allocate */
906 if (!(capabilities & BDI_CAP_MAP_COPY))
907 return -ENODEV;
908
909 /* we don't permit a private writable mapping to be
910 * shared with the backing device */
911 if (prot & PROT_WRITE)
912 capabilities &= ~BDI_CAP_MAP_DIRECT;
913 }
914
915 /* handle executable mappings and implied executable
916 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800917 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (prot & PROT_EXEC)
919 return -EPERM;
920 }
921 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
922 /* handle implication of PROT_EXEC by PROT_READ */
923 if (current->personality & READ_IMPLIES_EXEC) {
924 if (capabilities & BDI_CAP_EXEC_MAP)
925 prot |= PROT_EXEC;
926 }
927 }
928 else if ((prot & PROT_READ) &&
929 (prot & PROT_EXEC) &&
930 !(capabilities & BDI_CAP_EXEC_MAP)
931 ) {
932 /* backing file is not executable, try to copy */
933 capabilities &= ~BDI_CAP_MAP_DIRECT;
934 }
935 }
936 else {
937 /* anonymous mappings are always memory backed and can be
938 * privately mapped
939 */
940 capabilities = BDI_CAP_MAP_COPY;
941
942 /* handle PROT_EXEC implication by PROT_READ */
943 if ((prot & PROT_READ) &&
944 (current->personality & READ_IMPLIES_EXEC))
945 prot |= PROT_EXEC;
946 }
947
948 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -0400949 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (ret < 0)
951 return ret;
952
953 /* looks okay */
954 *_capabilities = capabilities;
955 return 0;
956}
957
958/*
959 * we've determined that we can make the mapping, now translate what we
960 * now know into VMA flags
961 */
962static unsigned long determine_vm_flags(struct file *file,
963 unsigned long prot,
964 unsigned long flags,
965 unsigned long capabilities)
966{
967 unsigned long vm_flags;
968
969 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
970 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
971 /* vm_flags |= mm->def_flags; */
972
973 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
974 /* attempt to share read-only copies of mapped file chunks */
975 if (file && !(prot & PROT_WRITE))
976 vm_flags |= VM_MAYSHARE;
977 }
978 else {
979 /* overlay a shareable mapping on the backing device or inode
980 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
981 * romfs/cramfs */
982 if (flags & MAP_SHARED)
983 vm_flags |= VM_MAYSHARE | VM_SHARED;
984 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
985 vm_flags |= VM_MAYSHARE;
986 }
987
988 /* refuse to let anyone share private mappings with this process if
989 * it's being traced - otherwise breakpoints set in it may interfere
990 * with another untraced process
991 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -0700992 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 vm_flags &= ~VM_MAYSHARE;
994
995 return vm_flags;
996}
997
998/*
David Howells8feae132009-01-08 12:04:47 +0000999 * set up a shared mapping on a file (the driver or filesystem provides and
1000 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
David Howells8feae132009-01-08 12:04:47 +00001002static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 int ret;
1005
1006 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001007 if (ret == 0) {
1008 vma->vm_region->vm_top = vma->vm_region->vm_end;
1009 return ret;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (ret != -ENOSYS)
1012 return ret;
1013
1014 /* getting an ENOSYS error indicates that direct mmap isn't
1015 * possible (as opposed to tried but failed) so we'll fall
1016 * through to making a private copy of the data and mapping
1017 * that if we can */
1018 return -ENODEV;
1019}
1020
1021/*
1022 * set up a private mapping or an anonymous shared mapping
1023 */
David Howells8feae132009-01-08 12:04:47 +00001024static int do_mmap_private(struct vm_area_struct *vma,
1025 struct vm_region *region,
1026 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
David Howells8feae132009-01-08 12:04:47 +00001028 struct page *pages;
1029 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 void *base;
David Howells8feae132009-01-08 12:04:47 +00001031 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* invoke the file's mapping function so that it can keep track of
1034 * shared mappings on devices or memory
1035 * - VM_MAYSHARE will be set if it may attempt to share
1036 */
1037 if (vma->vm_file) {
1038 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001039 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001041 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1042 vma->vm_region->vm_top = vma->vm_region->vm_end;
1043 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001045 if (ret != -ENOSYS)
1046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* getting an ENOSYS error indicates that direct mmap isn't
1049 * possible (as opposed to tried but failed) so we'll try to
1050 * make a private copy of the data and map that instead */
1051 }
1052
David Howells8feae132009-01-08 12:04:47 +00001053 rlen = PAGE_ALIGN(len);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* allocate some memory to hold the mapping
1056 * - note that this may not return a page-aligned address if the object
1057 * we're allocating is smaller than a page
1058 */
David Howells8feae132009-01-08 12:04:47 +00001059 order = get_order(rlen);
1060 kdebug("alloc order %d for %lx", order, len);
1061
1062 pages = alloc_pages(GFP_KERNEL, order);
1063 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto enomem;
1065
David Howells8feae132009-01-08 12:04:47 +00001066 total = 1 << order;
1067 atomic_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
David Howells8feae132009-01-08 12:04:47 +00001069 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001070
1071 /* we allocated a power-of-2 sized page set, so we may want to trim off
1072 * the excess */
1073 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1074 while (total > point) {
1075 order = ilog2(total - point);
1076 n = 1 << order;
1077 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1078 atomic_sub(n, &mmap_pages_allocated);
1079 total -= n;
1080 set_page_refcounted(pages + total);
1081 __free_pages(pages + total, order);
1082 }
David Howells8feae132009-01-08 12:04:47 +00001083 }
1084
David Howells8feae132009-01-08 12:04:47 +00001085 for (point = 1; point < total; point++)
1086 set_page_refcounted(&pages[point]);
1087
1088 base = page_address(pages);
1089 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1090 region->vm_start = (unsigned long) base;
1091 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001092 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001093
1094 vma->vm_start = region->vm_start;
1095 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (vma->vm_file) {
1098 /* read the contents of a file into the copy */
1099 mm_segment_t old_fs;
1100 loff_t fpos;
1101
1102 fpos = vma->vm_pgoff;
1103 fpos <<= PAGE_SHIFT;
1104
1105 old_fs = get_fs();
1106 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001107 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 set_fs(old_fs);
1109
1110 if (ret < 0)
1111 goto error_free;
1112
1113 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001114 if (ret < rlen)
1115 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 } else {
1118 /* if it's an anonymous mapping, then just clear it */
David Howells8feae132009-01-08 12:04:47 +00001119 memset(base, 0, rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 return 0;
1123
1124error_free:
David Howells8feae132009-01-08 12:04:47 +00001125 free_page_series(region->vm_start, region->vm_end);
1126 region->vm_start = vma->vm_start = 0;
1127 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001128 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return ret;
1130
1131enomem:
1132 printk("Allocation of length %lu from process %d failed\n",
1133 len, current->pid);
1134 show_free_areas();
1135 return -ENOMEM;
1136}
1137
1138/*
1139 * handle mapping creation for uClinux
1140 */
1141unsigned long do_mmap_pgoff(struct file *file,
1142 unsigned long addr,
1143 unsigned long len,
1144 unsigned long prot,
1145 unsigned long flags,
1146 unsigned long pgoff)
1147{
David Howells8feae132009-01-08 12:04:47 +00001148 struct vm_area_struct *vma;
1149 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001151 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int ret;
1153
David Howells8feae132009-01-08 12:04:47 +00001154 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1155
Eric Paris7cd94142007-11-26 18:47:40 -05001156 if (!(flags & MAP_FIXED))
1157 addr = round_hint_to_min(addr);
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* decide whether we should attempt the mapping, and if so what sort of
1160 * mapping */
1161 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1162 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001163 if (ret < 0) {
1164 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return ret;
David Howells8feae132009-01-08 12:04:47 +00001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* we've determined that we can make the mapping, now translate what we
1169 * now know into VMA flags */
1170 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1171
David Howells8feae132009-01-08 12:04:47 +00001172 /* we're going to need to record the mapping */
1173 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1174 if (!region)
1175 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
David Howells8feae132009-01-08 12:04:47 +00001177 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1178 if (!vma)
1179 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
David Howells8feae132009-01-08 12:04:47 +00001181 atomic_set(&region->vm_usage, 1);
1182 region->vm_flags = vm_flags;
1183 region->vm_pgoff = pgoff;
1184
1185 INIT_LIST_HEAD(&vma->anon_vma_node);
1186 vma->vm_flags = vm_flags;
1187 vma->vm_pgoff = pgoff;
1188
1189 if (file) {
1190 region->vm_file = file;
1191 get_file(file);
1192 vma->vm_file = file;
1193 get_file(file);
1194 if (vm_flags & VM_EXECUTABLE) {
1195 added_exe_file_vma(current->mm);
1196 vma->vm_mm = current->mm;
1197 }
1198 }
1199
1200 down_write(&nommu_region_sem);
1201
1202 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001204 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * - shared mappings on character devices and memory backed files are
1206 * permitted to overlap inexactly as far as we are concerned for in
1207 * these cases, sharing is handled in the driver or filesystem rather
1208 * than here
1209 */
1210 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001211 struct vm_region *pregion;
1212 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
David Howells8feae132009-01-08 12:04:47 +00001214 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1215 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001216
David Howells8feae132009-01-08 12:04:47 +00001217 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1218 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
David Howells8feae132009-01-08 12:04:47 +00001220 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 continue;
1222
1223 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001224 if (pregion->vm_file->f_path.dentry->d_inode !=
1225 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 continue;
1227
David Howells8feae132009-01-08 12:04:47 +00001228 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 continue;
1230
David Howells8feae132009-01-08 12:04:47 +00001231 rpglen = pregion->vm_end - pregion->vm_start;
1232 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1233 rpgend = pregion->vm_pgoff + rpglen;
1234 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 continue;
1236
David Howells8feae132009-01-08 12:04:47 +00001237 /* handle inexactly overlapping matches between
1238 * mappings */
1239 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1240 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1241 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1243 goto sharing_violation;
1244 continue;
1245 }
1246
David Howells8feae132009-01-08 12:04:47 +00001247 /* we've found a region we can share */
1248 atomic_inc(&pregion->vm_usage);
1249 vma->vm_region = pregion;
1250 start = pregion->vm_start;
1251 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1252 vma->vm_start = start;
1253 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
David Howells8feae132009-01-08 12:04:47 +00001255 if (pregion->vm_flags & VM_MAPPED_COPY) {
1256 kdebug("share copy");
1257 vma->vm_flags |= VM_MAPPED_COPY;
1258 } else {
1259 kdebug("share mmap");
1260 ret = do_mmap_shared_file(vma);
1261 if (ret < 0) {
1262 vma->vm_region = NULL;
1263 vma->vm_start = 0;
1264 vma->vm_end = 0;
1265 atomic_dec(&pregion->vm_usage);
1266 pregion = NULL;
1267 goto error_just_free;
1268 }
1269 }
1270 fput(region->vm_file);
1271 kmem_cache_free(vm_region_jar, region);
1272 region = pregion;
1273 result = start;
1274 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* obtain the address at which to make a shared mapping
1278 * - this is the hook for quasi-memory character devices to
1279 * tell us the location of a shared mapping
1280 */
1281 if (file && file->f_op->get_unmapped_area) {
1282 addr = file->f_op->get_unmapped_area(file, addr, len,
1283 pgoff, flags);
1284 if (IS_ERR((void *) addr)) {
1285 ret = addr;
1286 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001287 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /* the driver refused to tell us where to site
1290 * the mapping so we'll have to attempt to copy
1291 * it */
1292 ret = (unsigned long) -ENODEV;
1293 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001294 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001297 } else {
1298 vma->vm_start = region->vm_start = addr;
1299 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 }
1302 }
1303
David Howells8feae132009-01-08 12:04:47 +00001304 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* set up the mapping */
1307 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001308 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 else
David Howells8feae132009-01-08 12:04:47 +00001310 ret = do_mmap_private(vma, region, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001312 goto error_put_region;
1313
1314 add_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001317 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 current->mm->total_vm += len >> PAGE_SHIFT;
1320
David Howells8feae132009-01-08 12:04:47 +00001321share:
1322 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
David Howells8feae132009-01-08 12:04:47 +00001324 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (prot & PROT_EXEC)
David Howells8feae132009-01-08 12:04:47 +00001327 flush_icache_range(result, result + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
David Howells8feae132009-01-08 12:04:47 +00001329 kleave(" = %lx", result);
1330 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
David Howells8feae132009-01-08 12:04:47 +00001332error_put_region:
1333 __put_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (vma) {
Matt Helsley925d1c42008-04-29 01:01:36 -07001335 if (vma->vm_file) {
Gavin Lambert3fcd03e2006-09-30 23:27:01 -07001336 fput(vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07001337 if (vma->vm_flags & VM_EXECUTABLE)
1338 removed_exe_file_vma(vma->vm_mm);
1339 }
David Howells8feae132009-01-08 12:04:47 +00001340 kmem_cache_free(vm_area_cachep, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
David Howells8feae132009-01-08 12:04:47 +00001342 kleave(" = %d [pr]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return ret;
1344
David Howells8feae132009-01-08 12:04:47 +00001345error_just_free:
1346 up_write(&nommu_region_sem);
1347error:
1348 fput(region->vm_file);
1349 kmem_cache_free(vm_region_jar, region);
1350 fput(vma->vm_file);
1351 if (vma->vm_flags & VM_EXECUTABLE)
1352 removed_exe_file_vma(vma->vm_mm);
1353 kmem_cache_free(vm_area_cachep, vma);
1354 kleave(" = %d", ret);
1355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
David Howells8feae132009-01-08 12:04:47 +00001357sharing_violation:
1358 up_write(&nommu_region_sem);
1359 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1360 ret = -EINVAL;
1361 goto error;
1362
1363error_getting_vma:
1364 kmem_cache_free(vm_region_jar, region);
1365 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1366 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 len, current->pid);
1368 show_free_areas();
1369 return -ENOMEM;
1370
David Howells8feae132009-01-08 12:04:47 +00001371error_getting_region:
1372 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1373 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 len, current->pid);
1375 show_free_areas();
1376 return -ENOMEM;
1377}
Paul Mundtb5073172007-07-21 04:37:25 -07001378EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380/*
David Howells8feae132009-01-08 12:04:47 +00001381 * split a vma into two pieces at address 'addr', a new vma is allocated either
1382 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
David Howells8feae132009-01-08 12:04:47 +00001384int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1385 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
David Howells8feae132009-01-08 12:04:47 +00001387 struct vm_area_struct *new;
1388 struct vm_region *region;
1389 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David Howells8feae132009-01-08 12:04:47 +00001391 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
David Howells8feae132009-01-08 12:04:47 +00001393 /* we're only permitted to split anonymous regions that have a single
1394 * owner */
1395 if (vma->vm_file ||
1396 atomic_read(&vma->vm_region->vm_usage) != 1)
1397 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
David Howells8feae132009-01-08 12:04:47 +00001399 if (mm->map_count >= sysctl_max_map_count)
1400 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
David Howells8feae132009-01-08 12:04:47 +00001402 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1403 if (!region)
1404 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
David Howells8feae132009-01-08 12:04:47 +00001406 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1407 if (!new) {
1408 kmem_cache_free(vm_region_jar, region);
1409 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
David Howells8feae132009-01-08 12:04:47 +00001411
1412 /* most fields are the same, copy all, and then fixup */
1413 *new = *vma;
1414 *region = *vma->vm_region;
1415 new->vm_region = region;
1416
1417 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1418
1419 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001420 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001421 } else {
1422 region->vm_start = new->vm_start = addr;
1423 region->vm_pgoff = new->vm_pgoff += npages;
1424 }
1425
1426 if (new->vm_ops && new->vm_ops->open)
1427 new->vm_ops->open(new);
1428
1429 delete_vma_from_mm(vma);
1430 down_write(&nommu_region_sem);
1431 delete_nommu_region(vma->vm_region);
1432 if (new_below) {
1433 vma->vm_region->vm_start = vma->vm_start = addr;
1434 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1435 } else {
1436 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001437 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001438 }
1439 add_nommu_region(vma->vm_region);
1440 add_nommu_region(new->vm_region);
1441 up_write(&nommu_region_sem);
1442 add_vma_to_mm(mm, vma);
1443 add_vma_to_mm(mm, new);
1444 return 0;
1445}
1446
1447/*
1448 * shrink a VMA by removing the specified chunk from either the beginning or
1449 * the end
1450 */
1451static int shrink_vma(struct mm_struct *mm,
1452 struct vm_area_struct *vma,
1453 unsigned long from, unsigned long to)
1454{
1455 struct vm_region *region;
1456
1457 kenter("");
1458
1459 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1460 * and list */
1461 delete_vma_from_mm(vma);
1462 if (from > vma->vm_start)
1463 vma->vm_end = from;
1464 else
1465 vma->vm_start = to;
1466 add_vma_to_mm(mm, vma);
1467
1468 /* cut the backing region down to size */
1469 region = vma->vm_region;
1470 BUG_ON(atomic_read(&region->vm_usage) != 1);
1471
1472 down_write(&nommu_region_sem);
1473 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001474 if (from > region->vm_start) {
1475 to = region->vm_top;
1476 region->vm_top = region->vm_end = from;
1477 } else {
David Howells8feae132009-01-08 12:04:47 +00001478 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001479 }
David Howells8feae132009-01-08 12:04:47 +00001480 add_nommu_region(region);
1481 up_write(&nommu_region_sem);
1482
1483 free_page_series(from, to);
1484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
David Howells30340972006-09-27 01:50:20 -07001487/*
1488 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001489 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1490 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001491 */
David Howells8feae132009-01-08 12:04:47 +00001492int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
David Howells8feae132009-01-08 12:04:47 +00001494 struct vm_area_struct *vma;
1495 struct rb_node *rb;
1496 unsigned long end = start + len;
1497 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
David Howells8feae132009-01-08 12:04:47 +00001499 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
David Howells8feae132009-01-08 12:04:47 +00001501 if (len == 0)
1502 return -EINVAL;
1503
1504 /* find the first potentially overlapping VMA */
1505 vma = find_vma(mm, start);
1506 if (!vma) {
1507 printk(KERN_WARNING
1508 "munmap of memory not mmapped by process %d (%s):"
1509 " 0x%lx-0x%lx\n",
1510 current->pid, current->comm, start, start + len - 1);
1511 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
David Howells8feae132009-01-08 12:04:47 +00001514 /* we're allowed to split an anonymous VMA but not a file-backed one */
1515 if (vma->vm_file) {
1516 do {
1517 if (start > vma->vm_start) {
1518 kleave(" = -EINVAL [miss]");
1519 return -EINVAL;
1520 }
1521 if (end == vma->vm_end)
1522 goto erase_whole_vma;
1523 rb = rb_next(&vma->vm_rb);
1524 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1525 } while (rb);
1526 kleave(" = -EINVAL [split file]");
1527 return -EINVAL;
1528 } else {
1529 /* the chunk must be a subset of the VMA found */
1530 if (start == vma->vm_start && end == vma->vm_end)
1531 goto erase_whole_vma;
1532 if (start < vma->vm_start || end > vma->vm_end) {
1533 kleave(" = -EINVAL [superset]");
1534 return -EINVAL;
1535 }
1536 if (start & ~PAGE_MASK) {
1537 kleave(" = -EINVAL [unaligned start]");
1538 return -EINVAL;
1539 }
1540 if (end != vma->vm_end && end & ~PAGE_MASK) {
1541 kleave(" = -EINVAL [unaligned split]");
1542 return -EINVAL;
1543 }
1544 if (start != vma->vm_start && end != vma->vm_end) {
1545 ret = split_vma(mm, vma, start, 1);
1546 if (ret < 0) {
1547 kleave(" = %d [split]", ret);
1548 return ret;
1549 }
1550 }
1551 return shrink_vma(mm, vma, start, end);
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
David Howells8feae132009-01-08 12:04:47 +00001554erase_whole_vma:
1555 delete_vma_from_mm(vma);
1556 delete_vma(mm, vma);
1557 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559}
Paul Mundtb5073172007-07-21 04:37:25 -07001560EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
David Howells30340972006-09-27 01:50:20 -07001562asmlinkage long sys_munmap(unsigned long addr, size_t len)
1563{
1564 int ret;
1565 struct mm_struct *mm = current->mm;
1566
1567 down_write(&mm->mmap_sem);
1568 ret = do_munmap(mm, addr, len);
1569 up_write(&mm->mmap_sem);
1570 return ret;
1571}
1572
1573/*
David Howells8feae132009-01-08 12:04:47 +00001574 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001575 */
David Howells8feae132009-01-08 12:04:47 +00001576void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
David Howells8feae132009-01-08 12:04:47 +00001578 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
David Howells8feae132009-01-08 12:04:47 +00001580 if (!mm)
1581 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
David Howells8feae132009-01-08 12:04:47 +00001583 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
David Howells8feae132009-01-08 12:04:47 +00001585 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
David Howells8feae132009-01-08 12:04:47 +00001587 while ((vma = mm->mmap)) {
1588 mm->mmap = vma->vm_next;
1589 delete_vma_from_mm(vma);
1590 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
David Howells8feae132009-01-08 12:04:47 +00001592
1593 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596unsigned long do_brk(unsigned long addr, unsigned long len)
1597{
1598 return -ENOMEM;
1599}
1600
1601/*
David Howells6fa5f802006-09-27 01:50:21 -07001602 * expand (or shrink) an existing mapping, potentially moving it at the same
1603 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 *
David Howells6fa5f802006-09-27 01:50:21 -07001605 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001606 * as long as it stays within the region allocated by do_mmap_private() and the
1607 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 *
David Howells6fa5f802006-09-27 01:50:21 -07001609 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
1611unsigned long do_mremap(unsigned long addr,
1612 unsigned long old_len, unsigned long new_len,
1613 unsigned long flags, unsigned long new_addr)
1614{
David Howells6fa5f802006-09-27 01:50:21 -07001615 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001618 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return (unsigned long) -EINVAL;
1620
David Howells8feae132009-01-08 12:04:47 +00001621 if (addr & ~PAGE_MASK)
1622 return -EINVAL;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (flags & MREMAP_FIXED && new_addr != addr)
1625 return (unsigned long) -EINVAL;
1626
David Howells8feae132009-01-08 12:04:47 +00001627 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001628 if (!vma)
1629 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
David Howells6fa5f802006-09-27 01:50:21 -07001631 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return (unsigned long) -EFAULT;
1633
David Howells6fa5f802006-09-27 01:50:21 -07001634 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return (unsigned long) -EPERM;
1636
David Howells8feae132009-01-08 12:04:47 +00001637 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return (unsigned long) -ENOMEM;
1639
1640 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001641 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001642 return vma->vm_start;
1643}
Paul Mundtb5073172007-07-21 04:37:25 -07001644EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001645
David Howells8feae132009-01-08 12:04:47 +00001646asmlinkage
1647unsigned long sys_mremap(unsigned long addr,
1648 unsigned long old_len, unsigned long new_len,
1649 unsigned long flags, unsigned long new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001650{
1651 unsigned long ret;
1652
1653 down_write(&current->mm->mmap_sem);
1654 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1655 up_write(&current->mm->mmap_sem);
1656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Linus Torvalds6aab3412005-11-28 14:34:23 -08001659struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001660 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 return NULL;
1663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1666 unsigned long to, unsigned long size, pgprot_t prot)
1667{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001668 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
Luke Yang22c4af42006-07-14 00:24:09 -07001671EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Paul Mundtf905bc42008-02-04 22:29:59 -08001673int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1674 unsigned long pgoff)
1675{
1676 unsigned int size = vma->vm_end - vma->vm_start;
1677
1678 if (!(vma->vm_flags & VM_USERMAP))
1679 return -EINVAL;
1680
1681 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1682 vma->vm_end = vma->vm_start + size;
1683
1684 return 0;
1685}
1686EXPORT_SYMBOL(remap_vmalloc_range);
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1689{
1690}
1691
1692unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1693 unsigned long len, unsigned long pgoff, unsigned long flags)
1694{
1695 return -ENOMEM;
1696}
1697
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001698void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702void unmap_mapping_range(struct address_space *mapping,
1703 loff_t const holebegin, loff_t const holelen,
1704 int even_cows)
1705{
1706}
Luke Yang22c4af42006-07-14 00:24:09 -07001707EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709/*
David Howellsd56e03c2007-03-22 00:11:23 -08001710 * ask for an unmapped area at which to create a mapping on a file
1711 */
1712unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1713 unsigned long len, unsigned long pgoff,
1714 unsigned long flags)
1715{
1716 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1717 unsigned long, unsigned long);
1718
1719 get_area = current->mm->get_unmapped_area;
1720 if (file && file->f_op && file->f_op->get_unmapped_area)
1721 get_area = file->f_op->get_unmapped_area;
1722
1723 if (!get_area)
1724 return -ENOSYS;
1725
1726 return get_area(file, addr, len, pgoff, flags);
1727}
David Howellsd56e03c2007-03-22 00:11:23 -08001728EXPORT_SYMBOL(get_unmapped_area);
1729
1730/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * Check that a process has enough memory to allocate a new virtual
1732 * mapping. 0 means there is enough memory for the allocation to
1733 * succeed and -ENOMEM implies there is not.
1734 *
1735 * We currently support three overcommit policies, which are set via the
1736 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1737 *
1738 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1739 * Additional code 2002 Jul 20 by Robert Love.
1740 *
1741 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1742 *
1743 * Note this is a helper function intended to be used by LSMs which
1744 * wish to use this logic.
1745 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001746int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 unsigned long free, allowed;
1749
1750 vm_acct_memory(pages);
1751
1752 /*
1753 * Sometimes we want to use more memory than we have
1754 */
1755 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1756 return 0;
1757
1758 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1759 unsigned long n;
1760
Christoph Lameter347ce432006-06-30 01:55:35 -07001761 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 free += nr_swap_pages;
1763
1764 /*
1765 * Any slabs which are created with the
1766 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1767 * which are reclaimable, under pressure. The dentry
1768 * cache and most inode caches should fall into this
1769 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001770 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /*
1773 * Leave the last 3% for root
1774 */
1775 if (!cap_sys_admin)
1776 free -= free / 32;
1777
1778 if (free > pages)
1779 return 0;
1780
1781 /*
1782 * nr_free_pages() is very expensive on large systems,
1783 * only call if we're about to fail.
1784 */
1785 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001786
1787 /*
1788 * Leave reserved pages. The pages are not for anonymous pages.
1789 */
1790 if (n <= totalreserve_pages)
1791 goto error;
1792 else
1793 n -= totalreserve_pages;
1794
1795 /*
1796 * Leave the last 3% for root
1797 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (!cap_sys_admin)
1799 n -= n / 32;
1800 free += n;
1801
1802 if (free > pages)
1803 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001804
1805 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
1808 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1809 /*
1810 * Leave the last 3% for root
1811 */
1812 if (!cap_sys_admin)
1813 allowed -= allowed / 32;
1814 allowed += total_swap_pages;
1815
1816 /* Don't let a single process grow too big:
1817 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001818 if (mm)
1819 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Simon Derr2f60f8d2005-08-04 19:52:03 -07001821 /*
1822 * cast `allowed' as a signed long because vm_committed_space
1823 * sometimes has a negative value
1824 */
Alan Cox80119ef2008-05-23 13:04:31 -07001825 if (atomic_long_read(&vm_committed_space) < (long)allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001827error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 vm_unacct_memory(pages);
1829
1830 return -ENOMEM;
1831}
1832
1833int in_gate_area_no_task(unsigned long addr)
1834{
1835 return 0;
1836}
David Howellsb0e15192006-01-06 00:11:42 -08001837
Nick Piggind0217ac2007-07-19 01:47:03 -07001838int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001839{
1840 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001841 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001842}
Paul Mundtb5073172007-07-21 04:37:25 -07001843EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001844
1845/*
1846 * Access another process' address space.
1847 * - source/target buffer must be kernel space
1848 */
1849int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1850{
David Howells0ec76a12006-09-27 01:50:15 -07001851 struct vm_area_struct *vma;
1852 struct mm_struct *mm;
1853
1854 if (addr + len < addr)
1855 return 0;
1856
1857 mm = get_task_mm(tsk);
1858 if (!mm)
1859 return 0;
1860
1861 down_read(&mm->mmap_sem);
1862
1863 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001864 vma = find_vma(mm, addr);
1865 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001866 /* don't overrun this mapping */
1867 if (addr + len >= vma->vm_end)
1868 len = vma->vm_end - addr;
1869
1870 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001871 if (write && vma->vm_flags & VM_MAYWRITE)
David Howells0ec76a12006-09-27 01:50:15 -07001872 len -= copy_to_user((void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001873 else if (!write && vma->vm_flags & VM_MAYREAD)
David Howells0ec76a12006-09-27 01:50:15 -07001874 len -= copy_from_user(buf, (void *) addr, len);
1875 else
1876 len = 0;
1877 } else {
1878 len = 0;
1879 }
1880
1881 up_read(&mm->mmap_sem);
1882 mmput(mm);
1883 return len;
1884}