perf list: Add cache events
[linux-block.git] / mm / nommu.c
CommitLineData
1da177e4
LT
1/*
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 *
8feae131 9 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
1da177e4
LT
10 * 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>
eb6434d9 13 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
1da177e4
LT
14 */
15
f2b8544f 16#include <linux/module.h>
1da177e4
LT
17#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>
fa8e26cc 25#include <linux/tracehook.h>
1da177e4
LT
26#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>
8feae131
DH
36#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
1da177e4 58
b291f000
NP
59#include "internal.h"
60
1da177e4
LT
61void *high_memory;
62struct page *mem_map;
63unsigned long max_mapnr;
64unsigned long num_physpages;
00a62ce9 65struct percpu_counter vm_committed_as;
1da177e4
LT
66int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
67int sysctl_overcommit_ratio = 50; /* default is 50% */
68int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
fc4d5c29 69int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
1da177e4
LT
70int heap_stack_gap = 0;
71
35f2c2f6
PM
72/* amount of vm to protect from userspace access */
73unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
74
33e5d769 75atomic_long_t mmap_pages_allocated;
8feae131 76
1da177e4 77EXPORT_SYMBOL(mem_map);
6a04de6d 78EXPORT_SYMBOL(num_physpages);
1da177e4 79
8feae131
DH
80/* list of mapped, potentially shareable regions */
81static struct kmem_cache *vm_region_jar;
82struct rb_root nommu_region_tree = RB_ROOT;
83DECLARE_RWSEM(nommu_region_sem);
1da177e4
LT
84
85struct vm_operations_struct generic_file_vm_ops = {
86};
87
88/*
89 * Handle all mappings that got truncated by a "truncate()"
90 * system call.
91 *
92 * NOTE! We have to be ready to update the memory sharing
93 * between the file and the memory map for a potential last
94 * incomplete page. Ugly, but necessary.
95 */
96int vmtruncate(struct inode *inode, loff_t offset)
97{
98 struct address_space *mapping = inode->i_mapping;
99 unsigned long limit;
100
101 if (inode->i_size < offset)
102 goto do_expand;
103 i_size_write(inode, offset);
104
105 truncate_inode_pages(mapping, offset);
106 goto out_truncate;
107
108do_expand:
109 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
110 if (limit != RLIM_INFINITY && offset > limit)
111 goto out_sig;
112 if (offset > inode->i_sb->s_maxbytes)
113 goto out;
114 i_size_write(inode, offset);
115
116out_truncate:
acfa4380 117 if (inode->i_op->truncate)
1da177e4
LT
118 inode->i_op->truncate(inode);
119 return 0;
120out_sig:
121 send_sig(SIGXFSZ, current, 0);
122out:
123 return -EFBIG;
124}
125
126EXPORT_SYMBOL(vmtruncate);
127
128/*
129 * Return the total memory allocated for this pointer, not
130 * just what the caller asked for.
131 *
132 * Doesn't have to be accurate, i.e. may have races.
133 */
134unsigned int kobjsize(const void *objp)
135{
136 struct page *page;
137
4016a139
MH
138 /*
139 * If the object we have should not have ksize performed on it,
140 * return size of 0
141 */
5a1603be 142 if (!objp || !virt_addr_valid(objp))
6cfd53fc
PM
143 return 0;
144
145 page = virt_to_head_page(objp);
6cfd53fc
PM
146
147 /*
148 * If the allocator sets PageSlab, we know the pointer came from
149 * kmalloc().
150 */
1da177e4
LT
151 if (PageSlab(page))
152 return ksize(objp);
153
ab2e83ea
PM
154 /*
155 * If it's not a compound page, see if we have a matching VMA
156 * region. This test is intentionally done in reverse order,
157 * so if there's no VMA, we still fall through and hand back
158 * PAGE_SIZE for 0-order pages.
159 */
160 if (!PageCompound(page)) {
161 struct vm_area_struct *vma;
162
163 vma = find_vma(current->mm, (unsigned long)objp);
164 if (vma)
165 return vma->vm_end - vma->vm_start;
166 }
167
6cfd53fc
PM
168 /*
169 * The ksize() function is only guaranteed to work for pointers
5a1603be 170 * returned by kmalloc(). So handle arbitrary pointers here.
6cfd53fc 171 */
5a1603be 172 return PAGE_SIZE << compound_order(page);
1da177e4
LT
173}
174
b291f000 175int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
9d73777e
PZ
176 unsigned long start, int nr_pages, int flags,
177 struct page **pages, struct vm_area_struct **vmas)
1da177e4 178{
910e46da 179 struct vm_area_struct *vma;
7b4d5b8b
DH
180 unsigned long vm_flags;
181 int i;
b291f000
NP
182 int write = !!(flags & GUP_FLAGS_WRITE);
183 int force = !!(flags & GUP_FLAGS_FORCE);
184 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
7b4d5b8b
DH
185
186 /* calculate required read or write permissions.
187 * - if 'force' is set, we only require the "MAY" flags.
188 */
189 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
190 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1da177e4 191
9d73777e 192 for (i = 0; i < nr_pages; i++) {
910e46da 193 vma = find_vma(mm, start);
7b4d5b8b
DH
194 if (!vma)
195 goto finish_or_fault;
196
197 /* protect what we can, including chardevs */
198 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
b291f000 199 (!ignore && !(vm_flags & vma->vm_flags)))
7b4d5b8b 200 goto finish_or_fault;
910e46da 201
1da177e4
LT
202 if (pages) {
203 pages[i] = virt_to_page(start);
204 if (pages[i])
205 page_cache_get(pages[i]);
206 }
207 if (vmas)
910e46da 208 vmas[i] = vma;
1da177e4
LT
209 start += PAGE_SIZE;
210 }
7b4d5b8b
DH
211
212 return i;
213
214finish_or_fault:
215 return i ? : -EFAULT;
1da177e4 216}
b291f000
NP
217
218
219/*
220 * get a list of pages in an address range belonging to the specified process
221 * and indicate the VMA that covers each page
222 * - this is potentially dodgy as we may end incrementing the page count of a
223 * slab page or a secondary page from a compound page
224 * - don't permit access to VMAs that don't support it, such as I/O mappings
225 */
226int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
9d73777e 227 unsigned long start, int nr_pages, int write, int force,
b291f000
NP
228 struct page **pages, struct vm_area_struct **vmas)
229{
230 int flags = 0;
231
232 if (write)
233 flags |= GUP_FLAGS_WRITE;
234 if (force)
235 flags |= GUP_FLAGS_FORCE;
236
9d73777e 237 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
b291f000 238}
66aa2b4b
GU
239EXPORT_SYMBOL(get_user_pages);
240
1da177e4
LT
241DEFINE_RWLOCK(vmlist_lock);
242struct vm_struct *vmlist;
243
b3bdda02 244void vfree(const void *addr)
1da177e4
LT
245{
246 kfree(addr);
247}
b5073173 248EXPORT_SYMBOL(vfree);
1da177e4 249
dd0fc66f 250void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1da177e4
LT
251{
252 /*
8518609d
RD
253 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
254 * returns only a logical address.
1da177e4 255 */
84097518 256 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1da177e4 257}
b5073173 258EXPORT_SYMBOL(__vmalloc);
1da177e4 259
f905bc44
PM
260void *vmalloc_user(unsigned long size)
261{
262 void *ret;
263
264 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
265 PAGE_KERNEL);
266 if (ret) {
267 struct vm_area_struct *vma;
268
269 down_write(&current->mm->mmap_sem);
270 vma = find_vma(current->mm, (unsigned long)ret);
271 if (vma)
272 vma->vm_flags |= VM_USERMAP;
273 up_write(&current->mm->mmap_sem);
274 }
275
276 return ret;
277}
278EXPORT_SYMBOL(vmalloc_user);
279
b3bdda02 280struct page *vmalloc_to_page(const void *addr)
1da177e4
LT
281{
282 return virt_to_page(addr);
283}
b5073173 284EXPORT_SYMBOL(vmalloc_to_page);
1da177e4 285
b3bdda02 286unsigned long vmalloc_to_pfn(const void *addr)
1da177e4
LT
287{
288 return page_to_pfn(virt_to_page(addr));
289}
b5073173 290EXPORT_SYMBOL(vmalloc_to_pfn);
1da177e4
LT
291
292long vread(char *buf, char *addr, unsigned long count)
293{
294 memcpy(buf, addr, count);
295 return count;
296}
297
298long vwrite(char *buf, char *addr, unsigned long count)
299{
300 /* Don't allow overflow */
301 if ((unsigned long) addr + count < count)
302 count = -(unsigned long) addr;
303
304 memcpy(addr, buf, count);
305 return(count);
306}
307
308/*
309 * vmalloc - allocate virtually continguos memory
310 *
311 * @size: allocation size
312 *
313 * Allocate enough pages to cover @size from the page level
314 * allocator and map them into continguos kernel virtual space.
315 *
c1c8897f 316 * For tight control over page level allocator and protection flags
1da177e4
LT
317 * use __vmalloc() instead.
318 */
319void *vmalloc(unsigned long size)
320{
321 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
322}
f6138882
AM
323EXPORT_SYMBOL(vmalloc);
324
325void *vmalloc_node(unsigned long size, int node)
326{
327 return vmalloc(size);
328}
329EXPORT_SYMBOL(vmalloc_node);
1da177e4 330
1af446ed
PM
331#ifndef PAGE_KERNEL_EXEC
332# define PAGE_KERNEL_EXEC PAGE_KERNEL
333#endif
334
335/**
336 * vmalloc_exec - allocate virtually contiguous, executable memory
337 * @size: allocation size
338 *
339 * Kernel-internal function to allocate enough pages to cover @size
340 * the page level allocator and map them into contiguous and
341 * executable kernel virtual space.
342 *
343 * For tight control over page level allocator and protection flags
344 * use __vmalloc() instead.
345 */
346
347void *vmalloc_exec(unsigned long size)
348{
349 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
350}
351
b5073173
PM
352/**
353 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1da177e4
LT
354 * @size: allocation size
355 *
356 * Allocate enough 32bit PA addressable pages to cover @size from the
357 * page level allocator and map them into continguos kernel virtual space.
358 */
359void *vmalloc_32(unsigned long size)
360{
361 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
362}
b5073173
PM
363EXPORT_SYMBOL(vmalloc_32);
364
365/**
366 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
367 * @size: allocation size
368 *
369 * The resulting memory area is 32bit addressable and zeroed so it can be
370 * mapped to userspace without leaking data.
f905bc44
PM
371 *
372 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
373 * remap_vmalloc_range() are permissible.
b5073173
PM
374 */
375void *vmalloc_32_user(unsigned long size)
376{
f905bc44
PM
377 /*
378 * We'll have to sort out the ZONE_DMA bits for 64-bit,
379 * but for now this can simply use vmalloc_user() directly.
380 */
381 return vmalloc_user(size);
b5073173
PM
382}
383EXPORT_SYMBOL(vmalloc_32_user);
1da177e4
LT
384
385void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
386{
387 BUG();
388 return NULL;
389}
b5073173 390EXPORT_SYMBOL(vmap);
1da177e4 391
b3bdda02 392void vunmap(const void *addr)
1da177e4
LT
393{
394 BUG();
395}
b5073173 396EXPORT_SYMBOL(vunmap);
1da177e4 397
eb6434d9
PM
398void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
399{
400 BUG();
401 return NULL;
402}
403EXPORT_SYMBOL(vm_map_ram);
404
405void vm_unmap_ram(const void *mem, unsigned int count)
406{
407 BUG();
408}
409EXPORT_SYMBOL(vm_unmap_ram);
410
411void vm_unmap_aliases(void)
412{
413}
414EXPORT_SYMBOL_GPL(vm_unmap_aliases);
415
1eeb66a1
CH
416/*
417 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
418 * have one.
419 */
420void __attribute__((weak)) vmalloc_sync_all(void)
421{
422}
423
b5073173
PM
424int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
425 struct page *page)
426{
427 return -EINVAL;
428}
429EXPORT_SYMBOL(vm_insert_page);
430
1da177e4
LT
431/*
432 * sys_brk() for the most part doesn't need the global kernel
433 * lock, except when an application is doing something nasty
434 * like trying to un-brk an area that has already been mapped
435 * to a regular file. in this case, the unmapping will need
436 * to invoke file system routines that need the global lock.
437 */
6a6160a7 438SYSCALL_DEFINE1(brk, unsigned long, brk)
1da177e4
LT
439{
440 struct mm_struct *mm = current->mm;
441
442 if (brk < mm->start_brk || brk > mm->context.end_brk)
443 return mm->brk;
444
445 if (mm->brk == brk)
446 return mm->brk;
447
448 /*
449 * Always allow shrinking brk
450 */
451 if (brk <= mm->brk) {
452 mm->brk = brk;
453 return brk;
454 }
455
456 /*
457 * Ok, looks good - let it rip.
458 */
459 return mm->brk = brk;
460}
461
8feae131
DH
462/*
463 * initialise the VMA and region record slabs
464 */
465void __init mmap_init(void)
1da177e4 466{
00a62ce9
KM
467 int ret;
468
469 ret = percpu_counter_init(&vm_committed_as, 0);
470 VM_BUG_ON(ret);
33e5d769 471 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
1da177e4 472}
1da177e4 473
3034097a 474/*
8feae131
DH
475 * validate the region tree
476 * - the caller must hold the region lock
3034097a 477 */
8feae131
DH
478#ifdef CONFIG_DEBUG_NOMMU_REGIONS
479static noinline void validate_nommu_regions(void)
3034097a 480{
8feae131
DH
481 struct vm_region *region, *last;
482 struct rb_node *p, *lastp;
3034097a 483
8feae131
DH
484 lastp = rb_first(&nommu_region_tree);
485 if (!lastp)
486 return;
487
488 last = rb_entry(lastp, struct vm_region, vm_rb);
33e5d769
DH
489 BUG_ON(unlikely(last->vm_end <= last->vm_start));
490 BUG_ON(unlikely(last->vm_top < last->vm_end));
8feae131
DH
491
492 while ((p = rb_next(lastp))) {
493 region = rb_entry(p, struct vm_region, vm_rb);
494 last = rb_entry(lastp, struct vm_region, vm_rb);
495
33e5d769
DH
496 BUG_ON(unlikely(region->vm_end <= region->vm_start));
497 BUG_ON(unlikely(region->vm_top < region->vm_end));
498 BUG_ON(unlikely(region->vm_start < last->vm_top));
3034097a 499
8feae131
DH
500 lastp = p;
501 }
3034097a 502}
8feae131 503#else
33e5d769
DH
504static void validate_nommu_regions(void)
505{
506}
8feae131 507#endif
3034097a
DH
508
509/*
8feae131 510 * add a region into the global tree
3034097a 511 */
8feae131 512static void add_nommu_region(struct vm_region *region)
3034097a 513{
8feae131
DH
514 struct vm_region *pregion;
515 struct rb_node **p, *parent;
3034097a 516
8feae131
DH
517 validate_nommu_regions();
518
8feae131
DH
519 parent = NULL;
520 p = &nommu_region_tree.rb_node;
521 while (*p) {
522 parent = *p;
523 pregion = rb_entry(parent, struct vm_region, vm_rb);
524 if (region->vm_start < pregion->vm_start)
525 p = &(*p)->rb_left;
526 else if (region->vm_start > pregion->vm_start)
527 p = &(*p)->rb_right;
528 else if (pregion == region)
529 return;
530 else
531 BUG();
3034097a
DH
532 }
533
8feae131
DH
534 rb_link_node(&region->vm_rb, parent, p);
535 rb_insert_color(&region->vm_rb, &nommu_region_tree);
3034097a 536
8feae131 537 validate_nommu_regions();
3034097a 538}
3034097a 539
930e652a 540/*
8feae131 541 * delete a region from the global tree
930e652a 542 */
8feae131 543static void delete_nommu_region(struct vm_region *region)
930e652a 544{
8feae131 545 BUG_ON(!nommu_region_tree.rb_node);
930e652a 546
8feae131
DH
547 validate_nommu_regions();
548 rb_erase(&region->vm_rb, &nommu_region_tree);
549 validate_nommu_regions();
57c8f63e
GU
550}
551
6fa5f80b 552/*
8feae131 553 * free a contiguous series of pages
6fa5f80b 554 */
8feae131 555static void free_page_series(unsigned long from, unsigned long to)
6fa5f80b 556{
8feae131
DH
557 for (; from < to; from += PAGE_SIZE) {
558 struct page *page = virt_to_page(from);
559
560 kdebug("- free %lx", from);
33e5d769 561 atomic_long_dec(&mmap_pages_allocated);
8feae131 562 if (page_count(page) != 1)
33e5d769
DH
563 kdebug("free page %p: refcount not one: %d",
564 page, page_count(page));
8feae131 565 put_page(page);
6fa5f80b 566 }
6fa5f80b
DH
567}
568
3034097a 569/*
8feae131 570 * release a reference to a region
33e5d769 571 * - the caller must hold the region semaphore for writing, which this releases
dd8632a1 572 * - the region may not have been added to the tree yet, in which case vm_top
8feae131 573 * will equal vm_start
3034097a 574 */
8feae131
DH
575static void __put_nommu_region(struct vm_region *region)
576 __releases(nommu_region_sem)
1da177e4 577{
8feae131 578 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
1da177e4 579
8feae131 580 BUG_ON(!nommu_region_tree.rb_node);
1da177e4 581
8feae131 582 if (atomic_dec_and_test(&region->vm_usage)) {
dd8632a1 583 if (region->vm_top > region->vm_start)
8feae131
DH
584 delete_nommu_region(region);
585 up_write(&nommu_region_sem);
586
587 if (region->vm_file)
588 fput(region->vm_file);
589
590 /* IO memory and memory shared directly out of the pagecache
591 * from ramfs/tmpfs mustn't be released here */
592 if (region->vm_flags & VM_MAPPED_COPY) {
593 kdebug("free series");
dd8632a1 594 free_page_series(region->vm_start, region->vm_top);
8feae131
DH
595 }
596 kmem_cache_free(vm_region_jar, region);
597 } else {
598 up_write(&nommu_region_sem);
1da177e4 599 }
8feae131 600}
1da177e4 601
8feae131
DH
602/*
603 * release a reference to a region
604 */
605static void put_nommu_region(struct vm_region *region)
606{
607 down_write(&nommu_region_sem);
608 __put_nommu_region(region);
1da177e4
LT
609}
610
3034097a 611/*
8feae131
DH
612 * add a VMA into a process's mm_struct in the appropriate place in the list
613 * and tree and add to the address space's page tree also if not an anonymous
614 * page
615 * - should be called with mm->mmap_sem held writelocked
3034097a 616 */
8feae131 617static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 618{
8feae131 619 struct vm_area_struct *pvma, **pp;
1da177e4 620 struct address_space *mapping;
8feae131
DH
621 struct rb_node **p, *parent;
622
623 kenter(",%p", vma);
624
625 BUG_ON(!vma->vm_region);
626
627 mm->map_count++;
628 vma->vm_mm = mm;
1da177e4
LT
629
630 /* add the VMA to the mapping */
631 if (vma->vm_file) {
632 mapping = vma->vm_file->f_mapping;
633
634 flush_dcache_mmap_lock(mapping);
635 vma_prio_tree_insert(vma, &mapping->i_mmap);
636 flush_dcache_mmap_unlock(mapping);
637 }
638
8feae131
DH
639 /* add the VMA to the tree */
640 parent = NULL;
641 p = &mm->mm_rb.rb_node;
1da177e4
LT
642 while (*p) {
643 parent = *p;
644 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
645
8feae131
DH
646 /* sort by: start addr, end addr, VMA struct addr in that order
647 * (the latter is necessary as we may get identical VMAs) */
648 if (vma->vm_start < pvma->vm_start)
1da177e4 649 p = &(*p)->rb_left;
8feae131 650 else if (vma->vm_start > pvma->vm_start)
1da177e4 651 p = &(*p)->rb_right;
8feae131
DH
652 else if (vma->vm_end < pvma->vm_end)
653 p = &(*p)->rb_left;
654 else if (vma->vm_end > pvma->vm_end)
655 p = &(*p)->rb_right;
656 else if (vma < pvma)
657 p = &(*p)->rb_left;
658 else if (vma > pvma)
659 p = &(*p)->rb_right;
660 else
661 BUG();
1da177e4
LT
662 }
663
664 rb_link_node(&vma->vm_rb, parent, p);
8feae131
DH
665 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
666
667 /* add VMA to the VMA list also */
668 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
669 if (pvma->vm_start > vma->vm_start)
670 break;
671 if (pvma->vm_start < vma->vm_start)
672 continue;
673 if (pvma->vm_end < vma->vm_end)
674 break;
675 }
676
677 vma->vm_next = *pp;
678 *pp = vma;
1da177e4
LT
679}
680
3034097a 681/*
8feae131 682 * delete a VMA from its owning mm_struct and address space
3034097a 683 */
8feae131 684static void delete_vma_from_mm(struct vm_area_struct *vma)
1da177e4 685{
8feae131 686 struct vm_area_struct **pp;
1da177e4 687 struct address_space *mapping;
8feae131
DH
688 struct mm_struct *mm = vma->vm_mm;
689
690 kenter("%p", vma);
691
692 mm->map_count--;
693 if (mm->mmap_cache == vma)
694 mm->mmap_cache = NULL;
1da177e4
LT
695
696 /* remove the VMA from the mapping */
697 if (vma->vm_file) {
698 mapping = vma->vm_file->f_mapping;
699
700 flush_dcache_mmap_lock(mapping);
701 vma_prio_tree_remove(vma, &mapping->i_mmap);
702 flush_dcache_mmap_unlock(mapping);
703 }
704
8feae131
DH
705 /* remove from the MM's tree and list */
706 rb_erase(&vma->vm_rb, &mm->mm_rb);
707 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
708 if (*pp == vma) {
709 *pp = vma->vm_next;
710 break;
711 }
712 }
713
714 vma->vm_mm = NULL;
715}
716
717/*
718 * destroy a VMA record
719 */
720static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
721{
722 kenter("%p", vma);
723 if (vma->vm_ops && vma->vm_ops->close)
724 vma->vm_ops->close(vma);
725 if (vma->vm_file) {
726 fput(vma->vm_file);
727 if (vma->vm_flags & VM_EXECUTABLE)
728 removed_exe_file_vma(mm);
729 }
730 put_nommu_region(vma->vm_region);
731 kmem_cache_free(vm_area_cachep, vma);
732}
733
734/*
735 * look up the first VMA in which addr resides, NULL if none
736 * - should be called with mm->mmap_sem at least held readlocked
737 */
738struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
739{
740 struct vm_area_struct *vma;
741 struct rb_node *n = mm->mm_rb.rb_node;
742
743 /* check the cache first */
744 vma = mm->mmap_cache;
745 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
746 return vma;
747
748 /* trawl the tree (there may be multiple mappings in which addr
749 * resides) */
750 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
751 vma = rb_entry(n, struct vm_area_struct, vm_rb);
752 if (vma->vm_start > addr)
753 return NULL;
754 if (vma->vm_end > addr) {
755 mm->mmap_cache = vma;
756 return vma;
757 }
758 }
759
760 return NULL;
761}
762EXPORT_SYMBOL(find_vma);
763
764/*
765 * find a VMA
766 * - we don't extend stack VMAs under NOMMU conditions
767 */
768struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
769{
770 return find_vma(mm, addr);
771}
772
773/*
774 * expand a stack to a given address
775 * - not supported under NOMMU conditions
776 */
777int expand_stack(struct vm_area_struct *vma, unsigned long address)
778{
779 return -ENOMEM;
780}
781
782/*
783 * look up the first VMA exactly that exactly matches addr
784 * - should be called with mm->mmap_sem at least held readlocked
785 */
786static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
787 unsigned long addr,
788 unsigned long len)
789{
790 struct vm_area_struct *vma;
791 struct rb_node *n = mm->mm_rb.rb_node;
792 unsigned long end = addr + len;
793
794 /* check the cache first */
795 vma = mm->mmap_cache;
796 if (vma && vma->vm_start == addr && vma->vm_end == end)
797 return vma;
798
799 /* trawl the tree (there may be multiple mappings in which addr
800 * resides) */
801 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
802 vma = rb_entry(n, struct vm_area_struct, vm_rb);
803 if (vma->vm_start < addr)
804 continue;
805 if (vma->vm_start > addr)
806 return NULL;
807 if (vma->vm_end == end) {
808 mm->mmap_cache = vma;
809 return vma;
810 }
811 }
812
813 return NULL;
1da177e4
LT
814}
815
816/*
817 * determine whether a mapping should be permitted and, if so, what sort of
818 * mapping we're capable of supporting
819 */
820static int validate_mmap_request(struct file *file,
821 unsigned long addr,
822 unsigned long len,
823 unsigned long prot,
824 unsigned long flags,
825 unsigned long pgoff,
826 unsigned long *_capabilities)
827{
8feae131 828 unsigned long capabilities, rlen;
1da177e4
LT
829 unsigned long reqprot = prot;
830 int ret;
831
832 /* do the simple checks first */
833 if (flags & MAP_FIXED || addr) {
834 printk(KERN_DEBUG
835 "%d: Can't do fixed-address/overlay mmap of RAM\n",
836 current->pid);
837 return -EINVAL;
838 }
839
840 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
841 (flags & MAP_TYPE) != MAP_SHARED)
842 return -EINVAL;
843
f81cff0d 844 if (!len)
1da177e4
LT
845 return -EINVAL;
846
f81cff0d 847 /* Careful about overflows.. */
8feae131
DH
848 rlen = PAGE_ALIGN(len);
849 if (!rlen || rlen > TASK_SIZE)
f81cff0d
MF
850 return -ENOMEM;
851
1da177e4 852 /* offset overflow? */
8feae131 853 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
f81cff0d 854 return -EOVERFLOW;
1da177e4
LT
855
856 if (file) {
857 /* validate file mapping requests */
858 struct address_space *mapping;
859
860 /* files must support mmap */
861 if (!file->f_op || !file->f_op->mmap)
862 return -ENODEV;
863
864 /* work out if what we've got could possibly be shared
865 * - we support chardevs that provide their own "memory"
866 * - we support files/blockdevs that are memory backed
867 */
868 mapping = file->f_mapping;
869 if (!mapping)
e9536ae7 870 mapping = file->f_path.dentry->d_inode->i_mapping;
1da177e4
LT
871
872 capabilities = 0;
873 if (mapping && mapping->backing_dev_info)
874 capabilities = mapping->backing_dev_info->capabilities;
875
876 if (!capabilities) {
877 /* no explicit capabilities set, so assume some
878 * defaults */
e9536ae7 879 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
1da177e4
LT
880 case S_IFREG:
881 case S_IFBLK:
882 capabilities = BDI_CAP_MAP_COPY;
883 break;
884
885 case S_IFCHR:
886 capabilities =
887 BDI_CAP_MAP_DIRECT |
888 BDI_CAP_READ_MAP |
889 BDI_CAP_WRITE_MAP;
890 break;
891
892 default:
893 return -EINVAL;
894 }
895 }
896
897 /* eliminate any capabilities that we can't support on this
898 * device */
899 if (!file->f_op->get_unmapped_area)
900 capabilities &= ~BDI_CAP_MAP_DIRECT;
901 if (!file->f_op->read)
902 capabilities &= ~BDI_CAP_MAP_COPY;
903
904 if (flags & MAP_SHARED) {
905 /* do checks for writing, appending and locking */
906 if ((prot & PROT_WRITE) &&
907 !(file->f_mode & FMODE_WRITE))
908 return -EACCES;
909
e9536ae7 910 if (IS_APPEND(file->f_path.dentry->d_inode) &&
1da177e4
LT
911 (file->f_mode & FMODE_WRITE))
912 return -EACCES;
913
e9536ae7 914 if (locks_verify_locked(file->f_path.dentry->d_inode))
1da177e4
LT
915 return -EAGAIN;
916
917 if (!(capabilities & BDI_CAP_MAP_DIRECT))
918 return -ENODEV;
919
920 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
921 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
922 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
923 ) {
924 printk("MAP_SHARED not completely supported on !MMU\n");
925 return -EINVAL;
926 }
927
928 /* we mustn't privatise shared mappings */
929 capabilities &= ~BDI_CAP_MAP_COPY;
930 }
931 else {
932 /* we're going to read the file into private memory we
933 * allocate */
934 if (!(capabilities & BDI_CAP_MAP_COPY))
935 return -ENODEV;
936
937 /* we don't permit a private writable mapping to be
938 * shared with the backing device */
939 if (prot & PROT_WRITE)
940 capabilities &= ~BDI_CAP_MAP_DIRECT;
941 }
942
943 /* handle executable mappings and implied executable
944 * mappings */
e9536ae7 945 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1da177e4
LT
946 if (prot & PROT_EXEC)
947 return -EPERM;
948 }
949 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
950 /* handle implication of PROT_EXEC by PROT_READ */
951 if (current->personality & READ_IMPLIES_EXEC) {
952 if (capabilities & BDI_CAP_EXEC_MAP)
953 prot |= PROT_EXEC;
954 }
955 }
956 else if ((prot & PROT_READ) &&
957 (prot & PROT_EXEC) &&
958 !(capabilities & BDI_CAP_EXEC_MAP)
959 ) {
960 /* backing file is not executable, try to copy */
961 capabilities &= ~BDI_CAP_MAP_DIRECT;
962 }
963 }
964 else {
965 /* anonymous mappings are always memory backed and can be
966 * privately mapped
967 */
968 capabilities = BDI_CAP_MAP_COPY;
969
970 /* handle PROT_EXEC implication by PROT_READ */
971 if ((prot & PROT_READ) &&
972 (current->personality & READ_IMPLIES_EXEC))
973 prot |= PROT_EXEC;
974 }
975
976 /* allow the security API to have its say */
ed032189 977 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1da177e4
LT
978 if (ret < 0)
979 return ret;
980
981 /* looks okay */
982 *_capabilities = capabilities;
983 return 0;
984}
985
986/*
987 * we've determined that we can make the mapping, now translate what we
988 * now know into VMA flags
989 */
990static unsigned long determine_vm_flags(struct file *file,
991 unsigned long prot,
992 unsigned long flags,
993 unsigned long capabilities)
994{
995 unsigned long vm_flags;
996
997 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
998 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
999 /* vm_flags |= mm->def_flags; */
1000
1001 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1002 /* attempt to share read-only copies of mapped file chunks */
1003 if (file && !(prot & PROT_WRITE))
1004 vm_flags |= VM_MAYSHARE;
1005 }
1006 else {
1007 /* overlay a shareable mapping on the backing device or inode
1008 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1009 * romfs/cramfs */
1010 if (flags & MAP_SHARED)
1011 vm_flags |= VM_MAYSHARE | VM_SHARED;
1012 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
1013 vm_flags |= VM_MAYSHARE;
1014 }
1015
1016 /* refuse to let anyone share private mappings with this process if
1017 * it's being traced - otherwise breakpoints set in it may interfere
1018 * with another untraced process
1019 */
fa8e26cc 1020 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
1da177e4
LT
1021 vm_flags &= ~VM_MAYSHARE;
1022
1023 return vm_flags;
1024}
1025
1026/*
8feae131
DH
1027 * set up a shared mapping on a file (the driver or filesystem provides and
1028 * pins the storage)
1da177e4 1029 */
8feae131 1030static int do_mmap_shared_file(struct vm_area_struct *vma)
1da177e4
LT
1031{
1032 int ret;
1033
1034 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
dd8632a1
PM
1035 if (ret == 0) {
1036 vma->vm_region->vm_top = vma->vm_region->vm_end;
1037 return ret;
1038 }
1da177e4
LT
1039 if (ret != -ENOSYS)
1040 return ret;
1041
1042 /* getting an ENOSYS error indicates that direct mmap isn't
1043 * possible (as opposed to tried but failed) so we'll fall
1044 * through to making a private copy of the data and mapping
1045 * that if we can */
1046 return -ENODEV;
1047}
1048
1049/*
1050 * set up a private mapping or an anonymous shared mapping
1051 */
8feae131
DH
1052static int do_mmap_private(struct vm_area_struct *vma,
1053 struct vm_region *region,
1054 unsigned long len)
1da177e4 1055{
8feae131
DH
1056 struct page *pages;
1057 unsigned long total, point, n, rlen;
1da177e4 1058 void *base;
8feae131 1059 int ret, order;
1da177e4
LT
1060
1061 /* invoke the file's mapping function so that it can keep track of
1062 * shared mappings on devices or memory
1063 * - VM_MAYSHARE will be set if it may attempt to share
1064 */
1065 if (vma->vm_file) {
1066 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
dd8632a1 1067 if (ret == 0) {
1da177e4 1068 /* shouldn't return success if we're not sharing */
dd8632a1
PM
1069 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1070 vma->vm_region->vm_top = vma->vm_region->vm_end;
1071 return ret;
1da177e4 1072 }
dd8632a1
PM
1073 if (ret != -ENOSYS)
1074 return ret;
1da177e4
LT
1075
1076 /* getting an ENOSYS error indicates that direct mmap isn't
1077 * possible (as opposed to tried but failed) so we'll try to
1078 * make a private copy of the data and map that instead */
1079 }
1080
8feae131
DH
1081 rlen = PAGE_ALIGN(len);
1082
1da177e4
LT
1083 /* allocate some memory to hold the mapping
1084 * - note that this may not return a page-aligned address if the object
1085 * we're allocating is smaller than a page
1086 */
8feae131
DH
1087 order = get_order(rlen);
1088 kdebug("alloc order %d for %lx", order, len);
1089
1090 pages = alloc_pages(GFP_KERNEL, order);
1091 if (!pages)
1da177e4
LT
1092 goto enomem;
1093
8feae131 1094 total = 1 << order;
33e5d769 1095 atomic_long_add(total, &mmap_pages_allocated);
8feae131
DH
1096
1097 point = rlen >> PAGE_SHIFT;
dd8632a1
PM
1098
1099 /* we allocated a power-of-2 sized page set, so we may want to trim off
1100 * the excess */
1101 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1102 while (total > point) {
1103 order = ilog2(total - point);
1104 n = 1 << order;
1105 kdebug("shave %lu/%lu @%lu", n, total - point, total);
33e5d769 1106 atomic_long_sub(n, &mmap_pages_allocated);
dd8632a1
PM
1107 total -= n;
1108 set_page_refcounted(pages + total);
1109 __free_pages(pages + total, order);
1110 }
8feae131
DH
1111 }
1112
8feae131
DH
1113 for (point = 1; point < total; point++)
1114 set_page_refcounted(&pages[point]);
1da177e4 1115
8feae131
DH
1116 base = page_address(pages);
1117 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1118 region->vm_start = (unsigned long) base;
1119 region->vm_end = region->vm_start + rlen;
dd8632a1 1120 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
8feae131
DH
1121
1122 vma->vm_start = region->vm_start;
1123 vma->vm_end = region->vm_start + len;
1da177e4
LT
1124
1125 if (vma->vm_file) {
1126 /* read the contents of a file into the copy */
1127 mm_segment_t old_fs;
1128 loff_t fpos;
1129
1130 fpos = vma->vm_pgoff;
1131 fpos <<= PAGE_SHIFT;
1132
1133 old_fs = get_fs();
1134 set_fs(KERNEL_DS);
8feae131 1135 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
1da177e4
LT
1136 set_fs(old_fs);
1137
1138 if (ret < 0)
1139 goto error_free;
1140
1141 /* clear the last little bit */
8feae131
DH
1142 if (ret < rlen)
1143 memset(base + ret, 0, rlen - ret);
1da177e4
LT
1144
1145 } else {
1146 /* if it's an anonymous mapping, then just clear it */
8feae131 1147 memset(base, 0, rlen);
1da177e4
LT
1148 }
1149
1150 return 0;
1151
1152error_free:
8feae131
DH
1153 free_page_series(region->vm_start, region->vm_end);
1154 region->vm_start = vma->vm_start = 0;
1155 region->vm_end = vma->vm_end = 0;
dd8632a1 1156 region->vm_top = 0;
1da177e4
LT
1157 return ret;
1158
1159enomem:
05ae6fa3
GU
1160 printk("Allocation of length %lu from process %d (%s) failed\n",
1161 len, current->pid, current->comm);
1da177e4
LT
1162 show_free_areas();
1163 return -ENOMEM;
1164}
1165
1166/*
1167 * handle mapping creation for uClinux
1168 */
1169unsigned long do_mmap_pgoff(struct file *file,
1170 unsigned long addr,
1171 unsigned long len,
1172 unsigned long prot,
1173 unsigned long flags,
1174 unsigned long pgoff)
1175{
8feae131
DH
1176 struct vm_area_struct *vma;
1177 struct vm_region *region;
1da177e4 1178 struct rb_node *rb;
8feae131 1179 unsigned long capabilities, vm_flags, result;
1da177e4
LT
1180 int ret;
1181
8feae131
DH
1182 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1183
7cd94146
EP
1184 if (!(flags & MAP_FIXED))
1185 addr = round_hint_to_min(addr);
1186
1da177e4
LT
1187 /* decide whether we should attempt the mapping, and if so what sort of
1188 * mapping */
1189 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1190 &capabilities);
8feae131
DH
1191 if (ret < 0) {
1192 kleave(" = %d [val]", ret);
1da177e4 1193 return ret;
8feae131 1194 }
1da177e4
LT
1195
1196 /* we've determined that we can make the mapping, now translate what we
1197 * now know into VMA flags */
1198 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1199
8feae131
DH
1200 /* we're going to need to record the mapping */
1201 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1202 if (!region)
1203 goto error_getting_region;
1204
1205 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1206 if (!vma)
1207 goto error_getting_vma;
1da177e4 1208
8feae131
DH
1209 atomic_set(&region->vm_usage, 1);
1210 region->vm_flags = vm_flags;
1211 region->vm_pgoff = pgoff;
1212
1213 INIT_LIST_HEAD(&vma->anon_vma_node);
1214 vma->vm_flags = vm_flags;
1215 vma->vm_pgoff = pgoff;
1da177e4 1216
8feae131
DH
1217 if (file) {
1218 region->vm_file = file;
1219 get_file(file);
1220 vma->vm_file = file;
1221 get_file(file);
1222 if (vm_flags & VM_EXECUTABLE) {
1223 added_exe_file_vma(current->mm);
1224 vma->vm_mm = current->mm;
1225 }
1226 }
1227
1228 down_write(&nommu_region_sem);
1229
1230 /* if we want to share, we need to check for regions created by other
1da177e4 1231 * mmap() calls that overlap with our proposed mapping
8feae131 1232 * - we can only share with a superset match on most regular files
1da177e4
LT
1233 * - shared mappings on character devices and memory backed files are
1234 * permitted to overlap inexactly as far as we are concerned for in
1235 * these cases, sharing is handled in the driver or filesystem rather
1236 * than here
1237 */
1238 if (vm_flags & VM_MAYSHARE) {
8feae131
DH
1239 struct vm_region *pregion;
1240 unsigned long pglen, rpglen, pgend, rpgend, start;
1da177e4 1241
8feae131
DH
1242 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1243 pgend = pgoff + pglen;
165b2392 1244
8feae131
DH
1245 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1246 pregion = rb_entry(rb, struct vm_region, vm_rb);
1da177e4 1247
8feae131 1248 if (!(pregion->vm_flags & VM_MAYSHARE))
1da177e4
LT
1249 continue;
1250
1251 /* search for overlapping mappings on the same file */
8feae131
DH
1252 if (pregion->vm_file->f_path.dentry->d_inode !=
1253 file->f_path.dentry->d_inode)
1da177e4
LT
1254 continue;
1255
8feae131 1256 if (pregion->vm_pgoff >= pgend)
1da177e4
LT
1257 continue;
1258
8feae131
DH
1259 rpglen = pregion->vm_end - pregion->vm_start;
1260 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1261 rpgend = pregion->vm_pgoff + rpglen;
1262 if (pgoff >= rpgend)
1da177e4
LT
1263 continue;
1264
8feae131
DH
1265 /* handle inexactly overlapping matches between
1266 * mappings */
1267 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1268 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1269 /* new mapping is not a subset of the region */
1da177e4
LT
1270 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1271 goto sharing_violation;
1272 continue;
1273 }
1274
8feae131
DH
1275 /* we've found a region we can share */
1276 atomic_inc(&pregion->vm_usage);
1277 vma->vm_region = pregion;
1278 start = pregion->vm_start;
1279 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1280 vma->vm_start = start;
1281 vma->vm_end = start + len;
1282
1283 if (pregion->vm_flags & VM_MAPPED_COPY) {
1284 kdebug("share copy");
1285 vma->vm_flags |= VM_MAPPED_COPY;
1286 } else {
1287 kdebug("share mmap");
1288 ret = do_mmap_shared_file(vma);
1289 if (ret < 0) {
1290 vma->vm_region = NULL;
1291 vma->vm_start = 0;
1292 vma->vm_end = 0;
1293 atomic_dec(&pregion->vm_usage);
1294 pregion = NULL;
1295 goto error_just_free;
1296 }
1297 }
1298 fput(region->vm_file);
1299 kmem_cache_free(vm_region_jar, region);
1300 region = pregion;
1301 result = start;
1302 goto share;
1da177e4
LT
1303 }
1304
1da177e4
LT
1305 /* obtain the address at which to make a shared mapping
1306 * - this is the hook for quasi-memory character devices to
1307 * tell us the location of a shared mapping
1308 */
1309 if (file && file->f_op->get_unmapped_area) {
1310 addr = file->f_op->get_unmapped_area(file, addr, len,
1311 pgoff, flags);
1312 if (IS_ERR((void *) addr)) {
1313 ret = addr;
1314 if (ret != (unsigned long) -ENOSYS)
8feae131 1315 goto error_just_free;
1da177e4
LT
1316
1317 /* the driver refused to tell us where to site
1318 * the mapping so we'll have to attempt to copy
1319 * it */
1320 ret = (unsigned long) -ENODEV;
1321 if (!(capabilities & BDI_CAP_MAP_COPY))
8feae131 1322 goto error_just_free;
1da177e4
LT
1323
1324 capabilities &= ~BDI_CAP_MAP_DIRECT;
8feae131
DH
1325 } else {
1326 vma->vm_start = region->vm_start = addr;
1327 vma->vm_end = region->vm_end = addr + len;
1da177e4
LT
1328 }
1329 }
1330 }
1331
8feae131 1332 vma->vm_region = region;
1da177e4
LT
1333
1334 /* set up the mapping */
1335 if (file && vma->vm_flags & VM_SHARED)
8feae131 1336 ret = do_mmap_shared_file(vma);
1da177e4 1337 else
8feae131 1338 ret = do_mmap_private(vma, region, len);
1da177e4 1339 if (ret < 0)
8feae131
DH
1340 goto error_put_region;
1341
1342 add_nommu_region(region);
1da177e4
LT
1343
1344 /* okay... we have a mapping; now we have to register it */
8feae131 1345 result = vma->vm_start;
1da177e4 1346
1da177e4
LT
1347 current->mm->total_vm += len >> PAGE_SHIFT;
1348
8feae131
DH
1349share:
1350 add_vma_to_mm(current->mm, vma);
1da177e4 1351
8feae131 1352 up_write(&nommu_region_sem);
1da177e4
LT
1353
1354 if (prot & PROT_EXEC)
8feae131 1355 flush_icache_range(result, result + len);
1da177e4 1356
8feae131
DH
1357 kleave(" = %lx", result);
1358 return result;
1da177e4 1359
8feae131
DH
1360error_put_region:
1361 __put_nommu_region(region);
1da177e4 1362 if (vma) {
925d1c40 1363 if (vma->vm_file) {
3fcd03e0 1364 fput(vma->vm_file);
925d1c40
MH
1365 if (vma->vm_flags & VM_EXECUTABLE)
1366 removed_exe_file_vma(vma->vm_mm);
1367 }
8feae131 1368 kmem_cache_free(vm_area_cachep, vma);
1da177e4 1369 }
8feae131 1370 kleave(" = %d [pr]", ret);
1da177e4
LT
1371 return ret;
1372
8feae131
DH
1373error_just_free:
1374 up_write(&nommu_region_sem);
1375error:
1376 fput(region->vm_file);
1377 kmem_cache_free(vm_region_jar, region);
1378 fput(vma->vm_file);
1379 if (vma->vm_flags & VM_EXECUTABLE)
1380 removed_exe_file_vma(vma->vm_mm);
1381 kmem_cache_free(vm_area_cachep, vma);
1382 kleave(" = %d", ret);
1383 return ret;
1384
1385sharing_violation:
1386 up_write(&nommu_region_sem);
1387 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1388 ret = -EINVAL;
1389 goto error;
1da177e4 1390
8feae131
DH
1391error_getting_vma:
1392 kmem_cache_free(vm_region_jar, region);
1393 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1394 " from process %d failed\n",
1da177e4
LT
1395 len, current->pid);
1396 show_free_areas();
1397 return -ENOMEM;
1398
8feae131
DH
1399error_getting_region:
1400 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1401 " from process %d failed\n",
1da177e4
LT
1402 len, current->pid);
1403 show_free_areas();
1404 return -ENOMEM;
1405}
b5073173 1406EXPORT_SYMBOL(do_mmap_pgoff);
1da177e4
LT
1407
1408/*
8feae131
DH
1409 * split a vma into two pieces at address 'addr', a new vma is allocated either
1410 * for the first part or the tail.
1da177e4 1411 */
8feae131
DH
1412int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1413 unsigned long addr, int new_below)
1da177e4 1414{
8feae131
DH
1415 struct vm_area_struct *new;
1416 struct vm_region *region;
1417 unsigned long npages;
1da177e4 1418
8feae131 1419 kenter("");
1da177e4 1420
8feae131
DH
1421 /* we're only permitted to split anonymous regions that have a single
1422 * owner */
1423 if (vma->vm_file ||
1424 atomic_read(&vma->vm_region->vm_usage) != 1)
1425 return -ENOMEM;
1da177e4 1426
8feae131
DH
1427 if (mm->map_count >= sysctl_max_map_count)
1428 return -ENOMEM;
1da177e4 1429
8feae131
DH
1430 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1431 if (!region)
1432 return -ENOMEM;
1da177e4 1433
8feae131
DH
1434 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1435 if (!new) {
1436 kmem_cache_free(vm_region_jar, region);
1437 return -ENOMEM;
1438 }
1439
1440 /* most fields are the same, copy all, and then fixup */
1441 *new = *vma;
1442 *region = *vma->vm_region;
1443 new->vm_region = region;
1444
1445 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1446
1447 if (new_below) {
dd8632a1 1448 region->vm_top = region->vm_end = new->vm_end = addr;
8feae131
DH
1449 } else {
1450 region->vm_start = new->vm_start = addr;
1451 region->vm_pgoff = new->vm_pgoff += npages;
1da177e4 1452 }
8feae131
DH
1453
1454 if (new->vm_ops && new->vm_ops->open)
1455 new->vm_ops->open(new);
1456
1457 delete_vma_from_mm(vma);
1458 down_write(&nommu_region_sem);
1459 delete_nommu_region(vma->vm_region);
1460 if (new_below) {
1461 vma->vm_region->vm_start = vma->vm_start = addr;
1462 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1463 } else {
1464 vma->vm_region->vm_end = vma->vm_end = addr;
dd8632a1 1465 vma->vm_region->vm_top = addr;
8feae131
DH
1466 }
1467 add_nommu_region(vma->vm_region);
1468 add_nommu_region(new->vm_region);
1469 up_write(&nommu_region_sem);
1470 add_vma_to_mm(mm, vma);
1471 add_vma_to_mm(mm, new);
1472 return 0;
1da177e4
LT
1473}
1474
3034097a 1475/*
8feae131
DH
1476 * shrink a VMA by removing the specified chunk from either the beginning or
1477 * the end
3034097a 1478 */
8feae131
DH
1479static int shrink_vma(struct mm_struct *mm,
1480 struct vm_area_struct *vma,
1481 unsigned long from, unsigned long to)
1da177e4 1482{
8feae131 1483 struct vm_region *region;
1da177e4 1484
8feae131 1485 kenter("");
1da177e4 1486
8feae131
DH
1487 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1488 * and list */
1489 delete_vma_from_mm(vma);
1490 if (from > vma->vm_start)
1491 vma->vm_end = from;
1492 else
1493 vma->vm_start = to;
1494 add_vma_to_mm(mm, vma);
1da177e4 1495
8feae131
DH
1496 /* cut the backing region down to size */
1497 region = vma->vm_region;
1498 BUG_ON(atomic_read(&region->vm_usage) != 1);
1499
1500 down_write(&nommu_region_sem);
1501 delete_nommu_region(region);
dd8632a1
PM
1502 if (from > region->vm_start) {
1503 to = region->vm_top;
1504 region->vm_top = region->vm_end = from;
1505 } else {
8feae131 1506 region->vm_start = to;
dd8632a1 1507 }
8feae131
DH
1508 add_nommu_region(region);
1509 up_write(&nommu_region_sem);
1510
1511 free_page_series(from, to);
1512 return 0;
1513}
1da177e4 1514
8feae131
DH
1515/*
1516 * release a mapping
1517 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1518 * VMA, though it need not cover the whole VMA
1519 */
1520int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1521{
1522 struct vm_area_struct *vma;
1523 struct rb_node *rb;
1524 unsigned long end = start + len;
1525 int ret;
1da177e4 1526
8feae131 1527 kenter(",%lx,%zx", start, len);
1da177e4 1528
8feae131
DH
1529 if (len == 0)
1530 return -EINVAL;
365e9c87 1531
8feae131
DH
1532 /* find the first potentially overlapping VMA */
1533 vma = find_vma(mm, start);
1534 if (!vma) {
33e5d769
DH
1535 static int limit = 0;
1536 if (limit < 5) {
1537 printk(KERN_WARNING
1538 "munmap of memory not mmapped by process %d"
1539 " (%s): 0x%lx-0x%lx\n",
1540 current->pid, current->comm,
1541 start, start + len - 1);
1542 limit++;
1543 }
8feae131
DH
1544 return -EINVAL;
1545 }
1da177e4 1546
8feae131
DH
1547 /* we're allowed to split an anonymous VMA but not a file-backed one */
1548 if (vma->vm_file) {
1549 do {
1550 if (start > vma->vm_start) {
1551 kleave(" = -EINVAL [miss]");
1552 return -EINVAL;
1553 }
1554 if (end == vma->vm_end)
1555 goto erase_whole_vma;
1556 rb = rb_next(&vma->vm_rb);
1557 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1558 } while (rb);
1559 kleave(" = -EINVAL [split file]");
1560 return -EINVAL;
1561 } else {
1562 /* the chunk must be a subset of the VMA found */
1563 if (start == vma->vm_start && end == vma->vm_end)
1564 goto erase_whole_vma;
1565 if (start < vma->vm_start || end > vma->vm_end) {
1566 kleave(" = -EINVAL [superset]");
1567 return -EINVAL;
1568 }
1569 if (start & ~PAGE_MASK) {
1570 kleave(" = -EINVAL [unaligned start]");
1571 return -EINVAL;
1572 }
1573 if (end != vma->vm_end && end & ~PAGE_MASK) {
1574 kleave(" = -EINVAL [unaligned split]");
1575 return -EINVAL;
1576 }
1577 if (start != vma->vm_start && end != vma->vm_end) {
1578 ret = split_vma(mm, vma, start, 1);
1579 if (ret < 0) {
1580 kleave(" = %d [split]", ret);
1581 return ret;
1582 }
1583 }
1584 return shrink_vma(mm, vma, start, end);
1585 }
1da177e4 1586
8feae131
DH
1587erase_whole_vma:
1588 delete_vma_from_mm(vma);
1589 delete_vma(mm, vma);
1590 kleave(" = 0");
1da177e4
LT
1591 return 0;
1592}
b5073173 1593EXPORT_SYMBOL(do_munmap);
1da177e4 1594
6a6160a7 1595SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
3034097a
DH
1596{
1597 int ret;
1598 struct mm_struct *mm = current->mm;
1599
1600 down_write(&mm->mmap_sem);
1601 ret = do_munmap(mm, addr, len);
1602 up_write(&mm->mmap_sem);
1603 return ret;
1604}
1605
1606/*
8feae131 1607 * release all the mappings made in a process's VM space
3034097a 1608 */
8feae131 1609void exit_mmap(struct mm_struct *mm)
1da177e4 1610{
8feae131 1611 struct vm_area_struct *vma;
1da177e4 1612
8feae131
DH
1613 if (!mm)
1614 return;
1da177e4 1615
8feae131 1616 kenter("");
1da177e4 1617
8feae131 1618 mm->total_vm = 0;
1da177e4 1619
8feae131
DH
1620 while ((vma = mm->mmap)) {
1621 mm->mmap = vma->vm_next;
1622 delete_vma_from_mm(vma);
1623 delete_vma(mm, vma);
1da177e4 1624 }
8feae131
DH
1625
1626 kleave("");
1da177e4
LT
1627}
1628
1da177e4
LT
1629unsigned long do_brk(unsigned long addr, unsigned long len)
1630{
1631 return -ENOMEM;
1632}
1633
1634/*
6fa5f80b
DH
1635 * expand (or shrink) an existing mapping, potentially moving it at the same
1636 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1da177e4 1637 *
6fa5f80b 1638 * under NOMMU conditions, we only permit changing a mapping's size, and only
8feae131
DH
1639 * as long as it stays within the region allocated by do_mmap_private() and the
1640 * block is not shareable
1da177e4 1641 *
6fa5f80b 1642 * MREMAP_FIXED is not supported under NOMMU conditions
1da177e4
LT
1643 */
1644unsigned long do_mremap(unsigned long addr,
1645 unsigned long old_len, unsigned long new_len,
1646 unsigned long flags, unsigned long new_addr)
1647{
6fa5f80b 1648 struct vm_area_struct *vma;
1da177e4
LT
1649
1650 /* insanity checks first */
8feae131 1651 if (old_len == 0 || new_len == 0)
1da177e4
LT
1652 return (unsigned long) -EINVAL;
1653
8feae131
DH
1654 if (addr & ~PAGE_MASK)
1655 return -EINVAL;
1656
1da177e4
LT
1657 if (flags & MREMAP_FIXED && new_addr != addr)
1658 return (unsigned long) -EINVAL;
1659
8feae131 1660 vma = find_vma_exact(current->mm, addr, old_len);
6fa5f80b
DH
1661 if (!vma)
1662 return (unsigned long) -EINVAL;
1da177e4 1663
6fa5f80b 1664 if (vma->vm_end != vma->vm_start + old_len)
1da177e4
LT
1665 return (unsigned long) -EFAULT;
1666
6fa5f80b 1667 if (vma->vm_flags & VM_MAYSHARE)
1da177e4
LT
1668 return (unsigned long) -EPERM;
1669
8feae131 1670 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1da177e4
LT
1671 return (unsigned long) -ENOMEM;
1672
1673 /* all checks complete - do it */
6fa5f80b 1674 vma->vm_end = vma->vm_start + new_len;
6fa5f80b
DH
1675 return vma->vm_start;
1676}
b5073173 1677EXPORT_SYMBOL(do_mremap);
6fa5f80b 1678
6a6160a7
HC
1679SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1680 unsigned long, new_len, unsigned long, flags,
1681 unsigned long, new_addr)
6fa5f80b
DH
1682{
1683 unsigned long ret;
1684
1685 down_write(&current->mm->mmap_sem);
1686 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1687 up_write(&current->mm->mmap_sem);
1688 return ret;
1da177e4
LT
1689}
1690
6aab341e 1691struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
deceb6cd 1692 unsigned int foll_flags)
1da177e4
LT
1693{
1694 return NULL;
1695}
1696
1da177e4
LT
1697int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1698 unsigned long to, unsigned long size, pgprot_t prot)
1699{
66aa2b4b
GU
1700 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1701 return 0;
1da177e4 1702}
22c4af40 1703EXPORT_SYMBOL(remap_pfn_range);
1da177e4 1704
f905bc44
PM
1705int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1706 unsigned long pgoff)
1707{
1708 unsigned int size = vma->vm_end - vma->vm_start;
1709
1710 if (!(vma->vm_flags & VM_USERMAP))
1711 return -EINVAL;
1712
1713 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1714 vma->vm_end = vma->vm_start + size;
1715
1716 return 0;
1717}
1718EXPORT_SYMBOL(remap_vmalloc_range);
1719
1da177e4
LT
1720void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1721{
1722}
1723
1724unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1725 unsigned long len, unsigned long pgoff, unsigned long flags)
1726{
1727 return -ENOMEM;
1728}
1729
1363c3cd 1730void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1da177e4
LT
1731{
1732}
1733
1da177e4
LT
1734void unmap_mapping_range(struct address_space *mapping,
1735 loff_t const holebegin, loff_t const holelen,
1736 int even_cows)
1737{
1738}
22c4af40 1739EXPORT_SYMBOL(unmap_mapping_range);
1da177e4 1740
d56e03cd
DH
1741/*
1742 * ask for an unmapped area at which to create a mapping on a file
1743 */
1744unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1745 unsigned long len, unsigned long pgoff,
1746 unsigned long flags)
1747{
1748 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1749 unsigned long, unsigned long);
1750
1751 get_area = current->mm->get_unmapped_area;
1752 if (file && file->f_op && file->f_op->get_unmapped_area)
1753 get_area = file->f_op->get_unmapped_area;
1754
1755 if (!get_area)
1756 return -ENOSYS;
1757
1758 return get_area(file, addr, len, pgoff, flags);
1759}
d56e03cd
DH
1760EXPORT_SYMBOL(get_unmapped_area);
1761
1da177e4
LT
1762/*
1763 * Check that a process has enough memory to allocate a new virtual
1764 * mapping. 0 means there is enough memory for the allocation to
1765 * succeed and -ENOMEM implies there is not.
1766 *
1767 * We currently support three overcommit policies, which are set via the
1768 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1769 *
1770 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1771 * Additional code 2002 Jul 20 by Robert Love.
1772 *
1773 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1774 *
1775 * Note this is a helper function intended to be used by LSMs which
1776 * wish to use this logic.
1777 */
34b4e4aa 1778int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1da177e4
LT
1779{
1780 unsigned long free, allowed;
1781
1782 vm_acct_memory(pages);
1783
1784 /*
1785 * Sometimes we want to use more memory than we have
1786 */
1787 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1788 return 0;
1789
1790 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1791 unsigned long n;
1792
347ce434 1793 free = global_page_state(NR_FILE_PAGES);
1da177e4
LT
1794 free += nr_swap_pages;
1795
1796 /*
1797 * Any slabs which are created with the
1798 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1799 * which are reclaimable, under pressure. The dentry
1800 * cache and most inode caches should fall into this
1801 */
972d1a7b 1802 free += global_page_state(NR_SLAB_RECLAIMABLE);
1da177e4
LT
1803
1804 /*
1805 * Leave the last 3% for root
1806 */
1807 if (!cap_sys_admin)
1808 free -= free / 32;
1809
1810 if (free > pages)
1811 return 0;
1812
1813 /*
1814 * nr_free_pages() is very expensive on large systems,
1815 * only call if we're about to fail.
1816 */
1817 n = nr_free_pages();
d5ddc79b
HA
1818
1819 /*
1820 * Leave reserved pages. The pages are not for anonymous pages.
1821 */
1822 if (n <= totalreserve_pages)
1823 goto error;
1824 else
1825 n -= totalreserve_pages;
1826
1827 /*
1828 * Leave the last 3% for root
1829 */
1da177e4
LT
1830 if (!cap_sys_admin)
1831 n -= n / 32;
1832 free += n;
1833
1834 if (free > pages)
1835 return 0;
d5ddc79b
HA
1836
1837 goto error;
1da177e4
LT
1838 }
1839
1840 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1841 /*
1842 * Leave the last 3% for root
1843 */
1844 if (!cap_sys_admin)
1845 allowed -= allowed / 32;
1846 allowed += total_swap_pages;
1847
1848 /* Don't let a single process grow too big:
1849 leave 3% of the size of this process for other processes */
731572d3
AC
1850 if (mm)
1851 allowed -= mm->total_vm / 32;
1da177e4 1852
00a62ce9 1853 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1da177e4 1854 return 0;
00a62ce9 1855
d5ddc79b 1856error:
1da177e4
LT
1857 vm_unacct_memory(pages);
1858
1859 return -ENOMEM;
1860}
1861
1862int in_gate_area_no_task(unsigned long addr)
1863{
1864 return 0;
1865}
b0e15190 1866
d0217ac0 1867int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
b0e15190
DH
1868{
1869 BUG();
d0217ac0 1870 return 0;
b0e15190 1871}
b5073173 1872EXPORT_SYMBOL(filemap_fault);
0ec76a11
DH
1873
1874/*
1875 * Access another process' address space.
1876 * - source/target buffer must be kernel space
1877 */
1878int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1879{
0ec76a11
DH
1880 struct vm_area_struct *vma;
1881 struct mm_struct *mm;
1882
1883 if (addr + len < addr)
1884 return 0;
1885
1886 mm = get_task_mm(tsk);
1887 if (!mm)
1888 return 0;
1889
1890 down_read(&mm->mmap_sem);
1891
1892 /* the access must start within one of the target process's mappings */
0159b141
DH
1893 vma = find_vma(mm, addr);
1894 if (vma) {
0ec76a11
DH
1895 /* don't overrun this mapping */
1896 if (addr + len >= vma->vm_end)
1897 len = vma->vm_end - addr;
1898
1899 /* only read or write mappings where it is permitted */
d00c7b99 1900 if (write && vma->vm_flags & VM_MAYWRITE)
0ec76a11 1901 len -= copy_to_user((void *) addr, buf, len);
d00c7b99 1902 else if (!write && vma->vm_flags & VM_MAYREAD)
0ec76a11
DH
1903 len -= copy_from_user(buf, (void *) addr, len);
1904 else
1905 len = 0;
1906 } else {
1907 len = 0;
1908 }
1909
1910 up_read(&mm->mmap_sem);
1911 mmput(mm);
1912 return len;
1913}