The Xen
balloon driver can be enabled to allow the Xen hypervisor to dynamically resize the amount of RAM a
NetBSD VM is allowed to use. This allows the hypervisor to manage RAM more efficiently, similar in spirit to how the
NetBSD kernel would manage RAM wrt processes, with
uvm(9).
The
balloon driver negotiates domain VM memory allocation change requests from the Xen hypervisor, by reserving "pinned" memory for itself from the
NetBSD uvm(9) subsystem.
There are two methods to notify the
balloon driver of memory allocation change requests.
xenbus
In the xenbus method, the change notification originates outside the VM, presumable from the Hypervisor, and is communicated over the xenbus interface.
In the
sysctl(3) method, the change notification originates from within the VM. Two
sysctl(7) nodes are provided:
kern.xen.balloon.current
This node is read-only and returns the number of memory pages currently allocated to the VM.
kern.xen.balloon.target
This node can be written to, and sets a new target memory reservation, in pages. Note that targets set this way from within the VM do not reflect in the hypervisor. This is therefore a less preferred way to use the balloon
In either case, once a new allocation target is set, the balloon driver thread asynchronously attempts to reach this target in discrete steps. Every step, if it fails, is tried more than once. When the target is reached, the thread goes back to sleep, waiting for a new incoming target. If it is not reached, the target is updated to reflect the value after the last successful step.