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