Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hypervisor specific pause/resume API #6441

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions hypervisor/src/mshv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ impl hypervisor::Hypervisor for MshvHypervisor {
)
.map_err(|e| hypervisor::HypervisorError::SetPartitionProperty(e.into()))?;

// Always create a frozen partition
fd.set_partition_property(
russell-islam marked this conversation as resolved.
Show resolved Hide resolved
hv_partition_property_code_HV_PARTITION_PROPERTY_TIME_FREEZE,
1u64,
)
.map_err(|e| hypervisor::HypervisorError::SetPartitionProperty(e.into()))?;

let vm_fd = Arc::new(fd);

#[cfg(target_arch = "x86_64")]
Expand Down Expand Up @@ -2076,4 +2083,36 @@ impl vm::Vm for MshvVm {
fn get_preferred_target(&self, kvi: &mut VcpuInit) -> vm::Result<()> {
unimplemented!()
}

/// Pause the VM
fn pause(&self) -> vm::Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the mshv file - is this #[cfg(feature = "mshv")] needed?

// Freeze the partition
self.fd
.set_partition_property(
hv_partition_property_code_HV_PARTITION_PROPERTY_TIME_FREEZE,
1u64,
)
.map_err(|e| {
vm::HypervisorVmError::SetVmProperty(anyhow!(
"Failed to set partition property: {}",
e
))
})
}

/// Resume the VM
fn resume(&self) -> vm::Result<()> {
// Resuming the partition using TIME_FREEZE property
self.fd
.set_partition_property(
hv_partition_property_code_HV_PARTITION_PROPERTY_TIME_FREEZE,
0u64,
)
.map_err(|e| {
vm::HypervisorVmError::SetVmProperty(anyhow!(
"Failed to set partition property: {}",
e
))
})
}
}
14 changes: 14 additions & 0 deletions hypervisor/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ pub enum HypervisorVmError {
///
#[error("Failed to complete isolated import: {0}")]
CompleteIsolatedImport(#[source] anyhow::Error),
/// Failed to set VM property
///
#[error("Failed to set VM property: {0}")]
SetVmProperty(#[source] anyhow::Error),
}
///
/// Result type for returning from a function
Expand Down Expand Up @@ -390,6 +394,16 @@ pub trait Vm: Send + Sync + Any {
) -> Result<()> {
unimplemented!()
}

/// Pause the VM
fn pause(&self) -> Result<()> {
Ok(())
}

/// Resume the VM
fn resume(&self) -> Result<()> {
Ok(())
}
}

pub trait VmOps: Send + Sync {
Expand Down
20 changes: 20 additions & 0 deletions vmm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ pub enum Error {

#[error("Error injecting NMI")]
ErrorNmi,

#[error("Error resuming the VM: {0}")]
ResumeVm(#[source] hypervisor::HypervisorVmError),
}
pub type Result<T> = result::Result<T, Error>;

Expand Down Expand Up @@ -2173,6 +2176,11 @@ impl Vm {
self.vm.tdx_finalize().map_err(Error::FinalizeTdx)?;
}

// Resume the vm for MSHV
if current_state == VmState::Created {
self.vm.resume().map_err(Error::ResumeVm)?;
}

self.cpu_manager
.lock()
.unwrap()
Expand Down Expand Up @@ -2487,6 +2495,10 @@ impl Pausable for Vm {
self.cpu_manager.lock().unwrap().pause()?;
self.device_manager.lock().unwrap().pause()?;

self.vm
.pause()
.map_err(|e| MigratableError::Pause(anyhow!("Could not pause the VM: {}", e)))?;

*state = new_state;

event!("vm", "paused");
Expand All @@ -2495,6 +2507,7 @@ impl Pausable for Vm {

fn resume(&mut self) -> std::result::Result<(), MigratableError> {
event!("vm", "resuming");
let current_state = self.get_state().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can cause a panic - please propagate properly using ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I look at it - handling of this seems inconsistent in this file...

let mut state = self
.state
.try_write()
Expand All @@ -2514,6 +2527,13 @@ impl Pausable for Vm {
})?;
}
}

if current_state == VmState::Paused {
self.vm
.resume()
.map_err(|e| MigratableError::Resume(anyhow!("Could not resume the VM: {}", e)))?;
}

self.device_manager.lock().unwrap().resume()?;

// And we're back to the Running state.
Expand Down