SKSM: Synchronous Kernel Samepage Merging
Subsystems (7)
cover SKSM: Synchronous Kernel Samepage Merging
This series introduces SKSM, a new page deduplication ABI, aiming to fix the limitations inherent to the KSM ABI.
The implementation is simple enough: SKSM is implemented in about 100 LOC compared to 2.5k LOC for KSM (on top of the common KSM helpers).
This is sent as a proof of concept. It applies on top of v6.13.
Feedback is welcome!
Mathieu
Mathieu Desnoyers (2):
mm: Introduce SKSM: Synchronous Kernel Samepage Merging selftests/kskm: Introduce SKSM basic test
include/linux/ksm.h | 4 + include/linux/mm_types.h | 7 + include/linux/page-flags.h | 42 ++++ include/linux/sksm.h | 27 +++ include/uapi/asm-generic/mman-common.h | 2 + mm/Kconfig | 5 + mm/Makefile | 1 + mm/ksm-common.h | 228 ++++++++++++++++++++++ mm/ksm.c | 219 +-------------------- mm/madvise.c | 6 + mm/memory.c | 2 + mm/page_alloc.c | 3 + mm/sksm.c | 190 ++++++++++++++++++ tools/testing/selftests/sksm/.gitignore | 2 + tools/testing/selftests/sksm/Makefile | 14 ++ tools/testing/selftests/sksm/basic_test.c | 217 ++++++++++++++++++++ 16 files changed, 751 insertions(+), 218 deletions(-) create mode 100644 include/linux/sksm.h create mode 100644 mm/ksm-common.h create mode 100644 mm/sksm.c create mode 100644 tools/testing/selftests/sksm/.gitignore create mode 100644 tools/testing/selftests/sksm/Makefile create mode 100644 tools/testing/selftests/sksm/basic_test.c
Signature · 1 line
2.39.5
RFC PATCH 1/2 mm: Introduce SKSM: Synchronous Kernel Samepage Merging
* Main use-case targeted by SKSM: Code patching
The main use-case targeted by SKSM is deduplication of anonymous pages created by COW (Copy-On-Write) triggered by patching executable and library code for a user-space implementation of "static keys" and "alternative" code patching. Code patching improves:
- Runtime feature detection, where a constructor can dynamically enable a feature by turning a no-op into a jump.
- Instrumentation activation at runtime (e.g. tracepoints) (patch a dormant no-op instrumentation into a jump).
- Runtime assembler specialisation, where a constructor can dynamically modify assembler instructions to select the best alternative for the detected hardware and software environment (e.g. CPU features, rseq availability).
The main distinction between doing code patching at kernel-level and at user-space level is that in user-space, executable and library code is shared across all processes mapping the same executable or library files. This reduces memory use and improves cache locality by sharing executable pages across processes.
Writing to those private mappings trigger COW, which allocates anonymous pages within each process, and thus lose the benefit from sharing the same pages from the backing storage.
Without memory deduplication, this increases memory use, and therefore degrades cache locality: populating the patched content into separate COW pages within each process ends up using distinct CPU cache lines, thus trashing the CPU instruction and data caches.
* Why not use KSM ?
The KSM mechanism has the following downsides which the SKSM ABI aims to overcome:
- KSM requires careful tuning of scan parameters for the workload by the system administrator.
A) This makes KSM mostly useless with a standard distro config.
B) KSM is workload-specific.
C) Scanning pages adds overhead to the system, which is the reason why the scan parameters must be tuned for the workload.
- KSM has security implications, because it allows processes to confirm that an unrelated process has a page which contains a known content.
A) The documentation of madvise(2) MADV_MERGEABLE would benefit from advising against targeting memory that contains secret data, due to the risk of discovery through side-channel timing attack.
B) prctl(2) PR_SET_MEMORY_MERGE inherently marks the entire process memory as mergeable, which makes it incompatible with security oriented use-cases.
* SKSM Overview
SKSM enables synchronous dynamic sharing of identical pages found in different memory areas, even if they are not shared by fork().
Userspace must explicitly request for pages within specific address ranges to be merged with madvise MADV_MERGE. Those should *not* contain secrets, as side-channel timing attacks can allow a process to learn the existence of a known content within another process.
The synchronous memory merging performs the memory merging synchronously within madvise. There is no global scan and no need for background daemon.
The anonymous pages targeted for merge are write-protected and checksummed. They are then compared to other pages targeted for merge.
The mergeable pages are added to a hash table indexed by checksum of their content. The hash value is derived from the page content checksum, and its comparison function is based on comparison of the page content.
If a page is written to after being targeted for merge, a COW will be triggered, and thus a new page will be populated in its stead.
* Expected Use
User-space is expected to perform code patching, e.g. from a library constructor, and then when the text pages are expected to stay invariant for a long time, issue madvise(2) MADV_MERGE on those pages. At this point, the pages will be write-protected, and merged with identical SKSM pages. Further stores to those pages will trigger COW again.
* Results
Output of "cat /proc/vmstat | grep nr_anon_pages" while running 1000 instances of "sleep 500":
- Baseline (no preload): nr_anon_pages 39721
- COW each executable page from libc: nr_anon_pages 419927
- madvise MADV_MERGE after COW of libc: nr_anon_pages 45525
* Limitations
- This is a Proof-of-concept !
- It is incompatible with SKM (depends on !KSM) for now.
- Swap behavior under memory pressure is untested.
- The size of the hash table is static (65536 buckets) for now.
--- include/linux/ksm.h | 4 + include/linux/mm_types.h | 7 + include/linux/page-flags.h | 42 +++++ include/linux/sksm.h | 27 +++ include/uapi/asm-generic/mman-common.h | 2 + mm/Kconfig | 5 + mm/Makefile | 1 + mm/ksm-common.h | 228 +++++++++++++++++++++++++ mm/ksm.c | 219 +----------------------- mm/madvise.c | 6 + mm/memory.c | 2 + mm/page_alloc.c | 3 + mm/sksm.c | 190 +++++++++++++++++++++ 13 files changed, 518 insertions(+), 218 deletions(-) create mode 100644 include/linux/sksm.h create mode 100644 mm/ksm-common.h create mode 100644 mm/sksm.c
include/linux/ksm.h +4 -0
@@ -118,6 +118,10 @@ static inline void ksm_exit(struct mm_struct *mm) { } +static inline void ksm_map_zero_page (struct mm_struct *mm) +{ +} +static inline void ksm_might_unmap_zero_page (struct mm_struct *mm,pte_t pte) { }
include/linux/mm_types.h +7 -0
@@ -19,6 +19,7 @@#include <linux/workqueue.h> #include <linux/seqlock.h> #include <linux/percpu_counter.h> +#include <linux/types.h> #include <asm/mmu.h>
@@ -216,6 +217,12 @@ struct page {struct page *kmsan_shadow;struct page *kmsan_origin;#endif + +#ifdef CONFIG_SKSM +/* TODO: move those fields into unused union fields instead. */ +struct hlist_node sksm_node; +u32 checksum; +#endif } _struct_page_alignment;/*
include/linux/page-flags.h +42 -0
48 lines not on this page · raw message
include/linux/sksm.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_SKSM_H +#define __LINUX_SKSM_H +/* +* Synchronous memory merging support. +* +* This code enables synchronous dynamic sharing of identical pages +* found in different memory areas, even if they are not shared by +* fork(). +*/ + +#ifdef CONFIG_SKSM + +int sksm_merge (struct vm_area_struct *vma,unsigned long start, +unsigned long end); + +#else /* !CONFIG_KSM */ + +static inline int sksm_merge (struct vm_area_struct *vma,unsigned long start, +unsigned long end) +{ +return 0 ; +} + +#endif /* !CONFIG_KSM */ + +#endif /* __LINUX_SKSM_H */
include/uapi/asm-generic/mman-common.h +2 -0
@@ -79,6 +79,8 @@#define MADV_COLLAPSE 25/* Synchronous hugepage collapse */ +#define MADV_MERGE 26/* Synchronously merge identical pages */ +#define MADV_GUARD_INSTALL 102/* fatal signal on access to range */ #define MADV_GUARD_REMOVE 103/* unguard range */
mm/Kconfig +5 -0
@@ -740,6 +740,11 @@ config KSM until a program has madvised that an area is MADV_MERGEABLE, and root has set /sys/kernel/mm/ksm/run to 1 (if CONFIG_SYSFS is set). +config SKSM + bool "Enable Synchronous KSM for page merging" + depends on MMU && !KSM + select XXHASH + config DEFAULT_MMAP_MIN_ADDR int "Low address space to protect from user allocation" depends on MMU
mm/Makefile +1 -0
@@ -85,6 +85,7 @@ obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_KSM) += ksm.o +obj-$(CONFIG_SKSM) += sksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_KFENCE) += kfence/
mm/ksm-common.h new file +228 -0
228 lines not on this page · raw message
mm/ksm.c +1 -218
237 lines not on this page · raw message
mm/madvise.c +6 -0
@@ -22,6 +22,7 @@#include <linux/string.h> #include <linux/uio.h> #include <linux/ksm.h> +#include <linux/sksm.h> #include <linux/fs.h> #include <linux/file.h> #include <linux/blkdev.h>
@@ -1318,6 +1319,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,return madvise_guard_install (vma, prev, start, end);case MADV_GUARD_REMOVE :return madvise_guard_remove (vma, prev, start, end); +case MADV_MERGE : +return sksm_merge (vma, start, end); } anon_name =anon_vma_name (vma);
@@ -1422,6 +1425,9 @@ madvise_behavior_valid(int behavior)#ifdef CONFIG_MEMORY_FAILURE case MADV_SOFT_OFFLINE :case MADV_HWPOISON : +#endif +#ifdef CONFIG_SKSM +case MADV_MERGE :#endif return true;
mm/memory.c +2 -0
@@ -3618,6 +3618,8 @@ static bool wp_can_reuse_anon_folio(struct folio *folio,*/ if (folio_test_ksm (folio) ||folio_ref_count (folio) >3 )return false; +if (folio_test_sksm (folio)) +return false;if (!folio_test_lru (folio))/* * We cannot easily detect+handle references from
mm/page_alloc.c +3 -0
@@ -1122,6 +1122,7 @@ __always_inline bool free_pages_prepare(struct page *page,return false; } +sksm_page_remove (page);page_cpupid_reset_last (page); page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP ;reset_page_owner (page, order);
@@ -1509,6 +1510,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,set_page_private (page,0 );set_page_refcounted (page); +set_page_checksum (page,0 ); +init_page_sksm_node (page);arch_alloc_page (page, order);debug_pagealloc_map_pages (page,1 << order);
mm/sksm.c new file +190 -0
190 lines not on this page · raw message
Signature · 1 line
2.39.5
RFC PATCH 2/2 selftests/kskm: Introduce SKSM basic test
In reply to Mathieu DesnoyersIntroduce a basic selftest for SKSM. See ./basic_test -h for options.
--- tools/testing/selftests/sksm/.gitignore | 2 + tools/testing/selftests/sksm/Makefile | 14 ++ tools/testing/selftests/sksm/basic_test.c | 217 ++++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 tools/testing/selftests/sksm/.gitignore create mode 100644 tools/testing/selftests/sksm/Makefile create mode 100644 tools/testing/selftests/sksm/basic_test.c
tools/testing/selftests/sksm/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +basic_test
tools/testing/selftests/sksm/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0+ OR MIT + +top_srcdir = ../../../.. + +CFLAGS += -O2 -Wall -g -I./ $(KHDR_INCLUDES) -L$(OUTPUT) -Wl,-rpath=./ \ + $(CLANG_FLAGS) -I$(top_srcdir)/tools/include +LDLIBS += -lpthread + +TEST_GEN_PROGS = basic_test + +include ../lib.mk + +$(OUTPUT)/%: %.c + $(CC) $(CFLAGS) $< $(LDLIBS) -o $@
tools/testing/selftests/sksm/basic_test.c new file +217 -0
217 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 3 lines · Mathieu Desnoyers
This series introduces SKSM, a new page deduplication ABI, aiming to fix the limitations inherent to the KSM ABI.
So I'm not interested in seeing *another* KSM version.
Because I absolutely do *NOT* want a new chapter in the saga of SLUB vs SLAB vs SLOB.
However, if the feeling is that this can *replace* the current horror that is KSM, I'm a lot more interested. I suspect our current KSM model has largely been a failure, and this might be "good enough".
Linus
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Mathieu Desnoyers
This series introduces SKSM, a new page deduplication ABI, aiming to fix the limitations inherent to the KSM ABI.
Quoted 8 lines
So I'm not interested in seeing *another* KSM version.
Because I absolutely do *NOT* want a new chapter in the saga of SLUB vs SLAB vs SLOB.
However, if the feeling is that this can *replace* the current horror that is KSM, I'm a lot more interested. I suspect our current KSM model has largely been a failure, and this might be "good enough".
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM. I suspect that much of the problems KSM has today are caused by the semantic of the ABI it exposes, which were targeted solely for a host deduplicating guest VMs memory use-case.
KSM tracks memory meant to be mergeable on an ongoing basis with a worker thread:
madvise(2) MADV_{UN,}MERGEABLE
prctl(2) PR_{SET,GET}_MEMORY_MERGE (security concern) ~2.5k LOC exclusing ksm-common code
requires parameter fine-tuning from sysadmin
SKSM gets the hint from userspace that memory is a good candidate for merging in its current state and is expected to stay invariant:
madvise(2) MADV_MERGE
~100 LOC exclusing ksm-common code
The main reason why SKSM could be implemented without all the scanning complexity is because of this simpler ABI.
Thanks for the feedback!
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
In which case I don't want to see some second different interface that just makes it all worse.
Linus
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
Quoted 6 lines
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
Exactly that; and besides the VM use-case, lately people stated using it in the context of interpreters (IIRC inside Meta) quite successfully as well.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Quoted 5 lines
Quoted 2 lines
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
Exactly that; and besides the VM use-case, lately people stated using it in the context of interpreters (IIRC inside Meta) quite successfully as well.
Does Red Hat (or any other KVM supporters) actually recommend using KSM for VMs in cloud environments?
The security implications of scanning guest memory and having co-tenant VMs share mappings (should) make it a complete non-starter for any scenario where VMs and/or their workloads are owned by third parties.
I can imagine there might be first-party use cases, but I would expect many/most of those to be able to explicitly share mappings, which would provide far, far better power and performance characteristics.
Quoted 17 lines · Sean Christopherson
Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
Exactly that; and besides the VM use-case, lately people stated using it in the context of interpreters (IIRC inside Meta) quite successfully as well.
Does Red Hat (or any other KVM supporters) actually recommend using KSM for VMs in cloud environments?
Private clouds yes, that's where it is most commonly used for. I would assume that nobody for
For example, there is some older documentation here:
which touches on the security aspects:
"The page deduplication technology (used also by the KSM implementation) may introduce side channels that could potentially be used to leak information across multiple guests. In case this is a concern, KSM can be disabled on a per-guest basis."
Quoted 4 lines
The security implications of scanning guest memory and having co-tenant VMs share mappings (should) make it a complete non-starter for any scenario where VMs and/or their workloads are owned by third parties.
Jep.
Quoted 4 lines
I can imagine there might be first-party use cases, but I would expect many/most of those to be able to explicitly share mappings, which would provide far, far better power and performance characteristics.
Note that KSM can be very efficient when you have multiple VMs running the same kernel,executable,libraries etc. If my memory doesn't trick me, that's precisely for what it was originally invented, and how it is getting used today in the context of VMs.
For example, QEMU will mark all guest memory is mergeable using MADV, to limit the deduplicaton to guest RAM only.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 21 lines · David Hildenbrand
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · David Hildenbrand
Quoted 17 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Exactly that; and besides the VM use-case, lately people stated using it in the context of interpreters (IIRC inside Meta) quite successfully as well.
Does Red Hat (or any other KVM supporters) actually recommend using KSM for VMs in cloud environments?
Private clouds yes, that's where it is most commonly used for. I would assume that nobody for
forgot to complete that sentence: "... nobody really should be using that in public clouds."
Signature · 3 lines
Cheers, David / dhildenb
On 2025-02-28 10:10, David Hildenbrand wrote:
[...]
Quoted 2 lines
For example, QEMU will mark all guest memory is mergeable using MADV, to limit the deduplicaton to guest RAM only.
On a related note, I think the madvise(2) documentation is inaccurate.
It states:
MADV_MERGEABLE (since Linux 2.6.32)
Enable Kernel Samepage Merging (KSM) for the pages in the range specified by addr and length. [...]
AFAIU, based on code review of ksm_madvise(), this is not strictly true.
The KSM implementation enables KSM for pages in the entire vma containing the range. So if it so happens that two mmap areas with identical protection flags are merged, both will be considered mergeable by KSM as soon as at least one page from any of those areas is made mergeable.
This does not appear to be an issue in qemu because guard pages with different protection are placed between distinct mappings, which should prevent combining the vmas.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 14 lines · Mathieu Desnoyers
On 2025-02-28 10:10, David Hildenbrand wrote:
[...]Quoted 3 lines
For example, QEMU will mark all guest memory is mergeable using MADV, to limit the deduplicaton to guest RAM only.
On a related note, I think the madvise(2) documentation is inaccurate.
It states:
MADV_MERGEABLE(since Linux 2.6.32)
Enable Kernel Samepage Merging (KSM) for the pages in the range specified by addr and length. [...]
Quoted 6 lines
AFAIU, based on code review of
ksm_madvise(), this is not strictly true.The KSM implementation enables KSM for pages in the entire vma containing the range. So if it so happens that two mmap areas with identical protection flags are merged, both will be considered mergeable by KSM as soon as at least one page from any of those areas is made mergeable.
I *think* it does what is documented. In madvise_vma_behavior(), ksm_madvise() will update "new_flags".
Then we call madvise_update_vma() to split the VMA if required and set new_flags only on the split VMA. The handling is similar to other MADV operations that end up modifying vm_flags.
If I am missing something and this is indeed broken, we should definitely write a selftest for it and fix it.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 22 lines · David Hildenbrand
Quoted 14 lines · Mathieu Desnoyers
On 2025-02-28 10:10, David Hildenbrand wrote:
[...]Quoted 3 lines
For example, QEMU will mark all guest memory is mergeable using MADV, to limit the deduplicaton to guest RAM only.
On a related note, I think the madvise(2) documentation is inaccurate.
It states:
MADV_MERGEABLE(since Linux 2.6.32)
Enable Kernel Samepage Merging (KSM) for the pages in the range specified by addr and length. [...]Quoted 6 lines
AFAIU, based on code review of
ksm_madvise(), this is not strictly true.The KSM implementation enables KSM for pages in the entire vma containing the range. So if it so happens that two mmap areas with identical protection flags are merged, both will be considered mergeable by KSM as soon as at least one page from any of those areas is made mergeable.
Quoted 6 lines
I *think* it does what is documented. In
madvise_vma_behavior(),ksm_madvise()will update "new_flags".Then we call
madvise_update_vma()to split the VMA if required and setnew_flagsonly on the split VMA. The handling is similar to other MADV operations that end up modifyingvm_flags.If I am missing something and this is indeed broken, we should definitely write a selftest for it and fix it.
You are correct, I missed that part. Thanks for the clarification!
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 12 lines · David Hildenbrand
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
Quoted 9 lines
Quoted 6 lines
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
Exactly that; and besides the VM use-case, lately people stated using it in the context of interpreters (IIRC inside Meta) quite successfully as well.
I suspect that SKSM is a better fit for JIT and code patching than KSM, because user-space knows better when a set of pages is going to become invariant for a long time and thus benefit from merging. This removes the background scanning from the picture.
Does the interpreter use-case require background scanning, or does it know when a set of pages are meant to become invariant for a long time ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 25 lines · Mathieu Desnoyers
Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Quoted 7 lines
Quoted 2 lines
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
Exactly that; and besides the VM use-case, lately people stated using it in the context of interpreters (IIRC inside Meta) quite successfully as well.
Quoted 7 lines
I suspect that SKSM is a better fit for JIT and code patching than KSM, because user-space knows better when a set of pages is going to become invariant for a long time and thus benefit from merging. This removes the background scanning from the picture.
Quoted 1 line
Does the interpreter use-case require background scanning, or does
it know when a set of pages are meant to become invariant for a long time ?
To make the JIT/interpreter use case happy, people wanted ways to *force* KSM on for *the whole process*, not just individual VMAs like the traditional VM use case would have done.
I recall one of the reasons being that you don't really want to modify your JIT/interpreter to just make KSM work.
See [1] "KSM at Meta" for some details, and in general, optimization work to adapt KSM to new use cases.
Regarding some concerns you raised, Stefan did a lot of optimization work like "smart scanning" (slide "Optimization - Smart Scan (6.7)") to reduce the scanning overhead and make it much more efficient.
So people started optimizing for that already and got pretty good results.
[1] https://lpc.events/event/17/contributions/1625/attachments/1320/2649/KSM.pdf
Signature · 3 lines
Cheers, David / dhildenb
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Mathieu Desnoyers
I'd be fine with SKSM replacing KSM entirely. However, I don't think we should try to re-implement the existing KSM userspace ABIs over SKSM.
No, absolutely. The only point (for me) for your new synchronous one would be if it replaced the kernel thread async scanning, which would make the old user space interface basically pointless.
Quoted 7 lines
But I don't actually know who uses KSM right now. My reaction really comes from a "it's not nice code in the kernel", not from any actual knowledge of the users.
Maybe it works really well in some cloud VM environment, and we're stuck with it forever.
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
This would allow userspace to replace ksmd by tracking the age of those anonymous pages, and issue madvise MADV_MERGE on them to write-protect+merge them when it is deemed useful.
With both a new userfaultfd COW event and madvise MADV_MERGE, is there anything else that is fundamentally missing to move all the scanning complexity of KSM to userspace for the VM deduplication use-case ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See UFFDIO_REGISTER_MODE_WP:
https://man7.org/linux/man-pages/man2/userfaultfd.2.html
Quoted 9 lines
This would allow userspace to replace ksmd by tracking the age of those anonymous pages, and issue madvise
MADV_MERGEon them to write-protect+merge them when it is deemed useful.With both a new userfaultfd COW event and madvise
MADV_MERGE, is there anything else that is fundamentally missing to move all the scanning complexity of KSM to userspace for the VM deduplication use-case ?
Thanks,
Signature · 1 line
Peter Xu
Quoted 8 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:
userfaultfd UFFDIO_REGISTER only seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).
Also, I notice that do_wp_page() only calls handle_userfault VM_UFFD_WP when vm_fault flags does not have FAULT_FLAG_UNSHARE set.
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
Thanks,
Mathieu
Quoted 13 lines
Quoted 10 lines
This would allow userspace to replace ksmd by tracking the age of those anonymous pages, and issue madvise
MADV_MERGEon them to write-protect+merge them when it is deemed useful.With both a new userfaultfd COW event and madvise
MADV_MERGE, is there anything else that is fundamentally missing to move all the scanning complexity of KSM to userspace for the VM deduplication use-case ?Thanks,
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 8 lines · Mathieu Desnoyers
Quoted 7 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:
Quoted 6 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).
Yes, so far sync traps only supports RAM-based file systems, or anonymous. Generic private file mappings (that stores executables and libraries) are not yet supported.
Quoted 4 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.
AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
Quoted 3 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
I think you're right. So we have UFFD_FEATURE_WP_ASYNC that should work on most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on how async tracking works.
Thanks,
Signature · 1 line
Peter Xu
Quoted 11 lines · Peter Xu
Quoted 10 lines · Mathieu Desnoyers
Quoted 8 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:
Quoted 8 lines
Quoted 4 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).Yes, so far sync traps only supports RAM-based file systems, or anonymous. Generic private file mappings (that stores executables and libraries) are not yet supported.
OK, this confirms my observations.
Quoted 8 lines
Quoted 5 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
I'm confused by your comment. I thought unshare only applies to *write* faults. What am I missing ?
Quoted 9 lines
Quoted 4 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
I think you're right. So we have
UFFD_FEATURE_WP_ASYNCthat should work on most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on how async tracking works.
I'll try this out. It may not matter that it's async given a use-case use-cases of tracking the age since the WP fault on the COW pages. We don't need to react to the event in-place to alter its behavior, just a notification should be fine AFAIU.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 9 lines · Mathieu Desnoyers
Quoted 6 lines
Quoted 3 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
I'm confused by your comment. I thought unshare only applies to *write* faults. What am I missing ?
The major path so far to set unshare is here in GUP (ignoring two corner cases used in either s390 and ksm):
if (unshare) {
fault_flags |= FAULT_FLAG_UNSHARE;
/* FAULT_FLAG_WRITE and FAULT_FLAG_UNSHARE are incompatible */
VM_BUG_ON(fault_flags & FAULT_FLAG_WRITE);
}See the VM_BUG_ON() - if it's write it'll crash already.
"unshare", in its earliest form of patch, used to be called COR (Copy-On-Read), which might be more straightforward in this case.. so it's the counterpart of COW but for read cases where a copy is required. The patchset that introduced it has more information (e.g. a7f2266041).
Thanks,
Signature · 1 line
Peter Xu
Quoted 5 lines · Peter Xu
Quoted 4 lines · Mathieu Desnoyers
Quoted 4 lines
Quoted 3 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
I'm confused by your comment. I thought unshare only applies to *write* faults. What am I missing ?
The major path so far to set unshare is here in GUP (ignoring two corner cases used in either s390 and ksm):
"unshare" fault, in contrast to a write fault, will not turn the PTE writable.
That's why it does not trigger userfaultfd-wp: there is no write access, write-protection is left unchanged.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 29 lines · Peter Xu
Quoted 8 lines · Mathieu Desnoyers
Quoted 7 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:Quoted 6 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).Yes, so far sync traps only supports RAM-based file systems, or anonymous. Generic private file mappings (that stores executables and libraries) are not yet supported.
Quoted 4 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
I think you're right. So we have
UFFD_FEATURE_WP_ASYNCthat should work on most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on how async tracking works.
I'm looking more closely at admin-guide/mm/pagemap.rst and it appears to be a good fit. Here is what I have in mind to replace the ksmd scanning thread for the VM use-case by a purely user-space driven scanning:
Within qemu or similar user-space process:
1) Track guest memory with the userfaultfd UFFD_FEATURE_WP_ASYNC feature and UFFDIO_REGISTER_MODE_WP mode.
2) Protect user-space memory with the PAGEMAP_SCAN ioctl PM_SCAN_WP_MATCHING flag to detect memory which stays invariant for a long time.
3) Use the PAGEMAP_SCAN ioctl with PAGE_IS_WRITTEN to detect which pages are written to. Keep track of memory which is frequently modified, so it can be left alone and not write-protected nor merged anymore.
4) Whenever pages stay invariant for a given lapse of time, merge them with the new madvise(2) KSM_MERGE behavior.
Let me know if that makes sense.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 50 lines · Mathieu Desnoyers
Quoted 29 lines · Peter Xu
Quoted 8 lines · Mathieu Desnoyers
Quoted 7 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:Quoted 6 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).Yes, so far sync traps only supports RAM-based file systems, or anonymous. Generic private file mappings (that stores executables and libraries) are not yet supported.
Quoted 4 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
I think you're right. So we have
UFFD_FEATURE_WP_ASYNCthat should work on most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on how async tracking works.I'm looking more closely at
admin-guide/mm/pagemap.rstand it appears to be a good fit. Here is what I have in mind to replace the ksmd scanning thread for the VM use-case by a purely user-space driven scanning:Within qemu or similar user-space process:
1) Track guest memory with the userfaultfd
UFFD_FEATURE_WP_ASYNCfeature andUFFDIO_REGISTER_MODE_WPmode.2) Protect user-space memory with the
PAGEMAP_SCANioctlPM_SCAN_WP_MATCHINGflag to detect memory which stays invariant for a long time.
Quoted 8 lines
3) Use the
PAGEMAP_SCANioctl withPAGE_IS_WRITTENto detect which pages are written to. Keep track of memory which is frequently modified, so it can be left alone and not write-protected nor merged anymore.4) Whenever pages stay invariant for a given lapse of time, merge them with the new madvise(2)
KSM_MERGEbehavior.Let me know if that makes sense.
I can't speak of how KSM should go from there, but from userfault tracking POV, that makes sense to me.
Thanks,
Signature · 1 line
Peter Xu
Quoted 50 lines · Mathieu Desnoyers
Quoted 29 lines · Peter Xu
Quoted 8 lines · Mathieu Desnoyers
Quoted 7 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:Quoted 6 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).Yes, so far sync traps only supports RAM-based file systems, or anonymous. Generic private file mappings (that stores executables and libraries) are not yet supported.
Quoted 4 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
I think you're right. So we have
UFFD_FEATURE_WP_ASYNCthat should work on most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on how async tracking works.I'm looking more closely at
admin-guide/mm/pagemap.rstand it appears to be a good fit. Here is what I have in mind to replace the ksmd scanning thread for the VM use-case by a purely user-space driven scanning:Within qemu or similar user-space process:
1) Track guest memory with the userfaultfd
UFFD_FEATURE_WP_ASYNCfeature andUFFDIO_REGISTER_MODE_WPmode.2) Protect user-space memory with the
PAGEMAP_SCANioctlPM_SCAN_WP_MATCHINGflag to detect memory which stays invariant for a long time.
Quoted 8 lines
3) Use the
PAGEMAP_SCANioctl withPAGE_IS_WRITTENto detect which pages are written to. Keep track of memory which is frequently modified, so it can be left alone and not write-protected nor merged anymore.4) Whenever pages stay invariant for a given lapse of time, merge them with the new madvise(2)
KSM_MERGEbehavior.Let me know if that makes sense.
Note that one of the strengths of ksm in the kernel right now is that we write-protect + try-deduplicate only when we are fairly sure that we can deduplicate (unstable tree), and that the interaction with THPs / large folios is fairly well thought-through.
Also note that, just because data hasn't been written in some time interval, doesn't mean that it should be deduplicated and result in CoW on next write access.
One probably would have to mimic what the KSM implementation in the kernel does, and built something like the unstable tree, to find candidates where we can actually deduplciate. Then, have a way to not-deduplicate if the content changed.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 55 lines · David Hildenbrand
Quoted 54 lines · Mathieu Desnoyers
Quoted 29 lines · Peter Xu
Quoted 8 lines · Mathieu Desnoyers
Quoted 7 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we have such event for years.. See
UFFDIO_REGISTER_MODE_WP:Quoted 6 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).Yes, so far sync traps only supports RAM-based file systems, or anonymous. Generic private file mappings (that stores executables and libraries) are not yet supported.
Quoted 4 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes. So uffd-wp shouldn't trap any of those.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
I think you're right. So we have
UFFD_FEATURE_WP_ASYNCthat should work on most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on how async tracking works.I'm looking more closely at
admin-guide/mm/pagemap.rstand it appears to be a good fit. Here is what I have in mind to replace the ksmd scanning thread for the VM use-case by a purely user-space driven scanning:Within qemu or similar user-space process:
1) Track guest memory with the userfaultfd
UFFD_FEATURE_WP_ASYNCfeature andUFFDIO_REGISTER_MODE_WPmode.2) Protect user-space memory with the
PAGEMAP_SCANioctlPM_SCAN_WP_MATCHINGflag to detect memory which stays invariant for a long time.3) Use the
PAGEMAP_SCANioctl withPAGE_IS_WRITTENto detect which pages are written to. Keep track of memory which is frequently modified, so it can be left alone and not write-protected nor merged anymore.
Quoted 8 lines
Quoted 4 lines
4) Whenever pages stay invariant for a given lapse of time, merge them with the new madvise(2)
KSM_MERGEbehavior.Let me know if that makes sense.
Note that one of the strengths of ksm in the kernel right now is that we write-protect + try-deduplicate only when we are fairly sure that we can deduplicate (unstable tree), and that the interaction with THPs / large folios is fairly well thought-through.
Also note that, just because data hasn't been written in some time interval, doesn't mean that it should be deduplicated and result in CoW on next write access.
Right. This tracking of address range access pattern would have to be implemented in user-space.
Quoted 1 line
One probably would have to mimic what the KSM implementation in the kernel does, and built something like the unstable tree, to find candidates where we can actually deduplciate. Then, have a way to not- deduplicate if the content changed.
With madvise MADV_MERGE, there is no need to "unmerge". The merge write-protects the page and merges its content at the time of the MADV_MERGE with exact duplicates, and keeps that write protected page in a global hash table indexed by checksum.
However, unlike KSM, it won't track that range on an ongoing basis.
"Unmerging" the page is done naturally by writing to the merged address
range. Because it is write-protected, this will trigger COW, and will therefore provide a new anonymous page to the process, thus "unmerging" that page.
It's really just up to userspace to track COW faults and figure out that it really should not try to merge that range anymore, based on the the access pattern monitored through write-protection faults.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 89 lines · Mathieu Desnoyers
Quoted 71 lines · David Hildenbrand
Quoted 64 lines · Mathieu Desnoyers
Quoted 36 lines · Peter Xu
Quoted 9 lines · Mathieu Desnoyers
Quoted 8 lines · Peter Xu
Quoted 2 lines · Mathieu Desnoyers
For the VM use-case, I wonder if we could just add a userfaultfd "COW" event that would notify userspace when a COW happens ?
I don't know what's the best for KSM and how well this will work, but we
have such event for years.. SeeUFFDIO_REGISTER_MODE_WP:Quoted 6 lines
userfaultfd
UFFDIO_REGISTERonly seems to work if I pass an address resulting from a mmap mapping, but returns EINVAL if I pass a page-aligned address which sits within a private file mapping (e.g. executable data).Yes, so far sync traps only supports RAM-based file systems, or anonymous.
Generic private file mappings (that stores executables and libraries) are
not yet supported.Quoted 4 lines
Also, I notice that
do_wp_page()only callshandle_userfaultVM_UFFD_WPwhenvm_faultflags does not haveFAULT_FLAG_UNSHAREset.AFAICT that's expected, unshare should only be set on reads, never writes.
So uffd-wp shouldn't trap any of those.Quoted 3 lines
AFAIU, as it stands now userfaultfd would not help tracking COW faults caused by stores to private file mappings. Am I missing something ?
Quoted 6 lines
I think you're right. So we have
UFFD_FEATURE_WP_ASYNCthat should work on
most mappings. That one is async, though, so more like soft-dirty. It might be doable to try making it sync too without a lot of changes based on
how async tracking works.I'm looking more closely at
admin-guide/mm/pagemap.rstand it appears to be a good fit. Here is what I have in mind to replace the ksmd scanning thread for the VM use-case by a purely user-space driven scanning:Within qemu or similar user-space process:
1) Track guest memory with the userfaultfd
UFFD_FEATURE_WP_ASYNCfeature and
UFFDIO_REGISTER_MODE_WPmode.2) Protect user-space memory with the
PAGEMAP_SCANioctlPM_SCAN_WP_MATCHINGflag
to detect memory which stays invariant for a long time.3) Use the
PAGEMAP_SCANioctl withPAGE_IS_WRITTENto detect which pages are written to.
Keep track of memory which is frequently modified, so it can be
left alone and
not write-protected nor merged anymore.Quoted 5 lines
4) Whenever pages stay invariant for a given lapse of time, merge them with the new
madvise(2)KSM_MERGEbehavior.Let me know if that makes sense.
Quoted 8 lines
Note that one of the strengths of ksm in the kernel right now is that we write-protect + try-deduplicate only when we are fairly sure that we can deduplicate (unstable tree), and that the interaction with THPs / large folios is fairly well thought-through.
Also note that, just because data hasn't been written in some time interval, doesn't mean that it should be deduplicated and result in CoW on next write access.
Right. This tracking of address range access pattern would have to be implemented in user-space.
Quoted 4 lines
One probably would have to mimic what the KSM implementation in the kernel does, and built something like the unstable tree, to find candidates where we can actually deduplciate. Then, have a way to not- deduplicate if the content changed.
Quoted 4 lines
With madvise
MADV_MERGE, there is no need to "unmerge". The merge write-protects the page and merges its content at the time of theMADV_MERGEwith exact duplicates, and keeps that write protected page in a global hash table indexed by checksum.
Right, and that's a real problem.
Quoted 12 lines
However, unlike KSM, it won't track that range on an ongoing basis.
"Unmerging" the page is done naturally by writing to the merged address range. Because it is write-protected, this will trigger COW, and will therefore provide a new anonymous page to the process, thus "unmerging" that page.
It's really just up to userspace to track COW faults and figure out that it really should not try to merge that range anymore, based on the the access pattern monitored through write-protection faults.
Just to be clear, what you described here is very likely not performance-wise any feasible replacement for the in-tree ksm for the VM use case (again, the thing that was primarily invented for VMs).
Signature · 3 lines
Cheers, David / dhildenb
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Mathieu Desnoyers
This series introduces SKSM, a new page deduplication ABI, aiming to fix the limitations inherent to the KSM ABI.
Quoted 8 lines
So I'm not interested in seeing *another* KSM version.
Because I absolutely do *NOT* want a new chapter in the saga of SLUB vs SLAB vs SLOB.
However, if the feeling is that this can *replace* the current horror that is KSM, I'm a lot more interested. I suspect our current KSM model has largely been a failure, and this might be "good enough".
Maybe it would be comparable to khugepaged vs. MADV_COLLAPSE?
Many/most use cases just leave THP scanning+collapsing to khugepaged; selected ones might "know better" what to do, so they effectively disable khugepaged, and manually collapse THPs using MADV_COLLAPSE.
If it would be similar to that, it would not be completely different KSM version, just a different way to trigger merging: background scanning vs. user-space triggered ("synchronous").
I could see use cases for such a synchronous interface, but I doubt it could replace the background scanning that is actively getting used for existing use cases; I have similar thoughts about khugepaged vs. MADV_COLLAPSE.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 1 line · David Hildenbrand
Maybe it would be comparable to khugepaged vs.
MADV_COLLAPSE?
I think it is comparable ... because many people find khugepaged unacceptable and there are proposals to move that to userspace.