]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - mm/ksm.c
afs: Fix VNOVOL handling in address rotation
[mirror_ubuntu-bionic-kernel.git] / mm / ksm.c
index be8f4576f84211499e269f4c69f993a975a8e0a9..97f33e1a4ca5b7641e4d420088d5bac43b2c63af 100644 (file)
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1138,6 +1138,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));
@@ -2089,8 +2096,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) {
                        /*
@@ -2117,6 +2138,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);
                }
        }
 }
@@ -2346,8 +2381,12 @@ static int ksm_scan_thread(void *nothing)
                try_to_freeze();
 
                if (ksmd_should_run()) {
-                       schedule_timeout_interruptible(
-                               msecs_to_jiffies(ksm_thread_sleep_millisecs));
+                       if (ksm_thread_sleep_millisecs >= 1000)
+                               schedule_timeout_interruptible(
+                                       msecs_to_jiffies(round_jiffies_relative(ksm_thread_sleep_millisecs)));
+                       else
+                               schedule_timeout_interruptible(
+                                       msecs_to_jiffies(ksm_thread_sleep_millisecs));
                } else {
                        wait_event_freezable(ksm_thread_wait,
                                ksmd_should_run() || kthread_should_stop());