This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / mm / mmap.c
1 /*
2  * mm/mmap.c
3  *
4  * Written by obz.
5  *
6  * Address space accounting code        <alan@redhat.com>
7  */
8
9 #include <linux/slab.h>
10 #include <linux/mm.h>
11 #include <linux/shm.h>
12 #include <linux/mman.h>
13 #include <linux/pagemap.h>
14 #include <linux/swap.h>
15 #include <linux/syscalls.h>
16 #include <linux/init.h>
17 #include <linux/file.h>
18 #include <linux/fs.h>
19 #include <linux/personality.h>
20 #include <linux/security.h>
21 #include <linux/hugetlb.h>
22 #include <linux/profile.h>
23 #include <linux/module.h>
24 #include <linux/mount.h>
25 #include <linux/mempolicy.h>
26 #include <linux/rmap.h>
27 #include <linux/random.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/cacheflush.h>
31 #include <asm/tlb.h>
32
33 static void unmap_region(struct mm_struct *mm,
34                 struct vm_area_struct *vma, struct vm_area_struct *prev,
35                 unsigned long start, unsigned long end);
36
37 /*
38  * WARNING: the debugging will use recursive algorithms so never enable this
39  * unless you know what you are doing.
40  */
41 #undef DEBUG_MM_RB
42
43 /* description of effects of mapping type and prot in current implementation.
44  * this is due to the limited x86 page protection hardware.  The expected
45  * behavior is in parens:
46  *
47  * map_type     prot
48  *              PROT_NONE       PROT_READ       PROT_WRITE      PROT_EXEC
49  * MAP_SHARED   r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
50  *              w: (no) no      w: (no) no      w: (yes) yes    w: (no) no
51  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
52  *              
53  * MAP_PRIVATE  r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
54  *              w: (no) no      w: (no) no      w: (copy) copy  w: (no) no
55  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
56  *
57  */
58 pgprot_t protection_map[16] = {
59         __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
60         __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
61 };
62
63 int sysctl_overcommit_memory = OVERCOMMIT_GUESS;  /* heuristic overcommit */
64 int sysctl_overcommit_ratio = 50;       /* default is 50% */
65 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
66 atomic_t vm_committed_space = ATOMIC_INIT(0);
67
68 /*
69  * Check that a process has enough memory to allocate a new virtual
70  * mapping. 0 means there is enough memory for the allocation to
71  * succeed and -ENOMEM implies there is not.
72  *
73  * We currently support three overcommit policies, which are set via the
74  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
75  *
76  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
77  * Additional code 2002 Jul 20 by Robert Love.
78  *
79  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
80  *
81  * Note this is a helper function intended to be used by LSMs which
82  * wish to use this logic.
83  */
84 int __vm_enough_memory(long pages, int cap_sys_admin)
85 {
86         unsigned long free, allowed;
87
88         vm_acct_memory(pages);
89
90         /*
91          * Sometimes we want to use more memory than we have
92          */
93         if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
94                 return 0;
95
96         if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
97                 unsigned long n;
98
99                 free = get_page_cache_size();
100                 free += nr_swap_pages;
101
102                 /*
103                  * Any slabs which are created with the
104                  * SLAB_RECLAIM_ACCOUNT flag claim to have contents
105                  * which are reclaimable, under pressure.  The dentry
106                  * cache and most inode caches should fall into this
107                  */
108                 free += atomic_read(&slab_reclaim_pages);
109
110                 /*
111                  * Leave the last 3% for root
112                  */
113                 if (!cap_sys_admin)
114                         free -= free / 32;
115
116                 if (free > pages)
117                         return 0;
118
119                 /*
120                  * nr_free_pages() is very expensive on large systems,
121                  * only call if we're about to fail.
122                  */
123                 n = nr_free_pages();
124                 if (!cap_sys_admin)
125                         n -= n / 32;
126                 free += n;
127
128                 if (free > pages)
129                         return 0;
130                 vm_unacct_memory(pages);
131                 return -ENOMEM;
132         }
133
134         allowed = (totalram_pages - hugetlb_total_pages())
135                 * sysctl_overcommit_ratio / 100;
136         /*
137          * Leave the last 3% for root
138          */
139         if (!cap_sys_admin)
140                 allowed -= allowed / 32;
141         allowed += total_swap_pages;
142
143         /* Don't let a single process grow too big:
144            leave 3% of the size of this process for other processes */
145         allowed -= current->mm->total_vm / 32;
146
147         if (atomic_read(&vm_committed_space) < allowed)
148                 return 0;
149
150         vm_unacct_memory(pages);
151
152         return -ENOMEM;
153 }
154
155 EXPORT_SYMBOL(sysctl_overcommit_memory);
156 EXPORT_SYMBOL(sysctl_overcommit_ratio);
157 EXPORT_SYMBOL(sysctl_max_map_count);
158 EXPORT_SYMBOL(vm_committed_space);
159 EXPORT_SYMBOL(__vm_enough_memory);
160
161 /*
162  * Requires inode->i_mapping->i_mmap_lock
163  */
164 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
165                 struct file *file, struct address_space *mapping)
166 {
167         if (vma->vm_flags & VM_DENYWRITE)
168                 atomic_inc(&file->f_dentry->d_inode->i_writecount);
169         if (vma->vm_flags & VM_SHARED)
170                 mapping->i_mmap_writable--;
171
172         flush_dcache_mmap_lock(mapping);
173         if (unlikely(vma->vm_flags & VM_NONLINEAR))
174                 list_del_init(&vma->shared.vm_set.list);
175         else
176                 vma_prio_tree_remove(vma, &mapping->i_mmap);
177         flush_dcache_mmap_unlock(mapping);
178 }
179
180 /*
181  * Remove one vm structure and free it.
182  */
183 static void remove_vm_struct(struct vm_area_struct *vma)
184 {
185         struct file *file = vma->vm_file;
186
187         might_sleep();
188         if (file) {
189                 struct address_space *mapping = file->f_mapping;
190                 spin_lock(&mapping->i_mmap_lock);
191                 __remove_shared_vm_struct(vma, file, mapping);
192                 spin_unlock(&mapping->i_mmap_lock);
193         }
194         if (vma->vm_ops && vma->vm_ops->close)
195                 vma->vm_ops->close(vma);
196         if (file)
197                 fput(file);
198         anon_vma_unlink(vma);
199         mpol_free(vma_policy(vma));
200         kmem_cache_free(vm_area_cachep, vma);
201 }
202
203 /*
204  *  sys_brk() for the most part doesn't need the global kernel
205  *  lock, except when an application is doing something nasty
206  *  like trying to un-brk an area that has already been mapped
207  *  to a regular file.  in this case, the unmapping will need
208  *  to invoke file system routines that need the global lock.
209  */
210 asmlinkage unsigned long sys_brk(unsigned long brk)
211 {
212         unsigned long rlim, retval;
213         unsigned long newbrk, oldbrk;
214         struct mm_struct *mm = current->mm;
215
216         down_write(&mm->mmap_sem);
217
218         if (brk < mm->end_code)
219                 goto out;
220         newbrk = PAGE_ALIGN(brk);
221         oldbrk = PAGE_ALIGN(mm->brk);
222         if (oldbrk == newbrk)
223                 goto set_brk;
224
225         /* Always allow shrinking brk. */
226         if (brk <= mm->brk) {
227                 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
228                         goto set_brk;
229                 goto out;
230         }
231
232         /* Check against rlimit.. */
233         rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
234         if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
235                 goto out;
236
237         /* Check against existing mmap mappings. */
238         if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
239                 goto out;
240
241         /* Ok, looks good - let it rip. */
242         if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
243                 goto out;
244 set_brk:
245         mm->brk = brk;
246 out:
247         retval = mm->brk;
248         up_write(&mm->mmap_sem);
249         return retval;
250 }
251
252 #ifdef DEBUG_MM_RB
253 static int browse_rb(struct rb_root *root)
254 {
255         int i = 0, j;
256         struct rb_node *nd, *pn = NULL;
257         unsigned long prev = 0, pend = 0;
258
259         for (nd = rb_first(root); nd; nd = rb_next(nd)) {
260                 struct vm_area_struct *vma;
261                 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
262                 if (vma->vm_start < prev)
263                         printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
264                 if (vma->vm_start < pend)
265                         printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
266                 if (vma->vm_start > vma->vm_end)
267                         printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
268                 i++;
269                 pn = nd;
270         }
271         j = 0;
272         for (nd = pn; nd; nd = rb_prev(nd)) {
273                 j++;
274         }
275         if (i != j)
276                 printk("backwards %d, forwards %d\n", j, i), i = 0;
277         return i;
278 }
279
280 void validate_mm(struct mm_struct *mm)
281 {
282         int bug = 0;
283         int i = 0;
284         struct vm_area_struct *tmp = mm->mmap;
285         while (tmp) {
286                 tmp = tmp->vm_next;
287                 i++;
288         }
289         if (i != mm->map_count)
290                 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
291         i = browse_rb(&mm->mm_rb);
292         if (i != mm->map_count)
293                 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
294         if (bug)
295                 BUG();
296 }
297 #else
298 #define validate_mm(mm) do { } while (0)
299 #endif
300
301 static struct vm_area_struct *
302 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
303                 struct vm_area_struct **pprev, struct rb_node ***rb_link,
304                 struct rb_node ** rb_parent)
305 {
306         struct vm_area_struct * vma;
307         struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
308
309         __rb_link = &mm->mm_rb.rb_node;
310         rb_prev = __rb_parent = NULL;
311         vma = NULL;
312
313         while (*__rb_link) {
314                 struct vm_area_struct *vma_tmp;
315
316                 __rb_parent = *__rb_link;
317                 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
318
319                 if (vma_tmp->vm_end > addr) {
320                         vma = vma_tmp;
321                         if (vma_tmp->vm_start <= addr)
322                                 return vma;
323                         __rb_link = &__rb_parent->rb_left;
324                 } else {
325                         rb_prev = __rb_parent;
326                         __rb_link = &__rb_parent->rb_right;
327                 }
328         }
329
330         *pprev = NULL;
331         if (rb_prev)
332                 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
333         *rb_link = __rb_link;
334         *rb_parent = __rb_parent;
335         return vma;
336 }
337
338 static inline void
339 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
340                 struct vm_area_struct *prev, struct rb_node *rb_parent)
341 {
342         if (vma->vm_flags & VM_EXEC)
343                 arch_add_exec_range(mm, vma->vm_end);
344         if (prev) {
345                 vma->vm_next = prev->vm_next;
346                 prev->vm_next = vma;
347         } else {
348                 mm->mmap = vma;
349                 if (rb_parent)
350                         vma->vm_next = rb_entry(rb_parent,
351                                         struct vm_area_struct, vm_rb);
352                 else
353                         vma->vm_next = NULL;
354         }
355 }
356
357 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
358                 struct rb_node **rb_link, struct rb_node *rb_parent)
359 {
360         rb_link_node(&vma->vm_rb, rb_parent, rb_link);
361         rb_insert_color(&vma->vm_rb, &mm->mm_rb);
362 }
363
364 static inline void __vma_link_file(struct vm_area_struct *vma)
365 {
366         struct file * file;
367
368         file = vma->vm_file;
369         if (file) {
370                 struct address_space *mapping = file->f_mapping;
371
372                 if (vma->vm_flags & VM_DENYWRITE)
373                         atomic_dec(&file->f_dentry->d_inode->i_writecount);
374                 if (vma->vm_flags & VM_SHARED)
375                         mapping->i_mmap_writable++;
376
377                 flush_dcache_mmap_lock(mapping);
378                 if (unlikely(vma->vm_flags & VM_NONLINEAR))
379                         vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
380                 else
381                         vma_prio_tree_insert(vma, &mapping->i_mmap);
382                 flush_dcache_mmap_unlock(mapping);
383         }
384 }
385
386 static void
387 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
388         struct vm_area_struct *prev, struct rb_node **rb_link,
389         struct rb_node *rb_parent)
390 {
391         __vma_link_list(mm, vma, prev, rb_parent);
392         __vma_link_rb(mm, vma, rb_link, rb_parent);
393         __anon_vma_link(vma);
394 }
395
396 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
397                         struct vm_area_struct *prev, struct rb_node **rb_link,
398                         struct rb_node *rb_parent)
399 {
400         struct address_space *mapping = NULL;
401
402         if (vma->vm_file)
403                 mapping = vma->vm_file->f_mapping;
404
405         if (mapping) {
406                 spin_lock(&mapping->i_mmap_lock);
407                 vma->vm_truncate_count = mapping->truncate_count;
408         }
409         anon_vma_lock(vma);
410
411         __vma_link(mm, vma, prev, rb_link, rb_parent);
412         __vma_link_file(vma);
413
414         anon_vma_unlock(vma);
415         if (mapping)
416                 spin_unlock(&mapping->i_mmap_lock);
417
418         mm->map_count++;
419         validate_mm(mm);
420 }
421
422 /*
423  * Helper for vma_adjust in the split_vma insert case:
424  * insert vm structure into list and rbtree and anon_vma,
425  * but it has already been inserted into prio_tree earlier.
426  */
427 static void
428 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
429 {
430         struct vm_area_struct * __vma, * prev;
431         struct rb_node ** rb_link, * rb_parent;
432
433         __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
434         if (__vma && __vma->vm_start < vma->vm_end)
435                 BUG();
436         __vma_link(mm, vma, prev, rb_link, rb_parent);
437         mm->map_count++;
438 }
439
440 static inline void
441 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
442                 struct vm_area_struct *prev)
443 {
444         prev->vm_next = vma->vm_next;
445         rb_erase(&vma->vm_rb, &mm->mm_rb);
446         if (mm->mmap_cache == vma)
447                 mm->mmap_cache = prev;
448         if (vma->vm_flags & VM_EXEC)
449                 arch_remove_exec_range(mm, vma->vm_end);
450 }
451
452 /*
453  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
454  * is already present in an i_mmap tree without adjusting the tree.
455  * The following helper function should be used when such adjustments
456  * are necessary.  The "insert" vma (if any) is to be inserted
457  * before we drop the necessary locks.
458  */
459 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
460         unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
461 {
462         struct mm_struct *mm = vma->vm_mm;
463         struct vm_area_struct *next = vma->vm_next;
464         struct vm_area_struct *importer = NULL;
465         struct address_space *mapping = NULL;
466         struct prio_tree_root *root = NULL;
467         struct file *file = vma->vm_file;
468         struct anon_vma *anon_vma = NULL;
469         long adjust_next = 0;
470         int remove_next = 0;
471
472         if (next && !insert) {
473                 if (end >= next->vm_end) {
474                         /*
475                          * vma expands, overlapping all the next, and
476                          * perhaps the one after too (mprotect case 6).
477                          */
478 again:                  remove_next = 1 + (end > next->vm_end);
479                         end = next->vm_end;
480                         anon_vma = next->anon_vma;
481                         importer = vma;
482                 } else if (end > next->vm_start) {
483                         /*
484                          * vma expands, overlapping part of the next:
485                          * mprotect case 5 shifting the boundary up.
486                          */
487                         adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
488                         anon_vma = next->anon_vma;
489                         importer = vma;
490                 } else if (end < vma->vm_end) {
491                         /*
492                          * vma shrinks, and !insert tells it's not
493                          * split_vma inserting another: so it must be
494                          * mprotect case 4 shifting the boundary down.
495                          */
496                         adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
497                         anon_vma = next->anon_vma;
498                         importer = next;
499                 }
500         }
501
502         if (file) {
503                 mapping = file->f_mapping;
504                 if (!(vma->vm_flags & VM_NONLINEAR))
505                         root = &mapping->i_mmap;
506                 spin_lock(&mapping->i_mmap_lock);
507                 if (importer &&
508                     vma->vm_truncate_count != next->vm_truncate_count) {
509                         /*
510                          * unmap_mapping_range might be in progress:
511                          * ensure that the expanding vma is rescanned.
512                          */
513                         importer->vm_truncate_count = 0;
514                 }
515                 if (insert) {
516                         insert->vm_truncate_count = vma->vm_truncate_count;
517                         /*
518                          * Put into prio_tree now, so instantiated pages
519                          * are visible to arm/parisc __flush_dcache_page
520                          * throughout; but we cannot insert into address
521                          * space until vma start or end is updated.
522                          */
523                         __vma_link_file(insert);
524                 }
525         }
526
527         /*
528          * When changing only vma->vm_end, we don't really need
529          * anon_vma lock: but is that case worth optimizing out?
530          */
531         if (vma->anon_vma)
532                 anon_vma = vma->anon_vma;
533         if (anon_vma) {
534                 spin_lock(&anon_vma->lock);
535                 /*
536                  * Easily overlooked: when mprotect shifts the boundary,
537                  * make sure the expanding vma has anon_vma set if the
538                  * shrinking vma had, to cover any anon pages imported.
539                  */
540                 if (importer && !importer->anon_vma) {
541                         importer->anon_vma = anon_vma;
542                         __anon_vma_link(importer);
543                 }
544         }
545
546         if (root) {
547                 flush_dcache_mmap_lock(mapping);
548                 vma_prio_tree_remove(vma, root);
549                 if (adjust_next)
550                         vma_prio_tree_remove(next, root);
551         }
552
553         vma->vm_start = start;
554         vma->vm_end = end;
555         vma->vm_pgoff = pgoff;
556         if (adjust_next) {
557                 next->vm_start += adjust_next << PAGE_SHIFT;
558                 next->vm_pgoff += adjust_next;
559         }
560
561         if (root) {
562                 if (adjust_next)
563                         vma_prio_tree_insert(next, root);
564                 vma_prio_tree_insert(vma, root);
565                 flush_dcache_mmap_unlock(mapping);
566         }
567
568         if (remove_next) {
569                 /*
570                  * vma_merge has merged next into vma, and needs
571                  * us to remove next before dropping the locks.
572                  */
573                 __vma_unlink(mm, next, vma);
574                 if (file)
575                         __remove_shared_vm_struct(next, file, mapping);
576                 if (next->anon_vma)
577                         __anon_vma_merge(vma, next);
578         } else if (insert) {
579                 /*
580                  * split_vma has split insert from vma, and needs
581                  * us to insert it before dropping the locks
582                  * (it may either follow vma or precede it).
583                  */
584                 __insert_vm_struct(mm, insert);
585         }
586
587         if (anon_vma)
588                 spin_unlock(&anon_vma->lock);
589         if (mapping)
590                 spin_unlock(&mapping->i_mmap_lock);
591
592         if (remove_next) {
593                 if (file)
594                         fput(file);
595                 mm->map_count--;
596                 mpol_free(vma_policy(next));
597                 kmem_cache_free(vm_area_cachep, next);
598                 /*
599                  * In mprotect's case 6 (see comments on vma_merge),
600                  * we must remove another next too. It would clutter
601                  * up the code too much to do both in one go.
602                  */
603                 if (remove_next == 2) {
604                         next = vma->vm_next;
605                         goto again;
606                 }
607         }
608
609         validate_mm(mm);
610 }
611
612 /*
613  * If the vma has a ->close operation then the driver probably needs to release
614  * per-vma resources, so we don't attempt to merge those.
615  */
616 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
617
618 static inline int is_mergeable_vma(struct vm_area_struct *vma,
619                         struct file *file, unsigned long vm_flags)
620 {
621         if (vma->vm_flags != vm_flags)
622                 return 0;
623         if (vma->vm_file != file)
624                 return 0;
625         if (vma->vm_ops && vma->vm_ops->close)
626                 return 0;
627         return 1;
628 }
629
630 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
631                                         struct anon_vma *anon_vma2)
632 {
633         return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
634 }
635
636 /*
637  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
638  * in front of (at a lower virtual address and file offset than) the vma.
639  *
640  * We cannot merge two vmas if they have differently assigned (non-NULL)
641  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
642  *
643  * We don't check here for the merged mmap wrapping around the end of pagecache
644  * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
645  * wrap, nor mmaps which cover the final page at index -1UL.
646  */
647 static int
648 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
649         struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
650 {
651         if (is_mergeable_vma(vma, file, vm_flags) &&
652             is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
653                 if (vma->vm_pgoff == vm_pgoff)
654                         return 1;
655         }
656         return 0;
657 }
658
659 /*
660  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
661  * beyond (at a higher virtual address and file offset than) the vma.
662  *
663  * We cannot merge two vmas if they have differently assigned (non-NULL)
664  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
665  */
666 static int
667 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
668         struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
669 {
670         if (is_mergeable_vma(vma, file, vm_flags) &&
671             is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
672                 pgoff_t vm_pglen;
673                 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
674                 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
675                         return 1;
676         }
677         return 0;
678 }
679
680 /*
681  * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
682  * whether that can be merged with its predecessor or its successor.
683  * Or both (it neatly fills a hole).
684  *
685  * In most cases - when called for mmap, brk or mremap - [addr,end) is
686  * certain not to be mapped by the time vma_merge is called; but when
687  * called for mprotect, it is certain to be already mapped (either at
688  * an offset within prev, or at the start of next), and the flags of
689  * this area are about to be changed to vm_flags - and the no-change
690  * case has already been eliminated.
691  *
692  * The following mprotect cases have to be considered, where AAAA is
693  * the area passed down from mprotect_fixup, never extending beyond one
694  * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
695  *
696  *     AAAA             AAAA                AAAA          AAAA
697  *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
698  *    cannot merge    might become    might become    might become
699  *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
700  *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
701  *    mremap move:                                    PPPPNNNNNNNN 8
702  *        AAAA
703  *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
704  *    might become    case 1 below    case 2 below    case 3 below
705  *
706  * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
707  * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
708  */
709 struct vm_area_struct *vma_merge(struct mm_struct *mm,
710                         struct vm_area_struct *prev, unsigned long addr,
711                         unsigned long end, unsigned long vm_flags,
712                         struct anon_vma *anon_vma, struct file *file,
713                         pgoff_t pgoff, struct mempolicy *policy)
714 {
715         pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
716         struct vm_area_struct *area, *next;
717
718         /*
719          * We later require that vma->vm_flags == vm_flags,
720          * so this tests vma->vm_flags & VM_SPECIAL, too.
721          */
722         if (vm_flags & VM_SPECIAL)
723                 return NULL;
724
725         if (prev)
726                 next = prev->vm_next;
727         else
728                 next = mm->mmap;
729         area = next;
730         if (next && next->vm_end == end)                /* cases 6, 7, 8 */
731                 next = next->vm_next;
732
733         /*
734          * Can it merge with the predecessor?
735          */
736         if (prev && prev->vm_end == addr &&
737                         mpol_equal(vma_policy(prev), policy) &&
738                         can_vma_merge_after(prev, vm_flags,
739                                                 anon_vma, file, pgoff)) {
740                 /*
741                  * OK, it can.  Can we now merge in the successor as well?
742                  */
743                 if (next && end == next->vm_start &&
744                                 mpol_equal(policy, vma_policy(next)) &&
745                                 can_vma_merge_before(next, vm_flags,
746                                         anon_vma, file, pgoff+pglen) &&
747                                 is_mergeable_anon_vma(prev->anon_vma,
748                                                       next->anon_vma)) {
749                                                         /* cases 1, 6 */
750                         vma_adjust(prev, prev->vm_start,
751                                 next->vm_end, prev->vm_pgoff, NULL);
752                 } else                                  /* cases 2, 5, 7 */
753                         vma_adjust(prev, prev->vm_start,
754                                 end, prev->vm_pgoff, NULL);
755                 if (prev->vm_flags & VM_EXEC)
756                         arch_add_exec_range(mm, prev->vm_end);
757                 return prev;
758         }
759
760         /*
761          * Can this new request be merged in front of next?
762          */
763         if (next && end == next->vm_start &&
764                         mpol_equal(policy, vma_policy(next)) &&
765                         can_vma_merge_before(next, vm_flags,
766                                         anon_vma, file, pgoff+pglen)) {
767                 if (prev && addr < prev->vm_end)        /* case 4 */
768                         vma_adjust(prev, prev->vm_start,
769                                 addr, prev->vm_pgoff, NULL);
770                 else                                    /* cases 3, 8 */
771                         vma_adjust(area, addr, next->vm_end,
772                                 next->vm_pgoff - pglen, NULL);
773                 return area;
774         }
775
776         return NULL;
777 }
778
779 /*
780  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
781  * neighbouring vmas for a suitable anon_vma, before it goes off
782  * to allocate a new anon_vma.  It checks because a repetitive
783  * sequence of mprotects and faults may otherwise lead to distinct
784  * anon_vmas being allocated, preventing vma merge in subsequent
785  * mprotect.
786  */
787 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
788 {
789         struct vm_area_struct *near;
790         unsigned long vm_flags;
791
792         near = vma->vm_next;
793         if (!near)
794                 goto try_prev;
795
796         /*
797          * Since only mprotect tries to remerge vmas, match flags
798          * which might be mprotected into each other later on.
799          * Neither mlock nor madvise tries to remerge at present,
800          * so leave their flags as obstructing a merge.
801          */
802         vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
803         vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
804
805         if (near->anon_vma && vma->vm_end == near->vm_start &&
806                         mpol_equal(vma_policy(vma), vma_policy(near)) &&
807                         can_vma_merge_before(near, vm_flags,
808                                 NULL, vma->vm_file, vma->vm_pgoff +
809                                 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
810                 return near->anon_vma;
811 try_prev:
812         /*
813          * It is potentially slow to have to call find_vma_prev here.
814          * But it's only on the first write fault on the vma, not
815          * every time, and we could devise a way to avoid it later
816          * (e.g. stash info in next's anon_vma_node when assigning
817          * an anon_vma, or when trying vma_merge).  Another time.
818          */
819         if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
820                 BUG();
821         if (!near)
822                 goto none;
823
824         vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
825         vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
826
827         if (near->anon_vma && near->vm_end == vma->vm_start &&
828                         mpol_equal(vma_policy(near), vma_policy(vma)) &&
829                         can_vma_merge_after(near, vm_flags,
830                                 NULL, vma->vm_file, vma->vm_pgoff))
831                 return near->anon_vma;
832 none:
833         /*
834          * There's no absolute need to look only at touching neighbours:
835          * we could search further afield for "compatible" anon_vmas.
836          * But it would probably just be a waste of time searching,
837          * or lead to too many vmas hanging off the same anon_vma.
838          * We're trying to allow mprotect remerging later on,
839          * not trying to minimize memory used for anon_vmas.
840          */
841         return NULL;
842 }
843
844 #ifdef CONFIG_PROC_FS
845 void __vm_stat_account(struct mm_struct *mm, unsigned long flags,
846                                                 struct file *file, long pages)
847 {
848         const unsigned long stack_flags
849                 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
850
851 #ifdef CONFIG_HUGETLB
852         if (flags & VM_HUGETLB) {
853                 if (!(flags & VM_DONTCOPY))
854                         mm->shared_vm += pages;
855                 return;
856         }
857 #endif /* CONFIG_HUGETLB */
858
859         if (file) {
860                 mm->shared_vm += pages;
861                 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
862                         mm->exec_vm += pages;
863         } else if (flags & stack_flags)
864                 mm->stack_vm += pages;
865         if (flags & (VM_RESERVED|VM_IO))
866                 mm->reserved_vm += pages;
867 }
868 #endif /* CONFIG_PROC_FS */
869
870 /*
871  * The caller must hold down_write(current->mm->mmap_sem).
872  */
873
874 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
875                         unsigned long len, unsigned long prot,
876                         unsigned long flags, unsigned long pgoff)
877 {
878         struct mm_struct * mm = current->mm;
879         struct vm_area_struct * vma, * prev;
880         struct inode *inode;
881         unsigned int vm_flags;
882         int correct_wcount = 0;
883         int error;
884         struct rb_node ** rb_link, * rb_parent;
885         int accountable = 1;
886         unsigned long charged = 0, reqprot = prot;
887
888         if (file) {
889                 if (is_file_hugepages(file))
890                         accountable = 0;
891
892                 if (!file->f_op || !file->f_op->mmap)
893                         return -ENODEV;
894
895                 if ((prot & PROT_EXEC) &&
896                     (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
897                         return -EPERM;
898         }
899         /*
900          * Does the application expect PROT_READ to imply PROT_EXEC?
901          *
902          * (the exception is when the underlying filesystem is noexec
903          *  mounted, in which case we dont add PROT_EXEC.)
904          */
905         if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
906                 if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
907                         prot |= PROT_EXEC;
908
909         if (!len)
910                 return -EINVAL;
911
912         /* Careful about overflows.. */
913         len = PAGE_ALIGN(len);
914         if (!len || len > TASK_SIZE)
915                 return -ENOMEM;
916
917         /* offset overflow? */
918         if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
919                return -EOVERFLOW;
920
921         /* Too many mappings? */
922         if (mm->map_count > sysctl_max_map_count)
923                 return -ENOMEM;
924
925         /* Obtain the address to map to. we verify (or select) it and ensure
926          * that it represents a valid section of the address space.
927          */
928         addr = get_unmapped_area_prot(file, addr, len, pgoff, flags, prot & PROT_EXEC);
929         if (addr & ~PAGE_MASK)
930                 return addr;
931
932         /* Do simple checking here so the lower-level routines won't have
933          * to. we assume access permissions have been handled by the open
934          * of the memory object, so we don't do any here.
935          */
936         vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
937                         mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
938
939         if (flags & MAP_LOCKED) {
940                 if (!can_do_mlock())
941                         return -EPERM;
942                 vm_flags |= VM_LOCKED;
943         }
944         /* mlock MCL_FUTURE? */
945         if (vm_flags & VM_LOCKED) {
946                 unsigned long locked, lock_limit;
947                 locked = len >> PAGE_SHIFT;
948                 locked += mm->locked_vm;
949                 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
950                 lock_limit >>= PAGE_SHIFT;
951                 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
952                         return -EAGAIN;
953         }
954
955         inode = file ? file->f_dentry->d_inode : NULL;
956
957         if (file) {
958                 switch (flags & MAP_TYPE) {
959                 case MAP_SHARED:
960                         if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
961                                 return -EACCES;
962
963                         /*
964                          * Make sure we don't allow writing to an append-only
965                          * file..
966                          */
967                         if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
968                                 return -EACCES;
969
970                         /*
971                          * Make sure there are no mandatory locks on the file.
972                          */
973                         if (locks_verify_locked(inode))
974                                 return -EAGAIN;
975
976                         vm_flags |= VM_SHARED | VM_MAYSHARE;
977                         if (!(file->f_mode & FMODE_WRITE))
978                                 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
979
980                         /* fall through */
981                 case MAP_PRIVATE:
982                         if (!(file->f_mode & FMODE_READ))
983                                 return -EACCES;
984                         break;
985
986                 default:
987                         return -EINVAL;
988                 }
989         } else {
990                 switch (flags & MAP_TYPE) {
991                 case MAP_SHARED:
992                         vm_flags |= VM_SHARED | VM_MAYSHARE;
993                         break;
994                 case MAP_PRIVATE:
995                         /*
996                          * Set pgoff according to addr for anon_vma.
997                          */
998                         pgoff = addr >> PAGE_SHIFT;
999                         break;
1000                 default:
1001                         return -EINVAL;
1002                 }
1003         }
1004
1005         error = security_file_mmap(file, reqprot, prot, flags);
1006         if (error)
1007                 return error;
1008                 
1009         /* Clear old maps */
1010         error = -ENOMEM;
1011 munmap_back:
1012         vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1013         if (vma && vma->vm_start < addr + len) {
1014                 if (do_munmap(mm, addr, len))
1015                         return -ENOMEM;
1016                 goto munmap_back;
1017         }
1018
1019         /* Check against address space limit. */
1020         if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1021                 return -ENOMEM;
1022
1023         if (accountable && (!(flags & MAP_NORESERVE) ||
1024                             sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
1025                 if (vm_flags & VM_SHARED) {
1026                         /* Check memory availability in shmem_file_setup? */
1027                         vm_flags |= VM_ACCOUNT;
1028                 } else if (vm_flags & VM_WRITE) {
1029                         /*
1030                          * Private writable mapping: check memory availability
1031                          */
1032                         charged = len >> PAGE_SHIFT;
1033                         if (security_vm_enough_memory(charged))
1034                                 return -ENOMEM;
1035                         vm_flags |= VM_ACCOUNT;
1036                 }
1037         }
1038
1039         /*
1040          * Can we just expand an old private anonymous mapping?
1041          * The VM_SHARED test is necessary because shmem_zero_setup
1042          * will create the file object for a shared anonymous map below.
1043          */
1044         if (!file && !(vm_flags & VM_SHARED) &&
1045             vma_merge(mm, prev, addr, addr + len, vm_flags,
1046                                         NULL, NULL, pgoff, NULL))
1047                 goto out;
1048
1049         /*
1050          * Determine the object being mapped and call the appropriate
1051          * specific mapper. the address has already been validated, but
1052          * not unmapped, but the maps are removed from the list.
1053          */
1054         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1055         if (!vma) {
1056                 error = -ENOMEM;
1057                 goto unacct_error;
1058         }
1059         memset(vma, 0, sizeof(*vma));
1060
1061         vma->vm_mm = mm;
1062         vma->vm_start = addr;
1063         vma->vm_end = addr + len;
1064         vma->vm_flags = vm_flags;
1065         vma->vm_page_prot = protection_map[vm_flags & 0x0f];
1066         vma->vm_pgoff = pgoff;
1067
1068         if (file) {
1069                 error = -EINVAL;
1070                 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1071                         goto free_vma;
1072                 if (vm_flags & VM_DENYWRITE) {
1073                         error = deny_write_access(file);
1074                         if (error)
1075                                 goto free_vma;
1076                         correct_wcount = 1;
1077                 }
1078                 vma->vm_file = file;
1079                 get_file(file);
1080                 error = file->f_op->mmap(file, vma);
1081                 if (error)
1082                         goto unmap_and_free_vma;
1083         } else if (vm_flags & VM_SHARED) {
1084                 error = shmem_zero_setup(vma);
1085                 if (error)
1086                         goto free_vma;
1087         }
1088
1089         /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1090          * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1091          * that memory reservation must be checked; but that reservation
1092          * belongs to shared memory object, not to vma: so now clear it.
1093          */
1094         if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
1095                 vma->vm_flags &= ~VM_ACCOUNT;
1096
1097         /* Can addr have changed??
1098          *
1099          * Answer: Yes, several device drivers can do it in their
1100          *         f_op->mmap method. -DaveM
1101          */
1102         addr = vma->vm_start;
1103         pgoff = vma->vm_pgoff;
1104         vm_flags = vma->vm_flags;
1105
1106         if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
1107                         vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
1108                 file = vma->vm_file;
1109                 vma_link(mm, vma, prev, rb_link, rb_parent);
1110                 if (correct_wcount)
1111                         atomic_inc(&inode->i_writecount);
1112         } else {
1113                 if (file) {
1114                         if (correct_wcount)
1115                                 atomic_inc(&inode->i_writecount);
1116                         fput(file);
1117                 }
1118                 mpol_free(vma_policy(vma));
1119                 kmem_cache_free(vm_area_cachep, vma);
1120         }
1121 out:    
1122         vx_vmpages_add(mm, len >> PAGE_SHIFT);
1123         __vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1124         if (vm_flags & VM_LOCKED) {
1125                 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1126                 make_pages_present(addr, addr + len);
1127         }
1128         if (flags & MAP_POPULATE) {
1129                 up_write(&mm->mmap_sem);
1130                 sys_remap_file_pages(addr, len, 0,
1131                                         pgoff, flags & MAP_NONBLOCK);
1132                 down_write(&mm->mmap_sem);
1133         }
1134         return addr;
1135
1136 unmap_and_free_vma:
1137         if (correct_wcount)
1138                 atomic_inc(&inode->i_writecount);
1139         vma->vm_file = NULL;
1140         fput(file);
1141
1142         /* Undo any partial mapping done by a device driver. */
1143         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1144         charged = 0;
1145 free_vma:
1146         kmem_cache_free(vm_area_cachep, vma);
1147 unacct_error:
1148         if (charged)
1149                 vm_unacct_memory(charged);
1150         return error;
1151 }
1152
1153 EXPORT_SYMBOL(do_mmap_pgoff);
1154
1155 /* Get an address range which is currently unmapped.
1156  * For shmat() with addr=0.
1157  *
1158  * Ugly calling convention alert:
1159  * Return value with the low bits set means error value,
1160  * ie
1161  *      if (ret & ~PAGE_MASK)
1162  *              error = ret;
1163  *
1164  * This function "knows" that -ENOMEM has the bits set.
1165  */
1166 #ifndef HAVE_ARCH_UNMAPPED_AREA
1167 unsigned long
1168 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1169                 unsigned long len, unsigned long pgoff, unsigned long flags)
1170 {
1171         struct mm_struct *mm = current->mm;
1172         struct vm_area_struct *vma;
1173         unsigned long start_addr;
1174
1175         if (len > TASK_SIZE)
1176                 return -ENOMEM;
1177
1178         if (addr) {
1179                 addr = PAGE_ALIGN(addr);
1180                 vma = find_vma(mm, addr);
1181                 if (TASK_SIZE - len >= addr &&
1182                     (!vma || addr + len <= vma->vm_start))
1183                         return addr;
1184         }
1185         start_addr = addr = mm->free_area_cache;
1186
1187 full_search:
1188         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1189                 /* At this point:  (!vma || addr < vma->vm_end). */
1190                 if (TASK_SIZE - len < addr) {
1191                         /*
1192                          * Start a new search - just in case we missed
1193                          * some holes.
1194                          */
1195                         if (start_addr != TASK_UNMAPPED_BASE) {
1196                                 start_addr = addr = TASK_UNMAPPED_BASE;
1197                                 goto full_search;
1198                         }
1199                         return -ENOMEM;
1200                 }
1201                 if (!vma || addr + len <= vma->vm_start) {
1202                         /*
1203                          * Remember the place where we stopped the search:
1204                          */
1205                         mm->free_area_cache = addr + len;
1206                         return addr;
1207                 }
1208                 addr = vma->vm_end;
1209         }
1210 }
1211 #endif  
1212
1213 void arch_unmap_area(struct vm_area_struct *area)
1214 {
1215         /*
1216          * Is this a new hole at the lowest possible address?
1217          */
1218         if (area->vm_start >= TASK_UNMAPPED_BASE &&
1219                         area->vm_start < area->vm_mm->free_area_cache)
1220                 area->vm_mm->free_area_cache = area->vm_start;
1221 }
1222
1223 /*
1224  * This mmap-allocator allocates new areas top-down from below the
1225  * stack's low limit (the base):
1226  */
1227 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1228 unsigned long
1229 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1230                           const unsigned long len, const unsigned long pgoff,
1231                           const unsigned long flags)
1232 {
1233         struct vm_area_struct *vma;
1234         struct mm_struct *mm = current->mm;
1235         unsigned long addr = addr0;
1236
1237         /* requested length too big for entire address space */
1238         if (len > TASK_SIZE)
1239                 return -ENOMEM;
1240
1241         /* requesting a specific address */
1242         if (addr) {
1243                 addr = PAGE_ALIGN(addr);
1244                 vma = find_vma(mm, addr);
1245                 if (TASK_SIZE - len >= addr &&
1246                                 (!vma || addr + len <= vma->vm_start))
1247                         return addr;
1248         }
1249
1250         /* either no address requested or can't fit in requested address hole */
1251         addr = mm->free_area_cache;
1252
1253         /* make sure it can fit in the remaining address space */
1254         if (addr > len) {
1255                 vma = find_vma(mm, addr-len);
1256                 if (!vma || addr <= vma->vm_start)
1257                         /* remember the address as a hint for next time */
1258                         return (mm->free_area_cache = addr-len);
1259         }
1260
1261         addr = mm->mmap_base-len;
1262
1263         do {
1264                 /*
1265                  * Lookup failure means no vma is above this address,
1266                  * else if new region fits below vma->vm_start,
1267                  * return with success:
1268                  */
1269                 vma = find_vma(mm, addr);
1270                 if (!vma || addr+len <= vma->vm_start)
1271                         /* remember the address as a hint for next time */
1272                         return (mm->free_area_cache = addr);
1273
1274                 /* try just below the current vma->vm_start */
1275                 addr = vma->vm_start-len;
1276         } while (len < vma->vm_start);
1277
1278         /*
1279          * A failed mmap() very likely causes application failure,
1280          * so fall back to the bottom-up function here. This scenario
1281          * can happen with large stack limits and large mmap()
1282          * allocations.
1283          */
1284         mm->free_area_cache = TASK_UNMAPPED_BASE;
1285         addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1286         /*
1287          * Restore the topdown base:
1288          */
1289         mm->free_area_cache = mm->mmap_base;
1290
1291         return addr;
1292 }
1293 #endif
1294
1295 void arch_unmap_area_topdown(struct vm_area_struct *area)
1296 {
1297         /*
1298          * Is this a new hole at the highest possible address?
1299          */
1300         if (area->vm_end > area->vm_mm->free_area_cache)
1301                 area->vm_mm->free_area_cache = area->vm_end;
1302
1303         /* dont allow allocations above current base */
1304         if (area->vm_mm->free_area_cache > area->vm_mm->mmap_base)
1305                 area->vm_mm->free_area_cache = area->vm_mm->mmap_base;
1306 }
1307
1308
1309 unsigned long
1310 get_unmapped_area_prot(struct file *file, unsigned long addr, unsigned long len,
1311                 unsigned long pgoff, unsigned long flags, int exec)
1312 {
1313         unsigned long ret;
1314
1315         if (!(flags & MAP_FIXED)) {
1316                 unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1317
1318                 if (exec && current->mm->get_unmapped_exec_area)
1319                         get_area = current->mm->get_unmapped_exec_area;
1320                 else
1321                         get_area = current->mm->get_unmapped_area;
1322
1323                 if (file && file->f_op && file->f_op->get_unmapped_area)
1324                         get_area = file->f_op->get_unmapped_area;
1325                 addr = get_area(file, addr, len, pgoff, flags);
1326                 if (IS_ERR_VALUE(addr))
1327                         return addr;
1328         }
1329
1330         if (addr > TASK_SIZE - len)
1331                 return -ENOMEM;
1332         if (addr & ~PAGE_MASK)
1333                 return -EINVAL;
1334         if (file && is_file_hugepages(file))  {
1335                 /*
1336                  * Check if the given range is hugepage aligned, and
1337                  * can be made suitable for hugepages.
1338                  */
1339                 ret = prepare_hugepage_range(addr, len);
1340         } else {
1341                 /*
1342                  * Ensure that a normal request is not falling in a
1343                  * reserved hugepage range.  For some archs like IA-64,
1344                  * there is a separate region for hugepages.
1345                  */
1346                 ret = is_hugepage_only_range(current->mm, addr, len);
1347         }
1348         if (ret)
1349                 return -EINVAL;
1350         return addr;
1351 }
1352
1353 EXPORT_SYMBOL(get_unmapped_area_prot);
1354
1355 #define SHLIB_BASE             0x00111000
1356
1357 unsigned long arch_get_unmapped_exec_area(struct file *filp, unsigned long addr0,
1358                 unsigned long len0, unsigned long pgoff, unsigned long flags)
1359 {
1360         unsigned long addr = addr0, len = len0;
1361         struct mm_struct *mm = current->mm;
1362         struct vm_area_struct *vma;
1363         unsigned long tmp;
1364
1365         if (len > TASK_SIZE)
1366                 return -ENOMEM;
1367
1368         if (!addr && !(flags & MAP_FIXED))
1369                 addr = randomize_range(SHLIB_BASE, 0x01000000, len);
1370
1371         if (addr) {
1372                 addr = PAGE_ALIGN(addr);
1373                 vma = find_vma(mm, addr);
1374                 if (TASK_SIZE - len >= addr &&
1375                     (!vma || addr + len <= vma->vm_start)) {
1376                         return addr;
1377                 }
1378         }
1379
1380         addr = SHLIB_BASE;
1381         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1382                 /* At this point:  (!vma || addr < vma->vm_end). */
1383                 if (TASK_SIZE - len < addr)
1384                         return -ENOMEM;
1385
1386                 if (!vma || addr + len <= vma->vm_start) {
1387                         /*
1388                          * Must not let a PROT_EXEC mapping get into the
1389                          * brk area:
1390                          */
1391                         if (addr + len > mm->brk)
1392                                 goto failed;
1393
1394                         /*
1395                          * Up until the brk area we randomize addresses
1396                          * as much as possible:
1397                          */
1398                         if (addr >= 0x01000000) {
1399                                 tmp = randomize_range(0x01000000, PAGE_ALIGN(max(mm->start_brk, 0x08000000)), len);
1400                                 vma = find_vma(mm, tmp);
1401                                 if (TASK_SIZE - len >= tmp &&
1402                                     (!vma || tmp + len <= vma->vm_start))
1403                                         return tmp;
1404                         }
1405                         /*
1406                          * Ok, randomization didnt work out - return
1407                          * the result of the linear search:
1408                          */
1409                         return addr;
1410                 }
1411                 addr = vma->vm_end;
1412         }
1413
1414 failed:
1415         return current->mm->get_unmapped_area(filp, addr0, len0, pgoff, flags);
1416 }
1417
1418
1419 /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
1420 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1421 {
1422         struct vm_area_struct *vma = NULL;
1423
1424         if (mm) {
1425                 /* Check the cache first. */
1426                 /* (Cache hit rate is typically around 35%.) */
1427                 vma = mm->mmap_cache;
1428                 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1429                         struct rb_node * rb_node;
1430
1431                         rb_node = mm->mm_rb.rb_node;
1432                         vma = NULL;
1433
1434                         while (rb_node) {
1435                                 struct vm_area_struct * vma_tmp;
1436
1437                                 vma_tmp = rb_entry(rb_node,
1438                                                 struct vm_area_struct, vm_rb);
1439
1440                                 if (vma_tmp->vm_end > addr) {
1441                                         vma = vma_tmp;
1442                                         if (vma_tmp->vm_start <= addr)
1443                                                 break;
1444                                         rb_node = rb_node->rb_left;
1445                                 } else
1446                                         rb_node = rb_node->rb_right;
1447                         }
1448                         if (vma)
1449                                 mm->mmap_cache = vma;
1450                 }
1451         }
1452         return vma;
1453 }
1454
1455 EXPORT_SYMBOL(find_vma);
1456
1457 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1458 struct vm_area_struct *
1459 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1460                         struct vm_area_struct **pprev)
1461 {
1462         struct vm_area_struct *vma = NULL, *prev = NULL;
1463         struct rb_node * rb_node;
1464         if (!mm)
1465                 goto out;
1466
1467         /* Guard against addr being lower than the first VMA */
1468         vma = mm->mmap;
1469
1470         /* Go through the RB tree quickly. */
1471         rb_node = mm->mm_rb.rb_node;
1472
1473         while (rb_node) {
1474                 struct vm_area_struct *vma_tmp;
1475                 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1476
1477                 if (addr < vma_tmp->vm_end) {
1478                         rb_node = rb_node->rb_left;
1479                 } else {
1480                         prev = vma_tmp;
1481                         if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1482                                 break;
1483                         rb_node = rb_node->rb_right;
1484                 }
1485         }
1486
1487 out:
1488         *pprev = prev;
1489         return prev ? prev->vm_next : vma;
1490 }
1491
1492 static int over_stack_limit(unsigned long sz)
1493 {
1494         if (sz < EXEC_STACK_BIAS)
1495                 return 0;
1496         return (sz - EXEC_STACK_BIAS) >
1497                         current->signal->rlim[RLIMIT_STACK].rlim_cur;
1498 }
1499
1500 /*
1501  * Verify that the stack growth is acceptable and
1502  * update accounting. This is shared with both the
1503  * grow-up and grow-down cases.
1504  */
1505 static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
1506 {
1507         struct mm_struct *mm = vma->vm_mm;
1508         struct rlimit *rlim = current->signal->rlim;
1509
1510         /* address space limit tests */
1511         if (!may_expand_vm(mm, grow))
1512                 return -ENOMEM;
1513
1514         /* Stack limit test */
1515         if (over_stack_limit(size))
1516                 return -ENOMEM;
1517
1518         /* mlock limit tests */
1519         if (vma->vm_flags & VM_LOCKED) {
1520                 unsigned long locked;
1521                 unsigned long limit;
1522                 locked = mm->locked_vm + grow;
1523                 limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
1524                 if (locked > limit && !capable(CAP_IPC_LOCK))
1525                         return -ENOMEM;
1526         }
1527
1528         /*
1529          * Overcommit..  This must be the final test, as it will
1530          * update security statistics.
1531          */
1532         if (security_vm_enough_memory(grow))
1533                 return -ENOMEM;
1534
1535         /* Ok, everything looks good - let it rip */
1536         vx_vmpages_add(mm, grow);
1537         if (vma->vm_flags & VM_LOCKED)
1538                 vx_vmlocked_add(mm, grow);
1539         __vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
1540         return 0;
1541 }
1542
1543 #ifdef CONFIG_STACK_GROWSUP
1544 /*
1545  * vma is the first one with address > vma->vm_end.  Have to extend vma.
1546  */
1547 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1548 {
1549         int error;
1550
1551         if (!(vma->vm_flags & VM_GROWSUP))
1552                 return -EFAULT;
1553
1554         /*
1555          * We must make sure the anon_vma is allocated
1556          * so that the anon_vma locking is not a noop.
1557          */
1558         if (unlikely(anon_vma_prepare(vma)))
1559                 return -ENOMEM;
1560         anon_vma_lock(vma);
1561
1562         /*
1563          * vma->vm_start/vm_end cannot change under us because the caller
1564          * is required to hold the mmap_sem in read mode.  We need the
1565          * anon_vma lock to serialize against concurrent expand_stacks.
1566          */
1567         address += 4 + PAGE_SIZE - 1;
1568         address &= PAGE_MASK;
1569         error = 0;
1570
1571         /* Somebody else might have raced and expanded it already */
1572         if (address > vma->vm_end) {
1573                 unsigned long size, grow;
1574
1575                 size = address - vma->vm_start;
1576                 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1577
1578                 error = acct_stack_growth(vma, size, grow);
1579                 if (!error)
1580                         vma->vm_end = address;
1581         }
1582         anon_vma_unlock(vma);
1583         return error;
1584 }
1585
1586 struct vm_area_struct *
1587 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1588 {
1589         struct vm_area_struct *vma, *prev;
1590
1591         addr &= PAGE_MASK;
1592         vma = find_vma_prev(mm, addr, &prev);
1593         if (vma && (vma->vm_start <= addr))
1594                 return vma;
1595         if (!prev || expand_stack(prev, addr))
1596                 return NULL;
1597         if (prev->vm_flags & VM_LOCKED) {
1598                 make_pages_present(addr, prev->vm_end);
1599         }
1600         return prev;
1601 }
1602 #else
1603 /*
1604  * vma is the first one with address < vma->vm_start.  Have to extend vma.
1605  */
1606 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1607 {
1608         int error;
1609
1610         /*
1611          * We must make sure the anon_vma is allocated
1612          * so that the anon_vma locking is not a noop.
1613          */
1614         if (unlikely(anon_vma_prepare(vma)))
1615                 return -ENOMEM;
1616         anon_vma_lock(vma);
1617
1618         /*
1619          * vma->vm_start/vm_end cannot change under us because the caller
1620          * is required to hold the mmap_sem in read mode.  We need the
1621          * anon_vma lock to serialize against concurrent expand_stacks.
1622          */
1623         address &= PAGE_MASK;
1624         error = 0;
1625
1626         /* Somebody else might have raced and expanded it already */
1627         if (address < vma->vm_start) {
1628                 unsigned long size, grow;
1629
1630                 size = vma->vm_end - address;
1631                 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1632
1633                 error = acct_stack_growth(vma, size, grow);
1634                 if (!error) {
1635                         vma->vm_start = address;
1636                         vma->vm_pgoff -= grow;
1637                 }
1638         }
1639         anon_vma_unlock(vma);
1640         return error;
1641 }
1642
1643 struct vm_area_struct *
1644 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1645 {
1646         struct vm_area_struct * vma;
1647         unsigned long start;
1648
1649         addr &= PAGE_MASK;
1650         vma = find_vma(mm,addr);
1651         if (!vma)
1652                 return NULL;
1653         if (vma->vm_start <= addr)
1654                 return vma;
1655         if (!(vma->vm_flags & VM_GROWSDOWN))
1656                 return NULL;
1657         start = vma->vm_start;
1658         if (expand_stack(vma, addr))
1659                 return NULL;
1660         if (vma->vm_flags & VM_LOCKED) {
1661                 make_pages_present(addr, start);
1662         }
1663         return vma;
1664 }
1665 #endif
1666
1667 /* Normal function to fix up a mapping
1668  * This function is the default for when an area has no specific
1669  * function.  This may be used as part of a more specific routine.
1670  *
1671  * By the time this function is called, the area struct has been
1672  * removed from the process mapping list.
1673  */
1674 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1675 {
1676         size_t len = area->vm_end - area->vm_start;
1677
1678         vx_vmpages_sub(area->vm_mm, len >> PAGE_SHIFT);
1679
1680         if (area->vm_flags & VM_LOCKED)
1681                 vx_vmlocked_sub(area->vm_mm, len >> PAGE_SHIFT);
1682         vm_stat_unaccount(area);
1683         area->vm_mm->unmap_area(area);
1684         remove_vm_struct(area);
1685 }
1686
1687 /*
1688  * Update the VMA and inode share lists.
1689  *
1690  * Ok - we have the memory areas we should free on the 'free' list,
1691  * so release them, and do the vma updates.
1692  */
1693 static void unmap_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
1694 {
1695         do {
1696                 struct vm_area_struct *next = vma->vm_next;
1697                 unmap_vma(mm, vma);
1698                 vma = next;
1699         } while (vma);
1700         validate_mm(mm);
1701 }
1702
1703 /*
1704  * Get rid of page table information in the indicated region.
1705  *
1706  * Called with the page table lock held.
1707  */
1708 static void unmap_region(struct mm_struct *mm,
1709                 struct vm_area_struct *vma, struct vm_area_struct *prev,
1710                 unsigned long start, unsigned long end)
1711 {
1712         struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
1713         struct mmu_gather *tlb;
1714         unsigned long nr_accounted = 0;
1715
1716         lru_add_drain();
1717         spin_lock(&mm->page_table_lock);
1718         tlb = tlb_gather_mmu(mm, 0);
1719         unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1720         vm_unacct_memory(nr_accounted);
1721         free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1722                                  next? next->vm_start: 0);
1723         tlb_finish_mmu(tlb, start, end);
1724         spin_unlock(&mm->page_table_lock);
1725 }
1726
1727 /*
1728  * Create a list of vma's touched by the unmap, removing them from the mm's
1729  * vma list as we go..
1730  */
1731 static void
1732 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1733         struct vm_area_struct *prev, unsigned long end)
1734 {
1735         struct vm_area_struct **insertion_point;
1736         struct vm_area_struct *tail_vma = NULL;
1737
1738         insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1739         do {
1740                 rb_erase(&vma->vm_rb, &mm->mm_rb);
1741                 mm->map_count--;
1742                 tail_vma = vma;
1743                 vma = vma->vm_next;
1744         } while (vma && vma->vm_start < end);
1745         *insertion_point = vma;
1746         tail_vma->vm_next = NULL;
1747         mm->mmap_cache = NULL;          /* Kill the cache. */
1748 }
1749
1750 /*
1751  * Split a vma into two pieces at address 'addr', a new vma is allocated
1752  * either for the first part or the the tail.
1753  */
1754 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1755               unsigned long addr, int new_below)
1756 {
1757         struct mempolicy *pol;
1758         struct vm_area_struct *new;
1759
1760         if (is_vm_hugetlb_page(vma) && (addr & ~HPAGE_MASK))
1761                 return -EINVAL;
1762
1763         if (mm->map_count >= sysctl_max_map_count)
1764                 return -ENOMEM;
1765
1766         new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1767         if (!new)
1768                 return -ENOMEM;
1769
1770         /* most fields are the same, copy all, and then fixup */
1771         *new = *vma;
1772
1773         if (new_below)
1774                 new->vm_end = addr;
1775         else {
1776                 new->vm_start = addr;
1777                 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1778         }
1779
1780         pol = mpol_copy(vma_policy(vma));
1781         if (IS_ERR(pol)) {
1782                 kmem_cache_free(vm_area_cachep, new);
1783                 return PTR_ERR(pol);
1784         }
1785         vma_set_policy(new, pol);
1786
1787         if (new->vm_file)
1788                 get_file(new->vm_file);
1789
1790         if (new->vm_ops && new->vm_ops->open)
1791                 new->vm_ops->open(new);
1792
1793         if (new_below) {
1794                 unsigned long old_end = vma->vm_end;
1795
1796                 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1797                         ((addr - new->vm_start) >> PAGE_SHIFT), new);
1798                 if (vma->vm_flags & VM_EXEC)
1799                         arch_remove_exec_range(mm, old_end);
1800         } else
1801                 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1802
1803         return 0;
1804 }
1805
1806 /* Munmap is split into 2 main parts -- this part which finds
1807  * what needs doing, and the areas themselves, which do the
1808  * work.  This now handles partial unmappings.
1809  * Jeremy Fitzhardinge <jeremy@goop.org>
1810  */
1811 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1812 {
1813         unsigned long end;
1814         struct vm_area_struct *vma, *prev, *last;
1815
1816         if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1817                 return -EINVAL;
1818
1819         if ((len = PAGE_ALIGN(len)) == 0)
1820                 return -EINVAL;
1821
1822         /* Find the first overlapping VMA */
1823         vma = find_vma_prev(mm, start, &prev);
1824         if (!vma)
1825                 return 0;
1826         /* we have  start < vma->vm_end  */
1827
1828         /* if it doesn't overlap, we have nothing.. */
1829         end = start + len;
1830         if (vma->vm_start >= end)
1831                 return 0;
1832
1833         /*
1834          * If we need to split any vma, do it now to save pain later.
1835          *
1836          * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1837          * unmapped vm_area_struct will remain in use: so lower split_vma
1838          * places tmp vma above, and higher split_vma places tmp vma below.
1839          */
1840         if (start > vma->vm_start) {
1841                 int error = split_vma(mm, vma, start, 0);
1842                 if (error)
1843                         return error;
1844                 prev = vma;
1845         }
1846
1847         /* Does it split the last one? */
1848         last = find_vma(mm, end);
1849         if (last && end > last->vm_start) {
1850                 int error = split_vma(mm, last, end, 1);
1851                 if (error)
1852                         return error;
1853         }
1854         vma = prev? prev->vm_next: mm->mmap;
1855
1856         /*
1857          * Remove the vma's, and unmap the actual pages
1858          */
1859         detach_vmas_to_be_unmapped(mm, vma, prev, end);
1860         unmap_region(mm, vma, prev, start, end);
1861
1862         /* Fix up all other VM information */
1863         unmap_vma_list(mm, vma);
1864
1865         return 0;
1866 }
1867
1868 EXPORT_SYMBOL(do_munmap);
1869
1870 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1871 {
1872         int ret;
1873         struct mm_struct *mm = current->mm;
1874
1875         profile_munmap(addr);
1876
1877         down_write(&mm->mmap_sem);
1878         ret = do_munmap(mm, addr, len);
1879         up_write(&mm->mmap_sem);
1880         return ret;
1881 }
1882
1883 static inline void verify_mm_writelocked(struct mm_struct *mm)
1884 {
1885 #ifdef CONFIG_DEBUG_KERNEL
1886         if (unlikely(down_read_trylock(&mm->mmap_sem))) {
1887                 WARN_ON(1);
1888                 up_read(&mm->mmap_sem);
1889         }
1890 #endif
1891 }
1892
1893 /*
1894  *  this is really a simplified "do_mmap".  it only handles
1895  *  anonymous maps.  eventually we may be able to do some
1896  *  brk-specific accounting here.
1897  */
1898 unsigned long do_brk(unsigned long addr, unsigned long len)
1899 {
1900         struct mm_struct * mm = current->mm;
1901         struct vm_area_struct * vma, * prev;
1902         unsigned long flags;
1903         struct rb_node ** rb_link, * rb_parent;
1904         pgoff_t pgoff = addr >> PAGE_SHIFT;
1905
1906         len = PAGE_ALIGN(len);
1907         if (!len)
1908                 return addr;
1909
1910         if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1911                 return -EINVAL;
1912
1913         /*
1914          * mlock MCL_FUTURE?
1915          */
1916         if (mm->def_flags & VM_LOCKED) {
1917                 unsigned long locked, lock_limit;
1918                 locked = len >> PAGE_SHIFT;
1919                 locked += mm->locked_vm;
1920                 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1921                 lock_limit >>= PAGE_SHIFT;
1922                 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1923                         return -EAGAIN;
1924                 if (!vx_vmlocked_avail(mm, len >> PAGE_SHIFT))
1925                         return -ENOMEM;
1926         }
1927
1928         /*
1929          * mm->mmap_sem is required to protect against another thread
1930          * changing the mappings in case we sleep.
1931          */
1932         verify_mm_writelocked(mm);
1933
1934         /*
1935          * Clear old maps.  this also does some error checking for us
1936          */
1937  munmap_back:
1938         vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1939         if (vma && vma->vm_start < addr + len) {
1940                 if (do_munmap(mm, addr, len))
1941                         return -ENOMEM;
1942                 goto munmap_back;
1943         }
1944
1945         /* Check against address space limits *after* clearing old maps... */
1946         if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1947                 return -ENOMEM;
1948
1949         if (mm->map_count > sysctl_max_map_count)
1950                 return -ENOMEM;
1951
1952         if (security_vm_enough_memory(len >> PAGE_SHIFT))
1953                 return -ENOMEM;
1954
1955         flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1956
1957         /* Can we just expand an old private anonymous mapping? */
1958         if (vma_merge(mm, prev, addr, addr + len, flags,
1959                                         NULL, NULL, pgoff, NULL))
1960                 goto out;
1961
1962         /*
1963          * create a vma struct for an anonymous mapping
1964          */
1965         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1966         if (!vma) {
1967                 vm_unacct_memory(len >> PAGE_SHIFT);
1968                 return -ENOMEM;
1969         }
1970         memset(vma, 0, sizeof(*vma));
1971
1972         vma->vm_mm = mm;
1973         vma->vm_start = addr;
1974         vma->vm_end = addr + len;
1975         vma->vm_pgoff = pgoff;
1976         vma->vm_flags = flags;
1977         vma->vm_page_prot = protection_map[flags & 0x0f];
1978         vma_link(mm, vma, prev, rb_link, rb_parent);
1979 out:
1980         vx_vmpages_add(mm, len >> PAGE_SHIFT);
1981         if (flags & VM_LOCKED) {
1982                 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1983                 make_pages_present(addr, addr + len);
1984         }
1985         return addr;
1986 }
1987
1988 EXPORT_SYMBOL(do_brk);
1989
1990 /* Release all mmaps. */
1991 void exit_mmap(struct mm_struct *mm)
1992 {
1993         struct mmu_gather *tlb;
1994         struct vm_area_struct *vma = mm->mmap;
1995         unsigned long nr_accounted = 0;
1996         unsigned long end;
1997
1998         lru_add_drain();
1999
2000         spin_lock(&mm->page_table_lock);
2001
2002         flush_cache_mm(mm);
2003         tlb = tlb_gather_mmu(mm, 1);
2004         /* Use -1 here to ensure all VMAs in the mm are unmapped */
2005         end = unmap_vmas(&tlb, mm, vma, 0, -1, &nr_accounted, NULL);
2006         vm_unacct_memory(nr_accounted);
2007         free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
2008         tlb_finish_mmu(tlb, 0, end);
2009
2010         mm->mmap = mm->mmap_cache = NULL;
2011         mm->mm_rb = RB_ROOT;
2012         set_mm_counter(mm, rss, 0);
2013         vx_vmpages_sub(mm, mm->total_vm);
2014         vx_vmlocked_sub(mm, mm->locked_vm);
2015         arch_flush_exec_range(mm);
2016
2017         spin_unlock(&mm->page_table_lock);
2018
2019         /*
2020          * Walk the list again, actually closing and freeing it
2021          * without holding any MM locks.
2022          */
2023         while (vma) {
2024                 struct vm_area_struct *next = vma->vm_next;
2025                 remove_vm_struct(vma);
2026                 vma = next;
2027         }
2028
2029         BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
2030 }
2031
2032 /* Insert vm structure into process list sorted by address
2033  * and into the inode's i_mmap tree.  If vm_file is non-NULL
2034  * then i_mmap_lock is taken here.
2035  */
2036 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2037 {
2038         struct vm_area_struct * __vma, * prev;
2039         struct rb_node ** rb_link, * rb_parent;
2040
2041         /*
2042          * The vm_pgoff of a purely anonymous vma should be irrelevant
2043          * until its first write fault, when page's anon_vma and index
2044          * are set.  But now set the vm_pgoff it will almost certainly
2045          * end up with (unless mremap moves it elsewhere before that
2046          * first wfault), so /proc/pid/maps tells a consistent story.
2047          *
2048          * By setting it to reflect the virtual start address of the
2049          * vma, merges and splits can happen in a seamless way, just
2050          * using the existing file pgoff checks and manipulations.
2051          * Similarly in do_mmap_pgoff and in do_brk.
2052          */
2053         if (!vma->vm_file) {
2054                 BUG_ON(vma->anon_vma);
2055                 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2056         }
2057         __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2058         if (__vma && __vma->vm_start < vma->vm_end)
2059                 return -ENOMEM;
2060         vma_link(mm, vma, prev, rb_link, rb_parent);
2061         return 0;
2062 }
2063
2064 /*
2065  * Copy the vma structure to a new location in the same mm,
2066  * prior to moving page table entries, to effect an mremap move.
2067  */
2068 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2069         unsigned long addr, unsigned long len, pgoff_t pgoff)
2070 {
2071         struct vm_area_struct *vma = *vmap;
2072         unsigned long vma_start = vma->vm_start;
2073         struct mm_struct *mm = vma->vm_mm;
2074         struct vm_area_struct *new_vma, *prev;
2075         struct rb_node **rb_link, *rb_parent;
2076         struct mempolicy *pol;
2077
2078         /*
2079          * If anonymous vma has not yet been faulted, update new pgoff
2080          * to match new location, to increase its chance of merging.
2081          */
2082         if (!vma->vm_file && !vma->anon_vma)
2083                 pgoff = addr >> PAGE_SHIFT;
2084
2085         find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2086         new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2087                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2088         if (new_vma) {
2089                 /*
2090                  * Source vma may have been merged into new_vma
2091                  */
2092                 if (vma_start >= new_vma->vm_start &&
2093                     vma_start < new_vma->vm_end)
2094                         *vmap = new_vma;
2095         } else {
2096                 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2097                 if (new_vma) {
2098                         *new_vma = *vma;
2099                         pol = mpol_copy(vma_policy(vma));
2100                         if (IS_ERR(pol)) {
2101                                 kmem_cache_free(vm_area_cachep, new_vma);
2102                                 return NULL;
2103                         }
2104                         vma_set_policy(new_vma, pol);
2105                         new_vma->vm_start = addr;
2106                         new_vma->vm_end = addr + len;
2107                         new_vma->vm_pgoff = pgoff;
2108                         if (new_vma->vm_file)
2109                                 get_file(new_vma->vm_file);
2110                         if (new_vma->vm_ops && new_vma->vm_ops->open)
2111                                 new_vma->vm_ops->open(new_vma);
2112                         vma_link(mm, new_vma, prev, rb_link, rb_parent);
2113                 }
2114         }
2115         return new_vma;
2116 }
2117
2118 /*
2119  * Return true if the calling process may expand its vm space by the passed
2120  * number of pages
2121  */
2122 int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2123 {
2124         unsigned long cur = mm->total_vm;       /* pages */
2125         unsigned long lim;
2126
2127         lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2128
2129         if (cur + npages > lim)
2130                 return 0;
2131         if (!vx_vmpages_avail(mm, npages))
2132                 return 0;
2133         return 1;
2134 }
2135
2136 /*
2137  * Insert a new vma covering the given region, with the given flags and
2138  * protections.  Pre-install the mappings to zero or more leading pages
2139  * in the region.  Note, this does put_page on pages[0..npages-1] in all
2140  * cases, even on error return.
2141  */
2142 int install_special_mapping(struct mm_struct *mm,
2143                             unsigned long addr, unsigned long len,
2144                             unsigned long vm_flags, pgprot_t pgprot,
2145                             struct page **pages, unsigned int npages)
2146 {
2147         struct vm_area_struct *vma;
2148         int err = -ENOMEM;
2149
2150         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2151         if (unlikely(vma == NULL))
2152                 goto out;
2153         memset(vma, 0, sizeof(*vma));
2154
2155         vma->vm_mm = mm;
2156         vma->vm_start = addr;
2157         vma->vm_end = addr + len;
2158
2159         vma->vm_flags = vm_flags;
2160         vma->vm_page_prot = pgprot;
2161
2162         insert_vm_struct(mm, vma);
2163         mm->total_vm += len >> PAGE_SHIFT;
2164
2165         for (err = 0; npages > 0 && !err; --npages, ++pages, addr += PAGE_SIZE)
2166                 err = install_page(mm, vma, addr, *pages, vma->vm_page_prot);
2167
2168  out:
2169         for (; npages > 0; --npages, ++pages)
2170                 put_page(*pages);
2171
2172         return err;
2173 }