summaryrefslogtreecommitdiffstats
path: root/mm/ksm.c
diff options
context:
space:
mode:
authorClaudio Imbrenda <imbrenda@linux.vnet.ibm.com>2017-02-24 17:55:39 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2017-02-24 20:46:53 -0500
commite86c59b1b12d0db1c97eb5bec7586a691685c6cc (patch)
treeedd4499b1fd3115b1c761cfd8b4fc527d39cf368 /mm/ksm.c
parent8d4a0170026216e315b1a52ba15357b88487fbc8 (diff)
mm/ksm: improve deduplication of zero pages with colouring
Some architectures have a set of zero pages (coloured zero pages) instead of only one zero page, in order to improve the cache performance. In those cases, the kernel samepage merger (KSM) would merge all the allocated pages that happen to be filled with zeroes to the same deduplicated page, thus losing all the advantages of coloured zero pages. This behaviour is noticeable when a process accesses large arrays of allocated pages containing zeroes. A test I conducted on s390 shows that there is a speed penalty when KSM merges such pages, compared to not merging them or using actual zero pages from the start without breaking the COW. This patch fixes this behaviour. When coloured zero pages are present, the checksum of a zero page is calculated during initialisation, and compared with the checksum of the current canditate during merging. In case of a match, the normal merging routine is used to merge the page with the correct coloured zero page, which ensures the candidate page is checked to be equal to the target zero page. A sysfs entry is also added to toggle this behaviour, since it can potentially introduce performance regressions, especially on architectures without coloured zero pages. The default value is disabled, for backwards compatibility. With this patch, the performance with KSM is the same as with non COW-broken actual zero pages, which is also the same as without KSM. [akpm@linux-foundation.org: make zero_checksum and ksm_use_zero_pages __read_mostly, per Andrea] [imbrenda@linux.vnet.ibm.com: documentation for coloured zero pages deduplication] Link: http://lkml.kernel.org/r/1484927522-1964-1-git-send-email-imbrenda@linux.vnet.ibm.com Link: http://lkml.kernel.org/r/1484850953-23941-1-git-send-email-imbrenda@linux.vnet.ibm.com Signed-off-by: Claudio Imbrenda <imbrenda@linux.vnet.ibm.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Hugh Dickins <hughd@google.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/ksm.c')
-rw-r--r--mm/ksm.c68
1 files changed, 65 insertions, 3 deletions
diff --git a/mm/ksm.c b/mm/ksm.c
index 9ae6011a41f8..28bc5ab2b979 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -223,6 +223,12 @@ static unsigned int ksm_thread_pages_to_scan = 100;
223/* Milliseconds ksmd should sleep between batches */ 223/* Milliseconds ksmd should sleep between batches */
224static unsigned int ksm_thread_sleep_millisecs = 20; 224static unsigned int ksm_thread_sleep_millisecs = 20;
225 225
226/* Checksum of an empty (zeroed) page */
227static unsigned int zero_checksum __read_mostly;
228
229/* Whether to merge empty (zeroed) pages with actual zero pages */
230static bool ksm_use_zero_pages __read_mostly;
231
226#ifdef CONFIG_NUMA 232#ifdef CONFIG_NUMA
227/* Zeroed when merging across nodes is not allowed */ 233/* Zeroed when merging across nodes is not allowed */
228static unsigned int ksm_merge_across_nodes = 1; 234static unsigned int ksm_merge_across_nodes = 1;
@@ -926,6 +932,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
926 struct mm_struct *mm = vma->vm_mm; 932 struct mm_struct *mm = vma->vm_mm;
927 pmd_t *pmd; 933 pmd_t *pmd;
928 pte_t *ptep; 934 pte_t *ptep;
935 pte_t newpte;
929 spinlock_t *ptl; 936 spinlock_t *ptl;
930 unsigned long addr; 937 unsigned long addr;
931 int err = -EFAULT; 938 int err = -EFAULT;
@@ -950,12 +957,22 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
950 goto out_mn; 957 goto out_mn;
951 } 958 }
952 959
953 get_page(kpage); 960 /*
954 page_add_anon_rmap(kpage, vma, addr, false); 961 * No need to check ksm_use_zero_pages here: we can only have a
962 * zero_page here if ksm_use_zero_pages was enabled alreaady.
963 */
964 if (!is_zero_pfn(page_to_pfn(kpage))) {
965 get_page(kpage);
966 page_add_anon_rmap(kpage, vma, addr, false);
967 newpte = mk_pte(kpage, vma->vm_page_prot);
968 } else {
969 newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage),
970 vma->vm_page_prot));
971 }
955 972
956 flush_cache_page(vma, addr, pte_pfn(*ptep)); 973 flush_cache_page(vma, addr, pte_pfn(*ptep));
957 ptep_clear_flush_notify(vma, addr, ptep); 974 ptep_clear_flush_notify(vma, addr, ptep);
958 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); 975 set_pte_at_notify(mm, addr, ptep, newpte);
959 976
960 page_remove_rmap(page, false); 977 page_remove_rmap(page, false);
961 if (!page_mapped(page)) 978 if (!page_mapped(page))
@@ -1467,6 +1484,23 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
1467 return; 1484 return;
1468 } 1485 }
1469 1486
1487 /*
1488 * Same checksum as an empty page. We attempt to merge it with the
1489 * appropriate zero page if the user enabled this via sysfs.
1490 */
1491 if (ksm_use_zero_pages && (checksum == zero_checksum)) {
1492 struct vm_area_struct *vma;
1493
1494 vma = find_mergeable_vma(rmap_item->mm, rmap_item->address);
1495 err = try_to_merge_one_page(vma, page,
1496 ZERO_PAGE(rmap_item->address));
1497 /*
1498 * In case of failure, the page was not really empty, so we
1499 * need to continue. Otherwise we're done.
1500 */
1501 if (!err)
1502 return;
1503 }
1470 tree_rmap_item = 1504 tree_rmap_item =
1471 unstable_tree_search_insert(rmap_item, page, &tree_page); 1505 unstable_tree_search_insert(rmap_item, page, &tree_page);
1472 if (tree_rmap_item) { 1506 if (tree_rmap_item) {
@@ -2233,6 +2267,28 @@ static ssize_t merge_across_nodes_store(struct kobject *kobj,
2233KSM_ATTR(merge_across_nodes); 2267KSM_ATTR(merge_across_nodes);
2234#endif 2268#endif
2235 2269
2270static ssize_t use_zero_pages_show(struct kobject *kobj,
2271 struct kobj_attribute *attr, char *buf)
2272{
2273 return sprintf(buf, "%u\n", ksm_use_zero_pages);
2274}
2275static ssize_t use_zero_pages_store(struct kobject *kobj,
2276 struct kobj_attribute *attr,
2277 const char *buf, size_t count)
2278{
2279 int err;
2280 bool value;
2281
2282 err = kstrtobool(buf, &value);
2283 if (err)
2284 return -EINVAL;
2285
2286 ksm_use_zero_pages = value;
2287
2288 return count;
2289}
2290KSM_ATTR(use_zero_pages);
2291
2236static ssize_t pages_shared_show(struct kobject *kobj, 2292static ssize_t pages_shared_show(struct kobject *kobj,
2237 struct kobj_attribute *attr, char *buf) 2293 struct kobj_attribute *attr, char *buf)
2238{ 2294{
@@ -2290,6 +2346,7 @@ static struct attribute *ksm_attrs[] = {
2290#ifdef CONFIG_NUMA 2346#ifdef CONFIG_NUMA
2291 &merge_across_nodes_attr.attr, 2347 &merge_across_nodes_attr.attr,
2292#endif 2348#endif
2349 &use_zero_pages_attr.attr,
2293 NULL, 2350 NULL,
2294}; 2351};
2295 2352
@@ -2304,6 +2361,11 @@ static int __init ksm_init(void)
2304 struct task_struct *ksm_thread; 2361 struct task_struct *ksm_thread;
2305 int err; 2362 int err;
2306 2363
2364 /* The correct value depends on page size and endianness */
2365 zero_checksum = calc_checksum(ZERO_PAGE(0));
2366 /* Default to false for backwards compatibility */
2367 ksm_use_zero_pages = false;
2368
2307 err = ksm_slab_init(); 2369 err = ksm_slab_init();
2308 if (err) 2370 if (err)
2309 goto out; 2371 goto out;