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