KSM Fun2025-12-08

With the recent RAM pricing situation, I've been exploring some unconventional stop-gaps. I plan to write about a more practical one soon once I finish testing, but I figured I'd lead with this very silly one first.

Years ago KSM support was added to Linux which performed page deduplication, this was useful for eg. QEMU workloads. Later a patchset called UKSM was developed which extended this system-wide, it was never merged for obvious reasons. But it seems there was actually some recent work in kernel and systemd which allows enabling KSM for all services. The commands for this are below. Please note KSM has security issues regarding ASLR and side-channels, so this should't actually be used without serious considerations.

echo -e "[Service]\nMemoryKSM=true" > /usr/lib/systemd/system/service.d/ksm.conf #let systemd invoke KSM prctl
echo "w /sys/kernel/mm/ksm/run - - - - 1" > /usr/lib/tmpfiles.d/ksm.conf #persistently enable ksm
echo "w /sys/kernel/mm/ksm/advisor_mode - - - - scan-time" >> /usr/lib/tmpfiles.d/ksm.conf #enable scan time advisor
systemctl daemon-reload #let it take effect
reboot now #to restart all processes
watch -n1 grep -r . /sys/kernel/mm/ksm/ #monitor status

As for practicality? I saw anywhere between 10MB to 300MB of savings depending on workload. I suggest giving this LWN article a read if you are interested.

Comment on this: Fediverse

Back to blog index