• source navigation  • diff markup  • identifier search  • freetext search  • file search  • 


Version: 2.6.8   2.6.16   2.6.25   2.6.30   2.6.34  

Architecture: i386   arm   mips   ppc   alpha   m68k   sparc   sparc64  

linux/mm/mmap.c


  1 /*
  2  * mm/mmap.c
  3  *
  4  * Written by obz.
  5  *
  6  * Address space accounting code        <alan@redhat.com>
  7  */
  8 
  9 #include <linux/slab.h>
 10 #include <linux/shm.h>
 11 #include <linux/mman.h>
 12 #include <linux/pagemap.h>
 13 #include <linux/swap.h>
 14 #include <linux/syscalls.h>
 15 #include <linux/init.h>
 16 #include <linux/file.h>
 17 #include <linux/fs.h>
 18 #include <linux/personality.h>
 19 #include <linux/security.h>
 20 #include <linux/hugetlb.h>
 21 #include <linux/profile.h>
 22 #include <linux/module.h>
 23 #include <linux/mount.h>
 24 #include <linux/mempolicy.h>
 25 #include <linux/rmap.h>
 26 
 27 #include <asm/uaccess.h>
 28 #include <asm/cacheflush.h>
 29 #include <asm/tlb.h>
 30 
 31 /*
 32  * WARNING: the debugging will use recursive algorithms so never enable this
 33  * unless you know what you are doing.
 34  */
 35 #undef DEBUG_MM_RB
 36 
 37 /* description of effects of mapping type and prot in current implementation.
 38  * this is due to the limited x86 page protection hardware.  The expected
 39  * behavior is in parens:
 40  *
 41  * map_type     prot
 42  *              PROT_NONE       PROT_READ       PROT_WRITE      PROT_EXEC
 43  * MAP_SHARED   r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
 44  *              w: (no) no      w: (no) no      w: (yes) yes    w: (no) no
 45  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
 46  *              
 47  * MAP_PRIVATE  r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
 48  *              w: (no) no      w: (no) no      w: (copy) copy  w: (no) no
 49  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
 50  *
 51  */
 52 pgprot_t protection_map[16] = {
 53         __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
 54         __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
 55 };
 56 
 57 int sysctl_overcommit_memory = 0;       /* default is heuristic overcommit */
 58 int sysctl_overcommit_ratio = 50;       /* default is 50% */
 59 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
 60 atomic_t vm_committed_space = ATOMIC_INIT(0);
 61 
 62 EXPORT_SYMBOL(sysctl_overcommit_memory);
 63 EXPORT_SYMBOL(sysctl_overcommit_ratio);
 64 EXPORT_SYMBOL(sysctl_max_map_count);
 65 EXPORT_SYMBOL(vm_committed_space);
 66 
 67 /*
 68  * Requires inode->i_mapping->i_mmap_lock
 69  */
 70 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
 71                 struct file *file, struct address_space *mapping)
 72 {
 73         if (vma->vm_flags & VM_DENYWRITE)
 74                 atomic_inc(&file->f_dentry->d_inode->i_writecount);
 75         if (vma->vm_flags & VM_SHARED)
 76                 mapping->i_mmap_writable--;
 77 
 78         flush_dcache_mmap_lock(mapping);
 79         if (unlikely(vma->vm_flags & VM_NONLINEAR))
 80                 list_del_init(&vma->shared.vm_set.list);
 81         else
 82                 vma_prio_tree_remove(vma, &mapping->i_mmap);
 83         flush_dcache_mmap_unlock(mapping);
 84 }
 85 
 86 /*
 87  * Remove one vm structure and free it.
 88  */
 89 static void remove_vm_struct(struct vm_area_struct *vma)
 90 {
 91         struct file *file = vma->vm_file;
 92 
 93         if (file) {
 94                 struct address_space *mapping = file->f_mapping;
 95                 spin_lock(&mapping->i_mmap_lock);
 96                 __remove_shared_vm_struct(vma, file, mapping);
 97                 spin_unlock(&mapping->i_mmap_lock);
 98         }
 99         if (vma->vm_ops && vma->vm_ops->close)
100                 vma->vm_ops->close(vma);
101         if (file)
102                 fput(file);
103         anon_vma_unlink(vma);
104         mpol_free(vma_policy(vma));
105         kmem_cache_free(vm_area_cachep, vma);
106 }
107 
108 /*
109  *  sys_brk() for the most part doesn't need the global kernel
110  *  lock, except when an application is doing something nasty
111  *  like trying to un-brk an area that has already been mapped
112  *  to a regular file.  in this case, the unmapping will need
113  *  to invoke file system routines that need the global lock.
114  */
115 asmlinkage unsigned long sys_brk(unsigned long brk)
116 {
117         unsigned long rlim, retval;
118         unsigned long newbrk, oldbrk;
119         struct mm_struct *mm = current->mm;
120 
121         down_write(&mm->mmap_sem);
122 
123         if (brk < mm->end_code)
124                 goto out;
125         newbrk = PAGE_ALIGN(brk);
126         oldbrk = PAGE_ALIGN(mm->brk);
127         if (oldbrk == newbrk)
128                 goto set_brk;
129 
130         /* Always allow shrinking brk. */
131         if (brk <= mm->brk) {
132                 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
133                         goto set_brk;
134                 goto out;
135         }
136 
137         /* Check against rlimit.. */
138         rlim = current->rlim[RLIMIT_DATA].rlim_cur;
139         if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
140                 goto out;
141 
142         /* Check against existing mmap mappings. */
143         if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
144                 goto out;
145 
146         /* Ok, looks good - let it rip. */
147         if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
148                 goto out;
149 set_brk:
150         mm->brk = brk;
151 out:
152         retval = mm->brk;
153         up_write(&mm->mmap_sem);
154         return retval;
155 }
156 
157 #ifdef DEBUG_MM_RB
158 static int browse_rb(struct rb_root *root)
159 {
160         int i = 0, j;
161         struct rb_node *nd, *pn = NULL;
162         unsigned long prev = 0, pend = 0;
163 
164         for (nd = rb_first(root); nd; nd = rb_next(nd)) {
165                 struct vm_area_struct *vma;
166                 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
167                 if (vma->vm_start < prev)
168                         printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
169                 if (vma->vm_start < pend)
170                         printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
171                 if (vma->vm_start > vma->vm_end)
172                         printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
173                 i++;
174                 pn = nd;
175         }
176         j = 0;
177         for (nd = pn; nd; nd = rb_prev(nd)) {
178                 j++;
179         }
180         if (i != j)
181                 printk("backwards %d, forwards %d\n", j, i), i = 0;
182         return i;
183 }
184 
185 void validate_mm(struct mm_struct *mm)
186 {
187         int bug = 0;
188         int i = 0;
189         struct vm_area_struct *tmp = mm->mmap;
190         while (tmp) {
191                 tmp = tmp->vm_next;
192                 i++;
193         }
194         if (i != mm->map_count)
195                 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
196         i = browse_rb(&mm->mm_rb);
197         if (i != mm->map_count)
198                 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
199         if (bug)
200                 BUG();
201 }
202 #else
203 #define validate_mm(mm) do { } while (0)
204 #endif
205 
206 static struct vm_area_struct *
207 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
208                 struct vm_area_struct **pprev, struct rb_node ***rb_link,
209                 struct rb_node ** rb_parent)
210 {
211         struct vm_area_struct * vma;
212         struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
213 
214         __rb_link = &mm->mm_rb.rb_node;
215         rb_prev = __rb_parent = NULL;
216         vma = NULL;
217 
218         while (*__rb_link) {
219                 struct vm_area_struct *vma_tmp;
220 
221                 __rb_parent = *__rb_link;
222                 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
223 
224                 if (vma_tmp->vm_end > addr) {
225                         vma = vma_tmp;
226                         if (vma_tmp->vm_start <= addr)
227                                 return vma;
228                         __rb_link = &__rb_parent->rb_left;
229                 } else {
230                         rb_prev = __rb_parent;
231                         __rb_link = &__rb_parent->rb_right;
232                 }
233         }
234 
235         *pprev = NULL;
236         if (rb_prev)
237                 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
238         *rb_link = __rb_link;
239         *rb_parent = __rb_parent;
240         return vma;
241 }
242 
243 static inline void
244 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
245                 struct vm_area_struct *prev, struct rb_node *rb_parent)
246 {
247         if (prev) {
248                 vma->vm_next = prev->vm_next;
249                 prev->vm_next = vma;
250         } else {
251                 mm->mmap = vma;
252                 if (rb_parent)
253                         vma->vm_next = rb_entry(rb_parent,
254                                         struct vm_area_struct, vm_rb);
255                 else
256                         vma->vm_next = NULL;
257         }
258 }
259 
260 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
261                 struct rb_node **rb_link, struct rb_node *rb_parent)
262 {
263         rb_link_node(&vma->vm_rb, rb_parent, rb_link);
264         rb_insert_color(&vma->vm_rb, &mm->mm_rb);
265 }
266 
267 static inline void __vma_link_file(struct vm_area_struct *vma)
268 {
269         struct file * file;
270 
271         file = vma->vm_file;
272         if (file) {
273                 struct address_space *mapping = file->f_mapping;
274 
275                 if (vma->vm_flags & VM_DENYWRITE)
276                         atomic_dec(&file->f_dentry->d_inode->i_writecount);
277                 if (vma->vm_flags & VM_SHARED)
278                         mapping->i_mmap_writable++;
279 
280                 flush_dcache_mmap_lock(mapping);
281                 if (unlikely(vma->vm_flags & VM_NONLINEAR))
282                         list_add_tail(&vma->shared.vm_set.list,
283                                         &mapping->i_mmap_nonlinear);
284                 else
285                         vma_prio_tree_insert(vma, &mapping->i_mmap);
286                 flush_dcache_mmap_unlock(mapping);
287         }
288 }
289 
290 static void
291 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
292         struct vm_area_struct *prev, struct rb_node **rb_link,
293         struct rb_node *rb_parent)
294 {
295         __vma_link_list(mm, vma, prev, rb_parent);
296         __vma_link_rb(mm, vma, rb_link, rb_parent);
297         __anon_vma_link(vma);
298 }
299 
300 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
301                         struct vm_area_struct *prev, struct rb_node **rb_link,
302                         struct rb_node *rb_parent)
303 {
304         struct address_space *mapping = NULL;
305 
306         if (vma->vm_file)
307                 mapping = vma->vm_file->f_mapping;
308 
309         if (mapping)
310                 spin_lock(&mapping->i_mmap_lock);
311         anon_vma_lock(vma);
312 
313         __vma_link(mm, vma, prev, rb_link, rb_parent);
314         __vma_link_file(vma);
315 
316         anon_vma_unlock(vma);
317         if (mapping)
318                 spin_unlock(&mapping->i_mmap_lock);
319 
320         mm->map_count++;
321         validate_mm(mm);
322 }
323 
324 /*
325  * Helper for vma_adjust in the split_vma insert case:
326  * insert vm structure into list and rbtree and anon_vma,
327  * but it has already been inserted into prio_tree earlier.
328  */
329 static void
330 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
331 {
332         struct vm_area_struct * __vma, * prev;
333         struct rb_node ** rb_link, * rb_parent;
334 
335         __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
336         if (__vma && __vma->vm_start < vma->vm_end)
337                 BUG();
338         __vma_link(mm, vma, prev, rb_link, rb_parent);
339         mm->map_count++;
340 }
341 
342 static inline void
343 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
344                 struct vm_area_struct *prev)
345 {
346         prev->vm_next = vma->vm_next;
347         rb_erase(&vma->vm_rb, &mm->mm_rb);
348         if (mm->mmap_cache == vma)
349                 mm->mmap_cache = prev;
350 }
351 
352 /*
353  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
354  * is already present in an i_mmap tree without adjusting the tree.
355  * The following helper function should be used when such adjustments
356  * are necessary.  The "insert" vma (if any) is to be inserted
357  * before we drop the necessary locks.
358  */
359 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
360         unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
361 {
362         struct mm_struct *mm = vma->vm_mm;
363         struct vm_area_struct *next = vma->vm_next;
364         struct vm_area_struct *importer = NULL;
365         struct address_space *mapping = NULL;
366         struct prio_tree_root *root = NULL;
367         struct file *file = vma->vm_file;
368         struct anon_vma *anon_vma = NULL;
369         long adjust_next = 0;
370         int remove_next = 0;
371 
372         if (next && !insert) {
373                 if (end >= next->vm_end) {
374                         /*
375                          * vma expands, overlapping all the next, and
376                          * perhaps the one after too (mprotect case 6).
377                          */
378 again:                  remove_next = 1 + (end > next->vm_end);
379                         end = next->vm_end;
380                         anon_vma = next->anon_vma;
381                 } else if (end > next->vm_start) {
382                         /*
383                          * vma expands, overlapping part of the next:
384                          * mprotect case 5 shifting the boundary up.
385                          */
386                         adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
387                         anon_vma = next->anon_vma;
388                         importer = vma;
389                 } else if (end < vma->vm_end) {
390                         /*
391                          * vma shrinks, and !insert tells it's not
392                          * split_vma inserting another: so it must be
393                          * mprotect case 4 shifting the boundary down.
394                          */
395                         adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
396                         anon_vma = next->anon_vma;
397                         importer = next;
398                 }
399         }
400 
401         if (file) {
402                 mapping = file->f_mapping;
403                 if (!(vma->vm_flags & VM_NONLINEAR))
404                         root = &mapping->i_mmap;
405                 spin_lock(&mapping->i_mmap_lock);
406                 if (insert) {
407                         /*
408                          * Put into prio_tree now, so instantiated pages
409                          * are visible to arm/parisc __flush_dcache_page
410                          * throughout; but we cannot insert into address
411                          * space until vma start or end is updated.
412                          */
413                         __vma_link_file(insert);
414                 }
415         }
416 
417         /*
418          * When changing only vma->vm_end, we don't really need
419          * anon_vma lock: but is that case worth optimizing out?
420          */
421         if (vma->anon_vma)
422                 anon_vma = vma->anon_vma;
423         if (anon_vma) {
424                 spin_lock(&anon_vma->lock);
425                 /*
426                  * Easily overlooked: when mprotect shifts the boundary,
427                  * make sure the expanding vma has anon_vma set if the
428                  * shrinking vma had, to cover any anon pages imported.
429                  */
430                 if (importer && !importer->anon_vma) {
431                         importer->anon_vma = anon_vma;
432                         __anon_vma_link(importer);
433                 }
434         }
435 
436         if (root) {
437                 flush_dcache_mmap_lock(mapping);
438                 vma_prio_tree_remove(vma, root);
439                 if (adjust_next)
440                         vma_prio_tree_remove(next, root);
441         }
442 
443         vma->vm_start = start;
444         vma->vm_end = end;
445         vma->vm_pgoff = pgoff;
446         if (adjust_next) {
447                 next->vm_start += adjust_next << PAGE_SHIFT;
448                 next->vm_pgoff += adjust_next;
449         }
450 
451         if (root) {
452                 if (adjust_next) {
453                         vma_prio_tree_init(next);
454                         vma_prio_tree_insert(next, root);
455                 }
456                 vma_prio_tree_init(vma);
457                 vma_prio_tree_insert(vma, root);
458                 flush_dcache_mmap_unlock(mapping);
459         }
460 
461         if (remove_next) {
462                 /*
463                  * vma_merge has merged next into vma, and needs
464                  * us to remove next before dropping the locks.
465                  */
466                 __vma_unlink(mm, next, vma);
467                 if (file)
468                         __remove_shared_vm_struct(next, file, mapping);
469                 if (next->anon_vma)
470                         __anon_vma_merge(vma, next);
471         } else if (insert) {
472                 /*
473                  * split_vma has split insert from vma, and needs
474                  * us to insert it before dropping the locks
475                  * (it may either follow vma or precede it).
476                  */
477                 __insert_vm_struct(mm, insert);
478         }
479 
480         if (anon_vma)
481                 spin_unlock(&anon_vma->lock);
482         if (mapping)
483                 spin_unlock(&mapping->i_mmap_lock);
484 
485         if (remove_next) {
486                 if (file)
487                         fput(file);
488                 mm->map_count--;
489                 mpol_free(vma_policy(next));
490                 kmem_cache_free(vm_area_cachep, next);
491                 /*
492                  * In mprotect's case 6 (see comments on vma_merge),
493                  * we must remove another next too. It would clutter
494                  * up the code too much to do both in one go.
495                  */
496                 if (remove_next == 2) {
497                         next = vma->vm_next;
498                         goto again;
499                 }
500         }
501 
502         validate_mm(mm);
503 }
504 
505 /*
506  * If the vma has a ->close operation then the driver probably needs to release
507  * per-vma resources, so we don't attempt to merge those.
508  */
509 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
510 
511 static inline int is_mergeable_vma(struct vm_area_struct *vma,
512                         struct file *file, unsigned long vm_flags)
513 {
514         if (vma->vm_flags != vm_flags)
515                 return 0;
516         if (vma->vm_file != file)
517                 return 0;
518         if (vma->vm_ops && vma->vm_ops->close)
519                 return 0;
520         return 1;
521 }
522 
523 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
524                                         struct anon_vma *anon_vma2)
525 {
526         return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
527 }
528 
529 /*
530  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
531  * in front of (at a lower virtual address and file offset than) the vma.
532  *
533  * We cannot merge two vmas if they have differently assigned (non-NULL)
534  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
535  *
536  * We don't check here for the merged mmap wrapping around the end of pagecache
537  * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
538  * wrap, nor mmaps which cover the final page at index -1UL.
539  */
540 static int
541 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
542         struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
543 {
544         if (is_mergeable_vma(vma, file, vm_flags) &&
545             is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
546                 if (vma->vm_pgoff == vm_pgoff)
547                         return 1;
548         }
549         return 0;
550 }
551 
552 /*
553  * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
554  * beyond (at a higher virtual address and file offset than) the vma.
555  *
556  * We cannot merge two vmas if they have differently assigned (non-NULL)
557  * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
558  */
559 static int
560 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
561         struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
562 {
563         if (is_mergeable_vma(vma, file, vm_flags) &&
564             is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
565                 pgoff_t vm_pglen;
566                 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
567                 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
568                         return 1;
569         }
570         return 0;
571 }
572 
573 /*
574  * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
575  * whether that can be merged with its predecessor or its successor.
576  * Or both (it neatly fills a hole).
577  *
578  * In most cases - when called for mmap, brk or mremap - [addr,end) is
579  * certain not to be mapped by the time vma_merge is called; but when
580  * called for mprotect, it is certain to be already mapped (either at
581  * an offset within prev, or at the start of next), and the flags of
582  * this area are about to be changed to vm_flags - and the no-change
583  * case has already been eliminated.
584  *
585  * The following mprotect cases have to be considered, where AAAA is
586  * the area passed down from mprotect_fixup, never extending beyond one
587  * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
588  *
589  *     AAAA             AAAA                AAAA          AAAA
590  *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
591  *    cannot merge    might become    might become    might become
592  *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
593  *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
594  *    mremap move:                                    PPPPNNNNNNNN 8
595  *        AAAA
596  *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
597  *    might become    case 1 below    case 2 below    case 3 below
598  *
599  * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
600  * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
601  */
602 struct vm_area_struct *vma_merge(struct mm_struct *mm,
603                         struct vm_area_struct *prev, unsigned long addr,
604                         unsigned long end, unsigned long vm_flags,
605                         struct anon_vma *anon_vma, struct file *file,
606                         pgoff_t pgoff, struct mempolicy *policy)
607 {
608         pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
609         struct vm_area_struct *area, *next;
610 
611         /*
612          * We later require that vma->vm_flags == vm_flags,
613          * so this tests vma->vm_flags & VM_SPECIAL, too.
614          */
615         if (vm_flags & VM_SPECIAL)
616                 return NULL;
617 
618         if (prev)
619                 next = prev->vm_next;
620         else
621                 next = mm->mmap;
622         area = next;
623         if (next && next->vm_end == end)                /* cases 6, 7, 8 */
624                 next = next->vm_next;
625 
626         /*
627          * Can it merge with the predecessor?
628          */
629         if (prev && prev->vm_end == addr &&
630                         mpol_equal(vma_policy(prev), policy) &&
631                         can_vma_merge_after(prev, vm_flags,
632                                                 anon_vma, file, pgoff)) {
633                 /*
634                  * OK, it can.  Can we now merge in the successor as well?
635                  */
636                 if (next && end == next->vm_start &&
637                                 mpol_equal(policy, vma_policy(next)) &&
638                                 can_vma_merge_before(next, vm_flags,
639                                         anon_vma, file, pgoff+pglen) &&
640                                 is_mergeable_anon_vma(prev->anon_vma,
641                                                       next->anon_vma)) {
642                                                         /* cases 1, 6 */
643                         vma_adjust(prev, prev->vm_start,
644                                 next->vm_end, prev->vm_pgoff, NULL);
645                 } else                                  /* cases 2, 5, 7 */
646                         vma_adjust(prev, prev->vm_start,
647                                 end, prev->vm_pgoff, NULL);
648                 return prev;
649         }
650 
651         /*
652          * Can this new request be merged in front of next?
653          */
654         if (next && end == next->vm_start &&
655                         mpol_equal(policy, vma_policy(next)) &&
656                         can_vma_merge_before(next, vm_flags,
657                                         anon_vma, file, pgoff+pglen)) {
658                 if (prev && addr < prev->vm_end)        /* case 4 */
659                         vma_adjust(prev, prev->vm_start,
660                                 addr, prev->vm_pgoff, NULL);
661                 else                                    /* cases 3, 8 */
662                         vma_adjust(area, addr, next->vm_end,
663                                 next->vm_pgoff - pglen, NULL);
664                 return area;
665         }
666 
667         return NULL;
668 }
669 
670 /*
671  * find_mergeable_anon_vma is used by anon_vma_prepare, to check
672  * neighbouring vmas for a suitable anon_vma, before it goes off
673  * to allocate a new anon_vma.  It checks because a repetitive
674  * sequence of mprotects and faults may otherwise lead to distinct
675  * anon_vmas being allocated, preventing vma merge in subsequent
676  * mprotect.
677  */
678 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
679 {
680         struct vm_area_struct *near;
681         unsigned long vm_flags;
682 
683         near = vma->vm_next;
684         if (!near)
685                 goto try_prev;
686 
687         /*
688          * Since only mprotect tries to remerge vmas, match flags
689          * which might be mprotected into each other later on.
690          * Neither mlock nor madvise tries to remerge at present,
691          * so leave their flags as obstructing a merge.
692          */
693         vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
694         vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
695 
696         if (near->anon_vma && vma->vm_end == near->vm_start &&
697                         mpol_equal(vma_policy(vma), vma_policy(near)) &&
698                         can_vma_merge_before(near, vm_flags,
699                                 NULL, vma->vm_file, vma->vm_pgoff +
700                                 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
701                 return near->anon_vma;
702 try_prev:
703         /*
704          * It is potentially slow to have to call find_vma_prev here.
705          * But it's only on the first write fault on the vma, not
706          * every time, and we could devise a way to avoid it later
707          * (e.g. stash info in next's anon_vma_node when assigning
708          * an anon_vma, or when trying vma_merge).  Another time.
709          */
710         if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
711                 BUG();
712         if (!near)
713                 goto none;
714 
715         vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
716         vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
717 
718         if (near->anon_vma && near->vm_end == vma->vm_start &&
719                         mpol_equal(vma_policy(near), vma_policy(vma)) &&
720                         can_vma_merge_after(near, vm_flags,
721                                 NULL, vma->vm_file, vma->vm_pgoff))
722                 return near->anon_vma;
723 none:
724         /*
725          * There's no absolute need to look only at touching neighbours:
726          * we could search further afield for "compatible" anon_vmas.
727          * But it would probably just be a waste of time searching,
728          * or lead to too many vmas hanging off the same anon_vma.
729          * We're trying to allow mprotect remerging later on,
730          * not trying to minimize memory used for anon_vmas.
731          */
732         return NULL;
733 }
734 
735 /*
736  * The caller must hold down_write(current->mm->mmap_sem).
737  */
738 
739 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
740                         unsigned long len, unsigned long prot,
741                         unsigned long flags, unsigned long pgoff)
742 {
743         struct mm_struct * mm = current->mm;
744         struct vm_area_struct * vma, * prev;
745         struct inode *inode;
746         unsigned int vm_flags;
747         int correct_wcount = 0;
748         int error;
749         struct rb_node ** rb_link, * rb_parent;
750         int accountable = 1;
751         unsigned long charged = 0;
752 
753         /*
754          * Does the application expect PROT_READ to imply PROT_EXEC:
755          */
756         if (unlikely((prot & PROT_READ) &&
757                         (current->personality & READ_IMPLIES_EXEC)))
758                 prot |= PROT_EXEC;
759 
760         if (file) {
761                 if (is_file_hugepages(file))
762                         accountable = 0;
763 
764                 if (!file->f_op || !file->f_op->mmap)
765                         return -ENODEV;
766 
767                 if ((prot & PROT_EXEC) &&
768                     (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
769                         return -EPERM;
770         }
771 
772         if (!len)
773                 return addr;
774 
775         /* Careful about overflows.. */
776         len = PAGE_ALIGN(len);
777         if (!len || len > TASK_SIZE)
778                 return -EINVAL;
779 
780         /* offset overflow? */
781         if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
782                 return -EINVAL;
783 
784         /* Too many mappings? */
785         if (mm->map_count > sysctl_max_map_count)
786                 return -ENOMEM;
787 
788         /* Obtain the address to map to. we verify (or select) it and ensure
789          * that it represents a valid section of the address space.
790          */
791         addr = get_unmapped_area(file, addr, len, pgoff, flags);
792         if (addr & ~PAGE_MASK)
793                 return addr;
794 
795         /* Do simple checking here so the lower-level routines won't have
796          * to. we assume access permissions have been handled by the open
797          * of the memory object, so we don't do any here.
798          */
799         vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
800                         mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
801 
802         if (flags & MAP_LOCKED) {
803                 if (!capable(CAP_IPC_LOCK))
804                         return -EPERM;
805                 vm_flags |= VM_LOCKED;
806         }
807         /* mlock MCL_FUTURE? */
808         if (vm_flags & VM_LOCKED) {
809                 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
810                 locked += len;
811                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
812                         return -EAGAIN;
813         }
814 
815         inode = file ? file->f_dentry->d_inode : NULL;
816 
817         if (file) {
818                 switch (flags & MAP_TYPE) {
819                 case MAP_SHARED:
820                         if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
821                                 return -EACCES;
822 
823                         /*
824                          * Make sure we don't allow writing to an append-only
825                          * file..
826                          */
827                         if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
828                                 return -EACCES;
829 
830                         /*
831                          * Make sure there are no mandatory locks on the file.
832                          */
833                         if (locks_verify_locked(inode))
834                                 return -EAGAIN;
835 
836                         vm_flags |= VM_SHARED | VM_MAYSHARE;
837                         if (!(file->f_mode & FMODE_WRITE))
838                                 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
839 
840                         /* fall through */
841                 case MAP_PRIVATE:
842                         if (!(file->f_mode & FMODE_READ))
843                                 return -EACCES;
844                         break;
845 
846                 default:
847                         return -EINVAL;
848                 }
849         } else {
850                 switch (flags & MAP_TYPE) {
851                 case MAP_SHARED:
852                         vm_flags |= VM_SHARED | VM_MAYSHARE;
853                         break;
854                 case MAP_PRIVATE:
855                         /*
856                          * Set pgoff according to addr for anon_vma.
857                          */
858                         pgoff = addr >> PAGE_SHIFT;
859                         break;
860                 default:
861                         return -EINVAL;
862                 }
863         }
864 
865         error = security_file_mmap(file, prot, flags);
866         if (error)
867                 return error;
868                 
869         /* Clear old maps */
870         error = -ENOMEM;
871 munmap_back:
872         vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
873         if (vma && vma->vm_start < addr + len) {
874                 if (do_munmap(mm, addr, len))
875                         return -ENOMEM;
876                 goto munmap_back;
877         }
878 
879         /* Check against address space limit. */
880         if ((mm->total_vm << PAGE_SHIFT) + len
881             > current->rlim[RLIMIT_AS].rlim_cur)
882                 return -ENOMEM;
883 
884         if (accountable && (!(flags & MAP_NORESERVE) ||
885                         sysctl_overcommit_memory > 1)) {
886                 if (vm_flags & VM_SHARED) {
887                         /* Check memory availability in shmem_file_setup? */
888                         vm_flags |= VM_ACCOUNT;
889                 } else if (vm_flags & VM_WRITE) {
890                         /*
891                          * Private writable mapping: check memory availability
892                          */
893                         charged = len >> PAGE_SHIFT;
894                         if (security_vm_enough_memory(charged))
895                                 return -ENOMEM;
896                         vm_flags |= VM_ACCOUNT;
897                 }
898         }
899 
900         /*
901          * Can we just expand an old private anonymous mapping?
902          * The VM_SHARED test is necessary because shmem_zero_setup
903          * will create the file object for a shared anonymous map below.
904          */
905         if (!file && !(vm_flags & VM_SHARED) &&
906             vma_merge(mm, prev, addr, addr + len, vm_flags,
907                                         NULL, NULL, pgoff, NULL))
908                 goto out;
909 
910         /*
911          * Determine the object being mapped and call the appropriate
912          * specific mapper. the address has already been validated, but
913          * not unmapped, but the maps are removed from the list.
914          */
915         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
916         if (!vma) {
917                 error = -ENOMEM;
918                 goto unacct_error;
919         }
920         memset(vma, 0, sizeof(*vma));
921 
922         vma->vm_mm = mm;
923         vma->vm_start = addr;
924         vma->vm_end = addr + len;
925         vma->vm_flags = vm_flags;
926         vma->vm_page_prot = protection_map[vm_flags & 0x0f];
927         vma->vm_pgoff = pgoff;
928 
929         if (file) {
930                 error = -EINVAL;
931                 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
932                         goto free_vma;
933                 if (vm_flags & VM_DENYWRITE) {
934                         error = deny_write_access(file);
935                         if (error)
936                                 goto free_vma;
937                         correct_wcount = 1;
938                 }
939                 vma->vm_file = file;
940                 get_file(file);
941                 error = file->f_op->mmap(file, vma);
942                 if (error)
943                         goto unmap_and_free_vma;
944         } else if (vm_flags & VM_SHARED) {
945                 error = shmem_zero_setup(vma);
946                 if (error)
947                         goto free_vma;
948         }
949 
950         /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
951          * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
952          * that memory reservation must be checked; but that reservation
953          * belongs to shared memory object, not to vma: so now clear it.
954          */
955         if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
956                 vma->vm_flags &= ~VM_ACCOUNT;
957 
958         /* Can addr have changed??
959          *
960          * Answer: Yes, several device drivers can do it in their
961          *         f_op->mmap method. -DaveM
962          */
963         addr = vma->vm_start;
964 
965         if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
966                         vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
967                 vma_link(mm, vma, prev, rb_link, rb_parent);
968                 if (correct_wcount)
969                         atomic_inc(&inode->i_writecount);
970         } else {
971                 if (file) {
972                         if (correct_wcount)
973                                 atomic_inc(&inode->i_writecount);
974                         fput(file);
975                 }
976                 mpol_free(vma_policy(vma));
977                 kmem_cache_free(vm_area_cachep, vma);
978         }
979 out:    
980         mm->total_vm += len >> PAGE_SHIFT;
981         if (vm_flags & VM_LOCKED) {
982                 mm->locked_vm += len >> PAGE_SHIFT;
983                 make_pages_present(addr, addr + len);
984         }
985         if (flags & MAP_POPULATE) {
986                 up_write(&mm->mmap_sem);
987                 sys_remap_file_pages(addr, len, 0,
988                                         pgoff, flags & MAP_NONBLOCK);
989                 down_write(&mm->mmap_sem);
990         }
991         return addr;
992 
993 unmap_and_free_vma:
994         if (correct_wcount)
995                 atomic_inc(&inode->i_writecount);
996         vma->vm_file = NULL;
997         fput(file);
998 
999         /* Undo any partial mapping done by a device driver. */
1000         zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1001 free_vma:
1002         kmem_cache_free(vm_area_cachep, vma);
1003 unacct_error:
1004         if (charged)
1005                 vm_unacct_memory(charged);
1006         return error;
1007 }
1008 
1009 EXPORT_SYMBOL(do_mmap_pgoff);
1010 
1011 /* Get an address range which is currently unmapped.
1012  * For shmat() with addr=0.
1013  *
1014  * Ugly calling convention alert:
1015  * Return value with the low bits set means error value,
1016  * ie
1017  *      if (ret & ~PAGE_MASK)
1018  *              error = ret;
1019  *
1020  * This function "knows" that -ENOMEM has the bits set.
1021  */
1022 #ifndef HAVE_ARCH_UNMAPPED_AREA
1023 static inline unsigned long
1024 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1025                 unsigned long len, unsigned long pgoff, unsigned long flags)
1026 {
1027         struct mm_struct *mm = current->mm;
1028         struct vm_area_struct *vma;
1029         unsigned long start_addr;
1030 
1031         if (len > TASK_SIZE)
1032                 return -ENOMEM;
1033 
1034         if (addr) {
1035                 addr = PAGE_ALIGN(addr);
1036                 vma = find_vma(mm, addr);
1037                 if (TASK_SIZE - len >= addr &&
1038                     (!vma || addr + len <= vma->vm_start))
1039                         return addr;
1040         }
1041         start_addr = addr = mm->free_area_cache;
1042 
1043 full_search:
1044         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1045                 /* At this point:  (!vma || addr < vma->vm_end). */
1046                 if (TASK_SIZE - len < addr) {
1047                         /*
1048                          * Start a new search - just in case we missed
1049                          * some holes.
1050                          */
1051                         if (start_addr != TASK_UNMAPPED_BASE) {
1052                                 start_addr = addr = TASK_UNMAPPED_BASE;
1053                                 goto full_search;
1054                         }
1055                         return -ENOMEM;
1056                 }
1057                 if (!vma || addr + len <= vma->vm_start) {
1058                         /*
1059                          * Remember the place where we stopped the search:
1060                          */
1061                         mm->free_area_cache = addr + len;
1062                         return addr;
1063                 }
1064                 addr = vma->vm_end;
1065         }
1066 }
1067 #else
1068 extern unsigned long
1069 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
1070                         unsigned long, unsigned long);
1071 #endif  
1072 
1073 unsigned long
1074 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1075                 unsigned long pgoff, unsigned long flags)
1076 {
1077         unsigned long ret;
1078 
1079         if (!(flags & MAP_FIXED)) {
1080                 unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1081 
1082                 get_area = arch_get_unmapped_area;
1083                 if (file && file->f_op && file->f_op->get_unmapped_area)
1084                         get_area = file->f_op->get_unmapped_area;
1085                 addr = get_area(file, addr, len, pgoff, flags);
1086                 if (IS_ERR_VALUE(addr))
1087                         return addr;
1088         }
1089 
1090 
1091         if (addr > TASK_SIZE - len)
1092                 return -ENOMEM;
1093         if (addr & ~PAGE_MASK)
1094                 return -EINVAL;
1095         if (file && is_file_hugepages(file))  {
1096                 /*
1097                  * Check if the given range is hugepage aligned, and
1098                  * can be made suitable for hugepages.
1099                  */
1100                 ret = prepare_hugepage_range(addr, len);
1101         } else {
1102                 /*
1103                  * Ensure that a normal request is not falling in a
1104                  * reserved hugepage range.  For some archs like IA-64,
1105                  * there is a separate region for hugepages.
1106                  */
1107                 ret = is_hugepage_only_range(current->mm, addr, len);
1108         }
1109         if (ret)
1110                 return -EINVAL;
1111         return addr;
1112 }
1113 
1114 EXPORT_SYMBOL(get_unmapped_area);
1115 
1116 /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
1117 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1118 {
1119         struct vm_area_struct *vma = NULL;
1120 
1121         if (mm) {
1122                 /* Check the cache first. */
1123                 /* (Cache hit rate is typically around 35%.) */
1124                 vma = mm->mmap_cache;
1125                 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1126                         struct rb_node * rb_node;
1127 
1128                         rb_node = mm->mm_rb.rb_node;
1129                         vma = NULL;
1130 
1131                         while (rb_node) {
1132                                 struct vm_area_struct * vma_tmp;
1133 
1134                                 vma_tmp = rb_entry(rb_node,
1135                                                 struct vm_area_struct, vm_rb);
1136 
1137                                 if (vma_tmp->vm_end > addr) {
1138                                         vma = vma_tmp;
1139                                         if (vma_tmp->vm_start <= addr)
1140                                                 break;
1141                                         rb_node = rb_node->rb_left;
1142                                 } else
1143                                         rb_node = rb_node->rb_right;
1144                         }
1145                         if (vma)
1146                                 mm->mmap_cache = vma;
1147                 }
1148         }
1149         return vma;
1150 }
1151 
1152 EXPORT_SYMBOL(find_vma);
1153 
1154 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1155 struct vm_area_struct *
1156 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1157                         struct vm_area_struct **pprev)
1158 {
1159         struct vm_area_struct *vma = NULL, *prev = NULL;
1160         struct rb_node * rb_node;
1161         if (!mm)
1162                 goto out;
1163 
1164         /* Guard against addr being lower than the first VMA */
1165         vma = mm->mmap;
1166 
1167         /* Go through the RB tree quickly. */
1168         rb_node = mm->mm_rb.rb_node;
1169 
1170         while (rb_node) {
1171                 struct vm_area_struct *vma_tmp;
1172                 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1173 
1174                 if (addr < vma_tmp->vm_end) {
1175                         rb_node = rb_node->rb_left;
1176                 } else {
1177                         prev = vma_tmp;
1178                         if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1179                                 break;
1180                         rb_node = rb_node->rb_right;
1181                 }
1182         }
1183 
1184 out:
1185         *pprev = prev;
1186         return prev ? prev->vm_next : vma;
1187 }
1188 
1189 /*
1190  * Verify that the stack growth is acceptable and
1191  * update accounting. This is shared with both the
1192  * grow-up and grow-down cases.
1193  */
1194 static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
1195 {
1196         struct mm_struct *mm = vma->vm_mm;
1197         struct rlimit *rlim = current->rlim;
1198 
1199         /* address space limit tests */
1200         if (mm->total_vm + grow > rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT)
1201                 return -ENOMEM;
1202 
1203         /* Stack limit test */
1204         if (size > rlim[RLIMIT_STACK].rlim_cur)
1205                 return -ENOMEM;
1206 
1207         /*
1208          * Overcommit..  This must be the final test, as it will
1209          * update security statistics.
1210          */
1211         if (security_vm_enough_memory(grow))
1212                 return -ENOMEM;
1213 
1214         /* Ok, everything looks good - let it rip */
1215         mm->total_vm += grow;
1216         if (vma->vm_flags & VM_LOCKED)
1217                 mm->locked_vm += grow;
1218         return 0;
1219 }
1220 
1221 #ifdef CONFIG_STACK_GROWSUP
1222 /*
1223  * vma is the first one with address > vma->vm_end.  Have to extend vma.
1224  */
1225 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1226 {
1227         int error;
1228 
1229         if (!(vma->vm_flags & VM_GROWSUP))
1230                 return -EFAULT;
1231 
1232         /*
1233          * We must make sure the anon_vma is allocated
1234          * so that the anon_vma locking is not a noop.
1235          */
1236         if (unlikely(anon_vma_prepare(vma)))
1237                 return -ENOMEM;
1238         anon_vma_lock(vma);
1239 
1240         /*
1241          * vma->vm_start/vm_end cannot change under us because the caller
1242          * is required to hold the mmap_sem in read mode.  We need the
1243          * anon_vma lock to serialize against concurrent expand_stacks.
1244          */
1245         address += 4 + PAGE_SIZE - 1;
1246         address &= PAGE_MASK;
1247         error = 0;
1248 
1249         /* Somebody else might have raced and expanded it already */
1250         if (address > vma->vm_end) {
1251                 unsigned long size, grow;
1252 
1253                 size = address - vma->vm_start;
1254                 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1255 
1256                 error = acct_stack_growth(vma, size, grow);
1257                 if (!error)
1258                         vma->vm_end = address;
1259         }
1260         anon_vma_unlock(vma);
1261         return error;
1262 }
1263 
1264 struct vm_area_struct *
1265 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1266 {
1267         struct vm_area_struct *vma, *prev;
1268 
1269         addr &= PAGE_MASK;
1270         vma = find_vma_prev(mm, addr, &prev);
1271         if (vma && (vma->vm_start <= addr))
1272                 return vma;
1273         if (!prev || expand_stack(prev, addr))
1274                 return NULL;
1275         if (prev->vm_flags & VM_LOCKED) {
1276                 make_pages_present(addr, prev->vm_end);
1277         }
1278         return prev;
1279 }
1280 #else
1281 /*
1282  * vma is the first one with address < vma->vm_start.  Have to extend vma.
1283  */
1284 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1285 {
1286         int error;
1287 
1288         /*
1289          * We must make sure the anon_vma is allocated
1290          * so that the anon_vma locking is not a noop.
1291          */
1292         if (unlikely(anon_vma_prepare(vma)))
1293                 return -ENOMEM;
1294         anon_vma_lock(vma);
1295 
1296         /*
1297          * vma->vm_start/vm_end cannot change under us because the caller
1298          * is required to hold the mmap_sem in read mode.  We need the
1299          * anon_vma lock to serialize against concurrent expand_stacks.
1300          */
1301         address &= PAGE_MASK;
1302         error = 0;
1303 
1304         /* Somebody else might have raced and expanded it already */
1305         if (address < vma->vm_start) {
1306                 unsigned long size, grow;
1307 
1308                 size = vma->vm_end - address;
1309                 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1310 
1311                 error = acct_stack_growth(vma, size, grow);
1312                 if (!error) {
1313                         vma->vm_start = address;
1314                         vma->vm_pgoff -= grow;
1315                 }
1316         }
1317         anon_vma_unlock(vma);
1318         return error;
1319 }
1320 
1321 struct vm_area_struct *
1322 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1323 {
1324         struct vm_area_struct * vma;
1325         unsigned long start;
1326 
1327         addr &= PAGE_MASK;
1328         vma = find_vma(mm,addr);
1329         if (!vma)
1330                 return NULL;
1331         if (vma->vm_start <= addr)
1332                 return vma;
1333         if (!(vma->vm_flags & VM_GROWSDOWN))
1334                 return NULL;
1335         start = vma->vm_start;
1336         if (expand_stack(vma, addr))
1337                 return NULL;
1338         if (vma->vm_flags & VM_LOCKED) {
1339                 make_pages_present(addr, start);
1340         }
1341         return vma;
1342 }
1343 #endif
1344 
1345 /*
1346  * Try to free as many page directory entries as we can,
1347  * without having to work very hard at actually scanning
1348  * the page tables themselves.
1349  *
1350  * Right now we try to free page tables if we have a nice
1351  * PGDIR-aligned area that got free'd up. We could be more
1352  * granular if we want to, but this is fast and simple,
1353  * and covers the bad cases.
1354  *
1355  * "prev", if it exists, points to a vma before the one
1356  * we just free'd - but there's no telling how much before.
1357  */
1358 static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
1359         unsigned long start, unsigned long end)
1360 {
1361         unsigned long first = start & PGDIR_MASK;
1362         unsigned long last = end + PGDIR_SIZE - 1;
1363         unsigned long start_index, end_index;
1364         struct mm_struct *mm = tlb->mm;
1365 
1366         if (!prev) {
1367                 prev = mm->mmap;
1368                 if (!prev)
1369                         goto no_mmaps;
1370                 if (prev->vm_end > start) {
1371                         if (last > prev->vm_start)
1372                                 last = prev->vm_start;
1373                         goto no_mmaps;
1374                 }
1375         }
1376         for (;;) {
1377                 struct vm_area_struct *next = prev->vm_next;
1378 
1379                 if (next) {
1380                         if (next->vm_start < start) {
1381                                 prev = next;
1382                                 continue;
1383                         }
1384                         if (last > next->vm_start)
1385                                 last = next->vm_start;
1386                 }
1387                 if (prev->vm_end > first)
1388                         first = prev->vm_end + PGDIR_SIZE - 1;
1389                 break;
1390         }
1391 no_mmaps:
1392         if (last < first)       /* for arches with discontiguous pgd indices */
1393                 return;
1394         /*
1395          * If the PGD bits are not consecutive in the virtual address, the
1396          * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1397          */
1398         start_index = pgd_index(first);
1399         if (start_index < FIRST_USER_PGD_NR)
1400                 start_index = FIRST_USER_PGD_NR;
1401         end_index = pgd_index(last);
1402         if (end_index > start_index) {
1403                 clear_page_tables(tlb, start_index, end_index - start_index);
1404                 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
1405         }
1406 }
1407 
1408 /* Normal function to fix up a mapping
1409  * This function is the default for when an area has no specific
1410  * function.  This may be used as part of a more specific routine.
1411  *
1412  * By the time this function is called, the area struct has been
1413  * removed from the process mapping list.
1414  */
1415 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1416 {
1417         size_t len = area->vm_end - area->vm_start;
1418 
1419         area->vm_mm->total_vm -= len >> PAGE_SHIFT;
1420         if (area->vm_flags & VM_LOCKED)
1421                 area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1422         /*
1423          * Is this a new hole at the lowest possible address?
1424          */
1425         if (area->vm_start >= TASK_UNMAPPED_BASE &&
1426                                 area->vm_start < area->vm_mm->free_area_cache)
1427               area->vm_mm->free_area_cache = area->vm_start;
1428 
1429         remove_vm_struct(area);
1430 }
1431 
1432 /*
1433  * Update the VMA and inode share lists.
1434  *
1435  * Ok - we have the memory areas we should free on the 'free' list,
1436  * so release them, and do the vma updates.
1437  */
1438 static void unmap_vma_list(struct mm_struct *mm,
1439         struct vm_area_struct *mpnt)
1440 {
1441         do {
1442                 struct vm_area_struct *next = mpnt->vm_next;
1443                 unmap_vma(mm, mpnt);
1444                 mpnt = next;
1445         } while (mpnt != NULL);
1446         validate_mm(mm);
1447 }
1448 
1449 /*
1450  * Get rid of page table information in the indicated region.
1451  *
1452  * Called with the page table lock held.
1453  */
1454 static void unmap_region(struct mm_struct *mm,
1455         struct vm_area_struct *vma,
1456         struct vm_area_struct *prev,
1457         unsigned long start,
1458         unsigned long end)
1459 {
1460         struct mmu_gather *tlb;
1461         unsigned long nr_accounted = 0;
1462 
1463         lru_add_drain();
1464         tlb = tlb_gather_mmu(mm, 0);
1465         unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1466         vm_unacct_memory(nr_accounted);
1467 
1468         if (is_hugepage_only_range(mm, start, end - start))
1469                 hugetlb_free_pgtables(tlb, prev, start, end);
1470         else
1471                 free_pgtables(tlb, prev, start, end);
1472         tlb_finish_mmu(tlb, start, end);
1473 }
1474 
1475 /*
1476  * Create a list of vma's touched by the unmap, removing them from the mm's
1477  * vma list as we go..
1478  */
1479 static void
1480 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1481         struct vm_area_struct *prev, unsigned long end)
1482 {
1483         struct vm_area_struct **insertion_point;
1484         struct vm_area_struct *tail_vma = NULL;
1485 
1486         insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1487         do {
1488                 rb_erase(&vma->vm_rb, &mm->mm_rb);
1489                 mm->map_count--;
1490                 tail_vma = vma;
1491                 vma = vma->vm_next;
1492         } while (vma && vma->vm_start < end);
1493         *insertion_point = vma;
1494         tail_vma->vm_next = NULL;
1495         mm->mmap_cache = NULL;          /* Kill the cache. */
1496 }
1497 
1498 /*
1499  * Split a vma into two pieces at address 'addr', a new vma is allocated
1500  * either for the first part or the the tail.
1501  */
1502 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1503               unsigned long addr, int new_below)
1504 {
1505         struct mempolicy *pol;
1506         struct vm_area_struct *new;
1507 
1508         if (mm->map_count >= sysctl_max_map_count)
1509                 return -ENOMEM;
1510 
1511         new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1512         if (!new)
1513                 return -ENOMEM;
1514 
1515         /* most fields are the same, copy all, and then fixup */
1516         *new = *vma;
1517         vma_prio_tree_init(new);
1518 
1519         if (new_below)
1520                 new->vm_end = addr;
1521         else {
1522                 new->vm_start = addr;
1523                 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1524         }
1525 
1526         pol = mpol_copy(vma_policy(vma));
1527         if (IS_ERR(pol)) {
1528                 kmem_cache_free(vm_area_cachep, new);
1529                 return PTR_ERR(pol);
1530         }
1531         vma_set_policy(new, pol);
1532 
1533         if (new->vm_file)
1534                 get_file(new->vm_file);
1535 
1536         if (new->vm_ops && new->vm_ops->open)
1537                 new->vm_ops->open(new);
1538 
1539         if (new_below)
1540                 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1541                         ((addr - new->vm_start) >> PAGE_SHIFT), new);
1542         else
1543                 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1544 
1545         return 0;
1546 }
1547 
1548 /* Munmap is split into 2 main parts -- this part which finds
1549  * what needs doing, and the areas themselves, which do the
1550  * work.  This now handles partial unmappings.
1551  * Jeremy Fitzhardinge <jeremy@goop.org>
1552  */
1553 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1554 {
1555         unsigned long end;
1556         struct vm_area_struct *mpnt, *prev, *last;
1557 
1558         if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1559                 return -EINVAL;
1560 
1561         if ((len = PAGE_ALIGN(len)) == 0)
1562                 return -EINVAL;
1563 
1564         /* Find the first overlapping VMA */
1565         mpnt = find_vma_prev(mm, start, &prev);
1566         if (!mpnt)
1567                 return 0;
1568         /* we have  start < mpnt->vm_end  */
1569 
1570         if (is_vm_hugetlb_page(mpnt)) {
1571                 int ret = is_aligned_hugepage_range(start, len);
1572 
1573                 if (ret)
1574                         return ret;
1575         }
1576 
1577         /* if it doesn't overlap, we have nothing.. */
1578         end = start + len;
1579         if (mpnt->vm_start >= end)
1580                 return 0;
1581 
1582         /* Something will probably happen, so notify. */
1583         if (mpnt->vm_file && (mpnt->vm_flags & VM_EXEC))
1584                 profile_exec_unmap(mm);
1585  
1586         /*
1587          * If we need to split any vma, do it now to save pain later.
1588          *
1589          * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1590          * unmapped vm_area_struct will remain in use: so lower split_vma
1591          * places tmp vma above, and higher split_vma places tmp vma below.
1592          */
1593         if (start > mpnt->vm_start) {
1594                 if (split_vma(mm, mpnt, start, 0))
1595                         return -ENOMEM;
1596                 prev = mpnt;
1597         }
1598 
1599         /* Does it split the last one? */
1600         last = find_vma(mm, end);
1601         if (last && end > last->vm_start) {
1602                 if (split_vma(mm, last, end, 1))
1603                         return -ENOMEM;
1604         }
1605         mpnt = prev? prev->vm_next: mm->mmap;
1606 
1607         /*
1608          * Remove the vma's, and unmap the actual pages
1609          */
1610         detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1611         spin_lock(&mm->page_table_lock);
1612         unmap_region(mm, mpnt, prev, start, end);
1613         spin_unlock(&mm->page_table_lock);
1614 
1615         /* Fix up all other VM information */
1616         unmap_vma_list(mm, mpnt);
1617 
1618         return 0;
1619 }
1620 
1621 EXPORT_SYMBOL(do_munmap);
1622 
1623 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1624 {
1625         int ret;
1626         struct mm_struct *mm = current->mm;
1627 
1628         down_write(&mm->mmap_sem);
1629         ret = do_munmap(mm, addr, len);
1630         up_write(&mm->mmap_sem);
1631         return ret;
1632 }
1633 
1634 /*
1635  *  this is really a simplified "do_mmap".  it only handles
1636  *  anonymous maps.  eventually we may be able to do some
1637  *  brk-specific accounting here.
1638  */
1639 unsigned long do_brk(unsigned long addr, unsigned long len)
1640 {
1641         struct mm_struct * mm = current->mm;
1642         struct vm_area_struct * vma, * prev;
1643         unsigned long flags;
1644         struct rb_node ** rb_link, * rb_parent;
1645         pgoff_t pgoff = addr >> PAGE_SHIFT;
1646 
1647         len = PAGE_ALIGN(len);
1648         if (!len)
1649                 return addr;
1650 
1651         if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1652                 return -EINVAL;
1653 
1654         /*
1655          * mlock MCL_FUTURE?
1656          */
1657         if (mm->def_flags & VM_LOCKED) {
1658                 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
1659                 locked += len;
1660                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
1661                         return -EAGAIN;
1662         }
1663 
1664         /*
1665          * mm->mmap_sem is required to protect against another thread
1666          * changing the mappings in case we sleep.
1667          */
1668         WARN_ON(down_read_trylock(&mm->mmap_sem));
1669 
1670         /*
1671          * Clear old maps.  this also does some error checking for us
1672          */
1673  munmap_back:
1674         vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1675         if (vma && vma->vm_start < addr + len) {
1676                 if (do_munmap(mm, addr, len))
1677                         return -ENOMEM;
1678                 goto munmap_back;
1679         }
1680 
1681         /* Check against address space limits *after* clearing old maps... */
1682         if ((mm->total_vm << PAGE_SHIFT) + len
1683             > current->rlim[RLIMIT_AS].rlim_cur)
1684                 return -ENOMEM;
1685 
1686         if (mm->map_count > sysctl_max_map_count)
1687                 return -ENOMEM;
1688 
1689         if (security_vm_enough_memory(len >> PAGE_SHIFT))
1690                 return -ENOMEM;
1691 
1692         flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1693 
1694         /* Can we just expand an old private anonymous mapping? */
1695         if (vma_merge(mm, prev, addr, addr + len, flags,
1696                                         NULL, NULL, pgoff, NULL))
1697                 goto out;
1698 
1699         /*
1700          * create a vma struct for an anonymous mapping
1701          */
1702         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1703         if (!vma) {
1704                 vm_unacct_memory(len >> PAGE_SHIFT);
1705                 return -ENOMEM;
1706         }
1707         memset(vma, 0, sizeof(*vma));
1708 
1709         vma->vm_mm = mm;
1710         vma->vm_start = addr;
1711         vma->vm_end = addr + len;
1712         vma->vm_pgoff = pgoff;
1713         vma->vm_flags = flags;
1714         vma->vm_page_prot = protection_map[flags & 0x0f];
1715         vma_link(mm, vma, prev, rb_link, rb_parent);
1716 out:
1717         mm->total_vm += len >> PAGE_SHIFT;
1718         if (flags & VM_LOCKED) {
1719                 mm->locked_vm += len >> PAGE_SHIFT;
1720                 make_pages_present(addr, addr + len);
1721         }
1722         return addr;
1723 }
1724 
1725 EXPORT_SYMBOL(do_brk);
1726 
1727 /* Release all mmaps. */
1728 void exit_mmap(struct mm_struct *mm)
1729 {
1730         struct mmu_gather *tlb;
1731         struct vm_area_struct *vma;
1732         unsigned long nr_accounted = 0;
1733 
1734         profile_exit_mmap(mm);
1735  
1736         lru_add_drain();
1737 
1738         spin_lock(&mm->page_table_lock);
1739 
1740         tlb = tlb_gather_mmu(mm, 1);
1741         flush_cache_mm(mm);
1742         /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1743         mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1744                                         ~0UL, &nr_accounted, NULL);
1745         vm_unacct_memory(nr_accounted);
1746         BUG_ON(mm->map_count);  /* This is just debugging */
1747         clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1748         tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1749 
1750         vma = mm->mmap;
1751         mm->mmap = mm->mmap_cache = NULL;
1752         mm->mm_rb = RB_ROOT;
1753         mm->rss = 0;
1754         mm->total_vm = 0;
1755         mm->locked_vm = 0;
1756 
1757         spin_unlock(&mm->page_table_lock);
1758 
1759         /*
1760          * Walk the list again, actually closing and freeing it
1761          * without holding any MM locks.
1762          */
1763         while (vma) {
1764                 struct vm_area_struct *next = vma->vm_next;
1765                 remove_vm_struct(vma);
1766                 vma = next;
1767         }
1768 }
1769 
1770 /* Insert vm structure into process list sorted by address
1771  * and into the inode's i_mmap tree.  If vm_file is non-NULL
1772  * then i_mmap_lock is taken here.
1773  */
1774 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1775 {
1776         struct vm_area_struct * __vma, * prev;
1777         struct rb_node ** rb_link, * rb_parent;
1778 
1779         /*
1780          * The vm_pgoff of a purely anonymous vma should be irrelevant
1781          * until its first write fault, when page's anon_vma and index
1782          * are set.  But now set the vm_pgoff it will almost certainly
1783          * end up with (unless mremap moves it elsewhere before that
1784          * first wfault), so /proc/pid/maps tells a consistent story.
1785          *
1786          * By setting it to reflect the virtual start address of the
1787          * vma, merges and splits can happen in a seamless way, just
1788          * using the existing file pgoff checks and manipulations.
1789          * Similarly in do_mmap_pgoff and in do_brk.
1790          */
1791         if (!vma->vm_file) {
1792                 BUG_ON(vma->anon_vma);
1793                 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
1794         }
1795         __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
1796         if (__vma && __vma->vm_start < vma->vm_end)
1797                 return -ENOMEM;
1798         vma_link(mm, vma, prev, rb_link, rb_parent);
1799         return 0;
1800 }
1801 
1802 /*
1803  * Copy the vma structure to a new location in the same mm,
1804  * prior to moving page table entries, to effect an mremap move.
1805  */
1806 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1807         unsigned long addr, unsigned long len, pgoff_t pgoff)
1808 {
1809         struct vm_area_struct *vma = *vmap;
1810         unsigned long vma_start = vma->vm_start;
1811         struct mm_struct *mm = vma->vm_mm;
1812         struct vm_area_struct *new_vma, *prev;
1813         struct rb_node **rb_link, *rb_parent;
1814         struct mempolicy *pol;
1815 
1816         /*
1817          * If anonymous vma has not yet been faulted, update new pgoff
1818          * to match new location, to increase its chance of merging.
1819          */
1820         if (!vma->vm_file && !vma->anon_vma)
1821                 pgoff = addr >> PAGE_SHIFT;
1822 
1823         find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1824         new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
1825                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
1826         if (new_vma) {
1827                 /*
1828                  * Source vma may have been merged into new_vma
1829                  */
1830                 if (vma_start >= new_vma->vm_start &&
1831                     vma_start < new_vma->vm_end)
1832                         *vmap = new_vma;
1833         } else {
1834                 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1835                 if (new_vma) {
1836                         *new_vma = *vma;
1837                         vma_prio_tree_init(new_vma);
1838                         pol = mpol_copy(vma_policy(vma));
1839                         if (IS_ERR(pol)) {
1840                                 kmem_cache_free(vm_area_cachep, new_vma);
1841                                 return NULL;
1842                         }
1843                         vma_set_policy(new_vma, pol);
1844                         new_vma->vm_start = addr;
1845                         new_vma->vm_end = addr + len;
1846                         new_vma->vm_pgoff = pgoff;
1847                         if (new_vma->vm_file)
1848                                 get_file(new_vma->vm_file);
1849                         if (new_vma->vm_ops && new_vma->vm_ops->open)
1850                                 new_vma->vm_ops->open(new_vma);
1851                         vma_link(mm, new_vma, prev, rb_link, rb_parent);
1852                 }
1853         }
1854         return new_vma;
1855 }
1856 

This page was automatically generated by LXR 0.3.1. •  Linux is a registered trademark of Linus Torvalds