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