thinkpad-acpi: issue backlight class events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / mmap.c
blob707ce0f74e6edb3e796682b0a67da9b3db1824f9
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/backing-dev.h>
11 #include <linux/mm.h>
12 #include <linux/shm.h>
13 #include <linux/mman.h>
14 #include <linux/pagemap.h>
15 #include <linux/swap.h>
16 #include <linux/syscalls.h>
17 #include <linux/capability.h>
18 #include <linux/init.h>
19 #include <linux/file.h>
20 #include <linux/fs.h>
21 #include <linux/personality.h>
22 #include <linux/security.h>
23 #include <linux/hugetlb.h>
24 #include <linux/profile.h>
25 #include <linux/module.h>
26 #include <linux/mount.h>
27 #include <linux/mempolicy.h>
28 #include <linux/rmap.h>
29 #include <linux/mmu_notifier.h>
31 #include <asm/uaccess.h>
32 #include <asm/cacheflush.h>
33 #include <asm/tlb.h>
34 #include <asm/mmu_context.h>
36 #include "internal.h"
38 #ifndef arch_mmap_check
39 #define arch_mmap_check(addr, len, flags) (0)
40 #endif
42 #ifndef arch_rebalance_pgtables
43 #define arch_rebalance_pgtables(addr, len) (addr)
44 #endif
46 static void unmap_region(struct mm_struct *mm,
47 struct vm_area_struct *vma, struct vm_area_struct *prev,
48 unsigned long start, unsigned long end);
51 * WARNING: the debugging will use recursive algorithms so never enable this
52 * unless you know what you are doing.
54 #undef DEBUG_MM_RB
56 /* description of effects of mapping type and prot in current implementation.
57 * this is due to the limited x86 page protection hardware. The expected
58 * behavior is in parens:
60 * map_type prot
61 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
62 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
63 * w: (no) no w: (no) no w: (yes) yes w: (no) no
64 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
66 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
67 * w: (no) no w: (no) no w: (copy) copy w: (no) no
68 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
71 pgprot_t protection_map[16] = {
72 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
73 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
76 pgprot_t vm_get_page_prot(unsigned long vm_flags)
78 return __pgprot(pgprot_val(protection_map[vm_flags &
79 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
80 pgprot_val(arch_vm_get_page_prot(vm_flags)));
82 EXPORT_SYMBOL(vm_get_page_prot);
84 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
85 int sysctl_overcommit_ratio = 50; /* default is 50% */
86 int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
87 atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
89 /* amount of vm to protect from userspace access */
90 unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
93 * Check that a process has enough memory to allocate a new virtual
94 * mapping. 0 means there is enough memory for the allocation to
95 * succeed and -ENOMEM implies there is not.
97 * We currently support three overcommit policies, which are set via the
98 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
100 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
101 * Additional code 2002 Jul 20 by Robert Love.
103 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
105 * Note this is a helper function intended to be used by LSMs which
106 * wish to use this logic.
108 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
110 unsigned long free, allowed;
112 vm_acct_memory(pages);
115 * Sometimes we want to use more memory than we have
117 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
118 return 0;
120 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
121 unsigned long n;
123 free = global_page_state(NR_FILE_PAGES);
124 free += nr_swap_pages;
127 * Any slabs which are created with the
128 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
129 * which are reclaimable, under pressure. The dentry
130 * cache and most inode caches should fall into this
132 free += global_page_state(NR_SLAB_RECLAIMABLE);
135 * Leave the last 3% for root
137 if (!cap_sys_admin)
138 free -= free / 32;
140 if (free > pages)
141 return 0;
144 * nr_free_pages() is very expensive on large systems,
145 * only call if we're about to fail.
147 n = nr_free_pages();
150 * Leave reserved pages. The pages are not for anonymous pages.
152 if (n <= totalreserve_pages)
153 goto error;
154 else
155 n -= totalreserve_pages;
158 * Leave the last 3% for root
160 if (!cap_sys_admin)
161 n -= n / 32;
162 free += n;
164 if (free > pages)
165 return 0;
167 goto error;
170 allowed = (totalram_pages - hugetlb_total_pages())
171 * sysctl_overcommit_ratio / 100;
173 * Leave the last 3% for root
175 if (!cap_sys_admin)
176 allowed -= allowed / 32;
177 allowed += total_swap_pages;
179 /* Don't let a single process grow too big:
180 leave 3% of the size of this process for other processes */
181 if (mm)
182 allowed -= mm->total_vm / 32;
185 * cast `allowed' as a signed long because vm_committed_space
186 * sometimes has a negative value
188 if (atomic_long_read(&vm_committed_space) < (long)allowed)
189 return 0;
190 error:
191 vm_unacct_memory(pages);
193 return -ENOMEM;
197 * Requires inode->i_mapping->i_mmap_lock
199 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
200 struct file *file, struct address_space *mapping)
202 if (vma->vm_flags & VM_DENYWRITE)
203 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
204 if (vma->vm_flags & VM_SHARED)
205 mapping->i_mmap_writable--;
207 flush_dcache_mmap_lock(mapping);
208 if (unlikely(vma->vm_flags & VM_NONLINEAR))
209 list_del_init(&vma->shared.vm_set.list);
210 else
211 vma_prio_tree_remove(vma, &mapping->i_mmap);
212 flush_dcache_mmap_unlock(mapping);
216 * Unlink a file-based vm structure from its prio_tree, to hide
217 * vma from rmap and vmtruncate before freeing its page tables.
219 void unlink_file_vma(struct vm_area_struct *vma)
221 struct file *file = vma->vm_file;
223 if (file) {
224 struct address_space *mapping = file->f_mapping;
225 spin_lock(&mapping->i_mmap_lock);
226 __remove_shared_vm_struct(vma, file, mapping);
227 spin_unlock(&mapping->i_mmap_lock);
232 * Close a vm structure and free it, returning the next.
234 static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
236 struct vm_area_struct *next = vma->vm_next;
238 might_sleep();
239 if (vma->vm_ops && vma->vm_ops->close)
240 vma->vm_ops->close(vma);
241 if (vma->vm_file) {
242 fput(vma->vm_file);
243 if (vma->vm_flags & VM_EXECUTABLE)
244 removed_exe_file_vma(vma->vm_mm);
246 mpol_put(vma_policy(vma));
247 kmem_cache_free(vm_area_cachep, vma);
248 return next;
251 SYSCALL_DEFINE1(brk, unsigned long, brk)
253 unsigned long rlim, retval;
254 unsigned long newbrk, oldbrk;
255 struct mm_struct *mm = current->mm;
256 unsigned long min_brk;
258 down_write(&mm->mmap_sem);
260 #ifdef CONFIG_COMPAT_BRK
261 min_brk = mm->end_code;
262 #else
263 min_brk = mm->start_brk;
264 #endif
265 if (brk < min_brk)
266 goto out;
269 * Check against rlimit here. If this check is done later after the test
270 * of oldbrk with newbrk then it can escape the test and let the data
271 * segment grow beyond its set limit the in case where the limit is
272 * not page aligned -Ram Gupta
274 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
275 if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
276 (mm->end_data - mm->start_data) > rlim)
277 goto out;
279 newbrk = PAGE_ALIGN(brk);
280 oldbrk = PAGE_ALIGN(mm->brk);
281 if (oldbrk == newbrk)
282 goto set_brk;
284 /* Always allow shrinking brk. */
285 if (brk <= mm->brk) {
286 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
287 goto set_brk;
288 goto out;
291 /* Check against existing mmap mappings. */
292 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
293 goto out;
295 /* Ok, looks good - let it rip. */
296 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
297 goto out;
298 set_brk:
299 mm->brk = brk;
300 out:
301 retval = mm->brk;
302 up_write(&mm->mmap_sem);
303 return retval;
306 #ifdef DEBUG_MM_RB
307 static int browse_rb(struct rb_root *root)
309 int i = 0, j;
310 struct rb_node *nd, *pn = NULL;
311 unsigned long prev = 0, pend = 0;
313 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
314 struct vm_area_struct *vma;
315 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
316 if (vma->vm_start < prev)
317 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
318 if (vma->vm_start < pend)
319 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
320 if (vma->vm_start > vma->vm_end)
321 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
322 i++;
323 pn = nd;
324 prev = vma->vm_start;
325 pend = vma->vm_end;
327 j = 0;
328 for (nd = pn; nd; nd = rb_prev(nd)) {
329 j++;
331 if (i != j)
332 printk("backwards %d, forwards %d\n", j, i), i = 0;
333 return i;
336 void validate_mm(struct mm_struct *mm)
338 int bug = 0;
339 int i = 0;
340 struct vm_area_struct *tmp = mm->mmap;
341 while (tmp) {
342 tmp = tmp->vm_next;
343 i++;
345 if (i != mm->map_count)
346 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
347 i = browse_rb(&mm->mm_rb);
348 if (i != mm->map_count)
349 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
350 BUG_ON(bug);
352 #else
353 #define validate_mm(mm) do { } while (0)
354 #endif
356 static struct vm_area_struct *
357 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
358 struct vm_area_struct **pprev, struct rb_node ***rb_link,
359 struct rb_node ** rb_parent)
361 struct vm_area_struct * vma;
362 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
364 __rb_link = &mm->mm_rb.rb_node;
365 rb_prev = __rb_parent = NULL;
366 vma = NULL;
368 while (*__rb_link) {
369 struct vm_area_struct *vma_tmp;
371 __rb_parent = *__rb_link;
372 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
374 if (vma_tmp->vm_end > addr) {
375 vma = vma_tmp;
376 if (vma_tmp->vm_start <= addr)
377 break;
378 __rb_link = &__rb_parent->rb_left;
379 } else {
380 rb_prev = __rb_parent;
381 __rb_link = &__rb_parent->rb_right;
385 *pprev = NULL;
386 if (rb_prev)
387 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
388 *rb_link = __rb_link;
389 *rb_parent = __rb_parent;
390 return vma;
393 static inline void
394 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
395 struct vm_area_struct *prev, struct rb_node *rb_parent)
397 if (prev) {
398 vma->vm_next = prev->vm_next;
399 prev->vm_next = vma;
400 } else {
401 mm->mmap = vma;
402 if (rb_parent)
403 vma->vm_next = rb_entry(rb_parent,
404 struct vm_area_struct, vm_rb);
405 else
406 vma->vm_next = NULL;
410 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
411 struct rb_node **rb_link, struct rb_node *rb_parent)
413 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
414 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
417 static inline void __vma_link_file(struct vm_area_struct *vma)
419 struct file * file;
421 file = vma->vm_file;
422 if (file) {
423 struct address_space *mapping = file->f_mapping;
425 if (vma->vm_flags & VM_DENYWRITE)
426 atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
427 if (vma->vm_flags & VM_SHARED)
428 mapping->i_mmap_writable++;
430 flush_dcache_mmap_lock(mapping);
431 if (unlikely(vma->vm_flags & VM_NONLINEAR))
432 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
433 else
434 vma_prio_tree_insert(vma, &mapping->i_mmap);
435 flush_dcache_mmap_unlock(mapping);
439 static void
440 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
441 struct vm_area_struct *prev, struct rb_node **rb_link,
442 struct rb_node *rb_parent)
444 __vma_link_list(mm, vma, prev, rb_parent);
445 __vma_link_rb(mm, vma, rb_link, rb_parent);
446 __anon_vma_link(vma);
449 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
450 struct vm_area_struct *prev, struct rb_node **rb_link,
451 struct rb_node *rb_parent)
453 struct address_space *mapping = NULL;
455 if (vma->vm_file)
456 mapping = vma->vm_file->f_mapping;
458 if (mapping) {
459 spin_lock(&mapping->i_mmap_lock);
460 vma->vm_truncate_count = mapping->truncate_count;
462 anon_vma_lock(vma);
464 __vma_link(mm, vma, prev, rb_link, rb_parent);
465 __vma_link_file(vma);
467 anon_vma_unlock(vma);
468 if (mapping)
469 spin_unlock(&mapping->i_mmap_lock);
471 mm->map_count++;
472 validate_mm(mm);
476 * Helper for vma_adjust in the split_vma insert case:
477 * insert vm structure into list and rbtree and anon_vma,
478 * but it has already been inserted into prio_tree earlier.
480 static void
481 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
483 struct vm_area_struct * __vma, * prev;
484 struct rb_node ** rb_link, * rb_parent;
486 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
487 BUG_ON(__vma && __vma->vm_start < vma->vm_end);
488 __vma_link(mm, vma, prev, rb_link, rb_parent);
489 mm->map_count++;
492 static inline void
493 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
494 struct vm_area_struct *prev)
496 prev->vm_next = vma->vm_next;
497 rb_erase(&vma->vm_rb, &mm->mm_rb);
498 if (mm->mmap_cache == vma)
499 mm->mmap_cache = prev;
503 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
504 * is already present in an i_mmap tree without adjusting the tree.
505 * The following helper function should be used when such adjustments
506 * are necessary. The "insert" vma (if any) is to be inserted
507 * before we drop the necessary locks.
509 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
510 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
512 struct mm_struct *mm = vma->vm_mm;
513 struct vm_area_struct *next = vma->vm_next;
514 struct vm_area_struct *importer = NULL;
515 struct address_space *mapping = NULL;
516 struct prio_tree_root *root = NULL;
517 struct file *file = vma->vm_file;
518 struct anon_vma *anon_vma = NULL;
519 long adjust_next = 0;
520 int remove_next = 0;
522 if (next && !insert) {
523 if (end >= next->vm_end) {
525 * vma expands, overlapping all the next, and
526 * perhaps the one after too (mprotect case 6).
528 again: remove_next = 1 + (end > next->vm_end);
529 end = next->vm_end;
530 anon_vma = next->anon_vma;
531 importer = vma;
532 } else if (end > next->vm_start) {
534 * vma expands, overlapping part of the next:
535 * mprotect case 5 shifting the boundary up.
537 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
538 anon_vma = next->anon_vma;
539 importer = vma;
540 } else if (end < vma->vm_end) {
542 * vma shrinks, and !insert tells it's not
543 * split_vma inserting another: so it must be
544 * mprotect case 4 shifting the boundary down.
546 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
547 anon_vma = next->anon_vma;
548 importer = next;
552 if (file) {
553 mapping = file->f_mapping;
554 if (!(vma->vm_flags & VM_NONLINEAR))
555 root = &mapping->i_mmap;
556 spin_lock(&mapping->i_mmap_lock);
557 if (importer &&
558 vma->vm_truncate_count != next->vm_truncate_count) {
560 * unmap_mapping_range might be in progress:
561 * ensure that the expanding vma is rescanned.
563 importer->vm_truncate_count = 0;
565 if (insert) {
566 insert->vm_truncate_count = vma->vm_truncate_count;
568 * Put into prio_tree now, so instantiated pages
569 * are visible to arm/parisc __flush_dcache_page
570 * throughout; but we cannot insert into address
571 * space until vma start or end is updated.
573 __vma_link_file(insert);
578 * When changing only vma->vm_end, we don't really need
579 * anon_vma lock.
581 if (vma->anon_vma && (insert || importer || start != vma->vm_start))
582 anon_vma = vma->anon_vma;
583 if (anon_vma) {
584 spin_lock(&anon_vma->lock);
586 * Easily overlooked: when mprotect shifts the boundary,
587 * make sure the expanding vma has anon_vma set if the
588 * shrinking vma had, to cover any anon pages imported.
590 if (importer && !importer->anon_vma) {
591 importer->anon_vma = anon_vma;
592 __anon_vma_link(importer);
596 if (root) {
597 flush_dcache_mmap_lock(mapping);
598 vma_prio_tree_remove(vma, root);
599 if (adjust_next)
600 vma_prio_tree_remove(next, root);
603 vma->vm_start = start;
604 vma->vm_end = end;
605 vma->vm_pgoff = pgoff;
606 if (adjust_next) {
607 next->vm_start += adjust_next << PAGE_SHIFT;
608 next->vm_pgoff += adjust_next;
611 if (root) {
612 if (adjust_next)
613 vma_prio_tree_insert(next, root);
614 vma_prio_tree_insert(vma, root);
615 flush_dcache_mmap_unlock(mapping);
618 if (remove_next) {
620 * vma_merge has merged next into vma, and needs
621 * us to remove next before dropping the locks.
623 __vma_unlink(mm, next, vma);
624 if (file)
625 __remove_shared_vm_struct(next, file, mapping);
626 if (next->anon_vma)
627 __anon_vma_merge(vma, next);
628 } else if (insert) {
630 * split_vma has split insert from vma, and needs
631 * us to insert it before dropping the locks
632 * (it may either follow vma or precede it).
634 __insert_vm_struct(mm, insert);
637 if (anon_vma)
638 spin_unlock(&anon_vma->lock);
639 if (mapping)
640 spin_unlock(&mapping->i_mmap_lock);
642 if (remove_next) {
643 if (file) {
644 fput(file);
645 if (next->vm_flags & VM_EXECUTABLE)
646 removed_exe_file_vma(mm);
648 mm->map_count--;
649 mpol_put(vma_policy(next));
650 kmem_cache_free(vm_area_cachep, next);
652 * In mprotect's case 6 (see comments on vma_merge),
653 * we must remove another next too. It would clutter
654 * up the code too much to do both in one go.
656 if (remove_next == 2) {
657 next = vma->vm_next;
658 goto again;
662 validate_mm(mm);
666 * If the vma has a ->close operation then the driver probably needs to release
667 * per-vma resources, so we don't attempt to merge those.
669 #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP)
671 static inline int is_mergeable_vma(struct vm_area_struct *vma,
672 struct file *file, unsigned long vm_flags)
674 if (vma->vm_flags != vm_flags)
675 return 0;
676 if (vma->vm_file != file)
677 return 0;
678 if (vma->vm_ops && vma->vm_ops->close)
679 return 0;
680 return 1;
683 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
684 struct anon_vma *anon_vma2)
686 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
690 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
691 * in front of (at a lower virtual address and file offset than) the vma.
693 * We cannot merge two vmas if they have differently assigned (non-NULL)
694 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
696 * We don't check here for the merged mmap wrapping around the end of pagecache
697 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
698 * wrap, nor mmaps which cover the final page at index -1UL.
700 static int
701 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
702 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
704 if (is_mergeable_vma(vma, file, vm_flags) &&
705 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
706 if (vma->vm_pgoff == vm_pgoff)
707 return 1;
709 return 0;
713 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
714 * beyond (at a higher virtual address and file offset than) the vma.
716 * We cannot merge two vmas if they have differently assigned (non-NULL)
717 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
719 static int
720 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
721 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
723 if (is_mergeable_vma(vma, file, vm_flags) &&
724 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
725 pgoff_t vm_pglen;
726 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
727 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
728 return 1;
730 return 0;
734 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
735 * whether that can be merged with its predecessor or its successor.
736 * Or both (it neatly fills a hole).
738 * In most cases - when called for mmap, brk or mremap - [addr,end) is
739 * certain not to be mapped by the time vma_merge is called; but when
740 * called for mprotect, it is certain to be already mapped (either at
741 * an offset within prev, or at the start of next), and the flags of
742 * this area are about to be changed to vm_flags - and the no-change
743 * case has already been eliminated.
745 * The following mprotect cases have to be considered, where AAAA is
746 * the area passed down from mprotect_fixup, never extending beyond one
747 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
749 * AAAA AAAA AAAA AAAA
750 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
751 * cannot merge might become might become might become
752 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
753 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
754 * mremap move: PPPPNNNNNNNN 8
755 * AAAA
756 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
757 * might become case 1 below case 2 below case 3 below
759 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
760 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
762 struct vm_area_struct *vma_merge(struct mm_struct *mm,
763 struct vm_area_struct *prev, unsigned long addr,
764 unsigned long end, unsigned long vm_flags,
765 struct anon_vma *anon_vma, struct file *file,
766 pgoff_t pgoff, struct mempolicy *policy)
768 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
769 struct vm_area_struct *area, *next;
772 * We later require that vma->vm_flags == vm_flags,
773 * so this tests vma->vm_flags & VM_SPECIAL, too.
775 if (vm_flags & VM_SPECIAL)
776 return NULL;
778 if (prev)
779 next = prev->vm_next;
780 else
781 next = mm->mmap;
782 area = next;
783 if (next && next->vm_end == end) /* cases 6, 7, 8 */
784 next = next->vm_next;
787 * Can it merge with the predecessor?
789 if (prev && prev->vm_end == addr &&
790 mpol_equal(vma_policy(prev), policy) &&
791 can_vma_merge_after(prev, vm_flags,
792 anon_vma, file, pgoff)) {
794 * OK, it can. Can we now merge in the successor as well?
796 if (next && end == next->vm_start &&
797 mpol_equal(policy, vma_policy(next)) &&
798 can_vma_merge_before(next, vm_flags,
799 anon_vma, file, pgoff+pglen) &&
800 is_mergeable_anon_vma(prev->anon_vma,
801 next->anon_vma)) {
802 /* cases 1, 6 */
803 vma_adjust(prev, prev->vm_start,
804 next->vm_end, prev->vm_pgoff, NULL);
805 } else /* cases 2, 5, 7 */
806 vma_adjust(prev, prev->vm_start,
807 end, prev->vm_pgoff, NULL);
808 return prev;
812 * Can this new request be merged in front of next?
814 if (next && end == next->vm_start &&
815 mpol_equal(policy, vma_policy(next)) &&
816 can_vma_merge_before(next, vm_flags,
817 anon_vma, file, pgoff+pglen)) {
818 if (prev && addr < prev->vm_end) /* case 4 */
819 vma_adjust(prev, prev->vm_start,
820 addr, prev->vm_pgoff, NULL);
821 else /* cases 3, 8 */
822 vma_adjust(area, addr, next->vm_end,
823 next->vm_pgoff - pglen, NULL);
824 return area;
827 return NULL;
831 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
832 * neighbouring vmas for a suitable anon_vma, before it goes off
833 * to allocate a new anon_vma. It checks because a repetitive
834 * sequence of mprotects and faults may otherwise lead to distinct
835 * anon_vmas being allocated, preventing vma merge in subsequent
836 * mprotect.
838 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
840 struct vm_area_struct *near;
841 unsigned long vm_flags;
843 near = vma->vm_next;
844 if (!near)
845 goto try_prev;
848 * Since only mprotect tries to remerge vmas, match flags
849 * which might be mprotected into each other later on.
850 * Neither mlock nor madvise tries to remerge at present,
851 * so leave their flags as obstructing a merge.
853 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
854 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
856 if (near->anon_vma && vma->vm_end == near->vm_start &&
857 mpol_equal(vma_policy(vma), vma_policy(near)) &&
858 can_vma_merge_before(near, vm_flags,
859 NULL, vma->vm_file, vma->vm_pgoff +
860 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
861 return near->anon_vma;
862 try_prev:
864 * It is potentially slow to have to call find_vma_prev here.
865 * But it's only on the first write fault on the vma, not
866 * every time, and we could devise a way to avoid it later
867 * (e.g. stash info in next's anon_vma_node when assigning
868 * an anon_vma, or when trying vma_merge). Another time.
870 BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
871 if (!near)
872 goto none;
874 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
875 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
877 if (near->anon_vma && near->vm_end == vma->vm_start &&
878 mpol_equal(vma_policy(near), vma_policy(vma)) &&
879 can_vma_merge_after(near, vm_flags,
880 NULL, vma->vm_file, vma->vm_pgoff))
881 return near->anon_vma;
882 none:
884 * There's no absolute need to look only at touching neighbours:
885 * we could search further afield for "compatible" anon_vmas.
886 * But it would probably just be a waste of time searching,
887 * or lead to too many vmas hanging off the same anon_vma.
888 * We're trying to allow mprotect remerging later on,
889 * not trying to minimize memory used for anon_vmas.
891 return NULL;
894 #ifdef CONFIG_PROC_FS
895 void vm_stat_account(struct mm_struct *mm, unsigned long flags,
896 struct file *file, long pages)
898 const unsigned long stack_flags
899 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
901 if (file) {
902 mm->shared_vm += pages;
903 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
904 mm->exec_vm += pages;
905 } else if (flags & stack_flags)
906 mm->stack_vm += pages;
907 if (flags & (VM_RESERVED|VM_IO))
908 mm->reserved_vm += pages;
910 #endif /* CONFIG_PROC_FS */
913 * The caller must hold down_write(current->mm->mmap_sem).
916 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
917 unsigned long len, unsigned long prot,
918 unsigned long flags, unsigned long pgoff)
920 struct mm_struct * mm = current->mm;
921 struct inode *inode;
922 unsigned int vm_flags;
923 int error;
924 int accountable = 1;
925 unsigned long reqprot = prot;
928 * Does the application expect PROT_READ to imply PROT_EXEC?
930 * (the exception is when the underlying filesystem is noexec
931 * mounted, in which case we dont add PROT_EXEC.)
933 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
934 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
935 prot |= PROT_EXEC;
937 if (!len)
938 return -EINVAL;
940 if (!(flags & MAP_FIXED))
941 addr = round_hint_to_min(addr);
943 error = arch_mmap_check(addr, len, flags);
944 if (error)
945 return error;
947 /* Careful about overflows.. */
948 len = PAGE_ALIGN(len);
949 if (!len || len > TASK_SIZE)
950 return -ENOMEM;
952 /* offset overflow? */
953 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
954 return -EOVERFLOW;
956 /* Too many mappings? */
957 if (mm->map_count > sysctl_max_map_count)
958 return -ENOMEM;
960 /* Obtain the address to map to. we verify (or select) it and ensure
961 * that it represents a valid section of the address space.
963 addr = get_unmapped_area(file, addr, len, pgoff, flags);
964 if (addr & ~PAGE_MASK)
965 return addr;
967 /* Do simple checking here so the lower-level routines won't have
968 * to. we assume access permissions have been handled by the open
969 * of the memory object, so we don't do any here.
971 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
972 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
974 if (flags & MAP_LOCKED) {
975 if (!can_do_mlock())
976 return -EPERM;
977 vm_flags |= VM_LOCKED;
979 /* mlock MCL_FUTURE? */
980 if (vm_flags & VM_LOCKED) {
981 unsigned long locked, lock_limit;
982 locked = len >> PAGE_SHIFT;
983 locked += mm->locked_vm;
984 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
985 lock_limit >>= PAGE_SHIFT;
986 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
987 return -EAGAIN;
990 inode = file ? file->f_path.dentry->d_inode : NULL;
992 if (file) {
993 switch (flags & MAP_TYPE) {
994 case MAP_SHARED:
995 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
996 return -EACCES;
999 * Make sure we don't allow writing to an append-only
1000 * file..
1002 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1003 return -EACCES;
1006 * Make sure there are no mandatory locks on the file.
1008 if (locks_verify_locked(inode))
1009 return -EAGAIN;
1011 vm_flags |= VM_SHARED | VM_MAYSHARE;
1012 if (!(file->f_mode & FMODE_WRITE))
1013 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1015 /* fall through */
1016 case MAP_PRIVATE:
1017 if (!(file->f_mode & FMODE_READ))
1018 return -EACCES;
1019 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1020 if (vm_flags & VM_EXEC)
1021 return -EPERM;
1022 vm_flags &= ~VM_MAYEXEC;
1024 if (is_file_hugepages(file))
1025 accountable = 0;
1027 if (!file->f_op || !file->f_op->mmap)
1028 return -ENODEV;
1029 break;
1031 default:
1032 return -EINVAL;
1034 } else {
1035 switch (flags & MAP_TYPE) {
1036 case MAP_SHARED:
1038 * Ignore pgoff.
1040 pgoff = 0;
1041 vm_flags |= VM_SHARED | VM_MAYSHARE;
1042 break;
1043 case MAP_PRIVATE:
1045 * Set pgoff according to addr for anon_vma.
1047 pgoff = addr >> PAGE_SHIFT;
1048 break;
1049 default:
1050 return -EINVAL;
1054 error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1055 if (error)
1056 return error;
1058 return mmap_region(file, addr, len, flags, vm_flags, pgoff,
1059 accountable);
1061 EXPORT_SYMBOL(do_mmap_pgoff);
1064 * Some shared mappigns will want the pages marked read-only
1065 * to track write events. If so, we'll downgrade vm_page_prot
1066 * to the private version (using protection_map[] without the
1067 * VM_SHARED bit).
1069 int vma_wants_writenotify(struct vm_area_struct *vma)
1071 unsigned int vm_flags = vma->vm_flags;
1073 /* If it was private or non-writable, the write bit is already clear */
1074 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1075 return 0;
1077 /* The backer wishes to know when pages are first written to? */
1078 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1079 return 1;
1081 /* The open routine did something to the protections already? */
1082 if (pgprot_val(vma->vm_page_prot) !=
1083 pgprot_val(vm_get_page_prot(vm_flags)))
1084 return 0;
1086 /* Specialty mapping? */
1087 if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
1088 return 0;
1090 /* Can the mapping track the dirty pages? */
1091 return vma->vm_file && vma->vm_file->f_mapping &&
1092 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1095 unsigned long mmap_region(struct file *file, unsigned long addr,
1096 unsigned long len, unsigned long flags,
1097 unsigned int vm_flags, unsigned long pgoff,
1098 int accountable)
1100 struct mm_struct *mm = current->mm;
1101 struct vm_area_struct *vma, *prev;
1102 int correct_wcount = 0;
1103 int error;
1104 struct rb_node **rb_link, *rb_parent;
1105 unsigned long charged = 0;
1106 struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
1108 /* Clear old maps */
1109 error = -ENOMEM;
1110 munmap_back:
1111 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1112 if (vma && vma->vm_start < addr + len) {
1113 if (do_munmap(mm, addr, len))
1114 return -ENOMEM;
1115 goto munmap_back;
1118 /* Check against address space limit. */
1119 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1120 return -ENOMEM;
1122 if (flags & MAP_NORESERVE)
1123 vm_flags |= VM_NORESERVE;
1125 if (accountable && (!(flags & MAP_NORESERVE) ||
1126 sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
1127 if (vm_flags & VM_SHARED) {
1128 /* Check memory availability in shmem_file_setup? */
1129 vm_flags |= VM_ACCOUNT;
1130 } else if (vm_flags & VM_WRITE) {
1132 * Private writable mapping: check memory availability
1134 charged = len >> PAGE_SHIFT;
1135 if (security_vm_enough_memory(charged))
1136 return -ENOMEM;
1137 vm_flags |= VM_ACCOUNT;
1142 * Can we just expand an old private anonymous mapping?
1143 * The VM_SHARED test is necessary because shmem_zero_setup
1144 * will create the file object for a shared anonymous map below.
1146 if (!file && !(vm_flags & VM_SHARED) &&
1147 vma_merge(mm, prev, addr, addr + len, vm_flags,
1148 NULL, NULL, pgoff, NULL))
1149 goto out;
1152 * Determine the object being mapped and call the appropriate
1153 * specific mapper. the address has already been validated, but
1154 * not unmapped, but the maps are removed from the list.
1156 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1157 if (!vma) {
1158 error = -ENOMEM;
1159 goto unacct_error;
1162 vma->vm_mm = mm;
1163 vma->vm_start = addr;
1164 vma->vm_end = addr + len;
1165 vma->vm_flags = vm_flags;
1166 vma->vm_page_prot = vm_get_page_prot(vm_flags);
1167 vma->vm_pgoff = pgoff;
1169 if (file) {
1170 error = -EINVAL;
1171 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1172 goto free_vma;
1173 if (vm_flags & VM_DENYWRITE) {
1174 error = deny_write_access(file);
1175 if (error)
1176 goto free_vma;
1177 correct_wcount = 1;
1179 vma->vm_file = file;
1180 get_file(file);
1181 error = file->f_op->mmap(file, vma);
1182 if (error)
1183 goto unmap_and_free_vma;
1184 if (vm_flags & VM_EXECUTABLE)
1185 added_exe_file_vma(mm);
1186 } else if (vm_flags & VM_SHARED) {
1187 error = shmem_zero_setup(vma);
1188 if (error)
1189 goto free_vma;
1192 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1193 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1194 * that memory reservation must be checked; but that reservation
1195 * belongs to shared memory object, not to vma: so now clear it.
1197 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
1198 vma->vm_flags &= ~VM_ACCOUNT;
1200 /* Can addr have changed??
1202 * Answer: Yes, several device drivers can do it in their
1203 * f_op->mmap method. -DaveM
1205 addr = vma->vm_start;
1206 pgoff = vma->vm_pgoff;
1207 vm_flags = vma->vm_flags;
1209 if (vma_wants_writenotify(vma))
1210 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
1212 if (file && vma_merge(mm, prev, addr, vma->vm_end,
1213 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
1214 mpol_put(vma_policy(vma));
1215 kmem_cache_free(vm_area_cachep, vma);
1216 fput(file);
1217 if (vm_flags & VM_EXECUTABLE)
1218 removed_exe_file_vma(mm);
1219 } else {
1220 vma_link(mm, vma, prev, rb_link, rb_parent);
1221 file = vma->vm_file;
1224 /* Once vma denies write, undo our temporary denial count */
1225 if (correct_wcount)
1226 atomic_inc(&inode->i_writecount);
1227 out:
1228 mm->total_vm += len >> PAGE_SHIFT;
1229 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1230 if (vm_flags & VM_LOCKED) {
1231 mm->locked_vm += len >> PAGE_SHIFT;
1232 make_pages_present(addr, addr + len);
1234 if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
1235 make_pages_present(addr, addr + len);
1236 return addr;
1238 unmap_and_free_vma:
1239 if (correct_wcount)
1240 atomic_inc(&inode->i_writecount);
1241 vma->vm_file = NULL;
1242 fput(file);
1244 /* Undo any partial mapping done by a device driver. */
1245 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1246 charged = 0;
1247 free_vma:
1248 kmem_cache_free(vm_area_cachep, vma);
1249 unacct_error:
1250 if (charged)
1251 vm_unacct_memory(charged);
1252 return error;
1255 /* Get an address range which is currently unmapped.
1256 * For shmat() with addr=0.
1258 * Ugly calling convention alert:
1259 * Return value with the low bits set means error value,
1260 * ie
1261 * if (ret & ~PAGE_MASK)
1262 * error = ret;
1264 * This function "knows" that -ENOMEM has the bits set.
1266 #ifndef HAVE_ARCH_UNMAPPED_AREA
1267 unsigned long
1268 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1269 unsigned long len, unsigned long pgoff, unsigned long flags)
1271 struct mm_struct *mm = current->mm;
1272 struct vm_area_struct *vma;
1273 unsigned long start_addr;
1275 if (len > TASK_SIZE)
1276 return -ENOMEM;
1278 if (flags & MAP_FIXED)
1279 return addr;
1281 if (addr) {
1282 addr = PAGE_ALIGN(addr);
1283 vma = find_vma(mm, addr);
1284 if (TASK_SIZE - len >= addr &&
1285 (!vma || addr + len <= vma->vm_start))
1286 return addr;
1288 if (len > mm->cached_hole_size) {
1289 start_addr = addr = mm->free_area_cache;
1290 } else {
1291 start_addr = addr = TASK_UNMAPPED_BASE;
1292 mm->cached_hole_size = 0;
1295 full_search:
1296 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1297 /* At this point: (!vma || addr < vma->vm_end). */
1298 if (TASK_SIZE - len < addr) {
1300 * Start a new search - just in case we missed
1301 * some holes.
1303 if (start_addr != TASK_UNMAPPED_BASE) {
1304 addr = TASK_UNMAPPED_BASE;
1305 start_addr = addr;
1306 mm->cached_hole_size = 0;
1307 goto full_search;
1309 return -ENOMEM;
1311 if (!vma || addr + len <= vma->vm_start) {
1313 * Remember the place where we stopped the search:
1315 mm->free_area_cache = addr + len;
1316 return addr;
1318 if (addr + mm->cached_hole_size < vma->vm_start)
1319 mm->cached_hole_size = vma->vm_start - addr;
1320 addr = vma->vm_end;
1323 #endif
1325 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1328 * Is this a new hole at the lowest possible address?
1330 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
1331 mm->free_area_cache = addr;
1332 mm->cached_hole_size = ~0UL;
1337 * This mmap-allocator allocates new areas top-down from below the
1338 * stack's low limit (the base):
1340 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1341 unsigned long
1342 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1343 const unsigned long len, const unsigned long pgoff,
1344 const unsigned long flags)
1346 struct vm_area_struct *vma;
1347 struct mm_struct *mm = current->mm;
1348 unsigned long addr = addr0;
1350 /* requested length too big for entire address space */
1351 if (len > TASK_SIZE)
1352 return -ENOMEM;
1354 if (flags & MAP_FIXED)
1355 return addr;
1357 /* requesting a specific address */
1358 if (addr) {
1359 addr = PAGE_ALIGN(addr);
1360 vma = find_vma(mm, addr);
1361 if (TASK_SIZE - len >= addr &&
1362 (!vma || addr + len <= vma->vm_start))
1363 return addr;
1366 /* check if free_area_cache is useful for us */
1367 if (len <= mm->cached_hole_size) {
1368 mm->cached_hole_size = 0;
1369 mm->free_area_cache = mm->mmap_base;
1372 /* either no address requested or can't fit in requested address hole */
1373 addr = mm->free_area_cache;
1375 /* make sure it can fit in the remaining address space */
1376 if (addr > len) {
1377 vma = find_vma(mm, addr-len);
1378 if (!vma || addr <= vma->vm_start)
1379 /* remember the address as a hint for next time */
1380 return (mm->free_area_cache = addr-len);
1383 if (mm->mmap_base < len)
1384 goto bottomup;
1386 addr = mm->mmap_base-len;
1388 do {
1390 * Lookup failure means no vma is above this address,
1391 * else if new region fits below vma->vm_start,
1392 * return with success:
1394 vma = find_vma(mm, addr);
1395 if (!vma || addr+len <= vma->vm_start)
1396 /* remember the address as a hint for next time */
1397 return (mm->free_area_cache = addr);
1399 /* remember the largest hole we saw so far */
1400 if (addr + mm->cached_hole_size < vma->vm_start)
1401 mm->cached_hole_size = vma->vm_start - addr;
1403 /* try just below the current vma->vm_start */
1404 addr = vma->vm_start-len;
1405 } while (len < vma->vm_start);
1407 bottomup:
1409 * A failed mmap() very likely causes application failure,
1410 * so fall back to the bottom-up function here. This scenario
1411 * can happen with large stack limits and large mmap()
1412 * allocations.
1414 mm->cached_hole_size = ~0UL;
1415 mm->free_area_cache = TASK_UNMAPPED_BASE;
1416 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1418 * Restore the topdown base:
1420 mm->free_area_cache = mm->mmap_base;
1421 mm->cached_hole_size = ~0UL;
1423 return addr;
1425 #endif
1427 void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
1430 * Is this a new hole at the highest possible address?
1432 if (addr > mm->free_area_cache)
1433 mm->free_area_cache = addr;
1435 /* dont allow allocations above current base */
1436 if (mm->free_area_cache > mm->mmap_base)
1437 mm->free_area_cache = mm->mmap_base;
1440 unsigned long
1441 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1442 unsigned long pgoff, unsigned long flags)
1444 unsigned long (*get_area)(struct file *, unsigned long,
1445 unsigned long, unsigned long, unsigned long);
1447 get_area = current->mm->get_unmapped_area;
1448 if (file && file->f_op && file->f_op->get_unmapped_area)
1449 get_area = file->f_op->get_unmapped_area;
1450 addr = get_area(file, addr, len, pgoff, flags);
1451 if (IS_ERR_VALUE(addr))
1452 return addr;
1454 if (addr > TASK_SIZE - len)
1455 return -ENOMEM;
1456 if (addr & ~PAGE_MASK)
1457 return -EINVAL;
1459 return arch_rebalance_pgtables(addr, len);
1462 EXPORT_SYMBOL(get_unmapped_area);
1464 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1465 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1467 struct vm_area_struct *vma = NULL;
1469 if (mm) {
1470 /* Check the cache first. */
1471 /* (Cache hit rate is typically around 35%.) */
1472 vma = mm->mmap_cache;
1473 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1474 struct rb_node * rb_node;
1476 rb_node = mm->mm_rb.rb_node;
1477 vma = NULL;
1479 while (rb_node) {
1480 struct vm_area_struct * vma_tmp;
1482 vma_tmp = rb_entry(rb_node,
1483 struct vm_area_struct, vm_rb);
1485 if (vma_tmp->vm_end > addr) {
1486 vma = vma_tmp;
1487 if (vma_tmp->vm_start <= addr)
1488 break;
1489 rb_node = rb_node->rb_left;
1490 } else
1491 rb_node = rb_node->rb_right;
1493 if (vma)
1494 mm->mmap_cache = vma;
1497 return vma;
1500 EXPORT_SYMBOL(find_vma);
1502 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1503 struct vm_area_struct *
1504 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1505 struct vm_area_struct **pprev)
1507 struct vm_area_struct *vma = NULL, *prev = NULL;
1508 struct rb_node * rb_node;
1509 if (!mm)
1510 goto out;
1512 /* Guard against addr being lower than the first VMA */
1513 vma = mm->mmap;
1515 /* Go through the RB tree quickly. */
1516 rb_node = mm->mm_rb.rb_node;
1518 while (rb_node) {
1519 struct vm_area_struct *vma_tmp;
1520 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1522 if (addr < vma_tmp->vm_end) {
1523 rb_node = rb_node->rb_left;
1524 } else {
1525 prev = vma_tmp;
1526 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1527 break;
1528 rb_node = rb_node->rb_right;
1532 out:
1533 *pprev = prev;
1534 return prev ? prev->vm_next : vma;
1538 * Verify that the stack growth is acceptable and
1539 * update accounting. This is shared with both the
1540 * grow-up and grow-down cases.
1542 static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
1544 struct mm_struct *mm = vma->vm_mm;
1545 struct rlimit *rlim = current->signal->rlim;
1546 unsigned long new_start;
1548 /* address space limit tests */
1549 if (!may_expand_vm(mm, grow))
1550 return -ENOMEM;
1552 /* Stack limit test */
1553 if (size > rlim[RLIMIT_STACK].rlim_cur)
1554 return -ENOMEM;
1556 /* mlock limit tests */
1557 if (vma->vm_flags & VM_LOCKED) {
1558 unsigned long locked;
1559 unsigned long limit;
1560 locked = mm->locked_vm + grow;
1561 limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
1562 if (locked > limit && !capable(CAP_IPC_LOCK))
1563 return -ENOMEM;
1566 /* Check to ensure the stack will not grow into a hugetlb-only region */
1567 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1568 vma->vm_end - size;
1569 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1570 return -EFAULT;
1573 * Overcommit.. This must be the final test, as it will
1574 * update security statistics.
1576 if (security_vm_enough_memory_mm(mm, grow))
1577 return -ENOMEM;
1579 /* Ok, everything looks good - let it rip */
1580 mm->total_vm += grow;
1581 if (vma->vm_flags & VM_LOCKED)
1582 mm->locked_vm += grow;
1583 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
1584 return 0;
1587 #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
1589 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1590 * vma is the last one with address > vma->vm_end. Have to extend vma.
1592 int expand_upwards(struct vm_area_struct *vma, unsigned long address)
1594 int error;
1596 if (!(vma->vm_flags & VM_GROWSUP))
1597 return -EFAULT;
1600 * We must make sure the anon_vma is allocated
1601 * so that the anon_vma locking is not a noop.
1603 if (unlikely(anon_vma_prepare(vma)))
1604 return -ENOMEM;
1605 anon_vma_lock(vma);
1608 * vma->vm_start/vm_end cannot change under us because the caller
1609 * is required to hold the mmap_sem in read mode. We need the
1610 * anon_vma lock to serialize against concurrent expand_stacks.
1611 * Also guard against wrapping around to address 0.
1613 if (address < PAGE_ALIGN(address+4))
1614 address = PAGE_ALIGN(address+4);
1615 else {
1616 anon_vma_unlock(vma);
1617 return -ENOMEM;
1619 error = 0;
1621 /* Somebody else might have raced and expanded it already */
1622 if (address > vma->vm_end) {
1623 unsigned long size, grow;
1625 size = address - vma->vm_start;
1626 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1628 error = acct_stack_growth(vma, size, grow);
1629 if (!error)
1630 vma->vm_end = address;
1632 anon_vma_unlock(vma);
1633 return error;
1635 #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1638 * vma is the first one with address < vma->vm_start. Have to extend vma.
1640 static inline int expand_downwards(struct vm_area_struct *vma,
1641 unsigned long address)
1643 int error;
1646 * We must make sure the anon_vma is allocated
1647 * so that the anon_vma locking is not a noop.
1649 if (unlikely(anon_vma_prepare(vma)))
1650 return -ENOMEM;
1652 address &= PAGE_MASK;
1653 error = security_file_mmap(NULL, 0, 0, 0, address, 1);
1654 if (error)
1655 return error;
1657 anon_vma_lock(vma);
1660 * vma->vm_start/vm_end cannot change under us because the caller
1661 * is required to hold the mmap_sem in read mode. We need the
1662 * anon_vma lock to serialize against concurrent expand_stacks.
1665 /* Somebody else might have raced and expanded it already */
1666 if (address < vma->vm_start) {
1667 unsigned long size, grow;
1669 size = vma->vm_end - address;
1670 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1672 error = acct_stack_growth(vma, size, grow);
1673 if (!error) {
1674 vma->vm_start = address;
1675 vma->vm_pgoff -= grow;
1678 anon_vma_unlock(vma);
1679 return error;
1682 int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
1684 return expand_downwards(vma, address);
1687 #ifdef CONFIG_STACK_GROWSUP
1688 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1690 return expand_upwards(vma, address);
1693 struct vm_area_struct *
1694 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1696 struct vm_area_struct *vma, *prev;
1698 addr &= PAGE_MASK;
1699 vma = find_vma_prev(mm, addr, &prev);
1700 if (vma && (vma->vm_start <= addr))
1701 return vma;
1702 if (!prev || expand_stack(prev, addr))
1703 return NULL;
1704 if (prev->vm_flags & VM_LOCKED)
1705 make_pages_present(addr, prev->vm_end);
1706 return prev;
1708 #else
1709 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1711 return expand_downwards(vma, address);
1714 struct vm_area_struct *
1715 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1717 struct vm_area_struct * vma;
1718 unsigned long start;
1720 addr &= PAGE_MASK;
1721 vma = find_vma(mm,addr);
1722 if (!vma)
1723 return NULL;
1724 if (vma->vm_start <= addr)
1725 return vma;
1726 if (!(vma->vm_flags & VM_GROWSDOWN))
1727 return NULL;
1728 start = vma->vm_start;
1729 if (expand_stack(vma, addr))
1730 return NULL;
1731 if (vma->vm_flags & VM_LOCKED)
1732 make_pages_present(addr, start);
1733 return vma;
1735 #endif
1738 * Ok - we have the memory areas we should free on the vma list,
1739 * so release them, and do the vma updates.
1741 * Called with the mm semaphore held.
1743 static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
1745 /* Update high watermark before we lower total_vm */
1746 update_hiwater_vm(mm);
1747 do {
1748 long nrpages = vma_pages(vma);
1750 mm->total_vm -= nrpages;
1751 if (vma->vm_flags & VM_LOCKED)
1752 mm->locked_vm -= nrpages;
1753 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
1754 vma = remove_vma(vma);
1755 } while (vma);
1756 validate_mm(mm);
1760 * Get rid of page table information in the indicated region.
1762 * Called with the mm semaphore held.
1764 static void unmap_region(struct mm_struct *mm,
1765 struct vm_area_struct *vma, struct vm_area_struct *prev,
1766 unsigned long start, unsigned long end)
1768 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
1769 struct mmu_gather *tlb;
1770 unsigned long nr_accounted = 0;
1772 lru_add_drain();
1773 tlb = tlb_gather_mmu(mm, 0);
1774 update_hiwater_rss(mm);
1775 unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
1776 vm_unacct_memory(nr_accounted);
1777 free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1778 next? next->vm_start: 0);
1779 tlb_finish_mmu(tlb, start, end);
1783 * Create a list of vma's touched by the unmap, removing them from the mm's
1784 * vma list as we go..
1786 static void
1787 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1788 struct vm_area_struct *prev, unsigned long end)
1790 struct vm_area_struct **insertion_point;
1791 struct vm_area_struct *tail_vma = NULL;
1792 unsigned long addr;
1794 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1795 do {
1796 rb_erase(&vma->vm_rb, &mm->mm_rb);
1797 mm->map_count--;
1798 tail_vma = vma;
1799 vma = vma->vm_next;
1800 } while (vma && vma->vm_start < end);
1801 *insertion_point = vma;
1802 tail_vma->vm_next = NULL;
1803 if (mm->unmap_area == arch_unmap_area)
1804 addr = prev ? prev->vm_end : mm->mmap_base;
1805 else
1806 addr = vma ? vma->vm_start : mm->mmap_base;
1807 mm->unmap_area(mm, addr);
1808 mm->mmap_cache = NULL; /* Kill the cache. */
1812 * Split a vma into two pieces at address 'addr', a new vma is allocated
1813 * either for the first part or the tail.
1815 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1816 unsigned long addr, int new_below)
1818 struct mempolicy *pol;
1819 struct vm_area_struct *new;
1821 if (is_vm_hugetlb_page(vma) && (addr &
1822 ~(huge_page_mask(hstate_vma(vma)))))
1823 return -EINVAL;
1825 if (mm->map_count >= sysctl_max_map_count)
1826 return -ENOMEM;
1828 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1829 if (!new)
1830 return -ENOMEM;
1832 /* most fields are the same, copy all, and then fixup */
1833 *new = *vma;
1835 if (new_below)
1836 new->vm_end = addr;
1837 else {
1838 new->vm_start = addr;
1839 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1842 pol = mpol_dup(vma_policy(vma));
1843 if (IS_ERR(pol)) {
1844 kmem_cache_free(vm_area_cachep, new);
1845 return PTR_ERR(pol);
1847 vma_set_policy(new, pol);
1849 if (new->vm_file) {
1850 get_file(new->vm_file);
1851 if (vma->vm_flags & VM_EXECUTABLE)
1852 added_exe_file_vma(mm);
1855 if (new->vm_ops && new->vm_ops->open)
1856 new->vm_ops->open(new);
1858 if (new_below)
1859 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1860 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1861 else
1862 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1864 return 0;
1867 /* Munmap is split into 2 main parts -- this part which finds
1868 * what needs doing, and the areas themselves, which do the
1869 * work. This now handles partial unmappings.
1870 * Jeremy Fitzhardinge <jeremy@goop.org>
1872 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1874 unsigned long end;
1875 struct vm_area_struct *vma, *prev, *last;
1877 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1878 return -EINVAL;
1880 if ((len = PAGE_ALIGN(len)) == 0)
1881 return -EINVAL;
1883 /* Find the first overlapping VMA */
1884 vma = find_vma_prev(mm, start, &prev);
1885 if (!vma)
1886 return 0;
1887 /* we have start < vma->vm_end */
1889 /* if it doesn't overlap, we have nothing.. */
1890 end = start + len;
1891 if (vma->vm_start >= end)
1892 return 0;
1895 * If we need to split any vma, do it now to save pain later.
1897 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1898 * unmapped vm_area_struct will remain in use: so lower split_vma
1899 * places tmp vma above, and higher split_vma places tmp vma below.
1901 if (start > vma->vm_start) {
1902 int error = split_vma(mm, vma, start, 0);
1903 if (error)
1904 return error;
1905 prev = vma;
1908 /* Does it split the last one? */
1909 last = find_vma(mm, end);
1910 if (last && end > last->vm_start) {
1911 int error = split_vma(mm, last, end, 1);
1912 if (error)
1913 return error;
1915 vma = prev? prev->vm_next: mm->mmap;
1918 * Remove the vma's, and unmap the actual pages
1920 detach_vmas_to_be_unmapped(mm, vma, prev, end);
1921 unmap_region(mm, vma, prev, start, end);
1923 /* Fix up all other VM information */
1924 remove_vma_list(mm, vma);
1926 return 0;
1929 EXPORT_SYMBOL(do_munmap);
1931 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1933 int ret;
1934 struct mm_struct *mm = current->mm;
1936 profile_munmap(addr);
1938 down_write(&mm->mmap_sem);
1939 ret = do_munmap(mm, addr, len);
1940 up_write(&mm->mmap_sem);
1941 return ret;
1944 static inline void verify_mm_writelocked(struct mm_struct *mm)
1946 #ifdef CONFIG_DEBUG_VM
1947 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
1948 WARN_ON(1);
1949 up_read(&mm->mmap_sem);
1951 #endif
1955 * this is really a simplified "do_mmap". it only handles
1956 * anonymous maps. eventually we may be able to do some
1957 * brk-specific accounting here.
1959 unsigned long do_brk(unsigned long addr, unsigned long len)
1961 struct mm_struct * mm = current->mm;
1962 struct vm_area_struct * vma, * prev;
1963 unsigned long flags;
1964 struct rb_node ** rb_link, * rb_parent;
1965 pgoff_t pgoff = addr >> PAGE_SHIFT;
1966 int error;
1968 len = PAGE_ALIGN(len);
1969 if (!len)
1970 return addr;
1972 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1973 return -EINVAL;
1975 if (is_hugepage_only_range(mm, addr, len))
1976 return -EINVAL;
1978 error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
1979 if (error)
1980 return error;
1982 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1984 error = arch_mmap_check(addr, len, flags);
1985 if (error)
1986 return error;
1989 * mlock MCL_FUTURE?
1991 if (mm->def_flags & VM_LOCKED) {
1992 unsigned long locked, lock_limit;
1993 locked = len >> PAGE_SHIFT;
1994 locked += mm->locked_vm;
1995 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1996 lock_limit >>= PAGE_SHIFT;
1997 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1998 return -EAGAIN;
2002 * mm->mmap_sem is required to protect against another thread
2003 * changing the mappings in case we sleep.
2005 verify_mm_writelocked(mm);
2008 * Clear old maps. this also does some error checking for us
2010 munmap_back:
2011 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2012 if (vma && vma->vm_start < addr + len) {
2013 if (do_munmap(mm, addr, len))
2014 return -ENOMEM;
2015 goto munmap_back;
2018 /* Check against address space limits *after* clearing old maps... */
2019 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
2020 return -ENOMEM;
2022 if (mm->map_count > sysctl_max_map_count)
2023 return -ENOMEM;
2025 if (security_vm_enough_memory(len >> PAGE_SHIFT))
2026 return -ENOMEM;
2028 /* Can we just expand an old private anonymous mapping? */
2029 if (vma_merge(mm, prev, addr, addr + len, flags,
2030 NULL, NULL, pgoff, NULL))
2031 goto out;
2034 * create a vma struct for an anonymous mapping
2036 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2037 if (!vma) {
2038 vm_unacct_memory(len >> PAGE_SHIFT);
2039 return -ENOMEM;
2042 vma->vm_mm = mm;
2043 vma->vm_start = addr;
2044 vma->vm_end = addr + len;
2045 vma->vm_pgoff = pgoff;
2046 vma->vm_flags = flags;
2047 vma->vm_page_prot = vm_get_page_prot(flags);
2048 vma_link(mm, vma, prev, rb_link, rb_parent);
2049 out:
2050 mm->total_vm += len >> PAGE_SHIFT;
2051 if (flags & VM_LOCKED) {
2052 mm->locked_vm += len >> PAGE_SHIFT;
2053 make_pages_present(addr, addr + len);
2055 return addr;
2058 EXPORT_SYMBOL(do_brk);
2060 /* Release all mmaps. */
2061 void exit_mmap(struct mm_struct *mm)
2063 struct mmu_gather *tlb;
2064 struct vm_area_struct *vma = mm->mmap;
2065 unsigned long nr_accounted = 0;
2066 unsigned long end;
2068 /* mm's last user has gone, and its about to be pulled down */
2069 arch_exit_mmap(mm);
2070 mmu_notifier_release(mm);
2072 if (!mm->mmap) /* Can happen if dup_mmap() received an OOM */
2073 return;
2075 lru_add_drain();
2076 flush_cache_mm(mm);
2077 tlb = tlb_gather_mmu(mm, 1);
2078 /* Don't update_hiwater_rss(mm) here, do_exit already did */
2079 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2080 end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
2081 vm_unacct_memory(nr_accounted);
2082 free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
2083 tlb_finish_mmu(tlb, 0, end);
2086 * Walk the list again, actually closing and freeing it,
2087 * with preemption enabled, without holding any MM locks.
2089 while (vma)
2090 vma = remove_vma(vma);
2092 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
2095 /* Insert vm structure into process list sorted by address
2096 * and into the inode's i_mmap tree. If vm_file is non-NULL
2097 * then i_mmap_lock is taken here.
2099 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2101 struct vm_area_struct * __vma, * prev;
2102 struct rb_node ** rb_link, * rb_parent;
2105 * The vm_pgoff of a purely anonymous vma should be irrelevant
2106 * until its first write fault, when page's anon_vma and index
2107 * are set. But now set the vm_pgoff it will almost certainly
2108 * end up with (unless mremap moves it elsewhere before that
2109 * first wfault), so /proc/pid/maps tells a consistent story.
2111 * By setting it to reflect the virtual start address of the
2112 * vma, merges and splits can happen in a seamless way, just
2113 * using the existing file pgoff checks and manipulations.
2114 * Similarly in do_mmap_pgoff and in do_brk.
2116 if (!vma->vm_file) {
2117 BUG_ON(vma->anon_vma);
2118 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2120 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2121 if (__vma && __vma->vm_start < vma->vm_end)
2122 return -ENOMEM;
2123 if ((vma->vm_flags & VM_ACCOUNT) &&
2124 security_vm_enough_memory_mm(mm, vma_pages(vma)))
2125 return -ENOMEM;
2126 vma_link(mm, vma, prev, rb_link, rb_parent);
2127 return 0;
2131 * Copy the vma structure to a new location in the same mm,
2132 * prior to moving page table entries, to effect an mremap move.
2134 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2135 unsigned long addr, unsigned long len, pgoff_t pgoff)
2137 struct vm_area_struct *vma = *vmap;
2138 unsigned long vma_start = vma->vm_start;
2139 struct mm_struct *mm = vma->vm_mm;
2140 struct vm_area_struct *new_vma, *prev;
2141 struct rb_node **rb_link, *rb_parent;
2142 struct mempolicy *pol;
2145 * If anonymous vma has not yet been faulted, update new pgoff
2146 * to match new location, to increase its chance of merging.
2148 if (!vma->vm_file && !vma->anon_vma)
2149 pgoff = addr >> PAGE_SHIFT;
2151 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2152 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2153 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2154 if (new_vma) {
2156 * Source vma may have been merged into new_vma
2158 if (vma_start >= new_vma->vm_start &&
2159 vma_start < new_vma->vm_end)
2160 *vmap = new_vma;
2161 } else {
2162 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
2163 if (new_vma) {
2164 *new_vma = *vma;
2165 pol = mpol_dup(vma_policy(vma));
2166 if (IS_ERR(pol)) {
2167 kmem_cache_free(vm_area_cachep, new_vma);
2168 return NULL;
2170 vma_set_policy(new_vma, pol);
2171 new_vma->vm_start = addr;
2172 new_vma->vm_end = addr + len;
2173 new_vma->vm_pgoff = pgoff;
2174 if (new_vma->vm_file) {
2175 get_file(new_vma->vm_file);
2176 if (vma->vm_flags & VM_EXECUTABLE)
2177 added_exe_file_vma(mm);
2179 if (new_vma->vm_ops && new_vma->vm_ops->open)
2180 new_vma->vm_ops->open(new_vma);
2181 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2184 return new_vma;
2188 * Return true if the calling process may expand its vm space by the passed
2189 * number of pages
2191 int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2193 unsigned long cur = mm->total_vm; /* pages */
2194 unsigned long lim;
2196 lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2198 if (cur + npages > lim)
2199 return 0;
2200 return 1;
2204 static int special_mapping_fault(struct vm_area_struct *vma,
2205 struct vm_fault *vmf)
2207 pgoff_t pgoff;
2208 struct page **pages;
2211 * special mappings have no vm_file, and in that case, the mm
2212 * uses vm_pgoff internally. So we have to subtract it from here.
2213 * We are allowed to do this because we are the mm; do not copy
2214 * this code into drivers!
2216 pgoff = vmf->pgoff - vma->vm_pgoff;
2218 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2219 pgoff--;
2221 if (*pages) {
2222 struct page *page = *pages;
2223 get_page(page);
2224 vmf->page = page;
2225 return 0;
2228 return VM_FAULT_SIGBUS;
2232 * Having a close hook prevents vma merging regardless of flags.
2234 static void special_mapping_close(struct vm_area_struct *vma)
2238 static struct vm_operations_struct special_mapping_vmops = {
2239 .close = special_mapping_close,
2240 .fault = special_mapping_fault,
2244 * Called with mm->mmap_sem held for writing.
2245 * Insert a new vma covering the given region, with the given flags.
2246 * Its pages are supplied by the given array of struct page *.
2247 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2248 * The region past the last page supplied will always produce SIGBUS.
2249 * The array pointer and the pages it points to are assumed to stay alive
2250 * for as long as this mapping might exist.
2252 int install_special_mapping(struct mm_struct *mm,
2253 unsigned long addr, unsigned long len,
2254 unsigned long vm_flags, struct page **pages)
2256 int ret;
2257 struct vm_area_struct *vma;
2259 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2260 if (unlikely(vma == NULL))
2261 return -ENOMEM;
2263 vma->vm_mm = mm;
2264 vma->vm_start = addr;
2265 vma->vm_end = addr + len;
2267 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
2268 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2270 vma->vm_ops = &special_mapping_vmops;
2271 vma->vm_private_data = pages;
2273 ret = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
2274 if (ret)
2275 goto out;
2277 ret = insert_vm_struct(mm, vma);
2278 if (ret)
2279 goto out;
2281 mm->total_vm += len >> PAGE_SHIFT;
2283 return 0;
2285 out:
2286 kmem_cache_free(vm_area_cachep, vma);
2287 return ret;
2290 static DEFINE_MUTEX(mm_all_locks_mutex);
2292 static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
2294 if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
2296 * The LSB of head.next can't change from under us
2297 * because we hold the mm_all_locks_mutex.
2299 spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
2301 * We can safely modify head.next after taking the
2302 * anon_vma->lock. If some other vma in this mm shares
2303 * the same anon_vma we won't take it again.
2305 * No need of atomic instructions here, head.next
2306 * can't change from under us thanks to the
2307 * anon_vma->lock.
2309 if (__test_and_set_bit(0, (unsigned long *)
2310 &anon_vma->head.next))
2311 BUG();
2315 static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
2317 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2319 * AS_MM_ALL_LOCKS can't change from under us because
2320 * we hold the mm_all_locks_mutex.
2322 * Operations on ->flags have to be atomic because
2323 * even if AS_MM_ALL_LOCKS is stable thanks to the
2324 * mm_all_locks_mutex, there may be other cpus
2325 * changing other bitflags in parallel to us.
2327 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2328 BUG();
2329 spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
2334 * This operation locks against the VM for all pte/vma/mm related
2335 * operations that could ever happen on a certain mm. This includes
2336 * vmtruncate, try_to_unmap, and all page faults.
2338 * The caller must take the mmap_sem in write mode before calling
2339 * mm_take_all_locks(). The caller isn't allowed to release the
2340 * mmap_sem until mm_drop_all_locks() returns.
2342 * mmap_sem in write mode is required in order to block all operations
2343 * that could modify pagetables and free pages without need of
2344 * altering the vma layout (for example populate_range() with
2345 * nonlinear vmas). It's also needed in write mode to avoid new
2346 * anon_vmas to be associated with existing vmas.
2348 * A single task can't take more than one mm_take_all_locks() in a row
2349 * or it would deadlock.
2351 * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
2352 * mapping->flags avoid to take the same lock twice, if more than one
2353 * vma in this mm is backed by the same anon_vma or address_space.
2355 * We can take all the locks in random order because the VM code
2356 * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
2357 * takes more than one of them in a row. Secondly we're protected
2358 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2360 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2361 * that may have to take thousand of locks.
2363 * mm_take_all_locks() can fail if it's interrupted by signals.
2365 int mm_take_all_locks(struct mm_struct *mm)
2367 struct vm_area_struct *vma;
2368 int ret = -EINTR;
2370 BUG_ON(down_read_trylock(&mm->mmap_sem));
2372 mutex_lock(&mm_all_locks_mutex);
2374 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2375 if (signal_pending(current))
2376 goto out_unlock;
2377 if (vma->vm_file && vma->vm_file->f_mapping)
2378 vm_lock_mapping(mm, vma->vm_file->f_mapping);
2381 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2382 if (signal_pending(current))
2383 goto out_unlock;
2384 if (vma->anon_vma)
2385 vm_lock_anon_vma(mm, vma->anon_vma);
2388 ret = 0;
2390 out_unlock:
2391 if (ret)
2392 mm_drop_all_locks(mm);
2394 return ret;
2397 static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2399 if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
2401 * The LSB of head.next can't change to 0 from under
2402 * us because we hold the mm_all_locks_mutex.
2404 * We must however clear the bitflag before unlocking
2405 * the vma so the users using the anon_vma->head will
2406 * never see our bitflag.
2408 * No need of atomic instructions here, head.next
2409 * can't change from under us until we release the
2410 * anon_vma->lock.
2412 if (!__test_and_clear_bit(0, (unsigned long *)
2413 &anon_vma->head.next))
2414 BUG();
2415 spin_unlock(&anon_vma->lock);
2419 static void vm_unlock_mapping(struct address_space *mapping)
2421 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2423 * AS_MM_ALL_LOCKS can't change to 0 from under us
2424 * because we hold the mm_all_locks_mutex.
2426 spin_unlock(&mapping->i_mmap_lock);
2427 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2428 &mapping->flags))
2429 BUG();
2434 * The mmap_sem cannot be released by the caller until
2435 * mm_drop_all_locks() returns.
2437 void mm_drop_all_locks(struct mm_struct *mm)
2439 struct vm_area_struct *vma;
2441 BUG_ON(down_read_trylock(&mm->mmap_sem));
2442 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2444 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2445 if (vma->anon_vma)
2446 vm_unlock_anon_vma(vma->anon_vma);
2447 if (vma->vm_file && vma->vm_file->f_mapping)
2448 vm_unlock_mapping(vma->vm_file->f_mapping);
2451 mutex_unlock(&mm_all_locks_mutex);