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