Merge branch 'afs-dh' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / mm / ksm.c
index adb5f991da8e95461d116cfa731b04060c552a7b..e3cbf9a92f3cdd9519f7724f152d816cd22f3a44 100644 (file)
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1131,6 +1131,13 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
        } else {
                newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage),
                                               vma->vm_page_prot));
+               /*
+                * We're replacing an anonymous page with a zero page, which is
+                * not anonymous. We need to do proper accounting otherwise we
+                * will get wrong values in /proc, and a BUG message in dmesg
+                * when tearing down the mm.
+                */
+               dec_mm_counter(mm, MM_ANONPAGES);
        }
 
        flush_cache_page(vma, addr, pte_pfn(*ptep));
@@ -1318,10 +1325,10 @@ bool is_page_sharing_candidate(struct stable_node *stable_node)
        return __is_page_sharing_candidate(stable_node, 0);
 }
 
-struct page *stable_node_dup(struct stable_node **_stable_node_dup,
-                            struct stable_node **_stable_node,
-                            struct rb_root *root,
-                            bool prune_stale_stable_nodes)
+static struct page *stable_node_dup(struct stable_node **_stable_node_dup,
+                                   struct stable_node **_stable_node,
+                                   struct rb_root *root,
+                                   bool prune_stale_stable_nodes)
 {
        struct stable_node *dup, *found = NULL, *stable_node = *_stable_node;
        struct hlist_node *hlist_safe;
@@ -2082,8 +2089,22 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
        tree_rmap_item =
                unstable_tree_search_insert(rmap_item, page, &tree_page);
        if (tree_rmap_item) {
+               bool split;
+
                kpage = try_to_merge_two_pages(rmap_item, page,
                                                tree_rmap_item, tree_page);
+               /*
+                * If both pages we tried to merge belong to the same compound
+                * page, then we actually ended up increasing the reference
+                * count of the same compound page twice, and split_huge_page
+                * failed.
+                * Here we set a flag if that happened, and we use it later to
+                * try split_huge_page again. Since we call put_page right
+                * afterwards, the reference count will be correct and
+                * split_huge_page should succeed.
+                */
+               split = PageTransCompound(page)
+                       && compound_head(page) == compound_head(tree_page);
                put_page(tree_page);
                if (kpage) {
                        /*
@@ -2110,6 +2131,20 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
                                break_cow(tree_rmap_item);
                                break_cow(rmap_item);
                        }
+               } else if (split) {
+                       /*
+                        * We are here if we tried to merge two pages and
+                        * failed because they both belonged to the same
+                        * compound page. We will split the page now, but no
+                        * merging will take place.
+                        * We do not want to add the cost of a full lock; if
+                        * the page is locked, it is better to skip it and
+                        * perhaps try again later.
+                        */
+                       if (!trylock_page(page))
+                               return;
+                       split_huge_page(page);
+                       unlock_page(page);
                }
        }
 }