initial commit with v2.6.9
[linux-2.6.9-moxart.git] / mm / mmap.c
blob8ccbb00c208ffff415bdd825751dcda94bfb9fc6
1 /*
2 * mm/mmap.c
4 * Written by obz.
6 * Address space accounting code <alan@redhat.com>
7 */
9 #include <linux/slab.h>
10 #include <linux/shm.h>
11 #include <linux/mman.h>
12 #include <linux/pagemap.h>
13 #include <linux/swap.h>
14 #include <linux/syscalls.h>
15 #include <linux/init.h>
16 #include <linux/file.h>
17 #include <linux/fs.h>
18 #include <linux/personality.h>
19 #include <linux/security.h>
20 #include <linux/hugetlb.h>
21 #include <linux/profile.h>
22 #include <linux/module.h>
23 #include <linux/mount.h>
24 #include <linux/mempolicy.h>
25 #include <linux/rmap.h>
27 #include <asm/uaccess.h>
28 #include <asm/cacheflush.h>
29 #include <asm/tlb.h>
32 * WARNING: the debugging will use recursive algorithms so never enable this
33 * unless you know what you are doing.
35 #undef DEBUG_MM_RB
37 /* description of effects of mapping type and prot in current implementation.
38 * this is due to the limited x86 page protection hardware. The expected
39 * behavior is in parens:
41 * map_type prot
42 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
43 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
44 * w: (no) no w: (no) no w: (yes) yes w: (no) no
45 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
47 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
48 * w: (no) no w: (no) no w: (copy) copy w: (no) no
49 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
52 pgprot_t protection_map[16] = {
53 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
54 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
57 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
58 int sysctl_overcommit_ratio = 50; /* default is 50% */
59 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
60 atomic_t vm_committed_space = ATOMIC_INIT(0);
62 EXPORT_SYMBOL(sysctl_overcommit_memory);
63 EXPORT_SYMBOL(sysctl_overcommit_ratio);
64 EXPORT_SYMBOL(sysctl_max_map_count);
65 EXPORT_SYMBOL(vm_committed_space);
68 * Requires inode->i_mapping->i_mmap_lock
70 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
71 struct file *file, struct address_space *mapping)
73 if (vma->vm_flags & VM_DENYWRITE)
74 atomic_inc(&file->f_dentry->d_inode->i_writecount);
75 if (vma->vm_flags & VM_SHARED)
76 mapping->i_mmap_writable--;
78 flush_dcache_mmap_lock(mapping);
79 if (unlikely(vma->vm_flags & VM_NONLINEAR))
80 list_del_init(&vma->shared.vm_set.list);
81 else
82 vma_prio_tree_remove(vma, &mapping->i_mmap);
83 flush_dcache_mmap_unlock(mapping);
87 * Remove one vm structure and free it.
89 static void remove_vm_struct(struct vm_area_struct *vma)
91 struct file *file = vma->vm_file;
93 might_sleep();
94 if (file) {
95 struct address_space *mapping = file->f_mapping;
96 spin_lock(&mapping->i_mmap_lock);
97 __remove_shared_vm_struct(vma, file, mapping);
98 spin_unlock(&mapping->i_mmap_lock);
100 if (vma->vm_ops && vma->vm_ops->close)
101 vma->vm_ops->close(vma);
102 if (file)
103 fput(file);
104 anon_vma_unlink(vma);
105 mpol_free(vma_policy(vma));
106 kmem_cache_free(vm_area_cachep, vma);
110 * sys_brk() for the most part doesn't need the global kernel
111 * lock, except when an application is doing something nasty
112 * like trying to un-brk an area that has already been mapped
113 * to a regular file. in this case, the unmapping will need
114 * to invoke file system routines that need the global lock.
116 asmlinkage unsigned long sys_brk(unsigned long brk)
118 unsigned long rlim, retval;
119 unsigned long newbrk, oldbrk;
120 struct mm_struct *mm = current->mm;
122 down_write(&mm->mmap_sem);
124 if (brk < mm->end_code)
125 goto out;
126 newbrk = PAGE_ALIGN(brk);
127 oldbrk = PAGE_ALIGN(mm->brk);
128 if (oldbrk == newbrk)
129 goto set_brk;
131 /* Always allow shrinking brk. */
132 if (brk <= mm->brk) {
133 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
134 goto set_brk;
135 goto out;
138 /* Check against rlimit.. */
139 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
140 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
141 goto out;
143 /* Check against existing mmap mappings. */
144 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
145 goto out;
147 /* Ok, looks good - let it rip. */
148 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
149 goto out;
150 set_brk:
151 mm->brk = brk;
152 out:
153 retval = mm->brk;
154 up_write(&mm->mmap_sem);
155 return retval;
158 #ifdef DEBUG_MM_RB
159 static int browse_rb(struct rb_root *root)
161 int i = 0, j;
162 struct rb_node *nd, *pn = NULL;
163 unsigned long prev = 0, pend = 0;
165 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
166 struct vm_area_struct *vma;
167 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
168 if (vma->vm_start < prev)
169 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
170 if (vma->vm_start < pend)
171 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
172 if (vma->vm_start > vma->vm_end)
173 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
174 i++;
175 pn = nd;
177 j = 0;
178 for (nd = pn; nd; nd = rb_prev(nd)) {
179 j++;
181 if (i != j)
182 printk("backwards %d, forwards %d\n", j, i), i = 0;
183 return i;
186 void validate_mm(struct mm_struct *mm)
188 int bug = 0;
189 int i = 0;
190 struct vm_area_struct *tmp = mm->mmap;
191 while (tmp) {
192 tmp = tmp->vm_next;
193 i++;
195 if (i != mm->map_count)
196 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
197 i = browse_rb(&mm->mm_rb);
198 if (i != mm->map_count)
199 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
200 if (bug)
201 BUG();
203 #else
204 #define validate_mm(mm) do { } while (0)
205 #endif
207 static struct vm_area_struct *
208 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
209 struct vm_area_struct **pprev, struct rb_node ***rb_link,
210 struct rb_node ** rb_parent)
212 struct vm_area_struct * vma;
213 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
215 __rb_link = &mm->mm_rb.rb_node;
216 rb_prev = __rb_parent = NULL;
217 vma = NULL;
219 while (*__rb_link) {
220 struct vm_area_struct *vma_tmp;
222 __rb_parent = *__rb_link;
223 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
225 if (vma_tmp->vm_end > addr) {
226 vma = vma_tmp;
227 if (vma_tmp->vm_start <= addr)
228 return vma;
229 __rb_link = &__rb_parent->rb_left;
230 } else {
231 rb_prev = __rb_parent;
232 __rb_link = &__rb_parent->rb_right;
236 *pprev = NULL;
237 if (rb_prev)
238 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
239 *rb_link = __rb_link;
240 *rb_parent = __rb_parent;
241 return vma;
244 static inline void
245 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
246 struct vm_area_struct *prev, struct rb_node *rb_parent)
248 if (prev) {
249 vma->vm_next = prev->vm_next;
250 prev->vm_next = vma;
251 } else {
252 mm->mmap = vma;
253 if (rb_parent)
254 vma->vm_next = rb_entry(rb_parent,
255 struct vm_area_struct, vm_rb);
256 else
257 vma->vm_next = NULL;
261 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
262 struct rb_node **rb_link, struct rb_node *rb_parent)
264 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
265 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
268 static inline void __vma_link_file(struct vm_area_struct *vma)
270 struct file * file;
272 file = vma->vm_file;
273 if (file) {
274 struct address_space *mapping = file->f_mapping;
276 if (vma->vm_flags & VM_DENYWRITE)
277 atomic_dec(&file->f_dentry->d_inode->i_writecount);
278 if (vma->vm_flags & VM_SHARED)
279 mapping->i_mmap_writable++;
281 flush_dcache_mmap_lock(mapping);
282 if (unlikely(vma->vm_flags & VM_NONLINEAR))
283 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
284 else
285 vma_prio_tree_insert(vma, &mapping->i_mmap);
286 flush_dcache_mmap_unlock(mapping);
290 static void
291 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
292 struct vm_area_struct *prev, struct rb_node **rb_link,
293 struct rb_node *rb_parent)
295 __vma_link_list(mm, vma, prev, rb_parent);
296 __vma_link_rb(mm, vma, rb_link, rb_parent);
297 __anon_vma_link(vma);
300 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
301 struct vm_area_struct *prev, struct rb_node **rb_link,
302 struct rb_node *rb_parent)
304 struct address_space *mapping = NULL;
306 if (vma->vm_file)
307 mapping = vma->vm_file->f_mapping;
309 if (mapping)
310 spin_lock(&mapping->i_mmap_lock);
311 anon_vma_lock(vma);
313 __vma_link(mm, vma, prev, rb_link, rb_parent);
314 __vma_link_file(vma);
316 anon_vma_unlock(vma);
317 if (mapping)
318 spin_unlock(&mapping->i_mmap_lock);
320 mm->map_count++;
321 validate_mm(mm);
325 * Helper for vma_adjust in the split_vma insert case:
326 * insert vm structure into list and rbtree and anon_vma,
327 * but it has already been inserted into prio_tree earlier.
329 static void
330 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
332 struct vm_area_struct * __vma, * prev;
333 struct rb_node ** rb_link, * rb_parent;
335 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
336 if (__vma && __vma->vm_start < vma->vm_end)
337 BUG();
338 __vma_link(mm, vma, prev, rb_link, rb_parent);
339 mm->map_count++;
342 static inline void
343 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
344 struct vm_area_struct *prev)
346 prev->vm_next = vma->vm_next;
347 rb_erase(&vma->vm_rb, &mm->mm_rb);
348 if (mm->mmap_cache == vma)
349 mm->mmap_cache = prev;
353 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
354 * is already present in an i_mmap tree without adjusting the tree.
355 * The following helper function should be used when such adjustments
356 * are necessary. The "insert" vma (if any) is to be inserted
357 * before we drop the necessary locks.
359 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
360 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
362 struct mm_struct *mm = vma->vm_mm;
363 struct vm_area_struct *next = vma->vm_next;
364 struct vm_area_struct *importer = NULL;
365 struct address_space *mapping = NULL;
366 struct prio_tree_root *root = NULL;
367 struct file *file = vma->vm_file;
368 struct anon_vma *anon_vma = NULL;
369 long adjust_next = 0;
370 int remove_next = 0;
372 if (next && !insert) {
373 if (end >= next->vm_end) {
375 * vma expands, overlapping all the next, and
376 * perhaps the one after too (mprotect case 6).
378 again: remove_next = 1 + (end > next->vm_end);
379 end = next->vm_end;
380 anon_vma = next->anon_vma;
381 } else if (end > next->vm_start) {
383 * vma expands, overlapping part of the next:
384 * mprotect case 5 shifting the boundary up.
386 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
387 anon_vma = next->anon_vma;
388 importer = vma;
389 } else if (end < vma->vm_end) {
391 * vma shrinks, and !insert tells it's not
392 * split_vma inserting another: so it must be
393 * mprotect case 4 shifting the boundary down.
395 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
396 anon_vma = next->anon_vma;
397 importer = next;
401 if (file) {
402 mapping = file->f_mapping;
403 if (!(vma->vm_flags & VM_NONLINEAR))
404 root = &mapping->i_mmap;
405 spin_lock(&mapping->i_mmap_lock);
406 if (insert) {
408 * Put into prio_tree now, so instantiated pages
409 * are visible to arm/parisc __flush_dcache_page
410 * throughout; but we cannot insert into address
411 * space until vma start or end is updated.
413 __vma_link_file(insert);
418 * When changing only vma->vm_end, we don't really need
419 * anon_vma lock: but is that case worth optimizing out?
421 if (vma->anon_vma)
422 anon_vma = vma->anon_vma;
423 if (anon_vma) {
424 spin_lock(&anon_vma->lock);
426 * Easily overlooked: when mprotect shifts the boundary,
427 * make sure the expanding vma has anon_vma set if the
428 * shrinking vma had, to cover any anon pages imported.
430 if (importer && !importer->anon_vma) {
431 importer->anon_vma = anon_vma;
432 __anon_vma_link(importer);
436 if (root) {
437 flush_dcache_mmap_lock(mapping);
438 vma_prio_tree_remove(vma, root);
439 if (adjust_next)
440 vma_prio_tree_remove(next, root);
443 vma->vm_start = start;
444 vma->vm_end = end;
445 vma->vm_pgoff = pgoff;
446 if (adjust_next) {
447 next->vm_start += adjust_next << PAGE_SHIFT;
448 next->vm_pgoff += adjust_next;
451 if (root) {
452 if (adjust_next)
453 vma_prio_tree_insert(next, root);
454 vma_prio_tree_insert(vma, root);
455 flush_dcache_mmap_unlock(mapping);
458 if (remove_next) {
460 * vma_merge has merged next into vma, and needs
461 * us to remove next before dropping the locks.
463 __vma_unlink(mm, next, vma);
464 if (file)
465 __remove_shared_vm_struct(next, file, mapping);
466 if (next->anon_vma)
467 __anon_vma_merge(vma, next);
468 } else if (insert) {
470 * split_vma has split insert from vma, and needs
471 * us to insert it before dropping the locks
472 * (it may either follow vma or precede it).
474 __insert_vm_struct(mm, insert);
477 if (anon_vma)
478 spin_unlock(&anon_vma->lock);
479 if (mapping)
480 spin_unlock(&mapping->i_mmap_lock);
482 if (remove_next) {
483 if (file)
484 fput(file);
485 mm->map_count--;
486 mpol_free(vma_policy(next));
487 kmem_cache_free(vm_area_cachep, next);
489 * In mprotect's case 6 (see comments on vma_merge),
490 * we must remove another next too. It would clutter
491 * up the code too much to do both in one go.
493 if (remove_next == 2) {
494 next = vma->vm_next;
495 goto again;
499 validate_mm(mm);
503 * If the vma has a ->close operation then the driver probably needs to release
504 * per-vma resources, so we don't attempt to merge those.
506 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
508 static inline int is_mergeable_vma(struct vm_area_struct *vma,
509 struct file *file, unsigned long vm_flags)
511 if (vma->vm_flags != vm_flags)
512 return 0;
513 if (vma->vm_file != file)
514 return 0;
515 if (vma->vm_ops && vma->vm_ops->close)
516 return 0;
517 return 1;
520 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
521 struct anon_vma *anon_vma2)
523 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
527 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
528 * in front of (at a lower virtual address and file offset than) the vma.
530 * We cannot merge two vmas if they have differently assigned (non-NULL)
531 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
533 * We don't check here for the merged mmap wrapping around the end of pagecache
534 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
535 * wrap, nor mmaps which cover the final page at index -1UL.
537 static int
538 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
539 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
541 if (is_mergeable_vma(vma, file, vm_flags) &&
542 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
543 if (vma->vm_pgoff == vm_pgoff)
544 return 1;
546 return 0;
550 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
551 * beyond (at a higher virtual address and file offset than) the vma.
553 * We cannot merge two vmas if they have differently assigned (non-NULL)
554 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
556 static int
557 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
558 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
560 if (is_mergeable_vma(vma, file, vm_flags) &&
561 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
562 pgoff_t vm_pglen;
563 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
564 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
565 return 1;
567 return 0;
571 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
572 * whether that can be merged with its predecessor or its successor.
573 * Or both (it neatly fills a hole).
575 * In most cases - when called for mmap, brk or mremap - [addr,end) is
576 * certain not to be mapped by the time vma_merge is called; but when
577 * called for mprotect, it is certain to be already mapped (either at
578 * an offset within prev, or at the start of next), and the flags of
579 * this area are about to be changed to vm_flags - and the no-change
580 * case has already been eliminated.
582 * The following mprotect cases have to be considered, where AAAA is
583 * the area passed down from mprotect_fixup, never extending beyond one
584 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
586 * AAAA AAAA AAAA AAAA
587 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
588 * cannot merge might become might become might become
589 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
590 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
591 * mremap move: PPPPNNNNNNNN 8
592 * AAAA
593 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
594 * might become case 1 below case 2 below case 3 below
596 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
597 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
599 struct vm_area_struct *vma_merge(struct mm_struct *mm,
600 struct vm_area_struct *prev, unsigned long addr,
601 unsigned long end, unsigned long vm_flags,
602 struct anon_vma *anon_vma, struct file *file,
603 pgoff_t pgoff, struct mempolicy *policy)
605 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
606 struct vm_area_struct *area, *next;
609 * We later require that vma->vm_flags == vm_flags,
610 * so this tests vma->vm_flags & VM_SPECIAL, too.
612 if (vm_flags & VM_SPECIAL)
613 return NULL;
615 if (prev)
616 next = prev->vm_next;
617 else
618 next = mm->mmap;
619 area = next;
620 if (next && next->vm_end == end) /* cases 6, 7, 8 */
621 next = next->vm_next;
624 * Can it merge with the predecessor?
626 if (prev && prev->vm_end == addr &&
627 mpol_equal(vma_policy(prev), policy) &&
628 can_vma_merge_after(prev, vm_flags,
629 anon_vma, file, pgoff)) {
631 * OK, it can. Can we now merge in the successor as well?
633 if (next && end == next->vm_start &&
634 mpol_equal(policy, vma_policy(next)) &&
635 can_vma_merge_before(next, vm_flags,
636 anon_vma, file, pgoff+pglen) &&
637 is_mergeable_anon_vma(prev->anon_vma,
638 next->anon_vma)) {
639 /* cases 1, 6 */
640 vma_adjust(prev, prev->vm_start,
641 next->vm_end, prev->vm_pgoff, NULL);
642 } else /* cases 2, 5, 7 */
643 vma_adjust(prev, prev->vm_start,
644 end, prev->vm_pgoff, NULL);
645 return prev;
649 * Can this new request be merged in front of next?
651 if (next && end == next->vm_start &&
652 mpol_equal(policy, vma_policy(next)) &&
653 can_vma_merge_before(next, vm_flags,
654 anon_vma, file, pgoff+pglen)) {
655 if (prev && addr < prev->vm_end) /* case 4 */
656 vma_adjust(prev, prev->vm_start,
657 addr, prev->vm_pgoff, NULL);
658 else /* cases 3, 8 */
659 vma_adjust(area, addr, next->vm_end,
660 next->vm_pgoff - pglen, NULL);
661 return area;
664 return NULL;
668 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
669 * neighbouring vmas for a suitable anon_vma, before it goes off
670 * to allocate a new anon_vma. It checks because a repetitive
671 * sequence of mprotects and faults may otherwise lead to distinct
672 * anon_vmas being allocated, preventing vma merge in subsequent
673 * mprotect.
675 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
677 struct vm_area_struct *near;
678 unsigned long vm_flags;
680 near = vma->vm_next;
681 if (!near)
682 goto try_prev;
685 * Since only mprotect tries to remerge vmas, match flags
686 * which might be mprotected into each other later on.
687 * Neither mlock nor madvise tries to remerge at present,
688 * so leave their flags as obstructing a merge.
690 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
691 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
693 if (near->anon_vma && vma->vm_end == near->vm_start &&
694 mpol_equal(vma_policy(vma), vma_policy(near)) &&
695 can_vma_merge_before(near, vm_flags,
696 NULL, vma->vm_file, vma->vm_pgoff +
697 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
698 return near->anon_vma;
699 try_prev:
701 * It is potentially slow to have to call find_vma_prev here.
702 * But it's only on the first write fault on the vma, not
703 * every time, and we could devise a way to avoid it later
704 * (e.g. stash info in next's anon_vma_node when assigning
705 * an anon_vma, or when trying vma_merge). Another time.
707 if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
708 BUG();
709 if (!near)
710 goto none;
712 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
713 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
715 if (near->anon_vma && near->vm_end == vma->vm_start &&
716 mpol_equal(vma_policy(near), vma_policy(vma)) &&
717 can_vma_merge_after(near, vm_flags,
718 NULL, vma->vm_file, vma->vm_pgoff))
719 return near->anon_vma;
720 none:
722 * There's no absolute need to look only at touching neighbours:
723 * we could search further afield for "compatible" anon_vmas.
724 * But it would probably just be a waste of time searching,
725 * or lead to too many vmas hanging off the same anon_vma.
726 * We're trying to allow mprotect remerging later on,
727 * not trying to minimize memory used for anon_vmas.
729 return NULL;
732 #ifdef CONFIG_PROC_FS
733 void __vm_stat_account(struct mm_struct *mm, unsigned long flags,
734 struct file *file, long pages)
736 const unsigned long stack_flags
737 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
739 #ifdef CONFIG_HUGETLB
740 if (flags & VM_HUGETLB) {
741 if (!(flags & VM_DONTCOPY))
742 mm->shared_vm += pages;
743 return;
745 #endif /* CONFIG_HUGETLB */
747 if (file)
748 mm->shared_vm += pages;
749 else if (flags & stack_flags)
750 mm->stack_vm += pages;
751 if (flags & VM_EXEC)
752 mm->exec_vm += pages;
753 if (flags & (VM_RESERVED|VM_IO))
754 mm->reserved_vm += pages;
756 #endif /* CONFIG_PROC_FS */
759 * The caller must hold down_write(current->mm->mmap_sem).
762 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
763 unsigned long len, unsigned long prot,
764 unsigned long flags, unsigned long pgoff)
766 struct mm_struct * mm = current->mm;
767 struct vm_area_struct * vma, * prev;
768 struct inode *inode;
769 unsigned int vm_flags;
770 int correct_wcount = 0;
771 int error;
772 struct rb_node ** rb_link, * rb_parent;
773 int accountable = 1;
774 unsigned long charged = 0;
776 if (file) {
777 if (is_file_hugepages(file))
778 accountable = 0;
780 if (!file->f_op || !file->f_op->mmap)
781 return -ENODEV;
783 if ((prot & PROT_EXEC) &&
784 (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
785 return -EPERM;
788 * Does the application expect PROT_READ to imply PROT_EXEC?
790 * (the exception is when the underlying filesystem is noexec
791 * mounted, in which case we dont add PROT_EXEC.)
793 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
794 if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
795 prot |= PROT_EXEC;
797 if (!len)
798 return addr;
800 /* Careful about overflows.. */
801 len = PAGE_ALIGN(len);
802 if (!len || len > TASK_SIZE)
803 return -EINVAL;
805 /* offset overflow? */
806 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
807 return -EINVAL;
809 /* Too many mappings? */
810 if (mm->map_count > sysctl_max_map_count)
811 return -ENOMEM;
813 /* Obtain the address to map to. we verify (or select) it and ensure
814 * that it represents a valid section of the address space.
816 addr = get_unmapped_area(file, addr, len, pgoff, flags);
817 if (addr & ~PAGE_MASK)
818 return addr;
820 /* Do simple checking here so the lower-level routines won't have
821 * to. we assume access permissions have been handled by the open
822 * of the memory object, so we don't do any here.
824 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
825 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
827 if (flags & MAP_LOCKED) {
828 if (!can_do_mlock())
829 return -EPERM;
830 vm_flags |= VM_LOCKED;
832 /* mlock MCL_FUTURE? */
833 if (vm_flags & VM_LOCKED) {
834 unsigned long locked, lock_limit;
835 locked = mm->locked_vm << PAGE_SHIFT;
836 lock_limit = current->rlim[RLIMIT_MEMLOCK].rlim_cur;
837 locked += len;
838 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
839 return -EAGAIN;
842 inode = file ? file->f_dentry->d_inode : NULL;
844 if (file) {
845 switch (flags & MAP_TYPE) {
846 case MAP_SHARED:
847 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
848 return -EACCES;
851 * Make sure we don't allow writing to an append-only
852 * file..
854 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
855 return -EACCES;
858 * Make sure there are no mandatory locks on the file.
860 if (locks_verify_locked(inode))
861 return -EAGAIN;
863 vm_flags |= VM_SHARED | VM_MAYSHARE;
864 if (!(file->f_mode & FMODE_WRITE))
865 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
867 /* fall through */
868 case MAP_PRIVATE:
869 if (!(file->f_mode & FMODE_READ))
870 return -EACCES;
871 break;
873 default:
874 return -EINVAL;
876 } else {
877 switch (flags & MAP_TYPE) {
878 case MAP_SHARED:
879 vm_flags |= VM_SHARED | VM_MAYSHARE;
880 break;
881 case MAP_PRIVATE:
883 * Set pgoff according to addr for anon_vma.
885 pgoff = addr >> PAGE_SHIFT;
886 break;
887 default:
888 return -EINVAL;
892 error = security_file_mmap(file, prot, flags);
893 if (error)
894 return error;
896 /* Clear old maps */
897 error = -ENOMEM;
898 munmap_back:
899 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
900 if (vma && vma->vm_start < addr + len) {
901 if (do_munmap(mm, addr, len))
902 return -ENOMEM;
903 goto munmap_back;
906 /* Check against address space limit. */
907 if ((mm->total_vm << PAGE_SHIFT) + len
908 > current->rlim[RLIMIT_AS].rlim_cur)
909 return -ENOMEM;
911 if (accountable && (!(flags & MAP_NORESERVE) ||
912 sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
913 if (vm_flags & VM_SHARED) {
914 /* Check memory availability in shmem_file_setup? */
915 vm_flags |= VM_ACCOUNT;
916 } else if (vm_flags & VM_WRITE) {
918 * Private writable mapping: check memory availability
920 charged = len >> PAGE_SHIFT;
921 if (security_vm_enough_memory(charged))
922 return -ENOMEM;
923 vm_flags |= VM_ACCOUNT;
928 * Can we just expand an old private anonymous mapping?
929 * The VM_SHARED test is necessary because shmem_zero_setup
930 * will create the file object for a shared anonymous map below.
932 if (!file && !(vm_flags & VM_SHARED) &&
933 vma_merge(mm, prev, addr, addr + len, vm_flags,
934 NULL, NULL, pgoff, NULL))
935 goto out;
938 * Determine the object being mapped and call the appropriate
939 * specific mapper. the address has already been validated, but
940 * not unmapped, but the maps are removed from the list.
942 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
943 if (!vma) {
944 error = -ENOMEM;
945 goto unacct_error;
947 memset(vma, 0, sizeof(*vma));
949 vma->vm_mm = mm;
950 vma->vm_start = addr;
951 vma->vm_end = addr + len;
952 vma->vm_flags = vm_flags;
953 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
954 vma->vm_pgoff = pgoff;
956 if (file) {
957 error = -EINVAL;
958 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
959 goto free_vma;
960 if (vm_flags & VM_DENYWRITE) {
961 error = deny_write_access(file);
962 if (error)
963 goto free_vma;
964 correct_wcount = 1;
966 vma->vm_file = file;
967 get_file(file);
968 error = file->f_op->mmap(file, vma);
969 if (error)
970 goto unmap_and_free_vma;
971 } else if (vm_flags & VM_SHARED) {
972 error = shmem_zero_setup(vma);
973 if (error)
974 goto free_vma;
977 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
978 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
979 * that memory reservation must be checked; but that reservation
980 * belongs to shared memory object, not to vma: so now clear it.
982 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
983 vma->vm_flags &= ~VM_ACCOUNT;
985 /* Can addr have changed??
987 * Answer: Yes, several device drivers can do it in their
988 * f_op->mmap method. -DaveM
990 addr = vma->vm_start;
992 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
993 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
994 vma_link(mm, vma, prev, rb_link, rb_parent);
995 if (correct_wcount)
996 atomic_inc(&inode->i_writecount);
997 } else {
998 if (file) {
999 if (correct_wcount)
1000 atomic_inc(&inode->i_writecount);
1001 fput(file);
1003 mpol_free(vma_policy(vma));
1004 kmem_cache_free(vm_area_cachep, vma);
1006 out:
1007 mm->total_vm += len >> PAGE_SHIFT;
1008 if (vm_flags & VM_LOCKED) {
1009 mm->locked_vm += len >> PAGE_SHIFT;
1010 make_pages_present(addr, addr + len);
1012 if (flags & MAP_POPULATE) {
1013 up_write(&mm->mmap_sem);
1014 sys_remap_file_pages(addr, len, 0,
1015 pgoff, flags & MAP_NONBLOCK);
1016 down_write(&mm->mmap_sem);
1018 __vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1019 return addr;
1021 unmap_and_free_vma:
1022 if (correct_wcount)
1023 atomic_inc(&inode->i_writecount);
1024 vma->vm_file = NULL;
1025 fput(file);
1027 /* Undo any partial mapping done by a device driver. */
1028 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1029 free_vma:
1030 kmem_cache_free(vm_area_cachep, vma);
1031 unacct_error:
1032 if (charged)
1033 vm_unacct_memory(charged);
1034 return error;
1037 EXPORT_SYMBOL(do_mmap_pgoff);
1039 /* Get an address range which is currently unmapped.
1040 * For shmat() with addr=0.
1042 * Ugly calling convention alert:
1043 * Return value with the low bits set means error value,
1044 * ie
1045 * if (ret & ~PAGE_MASK)
1046 * error = ret;
1048 * This function "knows" that -ENOMEM has the bits set.
1050 #ifndef HAVE_ARCH_UNMAPPED_AREA
1051 unsigned long
1052 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1053 unsigned long len, unsigned long pgoff, unsigned long flags)
1055 struct mm_struct *mm = current->mm;
1056 struct vm_area_struct *vma;
1057 unsigned long start_addr;
1059 if (len > TASK_SIZE)
1060 return -ENOMEM;
1062 if (addr) {
1063 addr = PAGE_ALIGN(addr);
1064 vma = find_vma(mm, addr);
1065 if (TASK_SIZE - len >= addr &&
1066 (!vma || addr + len <= vma->vm_start))
1067 return addr;
1069 start_addr = addr = mm->free_area_cache;
1071 full_search:
1072 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1073 /* At this point: (!vma || addr < vma->vm_end). */
1074 if (TASK_SIZE - len < addr) {
1076 * Start a new search - just in case we missed
1077 * some holes.
1079 if (start_addr != TASK_UNMAPPED_BASE) {
1080 start_addr = addr = TASK_UNMAPPED_BASE;
1081 goto full_search;
1083 return -ENOMEM;
1085 if (!vma || addr + len <= vma->vm_start) {
1087 * Remember the place where we stopped the search:
1089 mm->free_area_cache = addr + len;
1090 return addr;
1092 addr = vma->vm_end;
1095 #endif
1097 void arch_unmap_area(struct vm_area_struct *area)
1100 * Is this a new hole at the lowest possible address?
1102 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1103 area->vm_start < area->vm_mm->free_area_cache)
1104 area->vm_mm->free_area_cache = area->vm_start;
1108 * This mmap-allocator allocates new areas top-down from below the
1109 * stack's low limit (the base):
1111 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1112 unsigned long
1113 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1114 const unsigned long len, const unsigned long pgoff,
1115 const unsigned long flags)
1117 struct vm_area_struct *vma, *prev_vma;
1118 struct mm_struct *mm = current->mm;
1119 unsigned long base = mm->mmap_base, addr = addr0;
1120 int first_time = 1;
1122 /* requested length too big for entire address space */
1123 if (len > TASK_SIZE)
1124 return -ENOMEM;
1126 /* dont allow allocations above current base */
1127 if (mm->free_area_cache > base)
1128 mm->free_area_cache = base;
1130 /* requesting a specific address */
1131 if (addr) {
1132 addr = PAGE_ALIGN(addr);
1133 vma = find_vma(mm, addr);
1134 if (TASK_SIZE - len >= addr &&
1135 (!vma || addr + len <= vma->vm_start))
1136 return addr;
1139 try_again:
1140 /* make sure it can fit in the remaining address space */
1141 if (mm->free_area_cache < len)
1142 goto fail;
1144 /* either no address requested or cant fit in requested address hole */
1145 addr = (mm->free_area_cache - len) & PAGE_MASK;
1146 do {
1148 * Lookup failure means no vma is above this address,
1149 * i.e. return with success:
1151 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
1152 return addr;
1155 * new region fits between prev_vma->vm_end and
1156 * vma->vm_start, use it:
1158 if (addr+len <= vma->vm_start &&
1159 (!prev_vma || (addr >= prev_vma->vm_end)))
1160 /* remember the address as a hint for next time */
1161 return (mm->free_area_cache = addr);
1162 else
1163 /* pull free_area_cache down to the first hole */
1164 if (mm->free_area_cache == vma->vm_end)
1165 mm->free_area_cache = vma->vm_start;
1167 /* try just below the current vma->vm_start */
1168 addr = vma->vm_start-len;
1169 } while (len <= vma->vm_start);
1171 fail:
1173 * if hint left us with no space for the requested
1174 * mapping then try again:
1176 if (first_time) {
1177 mm->free_area_cache = base;
1178 first_time = 0;
1179 goto try_again;
1182 * A failed mmap() very likely causes application failure,
1183 * so fall back to the bottom-up function here. This scenario
1184 * can happen with large stack limits and large mmap()
1185 * allocations.
1187 mm->free_area_cache = TASK_UNMAPPED_BASE;
1188 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1190 * Restore the topdown base:
1192 mm->free_area_cache = base;
1194 return addr;
1196 #endif
1198 void arch_unmap_area_topdown(struct vm_area_struct *area)
1201 * Is this a new hole at the highest possible address?
1203 if (area->vm_end > area->vm_mm->free_area_cache)
1204 area->vm_mm->free_area_cache = area->vm_end;
1207 unsigned long
1208 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1209 unsigned long pgoff, unsigned long flags)
1211 if (flags & MAP_FIXED) {
1212 unsigned long ret;
1214 if (addr > TASK_SIZE - len)
1215 return -ENOMEM;
1216 if (addr & ~PAGE_MASK)
1217 return -EINVAL;
1218 if (file && is_file_hugepages(file)) {
1220 * Check if the given range is hugepage aligned, and
1221 * can be made suitable for hugepages.
1223 ret = prepare_hugepage_range(addr, len);
1224 } else {
1226 * Ensure that a normal request is not falling in a
1227 * reserved hugepage range. For some archs like IA-64,
1228 * there is a separate region for hugepages.
1230 ret = is_hugepage_only_range(addr, len);
1232 if (ret)
1233 return -EINVAL;
1234 return addr;
1237 if (file && file->f_op && file->f_op->get_unmapped_area)
1238 return file->f_op->get_unmapped_area(file, addr, len,
1239 pgoff, flags);
1241 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
1244 EXPORT_SYMBOL(get_unmapped_area);
1246 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1247 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1249 struct vm_area_struct *vma = NULL;
1251 if (mm) {
1252 /* Check the cache first. */
1253 /* (Cache hit rate is typically around 35%.) */
1254 vma = mm->mmap_cache;
1255 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1256 struct rb_node * rb_node;
1258 rb_node = mm->mm_rb.rb_node;
1259 vma = NULL;
1261 while (rb_node) {
1262 struct vm_area_struct * vma_tmp;
1264 vma_tmp = rb_entry(rb_node,
1265 struct vm_area_struct, vm_rb);
1267 if (vma_tmp->vm_end > addr) {
1268 vma = vma_tmp;
1269 if (vma_tmp->vm_start <= addr)
1270 break;
1271 rb_node = rb_node->rb_left;
1272 } else
1273 rb_node = rb_node->rb_right;
1275 if (vma)
1276 mm->mmap_cache = vma;
1279 return vma;
1282 EXPORT_SYMBOL(find_vma);
1284 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1285 struct vm_area_struct *
1286 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1287 struct vm_area_struct **pprev)
1289 struct vm_area_struct *vma = NULL, *prev = NULL;
1290 struct rb_node * rb_node;
1291 if (!mm)
1292 goto out;
1294 /* Guard against addr being lower than the first VMA */
1295 vma = mm->mmap;
1297 /* Go through the RB tree quickly. */
1298 rb_node = mm->mm_rb.rb_node;
1300 while (rb_node) {
1301 struct vm_area_struct *vma_tmp;
1302 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1304 if (addr < vma_tmp->vm_end) {
1305 rb_node = rb_node->rb_left;
1306 } else {
1307 prev = vma_tmp;
1308 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1309 break;
1310 rb_node = rb_node->rb_right;
1314 out:
1315 *pprev = prev;
1316 return prev ? prev->vm_next : vma;
1319 #ifdef CONFIG_STACK_GROWSUP
1321 * vma is the first one with address > vma->vm_end. Have to extend vma.
1323 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1325 unsigned long grow;
1327 if (!(vma->vm_flags & VM_GROWSUP))
1328 return -EFAULT;
1331 * We must make sure the anon_vma is allocated
1332 * so that the anon_vma locking is not a noop.
1334 if (unlikely(anon_vma_prepare(vma)))
1335 return -ENOMEM;
1336 anon_vma_lock(vma);
1339 * vma->vm_start/vm_end cannot change under us because the caller
1340 * is required to hold the mmap_sem in read mode. We need the
1341 * anon_vma lock to serialize against concurrent expand_stacks.
1343 address += 4 + PAGE_SIZE - 1;
1344 address &= PAGE_MASK;
1345 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1347 /* Overcommit.. */
1348 if (security_vm_enough_memory(grow)) {
1349 anon_vma_unlock(vma);
1350 return -ENOMEM;
1353 if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
1354 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1355 current->rlim[RLIMIT_AS].rlim_cur) {
1356 anon_vma_unlock(vma);
1357 vm_unacct_memory(grow);
1358 return -ENOMEM;
1360 vma->vm_end = address;
1361 vma->vm_mm->total_vm += grow;
1362 if (vma->vm_flags & VM_LOCKED)
1363 vma->vm_mm->locked_vm += grow;
1364 __vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file, grow);
1365 anon_vma_unlock(vma);
1366 return 0;
1369 struct vm_area_struct *
1370 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1372 struct vm_area_struct *vma, *prev;
1374 addr &= PAGE_MASK;
1375 vma = find_vma_prev(mm, addr, &prev);
1376 if (vma && (vma->vm_start <= addr))
1377 return vma;
1378 if (!prev || expand_stack(prev, addr))
1379 return NULL;
1380 if (prev->vm_flags & VM_LOCKED) {
1381 make_pages_present(addr, prev->vm_end);
1383 return prev;
1385 #else
1387 * vma is the first one with address < vma->vm_start. Have to extend vma.
1389 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1391 unsigned long grow;
1394 * We must make sure the anon_vma is allocated
1395 * so that the anon_vma locking is not a noop.
1397 if (unlikely(anon_vma_prepare(vma)))
1398 return -ENOMEM;
1399 anon_vma_lock(vma);
1402 * vma->vm_start/vm_end cannot change under us because the caller
1403 * is required to hold the mmap_sem in read mode. We need the
1404 * anon_vma lock to serialize against concurrent expand_stacks.
1406 address &= PAGE_MASK;
1407 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1409 /* Overcommit.. */
1410 if (security_vm_enough_memory(grow)) {
1411 anon_vma_unlock(vma);
1412 return -ENOMEM;
1415 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
1416 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1417 current->rlim[RLIMIT_AS].rlim_cur) {
1418 anon_vma_unlock(vma);
1419 vm_unacct_memory(grow);
1420 return -ENOMEM;
1422 vma->vm_start = address;
1423 vma->vm_pgoff -= grow;
1424 vma->vm_mm->total_vm += grow;
1425 if (vma->vm_flags & VM_LOCKED)
1426 vma->vm_mm->locked_vm += grow;
1427 __vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file, grow);
1428 anon_vma_unlock(vma);
1429 return 0;
1432 struct vm_area_struct *
1433 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1435 struct vm_area_struct * vma;
1436 unsigned long start;
1438 addr &= PAGE_MASK;
1439 vma = find_vma(mm,addr);
1440 if (!vma)
1441 return NULL;
1442 if (vma->vm_start <= addr)
1443 return vma;
1444 if (!(vma->vm_flags & VM_GROWSDOWN))
1445 return NULL;
1446 start = vma->vm_start;
1447 if (expand_stack(vma, addr))
1448 return NULL;
1449 if (vma->vm_flags & VM_LOCKED) {
1450 make_pages_present(addr, start);
1452 return vma;
1454 #endif
1457 * Try to free as many page directory entries as we can,
1458 * without having to work very hard at actually scanning
1459 * the page tables themselves.
1461 * Right now we try to free page tables if we have a nice
1462 * PGDIR-aligned area that got free'd up. We could be more
1463 * granular if we want to, but this is fast and simple,
1464 * and covers the bad cases.
1466 * "prev", if it exists, points to a vma before the one
1467 * we just free'd - but there's no telling how much before.
1469 static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
1470 unsigned long start, unsigned long end)
1472 unsigned long first = start & PGDIR_MASK;
1473 unsigned long last = end + PGDIR_SIZE - 1;
1474 unsigned long start_index, end_index;
1475 struct mm_struct *mm = tlb->mm;
1477 if (!prev) {
1478 prev = mm->mmap;
1479 if (!prev)
1480 goto no_mmaps;
1481 if (prev->vm_end > start) {
1482 if (last > prev->vm_start)
1483 last = prev->vm_start;
1484 goto no_mmaps;
1487 for (;;) {
1488 struct vm_area_struct *next = prev->vm_next;
1490 if (next) {
1491 if (next->vm_start < start) {
1492 prev = next;
1493 continue;
1495 if (last > next->vm_start)
1496 last = next->vm_start;
1498 if (prev->vm_end > first)
1499 first = prev->vm_end + PGDIR_SIZE - 1;
1500 break;
1502 no_mmaps:
1503 if (last < first) /* for arches with discontiguous pgd indices */
1504 return;
1506 * If the PGD bits are not consecutive in the virtual address, the
1507 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1509 start_index = pgd_index(first);
1510 if (start_index < FIRST_USER_PGD_NR)
1511 start_index = FIRST_USER_PGD_NR;
1512 end_index = pgd_index(last);
1513 if (end_index > start_index) {
1514 clear_page_tables(tlb, start_index, end_index - start_index);
1515 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
1519 /* Normal function to fix up a mapping
1520 * This function is the default for when an area has no specific
1521 * function. This may be used as part of a more specific routine.
1523 * By the time this function is called, the area struct has been
1524 * removed from the process mapping list.
1526 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1528 size_t len = area->vm_end - area->vm_start;
1530 area->vm_mm->total_vm -= len >> PAGE_SHIFT;
1531 if (area->vm_flags & VM_LOCKED)
1532 area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1533 vm_stat_unaccount(area);
1534 area->vm_mm->unmap_area(area);
1535 remove_vm_struct(area);
1539 * Update the VMA and inode share lists.
1541 * Ok - we have the memory areas we should free on the 'free' list,
1542 * so release them, and do the vma updates.
1544 static void unmap_vma_list(struct mm_struct *mm,
1545 struct vm_area_struct *mpnt)
1547 do {
1548 struct vm_area_struct *next = mpnt->vm_next;
1549 unmap_vma(mm, mpnt);
1550 mpnt = next;
1551 } while (mpnt != NULL);
1552 validate_mm(mm);
1556 * Get rid of page table information in the indicated region.
1558 * Called with the page table lock held.
1560 static void unmap_region(struct mm_struct *mm,
1561 struct vm_area_struct *vma,
1562 struct vm_area_struct *prev,
1563 unsigned long start,
1564 unsigned long end)
1566 struct mmu_gather *tlb;
1567 unsigned long nr_accounted = 0;
1569 lru_add_drain();
1570 tlb = tlb_gather_mmu(mm, 0);
1571 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1572 vm_unacct_memory(nr_accounted);
1574 if (is_hugepage_only_range(start, end - start))
1575 hugetlb_free_pgtables(tlb, prev, start, end);
1576 else
1577 free_pgtables(tlb, prev, start, end);
1578 tlb_finish_mmu(tlb, start, end);
1582 * Create a list of vma's touched by the unmap, removing them from the mm's
1583 * vma list as we go..
1585 static void
1586 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1587 struct vm_area_struct *prev, unsigned long end)
1589 struct vm_area_struct **insertion_point;
1590 struct vm_area_struct *tail_vma = NULL;
1592 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1593 do {
1594 rb_erase(&vma->vm_rb, &mm->mm_rb);
1595 mm->map_count--;
1596 tail_vma = vma;
1597 vma = vma->vm_next;
1598 } while (vma && vma->vm_start < end);
1599 *insertion_point = vma;
1600 tail_vma->vm_next = NULL;
1601 mm->mmap_cache = NULL; /* Kill the cache. */
1605 * Split a vma into two pieces at address 'addr', a new vma is allocated
1606 * either for the first part or the the tail.
1608 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1609 unsigned long addr, int new_below)
1611 struct mempolicy *pol;
1612 struct vm_area_struct *new;
1614 if (mm->map_count >= sysctl_max_map_count)
1615 return -ENOMEM;
1617 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1618 if (!new)
1619 return -ENOMEM;
1621 /* most fields are the same, copy all, and then fixup */
1622 *new = *vma;
1624 if (new_below)
1625 new->vm_end = addr;
1626 else {
1627 new->vm_start = addr;
1628 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1631 pol = mpol_copy(vma_policy(vma));
1632 if (IS_ERR(pol)) {
1633 kmem_cache_free(vm_area_cachep, new);
1634 return PTR_ERR(pol);
1636 vma_set_policy(new, pol);
1638 if (new->vm_file)
1639 get_file(new->vm_file);
1641 if (new->vm_ops && new->vm_ops->open)
1642 new->vm_ops->open(new);
1644 if (new_below)
1645 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1646 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1647 else
1648 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1650 return 0;
1653 /* Munmap is split into 2 main parts -- this part which finds
1654 * what needs doing, and the areas themselves, which do the
1655 * work. This now handles partial unmappings.
1656 * Jeremy Fitzhardinge <jeremy@goop.org>
1658 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1660 unsigned long end;
1661 struct vm_area_struct *mpnt, *prev, *last;
1663 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1664 return -EINVAL;
1666 if ((len = PAGE_ALIGN(len)) == 0)
1667 return -EINVAL;
1669 /* Find the first overlapping VMA */
1670 mpnt = find_vma_prev(mm, start, &prev);
1671 if (!mpnt)
1672 return 0;
1673 /* we have start < mpnt->vm_end */
1675 if (is_vm_hugetlb_page(mpnt)) {
1676 int ret = is_aligned_hugepage_range(start, len);
1678 if (ret)
1679 return ret;
1682 /* if it doesn't overlap, we have nothing.. */
1683 end = start + len;
1684 if (mpnt->vm_start >= end)
1685 return 0;
1688 * If we need to split any vma, do it now to save pain later.
1690 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1691 * unmapped vm_area_struct will remain in use: so lower split_vma
1692 * places tmp vma above, and higher split_vma places tmp vma below.
1694 if (start > mpnt->vm_start) {
1695 if (split_vma(mm, mpnt, start, 0))
1696 return -ENOMEM;
1697 prev = mpnt;
1700 /* Does it split the last one? */
1701 last = find_vma(mm, end);
1702 if (last && end > last->vm_start) {
1703 if (split_vma(mm, last, end, 1))
1704 return -ENOMEM;
1706 mpnt = prev? prev->vm_next: mm->mmap;
1709 * Remove the vma's, and unmap the actual pages
1711 detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1712 spin_lock(&mm->page_table_lock);
1713 unmap_region(mm, mpnt, prev, start, end);
1714 spin_unlock(&mm->page_table_lock);
1716 /* Fix up all other VM information */
1717 unmap_vma_list(mm, mpnt);
1719 return 0;
1722 EXPORT_SYMBOL(do_munmap);
1724 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1726 int ret;
1727 struct mm_struct *mm = current->mm;
1729 profile_munmap(addr);
1731 down_write(&mm->mmap_sem);
1732 ret = do_munmap(mm, addr, len);
1733 up_write(&mm->mmap_sem);
1734 return ret;
1738 * this is really a simplified "do_mmap". it only handles
1739 * anonymous maps. eventually we may be able to do some
1740 * brk-specific accounting here.
1742 unsigned long do_brk(unsigned long addr, unsigned long len)
1744 struct mm_struct * mm = current->mm;
1745 struct vm_area_struct * vma, * prev;
1746 unsigned long flags;
1747 struct rb_node ** rb_link, * rb_parent;
1748 pgoff_t pgoff = addr >> PAGE_SHIFT;
1750 len = PAGE_ALIGN(len);
1751 if (!len)
1752 return addr;
1754 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1755 return -EINVAL;
1758 * mlock MCL_FUTURE?
1760 if (mm->def_flags & VM_LOCKED) {
1761 unsigned long locked, lock_limit;
1762 locked = mm->locked_vm << PAGE_SHIFT;
1763 lock_limit = current->rlim[RLIMIT_MEMLOCK].rlim_cur;
1764 locked += len;
1765 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1766 return -EAGAIN;
1770 * Clear old maps. this also does some error checking for us
1772 munmap_back:
1773 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1774 if (vma && vma->vm_start < addr + len) {
1775 if (do_munmap(mm, addr, len))
1776 return -ENOMEM;
1777 goto munmap_back;
1780 /* Check against address space limits *after* clearing old maps... */
1781 if ((mm->total_vm << PAGE_SHIFT) + len
1782 > current->rlim[RLIMIT_AS].rlim_cur)
1783 return -ENOMEM;
1785 if (mm->map_count > sysctl_max_map_count)
1786 return -ENOMEM;
1788 if (security_vm_enough_memory(len >> PAGE_SHIFT))
1789 return -ENOMEM;
1791 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1793 /* Can we just expand an old private anonymous mapping? */
1794 if (vma_merge(mm, prev, addr, addr + len, flags,
1795 NULL, NULL, pgoff, NULL))
1796 goto out;
1799 * create a vma struct for an anonymous mapping
1801 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1802 if (!vma) {
1803 vm_unacct_memory(len >> PAGE_SHIFT);
1804 return -ENOMEM;
1806 memset(vma, 0, sizeof(*vma));
1808 vma->vm_mm = mm;
1809 vma->vm_start = addr;
1810 vma->vm_end = addr + len;
1811 vma->vm_pgoff = pgoff;
1812 vma->vm_flags = flags;
1813 vma->vm_page_prot = protection_map[flags & 0x0f];
1814 vma_link(mm, vma, prev, rb_link, rb_parent);
1815 out:
1816 mm->total_vm += len >> PAGE_SHIFT;
1817 if (flags & VM_LOCKED) {
1818 mm->locked_vm += len >> PAGE_SHIFT;
1819 make_pages_present(addr, addr + len);
1821 return addr;
1824 EXPORT_SYMBOL(do_brk);
1826 /* Release all mmaps. */
1827 void exit_mmap(struct mm_struct *mm)
1829 struct mmu_gather *tlb;
1830 struct vm_area_struct *vma;
1831 unsigned long nr_accounted = 0;
1833 lru_add_drain();
1835 spin_lock(&mm->page_table_lock);
1837 tlb = tlb_gather_mmu(mm, 1);
1838 flush_cache_mm(mm);
1839 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1840 mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1841 ~0UL, &nr_accounted, NULL);
1842 vm_unacct_memory(nr_accounted);
1843 BUG_ON(mm->map_count); /* This is just debugging */
1844 clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1845 tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1847 vma = mm->mmap;
1848 mm->mmap = mm->mmap_cache = NULL;
1849 mm->mm_rb = RB_ROOT;
1850 mm->rss = 0;
1851 mm->total_vm = 0;
1852 mm->locked_vm = 0;
1854 spin_unlock(&mm->page_table_lock);
1857 * Walk the list again, actually closing and freeing it
1858 * without holding any MM locks.
1860 while (vma) {
1861 struct vm_area_struct *next = vma->vm_next;
1862 remove_vm_struct(vma);
1863 vma = next;
1867 /* Insert vm structure into process list sorted by address
1868 * and into the inode's i_mmap tree. If vm_file is non-NULL
1869 * then i_mmap_lock is taken here.
1871 void insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1873 struct vm_area_struct * __vma, * prev;
1874 struct rb_node ** rb_link, * rb_parent;
1877 * The vm_pgoff of a purely anonymous vma should be irrelevant
1878 * until its first write fault, when page's anon_vma and index
1879 * are set. But now set the vm_pgoff it will almost certainly
1880 * end up with (unless mremap moves it elsewhere before that
1881 * first wfault), so /proc/pid/maps tells a consistent story.
1883 * By setting it to reflect the virtual start address of the
1884 * vma, merges and splits can happen in a seamless way, just
1885 * using the existing file pgoff checks and manipulations.
1886 * Similarly in do_mmap_pgoff and in do_brk.
1888 if (!vma->vm_file) {
1889 BUG_ON(vma->anon_vma);
1890 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
1892 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
1893 if (__vma && __vma->vm_start < vma->vm_end)
1894 BUG();
1895 vma_link(mm, vma, prev, rb_link, rb_parent);
1899 * Copy the vma structure to a new location in the same mm,
1900 * prior to moving page table entries, to effect an mremap move.
1902 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1903 unsigned long addr, unsigned long len, pgoff_t pgoff)
1905 struct vm_area_struct *vma = *vmap;
1906 unsigned long vma_start = vma->vm_start;
1907 struct mm_struct *mm = vma->vm_mm;
1908 struct vm_area_struct *new_vma, *prev;
1909 struct rb_node **rb_link, *rb_parent;
1910 struct mempolicy *pol;
1913 * If anonymous vma has not yet been faulted, update new pgoff
1914 * to match new location, to increase its chance of merging.
1916 if (!vma->vm_file && !vma->anon_vma)
1917 pgoff = addr >> PAGE_SHIFT;
1919 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1920 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
1921 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
1922 if (new_vma) {
1924 * Source vma may have been merged into new_vma
1926 if (vma_start >= new_vma->vm_start &&
1927 vma_start < new_vma->vm_end)
1928 *vmap = new_vma;
1929 } else {
1930 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1931 if (new_vma) {
1932 *new_vma = *vma;
1933 pol = mpol_copy(vma_policy(vma));
1934 if (IS_ERR(pol)) {
1935 kmem_cache_free(vm_area_cachep, new_vma);
1936 return NULL;
1938 vma_set_policy(new_vma, pol);
1939 new_vma->vm_start = addr;
1940 new_vma->vm_end = addr + len;
1941 new_vma->vm_pgoff = pgoff;
1942 if (new_vma->vm_file)
1943 get_file(new_vma->vm_file);
1944 if (new_vma->vm_ops && new_vma->vm_ops->open)
1945 new_vma->vm_ops->open(new_vma);
1946 vma_link(mm, new_vma, prev, rb_link, rb_parent);
1949 return new_vma;