Skip to contentSkip to conversation

Improve KSM for code patching use-cases

LSF/MM/BPF TOPIC Mathieu Desnoyers 1 participant 2 messages Jan 28, 2025 – Apr 8, 2025 Memory management

lore
Share & tools
lore
#0 lore raw

Hi,

I submit this topic proposal for your consideration:

We are implementing a code patching library which essentially reimplements static keys for userspace. (work in progress, not released yet)

One of the problems we face when patching code from libraries and executables from nops to jumps is Copy-on-Write (CoW), which increases memory use and prevents sharing code pages across processes.

I suspect we can improve Kernel Samepage Merging (KSM) to better handle scenarios where pages which are the result of a CoW from a given file/offset are modified shortly after their CoW, and then are stable for most of their lifetime.

This would allow merging CoW pages when the resulting modification makes them identical again. This could be achieved by tracking possible duplicates on a per file/offset basis, which should be more efficient than a global scan.

I would like to discuss this topic with Filesystem and MM experts to get their input.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#1 lore raw
Quoted 18 lines · Mathieu Desnoyers

Hi,

I submit this topic proposal for your consideration:

We are implementing a code patching library which essentially reimplements static keys for userspace. (work in progress, not released yet)

One of the problems we face when patching code from libraries and executables from nops to jumps is Copy-on-Write (CoW), which increases memory use and prevents sharing code pages across processes.

I suspect we can improve Kernel Samepage Merging (KSM) to better handle scenarios where pages which are the result of a CoW from a given file/offset are modified shortly after their CoW, and then are stable for most of their lifetime.

Quoted 8 lines

This would allow merging CoW pages when the resulting modification makes them identical again. This could be achieved by tracking possible duplicates on a per file/offset basis, which should be more efficient than a global scan.

I would like to discuss this topic with Filesystem and MM experts to get their input.

Here are the slides I presented during my talk:

https://drive.google.com/file/d/18EkWsvILbPlTDRHI22sHIWUhlqBsYILy/view?usp=sharing

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com