runtime-rs: make the resize_vcpu sync

When hot plugging vcpu in dragonball hypervisor, use the synchronization
interface and wait until the hot plug cpu is executed in the guest
before returning. This ensures that the subsequent device hot plug will
not conflict with the previous call.

Signed-off-by: Fupan Li <fupan.lfp@antgroup.com>
This commit is contained in:
Fupan Li
2025-07-03 11:59:11 +08:00
committed by Fupan Li
parent 72a38457f0
commit fb1c35335a
2 changed files with 19 additions and 4 deletions

View File

@@ -28,7 +28,7 @@ use kata_types::{
}; };
use nix::mount::MsFlags; use nix::mount::MsFlags;
use persist::sandbox_persist::Persist; use persist::sandbox_persist::Persist;
use std::cmp::Ordering; use std::{cmp::Ordering, time::Duration};
use std::{collections::HashSet, fs::create_dir_all}; use std::{collections::HashSet, fs::create_dir_all};
use tokio::sync::mpsc; use tokio::sync::mpsc;
@@ -37,6 +37,9 @@ const DRAGONBALL_INITRD: &str = "initrd";
const DRAGONBALL_ROOT_FS: &str = "rootfs"; const DRAGONBALL_ROOT_FS: &str = "rootfs";
const BALLOON_DEVICE_ID: &str = "balloon0"; const BALLOON_DEVICE_ID: &str = "balloon0";
const MEM_DEVICE_ID: &str = "memmr0"; const MEM_DEVICE_ID: &str = "memmr0";
/// default hotplug timeout
const DEFAULT_HOTPLUG_TIMEOUT: u64 = 250;
#[derive(Debug)] #[derive(Debug)]
pub struct DragonballInner { pub struct DragonballInner {
/// sandbox id /// sandbox id
@@ -391,7 +394,10 @@ impl DragonballInner {
vcpu_count: Some(new_vcpus as u8), vcpu_count: Some(new_vcpus as u8),
}; };
self.vmm_instance self.vmm_instance
.resize_vcpu(&cpu_resize_info) .resize_vcpu(
&cpu_resize_info,
Some(Duration::from_millis(DEFAULT_HOTPLUG_TIMEOUT)),
)
.context(format!( .context(format!(
"failed to do_resize_vcpus on new_vcpus={:?}", "failed to do_resize_vcpus on new_vcpus={:?}",
new_vcpus new_vcpus

View File

@@ -9,6 +9,7 @@ use std::{
os::unix::{io::IntoRawFd, prelude::AsRawFd}, os::unix::{io::IntoRawFd, prelude::AsRawFd},
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
thread, thread,
time::Duration,
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
@@ -292,9 +293,17 @@ impl VmmInstance {
Ok(()) Ok(())
} }
pub fn resize_vcpu(&self, cfg: &VcpuResizeInfo) -> Result<()> { pub fn resize_vcpu(&self, cfg: &VcpuResizeInfo, timeout: Option<Duration>) -> Result<()> {
self.handle_request(Request::Sync(VmmAction::ResizeVcpu(cfg.clone()))) let vmmdata = self
.handle_request(Request::Sync(VmmAction::ResizeVcpu(cfg.clone())))
.with_context(|| format!("Failed to resize_vm(hotplug vcpu), cfg: {:?}", cfg))?; .with_context(|| format!("Failed to resize_vm(hotplug vcpu), cfg: {:?}", cfg))?;
if let Some(timeout) = timeout {
if let VmmData::SyncHotplug((_, receiver)) = vmmdata {
let _ = receiver.recv_timeout(timeout)?;
}
}
Ok(()) Ok(())
} }