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