Merge with Linux 2.6.0-test1.
[linux-2.6/linux-mips.git] / mm / mmap.c
blobbcc28864eab613183a4056bccd61fac7d1933aa6
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/init.h>
15 #include <linux/file.h>
16 #include <linux/fs.h>
17 #include <linux/personality.h>
18 #include <linux/security.h>
19 #include <linux/hugetlb.h>
20 #include <linux/profile.h>
21 #include <linux/module.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgalloc.h>
25 #include <asm/tlb.h>
28 * WARNING: the debugging will use recursive algorithms so never enable this
29 * unless you know what you are doing.
31 #undef DEBUG_MM_RB
33 /* description of effects of mapping type and prot in current implementation.
34 * this is due to the limited x86 page protection hardware. The expected
35 * behavior is in parens:
37 * map_type prot
38 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
39 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
40 * w: (no) no w: (no) no w: (yes) yes w: (no) no
41 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
43 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
44 * w: (no) no w: (no) no w: (copy) copy w: (no) no
45 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
48 pgprot_t protection_map[16] = {
49 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
50 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
53 int sysctl_overcommit_memory = 0; /* default is heuristic overcommit */
54 int sysctl_overcommit_ratio = 50; /* default is 50% */
55 atomic_t vm_committed_space = ATOMIC_INIT(0);
57 EXPORT_SYMBOL(sysctl_overcommit_memory);
58 EXPORT_SYMBOL(sysctl_overcommit_ratio);
59 EXPORT_SYMBOL(vm_committed_space);
62 * Requires inode->i_mapping->i_shared_sem
64 static inline void
65 __remove_shared_vm_struct(struct vm_area_struct *vma, struct inode *inode)
67 if (inode) {
68 if (vma->vm_flags & VM_DENYWRITE)
69 atomic_inc(&inode->i_writecount);
70 list_del_init(&vma->shared);
75 * Remove one vm structure from the inode's i_mapping address space.
77 static void remove_shared_vm_struct(struct vm_area_struct *vma)
79 struct file *file = vma->vm_file;
81 if (file) {
82 struct inode *inode = file->f_dentry->d_inode;
84 down(&inode->i_mapping->i_shared_sem);
85 __remove_shared_vm_struct(vma, inode);
86 up(&inode->i_mapping->i_shared_sem);
91 * sys_brk() for the most part doesn't need the global kernel
92 * lock, except when an application is doing something nasty
93 * like trying to un-brk an area that has already been mapped
94 * to a regular file. in this case, the unmapping will need
95 * to invoke file system routines that need the global lock.
97 asmlinkage unsigned long sys_brk(unsigned long brk)
99 unsigned long rlim, retval;
100 unsigned long newbrk, oldbrk;
101 struct mm_struct *mm = current->mm;
103 down_write(&mm->mmap_sem);
105 if (brk < mm->end_code)
106 goto out;
107 newbrk = PAGE_ALIGN(brk);
108 oldbrk = PAGE_ALIGN(mm->brk);
109 if (oldbrk == newbrk)
110 goto set_brk;
112 /* Always allow shrinking brk. */
113 if (brk <= mm->brk) {
114 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
115 goto set_brk;
116 goto out;
119 /* Check against rlimit.. */
120 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
121 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
122 goto out;
124 /* Check against existing mmap mappings. */
125 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
126 goto out;
128 /* Ok, looks good - let it rip. */
129 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
130 goto out;
131 set_brk:
132 mm->brk = brk;
133 out:
134 retval = mm->brk;
135 up_write(&mm->mmap_sem);
136 return retval;
139 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
140 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
141 * into "VM_xxx".
143 static inline unsigned long
144 calc_vm_flags(unsigned long prot, unsigned long flags)
146 #define _trans(x,bit1,bit2) \
147 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
149 unsigned long prot_bits, flag_bits;
150 prot_bits =
151 _trans(prot, PROT_READ, VM_READ) |
152 _trans(prot, PROT_WRITE, VM_WRITE) |
153 _trans(prot, PROT_EXEC, VM_EXEC);
154 flag_bits =
155 _trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN) |
156 _trans(flags, MAP_DENYWRITE, VM_DENYWRITE) |
157 _trans(flags, MAP_EXECUTABLE, VM_EXECUTABLE);
158 return prot_bits | flag_bits;
159 #undef _trans
162 #ifdef DEBUG_MM_RB
163 static int browse_rb(struct rb_node * rb_node) {
164 int i = 0;
165 if (rb_node) {
166 i++;
167 i += browse_rb(rb_node->rb_left);
168 i += browse_rb(rb_node->rb_right);
170 return i;
173 static void validate_mm(struct mm_struct * mm) {
174 int bug = 0;
175 int i = 0;
176 struct vm_area_struct * tmp = mm->mmap;
177 while (tmp) {
178 tmp = tmp->vm_next;
179 i++;
181 if (i != mm->map_count)
182 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
183 i = browse_rb(mm->mm_rb.rb_node);
184 if (i != mm->map_count)
185 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
186 if (bug)
187 BUG();
189 #else
190 #define validate_mm(mm) do { } while (0)
191 #endif
193 static struct vm_area_struct *
194 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
195 struct vm_area_struct **pprev, struct rb_node ***rb_link,
196 struct rb_node ** rb_parent)
198 struct vm_area_struct * vma;
199 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
201 __rb_link = &mm->mm_rb.rb_node;
202 rb_prev = __rb_parent = NULL;
203 vma = NULL;
205 while (*__rb_link) {
206 struct vm_area_struct *vma_tmp;
208 __rb_parent = *__rb_link;
209 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
211 if (vma_tmp->vm_end > addr) {
212 vma = vma_tmp;
213 if (vma_tmp->vm_start <= addr)
214 return vma;
215 __rb_link = &__rb_parent->rb_left;
216 } else {
217 rb_prev = __rb_parent;
218 __rb_link = &__rb_parent->rb_right;
222 *pprev = NULL;
223 if (rb_prev)
224 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
225 *rb_link = __rb_link;
226 *rb_parent = __rb_parent;
227 return vma;
230 static inline void
231 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
232 struct vm_area_struct *prev, struct rb_node *rb_parent)
234 if (prev) {
235 vma->vm_next = prev->vm_next;
236 prev->vm_next = vma;
237 } else {
238 mm->mmap = vma;
239 if (rb_parent)
240 vma->vm_next = rb_entry(rb_parent,
241 struct vm_area_struct, vm_rb);
242 else
243 vma->vm_next = NULL;
247 static void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
248 struct rb_node **rb_link, struct rb_node *rb_parent)
250 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
251 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
254 static inline void __vma_link_file(struct vm_area_struct *vma)
256 struct file * file;
258 file = vma->vm_file;
259 if (file) {
260 struct inode * inode = file->f_dentry->d_inode;
261 struct address_space *mapping = inode->i_mapping;
263 if (vma->vm_flags & VM_DENYWRITE)
264 atomic_dec(&inode->i_writecount);
266 if (vma->vm_flags & VM_SHARED)
267 list_add_tail(&vma->shared, &mapping->i_mmap_shared);
268 else
269 list_add_tail(&vma->shared, &mapping->i_mmap);
273 static void
274 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
275 struct vm_area_struct *prev, struct rb_node **rb_link,
276 struct rb_node *rb_parent)
278 __vma_link_list(mm, vma, prev, rb_parent);
279 __vma_link_rb(mm, vma, rb_link, rb_parent);
280 __vma_link_file(vma);
283 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
284 struct vm_area_struct *prev, struct rb_node **rb_link,
285 struct rb_node *rb_parent)
287 struct address_space *mapping = NULL;
289 if (vma->vm_file)
290 mapping = vma->vm_file->f_dentry->d_inode->i_mapping;
292 if (mapping)
293 down(&mapping->i_shared_sem);
294 spin_lock(&mm->page_table_lock);
295 __vma_link(mm, vma, prev, rb_link, rb_parent);
296 spin_unlock(&mm->page_table_lock);
297 if (mapping)
298 up(&mapping->i_shared_sem);
300 mark_mm_hugetlb(mm, vma);
301 mm->map_count++;
302 validate_mm(mm);
306 * If the vma has a ->close operation then the driver probably needs to release
307 * per-vma resources, so we don't attempt to merge those.
309 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
311 static inline int is_mergeable_vma(struct vm_area_struct *vma,
312 struct file *file, unsigned long vm_flags)
314 if (vma->vm_ops && vma->vm_ops->close)
315 return 0;
316 if (vma->vm_file != file)
317 return 0;
318 if (vma->vm_flags != vm_flags)
319 return 0;
320 if (vma->vm_private_data)
321 return 0;
322 return 1;
326 * Return true if we can merge this (vm_flags,file,vm_pgoff,size)
327 * in front of (at a lower virtual address and file offset than) the vma.
329 * We don't check here for the merged mmap wrapping around the end of pagecache
330 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
331 * wrap, nor mmaps which cover the final page at index -1UL.
333 static int
334 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
335 struct file *file, unsigned long vm_pgoff, unsigned long size)
337 if (is_mergeable_vma(vma, file, vm_flags)) {
338 if (!file)
339 return 1; /* anon mapping */
340 if (vma->vm_pgoff == vm_pgoff + size)
341 return 1;
343 return 0;
347 * Return true if we can merge this (vm_flags,file,vm_pgoff)
348 * beyond (at a higher virtual address and file offset than) the vma.
350 static int
351 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
352 struct file *file, unsigned long vm_pgoff)
354 if (is_mergeable_vma(vma, file, vm_flags)) {
355 unsigned long vma_size;
357 if (!file)
358 return 1; /* anon mapping */
360 vma_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
361 if (vma->vm_pgoff + vma_size == vm_pgoff)
362 return 1;
364 return 0;
368 * Given a new mapping request (addr,end,vm_flags,file,pgoff), figure out
369 * whether that can be merged with its predecessor or its successor. Or
370 * both (it neatly fills a hole).
372 static int vma_merge(struct mm_struct *mm, struct vm_area_struct *prev,
373 struct rb_node *rb_parent, unsigned long addr,
374 unsigned long end, unsigned long vm_flags,
375 struct file *file, unsigned long pgoff)
377 spinlock_t * lock = &mm->page_table_lock;
380 * We later require that vma->vm_flags == vm_flags, so this tests
381 * vma->vm_flags & VM_SPECIAL, too.
383 if (vm_flags & VM_SPECIAL)
384 return 0;
386 if (!prev) {
387 prev = rb_entry(rb_parent, struct vm_area_struct, vm_rb);
388 goto merge_next;
392 * Can it merge with the predecessor?
394 if (prev->vm_end == addr &&
395 is_mergeable_vma(prev, file, vm_flags) &&
396 can_vma_merge_after(prev, vm_flags, file, pgoff)) {
397 struct vm_area_struct *next;
398 struct inode *inode = file ? file->f_dentry->d_inode : NULL;
399 int need_up = 0;
401 if (unlikely(file && prev->vm_next &&
402 prev->vm_next->vm_file == file)) {
403 down(&inode->i_mapping->i_shared_sem);
404 need_up = 1;
406 spin_lock(lock);
407 prev->vm_end = end;
410 * OK, it did. Can we now merge in the successor as well?
412 next = prev->vm_next;
413 if (next && prev->vm_end == next->vm_start &&
414 can_vma_merge_before(next, vm_flags, file,
415 pgoff, (end - addr) >> PAGE_SHIFT)) {
416 prev->vm_end = next->vm_end;
417 __vma_unlink(mm, next, prev);
418 __remove_shared_vm_struct(next, inode);
419 spin_unlock(lock);
420 if (need_up)
421 up(&inode->i_mapping->i_shared_sem);
422 if (file)
423 fput(file);
425 mm->map_count--;
426 kmem_cache_free(vm_area_cachep, next);
427 return 1;
429 spin_unlock(lock);
430 if (need_up)
431 up(&inode->i_mapping->i_shared_sem);
432 return 1;
436 * Can this new request be merged in front of prev->vm_next?
438 prev = prev->vm_next;
439 if (prev) {
440 merge_next:
441 if (!can_vma_merge_before(prev, vm_flags, file,
442 pgoff, (end - addr) >> PAGE_SHIFT))
443 return 0;
444 if (end == prev->vm_start) {
445 spin_lock(lock);
446 prev->vm_start = addr;
447 prev->vm_pgoff -= (end - addr) >> PAGE_SHIFT;
448 spin_unlock(lock);
449 return 1;
453 return 0;
457 * The caller must hold down_write(current->mm->mmap_sem).
460 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
461 unsigned long len, unsigned long prot,
462 unsigned long flags, unsigned long pgoff)
464 struct mm_struct * mm = current->mm;
465 struct vm_area_struct * vma, * prev;
466 struct inode *inode;
467 unsigned int vm_flags;
468 int correct_wcount = 0;
469 int error;
470 struct rb_node ** rb_link, * rb_parent;
471 unsigned long charged = 0;
473 if (file && (!file->f_op || !file->f_op->mmap))
474 return -ENODEV;
476 if (!len)
477 return addr;
479 /* Careful about overflows.. */
480 len = PAGE_ALIGN(len);
481 if (!len || len > TASK_SIZE)
482 return -EINVAL;
484 /* offset overflow? */
485 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
486 return -EINVAL;
488 /* Too many mappings? */
489 if (mm->map_count > MAX_MAP_COUNT)
490 return -ENOMEM;
492 /* Obtain the address to map to. we verify (or select) it and ensure
493 * that it represents a valid section of the address space.
495 addr = get_unmapped_area(file, addr, len, pgoff, flags);
496 if (addr & ~PAGE_MASK)
497 return addr;
499 /* Do simple checking here so the lower-level routines won't have
500 * to. we assume access permissions have been handled by the open
501 * of the memory object, so we don't do any here.
503 vm_flags = calc_vm_flags(prot,flags) | mm->def_flags |
504 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
506 if (flags & MAP_LOCKED) {
507 if (!capable(CAP_IPC_LOCK))
508 return -EPERM;
509 vm_flags |= VM_LOCKED;
511 /* mlock MCL_FUTURE? */
512 if (vm_flags & VM_LOCKED) {
513 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
514 locked += len;
515 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
516 return -EAGAIN;
519 inode = file ? file->f_dentry->d_inode : NULL;
521 if (file) {
522 switch (flags & MAP_TYPE) {
523 case MAP_SHARED:
524 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
525 return -EACCES;
528 * Make sure we don't allow writing to an append-only
529 * file..
531 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
532 return -EACCES;
535 * Make sure there are no mandatory locks on the file.
537 if (locks_verify_locked(inode))
538 return -EAGAIN;
540 vm_flags |= VM_SHARED | VM_MAYSHARE;
541 if (!(file->f_mode & FMODE_WRITE))
542 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
544 /* fall through */
545 case MAP_PRIVATE:
546 if (!(file->f_mode & FMODE_READ))
547 return -EACCES;
548 break;
550 default:
551 return -EINVAL;
553 } else {
554 vm_flags |= VM_SHARED | VM_MAYSHARE;
555 switch (flags & MAP_TYPE) {
556 default:
557 return -EINVAL;
558 case MAP_PRIVATE:
559 vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
560 /* fall through */
561 case MAP_SHARED:
562 break;
566 error = security_file_mmap(file, prot, flags);
567 if (error)
568 return error;
570 /* Clear old maps */
571 error = -ENOMEM;
572 munmap_back:
573 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
574 if (vma && vma->vm_start < addr + len) {
575 if (do_munmap(mm, addr, len))
576 return -ENOMEM;
577 goto munmap_back;
580 /* Check against address space limit. */
581 if ((mm->total_vm << PAGE_SHIFT) + len
582 > current->rlim[RLIMIT_AS].rlim_cur)
583 return -ENOMEM;
585 if (!(flags & MAP_NORESERVE) || sysctl_overcommit_memory > 1) {
586 if (vm_flags & VM_SHARED) {
587 /* Check memory availability in shmem_file_setup? */
588 vm_flags |= VM_ACCOUNT;
589 } else if (vm_flags & VM_WRITE) {
591 * Private writable mapping: check memory availability
593 charged = len >> PAGE_SHIFT;
594 if (security_vm_enough_memory(charged))
595 return -ENOMEM;
596 vm_flags |= VM_ACCOUNT;
600 /* Can we just expand an old anonymous mapping? */
601 if (!file && !(vm_flags & VM_SHARED) && rb_parent)
602 if (vma_merge(mm, prev, rb_parent, addr, addr + len,
603 vm_flags, NULL, 0))
604 goto out;
607 * Determine the object being mapped and call the appropriate
608 * specific mapper. the address has already been validated, but
609 * not unmapped, but the maps are removed from the list.
611 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
612 error = -ENOMEM;
613 if (!vma)
614 goto unacct_error;
616 vma->vm_mm = mm;
617 vma->vm_start = addr;
618 vma->vm_end = addr + len;
619 vma->vm_flags = vm_flags;
620 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
621 vma->vm_ops = NULL;
622 vma->vm_pgoff = pgoff;
623 vma->vm_file = NULL;
624 vma->vm_private_data = NULL;
625 vma->vm_next = NULL;
626 INIT_LIST_HEAD(&vma->shared);
628 if (file) {
629 error = -EINVAL;
630 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
631 goto free_vma;
632 if (vm_flags & VM_DENYWRITE) {
633 error = deny_write_access(file);
634 if (error)
635 goto free_vma;
636 correct_wcount = 1;
638 vma->vm_file = file;
639 get_file(file);
640 error = file->f_op->mmap(file, vma);
641 if (error)
642 goto unmap_and_free_vma;
643 } else if (vm_flags & VM_SHARED) {
644 error = shmem_zero_setup(vma);
645 if (error)
646 goto free_vma;
649 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
650 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
651 * that memory reservation must be checked; but that reservation
652 * belongs to shared memory object, not to vma: so now clear it.
654 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
655 vma->vm_flags &= ~VM_ACCOUNT;
657 /* Can addr have changed??
659 * Answer: Yes, several device drivers can do it in their
660 * f_op->mmap method. -DaveM
662 addr = vma->vm_start;
664 if (!file || !rb_parent || !vma_merge(mm, prev, rb_parent, addr,
665 addr + len, vma->vm_flags, file, pgoff)) {
666 vma_link(mm, vma, prev, rb_link, rb_parent);
667 if (correct_wcount)
668 atomic_inc(&inode->i_writecount);
669 } else {
670 if (file) {
671 if (correct_wcount)
672 atomic_inc(&inode->i_writecount);
673 fput(file);
675 kmem_cache_free(vm_area_cachep, vma);
677 out:
678 mm->total_vm += len >> PAGE_SHIFT;
679 if (vm_flags & VM_LOCKED) {
680 mm->locked_vm += len >> PAGE_SHIFT;
681 make_pages_present(addr, addr + len);
683 if (flags & MAP_POPULATE) {
684 up_write(&mm->mmap_sem);
685 sys_remap_file_pages(addr, len, prot,
686 pgoff, flags & MAP_NONBLOCK);
687 down_write(&mm->mmap_sem);
689 return addr;
691 unmap_and_free_vma:
692 if (correct_wcount)
693 atomic_inc(&inode->i_writecount);
694 vma->vm_file = NULL;
695 fput(file);
697 /* Undo any partial mapping done by a device driver. */
698 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start);
699 free_vma:
700 kmem_cache_free(vm_area_cachep, vma);
701 unacct_error:
702 if (charged)
703 vm_unacct_memory(charged);
704 return error;
707 /* Get an address range which is currently unmapped.
708 * For shmat() with addr=0.
710 * Ugly calling convention alert:
711 * Return value with the low bits set means error value,
712 * ie
713 * if (ret & ~PAGE_MASK)
714 * error = ret;
716 * This function "knows" that -ENOMEM has the bits set.
718 #ifndef HAVE_ARCH_UNMAPPED_AREA
719 static inline unsigned long
720 arch_get_unmapped_area(struct file *filp, unsigned long addr,
721 unsigned long len, unsigned long pgoff, unsigned long flags)
723 struct mm_struct *mm = current->mm;
724 struct vm_area_struct *vma;
725 unsigned long start_addr;
727 if (len > TASK_SIZE)
728 return -ENOMEM;
730 if (addr) {
731 addr = PAGE_ALIGN(addr);
732 vma = find_vma(mm, addr);
733 if (TASK_SIZE - len >= addr &&
734 (!vma || addr + len <= vma->vm_start))
735 return addr;
737 start_addr = addr = mm->free_area_cache;
739 full_search:
740 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
741 /* At this point: (!vma || addr < vma->vm_end). */
742 if (TASK_SIZE - len < addr) {
744 * Start a new search - just in case we missed
745 * some holes.
747 if (start_addr != TASK_UNMAPPED_BASE) {
748 start_addr = addr = TASK_UNMAPPED_BASE;
749 goto full_search;
751 return -ENOMEM;
753 if (!vma || addr + len <= vma->vm_start) {
755 * Remember the place where we stopped the search:
757 mm->free_area_cache = addr + len;
758 return addr;
760 addr = vma->vm_end;
763 #else
764 extern unsigned long
765 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
766 unsigned long, unsigned long);
767 #endif
769 unsigned long
770 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
771 unsigned long pgoff, unsigned long flags)
773 if (flags & MAP_FIXED) {
774 unsigned long ret;
776 if (addr > TASK_SIZE - len)
777 return -ENOMEM;
778 if (addr & ~PAGE_MASK)
779 return -EINVAL;
780 if (file && is_file_hugepages(file)) {
782 * Make sure that addr and length are properly aligned.
784 ret = is_aligned_hugepage_range(addr, len);
785 } else {
787 * Ensure that a normal request is not falling in a
788 * reserved hugepage range. For some archs like IA-64,
789 * there is a separate region for hugepages.
791 ret = is_hugepage_only_range(addr, len);
793 if (ret)
794 return -EINVAL;
795 return addr;
798 if (file && file->f_op && file->f_op->get_unmapped_area)
799 return file->f_op->get_unmapped_area(file, addr, len,
800 pgoff, flags);
802 return arch_get_unmapped_area(file, addr, len, pgoff, flags);
805 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
806 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
808 struct vm_area_struct *vma = NULL;
810 if (mm) {
811 /* Check the cache first. */
812 /* (Cache hit rate is typically around 35%.) */
813 vma = mm->mmap_cache;
814 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
815 struct rb_node * rb_node;
817 rb_node = mm->mm_rb.rb_node;
818 vma = NULL;
820 while (rb_node) {
821 struct vm_area_struct * vma_tmp;
823 vma_tmp = rb_entry(rb_node,
824 struct vm_area_struct, vm_rb);
826 if (vma_tmp->vm_end > addr) {
827 vma = vma_tmp;
828 if (vma_tmp->vm_start <= addr)
829 break;
830 rb_node = rb_node->rb_left;
831 } else
832 rb_node = rb_node->rb_right;
834 if (vma)
835 mm->mmap_cache = vma;
838 return vma;
841 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
842 struct vm_area_struct *
843 find_vma_prev(struct mm_struct *mm, unsigned long addr,
844 struct vm_area_struct **pprev)
846 struct vm_area_struct *vma = NULL, *prev = NULL;
847 struct rb_node * rb_node;
848 if (!mm)
849 goto out;
851 /* Guard against addr being lower than the first VMA */
852 vma = mm->mmap;
854 /* Go through the RB tree quickly. */
855 rb_node = mm->mm_rb.rb_node;
857 while (rb_node) {
858 struct vm_area_struct *vma_tmp;
859 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
861 if (addr < vma_tmp->vm_end) {
862 rb_node = rb_node->rb_left;
863 } else {
864 prev = vma_tmp;
865 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
866 break;
867 rb_node = rb_node->rb_right;
871 out:
872 *pprev = prev;
873 return prev ? prev->vm_next : vma;
876 #ifdef CONFIG_STACK_GROWSUP
878 * vma is the first one with address > vma->vm_end. Have to extend vma.
880 int expand_stack(struct vm_area_struct * vma, unsigned long address)
882 unsigned long grow;
884 if (!(vma->vm_flags & VM_GROWSUP))
885 return -EFAULT;
888 * vma->vm_start/vm_end cannot change under us because the caller
889 * is required to hold the mmap_sem in read mode. We need to get
890 * the spinlock only before relocating the vma range ourself.
892 address += 4 + PAGE_SIZE - 1;
893 address &= PAGE_MASK;
894 spin_lock(&vma->vm_mm->page_table_lock);
895 grow = (address - vma->vm_end) >> PAGE_SHIFT;
897 /* Overcommit.. */
898 if (security_vm_enough_memory(grow)) {
899 spin_unlock(&vma->vm_mm->page_table_lock);
900 return -ENOMEM;
903 if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
904 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
905 current->rlim[RLIMIT_AS].rlim_cur) {
906 spin_unlock(&vma->vm_mm->page_table_lock);
907 vm_unacct_memory(grow);
908 return -ENOMEM;
910 vma->vm_end = address;
911 vma->vm_mm->total_vm += grow;
912 if (vma->vm_flags & VM_LOCKED)
913 vma->vm_mm->locked_vm += grow;
914 spin_unlock(&vma->vm_mm->page_table_lock);
915 return 0;
918 struct vm_area_struct *
919 find_extend_vma(struct mm_struct *mm, unsigned long addr)
921 struct vm_area_struct *vma, *prev;
923 addr &= PAGE_MASK;
924 vma = find_vma_prev(mm, addr, &prev);
925 if (vma && (vma->vm_start <= addr))
926 return vma;
927 if (!prev || expand_stack(prev, addr))
928 return NULL;
929 if (prev->vm_flags & VM_LOCKED) {
930 make_pages_present(addr, prev->vm_end);
932 return prev;
934 #else
936 * vma is the first one with address < vma->vm_start. Have to extend vma.
938 int expand_stack(struct vm_area_struct *vma, unsigned long address)
940 unsigned long grow;
943 * vma->vm_start/vm_end cannot change under us because the caller
944 * is required to hold the mmap_sem in read mode. We need to get
945 * the spinlock only before relocating the vma range ourself.
947 address &= PAGE_MASK;
948 spin_lock(&vma->vm_mm->page_table_lock);
949 grow = (vma->vm_start - address) >> PAGE_SHIFT;
951 /* Overcommit.. */
952 if (security_vm_enough_memory(grow)) {
953 spin_unlock(&vma->vm_mm->page_table_lock);
954 return -ENOMEM;
957 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
958 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
959 current->rlim[RLIMIT_AS].rlim_cur) {
960 spin_unlock(&vma->vm_mm->page_table_lock);
961 vm_unacct_memory(grow);
962 return -ENOMEM;
964 vma->vm_start = address;
965 vma->vm_pgoff -= grow;
966 vma->vm_mm->total_vm += grow;
967 if (vma->vm_flags & VM_LOCKED)
968 vma->vm_mm->locked_vm += grow;
969 spin_unlock(&vma->vm_mm->page_table_lock);
970 return 0;
973 struct vm_area_struct *
974 find_extend_vma(struct mm_struct * mm, unsigned long addr)
976 struct vm_area_struct * vma;
977 unsigned long start;
979 addr &= PAGE_MASK;
980 vma = find_vma(mm,addr);
981 if (!vma)
982 return NULL;
983 if (vma->vm_start <= addr)
984 return vma;
985 if (!(vma->vm_flags & VM_GROWSDOWN))
986 return NULL;
987 start = vma->vm_start;
988 if (expand_stack(vma, addr))
989 return NULL;
990 if (vma->vm_flags & VM_LOCKED) {
991 make_pages_present(addr, start);
993 return vma;
995 #endif
998 * Try to free as many page directory entries as we can,
999 * without having to work very hard at actually scanning
1000 * the page tables themselves.
1002 * Right now we try to free page tables if we have a nice
1003 * PGDIR-aligned area that got free'd up. We could be more
1004 * granular if we want to, but this is fast and simple,
1005 * and covers the bad cases.
1007 * "prev", if it exists, points to a vma before the one
1008 * we just free'd - but there's no telling how much before.
1010 static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
1011 unsigned long start, unsigned long end)
1013 unsigned long first = start & PGDIR_MASK;
1014 unsigned long last = end + PGDIR_SIZE - 1;
1015 unsigned long start_index, end_index;
1016 struct mm_struct *mm = tlb->mm;
1018 if (!prev) {
1019 prev = mm->mmap;
1020 if (!prev)
1021 goto no_mmaps;
1022 if (prev->vm_end > start) {
1023 if (last > prev->vm_start)
1024 last = prev->vm_start;
1025 goto no_mmaps;
1028 for (;;) {
1029 struct vm_area_struct *next = prev->vm_next;
1031 if (next) {
1032 if (next->vm_start < start) {
1033 prev = next;
1034 continue;
1036 if (last > next->vm_start)
1037 last = next->vm_start;
1039 if (prev->vm_end > first)
1040 first = prev->vm_end + PGDIR_SIZE - 1;
1041 break;
1043 no_mmaps:
1044 if (last < first) /* for arches with discontiguous pgd indices */
1045 return;
1047 * If the PGD bits are not consecutive in the virtual address, the
1048 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1050 start_index = pgd_index(first);
1051 if (start_index < FIRST_USER_PGD_NR)
1052 start_index = FIRST_USER_PGD_NR;
1053 end_index = pgd_index(last);
1054 if (end_index > start_index) {
1055 clear_page_tables(tlb, start_index, end_index - start_index);
1056 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
1060 /* Normal function to fix up a mapping
1061 * This function is the default for when an area has no specific
1062 * function. This may be used as part of a more specific routine.
1064 * By the time this function is called, the area struct has been
1065 * removed from the process mapping list.
1067 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1069 size_t len = area->vm_end - area->vm_start;
1071 area->vm_mm->total_vm -= len >> PAGE_SHIFT;
1072 if (area->vm_flags & VM_LOCKED)
1073 area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1075 * Is this a new hole at the lowest possible address?
1077 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1078 area->vm_start < area->vm_mm->free_area_cache)
1079 area->vm_mm->free_area_cache = area->vm_start;
1081 remove_shared_vm_struct(area);
1083 if (area->vm_ops && area->vm_ops->close)
1084 area->vm_ops->close(area);
1085 if (area->vm_file)
1086 fput(area->vm_file);
1087 kmem_cache_free(vm_area_cachep, area);
1091 * Update the VMA and inode share lists.
1093 * Ok - we have the memory areas we should free on the 'free' list,
1094 * so release them, and do the vma updates.
1096 static void unmap_vma_list(struct mm_struct *mm,
1097 struct vm_area_struct *mpnt)
1099 do {
1100 struct vm_area_struct *next = mpnt->vm_next;
1101 unmap_vma(mm, mpnt);
1102 mpnt = next;
1103 } while (mpnt != NULL);
1104 validate_mm(mm);
1108 * Get rid of page table information in the indicated region.
1110 * Called with the page table lock held.
1112 static void unmap_region(struct mm_struct *mm,
1113 struct vm_area_struct *vma,
1114 struct vm_area_struct *prev,
1115 unsigned long start,
1116 unsigned long end)
1118 struct mmu_gather *tlb;
1119 unsigned long nr_accounted = 0;
1121 lru_add_drain();
1122 tlb = tlb_gather_mmu(mm, 0);
1123 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted);
1124 vm_unacct_memory(nr_accounted);
1125 free_pgtables(tlb, prev, start, end);
1126 tlb_finish_mmu(tlb, start, end);
1130 * Create a list of vma's touched by the unmap, removing them from the mm's
1131 * vma list as we go..
1133 * Called with the page_table_lock held.
1135 static void
1136 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1137 struct vm_area_struct *prev, unsigned long end)
1139 struct vm_area_struct **insertion_point;
1140 struct vm_area_struct *tail_vma = NULL;
1142 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1143 do {
1144 rb_erase(&vma->vm_rb, &mm->mm_rb);
1145 mm->map_count--;
1146 tail_vma = vma;
1147 vma = vma->vm_next;
1148 } while (vma && vma->vm_start < end);
1149 *insertion_point = vma;
1150 tail_vma->vm_next = NULL;
1151 mm->mmap_cache = NULL; /* Kill the cache. */
1155 * Split a vma into two pieces at address 'addr', a new vma is allocated
1156 * either for the first part or the the tail.
1158 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1159 unsigned long addr, int new_below)
1161 struct vm_area_struct *new;
1163 if (mm->map_count >= MAX_MAP_COUNT)
1164 return -ENOMEM;
1166 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1167 if (!new)
1168 return -ENOMEM;
1170 /* most fields are the same, copy all, and then fixup */
1171 *new = *vma;
1173 INIT_LIST_HEAD(&new->shared);
1175 if (new_below) {
1176 new->vm_end = addr;
1177 vma->vm_start = addr;
1178 vma->vm_pgoff += ((addr - new->vm_start) >> PAGE_SHIFT);
1179 } else {
1180 vma->vm_end = addr;
1181 new->vm_start = addr;
1182 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1185 if (new->vm_file)
1186 get_file(new->vm_file);
1188 if (new->vm_ops && new->vm_ops->open)
1189 new->vm_ops->open(new);
1191 insert_vm_struct(mm, new);
1192 return 0;
1195 /* Munmap is split into 2 main parts -- this part which finds
1196 * what needs doing, and the areas themselves, which do the
1197 * work. This now handles partial unmappings.
1198 * Jeremy Fitzhardinge <jeremy@goop.org>
1200 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1202 unsigned long end;
1203 struct vm_area_struct *mpnt, *prev, *last;
1205 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1206 return -EINVAL;
1208 if ((len = PAGE_ALIGN(len)) == 0)
1209 return -EINVAL;
1211 /* Find the first overlapping VMA */
1212 mpnt = find_vma_prev(mm, start, &prev);
1213 if (!mpnt)
1214 return 0;
1215 /* we have start < mpnt->vm_end */
1217 if (is_vm_hugetlb_page(mpnt)) {
1218 int ret = is_aligned_hugepage_range(start, len);
1220 if (ret)
1221 return ret;
1224 /* if it doesn't overlap, we have nothing.. */
1225 end = start + len;
1226 if (mpnt->vm_start >= end)
1227 return 0;
1229 /* Something will probably happen, so notify. */
1230 if (mpnt->vm_file && (mpnt->vm_flags & VM_EXEC))
1231 profile_exec_unmap(mm);
1234 * If we need to split any vma, do it now to save pain later.
1236 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1237 * unmapped vm_area_struct will remain in use: so lower split_vma
1238 * places tmp vma above, and higher split_vma places tmp vma below.
1240 if (start > mpnt->vm_start) {
1241 if (split_vma(mm, mpnt, start, 0))
1242 return -ENOMEM;
1243 prev = mpnt;
1246 /* Does it split the last one? */
1247 last = find_vma(mm, end);
1248 if (last && end > last->vm_start) {
1249 if (split_vma(mm, last, end, 1))
1250 return -ENOMEM;
1252 mpnt = prev? prev->vm_next: mm->mmap;
1255 * Remove the vma's, and unmap the actual pages
1257 spin_lock(&mm->page_table_lock);
1258 detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1259 unmap_region(mm, mpnt, prev, start, end);
1260 spin_unlock(&mm->page_table_lock);
1262 /* Fix up all other VM information */
1263 unmap_vma_list(mm, mpnt);
1265 return 0;
1268 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1270 int ret;
1271 struct mm_struct *mm = current->mm;
1273 down_write(&mm->mmap_sem);
1274 ret = do_munmap(mm, addr, len);
1275 up_write(&mm->mmap_sem);
1276 return ret;
1280 * this is really a simplified "do_mmap". it only handles
1281 * anonymous maps. eventually we may be able to do some
1282 * brk-specific accounting here.
1284 unsigned long do_brk(unsigned long addr, unsigned long len)
1286 struct mm_struct * mm = current->mm;
1287 struct vm_area_struct * vma, * prev;
1288 unsigned long flags;
1289 struct rb_node ** rb_link, * rb_parent;
1291 len = PAGE_ALIGN(len);
1292 if (!len)
1293 return addr;
1296 * mlock MCL_FUTURE?
1298 if (mm->def_flags & VM_LOCKED) {
1299 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
1300 locked += len;
1301 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
1302 return -EAGAIN;
1306 * Clear old maps. this also does some error checking for us
1308 munmap_back:
1309 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1310 if (vma && vma->vm_start < addr + len) {
1311 if (do_munmap(mm, addr, len))
1312 return -ENOMEM;
1313 goto munmap_back;
1316 /* Check against address space limits *after* clearing old maps... */
1317 if ((mm->total_vm << PAGE_SHIFT) + len
1318 > current->rlim[RLIMIT_AS].rlim_cur)
1319 return -ENOMEM;
1321 if (mm->map_count > MAX_MAP_COUNT)
1322 return -ENOMEM;
1324 if (security_vm_enough_memory(len >> PAGE_SHIFT))
1325 return -ENOMEM;
1327 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1329 /* Can we just expand an old anonymous mapping? */
1330 if (rb_parent && vma_merge(mm, prev, rb_parent, addr, addr + len,
1331 flags, NULL, 0))
1332 goto out;
1335 * create a vma struct for an anonymous mapping
1337 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1338 if (!vma) {
1339 vm_unacct_memory(len >> PAGE_SHIFT);
1340 return -ENOMEM;
1343 vma->vm_mm = mm;
1344 vma->vm_start = addr;
1345 vma->vm_end = addr + len;
1346 vma->vm_flags = flags;
1347 vma->vm_page_prot = protection_map[flags & 0x0f];
1348 vma->vm_ops = NULL;
1349 vma->vm_pgoff = 0;
1350 vma->vm_file = NULL;
1351 vma->vm_private_data = NULL;
1352 INIT_LIST_HEAD(&vma->shared);
1354 vma_link(mm, vma, prev, rb_link, rb_parent);
1356 out:
1357 mm->total_vm += len >> PAGE_SHIFT;
1358 if (flags & VM_LOCKED) {
1359 mm->locked_vm += len >> PAGE_SHIFT;
1360 make_pages_present(addr, addr + len);
1362 return addr;
1365 /* Build the RB tree corresponding to the VMA list. */
1366 void build_mmap_rb(struct mm_struct * mm)
1368 struct vm_area_struct * vma;
1369 struct rb_node ** rb_link, * rb_parent;
1371 mm->mm_rb = RB_ROOT;
1372 rb_link = &mm->mm_rb.rb_node;
1373 rb_parent = NULL;
1374 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1375 __vma_link_rb(mm, vma, rb_link, rb_parent);
1376 rb_parent = &vma->vm_rb;
1377 rb_link = &rb_parent->rb_right;
1381 /* Release all mmaps. */
1382 void exit_mmap(struct mm_struct *mm)
1384 struct mmu_gather *tlb;
1385 struct vm_area_struct *vma;
1386 unsigned long nr_accounted = 0;
1388 profile_exit_mmap(mm);
1390 lru_add_drain();
1392 spin_lock(&mm->page_table_lock);
1394 tlb = tlb_gather_mmu(mm, 1);
1395 flush_cache_mm(mm);
1396 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1397 mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1398 ~0UL, &nr_accounted);
1399 vm_unacct_memory(nr_accounted);
1400 BUG_ON(mm->map_count); /* This is just debugging */
1401 clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1402 tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1404 vma = mm->mmap;
1405 mm->mmap = mm->mmap_cache = NULL;
1406 mm->mm_rb = RB_ROOT;
1407 mm->rss = 0;
1408 mm->total_vm = 0;
1409 mm->locked_vm = 0;
1411 spin_unlock(&mm->page_table_lock);
1414 * Walk the list again, actually closing and freeing it
1415 * without holding any MM locks.
1417 while (vma) {
1418 struct vm_area_struct *next = vma->vm_next;
1419 remove_shared_vm_struct(vma);
1420 if (vma->vm_ops) {
1421 if (vma->vm_ops->close)
1422 vma->vm_ops->close(vma);
1424 if (vma->vm_file)
1425 fput(vma->vm_file);
1426 kmem_cache_free(vm_area_cachep, vma);
1427 vma = next;
1431 /* Insert vm structure into process list sorted by address
1432 * and into the inode's i_mmap ring. If vm_file is non-NULL
1433 * then i_shared_sem is taken here.
1435 void insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1437 struct vm_area_struct * __vma, * prev;
1438 struct rb_node ** rb_link, * rb_parent;
1440 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
1441 if (__vma && __vma->vm_start < vma->vm_end)
1442 BUG();
1443 vma_link(mm, vma, prev, rb_link, rb_parent);
1444 validate_mm(mm);