freenode
Kernel & Low-Level

Linux virtual swap decouples zswap pages from disk slots

Nhat Pham's v4 series adds a persistent virtual swap device so compressed and zero-filled pages no longer reserve physical swap capacity.

Nhat Pham has posted the fourth revision of a Linux kernel patch series that introduces a virtual swap device, letting anonymous pages move to zswap or a zero-page backend without first claiming a slot on a physical swap device.

Today every swapped-out page is tied to a concrete offset on a swapfile or partition. That model wastes physical capacity when the page is only compressed in memory, and it forces memory cgroups to count zswap usage against the same swap limits that govern on-disk space. Virtual swap inserts an indirection layer built on the existing swap-table infrastructure: page-table entries point at a virtual slot, and the real backing (compressed store, zero page, or a physical disk slot allocated on demand) is recorded separately.

The virtual device is sized at 16 TB with 4 KB pages on 64-bit kernels and is created once at boot when the new vswap= command-line parameter is enabled. It cannot be swapon'd or swapoff'd and lives for the life of the system. When zswap declines a page, the path can still obtain a physical slot later; writeback and reclaim of those backing slots arrive in the same series. Transparent huge-page swap-in is allowed only when a batch shares contiguous physical slots on a synchronous device.

A related accounting change, suggested by Johannes Weiner, charges cgroup swap usage only when a virtual entry acquires real disk backing. With virtual swap on, memory.swap.current therefore reflects on-disk consumption alone, and a cgroup whose swap.max is zero can still reclaim anonymous memory through zswap. The series also defers per-cluster memcg metadata for physical clusters that hold only virtual backings, and widens internal size fields so the virtual device can grow far beyond the old 16 TB ceiling.

Kairui Song originally suggested the approach. The v4 posting replaces an earlier runtime sysctl with the boot parameter, fixes a use-after-free found by syzbot, and refreshes benchmarks that show no material regression.