Optimize andes_clear_page() and andes_copy_page() with prefetch
[linux-2.6/linux-mips.git] / mm / mmap.c
blobea8eba78333b59a20ac390831478af920c74e19c
1 /*
2 * linux/mm/mmap.c
4 * Written by obz.
5 */
6 #include <linux/slab.h>
7 #include <linux/shm.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:
23 * map_type prot
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.
59 long free;
61 /* Sometimes we want to use more memory than we have. */
62 if (sysctl_overcommit_memory)
63 return 1;
65 free = atomic_read(&buffermem_pages);
66 free += atomic_read(&page_cache_size);
67 free += nr_free_pages();
68 free += nr_swap_pages;
69 return free > pages;
72 /* Remove one vm structure from the inode's i_mapping address space. */
73 static inline void remove_shared_vm_struct(struct vm_area_struct *vma)
75 struct file * file = vma->vm_file;
77 if (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;
102 down(&mm->mmap_sem);
104 if (brk < mm->end_code)
105 goto out;
106 newbrk = PAGE_ALIGN(brk);
107 oldbrk = PAGE_ALIGN(mm->brk);
108 if (oldbrk == newbrk)
109 goto set_brk;
111 /* Always allow shrinking brk. */
112 if (brk <= mm->brk) {
113 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
114 goto set_brk;
115 goto out;
118 /* Check against rlimit.. */
119 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
120 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
121 goto out;
123 /* Check against existing mmap mappings. */
124 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
125 goto out;
127 /* Check if we have enough memory.. */
128 if (!vm_enough_memory((newbrk-oldbrk) >> PAGE_SHIFT))
129 goto out;
131 /* Ok, looks good - let it rip. */
132 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
133 goto out;
134 set_brk:
135 mm->brk = brk;
136 out:
137 retval = mm->brk;
138 up(&mm->mmap_sem);
139 return retval;
142 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
143 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
144 * into "VM_xxx".
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;
152 prot_bits =
153 _trans(prot, PROT_READ, VM_READ) |
154 _trans(prot, PROT_WRITE, VM_WRITE) |
155 _trans(prot, PROT_EXEC, VM_EXEC);
156 flag_bits =
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;
161 #undef _trans
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;
169 int correct_wcount = 0;
170 int error;
172 if (file && (!file->f_op || !file->f_op->mmap))
173 return -ENODEV;
175 if ((len = PAGE_ALIGN(len)) == 0)
176 return addr;
178 if (len > TASK_SIZE || addr > TASK_SIZE-len)
179 return -EINVAL;
181 /* offset overflow? */
182 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
183 return -EINVAL;
185 /* Too many mappings? */
186 if (mm->map_count > MAX_MAP_COUNT)
187 return -ENOMEM;
189 /* mlock MCL_FUTURE? */
190 if (mm->def_flags & VM_LOCKED) {
191 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
192 locked += len;
193 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
194 return -EAGAIN;
197 /* Do simple checking here so the lower-level routines won't have
198 * to. we assume access permissions have been handled by the open
199 * of the memory object, so we don't do any here.
201 if (file != NULL) {
202 switch (flags & MAP_TYPE) {
203 case MAP_SHARED:
204 if ((prot & PROT_WRITE) && !(file->f_mode & FMODE_WRITE))
205 return -EACCES;
207 /* Make sure we don't allow writing to an append-only file.. */
208 if (IS_APPEND(file->f_dentry->d_inode) && (file->f_mode & FMODE_WRITE))
209 return -EACCES;
211 /* make sure there are no mandatory locks on the file. */
212 if (locks_verify_locked(file->f_dentry->d_inode))
213 return -EAGAIN;
215 /* fall through */
216 case MAP_PRIVATE:
217 if (!(file->f_mode & FMODE_READ))
218 return -EACCES;
219 break;
221 default:
222 return -EINVAL;
226 /* Obtain the address to map to. we verify (or select) it and ensure
227 * that it represents a valid section of the address space.
229 if (flags & MAP_FIXED) {
230 if (addr & ~PAGE_MASK)
231 return -EINVAL;
232 } else {
233 addr = get_unmapped_area(addr, len);
234 if (!addr)
235 return -ENOMEM;
238 /* Determine the object being mapped and call the appropriate
239 * specific mapper. the address has already been validated, but
240 * not unmapped, but the maps are removed from the list.
242 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
243 if (!vma)
244 return -ENOMEM;
246 vma->vm_mm = mm;
247 vma->vm_start = addr;
248 vma->vm_end = addr + len;
249 vma->vm_flags = vm_flags(prot,flags) | mm->def_flags;
251 if (file) {
252 VM_ClearReadHint(vma);
253 vma->vm_raend = 0;
255 if (file->f_mode & FMODE_READ)
256 vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
257 if (flags & MAP_SHARED) {
258 vma->vm_flags |= VM_SHARED | VM_MAYSHARE;
260 /* This looks strange, but when we don't have the file open
261 * for writing, we can demote the shared mapping to a simpler
262 * private mapping. That also takes care of a security hole
263 * with ptrace() writing to a shared mapping without write
264 * permissions.
266 * We leave the VM_MAYSHARE bit on, just to get correct output
267 * from /proc/xxx/maps..
269 if (!(file->f_mode & FMODE_WRITE))
270 vma->vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
272 } else {
273 vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
274 if (flags & MAP_SHARED)
275 vma->vm_flags |= VM_SHARED | VM_MAYSHARE;
277 vma->vm_page_prot = protection_map[vma->vm_flags & 0x0f];
278 vma->vm_ops = NULL;
279 vma->vm_pgoff = pgoff;
280 vma->vm_file = NULL;
281 vma->vm_private_data = NULL;
283 /* Clear old maps */
284 error = -ENOMEM;
285 if (do_munmap(mm, addr, len))
286 goto free_vma;
288 /* Check against address space limit. */
289 if ((mm->total_vm << PAGE_SHIFT) + len
290 > current->rlim[RLIMIT_AS].rlim_cur)
291 goto free_vma;
293 /* Private writable mapping? Check memory availability.. */
294 if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) == VM_WRITE &&
295 !(flags & MAP_NORESERVE) &&
296 !vm_enough_memory(len >> PAGE_SHIFT))
297 goto free_vma;
299 if (file) {
300 if (vma->vm_flags & VM_DENYWRITE) {
301 error = deny_write_access(file);
302 if (error)
303 goto free_vma;
304 correct_wcount = 1;
306 vma->vm_file = file;
307 get_file(file);
308 error = file->f_op->mmap(file, vma);
309 if (error)
310 goto unmap_and_free_vma;
311 } else if (flags & MAP_SHARED) {
312 error = map_zero_setup(vma);
313 if (error)
314 goto free_vma;
318 * merge_segments may merge our vma, so we can't refer to it
319 * after the call. Save the values we need now ...
321 flags = vma->vm_flags;
322 addr = vma->vm_start; /* can addr have changed?? */
323 vmlist_modify_lock(mm);
324 insert_vm_struct(mm, vma);
325 if (correct_wcount)
326 atomic_inc(&file->f_dentry->d_inode->i_writecount);
327 merge_segments(mm, vma->vm_start, vma->vm_end);
328 vmlist_modify_unlock(mm);
330 mm->total_vm += len >> PAGE_SHIFT;
331 if (flags & VM_LOCKED) {
332 mm->locked_vm += len >> PAGE_SHIFT;
333 make_pages_present(addr, addr + len);
335 return addr;
337 unmap_and_free_vma:
338 if (correct_wcount)
339 atomic_inc(&file->f_dentry->d_inode->i_writecount);
340 vma->vm_file = NULL;
341 fput(file);
342 /* Undo any partial mapping done by a device driver. */
343 flush_cache_range(mm, vma->vm_start, vma->vm_end);
344 zap_page_range(mm, vma->vm_start, vma->vm_end - vma->vm_start);
345 flush_tlb_range(mm, vma->vm_start, vma->vm_end);
346 free_vma:
347 kmem_cache_free(vm_area_cachep, vma);
348 return error;
351 /* Get an address range which is currently unmapped.
352 * For mmap() without MAP_FIXED and shmat() with addr=0.
353 * Return value 0 means ENOMEM.
355 #ifndef HAVE_ARCH_UNMAPPED_AREA
356 unsigned long get_unmapped_area(unsigned long addr, unsigned long len)
358 struct vm_area_struct * vmm;
360 if (len > TASK_SIZE)
361 return 0;
362 if (!addr)
363 addr = TASK_UNMAPPED_BASE;
364 addr = PAGE_ALIGN(addr);
366 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
367 /* At this point: (!vmm || addr < vmm->vm_end). */
368 if (TASK_SIZE - len < addr)
369 return 0;
370 if (!vmm || addr + len <= vmm->vm_start)
371 return addr;
372 addr = vmm->vm_end;
375 #endif
377 #define vm_avl_empty (struct vm_area_struct *) NULL
379 #include "mmap_avl.c"
381 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
382 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
384 struct vm_area_struct *vma = NULL;
386 if (mm) {
387 /* Check the cache first. */
388 /* (Cache hit rate is typically around 35%.) */
389 vma = mm->mmap_cache;
390 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
391 if (!mm->mmap_avl) {
392 /* Go through the linear list. */
393 vma = mm->mmap;
394 while (vma && vma->vm_end <= addr)
395 vma = vma->vm_next;
396 } else {
397 /* Then go through the AVL tree quickly. */
398 struct vm_area_struct * tree = mm->mmap_avl;
399 vma = NULL;
400 for (;;) {
401 if (tree == vm_avl_empty)
402 break;
403 if (tree->vm_end > addr) {
404 vma = tree;
405 if (tree->vm_start <= addr)
406 break;
407 tree = tree->vm_avl_left;
408 } else
409 tree = tree->vm_avl_right;
412 if (vma)
413 mm->mmap_cache = vma;
416 return vma;
419 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
420 struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
421 struct vm_area_struct **pprev)
423 if (mm) {
424 if (!mm->mmap_avl) {
425 /* Go through the linear list. */
426 struct vm_area_struct * prev = NULL;
427 struct vm_area_struct * vma = mm->mmap;
428 while (vma && vma->vm_end <= addr) {
429 prev = vma;
430 vma = vma->vm_next;
432 *pprev = prev;
433 return vma;
434 } else {
435 /* Go through the AVL tree quickly. */
436 struct vm_area_struct * vma = NULL;
437 struct vm_area_struct * last_turn_right = NULL;
438 struct vm_area_struct * prev = NULL;
439 struct vm_area_struct * tree = mm->mmap_avl;
440 for (;;) {
441 if (tree == vm_avl_empty)
442 break;
443 if (tree->vm_end > addr) {
444 vma = tree;
445 prev = last_turn_right;
446 if (tree->vm_start <= addr)
447 break;
448 tree = tree->vm_avl_left;
449 } else {
450 last_turn_right = tree;
451 tree = tree->vm_avl_right;
454 if (vma) {
455 if (vma->vm_avl_left != vm_avl_empty) {
456 prev = vma->vm_avl_left;
457 while (prev->vm_avl_right != vm_avl_empty)
458 prev = prev->vm_avl_right;
460 if ((prev ? prev->vm_next : mm->mmap) != vma)
461 printk("find_vma_prev: tree inconsistent with list\n");
462 *pprev = prev;
463 return vma;
467 *pprev = NULL;
468 return NULL;
471 struct vm_area_struct * find_extend_vma(struct mm_struct * mm, unsigned long addr)
473 struct vm_area_struct * vma;
474 unsigned long start;
476 addr &= PAGE_MASK;
477 vma = find_vma(mm,addr);
478 if (!vma)
479 return NULL;
480 if (vma->vm_start <= addr)
481 return vma;
482 if (!(vma->vm_flags & VM_GROWSDOWN))
483 return NULL;
484 start = vma->vm_start;
485 if (expand_stack(vma, addr))
486 return NULL;
487 if (vma->vm_flags & VM_LOCKED) {
488 make_pages_present(addr, start);
490 return vma;
493 /* Normal function to fix up a mapping
494 * This function is the default for when an area has no specific
495 * function. This may be used as part of a more specific routine.
496 * This function works out what part of an area is affected and
497 * adjusts the mapping information. Since the actual page
498 * manipulation is done in do_mmap(), none need be done here,
499 * though it would probably be more appropriate.
501 * By the time this function is called, the area struct has been
502 * removed from the process mapping list, so it needs to be
503 * reinserted if necessary.
505 * The 4 main cases are:
506 * Unmapping the whole area
507 * Unmapping from the start of the segment to a point in it
508 * Unmapping from an intermediate point to the end
509 * Unmapping between to intermediate points, making a hole.
511 * Case 4 involves the creation of 2 new areas, for each side of
512 * the hole. If possible, we reuse the existing area rather than
513 * allocate a new one, and the return indicates whether the old
514 * area was reused.
516 static struct vm_area_struct * unmap_fixup(struct mm_struct *mm,
517 struct vm_area_struct *area, unsigned long addr, size_t len,
518 struct vm_area_struct *extra)
520 struct vm_area_struct *mpnt;
521 unsigned long end = addr + len;
523 area->vm_mm->total_vm -= len >> PAGE_SHIFT;
524 if (area->vm_flags & VM_LOCKED)
525 area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
527 /* Unmapping the whole area. */
528 if (addr == area->vm_start && end == area->vm_end) {
529 if (area->vm_ops && area->vm_ops->close)
530 area->vm_ops->close(area);
531 if (area->vm_file)
532 fput(area->vm_file);
533 kmem_cache_free(vm_area_cachep, area);
534 return extra;
537 /* Work out to one of the ends. */
538 if (end == area->vm_end) {
539 area->vm_end = addr;
540 vmlist_modify_lock(mm);
541 } else if (addr == area->vm_start) {
542 area->vm_pgoff += (end - area->vm_start) >> PAGE_SHIFT;
543 area->vm_start = end;
544 vmlist_modify_lock(mm);
545 } else {
546 /* Unmapping a hole: area->vm_start < addr <= end < area->vm_end */
547 /* Add end mapping -- leave beginning for below */
548 mpnt = extra;
549 extra = NULL;
551 mpnt->vm_mm = area->vm_mm;
552 mpnt->vm_start = end;
553 mpnt->vm_end = area->vm_end;
554 mpnt->vm_page_prot = area->vm_page_prot;
555 mpnt->vm_flags = area->vm_flags;
556 mpnt->vm_raend = 0;
557 mpnt->vm_ops = area->vm_ops;
558 mpnt->vm_pgoff = area->vm_pgoff + ((end - area->vm_start) >> PAGE_SHIFT);
559 mpnt->vm_file = area->vm_file;
560 mpnt->vm_private_data = area->vm_private_data;
561 if (mpnt->vm_file)
562 get_file(mpnt->vm_file);
563 if (mpnt->vm_ops && mpnt->vm_ops->open)
564 mpnt->vm_ops->open(mpnt);
565 area->vm_end = addr; /* Truncate area */
566 vmlist_modify_lock(mm);
567 insert_vm_struct(mm, mpnt);
570 insert_vm_struct(mm, area);
571 vmlist_modify_unlock(mm);
572 return extra;
576 * Try to free as many page directory entries as we can,
577 * without having to work very hard at actually scanning
578 * the page tables themselves.
580 * Right now we try to free page tables if we have a nice
581 * PGDIR-aligned area that got free'd up. We could be more
582 * granular if we want to, but this is fast and simple,
583 * and covers the bad cases.
585 * "prev", if it exists, points to a vma before the one
586 * we just free'd - but there's no telling how much before.
588 static void free_pgtables(struct mm_struct * mm, struct vm_area_struct *prev,
589 unsigned long start, unsigned long end)
591 unsigned long first = start & PGDIR_MASK;
592 unsigned long last = end + PGDIR_SIZE - 1;
593 unsigned long start_index, end_index;
595 if (!prev) {
596 prev = mm->mmap;
597 if (!prev)
598 goto no_mmaps;
599 if (prev->vm_end > start) {
600 if (last > prev->vm_start)
601 last = prev->vm_start;
602 goto no_mmaps;
605 for (;;) {
606 struct vm_area_struct *next = prev->vm_next;
608 if (next) {
609 if (next->vm_start < start) {
610 prev = next;
611 continue;
613 if (last > next->vm_start)
614 last = next->vm_start;
616 if (prev->vm_end > first)
617 first = prev->vm_end + PGDIR_SIZE - 1;
618 break;
620 no_mmaps:
622 * If the PGD bits are not consecutive in the virtual address, the
623 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
625 start_index = pgd_index(first);
626 end_index = pgd_index(last);
627 if (end_index > start_index) {
628 clear_page_tables(mm, start_index, end_index - start_index);
629 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
633 /* Munmap is split into 2 main parts -- this part which finds
634 * what needs doing, and the areas themselves, which do the
635 * work. This now handles partial unmappings.
636 * Jeremy Fitzhardine <jeremy@sw.oz.au>
638 int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
640 struct vm_area_struct *mpnt, *prev, **npp, *free, *extra;
642 if ((addr & ~PAGE_MASK) || addr > TASK_SIZE || len > TASK_SIZE-addr)
643 return -EINVAL;
645 if ((len = PAGE_ALIGN(len)) == 0)
646 return -EINVAL;
648 /* Check if this memory area is ok - put it on the temporary
649 * list if so.. The checks here are pretty simple --
650 * every area affected in some way (by any overlap) is put
651 * on the list. If nothing is put on, nothing is affected.
653 mpnt = find_vma_prev(mm, addr, &prev);
654 if (!mpnt)
655 return 0;
656 /* we have addr < mpnt->vm_end */
658 if (mpnt->vm_start >= addr+len)
659 return 0;
661 /* If we'll make "hole", check the vm areas limit */
662 if ((mpnt->vm_start < addr && mpnt->vm_end > addr+len)
663 && mm->map_count >= MAX_MAP_COUNT)
664 return -ENOMEM;
667 * We may need one additional vma to fix up the mappings ...
668 * and this is the last chance for an easy error exit.
670 extra = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
671 if (!extra)
672 return -ENOMEM;
674 npp = (prev ? &prev->vm_next : &mm->mmap);
675 free = NULL;
676 vmlist_modify_lock(mm);
677 for ( ; mpnt && mpnt->vm_start < addr+len; mpnt = *npp) {
678 *npp = mpnt->vm_next;
679 mpnt->vm_next = free;
680 free = mpnt;
681 if (mm->mmap_avl)
682 avl_remove(mpnt, &mm->mmap_avl);
684 mm->mmap_cache = NULL; /* Kill the cache. */
685 vmlist_modify_unlock(mm);
687 /* Ok - we have the memory areas we should free on the 'free' list,
688 * so release them, and unmap the page range..
689 * If the one of the segments is only being partially unmapped,
690 * it will put new vm_area_struct(s) into the address space.
691 * In that case we have to be careful with VM_DENYWRITE.
693 while ((mpnt = free) != NULL) {
694 unsigned long st, end, size;
695 struct file *file = NULL;
697 free = free->vm_next;
699 st = addr < mpnt->vm_start ? mpnt->vm_start : addr;
700 end = addr+len;
701 end = end > mpnt->vm_end ? mpnt->vm_end : end;
702 size = end - st;
704 if (mpnt->vm_ops && mpnt->vm_ops->unmap)
705 mpnt->vm_ops->unmap(mpnt, st, size);
707 if (mpnt->vm_flags & VM_DENYWRITE &&
708 (st != mpnt->vm_start || end != mpnt->vm_end) &&
709 (file = mpnt->vm_file) != NULL) {
710 atomic_dec(&file->f_dentry->d_inode->i_writecount);
712 remove_shared_vm_struct(mpnt);
713 mm->map_count--;
715 flush_cache_range(mm, st, end);
716 zap_page_range(mm, st, size);
717 flush_tlb_range(mm, st, end);
720 * Fix the mapping, and free the old area if it wasn't reused.
722 extra = unmap_fixup(mm, mpnt, st, size, extra);
723 if (file)
724 atomic_inc(&file->f_dentry->d_inode->i_writecount);
727 /* Release the extra vma struct if it wasn't used */
728 if (extra)
729 kmem_cache_free(vm_area_cachep, extra);
731 free_pgtables(mm, prev, addr, addr+len);
733 return 0;
736 asmlinkage long sys_munmap(unsigned long addr, size_t len)
738 int ret;
739 struct mm_struct *mm = current->mm;
741 down(&mm->mmap_sem);
742 ret = do_munmap(mm, addr, len);
743 up(&mm->mmap_sem);
744 return ret;
748 * this is really a simplified "do_mmap". it only handles
749 * anonymous maps. eventually we may be able to do some
750 * brk-specific accounting here.
752 unsigned long do_brk(unsigned long addr, unsigned long len)
754 struct mm_struct * mm = current->mm;
755 struct vm_area_struct * vma;
756 unsigned long flags, retval;
758 len = PAGE_ALIGN(len);
759 if (!len)
760 return addr;
763 * mlock MCL_FUTURE?
765 if (mm->def_flags & VM_LOCKED) {
766 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
767 locked += len;
768 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
769 return -EAGAIN;
773 * Clear old maps. this also does some error checking for us
775 retval = do_munmap(mm, addr, len);
776 if (retval != 0)
777 return retval;
779 /* Check against address space limits *after* clearing old maps... */
780 if ((mm->total_vm << PAGE_SHIFT) + len
781 > current->rlim[RLIMIT_AS].rlim_cur)
782 return -ENOMEM;
784 if (mm->map_count > MAX_MAP_COUNT)
785 return -ENOMEM;
787 if (!vm_enough_memory(len >> PAGE_SHIFT))
788 return -ENOMEM;
791 * create a vma struct for an anonymous mapping
793 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
794 if (!vma)
795 return -ENOMEM;
797 vma->vm_mm = mm;
798 vma->vm_start = addr;
799 vma->vm_end = addr + len;
800 vma->vm_flags = vm_flags(PROT_READ|PROT_WRITE|PROT_EXEC,
801 MAP_FIXED|MAP_PRIVATE) | mm->def_flags;
803 vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
804 vma->vm_page_prot = protection_map[vma->vm_flags & 0x0f];
805 vma->vm_ops = NULL;
806 vma->vm_pgoff = 0;
807 vma->vm_file = NULL;
808 vma->vm_private_data = NULL;
811 * merge_segments may merge our vma, so we can't refer to it
812 * after the call. Save the values we need now ...
814 flags = vma->vm_flags;
815 addr = vma->vm_start;
817 vmlist_modify_lock(mm);
818 insert_vm_struct(mm, vma);
819 merge_segments(mm, vma->vm_start, vma->vm_end);
820 vmlist_modify_unlock(mm);
822 mm->total_vm += len >> PAGE_SHIFT;
823 if (flags & VM_LOCKED) {
824 mm->locked_vm += len >> PAGE_SHIFT;
825 make_pages_present(addr, addr + len);
827 return addr;
830 /* Build the AVL tree corresponding to the VMA list. */
831 void build_mmap_avl(struct mm_struct * mm)
833 struct vm_area_struct * vma;
835 mm->mmap_avl = NULL;
836 for (vma = mm->mmap; vma; vma = vma->vm_next)
837 avl_insert(vma, &mm->mmap_avl);
840 /* Release all mmaps. */
841 void exit_mmap(struct mm_struct * mm)
843 struct vm_area_struct * mpnt;
845 release_segments(mm);
846 mpnt = mm->mmap;
847 vmlist_modify_lock(mm);
848 mm->mmap = mm->mmap_avl = mm->mmap_cache = NULL;
849 vmlist_modify_unlock(mm);
850 mm->rss = 0;
851 mm->total_vm = 0;
852 mm->locked_vm = 0;
853 while (mpnt) {
854 struct vm_area_struct * next = mpnt->vm_next;
855 unsigned long start = mpnt->vm_start;
856 unsigned long end = mpnt->vm_end;
857 unsigned long size = end - start;
859 if (mpnt->vm_ops) {
860 if (mpnt->vm_ops->unmap)
861 mpnt->vm_ops->unmap(mpnt, start, size);
862 if (mpnt->vm_ops->close)
863 mpnt->vm_ops->close(mpnt);
865 mm->map_count--;
866 remove_shared_vm_struct(mpnt);
867 zap_page_range(mm, start, size);
868 if (mpnt->vm_file)
869 fput(mpnt->vm_file);
870 kmem_cache_free(vm_area_cachep, mpnt);
871 mpnt = next;
874 /* This is just debugging */
875 if (mm->map_count)
876 printk("exit_mmap: map count is %d\n", mm->map_count);
878 clear_page_tables(mm, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
881 /* Insert vm structure into process list sorted by address
882 * and into the inode's i_mmap ring.
884 void insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vmp)
886 struct vm_area_struct **pprev;
887 struct file * file;
889 if (!mm->mmap_avl) {
890 pprev = &mm->mmap;
891 while (*pprev && (*pprev)->vm_start <= vmp->vm_start)
892 pprev = &(*pprev)->vm_next;
893 } else {
894 struct vm_area_struct *prev, *next;
895 avl_insert_neighbours(vmp, &mm->mmap_avl, &prev, &next);
896 pprev = (prev ? &prev->vm_next : &mm->mmap);
897 if (*pprev != next)
898 printk("insert_vm_struct: tree inconsistent with list\n");
900 vmp->vm_next = *pprev;
901 *pprev = vmp;
903 mm->map_count++;
904 if (mm->map_count >= AVL_MIN_MAP_COUNT && !mm->mmap_avl)
905 build_mmap_avl(mm);
907 file = vmp->vm_file;
908 if (file) {
909 struct inode * inode = file->f_dentry->d_inode;
910 struct address_space *mapping = inode->i_mapping;
911 if (vmp->vm_flags & VM_DENYWRITE)
912 atomic_dec(&inode->i_writecount);
914 /* insert vmp into inode's share list */
915 spin_lock(&mapping->i_shared_lock);
916 if((vmp->vm_next_share = mapping->i_mmap) != NULL)
917 mapping->i_mmap->vm_pprev_share = &vmp->vm_next_share;
918 mapping->i_mmap = vmp;
919 vmp->vm_pprev_share = &mapping->i_mmap;
920 spin_unlock(&mapping->i_shared_lock);
924 /* Merge the list of memory segments if possible.
925 * Redundant vm_area_structs are freed.
926 * This assumes that the list is ordered by address.
927 * We don't need to traverse the entire list, only those segments
928 * which intersect or are adjacent to a given interval.
930 * We must already hold the mm semaphore when we get here..
932 void merge_segments (struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
934 struct vm_area_struct *prev, *mpnt, *next, *prev1;
936 mpnt = find_vma_prev(mm, start_addr, &prev1);
937 if (!mpnt)
938 return;
940 if (prev1) {
941 prev = prev1;
942 } else {
943 prev = mpnt;
944 mpnt = mpnt->vm_next;
946 mm->mmap_cache = NULL; /* Kill the cache. */
948 /* prev and mpnt cycle through the list, as long as
949 * start_addr < mpnt->vm_end && prev->vm_start < end_addr
951 for ( ; mpnt && prev->vm_start < end_addr ; prev = mpnt, mpnt = next) {
952 next = mpnt->vm_next;
954 /* To share, we must have the same file, operations.. */
955 if ((mpnt->vm_file != prev->vm_file)||
956 (mpnt->vm_private_data != prev->vm_private_data) ||
957 (mpnt->vm_ops != prev->vm_ops) ||
958 (mpnt->vm_flags != prev->vm_flags) ||
959 (prev->vm_end != mpnt->vm_start))
960 continue;
963 * If we have a file or it's a shared memory area
964 * the offsets must be contiguous..
966 if ((mpnt->vm_file != NULL) || (mpnt->vm_flags & VM_SHM)) {
967 unsigned long off = prev->vm_pgoff;
968 off += (prev->vm_end - prev->vm_start) >> PAGE_SHIFT;
969 if (off != mpnt->vm_pgoff)
970 continue;
973 /* merge prev with mpnt and set up pointers so the new
974 * big segment can possibly merge with the next one.
975 * The old unused mpnt is freed.
977 if (mm->mmap_avl)
978 avl_remove(mpnt, &mm->mmap_avl);
979 prev->vm_end = mpnt->vm_end;
980 prev->vm_next = mpnt->vm_next;
981 if (mpnt->vm_ops && mpnt->vm_ops->close) {
982 mpnt->vm_pgoff += (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
983 mpnt->vm_start = mpnt->vm_end;
984 vmlist_modify_unlock(mm);
985 mpnt->vm_ops->close(mpnt);
986 vmlist_modify_lock(mm);
988 mm->map_count--;
989 remove_shared_vm_struct(mpnt);
990 if (mpnt->vm_file)
991 fput(mpnt->vm_file);
992 kmem_cache_free(vm_area_cachep, mpnt);
993 mpnt = prev;
997 void __init vma_init(void)
999 vm_area_cachep = kmem_cache_create("vm_area_struct",
1000 sizeof(struct vm_area_struct),
1001 0, SLAB_HWCACHE_ALIGN,
1002 NULL, NULL);
1003 if(!vm_area_cachep)
1004 panic("vma_init: Cannot alloc vm_area_struct cache.");
1006 mm_cachep = kmem_cache_create("mm_struct",
1007 sizeof(struct mm_struct),
1008 0, SLAB_HWCACHE_ALIGN,
1009 NULL, NULL);
1010 if(!mm_cachep)
1011 panic("vma_init: Cannot alloc mm_struct cache.");