diff --git a/src/libs/kata-types/src/config/hypervisor/mod.rs b/src/libs/kata-types/src/config/hypervisor/mod.rs index ab0a38aaa4cd..ed9312f93fb4 100644 --- a/src/libs/kata-types/src/config/hypervisor/mod.rs +++ b/src/libs/kata-types/src/config/hypervisor/mod.rs @@ -499,7 +499,7 @@ pub struct DeviceInfo { /// /// Enabling this will result in the VM balloon device having f_reporting=on set #[serde(default)] - pub enable_balloon_f_reporting: bool, + pub reclaim_guest_freed_memory: bool, } impl DeviceInfo { diff --git a/src/runtime-rs/config/configuration-cloud-hypervisor.toml.in b/src/runtime-rs/config/configuration-cloud-hypervisor.toml.in index 0cb17abb2fb8..726a2b33ada3 100644 --- a/src/runtime-rs/config/configuration-cloud-hypervisor.toml.in +++ b/src/runtime-rs/config/configuration-cloud-hypervisor.toml.in @@ -158,6 +158,16 @@ virtio_fs_cache = "@DEFVIRTIOFSCACHE@" # > 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 + # Block storage driver to be used for the hypervisor in case the container # rootfs is backed by a block device. block_device_driver = "virtio-blk-pci" diff --git a/src/runtime-rs/config/configuration-dragonball.toml.in b/src/runtime-rs/config/configuration-dragonball.toml.in index 07c943e71184..e2dc7ae856cb 100644 --- a/src/runtime-rs/config/configuration-dragonball.toml.in +++ b/src/runtime-rs/config/configuration-dragonball.toml.in @@ -98,11 +98,15 @@ default_maxvcpus = @DEFMAXVCPUS_DB@ # > 5 --> will be set to 5 default_bridges = @DEFBRIDGES@ -# Enable balloon f_reporting -# Enabling this will result in the VM balloon device having f_reporting=on set +# 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 -#enable_balloon_f_reporting = true +#reclaim_guest_freed_memory = true # Default memory size in MiB for SB/VM. # If unspecified then it will be set @DEFMEMSZ@ MiB. 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 3e3b3a1ac2eb..f17eed5171e1 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/ch-config/src/convert.rs b/src/runtime-rs/crates/hypervisor/ch-config/src/convert.rs index cdc1eef09c4d..fa8dfded4928 100644 --- a/src/runtime-rs/crates/hypervisor/ch-config/src/convert.rs +++ b/src/runtime-rs/crates/hypervisor/ch-config/src/convert.rs @@ -180,6 +180,15 @@ impl TryFrom for VmConfig { let platform = get_platform_cfg(guest_protection_to_use); + let balloon = if cfg.device_info.reclaim_guest_freed_memory { + Some(crate::BalloonConfig { + free_page_reporting: true, + ..Default::default() + }) + } else { + None + }; + let cfg = VmConfig { cpus, memory, @@ -193,6 +202,7 @@ impl TryFrom for VmConfig { vsock: Some(vsock), rng, platform, + balloon, ..Default::default() }; diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs index 57613813dde6..92637e2bc4bf 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs @@ -430,7 +430,7 @@ impl DragonballInner { use_shared_irq: None, use_generic_irq: None, f_deflate_on_oom: false, - f_reporting: self.config.device_info.enable_balloon_f_reporting, + f_reporting: self.config.device_info.reclaim_guest_freed_memory, }; self.vmm_instance .insert_balloon_device(balloon_config) @@ -462,7 +462,7 @@ impl DragonballInner { use_shared_irq: None, use_generic_irq: None, f_deflate_on_oom: false, - f_reporting: self.config.device_info.enable_balloon_f_reporting, + f_reporting: self.config.device_info.reclaim_guest_freed_memory, }; self.balloon_size = had_mem_mb - new_mem_mb; self.vmm_instance diff --git a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs index 9b0a238242c7..4b85420ad233 100644 --- a/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs @@ -85,7 +85,7 @@ impl Hypervisor for Dragonball { let mut inner = self.inner.write().await; let ret = inner.start_vm(timeout).await; - if ret.is_ok() && inner.config.device_info.enable_balloon_f_reporting { + if ret.is_ok() && inner.config.device_info.reclaim_guest_freed_memory { // The virtio-balloon device must be inserted into dragonball and // recognized by the guest kernel only after the dragonball upcall is ready. // The dragonball upcall is not ready immediately after the VM starts, 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 758cd92bfe00..94684738f0c1 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(), + ]) + } +} diff --git a/tools/packaging/kernel/configs/fragments/common/virtio.conf b/tools/packaging/kernel/configs/fragments/common/virtio.conf index 8dea2f854c08..a96c2cebcf74 100644 --- a/tools/packaging/kernel/configs/fragments/common/virtio.conf +++ b/tools/packaging/kernel/configs/fragments/common/virtio.conf @@ -23,3 +23,5 @@ CONFIG_VIRTIO_BLK=y CONFIG_TTY=y CONFIG_VIRTIO_CONSOLE=y CONFIG_VIRTIO_NET=y + +CONFIG_VIRTIO_BALLOON=y diff --git a/tools/packaging/kernel/kata_config_version b/tools/packaging/kernel/kata_config_version index aaacbe662910..fba7ed526e43 100644 --- a/tools/packaging/kernel/kata_config_version +++ b/tools/packaging/kernel/kata_config_version @@ -1 +1 @@ -142 +143