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