Skip to content

Commit bbadadb

Browse files
committed
refactor: move GuestMemoryState into VmState
Since the guest memory is now stored in `struct Vm`, we can more easily store the guest memory state inside the VmState. Signed-off-by: Patrick Roy <[email protected]>
1 parent 9e15173 commit bbadadb

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

src/vmm/src/arch/aarch64/vm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
66
use crate::Kvm;
77
use crate::arch::aarch64::gic::GicState;
88
use crate::vstate::vm::{VmCommon, VmError};
9+
use crate::vstate::memory::{GuestMemoryExtension, GuestMemoryState};
910

1011
/// Structure representing the current architecture's understand of what a "virtual machine" is.
1112
#[derive(Debug)]
@@ -68,6 +69,7 @@ impl ArchVm {
6869
/// Saves and returns the Kvm Vm state.
6970
pub fn save_state(&self, mpidrs: &[u64]) -> Result<VmState, ArchVmError> {
7071
Ok(VmState {
72+
memory: self.common.guest_memory.describe(),
7173
gic: self
7274
.get_irqchip()
7375
.save_device(mpidrs)
@@ -84,13 +86,16 @@ impl ArchVm {
8486
self.get_irqchip()
8587
.restore_device(mpidrs, &state.gic)
8688
.map_err(ArchVmError::RestoreGic)?;
89+
8790
Ok(())
8891
}
8992
}
9093

9194
/// Structure holding an general specific VM state.
9295
#[derive(Debug, Default, Serialize, Deserialize)]
9396
pub struct VmState {
97+
/// Guest memory state
98+
pub memory: GuestMemoryState,
9499
/// GIC state.
95100
pub gic: GicState,
96101
}

src/vmm/src/arch/x86_64/vm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
1212

1313
use crate::arch::x86_64::msr::MsrError;
1414
use crate::utils::u64_to_usize;
15+
use crate::vstate::memory::{GuestMemoryExtension, GuestMemoryState};
1516
use crate::vstate::vm::{VmCommon, VmError};
1617

1718
/// Error type for [`Vm::restore_state`]
@@ -185,6 +186,7 @@ impl ArchVm {
185186
.map_err(ArchVmError::VmGetIrqChip)?;
186187

187188
Ok(VmState {
189+
memory: self.common.guest_memory.describe(),
188190
pitstate,
189191
clock,
190192
pic_master,
@@ -207,6 +209,8 @@ impl ArchVm {
207209
#[derive(Default, Deserialize, Serialize)]
208210
/// Structure holding VM kvm state.
209211
pub struct VmState {
212+
/// guest memory state
213+
pub memory: GuestMemoryState,
210214
pitstate: kvm_pit_state2,
211215
clock: kvm_clock_data,
212216
// TODO: rename this field to adopt inclusive language once Linux updates it, too.

src/vmm/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ use crate::rate_limiter::BucketUpdate;
148148
use crate::snapshot::Persist;
149149
use crate::utils::u64_to_usize;
150150
use crate::vmm_config::instance_info::{InstanceInfo, VmState};
151-
use crate::vstate::memory::{
152-
GuestMemory, GuestMemoryExtension, GuestMemoryMmap, GuestMemoryRegion,
153-
};
151+
use crate::vstate::memory::{GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
154152
use crate::vstate::vcpu::VcpuState;
155153
pub use crate::vstate::vcpu::{Vcpu, VcpuConfig, VcpuEvent, VcpuHandle, VcpuResponse};
156154
pub use crate::vstate::vm::Vm;
@@ -529,12 +527,10 @@ impl Vmm {
529527
};
530528
let device_states = self.mmio_device_manager.save();
531529

532-
let memory_state = self.guest_memory().describe();
533530
let acpi_dev_state = self.acpi_device_manager.save();
534531

535532
Ok(MicrovmState {
536533
vm_info: vm_info.clone(),
537-
memory_state,
538534
kvm_state,
539535
vm_state,
540536
vcpu_states,

src/vmm/src/persist.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ impl From<&VmResources> for VmInfo {
7575
pub struct MicrovmState {
7676
/// Miscellaneous VM info.
7777
pub vm_info: VmInfo,
78-
/// Memory state.
79-
pub memory_state: GuestMemoryState,
8078
/// KVM KVM state.
8179
pub kvm_state: KvmState,
8280
/// VM KVM state.
@@ -367,7 +365,7 @@ pub fn snapshot_state_sanity_check(
367365
// Check if the snapshot contains at least 1 mem region.
368366
// Upper bound check will be done when creating guest memory by comparing against
369367
// KVM max supported value kvm_context.max_memslots().
370-
if microvm_state.memory_state.regions.is_empty() {
368+
if microvm_state.vm_state.memory.regions.is_empty() {
371369
return Err(SnapShotStateSanityCheckError::NoMemory);
372370
}
373371

@@ -450,7 +448,7 @@ pub fn restore_from_snapshot(
450448
snapshot_state_sanity_check(&microvm_state)?;
451449

452450
let mem_backend_path = &params.mem_backend.backend_path;
453-
let mem_state = &microvm_state.memory_state;
451+
let mem_state = &microvm_state.vm_state.memory;
454452

455453
let (guest_memory, uffd) = match params.mem_backend.backend_type {
456454
MemBackendType::File => {
@@ -745,13 +743,11 @@ mod tests {
745743
assert!(states.vsock_device.is_some());
746744
assert!(states.balloon_device.is_some());
747745

748-
let memory_state = vmm.guest_memory().describe();
749746
let vcpu_states = vec![VcpuState::default()];
750747
#[cfg(target_arch = "aarch64")]
751748
let mpidrs = construct_kvm_mpidrs(&vcpu_states);
752749
let microvm_state = MicrovmState {
753750
device_states: states,
754-
memory_state,
755751
vcpu_states,
756752
kvm_state: Default::default(),
757753
vm_info: VmInfo {

src/vmm/src/vstate/vm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ pub struct VmCommon {
2323
/// The KVM file descriptor used to access this Vm.
2424
pub fd: VmFd,
2525
max_memslots: usize,
26-
guest_memory: GuestMemoryMmap,
26+
/// The guest memory of this Vm.
27+
pub guest_memory: GuestMemoryMmap,
2728
}
2829

2930
/// Errors associated with the wrappers over KVM ioctls.

src/vmm/tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ fn test_snapshot_load_sanity_checks() {
299299
snapshot_state_sanity_check(&microvm_state).unwrap();
300300

301301
// Remove memory regions.
302-
microvm_state.memory_state.regions.clear();
302+
microvm_state.vm_state.memory.regions.clear();
303303

304304
// Validate sanity checks fail because there is no mem region in state.
305305
assert_eq!(

0 commit comments

Comments
 (0)