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