1 /*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
6 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
7 */
8
9 #include <linux/slab.h>
10 #include <linux/backing-dev.h>
11 #include <linux/mm.h>
12 #include <linux/shm.h>
13 #include <linux/mman.h>
14 #include <linux/pagemap.h>
15 #include <linux/swap.h>
16 #include <linux/syscalls.h>
17 #include <linux/capability.h>
18 #include <linux/init.h>
19 #include <linux/file.h>
20 #include <linux/fs.h>
21 #include <linux/personality.h>
22 #include <linux/security.h>
23 #include <linux/hugetlb.h>
24 #include <linux/profile.h>
25 #include <linux/module.h>
26 #include <linux/mount.h>
27 #include <linux/mempolicy.h>
28 #include <linux/rmap.h>
29 #include <linux/mmu_notifier.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/cacheflush.h>
33 #include <asm/tlb.h>
34 #include <asm/mmu_context.h>
35
36 #include "internal.h"
37
38 #ifndef arch_mmap_check
39 #define arch_mmap_check(addr, len, flags) (0)
40 #endif
41
42 #ifndef arch_rebalance_pgtables
43 #define arch_rebalance_pgtables(addr, len) (addr)
44 #endif
45
46 static void unmap_region(struct mm_struct *mm,
47 struct vm_area_struct *vma, struct vm_area_struct *prev,
48 unsigned long start, unsigned long end);
49
50 /*
51 * WARNING: the debugging will use recursive algorithms so never enable this
52 * unless you know what you are doing.
53 */
54 #undef DEBUG_MM_RB
55
56 /* description of effects of mapping type and prot in current implementation.
57 * this is due to the limited x86 page protection hardware. The expected
58 * behavior is in parens:
59 *
60 * map_type prot
61 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
62 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
63 * w: (no) no w: (no) no w: (yes) yes w: (no) no
64 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
65 *
66 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
67 * w: (no) no w: (no) no w: (copy) copy w: (no) no
68 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
69 *
70 */
71 pgprot_t protection_map[16] = {
72 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
73 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
74 };
75
vm_get_page_prot(unsigned long vm_flags)76 pgprot_t vm_get_page_prot(unsigned long vm_flags)
77 {
78 return __pgprot(pgprot_val(protection_map[vm_flags &
79 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
80 pgprot_val(arch_vm_get_page_prot(vm_flags)));
81 }
82 EXPORT_SYMBOL(vm_get_page_prot);
83
84 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
85 int sysctl_overcommit_ratio = 50; /* default is 50% */
86 int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
87 atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
88
89 /* amount of vm to protect from userspace access */
90 unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
91
92 /*
93 * Check that a process has enough memory to allocate a new virtual
94 * mapping. 0 means there is enough memory for the allocation to
95 * succeed and -ENOMEM implies there is not.
96 *
97 * We currently support three overcommit policies, which are set via the
98 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
99 *
100 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
101 * Additional code 2002 Jul 20 by Robert Love.
102 *
103 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
104 *
105 * Note this is a helper function intended to be used by LSMs which
106 * wish to use this logic.
107 */
__vm_enough_memory(struct mm_struct * mm,long pages,int cap_sys_admin)108 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
109 {
110 unsigned long free, allowed;
111
112 vm_acct_memory(pages);
113
114 /*
115 * Sometimes we want to use more memory than we have
116 */
117 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
118 return 0;
119
120 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
121 unsigned long n;
122
123 free = global_page_state(NR_FILE_PAGES);
124 free += nr_swap_pages;
125
126 /*
127 * Any slabs which are created with the
128 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
129 * which are reclaimable, under pressure. The dentry
130 * cache and most inode caches should fall into this
131 */
132 free += global_page_state(NR_SLAB_RECLAIMABLE);
133
134 /*
135 * Leave the last 3% for root
136 */
137 if (!cap_sys_admin)
138 free -= free / 32;
139
140 if (free > pages)
141 return 0;
142
143 /*
144 * nr_free_pages() is very expensive on large systems,
145 * only call if we're about to fail.
146 */
147 n = nr_free_pages();
148
149 /*
150 * Leave reserved pages. The pages are not for anonymous pages.
151 */
152 if (n <= totalreserve_pages)
153 goto error;
154 else
155 n -= totalreserve_pages;
156
157 /*
158 * Leave the last 3% for root
159 */
160 if (!cap_sys_admin)
161 n -= n / 32;
162 free += n;
163
164 if (free > pages)
165 return 0;
166
167 goto error;
168 }
169
170 allowed = (totalram_pages - hugetlb_total_pages())
171 * sysctl_overcommit_ratio / 100;
172 /*
173 * Leave the last 3% for root
174 */
175 if (!cap_sys_admin)
176 allowed -= allowed / 32;
177 allowed += total_swap_pages;
178
179 /* Don't let a single process grow too big:
180 leave 3% of the size of this process for other processes */
181 if (mm)
182 allowed -= mm->total_vm / 32;
183
184 /*
185 * cast `allowed' as a signed long because vm_committed_space
186 * sometimes has a negative value
187 */
188 if (atomic_long_read(&vm_committed_space) < (long)allowed)
189 return 0;
190 error:
191 vm_unacct_memory(pages);
192
193 return -ENOMEM;
194 }
195
196 /*
197 * Requires inode->i_mapping->i_mmap_lock
198 */
__remove_shared_vm_struct(struct vm_area_struct * vma,struct file * file,struct address_space * mapping)199 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
200 struct file *file, struct address_space *mapping)
201 {
202 if (vma->vm_flags & VM_DENYWRITE)
203 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
204 if (vma->vm_flags & VM_SHARED)
205 mapping->i_mmap_writable--;
206
207 flush_dcache_mmap_lock(mapping);
208 if (unlikely(vma->vm_flags & VM_NONLINEAR))
209 list_del_init(&vma->shared.vm_set.list);
210 else
211 vma_prio_tree_remove(vma, &mapping->i_mmap);
212 flush_dcache_mmap_unlock(mapping);
213 }
214
215 /*
216 * Unlink a file-based vm structure from its prio_tree, to hide
217 * vma from rmap and vmtruncate before freeing its page tables.
218 */
unlink_file_vma(struct vm_area_struct * vma)219 void unlink_file_vma(struct vm_area_struct *vma)
220 {
221 struct file *file = vma->vm_file;
222
223 if (file) {
224 struct address_space *mapping = file->f_mapping;
225 spin_lock(&mapping->i_mmap_lock);
226 __remove_shared_vm_struct(vma, file, mapping);
227 spin_unlock(&mapping->i_mmap_lock);
228 }
229 }
230
231 /*
232 * Close a vm structure and free it, returning the next.
233 */
remove_vma(struct vm_area_struct * vma)234 static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
235 {
236 struct vm_area_struct *next = vma->vm_next;
237
238 might_sleep();
239 if (vma->vm_ops && vma->vm_ops->close)
240 vma->vm_ops->close(vma);
241 if (vma->vm_file) {
242 fput(vma->vm_file);
243 if (vma->vm_flags & VM_EXECUTABLE)
244 removed_exe_file_vma(vma->vm_mm);
245 }
246 mpol_put(vma_policy(vma));
247 kmem_cache_free(vm_area_cachep, vma);
248 return next;
249 }
250
SYSCALL_DEFINE1(brk,unsigned long,brk)251 SYSCALL_DEFINE1(brk, unsigned long, brk)
252 {
253 unsigned long rlim, retval;
254 unsigned long newbrk, oldbrk;
255 struct mm_struct *mm = current->mm;
256 unsigned long min_brk;
257
258 down_write(&mm->mmap_sem);
259
260 #ifdef CONFIG_COMPAT_BRK
261 min_brk = mm->end_code;
262 #else
263 min_brk = mm->start_brk;
264 #endif
265 if (brk < min_brk)
266 goto out;
267
268 /*
269 * Check against rlimit here. If this check is done later after the test
270 * of oldbrk with newbrk then it can escape the test and let the data
271 * segment grow beyond its set limit the in case where the limit is
272 * not page aligned -Ram Gupta
273 */
274 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
275 if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
276 (mm->end_data - mm->start_data) > rlim)
277 goto out;
278
279 newbrk = PAGE_ALIGN(brk);
280 oldbrk = PAGE_ALIGN(mm->brk);
281 if (oldbrk == newbrk)
282 goto set_brk;
283
284 /* Always allow shrinking brk. */
285 if (brk <= mm->brk) {
286 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
287 goto set_brk;
288 goto out;
289 }
290
291 /* Check against existing mmap mappings. */
292 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
293 goto out;
294
295 /* Ok, looks good - let it rip. */
296 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
297 goto out;
298 set_brk:
299 mm->brk = brk;
300 out:
301 retval = mm->brk;
302 up_write(&mm->mmap_sem);
303 return retval;
304 }
305
306 #ifdef DEBUG_MM_RB
browse_rb(struct rb_root * root)307 static int browse_rb(struct rb_root *root)
308 {
309 int i = 0, j;
310 struct rb_node *nd, *pn = NULL;
311 unsigned long prev = 0, pend = 0;
312
313 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
314 struct vm_area_struct *vma;
315 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
316 if (vma->vm_start < prev)
317 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
318 if (vma->vm_start < pend)
319 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
320 if (vma->vm_start > vma->vm_end)
321 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
322 i++;
323 pn = nd;
324 prev = vma->vm_start;
325 pend = vma->vm_end;
326 }
327 j = 0;
328 for (nd = pn; nd; nd = rb_prev(nd)) {
329 j++;
330 }
331 if (i != j)
332 printk("backwards %d, forwards %d\n", j, i), i = 0;
333 return i;
334 }
335
validate_mm(struct mm_struct * mm)336 void validate_mm(struct mm_struct *mm)
337 {
338 int bug = 0;
339 int i = 0;
340 struct vm_area_struct *tmp = mm->mmap;
341 while (tmp) {
342 tmp = tmp->vm_next;
343 i++;
344 }
345 if (i != mm->map_count)
346 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
347 i = browse_rb(&mm->mm_rb);
348 if (i != mm->map_count)
349 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
350 BUG_ON(bug);
351 }
352 #else
353 #define validate_mm(mm) do { } while (0)
354 #endif
355
356 static struct vm_area_struct *
find_vma_prepare(struct mm_struct * mm,unsigned long addr,struct vm_area_struct ** pprev,struct rb_node *** rb_link,struct rb_node ** rb_parent)357 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
358 struct vm_area_struct **pprev, struct rb_node ***rb_link,
359 struct rb_node ** rb_parent)
360 {
361 struct vm_area_struct * vma;
362 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
363
364 __rb_link = &mm->mm_rb.rb_node;
365 rb_prev = __rb_parent = NULL;
366 vma = NULL;
367
368 while (*__rb_link) {
369 struct vm_area_struct *vma_tmp;
370
371 __rb_parent = *__rb_link;
372 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
373
374 if (vma_tmp->vm_end > addr) {
375 vma = vma_tmp;
376 if (vma_tmp->vm_start <= addr)
377 break;
378 __rb_link = &__rb_parent->rb_left;
379 } else {
380 rb_prev = __rb_parent;
381 __rb_link = &__rb_parent->rb_right;
382 }
383 }
384
385 *pprev = NULL;
386 if (rb_prev)
387 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
388 *rb_link = __rb_link;
389 *rb_parent = __rb_parent;
390 return vma;
391 }
392
393 static inline void
__vma_link_list(struct mm_struct * mm,struct vm_area_struct * vma,struct vm_area_struct * prev,struct rb_node * rb_parent)394 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
395 struct vm_area_struct *prev, struct rb_node *rb_parent)
396 {
397 if (prev) {
398 vma->vm_next = prev->vm_next;
399 prev->vm_next = vma;
400 } else {
401 mm->mmap = vma;
402 if (rb_parent)
403 vma->vm_next = rb_entry(rb_parent,
404 struct vm_area_struct, vm_rb);
405 else
406 vma->vm_next = NULL;
407 }
408 }
409
__vma_link_rb(struct mm_struct * mm,struct vm_area_struct * vma,struct rb_node ** rb_link,struct rb_node * rb_parent)410 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
411 struct rb_node **rb_link, struct rb_node *rb_parent)
412 {
413 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
414 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
415 }
416
__vma_link_file(struct vm_area_struct * vma)417 static void __vma_link_file(struct vm_area_struct *vma)
418 {
419 struct file *file;
420
421 file = vma->vm_file;
422 if (file) {
423 struct address_space *mapping = file->f_mapping;
424
425 if (vma->vm_flags & VM_DENYWRITE)
426 atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
427 if (vma->vm_flags & VM_SHARED)
428 mapping->i_mmap_writable++;
429
430 flush_dcache_mmap_lock(mapping);
431 if (unlikely(vma->vm_flags & VM_NONLINEAR))
432 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
433 else
434 vma_prio_tree_insert(vma, &mapping->i_mmap);
435 flush_dcache_mmap_unlock(mapping);
436 }
437 }
438
439 static void
__vma_link(struct mm_struct * mm,struct vm_area_struct * vma,struct vm_area_struct * prev,struct rb_node ** rb_link,struct rb_node * rb_parent)440 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
441 struct vm_area_struct *prev, struct rb_node **rb_link,
442 struct rb_node *rb_parent)
443 {
444 __vma_link_list(mm, vma, prev, rb_parent);
445 __vma_link_rb(mm, vma, rb_link, rb_parent);
446 __anon_vma_link(vma);
447 }
448
vma_link(struct mm_struct * mm,struct vm_area_struct * vma,struct vm_area_struct * prev,struct rb_node ** rb_link,struct rb_node * rb_parent)449 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
450 struct vm_area_struct *prev, struct rb_node **rb_link,
451 struct rb_node *rb_parent)
452 {
453 struct address_space *mapping = NULL;
454
455 if (vma->vm_file)
456 mapping = vma->vm_file->f_mapping;
457
458 if (mapping) {
459 spin_lock(&mapping->i_mmap_lock);
460 vma->vm_truncate_count = mapping->truncate_count;
461 }
462 anon_vma_lock(vma);
463
464 __vma_link(mm, vma, prev, rb_link, rb_parent);
465 __vma_link_file(vma);
466
467 anon_vma_unlock(vma);
468 if (mapping)
469 spin_unlock(&mapping->i_mmap_lock);
470
471 mm->map_count++;
472 validate_mm(mm);
473 }
474
475 /*
476 * Helper for vma_adjust in the split_vma insert case:
477 * insert vm structure into list and rbtree and anon_vma,
478 * but it has already been inserted into prio_tree earlier.
479 */
__insert_vm_struct(struct mm_struct * mm,struct vm_area_struct * vma)480 static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
481 {
482 struct vm_area_struct *__vma, *prev;
483 struct rb_node **rb_link, *rb_parent;
484
485 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
486 BUG_ON(__vma && __vma->vm_start < vma->vm_end);
487 __vma_link(mm, vma, prev, rb_link, rb_parent);
488 mm->map_count++;
489 }
490
491 static inline void
__vma_unlink(struct mm_struct * mm,struct vm_area_struct * vma,struct vm_area_struct * prev)492 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
493 struct vm_area_struct *prev)
494 {
495 prev->vm_next = vma->vm_next;
496 rb_erase(&vma->vm_rb, &mm->mm_rb);
497 if (mm->mmap_cache == vma)
498 mm->mmap_cache = prev;
499 }
500
501 /*
502 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
503 * is already present in an i_mmap tree without adjusting the tree.
504 * The following helper function should be used when such adjustments
505 * are necessary. The "insert" vma (if any) is to be inserted
506 * before we drop the necessary locks.
507 */
vma_adjust(struct vm_area_struct * vma,unsigned long start,unsigned long end,pgoff_t pgoff,struct vm_area_struct * insert)508 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
509 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
510 {
511 struct mm_struct *mm = vma->vm_mm;
512 struct vm_area_struct *next = vma->vm_next;
513 struct vm_area_struct *importer = NULL;
514 struct address_space *mapping = NULL;
515 struct prio_tree_root *root = NULL;
516 struct file *file = vma->vm_file;
517 struct anon_vma *anon_vma = NULL;
518 long adjust_next = 0;
519 int remove_next = 0;
520
521 if (next && !insert) {
522 if (end >= next->vm_end) {
523 /*
524 * vma expands, overlapping all the next, and
525 * perhaps the one after too (mprotect case 6).
526 */
527 again: remove_next = 1 + (end > next->vm_end);
528 end = next->vm_end;
529 anon_vma = next->anon_vma;
530 importer = vma;
531 } else if (end > next->vm_start) {
532 /*
533 * vma expands, overlapping part of the next:
534 * mprotect case 5 shifting the boundary up.
535 */
536 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
537 anon_vma = next->anon_vma;
538 importer = vma;
539 } else if (end < vma->vm_end) {
540 /*
541 * vma shrinks, and !insert tells it's not
542 * split_vma inserting another: so it must be
543 * mprotect case 4 shifting the boundary down.
544 */
545 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
546 anon_vma = next->anon_vma;
547 importer = next;
548 }
549 }
550
551 if (file) {
552 mapping = file->f_mapping;
553 if (!(vma->vm_flags & VM_NONLINEAR))
554 root = &mapping->i_mmap;
555 spin_lock(&mapping->i_mmap_lock);
556 if (importer &&
557 vma->vm_truncate_count != next->vm_truncate_count) {
558 /*
559 * unmap_mapping_range might be in progress:
560 * ensure that the expanding vma is rescanned.
561 */
562 importer->vm_truncate_count = 0;
563 }
564 if (insert) {
565 insert->vm_truncate_count = vma->vm_truncate_count;
566 /*
567 * Put into prio_tree now, so instantiated pages
568 * are visible to arm/parisc __flush_dcache_page
569 * throughout; but we cannot insert into address
570 * space until vma start or end is updated.
571 */
572 __vma_link_file(insert);
573 }
574 }
575
576 /*
577 * When changing only vma->vm_end, we don't really need
578 * anon_vma lock: but is that case worth optimizing out?
579 */
580 if (vma->anon_vma)
581 anon_vma = vma->anon_vma;
582 if (anon_vma) {
583 spin_lock(&anon_vma->lock);
584 /*
585 * Easily overlooked: when mprotect shifts the boundary,
586 * make sure the expanding vma has anon_vma set if the
587 * shrinking vma had, to cover any anon pages imported.
588 */
589 if (importer && !importer->anon_vma) {
590 importer->anon_vma = anon_vma;
591 __anon_vma_link(importer);
592 }
593 }
594
595 if (root) {
596 flush_dcache_mmap_lock(mapping);
597 vma_prio_tree_remove(vma, root);
598 if (adjust_next)
599 vma_prio_tree_remove(next, root);
600 }
601
602 vma->vm_start = start;
603 vma->vm_end = end;
604 vma->vm_pgoff = pgoff;
605 if (adjust_next) {
606 next->vm_start += adjust_next << PAGE_SHIFT;
607 next->vm_pgoff += adjust_next;
608 }
609
610 if (root) {
611 if (adjust_next)
612 vma_prio_tree_insert(next, root);
613 vma_prio_tree_insert(vma, root);
614 flush_dcache_mmap_unlock(mapping);
615 }
616
617 if (remove_next) {
618 /*
619 * vma_merge has merged next into vma, and needs
620 * us to remove next before dropping the locks.
621 */
622 __vma_unlink(mm, next, vma);
623 if (file)
624 __remove_shared_vm_struct(next, file, mapping);
625 if (next->anon_vma)
626 __anon_vma_merge(vma, next);
627 } else if (insert) {
628 /*
629 * split_vma has split insert from vma, and needs
630 * us to insert it before dropping the locks
631 * (it may either follow vma or precede it).
632 */
633 __insert_vm_struct(mm, insert);
634 }
635
636 if (anon_vma)
637 spin_unlock(&anon_vma->lock);
638 if (mapping)
639 spin_unlock(&mapping->i_mmap_lock);
640
641 if (remove_next) {
642 if (file) {
643 fput(file);
644 if (next->vm_flags & VM_EXECUTABLE)
645 removed_exe_file_vma(mm);
646 }
647 mm->map_count--;
648 mpol_put(vma_policy(next));
649 kmem_cache_free(vm_area_cachep, next);
650 /*
651 * In mprotect's case 6 (see comments on vma_merge),
652 * we must remove another next too. It would clutter
653 * up the code too much to do both in one go.
654 */
655 if (remove_next == 2) {
656 next = vma->vm_next;
657 goto again;
658 }
659 }
660
661 validate_mm(mm);
662 }
663
664 /* Flags that can be inherited from an existing mapping when merging */
665 #define VM_MERGEABLE_FLAGS (VM_CAN_NONLINEAR)
666
667 /*
668 * If the vma has a ->close operation then the driver probably needs to release
669 * per-vma resources, so we don't attempt to merge those.
670 */
is_mergeable_vma(struct vm_area_struct * vma,struct file * file,unsigned long vm_flags)671 static inline int is_mergeable_vma(struct vm_area_struct *vma,
672 struct file *file, unsigned long vm_flags)
673 {
674 if ((vma->vm_flags ^ vm_flags) & ~VM_MERGEABLE_FLAGS)
675 return 0;
676 if (vma->vm_file != file)
677 return 0;
678 if (vma->vm_ops && vma->vm_ops->close)
679 return 0;
680 return 1;
681 }
682
is_mergeable_anon_vma(struct anon_vma * anon_vma1,struct anon_vma * anon_vma2)683 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
684 struct anon_vma *anon_vma2)
685 {
686 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
687 }
688
689 /*
690 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
691 * in front of (at a lower virtual address and file offset than) the vma.
692 *
693 * We cannot merge two vmas if they have differently assigned (non-NULL)
694 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
695 *
696 * We don't check here for the merged mmap wrapping around the end of pagecache
697 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
698 * wrap, nor mmaps which cover the final page at index -1UL.
699 */
700 static int
can_vma_merge_before(struct vm_area_struct * vma,unsigned long vm_flags,struct anon_vma * anon_vma,struct file * file,pgoff_t vm_pgoff)701 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
702 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
703 {
704 if (is_mergeable_vma(vma, file, vm_flags) &&
705 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
706 if (vma->vm_pgoff == vm_pgoff)
707 return 1;
708 }
709 return 0;
710 }
711
712 /*
713 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
714 * beyond (at a higher virtual address and file offset than) the vma.
715 *
716 * We cannot merge two vmas if they have differently assigned (non-NULL)
717 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
718 */
719 static int
can_vma_merge_after(struct vm_area_struct * vma,unsigned long vm_flags,struct anon_vma * anon_vma,struct file * file,pgoff_t vm_pgoff)720 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
721 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
722 {
723 if (is_mergeable_vma(vma, file, vm_flags) &&
724 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
725 pgoff_t vm_pglen;
726 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
727 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
728 return 1;
729 }
730 return 0;
731 }
732
733 /*
734 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
735 * whether that can be merged with its predecessor or its successor.
736 * Or both (it neatly fills a hole).
737 *
738 * In most cases - when called for mmap, brk or mremap - [addr,end) is
739 * certain not to be mapped by the time vma_merge is called; but when
740 * called for mprotect, it is certain to be already mapped (either at
741 * an offset within prev, or at the start of next), and the flags of
742 * this area are about to be changed to vm_flags - and the no-change
743 * case has already been eliminated.
744 *
745 * The following mprotect cases have to be considered, where AAAA is
746 * the area passed down from mprotect_fixup, never extending beyond one
747 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
748 *
749 * AAAA AAAA AAAA AAAA
750 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
751 * cannot merge might become might become might become
752 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
753 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
754 * mremap move: PPPPNNNNNNNN 8
755 * AAAA
756 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
757 * might become case 1 below case 2 below case 3 below
758 *
759 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
760 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
761 */
vma_merge(struct mm_struct * mm,struct vm_area_struct * prev,unsigned long addr,unsigned long end,unsigned long vm_flags,struct anon_vma * anon_vma,struct file * file,pgoff_t pgoff,struct mempolicy * policy)762 struct vm_area_struct *vma_merge(struct mm_struct *mm,
763 struct vm_area_struct *prev, unsigned long addr,
764 unsigned long end, unsigned long vm_flags,
765 struct anon_vma *anon_vma, struct file *file,
766 pgoff_t pgoff, struct mempolicy *policy)
767 {
768 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
769 struct vm_area_struct *area, *next;
770
771 /*
772 * We later require that vma->vm_flags == vm_flags,
773 * so this tests vma->vm_flags & VM_SPECIAL, too.
774 */
775 if (vm_flags & VM_SPECIAL)
776 return NULL;
777
778 if (prev)
779 next = prev->vm_next;
780 else
781 next = mm->mmap;
782 area = next;
783 if (next && next->vm_end == end) /* cases 6, 7, 8 */
784 next = next->vm_next;
785
786 /*
787 * Can it merge with the predecessor?
788 */
789 if (prev && prev->vm_end == addr &&
790 mpol_equal(vma_policy(prev), policy) &&
791 can_vma_merge_after(prev, vm_flags,
792 anon_vma, file, pgoff)) {
793 /*
794 * OK, it can. Can we now merge in the successor as well?
795 */
796 if (next && end == next->vm_start &&
797 mpol_equal(policy, vma_policy(next)) &&
798 can_vma_merge_before(next, vm_flags,
799 anon_vma, file, pgoff+pglen) &&
800 is_mergeable_anon_vma(prev->anon_vma,
801 next->anon_vma)) {
802 /* cases 1, 6 */
803 vma_adjust(prev, prev->vm_start,
804 next->vm_end, prev->vm_pgoff, NULL);
805 } else /* cases 2, 5, 7 */
806 vma_adjust(prev, prev->vm_start,
807 end, prev->vm_pgoff, NULL);
808 return prev;
809 }
810
811 /*
812 * Can this new request be merged in front of next?
813 */
814 if (next && end == next->vm_start &&
815 mpol_equal(policy, vma_policy(next)) &&
816 can_vma_merge_before(next, vm_flags,
817 anon_vma, file, pgoff+pglen)) {
818 if (prev && addr < prev->vm_end) /* case 4 */
819 vma_adjust(prev, prev->vm_start,
820 addr, prev->vm_pgoff, NULL);
821 else /* cases 3, 8 */
822 vma_adjust(area, addr, next->vm_end,
823 next->vm_pgoff - pglen, NULL);
824 return area;
825 }
826
827 return NULL;
828 }
829
830 /*
831 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
832 * neighbouring vmas for a suitable anon_vma, before it goes off
833 * to allocate a new anon_vma. It checks because a repetitive
834 * sequence of mprotects and faults may otherwise lead to distinct
835 * anon_vmas being allocated, preventing vma merge in subsequent
836 * mprotect.
837 */
find_mergeable_anon_vma(struct vm_area_struct * vma)838 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
839 {
840 struct vm_area_struct *near;
841 unsigned long vm_flags;
842
843 near = vma->vm_next;
844 if (!near)
845 goto try_prev;
846
847 /*
848 * Since only mprotect tries to remerge vmas, match flags
849 * which might be mprotected into each other later on.
850 * Neither mlock nor madvise tries to remerge at present,
851 * so leave their flags as obstructing a merge.
852 */
853 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
854 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
855
856 if (near->anon_vma && vma->vm_end == near->vm_start &&
857 mpol_equal(vma_policy(vma), vma_policy(near)) &&
858 can_vma_merge_before(near, vm_flags,
859 NULL, vma->vm_file, vma->vm_pgoff +
860 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
861 return near->anon_vma;
862 try_prev:
863 /*
864 * It is potentially slow to have to call find_vma_prev here.
865 * But it's only on the first write fault on the vma, not
866 * every time, and we could devise a way to avoid it later
867 * (e.g. stash info in next's anon_vma_node when assigning
868 * an anon_vma, or when trying vma_merge). Another time.
869 */
870 BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
871 if (!near)
872 goto none;
873
874 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
875 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
876
877 if (near->anon_vma && near->vm_end == vma->vm_start &&
878 mpol_equal(vma_policy(near), vma_policy(vma)) &&
879 can_vma_merge_after(near, vm_flags,
880 NULL, vma->vm_file, vma->vm_pgoff))
881 return near->anon_vma;
882 none:
883 /*
884 * There's no absolute need to look only at touching neighbours:
885 * we could search further afield for "compatible" anon_vmas.
886 * But it would probably just be a waste of time searching,
887 * or lead to too many vmas hanging off the same anon_vma.
888 * We're trying to allow mprotect remerging later on,
889 * not trying to minimize memory used for anon_vmas.
890 */
891 return NULL;
892 }
893
894 #ifdef CONFIG_PROC_FS
vm_stat_account(struct mm_struct * mm,unsigned long flags,struct file * file,long pages)895 void vm_stat_account(struct mm_struct *mm, unsigned long flags,
896 struct file *file, long pages)
897 {
898 const unsigned long stack_flags
899 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
900
901 if (file) {
902 mm->shared_vm += pages;
903 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
904 mm->exec_vm += pages;
905 } else if (flags & stack_flags)
906 mm->stack_vm += pages;
907 if (flags & (VM_RESERVED|VM_IO))
908 mm->reserved_vm += pages;
909 }
910 #endif /* CONFIG_PROC_FS */
911
912 #ifdef CONFIG_QEMU_TRACE
913 extern void qemu_trace_mmap(struct vm_area_struct * vma);
914 extern void qemu_trace_munmap(unsigned long start, unsigned long end);
915 #endif
916
917 /*
918 * The caller must hold down_write(current->mm->mmap_sem).
919 */
920
do_mmap_pgoff(struct file * file,unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long pgoff)921 unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
922 unsigned long len, unsigned long prot,
923 unsigned long flags, unsigned long pgoff)
924 {
925 struct mm_struct * mm = current->mm;
926 struct inode *inode;
927 unsigned int vm_flags;
928 int error;
929 unsigned long reqprot = prot;
930
931 /*
932 * Does the application expect PROT_READ to imply PROT_EXEC?
933 *
934 * (the exception is when the underlying filesystem is noexec
935 * mounted, in which case we dont add PROT_EXEC.)
936 */
937 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
938 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
939 prot |= PROT_EXEC;
940
941 if (!len)
942 return -EINVAL;
943
944 if (!(flags & MAP_FIXED))
945 addr = round_hint_to_min(addr);
946
947 error = arch_mmap_check(addr, len, flags);
948 if (error)
949 return error;
950
951 /* Careful about overflows.. */
952 len = PAGE_ALIGN(len);
953 if (!len || len > TASK_SIZE)
954 return -ENOMEM;
955
956 /* offset overflow? */
957 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
958 return -EOVERFLOW;
959
960 /* Too many mappings? */
961 if (mm->map_count > sysctl_max_map_count)
962 return -ENOMEM;
963
964 /* Obtain the address to map to. we verify (or select) it and ensure
965 * that it represents a valid section of the address space.
966 */
967 addr = get_unmapped_area(file, addr, len, pgoff, flags);
968 if (addr & ~PAGE_MASK)
969 return addr;
970
971 /* Do simple checking here so the lower-level routines won't have
972 * to. we assume access permissions have been handled by the open
973 * of the memory object, so we don't do any here.
974 */
975 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
976 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
977
978 if (flags & MAP_LOCKED) {
979 if (!can_do_mlock())
980 return -EPERM;
981 vm_flags |= VM_LOCKED;
982 }
983
984 /* mlock MCL_FUTURE? */
985 if (vm_flags & VM_LOCKED) {
986 unsigned long locked, lock_limit;
987 locked = len >> PAGE_SHIFT;
988 locked += mm->locked_vm;
989 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
990 lock_limit >>= PAGE_SHIFT;
991 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
992 return -EAGAIN;
993 }
994
995 inode = file ? file->f_path.dentry->d_inode : NULL;
996
997 if (file) {
998 switch (flags & MAP_TYPE) {
999 case MAP_SHARED:
1000 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1001 return -EACCES;
1002
1003 /*
1004 * Make sure we don't allow writing to an append-only
1005 * file..
1006 */
1007 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1008 return -EACCES;
1009
1010 /*
1011 * Make sure there are no mandatory locks on the file.
1012 */
1013 if (locks_verify_locked(inode))
1014 return -EAGAIN;
1015
1016 vm_flags |= VM_SHARED | VM_MAYSHARE;
1017 if (!(file->f_mode & FMODE_WRITE))
1018 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1019
1020 /* fall through */
1021 case MAP_PRIVATE:
1022 if (!(file->f_mode & FMODE_READ))
1023 return -EACCES;
1024 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1025 if (vm_flags & VM_EXEC)
1026 return -EPERM;
1027 vm_flags &= ~VM_MAYEXEC;
1028 }
1029
1030 if (!file->f_op || !file->f_op->mmap)
1031 return -ENODEV;
1032 break;
1033
1034 default:
1035 return -EINVAL;
1036 }
1037 } else {
1038 switch (flags & MAP_TYPE) {
1039 case MAP_SHARED:
1040 /*
1041 * Ignore pgoff.
1042 */
1043 pgoff = 0;
1044 vm_flags |= VM_SHARED | VM_MAYSHARE;
1045 break;
1046 case MAP_PRIVATE:
1047 /*
1048 * Set pgoff according to addr for anon_vma.
1049 */
1050 pgoff = addr >> PAGE_SHIFT;
1051 break;
1052 default:
1053 return -EINVAL;
1054 }
1055 }
1056
1057 error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1058 if (error)
1059 return error;
1060
1061 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
1062 }
1063 EXPORT_SYMBOL(do_mmap_pgoff);
1064
1065 /*
1066 * Some shared mappigns will want the pages marked read-only
1067 * to track write events. If so, we'll downgrade vm_page_prot
1068 * to the private version (using protection_map[] without the
1069 * VM_SHARED bit).
1070 */
vma_wants_writenotify(struct vm_area_struct * vma)1071 int vma_wants_writenotify(struct vm_area_struct *vma)
1072 {
1073 unsigned int vm_flags = vma->vm_flags;
1074
1075 /* If it was private or non-writable, the write bit is already clear */
1076 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1077 return 0;
1078
1079 /* The backer wishes to know when pages are first written to? */
1080 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1081 return 1;
1082
1083 /* The open routine did something to the protections already? */
1084 if (pgprot_val(vma->vm_page_prot) !=
1085 pgprot_val(vm_get_page_prot(vm_flags)))
1086 return 0;
1087
1088 /* Specialty mapping? */
1089 if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
1090 return 0;
1091
1092 /* Can the mapping track the dirty pages? */
1093 return vma->vm_file && vma->vm_file->f_mapping &&
1094 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1095 }
1096
1097 /*
1098 * We account for memory if it's a private writeable mapping,
1099 * not hugepages and VM_NORESERVE wasn't set.
1100 */
accountable_mapping(struct file * file,unsigned int vm_flags)1101 static inline int accountable_mapping(struct file *file, unsigned int vm_flags)
1102 {
1103 /*
1104 * hugetlb has its own accounting separate from the core VM
1105 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1106 */
1107 if (file && is_file_hugepages(file))
1108 return 0;
1109
1110 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1111 }
1112
mmap_region(struct file * file,unsigned long addr,unsigned long len,unsigned long flags,unsigned int vm_flags,unsigned long pgoff)1113 unsigned long mmap_region(struct file *file, unsigned long addr,
1114 unsigned long len, unsigned long flags,
1115 unsigned int vm_flags, unsigned long pgoff)
1116 {
1117 struct mm_struct *mm = current->mm;
1118 struct vm_area_struct *vma, *prev;
1119 int correct_wcount = 0;
1120 int error;
1121 struct rb_node **rb_link, *rb_parent;
1122 unsigned long charged = 0;
1123 struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
1124
1125 /* Clear old maps */
1126 error = -ENOMEM;
1127 munmap_back:
1128 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1129 if (vma && vma->vm_start < addr + len) {
1130 if (do_munmap(mm, addr, len))
1131 return -ENOMEM;
1132 goto munmap_back;
1133 }
1134
1135 /* Check against address space limit. */
1136 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1137 return -ENOMEM;
1138
1139 /*
1140 * Set 'VM_NORESERVE' if we should not account for the
1141 * memory use of this mapping.
1142 */
1143 if ((flags & MAP_NORESERVE)) {
1144 /* We honor MAP_NORESERVE if allowed to overcommit */
1145 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1146 vm_flags |= VM_NORESERVE;
1147
1148 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1149 if (file && is_file_hugepages(file))
1150 vm_flags |= VM_NORESERVE;
1151 }
1152
1153 /*
1154 * Private writable mapping: check memory availability
1155 */
1156 if (accountable_mapping(file, vm_flags)) {
1157 charged = len >> PAGE_SHIFT;
1158 if (security_vm_enough_memory(charged))
1159 return -ENOMEM;
1160 vm_flags |= VM_ACCOUNT;
1161 }
1162
1163 /*
1164 * Can we just expand an old mapping?
1165 */
1166 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1167 if (vma)
1168 goto out;
1169
1170 /*
1171 * Determine the object being mapped and call the appropriate
1172 * specific mapper. the address has already been validated, but
1173 * not unmapped, but the maps are removed from the list.
1174 */
1175 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1176 if (!vma) {
1177 error = -ENOMEM;
1178 goto unacct_error;
1179 }
1180
1181 vma->vm_mm = mm;
1182 vma->vm_start = addr;
1183 vma->vm_end = addr + len;
1184 vma->vm_flags = vm_flags;
1185 vma->vm_page_prot = vm_get_page_prot(vm_flags);
1186 vma->vm_pgoff = pgoff;
1187
1188 if (file) {
1189 error = -EINVAL;
1190 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1191 goto free_vma;
1192 if (vm_flags & VM_DENYWRITE) {
1193 error = deny_write_access(file);
1194 if (error)
1195 goto free_vma;
1196 correct_wcount = 1;
1197 }
1198 vma->vm_file = file;
1199 get_file(file);
1200 error = file->f_op->mmap(file, vma);
1201 if (error)
1202 goto unmap_and_free_vma;
1203 if (vm_flags & VM_EXECUTABLE)
1204 added_exe_file_vma(mm);
1205 } else if (vm_flags & VM_SHARED) {
1206 error = shmem_zero_setup(vma);
1207 if (error)
1208 goto free_vma;
1209 }
1210
1211 /* Can addr have changed??
1212 *
1213 * Answer: Yes, several device drivers can do it in their
1214 * f_op->mmap method. -DaveM
1215 */
1216 addr = vma->vm_start;
1217 pgoff = vma->vm_pgoff;
1218 vm_flags = vma->vm_flags;
1219
1220 #ifdef CONFIG_QEMU_TRACE
1221 qemu_trace_mmap(vma);
1222 #endif
1223
1224 if (vma_wants_writenotify(vma))
1225 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
1226
1227 vma_link(mm, vma, prev, rb_link, rb_parent);
1228 file = vma->vm_file;
1229
1230 /* Once vma denies write, undo our temporary denial count */
1231 if (correct_wcount)
1232 atomic_inc(&inode->i_writecount);
1233 out:
1234 mm->total_vm += len >> PAGE_SHIFT;
1235 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1236 if (vm_flags & VM_LOCKED) {
1237 /*
1238 * makes pages present; downgrades, drops, reacquires mmap_sem
1239 */
1240 long nr_pages = mlock_vma_pages_range(vma, addr, addr + len);
1241 if (nr_pages < 0)
1242 return nr_pages; /* vma gone! */
1243 mm->locked_vm += (len >> PAGE_SHIFT) - nr_pages;
1244 } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
1245 make_pages_present(addr, addr + len);
1246 return addr;
1247
1248 unmap_and_free_vma:
1249 if (correct_wcount)
1250 atomic_inc(&inode->i_writecount);
1251 vma->vm_file = NULL;
1252 fput(file);
1253
1254 /* Undo any partial mapping done by a device driver. */
1255 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1256 charged = 0;
1257 free_vma:
1258 kmem_cache_free(vm_area_cachep, vma);
1259 unacct_error:
1260 if (charged)
1261 vm_unacct_memory(charged);
1262 return error;
1263 }
1264
1265 /* Get an address range which is currently unmapped.
1266 * For shmat() with addr=0.
1267 *
1268 * Ugly calling convention alert:
1269 * Return value with the low bits set means error value,
1270 * ie
1271 * if (ret & ~PAGE_MASK)
1272 * error = ret;
1273 *
1274 * This function "knows" that -ENOMEM has the bits set.
1275 */
1276 #ifndef HAVE_ARCH_UNMAPPED_AREA
1277 unsigned long
arch_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)1278 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1279 unsigned long len, unsigned long pgoff, unsigned long flags)
1280 {
1281 struct mm_struct *mm = current->mm;
1282 struct vm_area_struct *vma;
1283 unsigned long start_addr;
1284
1285 if (len > TASK_SIZE)
1286 return -ENOMEM;
1287
1288 if (flags & MAP_FIXED)
1289 return addr;
1290
1291 if (addr) {
1292 addr = PAGE_ALIGN(addr);
1293 vma = find_vma(mm, addr);
1294 if (TASK_SIZE - len >= addr &&
1295 (!vma || addr + len <= vma->vm_start))
1296 return addr;
1297 }
1298 if (len > mm->cached_hole_size) {
1299 start_addr = addr = mm->free_area_cache;
1300 } else {
1301 start_addr = addr = TASK_UNMAPPED_BASE;
1302 mm->cached_hole_size = 0;
1303 }
1304
1305 full_search:
1306 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1307 /* At this point: (!vma || addr < vma->vm_end). */
1308 if (TASK_SIZE - len < addr) {
1309 /*
1310 * Start a new search - just in case we missed
1311 * some holes.
1312 */
1313 if (start_addr != TASK_UNMAPPED_BASE) {
1314 addr = TASK_UNMAPPED_BASE;
1315 start_addr = addr;
1316 mm->cached_hole_size = 0;
1317 goto full_search;
1318 }
1319 return -ENOMEM;
1320 }
1321 if (!vma || addr + len <= vma->vm_start) {
1322 /*
1323 * Remember the place where we stopped the search:
1324 */
1325 mm->free_area_cache = addr + len;
1326 return addr;
1327 }
1328 if (addr + mm->cached_hole_size < vma->vm_start)
1329 mm->cached_hole_size = vma->vm_start - addr;
1330 addr = vma->vm_end;
1331 }
1332 }
1333 #endif
1334
arch_unmap_area(struct mm_struct * mm,unsigned long addr)1335 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1336 {
1337 /*
1338 * Is this a new hole at the lowest possible address?
1339 */
1340 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
1341 mm->free_area_cache = addr;
1342 mm->cached_hole_size = ~0UL;
1343 }
1344 }
1345
1346 /*
1347 * This mmap-allocator allocates new areas top-down from below the
1348 * stack's low limit (the base):
1349 */
1350 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1351 unsigned long
arch_get_unmapped_area_topdown(struct file * filp,const unsigned long addr0,const unsigned long len,const unsigned long pgoff,const unsigned long flags)1352 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1353 const unsigned long len, const unsigned long pgoff,
1354 const unsigned long flags)
1355 {
1356 struct vm_area_struct *vma;
1357 struct mm_struct *mm = current->mm;
1358 unsigned long addr = addr0;
1359
1360 /* requested length too big for entire address space */
1361 if (len > TASK_SIZE)
1362 return -ENOMEM;
1363
1364 if (flags & MAP_FIXED)
1365 return addr;
1366
1367 /* requesting a specific address */
1368 if (addr) {
1369 addr = PAGE_ALIGN(addr);
1370 vma = find_vma(mm, addr);
1371 if (TASK_SIZE - len >= addr &&
1372 (!vma || addr + len <= vma->vm_start))
1373 return addr;
1374 }
1375
1376 /* check if free_area_cache is useful for us */
1377 if (len <= mm->cached_hole_size) {
1378 mm->cached_hole_size = 0;
1379 mm->free_area_cache = mm->mmap_base;
1380 }
1381
1382 /* either no address requested or can't fit in requested address hole */
1383 addr = mm->free_area_cache;
1384
1385 /* make sure it can fit in the remaining address space */
1386 if (addr > len) {
1387 vma = find_vma(mm, addr-len);
1388 if (!vma || addr <= vma->vm_start)
1389 /* remember the address as a hint for next time */
1390 return (mm->free_area_cache = addr-len);
1391 }
1392
1393 if (mm->mmap_base < len)
1394 goto bottomup;
1395
1396 addr = mm->mmap_base-len;
1397
1398 do {
1399 /*
1400 * Lookup failure means no vma is above this address,
1401 * else if new region fits below vma->vm_start,
1402 * return with success:
1403 */
1404 vma = find_vma(mm, addr);
1405 if (!vma || addr+len <= vma->vm_start)
1406 /* remember the address as a hint for next time */
1407 return (mm->free_area_cache = addr);
1408
1409 /* remember the largest hole we saw so far */
1410 if (addr + mm->cached_hole_size < vma->vm_start)
1411 mm->cached_hole_size = vma->vm_start - addr;
1412
1413 /* try just below the current vma->vm_start */
1414 addr = vma->vm_start-len;
1415 } while (len < vma->vm_start);
1416
1417 bottomup:
1418 /*
1419 * A failed mmap() very likely causes application failure,
1420 * so fall back to the bottom-up function here. This scenario
1421 * can happen with large stack limits and large mmap()
1422 * allocations.
1423 */
1424 mm->cached_hole_size = ~0UL;
1425 mm->free_area_cache = TASK_UNMAPPED_BASE;
1426 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1427 /*
1428 * Restore the topdown base:
1429 */
1430 mm->free_area_cache = mm->mmap_base;
1431 mm->cached_hole_size = ~0UL;
1432
1433 return addr;
1434 }
1435 #endif
1436
arch_unmap_area_topdown(struct mm_struct * mm,unsigned long addr)1437 void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
1438 {
1439 /*
1440 * Is this a new hole at the highest possible address?
1441 */
1442 if (addr > mm->free_area_cache)
1443 mm->free_area_cache = addr;
1444
1445 /* dont allow allocations above current base */
1446 if (mm->free_area_cache > mm->mmap_base)
1447 mm->free_area_cache = mm->mmap_base;
1448 }
1449
1450 unsigned long
get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)1451 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1452 unsigned long pgoff, unsigned long flags)
1453 {
1454 unsigned long (*get_area)(struct file *, unsigned long,
1455 unsigned long, unsigned long, unsigned long);
1456
1457 get_area = current->mm->get_unmapped_area;
1458 if (file && file->f_op && file->f_op->get_unmapped_area)
1459 get_area = file->f_op->get_unmapped_area;
1460 addr = get_area(file, addr, len, pgoff, flags);
1461 if (IS_ERR_VALUE(addr))
1462 return addr;
1463
1464 if (addr > TASK_SIZE - len)
1465 return -ENOMEM;
1466 if (addr & ~PAGE_MASK)
1467 return -EINVAL;
1468
1469 return arch_rebalance_pgtables(addr, len);
1470 }
1471
1472 EXPORT_SYMBOL(get_unmapped_area);
1473
1474 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
find_vma(struct mm_struct * mm,unsigned long addr)1475 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
1476 {
1477 struct vm_area_struct *vma = NULL;
1478
1479 if (mm) {
1480 /* Check the cache first. */
1481 /* (Cache hit rate is typically around 35%.) */
1482 vma = mm->mmap_cache;
1483 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1484 struct rb_node * rb_node;
1485
1486 rb_node = mm->mm_rb.rb_node;
1487 vma = NULL;
1488
1489 while (rb_node) {
1490 struct vm_area_struct * vma_tmp;
1491
1492 vma_tmp = rb_entry(rb_node,
1493 struct vm_area_struct, vm_rb);
1494
1495 if (vma_tmp->vm_end > addr) {
1496 vma = vma_tmp;
1497 if (vma_tmp->vm_start <= addr)
1498 break;
1499 rb_node = rb_node->rb_left;
1500 } else
1501 rb_node = rb_node->rb_right;
1502 }
1503 if (vma)
1504 mm->mmap_cache = vma;
1505 }
1506 }
1507 return vma;
1508 }
1509
1510 EXPORT_SYMBOL(find_vma);
1511
1512 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1513 struct vm_area_struct *
find_vma_prev(struct mm_struct * mm,unsigned long addr,struct vm_area_struct ** pprev)1514 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1515 struct vm_area_struct **pprev)
1516 {
1517 struct vm_area_struct *vma = NULL, *prev = NULL;
1518 struct rb_node *rb_node;
1519 if (!mm)
1520 goto out;
1521
1522 /* Guard against addr being lower than the first VMA */
1523 vma = mm->mmap;
1524
1525 /* Go through the RB tree quickly. */
1526 rb_node = mm->mm_rb.rb_node;
1527
1528 while (rb_node) {
1529 struct vm_area_struct *vma_tmp;
1530 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1531
1532 if (addr < vma_tmp->vm_end) {
1533 rb_node = rb_node->rb_left;
1534 } else {
1535 prev = vma_tmp;
1536 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1537 break;
1538 rb_node = rb_node->rb_right;
1539 }
1540 }
1541
1542 out:
1543 *pprev = prev;
1544 return prev ? prev->vm_next : vma;
1545 }
1546
1547 /*
1548 * Verify that the stack growth is acceptable and
1549 * update accounting. This is shared with both the
1550 * grow-up and grow-down cases.
1551 */
acct_stack_growth(struct vm_area_struct * vma,unsigned long size,unsigned long grow)1552 static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
1553 {
1554 struct mm_struct *mm = vma->vm_mm;
1555 struct rlimit *rlim = current->signal->rlim;
1556 unsigned long new_start;
1557
1558 /* address space limit tests */
1559 if (!may_expand_vm(mm, grow))
1560 return -ENOMEM;
1561
1562 /* Stack limit test */
1563 if (size > rlim[RLIMIT_STACK].rlim_cur)
1564 return -ENOMEM;
1565
1566 /* mlock limit tests */
1567 if (vma->vm_flags & VM_LOCKED) {
1568 unsigned long locked;
1569 unsigned long limit;
1570 locked = mm->locked_vm + grow;
1571 limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
1572 if (locked > limit && !capable(CAP_IPC_LOCK))
1573 return -ENOMEM;
1574 }
1575
1576 /* Check to ensure the stack will not grow into a hugetlb-only region */
1577 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1578 vma->vm_end - size;
1579 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1580 return -EFAULT;
1581
1582 /*
1583 * Overcommit.. This must be the final test, as it will
1584 * update security statistics.
1585 */
1586 if (security_vm_enough_memory(grow))
1587 return -ENOMEM;
1588
1589 /* Ok, everything looks good - let it rip */
1590 mm->total_vm += grow;
1591 if (vma->vm_flags & VM_LOCKED)
1592 mm->locked_vm += grow;
1593 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
1594 return 0;
1595 }
1596
1597 #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
1598 /*
1599 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1600 * vma is the last one with address > vma->vm_end. Have to extend vma.
1601 */
1602 #ifndef CONFIG_IA64
1603 static
1604 #endif
expand_upwards(struct vm_area_struct * vma,unsigned long address)1605 int expand_upwards(struct vm_area_struct *vma, unsigned long address)
1606 {
1607 int error;
1608
1609 if (!(vma->vm_flags & VM_GROWSUP))
1610 return -EFAULT;
1611
1612 /*
1613 * We must make sure the anon_vma is allocated
1614 * so that the anon_vma locking is not a noop.
1615 */
1616 if (unlikely(anon_vma_prepare(vma)))
1617 return -ENOMEM;
1618 anon_vma_lock(vma);
1619
1620 /*
1621 * vma->vm_start/vm_end cannot change under us because the caller
1622 * is required to hold the mmap_sem in read mode. We need the
1623 * anon_vma lock to serialize against concurrent expand_stacks.
1624 * Also guard against wrapping around to address 0.
1625 */
1626 if (address < PAGE_ALIGN(address+4))
1627 address = PAGE_ALIGN(address+4);
1628 else {
1629 anon_vma_unlock(vma);
1630 return -ENOMEM;
1631 }
1632 error = 0;
1633
1634 /* Somebody else might have raced and expanded it already */
1635 if (address > vma->vm_end) {
1636 unsigned long size, grow;
1637
1638 size = address - vma->vm_start;
1639 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1640
1641 error = acct_stack_growth(vma, size, grow);
1642 if (!error)
1643 vma->vm_end = address;
1644 }
1645 anon_vma_unlock(vma);
1646 return error;
1647 }
1648 #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1649
1650 /*
1651 * vma is the first one with address < vma->vm_start. Have to extend vma.
1652 */
expand_downwards(struct vm_area_struct * vma,unsigned long address)1653 static int expand_downwards(struct vm_area_struct *vma,
1654 unsigned long address)
1655 {
1656 int error;
1657
1658 /*
1659 * We must make sure the anon_vma is allocated
1660 * so that the anon_vma locking is not a noop.
1661 */
1662 if (unlikely(anon_vma_prepare(vma)))
1663 return -ENOMEM;
1664
1665 address &= PAGE_MASK;
1666 error = security_file_mmap(NULL, 0, 0, 0, address, 1);
1667 if (error)
1668 return error;
1669
1670 anon_vma_lock(vma);
1671
1672 /*
1673 * vma->vm_start/vm_end cannot change under us because the caller
1674 * is required to hold the mmap_sem in read mode. We need the
1675 * anon_vma lock to serialize against concurrent expand_stacks.
1676 */
1677
1678 /* Somebody else might have raced and expanded it already */
1679 if (address < vma->vm_start) {
1680 unsigned long size, grow;
1681
1682 size = vma->vm_end - address;
1683 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1684
1685 error = acct_stack_growth(vma, size, grow);
1686 if (!error) {
1687 vma->vm_start = address;
1688 vma->vm_pgoff -= grow;
1689 }
1690 }
1691 anon_vma_unlock(vma);
1692 return error;
1693 }
1694
expand_stack_downwards(struct vm_area_struct * vma,unsigned long address)1695 int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
1696 {
1697 return expand_downwards(vma, address);
1698 }
1699
1700 #ifdef CONFIG_STACK_GROWSUP
expand_stack(struct vm_area_struct * vma,unsigned long address)1701 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1702 {
1703 return expand_upwards(vma, address);
1704 }
1705
1706 struct vm_area_struct *
find_extend_vma(struct mm_struct * mm,unsigned long addr)1707 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1708 {
1709 struct vm_area_struct *vma, *prev;
1710
1711 addr &= PAGE_MASK;
1712 vma = find_vma_prev(mm, addr, &prev);
1713 if (vma && (vma->vm_start <= addr))
1714 return vma;
1715 if (!prev || expand_stack(prev, addr))
1716 return NULL;
1717 if (prev->vm_flags & VM_LOCKED) {
1718 if (mlock_vma_pages_range(prev, addr, prev->vm_end) < 0)
1719 return NULL; /* vma gone! */
1720 }
1721 return prev;
1722 }
1723 #else
expand_stack(struct vm_area_struct * vma,unsigned long address)1724 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1725 {
1726 return expand_downwards(vma, address);
1727 }
1728
1729 struct vm_area_struct *
find_extend_vma(struct mm_struct * mm,unsigned long addr)1730 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1731 {
1732 struct vm_area_struct * vma;
1733 unsigned long start;
1734
1735 addr &= PAGE_MASK;
1736 vma = find_vma(mm,addr);
1737 if (!vma)
1738 return NULL;
1739 if (vma->vm_start <= addr)
1740 return vma;
1741 if (!(vma->vm_flags & VM_GROWSDOWN))
1742 return NULL;
1743 start = vma->vm_start;
1744 if (expand_stack(vma, addr))
1745 return NULL;
1746 if (vma->vm_flags & VM_LOCKED) {
1747 if (mlock_vma_pages_range(vma, addr, start) < 0)
1748 return NULL; /* vma gone! */
1749 }
1750 return vma;
1751 }
1752 #endif
1753
1754 /*
1755 * Ok - we have the memory areas we should free on the vma list,
1756 * so release them, and do the vma updates.
1757 *
1758 * Called with the mm semaphore held.
1759 */
remove_vma_list(struct mm_struct * mm,struct vm_area_struct * vma)1760 static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
1761 {
1762 /* Update high watermark before we lower total_vm */
1763 update_hiwater_vm(mm);
1764 do {
1765 long nrpages = vma_pages(vma);
1766
1767 mm->total_vm -= nrpages;
1768 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
1769 vma = remove_vma(vma);
1770 } while (vma);
1771 validate_mm(mm);
1772 }
1773
1774 /*
1775 * Get rid of page table information in the indicated region.
1776 *
1777 * Called with the mm semaphore held.
1778 */
unmap_region(struct mm_struct * mm,struct vm_area_struct * vma,struct vm_area_struct * prev,unsigned long start,unsigned long end)1779 static void unmap_region(struct mm_struct *mm,
1780 struct vm_area_struct *vma, struct vm_area_struct *prev,
1781 unsigned long start, unsigned long end)
1782 {
1783 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
1784 struct mmu_gather *tlb;
1785 unsigned long nr_accounted = 0;
1786
1787 lru_add_drain();
1788 tlb = tlb_gather_mmu(mm, 0);
1789 update_hiwater_rss(mm);
1790 unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
1791 vm_unacct_memory(nr_accounted);
1792 free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1793 next? next->vm_start: 0);
1794 tlb_finish_mmu(tlb, start, end);
1795 }
1796
1797 /*
1798 * Create a list of vma's touched by the unmap, removing them from the mm's
1799 * vma list as we go..
1800 */
1801 static void
detach_vmas_to_be_unmapped(struct mm_struct * mm,struct vm_area_struct * vma,struct vm_area_struct * prev,unsigned long end)1802 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1803 struct vm_area_struct *prev, unsigned long end)
1804 {
1805 struct vm_area_struct **insertion_point;
1806 struct vm_area_struct *tail_vma = NULL;
1807 unsigned long addr;
1808
1809 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1810 do {
1811 rb_erase(&vma->vm_rb, &mm->mm_rb);
1812 mm->map_count--;
1813 tail_vma = vma;
1814 vma = vma->vm_next;
1815 } while (vma && vma->vm_start < end);
1816 *insertion_point = vma;
1817 tail_vma->vm_next = NULL;
1818 if (mm->unmap_area == arch_unmap_area)
1819 addr = prev ? prev->vm_end : mm->mmap_base;
1820 else
1821 addr = vma ? vma->vm_start : mm->mmap_base;
1822 mm->unmap_area(mm, addr);
1823 mm->mmap_cache = NULL; /* Kill the cache. */
1824 }
1825
1826 /*
1827 * Split a vma into two pieces at address 'addr', a new vma is allocated
1828 * either for the first part or the tail.
1829 */
split_vma(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,int new_below)1830 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1831 unsigned long addr, int new_below)
1832 {
1833 struct mempolicy *pol;
1834 struct vm_area_struct *new;
1835
1836 if (is_vm_hugetlb_page(vma) && (addr &
1837 ~(huge_page_mask(hstate_vma(vma)))))
1838 return -EINVAL;
1839
1840 if (mm->map_count >= sysctl_max_map_count)
1841 return -ENOMEM;
1842
1843 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1844 if (!new)
1845 return -ENOMEM;
1846
1847 /* most fields are the same, copy all, and then fixup */
1848 *new = *vma;
1849
1850 if (new_below)
1851 new->vm_end = addr;
1852 else {
1853 new->vm_start = addr;
1854 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1855 }
1856
1857 pol = mpol_dup(vma_policy(vma));
1858 if (IS_ERR(pol)) {
1859 kmem_cache_free(vm_area_cachep, new);
1860 return PTR_ERR(pol);
1861 }
1862 vma_set_policy(new, pol);
1863
1864 if (new->vm_file) {
1865 get_file(new->vm_file);
1866 if (vma->vm_flags & VM_EXECUTABLE)
1867 added_exe_file_vma(mm);
1868 }
1869
1870 if (new->vm_ops && new->vm_ops->open)
1871 new->vm_ops->open(new);
1872
1873 if (new_below)
1874 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1875 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1876 else
1877 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1878
1879 return 0;
1880 }
1881
1882 /* Munmap is split into 2 main parts -- this part which finds
1883 * what needs doing, and the areas themselves, which do the
1884 * work. This now handles partial unmappings.
1885 * Jeremy Fitzhardinge <jeremy@goop.org>
1886 */
do_munmap(struct mm_struct * mm,unsigned long start,size_t len)1887 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1888 {
1889 unsigned long end;
1890 struct vm_area_struct *vma, *prev, *last;
1891
1892 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1893 return -EINVAL;
1894
1895 if ((len = PAGE_ALIGN(len)) == 0)
1896 return -EINVAL;
1897
1898 /* Find the first overlapping VMA */
1899 vma = find_vma_prev(mm, start, &prev);
1900 if (!vma)
1901 return 0;
1902 /* we have start < vma->vm_end */
1903
1904 /* if it doesn't overlap, we have nothing.. */
1905 end = start + len;
1906 if (vma->vm_start >= end)
1907 return 0;
1908
1909 /*
1910 * If we need to split any vma, do it now to save pain later.
1911 *
1912 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1913 * unmapped vm_area_struct will remain in use: so lower split_vma
1914 * places tmp vma above, and higher split_vma places tmp vma below.
1915 */
1916 if (start > vma->vm_start) {
1917 int error = split_vma(mm, vma, start, 0);
1918 if (error)
1919 return error;
1920 prev = vma;
1921 }
1922
1923 /* Does it split the last one? */
1924 last = find_vma(mm, end);
1925 if (last && end > last->vm_start) {
1926 int error = split_vma(mm, last, end, 1);
1927 if (error)
1928 return error;
1929 }
1930 vma = prev? prev->vm_next: mm->mmap;
1931
1932 /*
1933 * unlock any mlock()ed ranges before detaching vmas
1934 */
1935 if (mm->locked_vm) {
1936 struct vm_area_struct *tmp = vma;
1937 while (tmp && tmp->vm_start < end) {
1938 if (tmp->vm_flags & VM_LOCKED) {
1939 mm->locked_vm -= vma_pages(tmp);
1940 munlock_vma_pages_all(tmp);
1941 }
1942 tmp = tmp->vm_next;
1943 }
1944 }
1945
1946 /*
1947 * Remove the vma's, and unmap the actual pages
1948 */
1949 detach_vmas_to_be_unmapped(mm, vma, prev, end);
1950
1951 #ifdef CONFIG_QEMU_TRACE
1952 qemu_trace_munmap(start, end);
1953 #endif
1954 unmap_region(mm, vma, prev, start, end);
1955
1956 /* Fix up all other VM information */
1957 remove_vma_list(mm, vma);
1958
1959 return 0;
1960 }
1961
1962 EXPORT_SYMBOL(do_munmap);
1963
SYSCALL_DEFINE2(munmap,unsigned long,addr,size_t,len)1964 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1965 {
1966 int ret;
1967 struct mm_struct *mm = current->mm;
1968
1969 profile_munmap(addr);
1970
1971 down_write(&mm->mmap_sem);
1972 ret = do_munmap(mm, addr, len);
1973 up_write(&mm->mmap_sem);
1974 return ret;
1975 }
1976
verify_mm_writelocked(struct mm_struct * mm)1977 static inline void verify_mm_writelocked(struct mm_struct *mm)
1978 {
1979 #ifdef CONFIG_DEBUG_VM
1980 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
1981 WARN_ON(1);
1982 up_read(&mm->mmap_sem);
1983 }
1984 #endif
1985 }
1986
1987 /*
1988 * this is really a simplified "do_mmap". it only handles
1989 * anonymous maps. eventually we may be able to do some
1990 * brk-specific accounting here.
1991 */
do_brk(unsigned long addr,unsigned long len)1992 unsigned long do_brk(unsigned long addr, unsigned long len)
1993 {
1994 struct mm_struct * mm = current->mm;
1995 struct vm_area_struct * vma, * prev;
1996 unsigned long flags;
1997 struct rb_node ** rb_link, * rb_parent;
1998 pgoff_t pgoff = addr >> PAGE_SHIFT;
1999 int error;
2000
2001 len = PAGE_ALIGN(len);
2002 if (!len)
2003 return addr;
2004
2005 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
2006 return -EINVAL;
2007
2008 if (is_hugepage_only_range(mm, addr, len))
2009 return -EINVAL;
2010
2011 error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
2012 if (error)
2013 return error;
2014
2015 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2016
2017 error = arch_mmap_check(addr, len, flags);
2018 if (error)
2019 return error;
2020
2021 /*
2022 * mlock MCL_FUTURE?
2023 */
2024 if (mm->def_flags & VM_LOCKED) {
2025 unsigned long locked, lock_limit;
2026 locked = len >> PAGE_SHIFT;
2027 locked += mm->locked_vm;
2028 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2029 lock_limit >>= PAGE_SHIFT;
2030 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
2031 return -EAGAIN;
2032 }
2033
2034 /*
2035 * mm->mmap_sem is required to protect against another thread
2036 * changing the mappings in case we sleep.
2037 */
2038 verify_mm_writelocked(mm);
2039
2040 /*
2041 * Clear old maps. this also does some error checking for us
2042 */
2043 munmap_back:
2044 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2045 if (vma && vma->vm_start < addr + len) {
2046 if (do_munmap(mm, addr, len))
2047 return -ENOMEM;
2048 goto munmap_back;
2049 }
2050
2051 /* Check against address space limits *after* clearing old maps... */
2052 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
2053 return -ENOMEM;
2054
2055 if (mm->map_count > sysctl_max_map_count)
2056 return -ENOMEM;
2057
2058 if (security_vm_enough_memory(len >> PAGE_SHIFT))
2059 return -ENOMEM;
2060
2061 /* Can we just expand an old private anonymous mapping? */
2062 vma = vma_merge(mm, prev, addr, addr + len, flags,
2063 NULL, NULL, pgoff, NULL);
2064 if (vma)
2065 goto out;
2066
2067 /*
2068 * create a vma struct for an anonymous mapping
2069 */
2070 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2071 if (!vma) {
2072 vm_unacct_memory(len >> PAGE_SHIFT);
2073 return -ENOMEM;
2074 }
2075
2076 vma->vm_mm = mm;
2077 vma->vm_start = addr;
2078 vma->vm_end = addr + len;
2079 vma->vm_pgoff = pgoff;
2080 vma->vm_flags = flags;
2081 vma->vm_page_prot = vm_get_page_prot(flags);
2082 vma_link(mm, vma, prev, rb_link, rb_parent);
2083 out:
2084 mm->total_vm += len >> PAGE_SHIFT;
2085 if (flags & VM_LOCKED) {
2086 if (!mlock_vma_pages_range(vma, addr, addr + len))
2087 mm->locked_vm += (len >> PAGE_SHIFT);
2088 }
2089 return addr;
2090 }
2091
2092 EXPORT_SYMBOL(do_brk);
2093
2094 /* Release all mmaps. */
exit_mmap(struct mm_struct * mm)2095 void exit_mmap(struct mm_struct *mm)
2096 {
2097 struct mmu_gather *tlb;
2098 struct vm_area_struct *vma;
2099 unsigned long nr_accounted = 0;
2100 unsigned long end;
2101
2102 /* mm's last user has gone, and its about to be pulled down */
2103 mmu_notifier_release(mm);
2104
2105 if (mm->locked_vm) {
2106 vma = mm->mmap;
2107 while (vma) {
2108 if (vma->vm_flags & VM_LOCKED)
2109 munlock_vma_pages_all(vma);
2110 vma = vma->vm_next;
2111 }
2112 }
2113
2114 arch_exit_mmap(mm);
2115
2116 vma = mm->mmap;
2117 if (!vma) /* Can happen if dup_mmap() received an OOM */
2118 return;
2119
2120 lru_add_drain();
2121 flush_cache_mm(mm);
2122 tlb = tlb_gather_mmu(mm, 1);
2123 /* update_hiwater_rss(mm) here? but nobody should be looking */
2124 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2125 end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
2126 vm_unacct_memory(nr_accounted);
2127 free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
2128 tlb_finish_mmu(tlb, 0, end);
2129
2130 /*
2131 * Walk the list again, actually closing and freeing it,
2132 * with preemption enabled, without holding any MM locks.
2133 */
2134 while (vma)
2135 vma = remove_vma(vma);
2136
2137 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
2138 }
2139
2140 /* Insert vm structure into process list sorted by address
2141 * and into the inode's i_mmap tree. If vm_file is non-NULL
2142 * then i_mmap_lock is taken here.
2143 */
insert_vm_struct(struct mm_struct * mm,struct vm_area_struct * vma)2144 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2145 {
2146 struct vm_area_struct * __vma, * prev;
2147 struct rb_node ** rb_link, * rb_parent;
2148
2149 /*
2150 * The vm_pgoff of a purely anonymous vma should be irrelevant
2151 * until its first write fault, when page's anon_vma and index
2152 * are set. But now set the vm_pgoff it will almost certainly
2153 * end up with (unless mremap moves it elsewhere before that
2154 * first wfault), so /proc/pid/maps tells a consistent story.
2155 *
2156 * By setting it to reflect the virtual start address of the
2157 * vma, merges and splits can happen in a seamless way, just
2158 * using the existing file pgoff checks and manipulations.
2159 * Similarly in do_mmap_pgoff and in do_brk.
2160 */
2161 if (!vma->vm_file) {
2162 BUG_ON(vma->anon_vma);
2163 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2164 }
2165 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2166 if (__vma && __vma->vm_start < vma->vm_end)
2167 return -ENOMEM;
2168 if ((vma->vm_flags & VM_ACCOUNT) &&
2169 security_vm_enough_memory_mm(mm, vma_pages(vma)))
2170 return -ENOMEM;
2171 vma_link(mm, vma, prev, rb_link, rb_parent);
2172 return 0;
2173 }
2174
2175 /*
2176 * Copy the vma structure to a new location in the same mm,
2177 * prior to moving page table entries, to effect an mremap move.
2178 */
copy_vma(struct vm_area_struct ** vmap,unsigned long addr,unsigned long len,pgoff_t pgoff)2179 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2180 unsigned long addr, unsigned long len, pgoff_t pgoff)
2181 {
2182 struct vm_area_struct *vma = *vmap;
2183 unsigned long vma_start = vma->vm_start;
2184 struct mm_struct *mm = vma->vm_mm;
2185 struct vm_area_struct *new_vma, *prev;
2186 struct rb_node **rb_link, *rb_parent;
2187 struct mempolicy *pol;
2188
2189 /*
2190 * If anonymous vma has not yet been faulted, update new pgoff
2191 * to match new location, to increase its chance of merging.
2192 */
2193 if (!vma->vm_file && !vma->anon_vma)
2194 pgoff = addr >> PAGE_SHIFT;
2195
2196 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2197 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2198 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2199 if (new_vma) {
2200 /*
2201 * Source vma may have been merged into new_vma
2202 */
2203 if (vma_start >= new_vma->vm_start &&
2204 vma_start < new_vma->vm_end)
2205 *vmap = new_vma;
2206 } else {
2207 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
2208 if (new_vma) {
2209 *new_vma = *vma;
2210 pol = mpol_dup(vma_policy(vma));
2211 if (IS_ERR(pol)) {
2212 kmem_cache_free(vm_area_cachep, new_vma);
2213 return NULL;
2214 }
2215 vma_set_policy(new_vma, pol);
2216 new_vma->vm_start = addr;
2217 new_vma->vm_end = addr + len;
2218 new_vma->vm_pgoff = pgoff;
2219 if (new_vma->vm_file) {
2220 get_file(new_vma->vm_file);
2221 if (vma->vm_flags & VM_EXECUTABLE)
2222 added_exe_file_vma(mm);
2223 }
2224 if (new_vma->vm_ops && new_vma->vm_ops->open)
2225 new_vma->vm_ops->open(new_vma);
2226 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2227 }
2228 }
2229 return new_vma;
2230 }
2231
2232 /*
2233 * Return true if the calling process may expand its vm space by the passed
2234 * number of pages
2235 */
may_expand_vm(struct mm_struct * mm,unsigned long npages)2236 int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2237 {
2238 unsigned long cur = mm->total_vm; /* pages */
2239 unsigned long lim;
2240
2241 lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2242
2243 if (cur + npages > lim)
2244 return 0;
2245 return 1;
2246 }
2247
2248
special_mapping_fault(struct vm_area_struct * vma,struct vm_fault * vmf)2249 static int special_mapping_fault(struct vm_area_struct *vma,
2250 struct vm_fault *vmf)
2251 {
2252 pgoff_t pgoff;
2253 struct page **pages;
2254
2255 /*
2256 * special mappings have no vm_file, and in that case, the mm
2257 * uses vm_pgoff internally. So we have to subtract it from here.
2258 * We are allowed to do this because we are the mm; do not copy
2259 * this code into drivers!
2260 */
2261 pgoff = vmf->pgoff - vma->vm_pgoff;
2262
2263 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2264 pgoff--;
2265
2266 if (*pages) {
2267 struct page *page = *pages;
2268 get_page(page);
2269 vmf->page = page;
2270 return 0;
2271 }
2272
2273 return VM_FAULT_SIGBUS;
2274 }
2275
2276 /*
2277 * Having a close hook prevents vma merging regardless of flags.
2278 */
special_mapping_close(struct vm_area_struct * vma)2279 static void special_mapping_close(struct vm_area_struct *vma)
2280 {
2281 }
2282
2283 static struct vm_operations_struct special_mapping_vmops = {
2284 .close = special_mapping_close,
2285 .fault = special_mapping_fault,
2286 };
2287
2288 /*
2289 * Called with mm->mmap_sem held for writing.
2290 * Insert a new vma covering the given region, with the given flags.
2291 * Its pages are supplied by the given array of struct page *.
2292 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2293 * The region past the last page supplied will always produce SIGBUS.
2294 * The array pointer and the pages it points to are assumed to stay alive
2295 * for as long as this mapping might exist.
2296 */
install_special_mapping(struct mm_struct * mm,unsigned long addr,unsigned long len,unsigned long vm_flags,struct page ** pages)2297 int install_special_mapping(struct mm_struct *mm,
2298 unsigned long addr, unsigned long len,
2299 unsigned long vm_flags, struct page **pages)
2300 {
2301 struct vm_area_struct *vma;
2302
2303 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2304 if (unlikely(vma == NULL))
2305 return -ENOMEM;
2306
2307 vma->vm_mm = mm;
2308 vma->vm_start = addr;
2309 vma->vm_end = addr + len;
2310
2311 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
2312 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
2313
2314 vma->vm_ops = &special_mapping_vmops;
2315 vma->vm_private_data = pages;
2316
2317 if (unlikely(insert_vm_struct(mm, vma))) {
2318 kmem_cache_free(vm_area_cachep, vma);
2319 return -ENOMEM;
2320 }
2321
2322 mm->total_vm += len >> PAGE_SHIFT;
2323
2324 return 0;
2325 }
2326
2327 static DEFINE_MUTEX(mm_all_locks_mutex);
2328
vm_lock_anon_vma(struct mm_struct * mm,struct anon_vma * anon_vma)2329 static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
2330 {
2331 if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
2332 /*
2333 * The LSB of head.next can't change from under us
2334 * because we hold the mm_all_locks_mutex.
2335 */
2336 spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
2337 /*
2338 * We can safely modify head.next after taking the
2339 * anon_vma->lock. If some other vma in this mm shares
2340 * the same anon_vma we won't take it again.
2341 *
2342 * No need of atomic instructions here, head.next
2343 * can't change from under us thanks to the
2344 * anon_vma->lock.
2345 */
2346 if (__test_and_set_bit(0, (unsigned long *)
2347 &anon_vma->head.next))
2348 BUG();
2349 }
2350 }
2351
vm_lock_mapping(struct mm_struct * mm,struct address_space * mapping)2352 static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
2353 {
2354 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2355 /*
2356 * AS_MM_ALL_LOCKS can't change from under us because
2357 * we hold the mm_all_locks_mutex.
2358 *
2359 * Operations on ->flags have to be atomic because
2360 * even if AS_MM_ALL_LOCKS is stable thanks to the
2361 * mm_all_locks_mutex, there may be other cpus
2362 * changing other bitflags in parallel to us.
2363 */
2364 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2365 BUG();
2366 spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
2367 }
2368 }
2369
2370 /*
2371 * This operation locks against the VM for all pte/vma/mm related
2372 * operations that could ever happen on a certain mm. This includes
2373 * vmtruncate, try_to_unmap, and all page faults.
2374 *
2375 * The caller must take the mmap_sem in write mode before calling
2376 * mm_take_all_locks(). The caller isn't allowed to release the
2377 * mmap_sem until mm_drop_all_locks() returns.
2378 *
2379 * mmap_sem in write mode is required in order to block all operations
2380 * that could modify pagetables and free pages without need of
2381 * altering the vma layout (for example populate_range() with
2382 * nonlinear vmas). It's also needed in write mode to avoid new
2383 * anon_vmas to be associated with existing vmas.
2384 *
2385 * A single task can't take more than one mm_take_all_locks() in a row
2386 * or it would deadlock.
2387 *
2388 * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
2389 * mapping->flags avoid to take the same lock twice, if more than one
2390 * vma in this mm is backed by the same anon_vma or address_space.
2391 *
2392 * We can take all the locks in random order because the VM code
2393 * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
2394 * takes more than one of them in a row. Secondly we're protected
2395 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2396 *
2397 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2398 * that may have to take thousand of locks.
2399 *
2400 * mm_take_all_locks() can fail if it's interrupted by signals.
2401 */
mm_take_all_locks(struct mm_struct * mm)2402 int mm_take_all_locks(struct mm_struct *mm)
2403 {
2404 struct vm_area_struct *vma;
2405 int ret = -EINTR;
2406
2407 BUG_ON(down_read_trylock(&mm->mmap_sem));
2408
2409 mutex_lock(&mm_all_locks_mutex);
2410
2411 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2412 if (signal_pending(current))
2413 goto out_unlock;
2414 if (vma->vm_file && vma->vm_file->f_mapping)
2415 vm_lock_mapping(mm, vma->vm_file->f_mapping);
2416 }
2417
2418 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2419 if (signal_pending(current))
2420 goto out_unlock;
2421 if (vma->anon_vma)
2422 vm_lock_anon_vma(mm, vma->anon_vma);
2423 }
2424
2425 ret = 0;
2426
2427 out_unlock:
2428 if (ret)
2429 mm_drop_all_locks(mm);
2430
2431 return ret;
2432 }
2433
vm_unlock_anon_vma(struct anon_vma * anon_vma)2434 static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2435 {
2436 if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
2437 /*
2438 * The LSB of head.next can't change to 0 from under
2439 * us because we hold the mm_all_locks_mutex.
2440 *
2441 * We must however clear the bitflag before unlocking
2442 * the vma so the users using the anon_vma->head will
2443 * never see our bitflag.
2444 *
2445 * No need of atomic instructions here, head.next
2446 * can't change from under us until we release the
2447 * anon_vma->lock.
2448 */
2449 if (!__test_and_clear_bit(0, (unsigned long *)
2450 &anon_vma->head.next))
2451 BUG();
2452 spin_unlock(&anon_vma->lock);
2453 }
2454 }
2455
vm_unlock_mapping(struct address_space * mapping)2456 static void vm_unlock_mapping(struct address_space *mapping)
2457 {
2458 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2459 /*
2460 * AS_MM_ALL_LOCKS can't change to 0 from under us
2461 * because we hold the mm_all_locks_mutex.
2462 */
2463 spin_unlock(&mapping->i_mmap_lock);
2464 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2465 &mapping->flags))
2466 BUG();
2467 }
2468 }
2469
2470 /*
2471 * The mmap_sem cannot be released by the caller until
2472 * mm_drop_all_locks() returns.
2473 */
mm_drop_all_locks(struct mm_struct * mm)2474 void mm_drop_all_locks(struct mm_struct *mm)
2475 {
2476 struct vm_area_struct *vma;
2477
2478 BUG_ON(down_read_trylock(&mm->mmap_sem));
2479 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2480
2481 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2482 if (vma->anon_vma)
2483 vm_unlock_anon_vma(vma->anon_vma);
2484 if (vma->vm_file && vma->vm_file->f_mapping)
2485 vm_unlock_mapping(vma->vm_file->f_mapping);
2486 }
2487
2488 mutex_unlock(&mm_all_locks_mutex);
2489 }
2490
2491 /*
2492 * initialise the VMA slab
2493 */
mmap_init(void)2494 void __init mmap_init(void)
2495 {
2496 vm_area_cachep = kmem_cache_create("vm_area_struct",
2497 sizeof(struct vm_area_struct), 0,
2498 SLAB_PANIC, NULL);
2499 }
2500