Copy-on-Write at the Hardware Level: Memory Deduplication in Modern CPUs
Copy-on-write is typically discussed as a software pattern—fork semantics, filesystem snapshots, database isolation—but modern CPUs provide hardware primitives that make memory deduplication at the page level both faster and more secure. These mechanisms allow operating systems and hypervisors to transparently share identical memory pages across processes or virtual machines, breaking them apart only when one attempts a write.
The Memory Duplication Problem
In cloud environments, hundreds of virtual machines often run nearly identical operating system images, libraries, and application runtimes. Each VM traditionally gets its own full copy of these pages in physical memory, even when the contents are byte-for-byte identical. A hypervisor running 50 Linux VMs might have 50 copies of the same kernel text, glibc, and common binaries—gigabytes of redundant data occupying scarce DRAM.
Memory deduplication scans for identical pages and merges them, mapping multiple virtual addresses to a single physical frame. The challenge is making this transparent and safe: a write to one virtual page must not corrupt another process’s view of that memory.
Hardware Assist for Copy-on-Write
Intel’s Memory Protection Extensions (MPX, now deprecated) and more recent work in ARM’s Memory Tagging Extension (MTE) and AMD’s Secure Memory Encryption (SME) have explored hardware support for memory sharing and isolation. But the most widely deployed primitive is simpler: CPU support for efficient page table manipulation and dirty bit tracking.
When the OS or hypervisor marks a shared page as read-only in the page tables, any write attempt triggers a page fault. The kernel intercepts this fault, allocates a new physical frame, copies the original page contents, and updates the faulting process’s page table to point to the private copy—now writable. The original shared page remains intact for other processes.
This mechanism relies on the CPU’s memory management unit (MMU) to enforce read-only semantics at hardware speed, with no per-access overhead until a write occurs. Dirty bits in page table entries allow the kernel to quickly identify which pages have been modified, skipping them during future deduplication scans.
Kernel Samepage Merging (KSM) and Hypervisor Use
Linux’s KSM subsystem periodically scans memory looking for identical pages, merging them and marking the shared frame as copy-on-write. This runs in a background kernel thread, using page content hashing to candidate pairs for byte-by-byte comparison. When a match is found, the kernel updates page tables across processes or VMs to point to a single physical page, with the read-only bit set.
Hypervisors like KVM and Xen use KSM to deduplicate memory across guest VMs. VMware’s Transparent Page Sharing (TPS) implements a similar strategy, though it’s often disabled by default due to historical side-channel concerns.
The performance cost comes from scanning and comparison overhead, not from the copy-on-write mechanism itself. Once pages are shared, read access is native speed. Writes incur a one-time page fault and copy, amortized over the page’s lifetime.
Security Considerations
Memory deduplication has been exploited as a side channel. By carefully timing write faults, an attacker can infer whether another VM or process has an identical page in memory—leaking information about running software or even cryptographic key material through cache timing.
Modern implementations mitigate this by restricting deduplication scope (same security domain only), adding jitter to timing, or disabling the feature in sensitive environments. Hardware support for memory tagging and encryption can further isolate shared pages, ensuring that even if physical frames are shared, access patterns remain opaque to side-channel analysis.
When Hardware Support Matters
Without CPU dirty tracking and fast page table updates, copy-on-write deduplication would require software interposition on every memory access—unacceptably slow. Hardware makes it zero-cost until the fault, and the fault itself is a well-optimized path in modern kernels.
Emerging memory technologies like CXL-attached memory pools may extend this model across nodes, sharing pages between physical servers. Hardware-accelerated hashing and comparison units could move scanning out of the CPU entirely, reducing the overhead of finding deduplication candidates.
The pattern shows how a software technique becomes practical only when the hardware provides the right primitives: protected pages, fast faults, and efficient MMU updates. Copy-on-write at the page level is not just a kernel feature—it’s a collaboration between software policy and hardware mechanism.