diff --git a/src/runtime-rs/config/configuration-qemu-runtime-rs.toml.in b/src/runtime-rs/config/configuration-qemu-runtime-rs.toml.in index 3e3b3a1ac..f17eed517 100644 --- a/src/runtime-rs/config/configuration-qemu-runtime-rs.toml.in +++ b/src/runtime-rs/config/configuration-qemu-runtime-rs.toml.in @@ -143,6 +143,16 @@ default_maxvcpus = @DEFMAXVCPUS_QEMU@ # > 5 --> will be set to 5 default_bridges = @DEFBRIDGES@ +# Reclaim guest freed memory. +# Enabling this will result in the VM balloon device having f_reporting=on set. +# Then the hypervisor will use it to reclaim guest freed memory. +# This is useful for reducing the amount of memory used by a VM. +# Enabling this feature may sometimes reduce the speed of memory access in +# the VM. +# +# Default false +#reclaim_guest_freed_memory = true + # Default memory size in MiB for SB/VM. # If unspecified then it will be set @DEFMEMSZ@ MiB. default_memory = @DEFMEMSZ@ diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs b/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs index 758cd92bf..94684738f 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs @@ -1793,6 +1793,10 @@ impl<'a> QemuCmdLine<'a> { qemu_cmd_line.add_scsi_controller(); } + if config.device_info.reclaim_guest_freed_memory { + qemu_cmd_line.add_virtio_balloon(); + } + Ok(qemu_cmd_line) } @@ -2007,6 +2011,11 @@ impl<'a> QemuCmdLine<'a> { self.devices.push(Box::new(console_socket_chardev)); } + pub fn add_virtio_balloon(&mut self) { + let balloon_device = DeviceVirtioBalloon::new(); + self.devices.push(Box::new(balloon_device)); + } + pub async fn build(&self) -> Result> { let mut result = Vec::new(); @@ -2071,3 +2080,22 @@ fn get_devno_ccw(ccw_subchannel: &mut Option, device_name: &str) ) }) } + +#[derive(Debug)] +struct DeviceVirtioBalloon {} + +impl DeviceVirtioBalloon { + fn new() -> Self { + DeviceVirtioBalloon {} + } +} + +#[async_trait] +impl ToQemuParams for DeviceVirtioBalloon { + async fn qemu_params(&self) -> Result> { + Ok(vec![ + "-device".to_owned(), + "virtio-balloon,free-page-reporting=on".to_owned(), + ]) + } +}