6 #include <linux/slab.h>
8 #include <linux/mman.h>
9 #include <linux/pagemap.h>
10 #include <linux/swap.h>
11 #include <linux/swapctl.h>
12 #include <linux/smp_lock.h>
13 #include <linux/init.h>
14 #include <linux/file.h>
16 #include <asm/uaccess.h>
17 #include <asm/pgalloc.h>
19 /* description of effects of mapping type and prot in current implementation.
20 * this is due to the limited x86 page protection hardware. The expected
21 * behavior is in parens:
24 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
25 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
26 * w: (no) no w: (no) no w: (yes) yes w: (no) no
27 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
29 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
30 * w: (no) no w: (no) no w: (copy) copy w: (no) no
31 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
34 pgprot_t protection_map
[16] = {
35 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
36 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
39 int sysctl_overcommit_memory
;
41 /* Check that a process has enough memory to allocate a
42 * new virtual mapping.
44 int vm_enough_memory(long pages
)
46 /* Stupid algorithm to decide if we have enough memory: while
47 * simple, it hopefully works in most obvious cases.. Easy to
48 * fool it, but this should catch most mistakes.
50 /* 23/11/98 NJC: Somewhat less stupid version of algorithm,
51 * which tries to do "TheRightThing". Instead of using half of
52 * (buffers+cache), use the minimum values. Allow an extra 2%
53 * of num_physpages for safety margin.
58 /* Sometimes we want to use more memory than we have. */
59 if (sysctl_overcommit_memory
)
62 free
= atomic_read(&buffermem_pages
);
63 free
+= atomic_read(&page_cache_size
);
64 free
+= nr_free_pages();
65 free
+= nr_swap_pages
;
69 /* Remove one vm structure from the inode's i_mapping address space. */
70 static inline void __remove_shared_vm_struct(struct vm_area_struct
*vma
)
72 struct file
* file
= vma
->vm_file
;
75 struct inode
*inode
= file
->f_dentry
->d_inode
;
76 if (vma
->vm_flags
& VM_DENYWRITE
)
77 atomic_inc(&inode
->i_writecount
);
78 if(vma
->vm_next_share
)
79 vma
->vm_next_share
->vm_pprev_share
= vma
->vm_pprev_share
;
80 *vma
->vm_pprev_share
= vma
->vm_next_share
;
84 static inline void remove_shared_vm_struct(struct vm_area_struct
*vma
)
86 lock_vma_mappings(vma
);
87 __remove_shared_vm_struct(vma
);
88 unlock_vma_mappings(vma
);
91 void lock_vma_mappings(struct vm_area_struct
*vma
)
93 struct address_space
*mapping
;
97 mapping
= vma
->vm_file
->f_dentry
->d_inode
->i_mapping
;
99 spin_lock(&mapping
->i_shared_lock
);
102 void unlock_vma_mappings(struct vm_area_struct
*vma
)
104 struct address_space
*mapping
;
108 mapping
= vma
->vm_file
->f_dentry
->d_inode
->i_mapping
;
110 spin_unlock(&mapping
->i_shared_lock
);
114 * sys_brk() for the most part doesn't need the global kernel
115 * lock, except when an application is doing something nasty
116 * like trying to un-brk an area that has already been mapped
117 * to a regular file. in this case, the unmapping will need
118 * to invoke file system routines that need the global lock.
120 asmlinkage
unsigned long sys_brk(unsigned long brk
)
122 unsigned long rlim
, retval
;
123 unsigned long newbrk
, oldbrk
;
124 struct mm_struct
*mm
= current
->mm
;
128 if (brk
< mm
->end_code
)
130 newbrk
= PAGE_ALIGN(brk
);
131 oldbrk
= PAGE_ALIGN(mm
->brk
);
132 if (oldbrk
== newbrk
)
135 /* Always allow shrinking brk. */
136 if (brk
<= mm
->brk
) {
137 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
142 /* Check against rlimit.. */
143 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
144 if (rlim
< RLIM_INFINITY
&& brk
- mm
->start_data
> rlim
)
147 /* Check against existing mmap mappings. */
148 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
151 /* Check if we have enough memory.. */
152 if (!vm_enough_memory((newbrk
-oldbrk
) >> PAGE_SHIFT
))
155 /* Ok, looks good - let it rip. */
156 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
166 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
167 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
170 static inline unsigned long vm_flags(unsigned long prot
, unsigned long flags
)
172 #define _trans(x,bit1,bit2) \
173 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
175 unsigned long prot_bits
, flag_bits
;
177 _trans(prot
, PROT_READ
, VM_READ
) |
178 _trans(prot
, PROT_WRITE
, VM_WRITE
) |
179 _trans(prot
, PROT_EXEC
, VM_EXEC
);
181 _trans(flags
, MAP_GROWSDOWN
, VM_GROWSDOWN
) |
182 _trans(flags
, MAP_DENYWRITE
, VM_DENYWRITE
) |
183 _trans(flags
, MAP_EXECUTABLE
, VM_EXECUTABLE
);
184 return prot_bits
| flag_bits
;
188 unsigned long do_mmap_pgoff(struct file
* file
, unsigned long addr
, unsigned long len
,
189 unsigned long prot
, unsigned long flags
, unsigned long pgoff
)
191 struct mm_struct
* mm
= current
->mm
;
192 struct vm_area_struct
* vma
;
193 int correct_wcount
= 0;
196 if (file
&& (!file
->f_op
|| !file
->f_op
->mmap
))
199 if ((len
= PAGE_ALIGN(len
)) == 0)
202 if (len
> TASK_SIZE
|| addr
> TASK_SIZE
-len
)
205 /* offset overflow? */
206 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
209 /* Too many mappings? */
210 if (mm
->map_count
> MAX_MAP_COUNT
)
213 /* mlock MCL_FUTURE? */
214 if (mm
->def_flags
& VM_LOCKED
) {
215 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
217 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
221 /* Do simple checking here so the lower-level routines won't have
222 * to. we assume access permissions have been handled by the open
223 * of the memory object, so we don't do any here.
226 switch (flags
& MAP_TYPE
) {
228 if ((prot
& PROT_WRITE
) && !(file
->f_mode
& FMODE_WRITE
))
231 /* Make sure we don't allow writing to an append-only file.. */
232 if (IS_APPEND(file
->f_dentry
->d_inode
) && (file
->f_mode
& FMODE_WRITE
))
235 /* make sure there are no mandatory locks on the file. */
236 if (locks_verify_locked(file
->f_dentry
->d_inode
))
241 if (!(file
->f_mode
& FMODE_READ
))
250 /* Obtain the address to map to. we verify (or select) it and ensure
251 * that it represents a valid section of the address space.
253 if (flags
& MAP_FIXED
) {
254 if (addr
& ~PAGE_MASK
)
257 addr
= get_unmapped_area(addr
, len
);
262 /* Determine the object being mapped and call the appropriate
263 * specific mapper. the address has already been validated, but
264 * not unmapped, but the maps are removed from the list.
266 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
271 vma
->vm_start
= addr
;
272 vma
->vm_end
= addr
+ len
;
273 vma
->vm_flags
= vm_flags(prot
,flags
) | mm
->def_flags
;
276 VM_ClearReadHint(vma
);
279 if (file
->f_mode
& FMODE_READ
)
280 vma
->vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
281 if (flags
& MAP_SHARED
) {
282 vma
->vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
284 /* This looks strange, but when we don't have the file open
285 * for writing, we can demote the shared mapping to a simpler
286 * private mapping. That also takes care of a security hole
287 * with ptrace() writing to a shared mapping without write
290 * We leave the VM_MAYSHARE bit on, just to get correct output
291 * from /proc/xxx/maps..
293 if (!(file
->f_mode
& FMODE_WRITE
))
294 vma
->vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
297 vma
->vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
298 if (flags
& MAP_SHARED
)
299 vma
->vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
301 vma
->vm_page_prot
= protection_map
[vma
->vm_flags
& 0x0f];
303 vma
->vm_pgoff
= pgoff
;
305 vma
->vm_private_data
= NULL
;
309 if (do_munmap(mm
, addr
, len
))
312 /* Check against address space limit. */
313 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
314 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
317 /* Private writable mapping? Check memory availability.. */
318 if ((vma
->vm_flags
& (VM_SHARED
| VM_WRITE
)) == VM_WRITE
&&
319 !(flags
& MAP_NORESERVE
) &&
320 !vm_enough_memory(len
>> PAGE_SHIFT
))
324 if (vma
->vm_flags
& VM_DENYWRITE
) {
325 error
= deny_write_access(file
);
332 error
= file
->f_op
->mmap(file
, vma
);
334 goto unmap_and_free_vma
;
335 } else if (flags
& MAP_SHARED
) {
336 error
= map_zero_setup(vma
);
342 * merge_segments may merge our vma, so we can't refer to it
343 * after the call. Save the values we need now ...
345 flags
= vma
->vm_flags
;
347 /* Can addr have changed??
349 * Answer: Yes, several device drivers can do it in their
350 * f_op->mmap method. -DaveM
352 addr
= vma
->vm_start
;
354 lock_vma_mappings(vma
);
355 spin_lock(&mm
->page_table_lock
);
356 __insert_vm_struct(mm
, vma
);
358 atomic_inc(&file
->f_dentry
->d_inode
->i_writecount
);
359 merge_segments(mm
, vma
->vm_start
, vma
->vm_end
);
360 spin_unlock(&mm
->page_table_lock
);
361 unlock_vma_mappings(vma
);
363 mm
->total_vm
+= len
>> PAGE_SHIFT
;
364 if (flags
& VM_LOCKED
) {
365 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
366 make_pages_present(addr
, addr
+ len
);
372 atomic_inc(&file
->f_dentry
->d_inode
->i_writecount
);
375 /* Undo any partial mapping done by a device driver. */
376 flush_cache_range(mm
, vma
->vm_start
, vma
->vm_end
);
377 zap_page_range(mm
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
378 flush_tlb_range(mm
, vma
->vm_start
, vma
->vm_end
);
380 kmem_cache_free(vm_area_cachep
, vma
);
384 /* Get an address range which is currently unmapped.
385 * For mmap() without MAP_FIXED and shmat() with addr=0.
386 * Return value 0 means ENOMEM.
388 #ifndef HAVE_ARCH_UNMAPPED_AREA
389 unsigned long get_unmapped_area(unsigned long addr
, unsigned long len
)
391 struct vm_area_struct
* vmm
;
396 addr
= TASK_UNMAPPED_BASE
;
397 addr
= PAGE_ALIGN(addr
);
399 for (vmm
= find_vma(current
->mm
, addr
); ; vmm
= vmm
->vm_next
) {
400 /* At this point: (!vmm || addr < vmm->vm_end). */
401 if (TASK_SIZE
- len
< addr
)
403 if (!vmm
|| addr
+ len
<= vmm
->vm_start
)
410 #define vm_avl_empty (struct vm_area_struct *) NULL
412 #include "mmap_avl.c"
414 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
415 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
417 struct vm_area_struct
*vma
= NULL
;
420 /* Check the cache first. */
421 /* (Cache hit rate is typically around 35%.) */
422 vma
= mm
->mmap_cache
;
423 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
425 /* Go through the linear list. */
427 while (vma
&& vma
->vm_end
<= addr
)
430 /* Then go through the AVL tree quickly. */
431 struct vm_area_struct
* tree
= mm
->mmap_avl
;
434 if (tree
== vm_avl_empty
)
436 if (tree
->vm_end
> addr
) {
438 if (tree
->vm_start
<= addr
)
440 tree
= tree
->vm_avl_left
;
442 tree
= tree
->vm_avl_right
;
446 mm
->mmap_cache
= vma
;
452 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
453 struct vm_area_struct
* find_vma_prev(struct mm_struct
* mm
, unsigned long addr
,
454 struct vm_area_struct
**pprev
)
458 /* Go through the linear list. */
459 struct vm_area_struct
* prev
= NULL
;
460 struct vm_area_struct
* vma
= mm
->mmap
;
461 while (vma
&& vma
->vm_end
<= addr
) {
468 /* Go through the AVL tree quickly. */
469 struct vm_area_struct
* vma
= NULL
;
470 struct vm_area_struct
* last_turn_right
= NULL
;
471 struct vm_area_struct
* prev
= NULL
;
472 struct vm_area_struct
* tree
= mm
->mmap_avl
;
474 if (tree
== vm_avl_empty
)
476 if (tree
->vm_end
> addr
) {
478 prev
= last_turn_right
;
479 if (tree
->vm_start
<= addr
)
481 tree
= tree
->vm_avl_left
;
483 last_turn_right
= tree
;
484 tree
= tree
->vm_avl_right
;
488 if (vma
->vm_avl_left
!= vm_avl_empty
) {
489 prev
= vma
->vm_avl_left
;
490 while (prev
->vm_avl_right
!= vm_avl_empty
)
491 prev
= prev
->vm_avl_right
;
493 if ((prev
? prev
->vm_next
: mm
->mmap
) != vma
)
494 printk("find_vma_prev: tree inconsistent with list\n");
504 struct vm_area_struct
* find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
506 struct vm_area_struct
* vma
;
510 vma
= find_vma(mm
,addr
);
513 if (vma
->vm_start
<= addr
)
515 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
517 start
= vma
->vm_start
;
518 if (expand_stack(vma
, addr
))
520 if (vma
->vm_flags
& VM_LOCKED
) {
521 make_pages_present(addr
, start
);
526 /* Normal function to fix up a mapping
527 * This function is the default for when an area has no specific
528 * function. This may be used as part of a more specific routine.
529 * This function works out what part of an area is affected and
530 * adjusts the mapping information. Since the actual page
531 * manipulation is done in do_mmap(), none need be done here,
532 * though it would probably be more appropriate.
534 * By the time this function is called, the area struct has been
535 * removed from the process mapping list, so it needs to be
536 * reinserted if necessary.
538 * The 4 main cases are:
539 * Unmapping the whole area
540 * Unmapping from the start of the segment to a point in it
541 * Unmapping from an intermediate point to the end
542 * Unmapping between to intermediate points, making a hole.
544 * Case 4 involves the creation of 2 new areas, for each side of
545 * the hole. If possible, we reuse the existing area rather than
546 * allocate a new one, and the return indicates whether the old
549 static struct vm_area_struct
* unmap_fixup(struct mm_struct
*mm
,
550 struct vm_area_struct
*area
, unsigned long addr
, size_t len
,
551 struct vm_area_struct
*extra
)
553 struct vm_area_struct
*mpnt
;
554 unsigned long end
= addr
+ len
;
556 area
->vm_mm
->total_vm
-= len
>> PAGE_SHIFT
;
557 if (area
->vm_flags
& VM_LOCKED
)
558 area
->vm_mm
->locked_vm
-= len
>> PAGE_SHIFT
;
560 /* Unmapping the whole area. */
561 if (addr
== area
->vm_start
&& end
== area
->vm_end
) {
562 if (area
->vm_ops
&& area
->vm_ops
->close
)
563 area
->vm_ops
->close(area
);
566 kmem_cache_free(vm_area_cachep
, area
);
570 /* Work out to one of the ends. */
571 if (end
== area
->vm_end
) {
573 lock_vma_mappings(area
);
574 spin_lock(&mm
->page_table_lock
);
575 } else if (addr
== area
->vm_start
) {
576 area
->vm_pgoff
+= (end
- area
->vm_start
) >> PAGE_SHIFT
;
577 area
->vm_start
= end
;
578 lock_vma_mappings(area
);
579 spin_lock(&mm
->page_table_lock
);
581 /* Unmapping a hole: area->vm_start < addr <= end < area->vm_end */
582 /* Add end mapping -- leave beginning for below */
586 mpnt
->vm_mm
= area
->vm_mm
;
587 mpnt
->vm_start
= end
;
588 mpnt
->vm_end
= area
->vm_end
;
589 mpnt
->vm_page_prot
= area
->vm_page_prot
;
590 mpnt
->vm_flags
= area
->vm_flags
;
592 mpnt
->vm_ops
= area
->vm_ops
;
593 mpnt
->vm_pgoff
= area
->vm_pgoff
+ ((end
- area
->vm_start
) >> PAGE_SHIFT
);
594 mpnt
->vm_file
= area
->vm_file
;
595 mpnt
->vm_private_data
= area
->vm_private_data
;
597 get_file(mpnt
->vm_file
);
598 if (mpnt
->vm_ops
&& mpnt
->vm_ops
->open
)
599 mpnt
->vm_ops
->open(mpnt
);
600 area
->vm_end
= addr
; /* Truncate area */
602 /* Because mpnt->vm_file == area->vm_file this locks
605 lock_vma_mappings(area
);
606 spin_lock(&mm
->page_table_lock
);
607 __insert_vm_struct(mm
, mpnt
);
610 __insert_vm_struct(mm
, area
);
611 spin_unlock(&mm
->page_table_lock
);
612 unlock_vma_mappings(area
);
617 * Try to free as many page directory entries as we can,
618 * without having to work very hard at actually scanning
619 * the page tables themselves.
621 * Right now we try to free page tables if we have a nice
622 * PGDIR-aligned area that got free'd up. We could be more
623 * granular if we want to, but this is fast and simple,
624 * and covers the bad cases.
626 * "prev", if it exists, points to a vma before the one
627 * we just free'd - but there's no telling how much before.
629 static void free_pgtables(struct mm_struct
* mm
, struct vm_area_struct
*prev
,
630 unsigned long start
, unsigned long end
)
632 unsigned long first
= start
& PGDIR_MASK
;
633 unsigned long last
= end
+ PGDIR_SIZE
- 1;
634 unsigned long start_index
, end_index
;
640 if (prev
->vm_end
> start
) {
641 if (last
> prev
->vm_start
)
642 last
= prev
->vm_start
;
647 struct vm_area_struct
*next
= prev
->vm_next
;
650 if (next
->vm_start
< start
) {
654 if (last
> next
->vm_start
)
655 last
= next
->vm_start
;
657 if (prev
->vm_end
> first
)
658 first
= prev
->vm_end
+ PGDIR_SIZE
- 1;
663 * If the PGD bits are not consecutive in the virtual address, the
664 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
666 start_index
= pgd_index(first
);
667 end_index
= pgd_index(last
);
668 if (end_index
> start_index
) {
669 clear_page_tables(mm
, start_index
, end_index
- start_index
);
670 flush_tlb_pgtables(mm
, first
& PGDIR_MASK
, last
& PGDIR_MASK
);
674 /* Munmap is split into 2 main parts -- this part which finds
675 * what needs doing, and the areas themselves, which do the
676 * work. This now handles partial unmappings.
677 * Jeremy Fitzhardine <jeremy@sw.oz.au>
679 int do_munmap(struct mm_struct
*mm
, unsigned long addr
, size_t len
)
681 struct vm_area_struct
*mpnt
, *prev
, **npp
, *free
, *extra
;
683 if ((addr
& ~PAGE_MASK
) || addr
> TASK_SIZE
|| len
> TASK_SIZE
-addr
)
686 if ((len
= PAGE_ALIGN(len
)) == 0)
689 /* Check if this memory area is ok - put it on the temporary
690 * list if so.. The checks here are pretty simple --
691 * every area affected in some way (by any overlap) is put
692 * on the list. If nothing is put on, nothing is affected.
694 mpnt
= find_vma_prev(mm
, addr
, &prev
);
697 /* we have addr < mpnt->vm_end */
699 if (mpnt
->vm_start
>= addr
+len
)
702 /* If we'll make "hole", check the vm areas limit */
703 if ((mpnt
->vm_start
< addr
&& mpnt
->vm_end
> addr
+len
)
704 && mm
->map_count
>= MAX_MAP_COUNT
)
708 * We may need one additional vma to fix up the mappings ...
709 * and this is the last chance for an easy error exit.
711 extra
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
715 npp
= (prev
? &prev
->vm_next
: &mm
->mmap
);
717 spin_lock(&mm
->page_table_lock
);
718 for ( ; mpnt
&& mpnt
->vm_start
< addr
+len
; mpnt
= *npp
) {
719 *npp
= mpnt
->vm_next
;
720 mpnt
->vm_next
= free
;
723 avl_remove(mpnt
, &mm
->mmap_avl
);
725 mm
->mmap_cache
= NULL
; /* Kill the cache. */
726 spin_unlock(&mm
->page_table_lock
);
728 /* Ok - we have the memory areas we should free on the 'free' list,
729 * so release them, and unmap the page range..
730 * If the one of the segments is only being partially unmapped,
731 * it will put new vm_area_struct(s) into the address space.
732 * In that case we have to be careful with VM_DENYWRITE.
734 while ((mpnt
= free
) != NULL
) {
735 unsigned long st
, end
, size
;
736 struct file
*file
= NULL
;
738 free
= free
->vm_next
;
740 st
= addr
< mpnt
->vm_start
? mpnt
->vm_start
: addr
;
742 end
= end
> mpnt
->vm_end
? mpnt
->vm_end
: end
;
745 if (mpnt
->vm_ops
&& mpnt
->vm_ops
->unmap
)
746 mpnt
->vm_ops
->unmap(mpnt
, st
, size
);
748 if (mpnt
->vm_flags
& VM_DENYWRITE
&&
749 (st
!= mpnt
->vm_start
|| end
!= mpnt
->vm_end
) &&
750 (file
= mpnt
->vm_file
) != NULL
) {
751 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
753 remove_shared_vm_struct(mpnt
);
756 flush_cache_range(mm
, st
, end
);
757 zap_page_range(mm
, st
, size
);
758 flush_tlb_range(mm
, st
, end
);
761 * Fix the mapping, and free the old area if it wasn't reused.
763 extra
= unmap_fixup(mm
, mpnt
, st
, size
, extra
);
765 atomic_inc(&file
->f_dentry
->d_inode
->i_writecount
);
768 /* Release the extra vma struct if it wasn't used */
770 kmem_cache_free(vm_area_cachep
, extra
);
772 free_pgtables(mm
, prev
, addr
, addr
+len
);
777 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
780 struct mm_struct
*mm
= current
->mm
;
783 ret
= do_munmap(mm
, addr
, len
);
789 * this is really a simplified "do_mmap". it only handles
790 * anonymous maps. eventually we may be able to do some
791 * brk-specific accounting here.
793 unsigned long do_brk(unsigned long addr
, unsigned long len
)
795 struct mm_struct
* mm
= current
->mm
;
796 struct vm_area_struct
* vma
;
797 unsigned long flags
, retval
;
799 len
= PAGE_ALIGN(len
);
806 if (mm
->def_flags
& VM_LOCKED
) {
807 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
809 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
814 * Clear old maps. this also does some error checking for us
816 retval
= do_munmap(mm
, addr
, len
);
820 /* Check against address space limits *after* clearing old maps... */
821 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
822 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
825 if (mm
->map_count
> MAX_MAP_COUNT
)
828 if (!vm_enough_memory(len
>> PAGE_SHIFT
))
832 * create a vma struct for an anonymous mapping
834 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
839 vma
->vm_start
= addr
;
840 vma
->vm_end
= addr
+ len
;
841 vma
->vm_flags
= vm_flags(PROT_READ
|PROT_WRITE
|PROT_EXEC
,
842 MAP_FIXED
|MAP_PRIVATE
) | mm
->def_flags
;
844 vma
->vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
845 vma
->vm_page_prot
= protection_map
[vma
->vm_flags
& 0x0f];
849 vma
->vm_private_data
= NULL
;
852 * merge_segments may merge our vma, so we can't refer to it
853 * after the call. Save the values we need now ...
855 flags
= vma
->vm_flags
;
856 addr
= vma
->vm_start
;
858 lock_vma_mappings(vma
);
859 spin_lock(&mm
->page_table_lock
);
860 __insert_vm_struct(mm
, vma
);
861 merge_segments(mm
, vma
->vm_start
, vma
->vm_end
);
862 spin_unlock(&mm
->page_table_lock
);
863 unlock_vma_mappings(vma
);
865 mm
->total_vm
+= len
>> PAGE_SHIFT
;
866 if (flags
& VM_LOCKED
) {
867 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
868 make_pages_present(addr
, addr
+ len
);
873 /* Build the AVL tree corresponding to the VMA list. */
874 void build_mmap_avl(struct mm_struct
* mm
)
876 struct vm_area_struct
* vma
;
879 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
)
880 avl_insert(vma
, &mm
->mmap_avl
);
883 /* Release all mmaps. */
884 void exit_mmap(struct mm_struct
* mm
)
886 struct vm_area_struct
* mpnt
;
888 release_segments(mm
);
889 spin_lock(&mm
->page_table_lock
);
891 mm
->mmap
= mm
->mmap_avl
= mm
->mmap_cache
= NULL
;
892 spin_unlock(&mm
->page_table_lock
);
897 struct vm_area_struct
* next
= mpnt
->vm_next
;
898 unsigned long start
= mpnt
->vm_start
;
899 unsigned long end
= mpnt
->vm_end
;
900 unsigned long size
= end
- start
;
903 if (mpnt
->vm_ops
->unmap
)
904 mpnt
->vm_ops
->unmap(mpnt
, start
, size
);
905 if (mpnt
->vm_ops
->close
)
906 mpnt
->vm_ops
->close(mpnt
);
909 remove_shared_vm_struct(mpnt
);
910 flush_cache_range(mm
, start
, end
);
911 zap_page_range(mm
, start
, size
);
914 kmem_cache_free(vm_area_cachep
, mpnt
);
918 /* This is just debugging */
920 printk("exit_mmap: map count is %d\n", mm
->map_count
);
922 clear_page_tables(mm
, FIRST_USER_PGD_NR
, USER_PTRS_PER_PGD
);
925 /* Insert vm structure into process list sorted by address
926 * and into the inode's i_mmap ring. If vm_file is non-NULL
927 * then the i_shared_lock must be held here.
929 void __insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vmp
)
931 struct vm_area_struct
**pprev
;
936 while (*pprev
&& (*pprev
)->vm_start
<= vmp
->vm_start
)
937 pprev
= &(*pprev
)->vm_next
;
939 struct vm_area_struct
*prev
, *next
;
940 avl_insert_neighbours(vmp
, &mm
->mmap_avl
, &prev
, &next
);
941 pprev
= (prev
? &prev
->vm_next
: &mm
->mmap
);
943 printk("insert_vm_struct: tree inconsistent with list\n");
945 vmp
->vm_next
= *pprev
;
949 if (mm
->map_count
>= AVL_MIN_MAP_COUNT
&& !mm
->mmap_avl
)
954 struct inode
* inode
= file
->f_dentry
->d_inode
;
955 struct address_space
*mapping
= inode
->i_mapping
;
956 struct vm_area_struct
**head
;
958 if (vmp
->vm_flags
& VM_DENYWRITE
)
959 atomic_dec(&inode
->i_writecount
);
961 head
= &mapping
->i_mmap
;
962 if (vmp
->vm_flags
& VM_SHARED
)
963 head
= &mapping
->i_mmap_shared
;
965 /* insert vmp into inode's share list */
966 if((vmp
->vm_next_share
= *head
) != NULL
)
967 (*head
)->vm_pprev_share
= &vmp
->vm_next_share
;
969 vmp
->vm_pprev_share
= head
;
973 void insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vmp
)
975 lock_vma_mappings(vmp
);
976 __insert_vm_struct(mm
, vmp
);
977 unlock_vma_mappings(vmp
);
980 /* Merge the list of memory segments if possible.
981 * Redundant vm_area_structs are freed.
982 * This assumes that the list is ordered by address.
983 * We don't need to traverse the entire list, only those segments
984 * which intersect or are adjacent to a given interval.
986 * We must already hold the mm semaphore when we get here..
988 void merge_segments (struct mm_struct
* mm
, unsigned long start_addr
, unsigned long end_addr
)
990 struct vm_area_struct
*prev
, *mpnt
, *next
, *prev1
;
992 mpnt
= find_vma_prev(mm
, start_addr
, &prev1
);
1000 mpnt
= mpnt
->vm_next
;
1002 mm
->mmap_cache
= NULL
; /* Kill the cache. */
1004 /* prev and mpnt cycle through the list, as long as
1005 * start_addr < mpnt->vm_end && prev->vm_start < end_addr
1007 for ( ; mpnt
&& prev
->vm_start
< end_addr
; prev
= mpnt
, mpnt
= next
) {
1008 next
= mpnt
->vm_next
;
1010 /* To share, we must have the same file, operations.. */
1011 if ((mpnt
->vm_file
!= prev
->vm_file
)||
1012 (mpnt
->vm_private_data
!= prev
->vm_private_data
) ||
1013 (mpnt
->vm_ops
!= prev
->vm_ops
) ||
1014 (mpnt
->vm_flags
!= prev
->vm_flags
) ||
1015 (prev
->vm_end
!= mpnt
->vm_start
))
1019 * If we have a file or it's a shared memory area
1020 * the offsets must be contiguous..
1022 if ((mpnt
->vm_file
!= NULL
) || (mpnt
->vm_flags
& VM_SHM
)) {
1023 unsigned long off
= prev
->vm_pgoff
;
1024 off
+= (prev
->vm_end
- prev
->vm_start
) >> PAGE_SHIFT
;
1025 if (off
!= mpnt
->vm_pgoff
)
1029 /* merge prev with mpnt and set up pointers so the new
1030 * big segment can possibly merge with the next one.
1031 * The old unused mpnt is freed.
1034 avl_remove(mpnt
, &mm
->mmap_avl
);
1035 prev
->vm_end
= mpnt
->vm_end
;
1036 prev
->vm_next
= mpnt
->vm_next
;
1037 if (mpnt
->vm_ops
&& mpnt
->vm_ops
->close
) {
1038 mpnt
->vm_pgoff
+= (mpnt
->vm_end
- mpnt
->vm_start
) >> PAGE_SHIFT
;
1039 mpnt
->vm_start
= mpnt
->vm_end
;
1040 spin_unlock(&mm
->page_table_lock
);
1041 unlock_vma_mappings(mpnt
);
1042 mpnt
->vm_ops
->close(mpnt
);
1043 lock_vma_mappings(mpnt
);
1044 spin_lock(&mm
->page_table_lock
);
1047 __remove_shared_vm_struct(mpnt
);
1049 fput(mpnt
->vm_file
);
1050 kmem_cache_free(vm_area_cachep
, mpnt
);