Skip to content

Commit

Permalink
Merge pull request kata-containers#10711 from teawater/balloon
Browse files Browse the repository at this point in the history
Add reclaim_guest_freed_memory config to qemu and cloud-hypervisor
  • Loading branch information
gkurz authored Jan 22, 2025
2 parents 4e9d136 + 185b94b commit 17d053f
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/libs/kata-types/src/config/hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions src/runtime-rs/config/configuration-cloud-hypervisor.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions src/runtime-rs/config/configuration-dragonball.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/runtime-rs/config/configuration-qemu-runtime-rs.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@
Expand Down
10 changes: 10 additions & 0 deletions src/runtime-rs/crates/hypervisor/ch-config/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ impl TryFrom<NamedHypervisorConfig> 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,
Expand All @@ -193,6 +202,7 @@ impl TryFrom<NamedHypervisorConfig> for VmConfig {
vsock: Some(vsock),
rng,
platform,
balloon,

..Default::default()
};
Expand Down
4 changes: 2 additions & 2 deletions src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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<Vec<String>> {
let mut result = Vec::new();

Expand Down Expand Up @@ -2071,3 +2080,22 @@ fn get_devno_ccw(ccw_subchannel: &mut Option<CcwSubChannel>, 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<Vec<String>> {
Ok(vec![
"-device".to_owned(),
"virtio-balloon,free-page-reporting=on".to_owned(),
])
}
}
2 changes: 2 additions & 0 deletions tools/packaging/kernel/configs/fragments/common/virtio.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ CONFIG_VIRTIO_BLK=y
CONFIG_TTY=y
CONFIG_VIRTIO_CONSOLE=y
CONFIG_VIRTIO_NET=y

CONFIG_VIRTIO_BALLOON=y
2 changes: 1 addition & 1 deletion tools/packaging/kernel/kata_config_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142
143

0 comments on commit 17d053f

Please sign in to comment.