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 /* SLAB cache for vm_area_struct's. */
40 kmem_cache_t
*vm_area_cachep
;
42 int sysctl_overcommit_memory
;
44 /* Check that a process has enough memory to allocate a
45 * new virtual mapping.
47 int vm_enough_memory(long pages
)
49 /* Stupid algorithm to decide if we have enough memory: while
50 * simple, it hopefully works in most obvious cases.. Easy to
51 * fool it, but this should catch most mistakes.
53 /* 23/11/98 NJC: Somewhat less stupid version of algorithm,
54 * which tries to do "TheRightThing". Instead of using half of
55 * (buffers+cache), use the minimum values. Allow an extra 2%
56 * of num_physpages for safety margin.
61 /* Sometimes we want to use more memory than we have. */
62 if (sysctl_overcommit_memory
)
65 free
= atomic_read(&buffermem_pages
);
66 free
+= atomic_read(&page_cache_size
);
67 free
+= nr_free_pages();
68 free
+= nr_swap_pages
;
72 /* Remove one vm structure from the inode's i_mmap ring. */
73 static inline void remove_shared_vm_struct(struct vm_area_struct
*vma
)
75 struct file
* file
= vma
->vm_file
;
78 struct inode
*inode
= file
->f_dentry
->d_inode
;
79 if (vma
->vm_flags
& VM_DENYWRITE
)
80 atomic_inc(&inode
->i_writecount
);
81 spin_lock(&inode
->i_mapping
->i_shared_lock
);
82 if(vma
->vm_next_share
)
83 vma
->vm_next_share
->vm_pprev_share
= vma
->vm_pprev_share
;
84 *vma
->vm_pprev_share
= vma
->vm_next_share
;
85 spin_unlock(&inode
->i_mapping
->i_shared_lock
);
90 * sys_brk() for the most part doesn't need the global kernel
91 * lock, except when an application is doing something nasty
92 * like trying to un-brk an area that has already been mapped
93 * to a regular file. in this case, the unmapping will need
94 * to invoke file system routines that need the global lock.
96 asmlinkage
unsigned long sys_brk(unsigned long brk
)
98 unsigned long rlim
, retval
;
99 unsigned long newbrk
, oldbrk
;
100 struct mm_struct
*mm
= current
->mm
;
104 if (brk
< mm
->end_code
)
106 newbrk
= PAGE_ALIGN(brk
);
107 oldbrk
= PAGE_ALIGN(mm
->brk
);
108 if (oldbrk
== newbrk
)
111 /* Always allow shrinking brk. */
112 if (brk
<= mm
->brk
) {
113 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
118 /* Check against rlimit.. */
119 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
120 if (rlim
< RLIM_INFINITY
&& brk
- mm
->start_data
> rlim
)
123 /* Check against existing mmap mappings. */
124 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
127 /* Check if we have enough memory.. */
128 if (!vm_enough_memory((newbrk
-oldbrk
) >> PAGE_SHIFT
))
131 /* Ok, looks good - let it rip. */
132 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
142 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
143 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
146 static inline unsigned long vm_flags(unsigned long prot
, unsigned long flags
)
148 #define _trans(x,bit1,bit2) \
149 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
151 unsigned long prot_bits
, flag_bits
;
153 _trans(prot
, PROT_READ
, VM_READ
) |
154 _trans(prot
, PROT_WRITE
, VM_WRITE
) |
155 _trans(prot
, PROT_EXEC
, VM_EXEC
);
157 _trans(flags
, MAP_GROWSDOWN
, VM_GROWSDOWN
) |
158 _trans(flags
, MAP_DENYWRITE
, VM_DENYWRITE
) |
159 _trans(flags
, MAP_EXECUTABLE
, VM_EXECUTABLE
);
160 return prot_bits
| flag_bits
;
164 unsigned long do_mmap_pgoff(struct file
* file
, unsigned long addr
, unsigned long len
,
165 unsigned long prot
, unsigned long flags
, unsigned long pgoff
)
167 struct mm_struct
* mm
= current
->mm
;
168 struct vm_area_struct
* vma
;
171 if (file
&& (!file
->f_op
|| !file
->f_op
->mmap
))
174 if ((len
= PAGE_ALIGN(len
)) == 0)
177 if (len
> TASK_SIZE
|| addr
> TASK_SIZE
-len
)
180 /* offset overflow? */
181 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
184 /* Too many mappings? */
185 if (mm
->map_count
> MAX_MAP_COUNT
)
188 /* mlock MCL_FUTURE? */
189 if (mm
->def_flags
& VM_LOCKED
) {
190 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
192 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
196 /* Do simple checking here so the lower-level routines won't have
197 * to. we assume access permissions have been handled by the open
198 * of the memory object, so we don't do any here.
201 switch (flags
& MAP_TYPE
) {
203 if ((prot
& PROT_WRITE
) && !(file
->f_mode
& FMODE_WRITE
))
206 /* Make sure we don't allow writing to an append-only file.. */
207 if (IS_APPEND(file
->f_dentry
->d_inode
) && (file
->f_mode
& FMODE_WRITE
))
210 /* make sure there are no mandatory locks on the file. */
211 if (locks_verify_locked(file
->f_dentry
->d_inode
))
216 if (!(file
->f_mode
& FMODE_READ
))
225 /* Obtain the address to map to. we verify (or select) it and ensure
226 * that it represents a valid section of the address space.
228 if (flags
& MAP_FIXED
) {
229 if (addr
& ~PAGE_MASK
)
232 addr
= get_unmapped_area(addr
, len
);
237 /* Determine the object being mapped and call the appropriate
238 * specific mapper. the address has already been validated, but
239 * not unmapped, but the maps are removed from the list.
241 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
246 vma
->vm_start
= addr
;
247 vma
->vm_end
= addr
+ len
;
248 vma
->vm_flags
= vm_flags(prot
,flags
) | mm
->def_flags
;
251 VM_ClearReadHint(vma
);
254 if (file
->f_mode
& FMODE_READ
)
255 vma
->vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
256 if (flags
& MAP_SHARED
) {
257 vma
->vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
259 /* This looks strange, but when we don't have the file open
260 * for writing, we can demote the shared mapping to a simpler
261 * private mapping. That also takes care of a security hole
262 * with ptrace() writing to a shared mapping without write
265 * We leave the VM_MAYSHARE bit on, just to get correct output
266 * from /proc/xxx/maps..
268 if (!(file
->f_mode
& FMODE_WRITE
))
269 vma
->vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
272 vma
->vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
273 if (flags
& MAP_SHARED
)
274 vma
->vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
276 vma
->vm_page_prot
= protection_map
[vma
->vm_flags
& 0x0f];
278 vma
->vm_pgoff
= pgoff
;
280 vma
->vm_private_data
= NULL
;
284 if (do_munmap(mm
, addr
, len
))
287 /* Check against address space limit. */
288 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
289 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
292 /* Private writable mapping? Check memory availability.. */
293 if ((vma
->vm_flags
& (VM_SHARED
| VM_WRITE
)) == VM_WRITE
&&
294 !(flags
& MAP_NORESERVE
) &&
295 !vm_enough_memory(len
>> PAGE_SHIFT
))
299 int correct_wcount
= 0;
300 if (vma
->vm_flags
& VM_DENYWRITE
) {
301 if (atomic_read(&file
->f_dentry
->d_inode
->i_writecount
) > 0) {
305 /* f_op->mmap might possibly sleep
306 * (generic_file_mmap doesn't, but other code
307 * might). In any case, this takes care of any
308 * race that this might cause.
310 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
315 error
= file
->f_op
->mmap(file
, vma
);
316 /* Fix up the count if necessary, then check for an error */
318 atomic_inc(&file
->f_dentry
->d_inode
->i_writecount
);
320 goto unmap_and_free_vma
;
321 } else if (flags
& MAP_SHARED
) {
322 error
= map_zero_setup(vma
);
328 * merge_segments may merge our vma, so we can't refer to it
329 * after the call. Save the values we need now ...
331 flags
= vma
->vm_flags
;
332 addr
= vma
->vm_start
; /* can addr have changed?? */
333 vmlist_modify_lock(mm
);
334 insert_vm_struct(mm
, vma
);
335 merge_segments(mm
, vma
->vm_start
, vma
->vm_end
);
336 vmlist_modify_unlock(mm
);
338 mm
->total_vm
+= len
>> PAGE_SHIFT
;
339 if (flags
& VM_LOCKED
) {
340 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
341 make_pages_present(addr
, addr
+ len
);
348 /* Undo any partial mapping done by a device driver. */
349 flush_cache_range(mm
, vma
->vm_start
, vma
->vm_end
);
350 zap_page_range(mm
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
351 flush_tlb_range(mm
, vma
->vm_start
, vma
->vm_end
);
353 kmem_cache_free(vm_area_cachep
, vma
);
357 /* Get an address range which is currently unmapped.
358 * For mmap() without MAP_FIXED and shmat() with addr=0.
359 * Return value 0 means ENOMEM.
361 #ifndef HAVE_ARCH_UNMAPPED_AREA
362 unsigned long get_unmapped_area(unsigned long addr
, unsigned long len
)
364 struct vm_area_struct
* vmm
;
369 addr
= TASK_UNMAPPED_BASE
;
370 addr
= PAGE_ALIGN(addr
);
372 for (vmm
= find_vma(current
->mm
, addr
); ; vmm
= vmm
->vm_next
) {
373 /* At this point: (!vmm || addr < vmm->vm_end). */
374 if (TASK_SIZE
- len
< addr
)
376 if (!vmm
|| addr
+ len
<= vmm
->vm_start
)
383 #define vm_avl_empty (struct vm_area_struct *) NULL
385 #include "mmap_avl.c"
387 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
388 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
390 struct vm_area_struct
*vma
= NULL
;
393 /* Check the cache first. */
394 /* (Cache hit rate is typically around 35%.) */
395 vma
= mm
->mmap_cache
;
396 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
398 /* Go through the linear list. */
400 while (vma
&& vma
->vm_end
<= addr
)
403 /* Then go through the AVL tree quickly. */
404 struct vm_area_struct
* tree
= mm
->mmap_avl
;
407 if (tree
== vm_avl_empty
)
409 if (tree
->vm_end
> addr
) {
411 if (tree
->vm_start
<= addr
)
413 tree
= tree
->vm_avl_left
;
415 tree
= tree
->vm_avl_right
;
419 mm
->mmap_cache
= vma
;
425 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
426 struct vm_area_struct
* find_vma_prev(struct mm_struct
* mm
, unsigned long addr
,
427 struct vm_area_struct
**pprev
)
431 /* Go through the linear list. */
432 struct vm_area_struct
* prev
= NULL
;
433 struct vm_area_struct
* vma
= mm
->mmap
;
434 while (vma
&& vma
->vm_end
<= addr
) {
441 /* Go through the AVL tree quickly. */
442 struct vm_area_struct
* vma
= NULL
;
443 struct vm_area_struct
* last_turn_right
= NULL
;
444 struct vm_area_struct
* prev
= NULL
;
445 struct vm_area_struct
* tree
= mm
->mmap_avl
;
447 if (tree
== vm_avl_empty
)
449 if (tree
->vm_end
> addr
) {
451 prev
= last_turn_right
;
452 if (tree
->vm_start
<= addr
)
454 tree
= tree
->vm_avl_left
;
456 last_turn_right
= tree
;
457 tree
= tree
->vm_avl_right
;
461 if (vma
->vm_avl_left
!= vm_avl_empty
) {
462 prev
= vma
->vm_avl_left
;
463 while (prev
->vm_avl_right
!= vm_avl_empty
)
464 prev
= prev
->vm_avl_right
;
466 if ((prev
? prev
->vm_next
: mm
->mmap
) != vma
)
467 printk("find_vma_prev: tree inconsistent with list\n");
477 struct vm_area_struct
* find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
479 struct vm_area_struct
* vma
;
483 vma
= find_vma(mm
,addr
);
486 if (vma
->vm_start
<= addr
)
488 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
490 start
= vma
->vm_start
;
491 if (expand_stack(vma
, addr
))
493 if (vma
->vm_flags
& VM_LOCKED
) {
494 make_pages_present(addr
, start
);
499 /* Normal function to fix up a mapping
500 * This function is the default for when an area has no specific
501 * function. This may be used as part of a more specific routine.
502 * This function works out what part of an area is affected and
503 * adjusts the mapping information. Since the actual page
504 * manipulation is done in do_mmap(), none need be done here,
505 * though it would probably be more appropriate.
507 * By the time this function is called, the area struct has been
508 * removed from the process mapping list, so it needs to be
509 * reinserted if necessary.
511 * The 4 main cases are:
512 * Unmapping the whole area
513 * Unmapping from the start of the segment to a point in it
514 * Unmapping from an intermediate point to the end
515 * Unmapping between to intermediate points, making a hole.
517 * Case 4 involves the creation of 2 new areas, for each side of
518 * the hole. If possible, we reuse the existing area rather than
519 * allocate a new one, and the return indicates whether the old
522 static struct vm_area_struct
* unmap_fixup(struct mm_struct
*mm
,
523 struct vm_area_struct
*area
, unsigned long addr
, size_t len
,
524 struct vm_area_struct
*extra
)
526 struct vm_area_struct
*mpnt
;
527 unsigned long end
= addr
+ len
;
529 area
->vm_mm
->total_vm
-= len
>> PAGE_SHIFT
;
530 if (area
->vm_flags
& VM_LOCKED
)
531 area
->vm_mm
->locked_vm
-= len
>> PAGE_SHIFT
;
533 /* Unmapping the whole area. */
534 if (addr
== area
->vm_start
&& end
== area
->vm_end
) {
535 if (area
->vm_ops
&& area
->vm_ops
->close
)
536 area
->vm_ops
->close(area
);
539 kmem_cache_free(vm_area_cachep
, area
);
543 /* Work out to one of the ends. */
544 if (end
== area
->vm_end
) {
546 vmlist_modify_lock(mm
);
547 } else if (addr
== area
->vm_start
) {
548 area
->vm_pgoff
+= (end
- area
->vm_start
) >> PAGE_SHIFT
;
549 area
->vm_start
= end
;
550 vmlist_modify_lock(mm
);
552 /* Unmapping a hole: area->vm_start < addr <= end < area->vm_end */
553 /* Add end mapping -- leave beginning for below */
557 mpnt
->vm_mm
= area
->vm_mm
;
558 mpnt
->vm_start
= end
;
559 mpnt
->vm_end
= area
->vm_end
;
560 mpnt
->vm_page_prot
= area
->vm_page_prot
;
561 mpnt
->vm_flags
= area
->vm_flags
;
563 mpnt
->vm_ops
= area
->vm_ops
;
564 mpnt
->vm_pgoff
= area
->vm_pgoff
+ ((end
- area
->vm_start
) >> PAGE_SHIFT
);
565 mpnt
->vm_file
= area
->vm_file
;
566 mpnt
->vm_private_data
= area
->vm_private_data
;
568 get_file(mpnt
->vm_file
);
569 if (mpnt
->vm_ops
&& mpnt
->vm_ops
->open
)
570 mpnt
->vm_ops
->open(mpnt
);
571 area
->vm_end
= addr
; /* Truncate area */
572 vmlist_modify_lock(mm
);
573 insert_vm_struct(mm
, mpnt
);
576 insert_vm_struct(mm
, area
);
577 vmlist_modify_unlock(mm
);
582 * Try to free as many page directory entries as we can,
583 * without having to work very hard at actually scanning
584 * the page tables themselves.
586 * Right now we try to free page tables if we have a nice
587 * PGDIR-aligned area that got free'd up. We could be more
588 * granular if we want to, but this is fast and simple,
589 * and covers the bad cases.
591 * "prev", if it exists, points to a vma before the one
592 * we just free'd - but there's no telling how much before.
594 static void free_pgtables(struct mm_struct
* mm
, struct vm_area_struct
*prev
,
595 unsigned long start
, unsigned long end
)
597 unsigned long first
= start
& PGDIR_MASK
;
598 unsigned long last
= end
+ PGDIR_SIZE
- 1;
599 unsigned long start_index
, end_index
;
605 if (prev
->vm_end
> start
) {
606 if (last
> prev
->vm_start
)
607 last
= prev
->vm_start
;
612 struct vm_area_struct
*next
= prev
->vm_next
;
615 if (next
->vm_start
< start
) {
619 if (last
> next
->vm_start
)
620 last
= next
->vm_start
;
622 if (prev
->vm_end
> first
)
623 first
= prev
->vm_end
+ PGDIR_SIZE
- 1;
628 * If the PGD bits are not consecutive in the virtual address, the
629 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
631 start_index
= pgd_index(first
);
632 end_index
= pgd_index(last
);
633 if (end_index
> start_index
) {
634 clear_page_tables(mm
, start_index
, end_index
- start_index
);
635 flush_tlb_pgtables(mm
, first
& PGDIR_MASK
, last
& PGDIR_MASK
);
639 /* Munmap is split into 2 main parts -- this part which finds
640 * what needs doing, and the areas themselves, which do the
641 * work. This now handles partial unmappings.
642 * Jeremy Fitzhardine <jeremy@sw.oz.au>
644 int do_munmap(struct mm_struct
*mm
, unsigned long addr
, size_t len
)
646 struct vm_area_struct
*mpnt
, *prev
, **npp
, *free
, *extra
;
648 if ((addr
& ~PAGE_MASK
) || addr
> TASK_SIZE
|| len
> TASK_SIZE
-addr
)
651 if ((len
= PAGE_ALIGN(len
)) == 0)
654 /* Check if this memory area is ok - put it on the temporary
655 * list if so.. The checks here are pretty simple --
656 * every area affected in some way (by any overlap) is put
657 * on the list. If nothing is put on, nothing is affected.
659 mpnt
= find_vma_prev(mm
, addr
, &prev
);
662 /* we have addr < mpnt->vm_end */
664 if (mpnt
->vm_start
>= addr
+len
)
667 /* If we'll make "hole", check the vm areas limit */
668 if ((mpnt
->vm_start
< addr
&& mpnt
->vm_end
> addr
+len
)
669 && mm
->map_count
>= MAX_MAP_COUNT
)
673 * We may need one additional vma to fix up the mappings ...
674 * and this is the last chance for an easy error exit.
676 extra
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
680 npp
= (prev
? &prev
->vm_next
: &mm
->mmap
);
682 vmlist_modify_lock(mm
);
683 for ( ; mpnt
&& mpnt
->vm_start
< addr
+len
; mpnt
= *npp
) {
684 *npp
= mpnt
->vm_next
;
685 mpnt
->vm_next
= free
;
688 avl_remove(mpnt
, &mm
->mmap_avl
);
690 mm
->mmap_cache
= NULL
; /* Kill the cache. */
691 vmlist_modify_unlock(mm
);
693 /* Ok - we have the memory areas we should free on the 'free' list,
694 * so release them, and unmap the page range..
695 * If the one of the segments is only being partially unmapped,
696 * it will put new vm_area_struct(s) into the address space.
698 while ((mpnt
= free
) != NULL
) {
699 unsigned long st
, end
, size
;
701 free
= free
->vm_next
;
703 st
= addr
< mpnt
->vm_start
? mpnt
->vm_start
: addr
;
705 end
= end
> mpnt
->vm_end
? mpnt
->vm_end
: end
;
708 if (mpnt
->vm_ops
&& mpnt
->vm_ops
->unmap
)
709 mpnt
->vm_ops
->unmap(mpnt
, st
, size
);
711 remove_shared_vm_struct(mpnt
);
714 flush_cache_range(mm
, st
, end
);
715 zap_page_range(mm
, st
, size
);
716 flush_tlb_range(mm
, st
, end
);
719 * Fix the mapping, and free the old area if it wasn't reused.
721 extra
= unmap_fixup(mm
, mpnt
, st
, size
, extra
);
724 /* Release the extra vma struct if it wasn't used */
726 kmem_cache_free(vm_area_cachep
, extra
);
728 free_pgtables(mm
, prev
, addr
, addr
+len
);
733 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
736 struct mm_struct
*mm
= current
->mm
;
739 ret
= do_munmap(mm
, addr
, len
);
745 * this is really a simplified "do_mmap". it only handles
746 * anonymous maps. eventually we may be able to do some
747 * brk-specific accounting here.
749 unsigned long do_brk(unsigned long addr
, unsigned long len
)
751 struct mm_struct
* mm
= current
->mm
;
752 struct vm_area_struct
* vma
;
753 unsigned long flags
, retval
;
755 len
= PAGE_ALIGN(len
);
762 if (mm
->def_flags
& VM_LOCKED
) {
763 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
765 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
770 * Clear old maps. this also does some error checking for us
772 retval
= do_munmap(mm
, addr
, len
);
776 /* Check against address space limits *after* clearing old maps... */
777 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
778 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
781 if (mm
->map_count
> MAX_MAP_COUNT
)
784 if (!vm_enough_memory(len
>> PAGE_SHIFT
))
788 * create a vma struct for an anonymous mapping
790 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
795 vma
->vm_start
= addr
;
796 vma
->vm_end
= addr
+ len
;
797 vma
->vm_flags
= vm_flags(PROT_READ
|PROT_WRITE
|PROT_EXEC
,
798 MAP_FIXED
|MAP_PRIVATE
) | mm
->def_flags
;
800 vma
->vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
801 vma
->vm_page_prot
= protection_map
[vma
->vm_flags
& 0x0f];
805 vma
->vm_private_data
= NULL
;
808 * merge_segments may merge our vma, so we can't refer to it
809 * after the call. Save the values we need now ...
811 flags
= vma
->vm_flags
;
812 addr
= vma
->vm_start
;
814 vmlist_modify_lock(mm
);
815 insert_vm_struct(mm
, vma
);
816 merge_segments(mm
, vma
->vm_start
, vma
->vm_end
);
817 vmlist_modify_unlock(mm
);
819 mm
->total_vm
+= len
>> PAGE_SHIFT
;
820 if (flags
& VM_LOCKED
) {
821 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
822 make_pages_present(addr
, addr
+ len
);
827 /* Build the AVL tree corresponding to the VMA list. */
828 void build_mmap_avl(struct mm_struct
* mm
)
830 struct vm_area_struct
* vma
;
833 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
)
834 avl_insert(vma
, &mm
->mmap_avl
);
837 /* Release all mmaps. */
838 void exit_mmap(struct mm_struct
* mm
)
840 struct vm_area_struct
* mpnt
;
842 release_segments(mm
);
844 vmlist_modify_lock(mm
);
845 mm
->mmap
= mm
->mmap_avl
= mm
->mmap_cache
= NULL
;
846 vmlist_modify_unlock(mm
);
851 struct vm_area_struct
* next
= mpnt
->vm_next
;
852 unsigned long start
= mpnt
->vm_start
;
853 unsigned long end
= mpnt
->vm_end
;
854 unsigned long size
= end
- start
;
857 if (mpnt
->vm_ops
->unmap
)
858 mpnt
->vm_ops
->unmap(mpnt
, start
, size
);
859 if (mpnt
->vm_ops
->close
)
860 mpnt
->vm_ops
->close(mpnt
);
863 remove_shared_vm_struct(mpnt
);
864 zap_page_range(mm
, start
, size
);
867 kmem_cache_free(vm_area_cachep
, mpnt
);
871 /* This is just debugging */
873 printk("exit_mmap: map count is %d\n", mm
->map_count
);
875 clear_page_tables(mm
, FIRST_USER_PGD_NR
, USER_PTRS_PER_PGD
);
878 /* Insert vm structure into process list sorted by address
879 * and into the inode's i_mmap ring.
881 void insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vmp
)
883 struct vm_area_struct
**pprev
;
888 while (*pprev
&& (*pprev
)->vm_start
<= vmp
->vm_start
)
889 pprev
= &(*pprev
)->vm_next
;
891 struct vm_area_struct
*prev
, *next
;
892 avl_insert_neighbours(vmp
, &mm
->mmap_avl
, &prev
, &next
);
893 pprev
= (prev
? &prev
->vm_next
: &mm
->mmap
);
895 printk("insert_vm_struct: tree inconsistent with list\n");
897 vmp
->vm_next
= *pprev
;
901 if (mm
->map_count
>= AVL_MIN_MAP_COUNT
&& !mm
->mmap_avl
)
906 struct inode
* inode
= file
->f_dentry
->d_inode
;
907 struct address_space
*mapping
= inode
->i_mapping
;
908 if (vmp
->vm_flags
& VM_DENYWRITE
)
909 atomic_dec(&inode
->i_writecount
);
911 /* insert vmp into inode's share list */
912 spin_lock(&mapping
->i_shared_lock
);
913 if((vmp
->vm_next_share
= mapping
->i_mmap
) != NULL
)
914 mapping
->i_mmap
->vm_pprev_share
= &vmp
->vm_next_share
;
915 mapping
->i_mmap
= vmp
;
916 vmp
->vm_pprev_share
= &mapping
->i_mmap
;
917 spin_unlock(&mapping
->i_shared_lock
);
921 /* Merge the list of memory segments if possible.
922 * Redundant vm_area_structs are freed.
923 * This assumes that the list is ordered by address.
924 * We don't need to traverse the entire list, only those segments
925 * which intersect or are adjacent to a given interval.
927 * We must already hold the mm semaphore when we get here..
929 void merge_segments (struct mm_struct
* mm
, unsigned long start_addr
, unsigned long end_addr
)
931 struct vm_area_struct
*prev
, *mpnt
, *next
, *prev1
;
933 mpnt
= find_vma_prev(mm
, start_addr
, &prev1
);
941 mpnt
= mpnt
->vm_next
;
943 mm
->mmap_cache
= NULL
; /* Kill the cache. */
945 /* prev and mpnt cycle through the list, as long as
946 * start_addr < mpnt->vm_end && prev->vm_start < end_addr
948 for ( ; mpnt
&& prev
->vm_start
< end_addr
; prev
= mpnt
, mpnt
= next
) {
949 next
= mpnt
->vm_next
;
951 /* To share, we must have the same file, operations.. */
952 if ((mpnt
->vm_file
!= prev
->vm_file
)||
953 (mpnt
->vm_private_data
!= prev
->vm_private_data
) ||
954 (mpnt
->vm_ops
!= prev
->vm_ops
) ||
955 (mpnt
->vm_flags
!= prev
->vm_flags
) ||
956 (prev
->vm_end
!= mpnt
->vm_start
))
960 * If we have a file or it's a shared memory area
961 * the offsets must be contiguous..
963 if ((mpnt
->vm_file
!= NULL
) || (mpnt
->vm_flags
& VM_SHM
)) {
964 unsigned long off
= prev
->vm_pgoff
;
965 off
+= (prev
->vm_end
- prev
->vm_start
) >> PAGE_SHIFT
;
966 if (off
!= mpnt
->vm_pgoff
)
970 /* merge prev with mpnt and set up pointers so the new
971 * big segment can possibly merge with the next one.
972 * The old unused mpnt is freed.
975 avl_remove(mpnt
, &mm
->mmap_avl
);
976 prev
->vm_end
= mpnt
->vm_end
;
977 prev
->vm_next
= mpnt
->vm_next
;
978 if (mpnt
->vm_ops
&& mpnt
->vm_ops
->close
) {
979 mpnt
->vm_pgoff
+= (mpnt
->vm_end
- mpnt
->vm_start
) >> PAGE_SHIFT
;
980 mpnt
->vm_start
= mpnt
->vm_end
;
981 vmlist_modify_unlock(mm
);
982 mpnt
->vm_ops
->close(mpnt
);
983 vmlist_modify_lock(mm
);
986 remove_shared_vm_struct(mpnt
);
989 kmem_cache_free(vm_area_cachep
, mpnt
);
994 void __init
vma_init(void)
996 vm_area_cachep
= kmem_cache_create("vm_area_struct",
997 sizeof(struct vm_area_struct
),
998 0, SLAB_HWCACHE_ALIGN
,
1001 panic("vma_init: Cannot alloc vm_area_struct cache.");
1003 mm_cachep
= kmem_cache_create("mm_struct",
1004 sizeof(struct mm_struct
),
1005 0, SLAB_HWCACHE_ALIGN
,
1008 panic("vma_init: Cannot alloc mm_struct cache.");