From 0e02b3a9ef98e369256588f044f1f28ee322cf05 Mon Sep 17 00:00:00 2001 From: Nikhil Unni Date: Sun, 28 Jun 2026 09:17:53 -0700 Subject: [PATCH] Retire the v1 MAP_SHARED/Msync snapshot surface (superseded by v2 substrate + v3 vmstate-only) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The engrams live-migration fork landed in three commits on upstream v1.16.0: v1 (5ca2fba4 — MAP_SHARED memory + Msync/MsyncAndState off-pause flush), v2 (MAP_PRIVATE base-file + MISSING|MINOR substrate), and v3 (vmstate_only create param). v1's MAP_SHARED/msync design was superseded by the v2 UFFD substrate and the v3 post-copy path, and is dead: nothing emits it (the engrams host's SnapshotType is {Full, Diff}; there is no `shared` load param on the host side). engrams ADR 0045 D1 marked it for retirement. Remove the v1 surface (a revert of 5ca2fba4 reconciled with v2/v3): - SnapshotType::{Msync, MsyncAndState}. - The `shared` load param (LoadSnapshotParams/LoadSnapshotConfig + swagger). - vstate/memory.rs `msync()` + the `shared` MAP_PRIVATE->MAP_SHARED flag on `snapshot_file` (back to the upstream 3-arg, MAP_PRIVATE signature). - vstate/vm.rs msync dispatch in `snapshot_memory_to_file`. - persist.rs Msync state-file skip + the now-vacuous vmstate_only-vs-Msync guard (vmstate_only combines only with Full/Diff, the only remaining types). - api_server + rpc_interface surface; the integration test for the dead guard. v2 (substrate) and v3 (vmstate_only) are preserved intact; the v2 base-file path that passed `snapshot_file(.., shared=false)` now calls the reverted 3-arg signature (MAP_PRIVATE — its existing behavior). No change to src/vmm/src/snapshot/ and no SNAPSHOT_VERSION bump — byte-compatible with stock (the v1 commit's invariant). Validated: `cargo check -p vmm` (lib + tests) for aarch64-unknown-linux-musl is clean. The firecracker-binary build isn't musl-checkable here (the upstream `seccompiler` crate has no libc `memfd_create` on musl), so the full build + stock<->fork snapshot-compat test run in CI. Co-Authored-By: Claude Opus 4.8 (1M context) Claude-Session: https://claude.ai/code/session_013EkVddad6rxeV6xFfovQ8x --- src/firecracker/src/api_server/mod.rs | 2 - .../src/api_server/request/snapshot.rs | 6 -- src/firecracker/swagger/firecracker.yaml | 16 +---- src/vmm/src/persist.rs | 47 +++------------ src/vmm/src/rpc_interface.rs | 10 ---- src/vmm/src/vmm_config/snapshot.rs | 19 +----- src/vmm/src/vstate/memory.rs | 58 ++++--------------- src/vmm/src/vstate/vm.rs | 13 ----- src/vmm/tests/integration_tests.rs | 16 ----- 9 files changed, 24 insertions(+), 163 deletions(-) diff --git a/src/firecracker/src/api_server/mod.rs b/src/firecracker/src/api_server/mod.rs index 141f1cd87..045defc35 100644 --- a/src/firecracker/src/api_server/mod.rs +++ b/src/firecracker/src/api_server/mod.rs @@ -159,8 +159,6 @@ impl ApiServer { &METRICS.latencies_us.diff_create_snapshot, "create diff snapshot", )), - // ADR 0045 off-pause flush — no dedicated latency metric. - SnapshotType::Msync | SnapshotType::MsyncAndState => None, }, VmmAction::LoadSnapshot(_) => { Some((&METRICS.latencies_us.load_snapshot, "load snapshot")) diff --git a/src/firecracker/src/api_server/request/snapshot.rs b/src/firecracker/src/api_server/request/snapshot.rs index d84d99eac..385d3a14c 100644 --- a/src/firecracker/src/api_server/request/snapshot.rs +++ b/src/firecracker/src/api_server/request/snapshot.rs @@ -112,7 +112,6 @@ fn parse_put_snapshot_load(body: &Body) -> Result { network_overrides: snapshot_config.network_overrides, vsock_override: snapshot_config.vsock_override, clock_realtime: snapshot_config.clock_realtime, - shared: snapshot_config.shared, uffd_base_file: snapshot_config.uffd_base_file, }; @@ -211,7 +210,6 @@ mod tests { network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }; let mut parsed_request = parse_put_snapshot(&Body::new(body), Some("load")).unwrap(); @@ -245,7 +243,6 @@ mod tests { network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }; let mut parsed_request = parse_put_snapshot(&Body::new(body), Some("load")).unwrap(); @@ -279,7 +276,6 @@ mod tests { network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }; let mut parsed_request = parse_put_snapshot(&Body::new(body), Some("load")).unwrap(); @@ -322,7 +318,6 @@ mod tests { }], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }; let mut parsed_request = parse_put_snapshot(&Body::new(body), Some("load")).unwrap(); @@ -353,7 +348,6 @@ mod tests { network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }; let parsed_request = parse_put_snapshot(&Body::new(body), Some("load")).unwrap(); diff --git a/src/firecracker/swagger/firecracker.yaml b/src/firecracker/swagger/firecracker.yaml index f3bdaec83..fb39895c4 100644 --- a/src/firecracker/swagger/firecracker.yaml +++ b/src/firecracker/swagger/firecracker.yaml @@ -1621,19 +1621,14 @@ definitions: enum: - Full - Diff - - Msync - - MsyncAndState description: Type of snapshot to create. It is optional and by default, a full - snapshot is created. `Msync` flushes a MAP_SHARED file-backed guest - memory to its backing memfile via msync(MS_SYNC) without writing the - machine state; `MsyncAndState` also writes the state file. + snapshot is created. vmstate_only: type: boolean description: Write only the microVM state file and skip the guest-memory leg - entirely (mem_file_path is ignored). Incompatible with the - Msync/MsyncAndState snapshot types. Optional and defaults to false. + entirely (mem_file_path is ignored). Optional and defaults to false. NetworkOverride: type: object @@ -1720,13 +1715,6 @@ definitions: elapsed since the snapshot was taken. When false (default), kvmclock resumes from where it was at snapshot time. This option may be extended to other clock sources and CPU architectures in the future." - shared: - type: boolean - description: - When set to true and the memory is restored from a file, map the guest - memory MAP_SHARED so guest writes flush back to the backing memfile (the - page-readable source for post-copy live migration). No effect for the UFFD - backend. Optional; defaults to false (MAP_PRIVATE). uffd_base_file: type: string description: diff --git a/src/vmm/src/persist.rs b/src/vmm/src/persist.rs index 0413f4198..f5e5326cb 100644 --- a/src/vmm/src/persist.rs +++ b/src/vmm/src/persist.rs @@ -34,9 +34,7 @@ use crate::utils::u64_to_usize; use crate::vmm_config::boot_source::BootSourceConfig; use crate::vmm_config::instance_info::InstanceInfo; use crate::vmm_config::machine_config::{HugePageConfig, MachineConfigError, MachineConfigUpdate}; -use crate::vmm_config::snapshot::{ - CreateSnapshotParams, LoadSnapshotParams, MemBackendType, SnapshotType, -}; +use crate::vmm_config::snapshot::{CreateSnapshotParams, LoadSnapshotParams, MemBackendType}; use crate::vstate::kvm::KvmState; use crate::vstate::memory::{ self, GuestMemoryState, GuestRegionMmap, GuestRegionType, MemoryError, @@ -170,31 +168,11 @@ pub fn create_snapshot( vm_info: &VmInfo, params: &CreateSnapshotParams, ) -> Result<(), CreateSnapshotError> { - // `vmstate_only` skips the memory leg; the msync types ARE memory - // flushes, so the combination would write nothing at all — reject it. - if params.vmstate_only - && matches!( - params.snapshot_type, - SnapshotType::Msync | SnapshotType::MsyncAndState - ) - { - return Err(CreateSnapshotError::MicrovmState( - MicrovmStateError::NotAllowed( - "vmstate_only cannot combine with the msync snapshot types".into(), - ), - )); - } - - // `Msync` is a memory-only off-pause flush: the machine state file is not - // written (the durable state rides a separate `MsyncAndState`/`Full` capture). - // Every other type writes the state file as usual. - if params.snapshot_type != SnapshotType::Msync { - let microvm_state = vmm - .save_state(vm_info) - .map_err(CreateSnapshotError::MicrovmState)?; + let microvm_state = vmm + .save_state(vm_info) + .map_err(CreateSnapshotError::MicrovmState)?; - snapshot_state_to_file(µvm_state, ¶ms.snapshot_path)?; - } + snapshot_state_to_file(µvm_state, ¶ms.snapshot_path)?; // engram v3 (ADR 0045 C2): vmstate-only capture for post-copy live // migration — skip the memory leg (and its dirty-queue correction, which @@ -482,7 +460,7 @@ pub fn restore_from_snapshot( .into()); } ( - guest_memory_from_file(mem_backend_path, mem_state, track_dirty_pages, params.shared) + guest_memory_from_file(mem_backend_path, mem_state, track_dirty_pages) .map_err(RestoreFromSnapshotGuestMemoryError::File)?, None, ) @@ -546,16 +524,9 @@ fn guest_memory_from_file( mem_file_path: &Path, mem_state: &GuestMemoryState, track_dirty_pages: bool, - shared: bool, ) -> Result, GuestMemoryFromFileError> { - // A `MAP_SHARED` mapping that flushes guest writes back needs the fd opened - // for writing; the default `MAP_PRIVATE` restore only reads. - let mem_file = OpenOptions::new() - .read(true) - .write(shared) - .open(mem_file_path)?; - let guest_mem = - memory::snapshot_file(mem_file, mem_state.regions(), track_dirty_pages, shared)?; + let mem_file = File::open(mem_file_path)?; + let guest_mem = memory::snapshot_file(mem_file, mem_state.regions(), track_dirty_pages)?; Ok(guest_mem) } @@ -641,7 +612,7 @@ fn create_guest_memory( return Err(GuestMemoryFromUffdError::BaseFileHugetlbfs); } let file = std::fs::File::open(path).map_err(GuestMemoryFromUffdError::OpenBaseFile)?; - memory::snapshot_file(file, mem_state.regions(), track_dirty_pages, /* shared */ false) + memory::snapshot_file(file, mem_state.regions(), track_dirty_pages) .map_err(GuestMemoryFromUffdError::BaseFileMap)? } None => memory::anonymous(mem_state.regions(), track_dirty_pages, huge_pages)?, diff --git a/src/vmm/src/rpc_interface.rs b/src/vmm/src/rpc_interface.rs index a1a310672..50a695c61 100644 --- a/src/vmm/src/rpc_interface.rs +++ b/src/vmm/src/rpc_interface.rs @@ -952,15 +952,6 @@ impl RuntimeApiController { elapsed_time_us ); } - // ADR 0045 off-pause flush. No dedicated latency metric (reuses the - // monotonic clock for the log line) to keep the fork surface minimal. - SnapshotType::Msync | SnapshotType::MsyncAndState => { - let elapsed_time_us = get_time_us(ClockType::Monotonic) - create_start_us; - info!( - "'create msync snapshot' VMM action took {} us.", - elapsed_time_us - ); - } } Ok(VmmData::Empty) } @@ -1341,7 +1332,6 @@ mod tests { network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }, ))); diff --git a/src/vmm/src/vmm_config/snapshot.rs b/src/vmm/src/vmm_config/snapshot.rs index 7d9e4a8b6..d12d329ec 100644 --- a/src/vmm/src/vmm_config/snapshot.rs +++ b/src/vmm/src/vmm_config/snapshot.rs @@ -18,13 +18,6 @@ pub enum SnapshotType { /// Full snapshot. #[default] Full, - /// Memory-only flush of a `MAP_SHARED` file-backed guest memory to its backing - /// memfile via `msync(MS_SYNC)`. No machine state is written. The off-pause - /// continuous-flush primitive for ADR 0045 live teleport. - Msync, - /// Like [`SnapshotType::Msync`], but the machine state file is also written — - /// an `msync` memory flush paired with a normal `state` file. - MsyncAndState, } /// Specifies the method through which guest memory will get populated when @@ -56,8 +49,7 @@ pub struct CreateSnapshotParams { /// entirely (`mem_file_path` is ignored). The post-copy live-migration /// blackout primitive for ADR 0045: the dirty memory never materializes /// as a file — the destination demand-faults it from the paused source's - /// address space. Incompatible with the `Msync`/`MsyncAndState` types - /// (those ARE memory flushes). + /// address space. #[serde(default)] pub vmstate_only: bool, } @@ -100,11 +92,6 @@ pub struct LoadSnapshotParams { /// advancing kvmclock by the wall-clock time elapsed since the snapshot was taken. When false /// (default), kvmclock resumes from where it was at snapshot time. pub clock_realtime: bool, - /// When set and the memory is restored from a file (`MemBackendType::File`), - /// map the guest memory `MAP_SHARED` so guest writes flush back to the backing - /// memfile — the page-readable source for ADR 0045 post-copy. No effect for - /// the UFFD backend. Defaults to false (today's `MAP_PRIVATE` behavior). - pub shared: bool, /// ADR 0045 unified memory substrate (v2b). When set with the Uffd memory /// backend, guest memory is created as `MAP_PRIVATE` of this file (a /// tmpfs/shmem per-template base) instead of anonymous, and the UFFD @@ -150,10 +137,6 @@ pub struct LoadSnapshotConfig { /// [x86_64 only] When set to true, passes `KVM_CLOCK_REALTIME` to `KVM_SET_CLOCK` on restore. #[serde(default)] pub clock_realtime: bool, - /// Whether to map file-backed guest memory `MAP_SHARED` (ADR 0045 post-copy - /// source). Top-level (not inside `mem_backend`). Defaults to false. - #[serde(default)] - pub shared: bool, /// ADR 0045 substrate v2b: back Uffd-restored guest memory `MAP_PRIVATE` /// by this shmem base file and register UFFD `MISSING | MINOR`. Top-level /// (not inside `mem_backend`, which is `deny_unknown_fields`). diff --git a/src/vmm/src/vstate/memory.rs b/src/vmm/src/vstate/memory.rs index 8de06a1e2..7bff6ad07 100644 --- a/src/vmm/src/vstate/memory.rs +++ b/src/vmm/src/vstate/memory.rs @@ -76,8 +76,6 @@ pub enum MemoryError { SeekError(std::io::Error), /// Volatile memory error: {0} VolatileMemoryError(vm_memory::VolatileMemoryError), - /// Cannot msync guest memory to its backing file: {0} - Msync(std::io::Error), } impl From for MemoryError { @@ -576,15 +574,11 @@ pub fn anonymous( } /// Creates a GuestMemoryMmap given a `file` containing the data -/// and a `state` containing mapping information. When `shared` is set, the -/// regions are mapped `MAP_SHARED` so guest writes flush back to the backing -/// file (the page-readable post-copy source, ADR 0045); otherwise `MAP_PRIVATE` -/// (the default restore behavior). +/// and a `state` containing mapping information. pub fn snapshot_file( file: File, regions: impl Iterator, track_dirty_pages: bool, - shared: bool, ) -> Result, MemoryError> { let regions: Vec<_> = regions.collect(); let memory_size = regions @@ -599,12 +593,12 @@ pub fn snapshot_file( return Err(MemoryError::OffsetTooLarge); } - let flags = if shared { - libc::MAP_SHARED - } else { - libc::MAP_PRIVATE - }; - create(regions.into_iter(), flags, Some(file), track_dirty_pages) + create( + regions.into_iter(), + libc::MAP_PRIVATE, + Some(file), + track_dirty_pages, + ) } /// Defines the interface for snapshotting memory. @@ -628,11 +622,6 @@ where dirty_bitmap: &DirtyBitmap, ) -> Result<(), MemoryError>; - /// Flushes the regions' dirty pages to their backing file via `msync(MS_SYNC)`. - /// Only meaningful when the memory is mapped `MAP_SHARED` on a file (ADR 0045 - /// off-pause flush); a harmless flush for anonymous mappings. - fn msync(&self) -> Result<(), MemoryError>; - /// Resets all the memory region bitmaps fn reset_dirty(&self); @@ -724,28 +713,6 @@ impl GuestMemoryExtension for GuestMemoryMmap { .map_err(MemoryError::WriteMemory) } - /// Flushes the regions' dirty pages to their backing file via `msync(MS_SYNC)`. - fn msync(&self) -> Result<(), MemoryError> { - self.iter().try_for_each(|region| { - // `region` derefs to its underlying MmapRegion. For a MAP_SHARED - // file-backed region, MS_SYNC blocks until the dirty pages are - // written back to the backing file; for anonymous memory it's a - // harmless flush. - // SAFETY: `as_ptr()`/`size()` describe this region's valid host mmap. - let ret = unsafe { - libc::msync( - region.as_ptr().cast::(), - region.size(), - libc::MS_SYNC, - ) - }; - if ret != 0 { - return Err(MemoryError::Msync(std::io::Error::last_os_error())); - } - Ok(()) - }) - } - /// Dumps all pages of GuestMemoryMmap present in `dirty_bitmap` to a writer. fn dump_dirty( &self, @@ -954,7 +921,7 @@ mod tests { let regions = vec![(GuestAddress(0), page_size)]; let guest_regions = - snapshot_file(file, regions.into_iter(), dirty_page_tracking, false).unwrap(); + snapshot_file(file, regions.into_iter(), dirty_page_tracking).unwrap(); assert_eq!(guest_regions.len(), 1); guest_regions.iter().for_each(|region| { assert_eq!(region.bitmap().is_some(), dirty_page_tracking); @@ -975,7 +942,7 @@ mod tests { (GuestAddress(0x10000), page_size), (GuestAddress(0x20000), page_size), ]; - let guest_regions = snapshot_file(file, regions.into_iter(), false, false).unwrap(); + let guest_regions = snapshot_file(file, regions.into_iter(), false).unwrap(); assert_eq!(guest_regions.len(), 3); } @@ -987,7 +954,7 @@ mod tests { file.write_all(&vec![0x42u8; page_size]).unwrap(); let regions = vec![(GuestAddress(0), 2 * page_size)]; - let result = snapshot_file(file, regions.into_iter(), false, false); + let result = snapshot_file(file, regions.into_iter(), false); assert!(matches!(result.unwrap_err(), MemoryError::OffsetTooLarge)); } @@ -1178,7 +1145,7 @@ mod tests { guest_memory.dump(&mut memory_file).unwrap(); let restored_guest_memory = - into_region_ext(snapshot_file(memory_file, memory_state.regions(), false, false).unwrap()); + into_region_ext(snapshot_file(memory_file, memory_state.regions(), false).unwrap()); // Check that the region contents are the same. let mut restored_region = vec![0u8; page_size * 2]; @@ -1243,7 +1210,7 @@ mod tests { // We can restore from this because this is the first dirty dump. let restored_guest_memory = - into_region_ext(snapshot_file(file, memory_state.regions(), false, false).unwrap()); + into_region_ext(snapshot_file(file, memory_state.regions(), false).unwrap()); // Check that the region contents are the same. let mut restored_region = vec![0u8; region_size]; @@ -1467,7 +1434,6 @@ mod tests { memory_file, std::iter::once((GuestAddress(0), 2 * page_size)), false, - false, ) .unwrap(), ); diff --git a/src/vmm/src/vstate/vm.rs b/src/vmm/src/vstate/vm.rs index 8a49cdbea..4ba4bdc9c 100644 --- a/src/vmm/src/vstate/vm.rs +++ b/src/vmm/src/vstate/vm.rs @@ -575,17 +575,6 @@ impl KvmVm { ) -> Result<(), CreateSnapshotError> { use self::CreateSnapshotError::*; - // `Msync`/`MsyncAndState`: the guest memory is mmap'd `MAP_SHARED` on its - // own backing memfile, so "saving" memory is an in-place `msync(MS_SYNC)` - // of the regions — not a dump to a separate `mem_file_path` (unused here). - if matches!( - snapshot_type, - SnapshotType::Msync | SnapshotType::MsyncAndState - ) { - self.guest_memory().msync()?; - return Ok(()); - } - // Need to check this here, as we create the file in the line below let file_existed = mem_file_path.exists(); @@ -633,8 +622,6 @@ impl KvmVm { self.reset_dirty_bitmap(); self.guest_memory().reset_dirty(); } - // Handled by the early return above (in-place `msync`, no file dump). - SnapshotType::Msync | SnapshotType::MsyncAndState => unreachable!(), }; file.flush() diff --git a/src/vmm/tests/integration_tests.rs b/src/vmm/tests/integration_tests.rs index 37db1db2b..eb8b15456 100644 --- a/src/vmm/tests/integration_tests.rs +++ b/src/vmm/tests/integration_tests.rs @@ -318,7 +318,6 @@ fn verify_load_snapshot(snapshot_file: TempFile, memory_file: TempFile) { network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, })) .unwrap(); @@ -363,20 +362,6 @@ fn test_create_snapshot_vmstate_only() { .handle_request(VmmAction::Pause, &mut event_manager) .unwrap(); - // The msync types ARE memory flushes — combining them with `vmstate_only` - // would write nothing at all and is rejected outright. - controller - .handle_request( - VmmAction::CreateSnapshot(CreateSnapshotParams { - snapshot_type: SnapshotType::Msync, - snapshot_path: snapshot_file.as_path().to_path_buf(), - mem_file_path: memory_file.as_path().to_path_buf(), - vmstate_only: true, - }), - &mut event_manager, - ) - .unwrap_err(); - // vmstate-only writes the state file and never touches the memory path. controller .handle_request( @@ -458,7 +443,6 @@ fn verify_load_snap_disallowed_after_boot_resources(res: VmmAction, res_name: &s network_overrides: vec![], vsock_override: None, clock_realtime: false, - shared: false, uffd_base_file: None, }); let err = preboot_api_controller.handle_preboot_request(req);