UVM: A love letter

Recently, I had some DIMM corruption, which led me to decommission one of my machines. The hole situation got me thinking about the fragility of software and how not even the most elegant abstraction, can save you from the grim reality of hardware failure. It also happens to be a good reason to talk about one of my favorite pieces of software: The UVM Virtual Memory System.

Let there be light

UVM is the virtual memory subsystem used in the kernels of OpenBSD and NetBSD. It was written to replace the old (4.4BSD) virtual memory subsystem, which was a modified version of the one found in Carnegie Mellon’s Mach microkernel. The reason for replacement was straightforward: Poor performance, complex data structures, and poor documentation. Which is quite funny because today (2023) the best documentation I could find about the Mach VM comes from Apple and MIT, not from Carnegie Mellon.

The reason for UVM is the classic triad: Simplicity, performance, and maintainability.

Masters of architecture

Having a great VM implementation is one thing, but having a great paper to companion it is something else entirely. With UVM, Cranor and Parulkar give us both.

uvm_arch The architecture is easy to understand. The vmspace structure contains pointers to a process’s pmap and memory map structures, it also contains some statistics. The pmap is the machine-dependent structure, while the memory map is the machine-independent structure that holds the memory objects.

Some observations: The various segments (text/data/stack) are seperate objects, which is beneficial for security. The objects can be of different types, and backed different pagers. The important thing here, is that these types and pagers can be implemented by other kernel subsystems (I think this is really cool). Finally, UVM allows for anonymous mapping, which is usefull if you have two processes that map the same object (like the text segment of /bin/sh).

The sound of memory

Wether it’s kmalloc() or mmap(), in the end they all function because of UVM. And because UVM presents a simple and consistent interface, it will remain one of my favorite pieces of software.

Sources