aboutsummaryrefslogtreecommitdiffstats
path: root/mm/mmap.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-12-11 21:05:37 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2012-12-11 21:05:37 -0500
commit608ff1a210ab0e8b969399039bf8e18693605910 (patch)
treefaea7bb1764461c73d0953089bd5439d91733a03 /mm/mmap.c
parent414a6750e59b0b687034764c464e9ddecac0f7a6 (diff)
parent74d42d8fe146e870c52bde3b1c692f86cc8ff844 (diff)
Merge branch 'akpm' (Andrew's patchbomb)
Merge misc updates from Andrew Morton: "About half of most of MM. Going very early this time due to uncertainty over the coreautounifiednumasched things. I'll send the other half of most of MM tomorrow. The rest of MM awaits a slab merge from Pekka." * emailed patches from Andrew Morton: (71 commits) memory_hotplug: ensure every online node has NORMAL memory memory_hotplug: handle empty zone when online_movable/online_kernel mm, memory-hotplug: dynamic configure movable memory and portion memory drivers/base/node.c: cleanup node_state_attr[] bootmem: fix wrong call parameter for free_bootmem() avr32, kconfig: remove HAVE_ARCH_BOOTMEM mm: cma: remove watermark hacks mm: cma: skip watermarks check for already isolated blocks in split_free_page() mm, oom: fix race when specifying a thread as the oom origin mm, oom: change type of oom_score_adj to short mm: cleanup register_node() mm, mempolicy: remove duplicate code mm/vmscan.c: try_to_freeze() returns boolean mm: introduce putback_movable_pages() virtio_balloon: introduce migration primitives to balloon pages mm: introduce compaction and migration for ballooned pages mm: introduce a common interface for balloon pages mobility mm: redefine address_space.assoc_mapping mm: adjust address_space_operations.migratepage() return code arch/sparc/kernel/sys_sparc_64.c: s/COLOUR/COLOR/ ...
Diffstat (limited to 'mm/mmap.c')
-rw-r--r--mm/mmap.c513
1 files changed, 396 insertions, 117 deletions
diff --git a/mm/mmap.c b/mm/mmap.c
index 7d416055f08c..f940062c8d4b 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -31,6 +31,7 @@
31#include <linux/audit.h> 31#include <linux/audit.h>
32#include <linux/khugepaged.h> 32#include <linux/khugepaged.h>
33#include <linux/uprobes.h> 33#include <linux/uprobes.h>
34#include <linux/rbtree_augmented.h>
34 35
35#include <asm/uaccess.h> 36#include <asm/uaccess.h>
36#include <asm/cacheflush.h> 37#include <asm/cacheflush.h>
@@ -311,40 +312,88 @@ out:
311 return retval; 312 return retval;
312} 313}
313 314
315static long vma_compute_subtree_gap(struct vm_area_struct *vma)
316{
317 unsigned long max, subtree_gap;
318 max = vma->vm_start;
319 if (vma->vm_prev)
320 max -= vma->vm_prev->vm_end;
321 if (vma->vm_rb.rb_left) {
322 subtree_gap = rb_entry(vma->vm_rb.rb_left,
323 struct vm_area_struct, vm_rb)->rb_subtree_gap;
324 if (subtree_gap > max)
325 max = subtree_gap;
326 }
327 if (vma->vm_rb.rb_right) {
328 subtree_gap = rb_entry(vma->vm_rb.rb_right,
329 struct vm_area_struct, vm_rb)->rb_subtree_gap;
330 if (subtree_gap > max)
331 max = subtree_gap;
332 }
333 return max;
334}
335
314#ifdef CONFIG_DEBUG_VM_RB 336#ifdef CONFIG_DEBUG_VM_RB
315static int browse_rb(struct rb_root *root) 337static int browse_rb(struct rb_root *root)
316{ 338{
317 int i = 0, j; 339 int i = 0, j, bug = 0;
318 struct rb_node *nd, *pn = NULL; 340 struct rb_node *nd, *pn = NULL;
319 unsigned long prev = 0, pend = 0; 341 unsigned long prev = 0, pend = 0;
320 342
321 for (nd = rb_first(root); nd; nd = rb_next(nd)) { 343 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
322 struct vm_area_struct *vma; 344 struct vm_area_struct *vma;
323 vma = rb_entry(nd, struct vm_area_struct, vm_rb); 345 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
324 if (vma->vm_start < prev) 346 if (vma->vm_start < prev) {
325 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1; 347 printk("vm_start %lx prev %lx\n", vma->vm_start, prev);
326 if (vma->vm_start < pend) 348 bug = 1;
349 }
350 if (vma->vm_start < pend) {
327 printk("vm_start %lx pend %lx\n", vma->vm_start, pend); 351 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
328 if (vma->vm_start > vma->vm_end) 352 bug = 1;
329 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start); 353 }
354 if (vma->vm_start > vma->vm_end) {
355 printk("vm_end %lx < vm_start %lx\n",
356 vma->vm_end, vma->vm_start);
357 bug = 1;
358 }
359 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
360 printk("free gap %lx, correct %lx\n",
361 vma->rb_subtree_gap,
362 vma_compute_subtree_gap(vma));
363 bug = 1;
364 }
330 i++; 365 i++;
331 pn = nd; 366 pn = nd;
332 prev = vma->vm_start; 367 prev = vma->vm_start;
333 pend = vma->vm_end; 368 pend = vma->vm_end;
334 } 369 }
335 j = 0; 370 j = 0;
336 for (nd = pn; nd; nd = rb_prev(nd)) { 371 for (nd = pn; nd; nd = rb_prev(nd))
337 j++; 372 j++;
373 if (i != j) {
374 printk("backwards %d, forwards %d\n", j, i);
375 bug = 1;
376 }
377 return bug ? -1 : i;
378}
379
380static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
381{
382 struct rb_node *nd;
383
384 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
385 struct vm_area_struct *vma;
386 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
387 BUG_ON(vma != ignore &&
388 vma->rb_subtree_gap != vma_compute_subtree_gap(vma));
338 } 389 }
339 if (i != j)
340 printk("backwards %d, forwards %d\n", j, i), i = 0;
341 return i;
342} 390}
343 391
344void validate_mm(struct mm_struct *mm) 392void validate_mm(struct mm_struct *mm)
345{ 393{
346 int bug = 0; 394 int bug = 0;
347 int i = 0; 395 int i = 0;
396 unsigned long highest_address = 0;
348 struct vm_area_struct *vma = mm->mmap; 397 struct vm_area_struct *vma = mm->mmap;
349 while (vma) { 398 while (vma) {
350 struct anon_vma_chain *avc; 399 struct anon_vma_chain *avc;
@@ -352,20 +401,73 @@ void validate_mm(struct mm_struct *mm)
352 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 401 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
353 anon_vma_interval_tree_verify(avc); 402 anon_vma_interval_tree_verify(avc);
354 vma_unlock_anon_vma(vma); 403 vma_unlock_anon_vma(vma);
404 highest_address = vma->vm_end;
355 vma = vma->vm_next; 405 vma = vma->vm_next;
356 i++; 406 i++;
357 } 407 }
358 if (i != mm->map_count) 408 if (i != mm->map_count) {
359 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1; 409 printk("map_count %d vm_next %d\n", mm->map_count, i);
410 bug = 1;
411 }
412 if (highest_address != mm->highest_vm_end) {
413 printk("mm->highest_vm_end %lx, found %lx\n",
414 mm->highest_vm_end, highest_address);
415 bug = 1;
416 }
360 i = browse_rb(&mm->mm_rb); 417 i = browse_rb(&mm->mm_rb);
361 if (i != mm->map_count) 418 if (i != mm->map_count) {
362 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1; 419 printk("map_count %d rb %d\n", mm->map_count, i);
420 bug = 1;
421 }
363 BUG_ON(bug); 422 BUG_ON(bug);
364} 423}
365#else 424#else
425#define validate_mm_rb(root, ignore) do { } while (0)
366#define validate_mm(mm) do { } while (0) 426#define validate_mm(mm) do { } while (0)
367#endif 427#endif
368 428
429RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
430 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
431
432/*
433 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
434 * vma->vm_prev->vm_end values changed, without modifying the vma's position
435 * in the rbtree.
436 */
437static void vma_gap_update(struct vm_area_struct *vma)
438{
439 /*
440 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
441 * function that does exacltly what we want.
442 */
443 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
444}
445
446static inline void vma_rb_insert(struct vm_area_struct *vma,
447 struct rb_root *root)
448{
449 /* All rb_subtree_gap values must be consistent prior to insertion */
450 validate_mm_rb(root, NULL);
451
452 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
453}
454
455static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
456{
457 /*
458 * All rb_subtree_gap values must be consistent prior to erase,
459 * with the possible exception of the vma being erased.
460 */
461 validate_mm_rb(root, vma);
462
463 /*
464 * Note rb_erase_augmented is a fairly large inline function,
465 * so make sure we instantiate it only once with our desired
466 * augmented rbtree callbacks.
467 */
468 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
469}
470
369/* 471/*
370 * vma has some anon_vma assigned, and is already inserted on that 472 * vma has some anon_vma assigned, and is already inserted on that
371 * anon_vma's interval trees. 473 * anon_vma's interval trees.
@@ -435,8 +537,25 @@ static int find_vma_links(struct mm_struct *mm, unsigned long addr,
435void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma, 537void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
436 struct rb_node **rb_link, struct rb_node *rb_parent) 538 struct rb_node **rb_link, struct rb_node *rb_parent)
437{ 539{
540 /* Update tracking information for the gap following the new vma. */
541 if (vma->vm_next)
542 vma_gap_update(vma->vm_next);
543 else
544 mm->highest_vm_end = vma->vm_end;
545
546 /*
547 * vma->vm_prev wasn't known when we followed the rbtree to find the
548 * correct insertion point for that vma. As a result, we could not
549 * update the vma vm_rb parents rb_subtree_gap values on the way down.
550 * So, we first insert the vma with a zero rb_subtree_gap value
551 * (to be consistent with what we did on the way down), and then
552 * immediately update the gap to the correct value. Finally we
553 * rebalance the rbtree after all augmented values have been set.
554 */
438 rb_link_node(&vma->vm_rb, rb_parent, rb_link); 555 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
439 rb_insert_color(&vma->vm_rb, &mm->mm_rb); 556 vma->rb_subtree_gap = 0;
557 vma_gap_update(vma);
558 vma_rb_insert(vma, &mm->mm_rb);
440} 559}
441 560
442static void __vma_link_file(struct vm_area_struct *vma) 561static void __vma_link_file(struct vm_area_struct *vma)
@@ -512,12 +631,12 @@ static inline void
512__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma, 631__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
513 struct vm_area_struct *prev) 632 struct vm_area_struct *prev)
514{ 633{
515 struct vm_area_struct *next = vma->vm_next; 634 struct vm_area_struct *next;
516 635
517 prev->vm_next = next; 636 vma_rb_erase(vma, &mm->mm_rb);
637 prev->vm_next = next = vma->vm_next;
518 if (next) 638 if (next)
519 next->vm_prev = prev; 639 next->vm_prev = prev;
520 rb_erase(&vma->vm_rb, &mm->mm_rb);
521 if (mm->mmap_cache == vma) 640 if (mm->mmap_cache == vma)
522 mm->mmap_cache = prev; 641 mm->mmap_cache = prev;
523} 642}
@@ -539,6 +658,7 @@ int vma_adjust(struct vm_area_struct *vma, unsigned long start,
539 struct rb_root *root = NULL; 658 struct rb_root *root = NULL;
540 struct anon_vma *anon_vma = NULL; 659 struct anon_vma *anon_vma = NULL;
541 struct file *file = vma->vm_file; 660 struct file *file = vma->vm_file;
661 bool start_changed = false, end_changed = false;
542 long adjust_next = 0; 662 long adjust_next = 0;
543 int remove_next = 0; 663 int remove_next = 0;
544 664
@@ -629,8 +749,14 @@ again: remove_next = 1 + (end > next->vm_end);
629 vma_interval_tree_remove(next, root); 749 vma_interval_tree_remove(next, root);
630 } 750 }
631 751
632 vma->vm_start = start; 752 if (start != vma->vm_start) {
633 vma->vm_end = end; 753 vma->vm_start = start;
754 start_changed = true;
755 }
756 if (end != vma->vm_end) {
757 vma->vm_end = end;
758 end_changed = true;
759 }
634 vma->vm_pgoff = pgoff; 760 vma->vm_pgoff = pgoff;
635 if (adjust_next) { 761 if (adjust_next) {
636 next->vm_start += adjust_next << PAGE_SHIFT; 762 next->vm_start += adjust_next << PAGE_SHIFT;
@@ -659,6 +785,15 @@ again: remove_next = 1 + (end > next->vm_end);
659 * (it may either follow vma or precede it). 785 * (it may either follow vma or precede it).
660 */ 786 */
661 __insert_vm_struct(mm, insert); 787 __insert_vm_struct(mm, insert);
788 } else {
789 if (start_changed)
790 vma_gap_update(vma);
791 if (end_changed) {
792 if (!next)
793 mm->highest_vm_end = end;
794 else if (!adjust_next)
795 vma_gap_update(next);
796 }
662 } 797 }
663 798
664 if (anon_vma) { 799 if (anon_vma) {
@@ -692,10 +827,13 @@ again: remove_next = 1 + (end > next->vm_end);
692 * we must remove another next too. It would clutter 827 * we must remove another next too. It would clutter
693 * up the code too much to do both in one go. 828 * up the code too much to do both in one go.
694 */ 829 */
695 if (remove_next == 2) { 830 next = vma->vm_next;
696 next = vma->vm_next; 831 if (remove_next == 2)
697 goto again; 832 goto again;
698 } 833 else if (next)
834 vma_gap_update(next);
835 else
836 mm->highest_vm_end = end;
699 } 837 }
700 if (insert && file) 838 if (insert && file)
701 uprobe_mmap(insert); 839 uprobe_mmap(insert);
@@ -1167,8 +1305,9 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1167 * memory so no accounting is necessary 1305 * memory so no accounting is necessary
1168 */ 1306 */
1169 file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len, 1307 file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
1170 VM_NORESERVE, &user, 1308 VM_NORESERVE,
1171 HUGETLB_ANONHUGE_INODE); 1309 &user, HUGETLB_ANONHUGE_INODE,
1310 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
1172 if (IS_ERR(file)) 1311 if (IS_ERR(file))
1173 return PTR_ERR(file); 1312 return PTR_ERR(file);
1174 } 1313 }
@@ -1414,6 +1553,206 @@ unacct_error:
1414 return error; 1553 return error;
1415} 1554}
1416 1555
1556unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1557{
1558 /*
1559 * We implement the search by looking for an rbtree node that
1560 * immediately follows a suitable gap. That is,
1561 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1562 * - gap_end = vma->vm_start >= info->low_limit + length;
1563 * - gap_end - gap_start >= length
1564 */
1565
1566 struct mm_struct *mm = current->mm;
1567 struct vm_area_struct *vma;
1568 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1569
1570 /* Adjust search length to account for worst case alignment overhead */
1571 length = info->length + info->align_mask;
1572 if (length < info->length)
1573 return -ENOMEM;
1574
1575 /* Adjust search limits by the desired length */
1576 if (info->high_limit < length)
1577 return -ENOMEM;
1578 high_limit = info->high_limit - length;
1579
1580 if (info->low_limit > high_limit)
1581 return -ENOMEM;
1582 low_limit = info->low_limit + length;
1583
1584 /* Check if rbtree root looks promising */
1585 if (RB_EMPTY_ROOT(&mm->mm_rb))
1586 goto check_highest;
1587 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1588 if (vma->rb_subtree_gap < length)
1589 goto check_highest;
1590
1591 while (true) {
1592 /* Visit left subtree if it looks promising */
1593 gap_end = vma->vm_start;
1594 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1595 struct vm_area_struct *left =
1596 rb_entry(vma->vm_rb.rb_left,
1597 struct vm_area_struct, vm_rb);
1598 if (left->rb_subtree_gap >= length) {
1599 vma = left;
1600 continue;
1601 }
1602 }
1603
1604 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1605check_current:
1606 /* Check if current node has a suitable gap */
1607 if (gap_start > high_limit)
1608 return -ENOMEM;
1609 if (gap_end >= low_limit && gap_end - gap_start >= length)
1610 goto found;
1611
1612 /* Visit right subtree if it looks promising */
1613 if (vma->vm_rb.rb_right) {
1614 struct vm_area_struct *right =
1615 rb_entry(vma->vm_rb.rb_right,
1616 struct vm_area_struct, vm_rb);
1617 if (right->rb_subtree_gap >= length) {
1618 vma = right;
1619 continue;
1620 }
1621 }
1622
1623 /* Go back up the rbtree to find next candidate node */
1624 while (true) {
1625 struct rb_node *prev = &vma->vm_rb;
1626 if (!rb_parent(prev))
1627 goto check_highest;
1628 vma = rb_entry(rb_parent(prev),
1629 struct vm_area_struct, vm_rb);
1630 if (prev == vma->vm_rb.rb_left) {
1631 gap_start = vma->vm_prev->vm_end;
1632 gap_end = vma->vm_start;
1633 goto check_current;
1634 }
1635 }
1636 }
1637
1638check_highest:
1639 /* Check highest gap, which does not precede any rbtree node */
1640 gap_start = mm->highest_vm_end;
1641 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1642 if (gap_start > high_limit)
1643 return -ENOMEM;
1644
1645found:
1646 /* We found a suitable gap. Clip it with the original low_limit. */
1647 if (gap_start < info->low_limit)
1648 gap_start = info->low_limit;
1649
1650 /* Adjust gap address to the desired alignment */
1651 gap_start += (info->align_offset - gap_start) & info->align_mask;
1652
1653 VM_BUG_ON(gap_start + info->length > info->high_limit);
1654 VM_BUG_ON(gap_start + info->length > gap_end);
1655 return gap_start;
1656}
1657
1658unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1659{
1660 struct mm_struct *mm = current->mm;
1661 struct vm_area_struct *vma;
1662 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1663
1664 /* Adjust search length to account for worst case alignment overhead */
1665 length = info->length + info->align_mask;
1666 if (length < info->length)
1667 return -ENOMEM;
1668
1669 /*
1670 * Adjust search limits by the desired length.
1671 * See implementation comment at top of unmapped_area().
1672 */
1673 gap_end = info->high_limit;
1674 if (gap_end < length)
1675 return -ENOMEM;
1676 high_limit = gap_end - length;
1677
1678 if (info->low_limit > high_limit)
1679 return -ENOMEM;
1680 low_limit = info->low_limit + length;
1681
1682 /* Check highest gap, which does not precede any rbtree node */
1683 gap_start = mm->highest_vm_end;
1684 if (gap_start <= high_limit)
1685 goto found_highest;
1686
1687 /* Check if rbtree root looks promising */
1688 if (RB_EMPTY_ROOT(&mm->mm_rb))
1689 return -ENOMEM;
1690 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1691 if (vma->rb_subtree_gap < length)
1692 return -ENOMEM;
1693
1694 while (true) {
1695 /* Visit right subtree if it looks promising */
1696 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1697 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1698 struct vm_area_struct *right =
1699 rb_entry(vma->vm_rb.rb_right,
1700 struct vm_area_struct, vm_rb);
1701 if (right->rb_subtree_gap >= length) {
1702 vma = right;
1703 continue;
1704 }
1705 }
1706
1707check_current:
1708 /* Check if current node has a suitable gap */
1709 gap_end = vma->vm_start;
1710 if (gap_end < low_limit)
1711 return -ENOMEM;
1712 if (gap_start <= high_limit && gap_end - gap_start >= length)
1713 goto found;
1714
1715 /* Visit left subtree if it looks promising */
1716 if (vma->vm_rb.rb_left) {
1717 struct vm_area_struct *left =
1718 rb_entry(vma->vm_rb.rb_left,
1719 struct vm_area_struct, vm_rb);
1720 if (left->rb_subtree_gap >= length) {
1721 vma = left;
1722 continue;
1723 }
1724 }
1725
1726 /* Go back up the rbtree to find next candidate node */
1727 while (true) {
1728 struct rb_node *prev = &vma->vm_rb;
1729 if (!rb_parent(prev))
1730 return -ENOMEM;
1731 vma = rb_entry(rb_parent(prev),
1732 struct vm_area_struct, vm_rb);
1733 if (prev == vma->vm_rb.rb_right) {
1734 gap_start = vma->vm_prev ?
1735 vma->vm_prev->vm_end : 0;
1736 goto check_current;
1737 }
1738 }
1739 }
1740
1741found:
1742 /* We found a suitable gap. Clip it with the original high_limit. */
1743 if (gap_end > info->high_limit)
1744 gap_end = info->high_limit;
1745
1746found_highest:
1747 /* Compute highest gap address at the desired alignment */
1748 gap_end -= info->length;
1749 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1750
1751 VM_BUG_ON(gap_end < info->low_limit);
1752 VM_BUG_ON(gap_end < gap_start);
1753 return gap_end;
1754}
1755
1417/* Get an address range which is currently unmapped. 1756/* Get an address range which is currently unmapped.
1418 * For shmat() with addr=0. 1757 * For shmat() with addr=0.
1419 * 1758 *
@@ -1432,7 +1771,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
1432{ 1771{
1433 struct mm_struct *mm = current->mm; 1772 struct mm_struct *mm = current->mm;
1434 struct vm_area_struct *vma; 1773 struct vm_area_struct *vma;
1435 unsigned long start_addr; 1774 struct vm_unmapped_area_info info;
1436 1775
1437 if (len > TASK_SIZE) 1776 if (len > TASK_SIZE)
1438 return -ENOMEM; 1777 return -ENOMEM;
@@ -1447,40 +1786,13 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
1447 (!vma || addr + len <= vma->vm_start)) 1786 (!vma || addr + len <= vma->vm_start))
1448 return addr; 1787 return addr;
1449 } 1788 }
1450 if (len > mm->cached_hole_size) {
1451 start_addr = addr = mm->free_area_cache;
1452 } else {
1453 start_addr = addr = TASK_UNMAPPED_BASE;
1454 mm->cached_hole_size = 0;
1455 }
1456 1789
1457full_search: 1790 info.flags = 0;
1458 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { 1791 info.length = len;
1459 /* At this point: (!vma || addr < vma->vm_end). */ 1792 info.low_limit = TASK_UNMAPPED_BASE;
1460 if (TASK_SIZE - len < addr) { 1793 info.high_limit = TASK_SIZE;
1461 /* 1794 info.align_mask = 0;
1462 * Start a new search - just in case we missed 1795 return vm_unmapped_area(&info);
1463 * some holes.
1464 */
1465 if (start_addr != TASK_UNMAPPED_BASE) {
1466 addr = TASK_UNMAPPED_BASE;
1467 start_addr = addr;
1468 mm->cached_hole_size = 0;
1469 goto full_search;
1470 }
1471 return -ENOMEM;
1472 }
1473 if (!vma || addr + len <= vma->vm_start) {
1474 /*
1475 * Remember the place where we stopped the search:
1476 */
1477 mm->free_area_cache = addr + len;
1478 return addr;
1479 }
1480 if (addr + mm->cached_hole_size < vma->vm_start)
1481 mm->cached_hole_size = vma->vm_start - addr;
1482 addr = vma->vm_end;
1483 }
1484} 1796}
1485#endif 1797#endif
1486 1798
@@ -1505,7 +1817,8 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1505{ 1817{
1506 struct vm_area_struct *vma; 1818 struct vm_area_struct *vma;
1507 struct mm_struct *mm = current->mm; 1819 struct mm_struct *mm = current->mm;
1508 unsigned long addr = addr0, start_addr; 1820 unsigned long addr = addr0;
1821 struct vm_unmapped_area_info info;
1509 1822
1510 /* requested length too big for entire address space */ 1823 /* requested length too big for entire address space */
1511 if (len > TASK_SIZE) 1824 if (len > TASK_SIZE)
@@ -1523,53 +1836,12 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1523 return addr; 1836 return addr;
1524 } 1837 }
1525 1838
1526 /* check if free_area_cache is useful for us */ 1839 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1527 if (len <= mm->cached_hole_size) { 1840 info.length = len;
1528 mm->cached_hole_size = 0; 1841 info.low_limit = PAGE_SIZE;
1529 mm->free_area_cache = mm->mmap_base; 1842 info.high_limit = mm->mmap_base;
1530 } 1843 info.align_mask = 0;
1531 1844 addr = vm_unmapped_area(&info);
1532try_again:
1533 /* either no address requested or can't fit in requested address hole */
1534 start_addr = addr = mm->free_area_cache;
1535
1536 if (addr < len)
1537 goto fail;
1538
1539 addr -= len;
1540 do {
1541 /*
1542 * Lookup failure means no vma is above this address,
1543 * else if new region fits below vma->vm_start,
1544 * return with success:
1545 */
1546 vma = find_vma(mm, addr);
1547 if (!vma || addr+len <= vma->vm_start)
1548 /* remember the address as a hint for next time */
1549 return (mm->free_area_cache = addr);
1550
1551 /* remember the largest hole we saw so far */
1552 if (addr + mm->cached_hole_size < vma->vm_start)
1553 mm->cached_hole_size = vma->vm_start - addr;
1554
1555 /* try just below the current vma->vm_start */
1556 addr = vma->vm_start-len;
1557 } while (len < vma->vm_start);
1558
1559fail:
1560 /*
1561 * if hint left us with no space for the requested
1562 * mapping then try again:
1563 *
1564 * Note: this is different with the case of bottomup
1565 * which does the fully line-search, but we use find_vma
1566 * here that causes some holes skipped.
1567 */
1568 if (start_addr != mm->mmap_base) {
1569 mm->free_area_cache = mm->mmap_base;
1570 mm->cached_hole_size = 0;
1571 goto try_again;
1572 }
1573 1845
1574 /* 1846 /*
1575 * A failed mmap() very likely causes application failure, 1847 * A failed mmap() very likely causes application failure,
@@ -1577,14 +1849,13 @@ fail:
1577 * can happen with large stack limits and large mmap() 1849 * can happen with large stack limits and large mmap()
1578 * allocations. 1850 * allocations.
1579 */ 1851 */
1580 mm->cached_hole_size = ~0UL; 1852 if (addr & ~PAGE_MASK) {
1581 mm->free_area_cache = TASK_UNMAPPED_BASE; 1853 VM_BUG_ON(addr != -ENOMEM);
1582 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); 1854 info.flags = 0;
1583 /* 1855 info.low_limit = TASK_UNMAPPED_BASE;
1584 * Restore the topdown base: 1856 info.high_limit = TASK_SIZE;
1585 */ 1857 addr = vm_unmapped_area(&info);
1586 mm->free_area_cache = mm->mmap_base; 1858 }
1587 mm->cached_hole_size = ~0UL;
1588 1859
1589 return addr; 1860 return addr;
1590} 1861}
@@ -1797,6 +2068,10 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
1797 anon_vma_interval_tree_pre_update_vma(vma); 2068 anon_vma_interval_tree_pre_update_vma(vma);
1798 vma->vm_end = address; 2069 vma->vm_end = address;
1799 anon_vma_interval_tree_post_update_vma(vma); 2070 anon_vma_interval_tree_post_update_vma(vma);
2071 if (vma->vm_next)
2072 vma_gap_update(vma->vm_next);
2073 else
2074 vma->vm_mm->highest_vm_end = address;
1800 perf_event_mmap(vma); 2075 perf_event_mmap(vma);
1801 } 2076 }
1802 } 2077 }
@@ -1851,6 +2126,7 @@ int expand_downwards(struct vm_area_struct *vma,
1851 vma->vm_start = address; 2126 vma->vm_start = address;
1852 vma->vm_pgoff -= grow; 2127 vma->vm_pgoff -= grow;
1853 anon_vma_interval_tree_post_update_vma(vma); 2128 anon_vma_interval_tree_post_update_vma(vma);
2129 vma_gap_update(vma);
1854 perf_event_mmap(vma); 2130 perf_event_mmap(vma);
1855 } 2131 }
1856 } 2132 }
@@ -1973,14 +2249,17 @@ detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1973 insertion_point = (prev ? &prev->vm_next : &mm->mmap); 2249 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1974 vma->vm_prev = NULL; 2250 vma->vm_prev = NULL;
1975 do { 2251 do {
1976 rb_erase(&vma->vm_rb, &mm->mm_rb); 2252 vma_rb_erase(vma, &mm->mm_rb);
1977 mm->map_count--; 2253 mm->map_count--;
1978 tail_vma = vma; 2254 tail_vma = vma;
1979 vma = vma->vm_next; 2255 vma = vma->vm_next;
1980 } while (vma && vma->vm_start < end); 2256 } while (vma && vma->vm_start < end);
1981 *insertion_point = vma; 2257 *insertion_point = vma;
1982 if (vma) 2258 if (vma) {
1983 vma->vm_prev = prev; 2259 vma->vm_prev = prev;
2260 vma_gap_update(vma);
2261 } else
2262 mm->highest_vm_end = prev ? prev->vm_end : 0;
1984 tail_vma->vm_next = NULL; 2263 tail_vma->vm_next = NULL;
1985 if (mm->unmap_area == arch_unmap_area) 2264 if (mm->unmap_area == arch_unmap_area)
1986 addr = prev ? prev->vm_end : mm->mmap_base; 2265 addr = prev ? prev->vm_end : mm->mmap_base;