Skip to content

Commit 4ea49b0

Browse files
committed
support mmap shared
1 parent 3d9dc46 commit 4ea49b0

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/seccomp/aarch64-unknown-linux-musl.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,19 @@
596596
}
597597
]
598598
},
599+
{
600+
"syscall": "msync",
601+
"comment": "Used to sync memory from mmap to disk",
602+
"args": [
603+
{
604+
"index": 2,
605+
"type": "dword",
606+
"op": "eq",
607+
"val": 4,
608+
"comment": "MS_SYNC"
609+
}
610+
]
611+
},
599612
{
600613
"syscall": "rt_sigaction",
601614
"comment": "rt_sigaction is used by libc::abort during a panic to install the default handler for SIGABRT",

resources/seccomp/x86_64-unknown-linux-musl.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@
248248
}
249249
]
250250
},
251+
{
252+
"syscall": "msync",
253+
"comment": "Used to sync memory from mmap to disk",
254+
"args": [
255+
{
256+
"index": 2,
257+
"type": "dword",
258+
"op": "eq",
259+
"val": 4,
260+
"comment": "MS_SYNC"
261+
}
262+
]
263+
},
251264
{
252265
"syscall": "rt_sigaction",
253266
"comment": "rt_sigaction is used by libc::abort during a panic to install the default handler for SIGABRT",

src/vm-memory/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn create_guest_memory(
116116
for region in regions {
117117
let flags = match region.0 {
118118
None => libc::MAP_NORESERVE | libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
119-
Some(_) => libc::MAP_NORESERVE | libc::MAP_PRIVATE,
119+
Some(_) => libc::MAP_NORESERVE | libc::MAP_SHARED,
120120
};
121121

122122
let mmap_region =

src/vmm/src/persist.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use arch::regs::{get_manufacturer_id_from_host, get_manufacturer_id_from_state};
1616
#[cfg(target_arch = "x86_64")]
1717
use cpuid::common::{get_vendor_id_from_cpuid, get_vendor_id_from_host};
1818
use devices::virtio::TYPE_NET;
19-
use logger::{error, info};
19+
use logger::{error, info, warn};
2020
use seccompiler::BpfThreadMap;
2121
use serde::Serialize;
2222
use snapshot::Snapshot;
@@ -25,7 +25,7 @@ use utils::sock_ctrl_msg::ScmSocket;
2525
use versionize::{VersionMap, Versionize, VersionizeResult};
2626
use versionize_derive::Versionize;
2727
use virtio_gen::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
28-
use vm_memory::{GuestMemory, GuestMemoryMmap};
28+
use vm_memory::{GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
2929

3030
use crate::builder::{self, StartMicrovmError};
3131
use crate::device_manager::persist::{DeviceStates, Error as DevicePersistError};
@@ -324,6 +324,25 @@ fn snapshot_memory_to_file(
324324
mem_file_path: &Path,
325325
snapshot_type: &SnapshotType,
326326
) -> std::result::Result<(), CreateSnapshotError> {
327+
if OpenOptions::new().read(true).open(mem_file_path).is_ok() {
328+
// The memory file already exists.
329+
// We're going to use the msync behaviour
330+
for region in vmm.guest_memory().iter() {
331+
warn!("saving memory region");
332+
unsafe {
333+
if libc::msync(region.as_ptr() as _, region.len() as _, libc::MS_SYNC) == -1 {
334+
return Err(CreateSnapshotError::Memory(
335+
memory_snapshot::Error::CreateRegion(vm_memory::MmapRegionError::Mmap(
336+
std::io::Error::last_os_error(),
337+
)),
338+
));
339+
}
340+
};
341+
}
342+
343+
return Ok(());
344+
}
345+
327346
use self::CreateSnapshotError::*;
328347
let mut file = OpenOptions::new()
329348
.write(true)
@@ -552,7 +571,11 @@ fn guest_memory_from_file(
552571
track_dirty_pages: bool,
553572
) -> std::result::Result<GuestMemoryMmap, LoadSnapshotError> {
554573
use self::LoadSnapshotError::{DeserializeMemory, MemoryBackingFile};
555-
let mem_file = File::open(mem_file_path).map_err(MemoryBackingFile)?;
574+
let mem_file = OpenOptions::new()
575+
.write(true)
576+
.read(true)
577+
.open(mem_file_path)
578+
.map_err(MemoryBackingFile)?;
556579
GuestMemoryMmap::restore(Some(&mem_file), mem_state, track_dirty_pages)
557580
.map_err(DeserializeMemory)
558581
}

0 commit comments

Comments
 (0)