Commit e86c59b1 authored by Claudio Imbrenda's avatar Claudio Imbrenda Committed by Linus Torvalds

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.comSigned-off-by: default avatarClaudio 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: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8d4a0170
...@@ -80,6 +80,20 @@ run - set 0 to stop ksmd from running but keep merged pages, ...@@ -80,6 +80,20 @@ run - set 0 to stop ksmd from running but keep merged pages,
Default: 0 (must be changed to 1 to activate KSM, Default: 0 (must be changed to 1 to activate KSM,
except if CONFIG_SYSFS is disabled) except if CONFIG_SYSFS is disabled)
use_zero_pages - specifies whether empty pages (i.e. allocated pages
that only contain zeroes) should be treated specially.
When set to 1, empty pages are merged with the kernel
zero page(s) instead of with each other as it would
happen normally. This can improve the performance on
architectures with coloured zero pages, depending on
the workload. Care should be taken when enabling this
setting, as it can potentially degrade the performance
of KSM for some workloads, for example if the checksums
of pages candidate for merging match the checksum of
an empty page. This setting can be changed at any time,
it is only effective for pages merged after the change.
Default: 0 (normal KSM behaviour as in earlier releases)
The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/: The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:
pages_shared - how many shared pages are being used pages_shared - how many shared pages are being used
......
...@@ -223,6 +223,12 @@ static unsigned int ksm_thread_pages_to_scan = 100; ...@@ -223,6 +223,12 @@ static unsigned int ksm_thread_pages_to_scan = 100;
/* Milliseconds ksmd should sleep between batches */ /* Milliseconds ksmd should sleep between batches */
static unsigned int ksm_thread_sleep_millisecs = 20; static unsigned int ksm_thread_sleep_millisecs = 20;
/* Checksum of an empty (zeroed) page */
static unsigned int zero_checksum __read_mostly;
/* Whether to merge empty (zeroed) pages with actual zero pages */
static bool ksm_use_zero_pages __read_mostly;
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
/* Zeroed when merging across nodes is not allowed */ /* Zeroed when merging across nodes is not allowed */
static unsigned int ksm_merge_across_nodes = 1; static unsigned int ksm_merge_across_nodes = 1;
...@@ -926,6 +932,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, ...@@ -926,6 +932,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
struct mm_struct *mm = vma->vm_mm; struct mm_struct *mm = vma->vm_mm;
pmd_t *pmd; pmd_t *pmd;
pte_t *ptep; pte_t *ptep;
pte_t newpte;
spinlock_t *ptl; spinlock_t *ptl;
unsigned long addr; unsigned long addr;
int err = -EFAULT; int err = -EFAULT;
...@@ -950,12 +957,22 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, ...@@ -950,12 +957,22 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
goto out_mn; goto out_mn;
} }
/*
* No need to check ksm_use_zero_pages here: we can only have a
* zero_page here if ksm_use_zero_pages was enabled alreaady.
*/
if (!is_zero_pfn(page_to_pfn(kpage))) {
get_page(kpage); get_page(kpage);
page_add_anon_rmap(kpage, vma, addr, false); page_add_anon_rmap(kpage, vma, addr, false);
newpte = mk_pte(kpage, vma->vm_page_prot);
} else {
newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage),
vma->vm_page_prot));
}
flush_cache_page(vma, addr, pte_pfn(*ptep)); flush_cache_page(vma, addr, pte_pfn(*ptep));
ptep_clear_flush_notify(vma, addr, ptep); ptep_clear_flush_notify(vma, addr, ptep);
set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); set_pte_at_notify(mm, addr, ptep, newpte);
page_remove_rmap(page, false); page_remove_rmap(page, false);
if (!page_mapped(page)) if (!page_mapped(page))
...@@ -1467,6 +1484,23 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) ...@@ -1467,6 +1484,23 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
return; return;
} }
/*
* Same checksum as an empty page. We attempt to merge it with the
* appropriate zero page if the user enabled this via sysfs.
*/
if (ksm_use_zero_pages && (checksum == zero_checksum)) {
struct vm_area_struct *vma;
vma = find_mergeable_vma(rmap_item->mm, rmap_item->address);
err = try_to_merge_one_page(vma, page,
ZERO_PAGE(rmap_item->address));
/*
* In case of failure, the page was not really empty, so we
* need to continue. Otherwise we're done.
*/
if (!err)
return;
}
tree_rmap_item = tree_rmap_item =
unstable_tree_search_insert(rmap_item, page, &tree_page); unstable_tree_search_insert(rmap_item, page, &tree_page);
if (tree_rmap_item) { if (tree_rmap_item) {
...@@ -2233,6 +2267,28 @@ static ssize_t merge_across_nodes_store(struct kobject *kobj, ...@@ -2233,6 +2267,28 @@ static ssize_t merge_across_nodes_store(struct kobject *kobj,
KSM_ATTR(merge_across_nodes); KSM_ATTR(merge_across_nodes);
#endif #endif
static ssize_t use_zero_pages_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%u\n", ksm_use_zero_pages);
}
static ssize_t use_zero_pages_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
int err;
bool value;
err = kstrtobool(buf, &value);
if (err)
return -EINVAL;
ksm_use_zero_pages = value;
return count;
}
KSM_ATTR(use_zero_pages);
static ssize_t pages_shared_show(struct kobject *kobj, static ssize_t pages_shared_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf) struct kobj_attribute *attr, char *buf)
{ {
...@@ -2290,6 +2346,7 @@ static struct attribute *ksm_attrs[] = { ...@@ -2290,6 +2346,7 @@ static struct attribute *ksm_attrs[] = {
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
&merge_across_nodes_attr.attr, &merge_across_nodes_attr.attr,
#endif #endif
&use_zero_pages_attr.attr,
NULL, NULL,
}; };
...@@ -2304,6 +2361,11 @@ static int __init ksm_init(void) ...@@ -2304,6 +2361,11 @@ static int __init ksm_init(void)
struct task_struct *ksm_thread; struct task_struct *ksm_thread;
int err; int err;
/* The correct value depends on page size and endianness */
zero_checksum = calc_checksum(ZERO_PAGE(0));
/* Default to false for backwards compatibility */
ksm_use_zero_pages = false;
err = ksm_slab_init(); err = ksm_slab_init();
if (err) if (err)
goto out; goto out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment