Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions openvmm/openvmm_core/src/worker/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4185,7 +4185,12 @@ impl LoadedVm {
rpc.complete(Err(RemoteError::new(start_error.context(
"worker restart failed and the VM could not resume",
))));
continue;
// The VM cannot resume, but it is
// still marked running while its
// state units are stopped, so a later
// pause would stop them again. Tear
// the VM down instead.
break;
}
}
rpc.complete(Err(RemoteError::new(err)));
Expand Down Expand Up @@ -4623,7 +4628,13 @@ impl LoadedVm {
}

device_removal.drain().await;
self.pause().await;
// Stop the state units whenever they run, not only when the VM does:
// after a failed state change, such as a restart that could not resume
// the VM, the two can disagree.
if self.state_units.is_running() {
self.state_units.stop().await;
}
self.running = false;
self.teardown_chipset_devices().await;
self.inner.partition_unit.teardown().await;
if let Some(vmbus) = self.inner.vmbus_server {
Expand Down
4 changes: 4 additions & 0 deletions vmm_core/state_unit/src/advance_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ impl StateUnits {
/// Only units added with [`UnitBuilder::advances_time`](crate::UnitBuilder::advances_time)
/// receive the request; the others pass through the operation without a
/// round trip.
///
/// Fails without sending a request while a unit is in a terminal state,
/// whether or not the units run. Otherwise panics if running.
pub async fn advance_time(&mut self, duration: Duration) -> Result<(), StateTransitionError> {
self.check_no_terminal_units("advance_time")?;
assert!(!self.running);
let results = self
.run_op(
Expand Down
59 changes: 56 additions & 3 deletions vmm_core/state_unit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,28 @@ impl StateUnits {
self.running
}

/// Fails `op`, naming each unit in a terminal state, if any unit is in
/// one.
fn check_no_terminal_units(&self, op: &'static str) -> Result<(), StateTransitionError> {
let inner = self.inner.lock();
check(
op,
inner
.units
.values()
.filter(|unit| matches!(unit.state, State::StartUncertain))
.map(|unit| {
(
unit.name.clone(),
Err::<(), _>(anyhow::anyhow!(
"unit is in terminal state {:?}",
unit.state
)),
)
}),
)
}

/// Starts any units that are individually stopped, because they were added
/// via [`StateUnits::add`] while the VM was running.
///
Expand All @@ -571,8 +593,25 @@ impl StateUnits {
}

/// Stops all the state units.
///
/// This also stops the units whose start is uncertain, since they may be
/// running, and leaves them in that terminal state.
pub async fn stop(&mut self) {
assert!(self.running);
// Make the units whose start is uncertain receive the stop, then
// restore their terminal state after the ordered operation.
let uncertain_ids = {
let mut inner = self.inner.lock();
inner
.units
.iter_mut()
.filter(|(_, unit)| unit.state == State::StartUncertain)
.map(|(&id, unit)| {
unit.state = State::Running;
id
})
.collect::<Vec<_>>()
};
// Stop units in reverse dependency order so that a dependency is not
// stopped before its dependant.
self.run_op(
Expand All @@ -586,13 +625,22 @@ impl StateUnits {
|unit| Some(&unit.dependents),
)
.await;
let mut inner = self.inner.lock();
for id in uncertain_ids {
if let Some(unit) = inner.units.get_mut(&id) {
assert_eq!(unit.state, State::Stopped);
unit.state = State::StartUncertain;
}
}
self.running = false;
}

/// Resets all the state units.
///
/// Panics if running.
/// Fails without sending a request while a unit is in a terminal state,
/// whether or not the units run. Otherwise panics if running.
pub async fn reset(&mut self) -> Result<(), StateTransitionError> {
self.check_no_terminal_units("reset")?;
assert!(!self.running);
self.restore_allowed = false;
// Reset in dependency order so that dependants observe their
Expand All @@ -617,8 +665,10 @@ impl StateUnits {

/// Saves all the state units.
///
/// Panics if running.
/// Fails without sending a request while a unit is in a terminal state,
/// whether or not the units run. Otherwise panics if running.
pub async fn save(&mut self) -> Result<Vec<SavedStateUnit>, StateTransitionError> {
self.check_no_terminal_units("save")?;
assert!(!self.running);
// Save can occur in any order since it will not observably mutate
// state.
Expand Down Expand Up @@ -652,11 +702,13 @@ impl StateUnits {
/// unit-name validation errors leave eligibility unchanged. Saving does not
/// consume eligibility, and saving restored state before start is supported.
///
/// Panics if running.
/// Fails without sending a request while a unit is in a terminal state,
/// whether or not the units run. Otherwise panics if running.
pub async fn restore(
&mut self,
states: Vec<SavedStateUnit>,
) -> Result<(), StateTransitionError> {
self.check_no_terminal_units("restore")?;
assert!(!self.running);
if !self.restore_allowed {
return Err(StateTransitionError {
Expand Down Expand Up @@ -1145,6 +1197,7 @@ mod tests {
mod quiesce;
mod slow;
mod start;
mod terminal;

use super::StateTransitionErrorKind;
use super::StateUnit;
Expand Down
6 changes: 4 additions & 2 deletions vmm_core/state_unit/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ impl StateUnits {
/// not start or did not confirm its start. If a unit fails to confirm its
/// start, or its task does not answer its start or confirmation request,
/// the units that did start, the failed unit among them, are stopped
/// again. A unit whose task does not answer is left in a terminal state
/// that fails later starts; a unit that failed to confirm its start can
/// again. A unit whose task does not answer is left in a terminal state:
/// while it is in that state, later starts, resets, saves, restores, and
/// time advances fail, and a stop stops it, since it may be running, but
/// leaves it in that state. A unit that failed to confirm its start can
/// start again.
///
/// [`StateUnit::confirm_start`]: crate::StateUnit::confirm_start
Expand Down
Loading
Loading