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