Skip to content

Commit b2bdff5

Browse files
committed
refactor(acpi): Encapsule post-restore logic within restore()
Since the restore and its post-restore steps must always be executed together. Move the post-restore logic into restore() so the flow is self-contained. This prevents the restore and its post-restore handling from being accidentally separated in the future. Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
1 parent e9d0106 commit b2bdff5

5 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/vmm/src/device_manager/acpi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub enum ACPIDeviceError {
1616
RegisterIrq(#[from] kvm_ioctls::Error),
1717
/// Could not write to guest memory: {0}
1818
WriteGuestMemory(#[from] GuestMemoryError),
19+
/// Could not notify guest: {0}
20+
NotifyGuest(#[from] std::io::Error),
1921
}
2022

2123
#[derive(Debug)]

src/vmm/src/device_manager/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,6 @@ pub enum DevicePersistError {
412412
AcpiRestore(#[from] ACPIDeviceError),
413413
/// Error restoring PCI devices: {0}
414414
PciRestore(#[from] PciManagerError),
415-
/// Error notifying VMGenID device: {0}
416-
VmGenidUpdate(#[from] std::io::Error),
417415
/// Error resetting serial console: {0}
418416
SerialRestore(#[from] EmulateSerialInitError),
419417
/// Error inserting device in bus: {0}
@@ -479,11 +477,7 @@ impl<'a> Persist<'a> for DeviceManager {
479477
let mmio_devices = MMIODeviceManager::restore(mmio_ctor_args, &state.mmio_state)?;
480478

481479
// Restore ACPI devices
482-
let mut acpi_devices = ACPIDeviceManager::restore(constructor_args.vm, &state.acpi_state)?;
483-
acpi_devices.vmgenid.notify_guest()?;
484-
acpi_devices
485-
.vmclock
486-
.post_load_update(constructor_args.vm.guest_memory());
480+
let acpi_devices = ACPIDeviceManager::restore(constructor_args.vm, &state.acpi_state)?;
487481

488482
// Restore PCI devices
489483
let pci_ctor_args = PciDevicesConstructorArgs {

src/vmm/src/device_manager/persist.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,19 @@ impl<'a> Persist<'a> for ACPIDeviceManager {
183183
}
184184

185185
fn restore(vm: Self::ConstructorArgs, state: &Self::State) -> Result<Self, Self::Error> {
186-
let acpi_devices = ACPIDeviceManager {
186+
let mut acpi_devices = ACPIDeviceManager {
187187
// Safe to unwrap() here, this will never return an error.
188188
vmgenid: VmGenId::restore((), &state.vmgenid).unwrap(),
189189
// Safe to unwrap() here, this will never return an error.
190190
vmclock: VmClock::restore((), &state.vmclock).unwrap(),
191191
};
192192

193193
acpi_devices.attach_vmgenid(vm)?;
194+
acpi_devices.vmgenid.do_post_restore()?;
195+
194196
acpi_devices.attach_vmclock(vm)?;
197+
acpi_devices.vmclock.do_post_restore(vm.guest_memory());
198+
195199
Ok(acpi_devices)
196200
}
197201
}

src/vmm/src/devices/acpi/vmclock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ impl VmClock {
104104
Ok(())
105105
}
106106

107-
/// Bump the VM generation counter
108-
pub fn post_load_update(&mut self, mem: &GuestMemoryMmap) {
107+
/// Bump the VM generation counter and notify guest after snapshot restore
108+
pub fn do_post_restore(&mut self, mem: &GuestMemoryMmap) {
109109
write_vmclock_field!(self, mem, seq_count, self.inner.seq_count | 1);
110110

111111
// This fence ensures guest sees all previous writes. It is matched to a
@@ -263,7 +263,7 @@ mod tests {
263263

264264
let state = vmclock.save();
265265
let mut vmclock_new = VmClock::restore((), &state).unwrap();
266-
vmclock_new.post_load_update(&mem);
266+
vmclock_new.do_post_restore(&mem);
267267

268268
let guest_data_new: vmclock_abi = mem.read_obj(VMCLOCK_TEST_GUEST_ADDR).unwrap();
269269
assert_ne!(guest_data_new, vmclock.inner);

src/vmm/src/devices/acpi/vmgenid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ impl VmGenId {
8484
u128::from_le_bytes(gen_id_bytes)
8585
}
8686

87-
/// Send an ACPI notification to guest device.
87+
/// Notify guest after snapshot restore
8888
///
8989
/// This will only have effect if we have updated the generation ID in guest memory, i.e. when
9090
/// re-creating the device after snapshot resumption.
91-
pub fn notify_guest(&mut self) -> Result<(), std::io::Error> {
91+
pub fn do_post_restore(&self) -> Result<(), std::io::Error> {
9292
self.interrupt_evt
9393
.trigger()
9494
.inspect_err(|err| error!("vmgenid: could not send guest notification: {err}"))?;

0 commit comments

Comments
 (0)