Skip to content

Commit 8a02342

Browse files
committed
perf: use restore_preserving_file_mappings for initrd
Call restore_preserving_file_mappings when a file mapping (initrd) exists, skipping the unmap/remap cycle on every snapshot restore. Remove the now-unused file_mapping_base field. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 6027206 commit 8a02342

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

host/src/lib.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,10 +2008,9 @@ pub struct Sandbox {
20082008
inner: MultiUseSandbox,
20092009
/// Post-init snapshot for fast restore between calls.
20102010
snapshot: Option<Arc<Snapshot>>,
2011-
/// File mapping to re-register after snapshot restore.
2012-
/// Snapshot restore unmaps all non-snapshot regions.
2011+
/// When set, restore uses the preserving variant to keep the
2012+
/// read-only file mapping alive across restores.
20132013
file_mapping_path: Option<std::path::PathBuf>,
2014-
file_mapping_base: u64,
20152014
exit_code: Arc<AtomicI32>,
20162015
/// Shared socket table — cleared on [`Sandbox::restore`] so that
20172016
/// host-side fds don't leak across guest restore cycles.
@@ -2247,7 +2246,7 @@ impl Sandbox {
22472246
tools_ref.dispatch(&payload)
22482247
})?;
22492248

2250-
Self::finish_evolve(usbox, None, 0, exit_code, sleep_cancel, socket_table)
2249+
Self::finish_evolve(usbox, None, exit_code, sleep_cancel, socket_table)
22512250
}
22522251

22532252
/// Low-level: boot with a zero-copy mapped initrd file. Prefer the builder.
@@ -2304,7 +2303,6 @@ impl Sandbox {
23042303
Self::finish_evolve(
23052304
usbox,
23062305
initrd_path.map(|p| p.to_path_buf()),
2307-
INITRD_MAP_BASE,
23082306
exit_code,
23092307
sleep_cancel,
23102308
socket_table,
@@ -2314,7 +2312,6 @@ impl Sandbox {
23142312
fn finish_evolve(
23152313
usbox: UninitializedSandbox,
23162314
file_mapping_path: Option<std::path::PathBuf>,
2317-
file_mapping_base: u64,
23182315
exit_code: Arc<AtomicI32>,
23192316
sleep_cancel: SleepCancel,
23202317
socket_table: Option<Arc<Mutex<SocketTable>>>,
@@ -2325,7 +2322,6 @@ impl Sandbox {
23252322
inner,
23262323
snapshot,
23272324
file_mapping_path,
2328-
file_mapping_base,
23292325
exit_code,
23302326
socket_table,
23312327
sleep_cancel,
@@ -2338,15 +2334,12 @@ impl Sandbox {
23382334
/// guest memory to the state captured after init.
23392335
pub fn restore(&mut self) -> Result<()> {
23402336
if let Some(ref snap) = self.snapshot {
2341-
self.inner.restore(snap.clone())?;
2342-
}
2343-
// Re-register file mapping after restore (snapshot restore
2344-
// unmaps all non-snapshot regions including file mappings)
2345-
if let Some(ref path) = self.file_mapping_path {
2346-
self.inner
2347-
.map_file_cow(path, self.file_mapping_base, Some("initrd"))?;
2337+
if self.file_mapping_path.is_some() {
2338+
self.inner.restore_preserving_file_mappings(snap.clone())?;
2339+
} else {
2340+
self.inner.restore(snap.clone())?;
2341+
}
23482342
}
2349-
// Close leaked host-side sockets the guest "forgot" about.
23502343
if let Some(ref table) = self.socket_table {
23512344
table.lock().unwrap().clear();
23522345
}
@@ -2543,7 +2536,6 @@ impl Sandbox {
25432536
inner,
25442537
snapshot: Some(arc),
25452538
file_mapping_path: initrd,
2546-
file_mapping_base: INITRD_MAP_BASE,
25472539
exit_code,
25482540
socket_table,
25492541
sleep_cancel,

0 commit comments

Comments
 (0)