Skip to content

KSM: introduce only_merge_zero_pages

Xu Xin (vivo) linux-doc linux-kselftest linux-mm lkml 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest4 patches 11 repliesIn review
  2. v1 posting 2/21 patch 11 repliesSuperseded
  3. v1 posting 1/21 patch 0 repliesSuperseded

Changes in v2 from the cover letter

- Patch 1: Take ksm_thread_mutex around the check-and-set in both use_zero_pages_store() and only_merge_zero_pages_store(), so that a concurrent write to the two attributes cannot leave use_zero_pages=0 and only_merge_zero_pages=1, a state in which ksmd would scan and checksum pages but never merge any.

- New patch 2, 3 and 4 are added.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4ksm: add only_merge_zero_pages to merge only empty anonymous pagesDocumentation/admin-guide/mm/ksm.rst mm/ksm.c+106 -11 reply
2/4ksm: skip rmap_item allocation for non-zero pages under only_merge_zero_pagesmm/ksm.c+82 -08 replies
3/4selftests/ksm: consolidate ksm_stat readers into ksm_get_self_stat()tools/testing/selftests/mm/ksm_functional_tests.c tools/testing/selftests/mm/vm_util.c tools/testing/selftests/mm/vm_util.h+22 -390 replies
4/4selftests/ksm: add test for only_merge_zero_pagestools/testing/selftests/mm/ksm_functional_tests.c+117 -00 replies

Reviews trailers per patch

1 of 4 patches has a Reviewed-by or Acked-by.

PatchHuan Yang
1/4R-b
2/4
3/4
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/4] KSM: introduce only_merge_zero_pages

Hi all,

This series adds a new KSM sysfs attribute, only_merge_zero_pages, which makes ksmd merge only empty (zeroed) anonymous pages with the kernel zero page and skip the stable/unstable trees for pages that carry data.

Patch 1 adds the attribute and the early return in cmp_and_merge_page().

Patch 2 (co-developed with Jinjiang Tu) goes one step further and skips non-zero pages already in the scan path, before an rmap_item is allocated for them, which is what makes the mode practical on large-memory workloads with mostly non-mergeable anonymous memory.

Patch 3 consolidates the ksm_stat readers in vm_util.c into a single ksm_get_self_stat() helper (pure refactor). Patch 4 adds a functional selftest.

Changelog
=========
v1 -> v2

- Patch 1: Take ksm_thread_mutex around the check-and-set in both use_zero_pages_store() and only_merge_zero_pages_store(), so that a concurrent write to the two attributes cannot leave use_zero_pages=0 and only_merge_zero_pages=1, a state in which ksmd would scan and checksum pages but never merge any.

- New patch 2, 3 and 4 are added.

Motivation
==========

On an Android device, I measured the KSM savings potential on 15.7 GiB RAM, ~1100 processes, a typical usage session with several video apps running) using a userspace survey tool that walks /proc/<pid>/maps + /proc/<pid>/pagemap, deduplicates anonymous pages by their physical frame number (PFN), and hashes page contents to find identical pages. Key results (two samples taken 10 minutes apart):

  Category              Resident    Savings (upper bound)
  -------------------   ---------   ---------------------
  anon, non-zero pages  2.9G        67-74 MiB (2.3-2.4%)
  anon, zero pages      -           281-424 MiB (9.5-13.9%)

In other scenario with a MySQL instance from huawei, there are around 15 GB consumed by hashtables, which contain many zero-filled pages that are eligible for merging to reduce memory footprint [1].

[1] https://lore.kernel.org/all/[email protected]/

Xu Xin (vivo) (4):
ksm: add only_merge_zero_pages to merge only empty anonymous pages
ksm: skip rmap_item allocation for non-zero pages under
only_merge_zero_pages
selftests/ksm: consolidate ksm_stat readers into ksm_get_self_stat() selftests/ksm: add test for only_merge_zero_pages

 Documentation/admin-guide/mm/ksm.rst          |  40 ++++-
 mm/ksm.c                                      | 149 ++++++++++++++++++
 .../selftests/mm/ksm_functional_tests.c       | 135 ++++++++++++++--
 tools/testing/selftests/mm/vm_util.c          |  40 ++---
 tools/testing/selftests/mm/vm_util.h          |   3 +-
 5 files changed, 327 insertions(+), 40 deletions(-)
Signature · 1 line
2.34.1