mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Xu Xin (vivo)" <xu.xin@linux.dev>
Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev,
	corbet@lwn.net, link@vivo.com, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-mm@kvack.org, ljs@kernel.org, odys.yuan@gmail.com,
	rppt@kernel.org, surenb@google.com, tujinjiang@huawei.com,
	vbabka@kernel.org, xiao.wang@vivo.com
Subject: Re: [PATCH 1/4] ksm: add only_merge_zero_pages to merge only empty anonymous pages
Date: Wed, 11 Nov 2026 10:10:07 +0100	[thread overview]
Message-ID: <d43cf7bd-6a6a-4102-95cc-cb236eb49e0d@kernel.org> (raw)
In-Reply-To: <20261110074451.220524-1-xu.xin@linux.dev>

On 11/10/26 08:44, Xu Xin (vivo) wrote:
> Add a new sysfs attribute, /sys/kernel/mm/ksm/only_merge_zero_pages,
> which, when enabled, makes ksmd merge only empty (zeroed) anonymous
> pages with the kernel zero page and skip the stable and unstable trees
> entirely for pages that carry data.
> 
> The attribute requires use_zero_pages to be 1, and the two are kept
> consistent by symmetric constraints: it cannot be enabled while
> use_zero_pages is 0 (-EINVAL), and use_zero_pages cannot be turned
> off while it is active (-EBUSY). Both store handlers take
> ksm_thread_mutex around their check-and-set 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. The new attribute can be changed at any time
> and is only effective for pages merged after the change.
> 
> Benefits in three aspects
> =========================
> - Memory saving:
>   On memory-constrained systems such as Android and embedded devices,
>   zero-filled anonymous pages are the largest source of duplicate
>   anonymous memory. They are produced by userspace allocators: the
>   zeroing of freshly allocated Java objects and buffers in the ART heap
>   (new byte[]/int[], Bitmap buffers), calloc/memset in native code, and
>   preallocated media buffers. Merging them with the kernel zero page
>   frees a significant amount of physical memory, which matters as DRAM
>   costs keep rising. With this enabled, we measured 100MB–300MB saving
>   tested with several popular applications on aarch64 (Android).
> 
> - CPU overhead reduction:
>   Merging only zero pages also reduces the overhead of ksmd. A page that
>   is not merged with the zero page carries data and can never be merged
>   with the zero page, so there is no point in searching the stable tree
>   or inserting it into the unstable tree for it. Skipping those tree
>   operations for data-bearing pages reduces both the CPU time and the
>   rhashtable memory footprint of ksmd, which makes zero page merging
>   practical on low-resource devices where full KSM scanning would be too
>   expensive.
> 
> - Security:
>   Finally, this shrinks the KSM side-channel attack surface: only zero
>   pages are ever merged, so the sharing status of data-bearing pages is
>   no longer observable through KSM timing side channels.
> 
> Note
> =====
> - Suitable for Android, embedded and low-memory devices, where zero-page
>   deduplication is wanted but full KSM scanning is too costly, and reduced
>   KSM side-channel exposure is desired. Many handset vendors may prefer
>   this knob, which can be used as a default fallback policy.
> 
> - Not suitable for server and virtualization workloads containing many
>   non-zero duplicate pages, where vanilla KSM provides better memory
>   savings.
> 
> Signed-off-by: Xu Xin (vivo) <xu.xin@linux.dev>
> Reviewed-by: Huan Yang <link@vivo.com>
> Cc: Wang Xiao <xiao.wang@vivo.com>
> ---

See my reply to #2. I think the overall feature makes sense, but reusing all of
KSM infrastructure just to deduplicate zero-filled pages looks suboptimal.

Please investigate how we could selectively reuse some pieces to just scan +
dedulicate zero-filled pages, avoiding the whole rmap item stuff etc.

-- 
Cheers,

David

  parent reply	other threads:[~2026-11-11  9:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  7:43 [PATCH v2 0/4] KSM: introduce only_merge_zero_pages Xu Xin (vivo)
2026-11-10  7:44 ` [PATCH 1/4] ksm: add only_merge_zero_pages to merge only empty anonymous pages Xu Xin (vivo)
2026-11-10  7:44   ` [PATCH 2/4] ksm: skip rmap_item allocation for non-zero pages under only_merge_zero_pages Xu Xin (vivo)
2026-11-10 10:05     ` Lorenzo Stoakes (ARM)
2026-11-10 11:22       ` Xu Xin (vivo)
2026-11-10 12:42         ` Lorenzo Stoakes (ARM)
2026-11-11  8:36           ` Xu Xin (vivo)
2026-11-11  8:48             ` Lorenzo Stoakes (ARM)
2026-11-11  9:04             ` David Hildenbrand (Arm)
2026-11-11  9:08     ` David Hildenbrand (Arm)
2026-11-11  9:36       ` Xu Xin (vivo)
2026-11-10  7:44   ` [PATCH 3/4] selftests/ksm: consolidate ksm_stat readers into ksm_get_self_stat() Xu Xin (vivo)
2026-11-10  7:44   ` [PATCH 4/4] selftests/ksm: add test for only_merge_zero_pages Xu Xin (vivo)
2026-11-11  9:10   ` David Hildenbrand (Arm) [this message]
2026-11-10 10:08 ` [PATCH v2 0/4] KSM: introduce only_merge_zero_pages Lorenzo Stoakes (ARM)
2026-11-10 11:34   ` Xu Xin (vivo)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d43cf7bd-6a6a-4102-95cc-cb236eb49e0d@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=corbet@lwn.net \
    --cc=link@vivo.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=odys.yuan@gmail.com \
    --cc=rppt@kernel.org \
    --cc=surenb@google.com \
    --cc=tujinjiang@huawei.com \
    --cc=vbabka@kernel.org \
    --cc=xiao.wang@vivo.com \
    --cc=xu.xin@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®