runtime-rs: check the update memory size

check the update memory size greater than default max memory size

Fixes:#6875
Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
This commit is contained in:
Zhongtao Hu
2023-06-21 18:10:38 +08:00
parent 6039417104
commit 9a37e77f2a
15 changed files with 201 additions and 122 deletions

View File

@@ -756,7 +756,7 @@ impl CloudHypervisorInner {
todo!()
}
pub(crate) fn resize_memory(&self, _req_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
pub(crate) fn resize_memory(&self, _new_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
todo!()
}
}

View File

@@ -178,9 +178,9 @@ impl Hypervisor for CloudHypervisor {
inner.guest_memory_block_size_mb()
}
async fn resize_memory(&self, req_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
async fn resize_memory(&self, new_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
let inner = self.inner.read().await;
inner.resize_memory(req_mem_mb)
inner.resize_memory(new_mem_mb)
}
}

View File

@@ -32,7 +32,8 @@ use std::{collections::HashSet, fs::create_dir_all};
const DRAGONBALL_KERNEL: &str = "vmlinux";
const DRAGONBALL_ROOT_FS: &str = "rootfs";
const BALLOON_DEVICE_ID: &str = "balloon0";
const MEM_DEVICE_ID: &str = "memmr0";
#[derive(Debug)]
pub struct DragonballInner {
/// sandbox id
@@ -76,6 +77,9 @@ pub struct DragonballInner {
/// the hotplug memory size
pub(crate) mem_hotplug_size_mb: u32,
/// the balloon size
pub(crate) balloon_size: u32,
}
impl DragonballInner {
@@ -85,7 +89,8 @@ impl DragonballInner {
CapabilityBits::BlockDeviceSupport
| CapabilityBits::BlockDeviceHotplugSupport
| CapabilityBits::FsSharingSupport
| CapabilityBits::HybridVsockSupport,
| CapabilityBits::HybridVsockSupport
| CapabilityBits::GuestMemoryProbe,
);
DragonballInner {
id: "".to_string(),
@@ -102,6 +107,7 @@ impl DragonballInner {
capabilities,
guest_memory_block_size_mb: 0,
mem_hotplug_size_mb: 0,
balloon_size: 0,
}
}
@@ -349,33 +355,51 @@ impl DragonballInner {
Ok((old_vcpus, new_vcpus))
}
pub(crate) fn resize_memory(&mut self, req_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
pub(crate) fn resize_memory(&mut self, new_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
// check the invalid request memory
if new_mem_mb > self.hypervisor_config().memory_info.default_maxmemory {
warn!(
sl!(),
"memory size unchanged, the request memory size {} is greater than the max memory size {}",
new_mem_mb, self.hypervisor_config().memory_info.default_maxmemory
);
return Ok((
0,
MemoryConfig {
..Default::default()
},
));
}
let had_mem_mb = self.config.memory_info.default_memory + self.mem_hotplug_size_mb;
match req_mem_mb.cmp(&had_mem_mb) {
match new_mem_mb.cmp(&had_mem_mb) {
Ordering::Greater => {
// clean virtio-ballon device before hotplug memory, resize to 0
let balloon_config = BalloonDeviceConfigInfo {
balloon_id: "balloon0".to_owned(),
size_mib: 0,
use_shared_irq: None,
use_generic_irq: None,
f_deflate_on_oom: false,
f_reporting: false,
};
self.vmm_instance
.insert_balloon_device(balloon_config)
.context("failed to insert balloon device")?;
if self.balloon_size > 0 {
let balloon_config = BalloonDeviceConfigInfo {
balloon_id: BALLOON_DEVICE_ID.to_owned(),
size_mib: 0,
use_shared_irq: None,
use_generic_irq: None,
f_deflate_on_oom: false,
f_reporting: false,
};
self.vmm_instance
.insert_balloon_device(balloon_config)
.context("failed to insert balloon device")?;
}
// update the hotplug size
self.mem_hotplug_size_mb = req_mem_mb - self.config.memory_info.default_memory;
self.mem_hotplug_size_mb = new_mem_mb - self.config.memory_info.default_memory;
self.balloon_size = 0;
// insert a new memory device
let add_mem_mb = req_mem_mb - had_mem_mb;
let add_mem_mb = new_mem_mb - had_mem_mb;
self.vmm_instance.insert_mem_device(MemDeviceConfigInfo {
mem_id: format!("mem{}", self.mem_hotplug_size_mb),
mem_id: MEM_DEVICE_ID.to_owned(),
size_mib: add_mem_mb as u64,
capacity_mib: add_mem_mb as u64,
multi_region: false,
capacity_mib: self.hypervisor_config().memory_info.default_maxmemory as u64,
multi_region: true,
host_numa_node_id: None,
guest_numa_node_id: None,
use_shared_irq: None,
@@ -384,15 +408,16 @@ impl DragonballInner {
}
Ordering::Less => {
// we only use one balloon device here, and resize it to release memory
// the operation we do here is inserting a new balloon0 device or resizing it
// the operation we do here is inserting a new BALLOON_DEVICE_ID device or resizing it
let balloon_config = BalloonDeviceConfigInfo {
balloon_id: "balloon0".to_owned(),
size_mib: (had_mem_mb - req_mem_mb) as u64,
balloon_id: BALLOON_DEVICE_ID.to_owned(),
size_mib: (had_mem_mb - new_mem_mb) as u64,
use_shared_irq: None,
use_generic_irq: None,
f_deflate_on_oom: false,
f_reporting: false,
};
self.balloon_size = had_mem_mb - new_mem_mb;
self.vmm_instance
.insert_balloon_device(balloon_config)
.context("failed to insert balloon device")?;
@@ -407,7 +432,7 @@ impl DragonballInner {
};
Ok((
req_mem_mb,
new_mem_mb,
MemoryConfig {
..Default::default()
},
@@ -476,6 +501,7 @@ impl Persist for DragonballInner {
capabilities: Capabilities::new(),
guest_memory_block_size_mb: 0,
mem_hotplug_size_mb: 0,
balloon_size: 0,
})
}
}

View File

@@ -194,9 +194,9 @@ impl Hypervisor for Dragonball {
inner.guest_memory_block_size_mb()
}
async fn resize_memory(&self, req_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
async fn resize_memory(&self, new_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
let mut inner = self.inner.write().await;
inner.resize_memory(req_mem_mb)
inner.resize_memory(new_mem_mb)
}
}

View File

@@ -91,7 +91,7 @@ pub trait Hypervisor: std::fmt::Debug + Send + Sync {
async fn save_vm(&self) -> Result<()>;
async fn resume_vm(&self) -> Result<()>;
async fn resize_vcpu(&self, old_vcpus: u32, new_vcpus: u32) -> Result<(u32, u32)>; // returns (old_vcpus, new_vcpus)
async fn resize_memory(&self, req_mem_mb: u32) -> Result<(u32, MemoryConfig)>;
async fn resize_memory(&self, new_mem_mb: u32) -> Result<(u32, MemoryConfig)>;
// device manager
async fn add_device(&self, device: DeviceType) -> Result<DeviceType>;

View File

@@ -153,7 +153,7 @@ impl QemuInner {
todo!()
}
pub(crate) fn resize_memory(&self, _req_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
pub(crate) fn resize_memory(&self, _new_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
todo!()
}
}

View File

@@ -173,8 +173,8 @@ impl Hypervisor for Qemu {
inner.guest_memory_block_size_mb()
}
async fn resize_memory(&self, req_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
async fn resize_memory(&self, new_mem_mb: u32) -> Result<(u32, MemoryConfig)> {
let inner = self.inner.read().await;
inner.resize_memory(req_mem_mb)
inner.resize_memory(new_mem_mb)
}
}