Skip to content

Commit 9a1b731

Browse files
committed
feat(virtio-pmem): add a Drop implementation
Event though during Firecracker normal run pmem devices are never removed, so there is no need to unmap memory, it is important for fuzzer runs, as otherwise it runs out of virtual memory. Additionally make `mmap_backing_file` private since it is only called from within Pmem device. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 2bccf9a commit 9a1b731

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/vmm/src/devices/virtio/pmem/device.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ pub struct Pmem {
9595
pub config: PmemConfig,
9696
}
9797

98+
impl Drop for Pmem {
99+
fn drop(&mut self) {
100+
let mmap_len = align_up(self.file_len, Self::ALIGNMENT);
101+
// SAFETY: `mmap_ptr` is a valid pointer since Pmem can only be created with `new*` methods.
102+
// Mapping size calculation is same for original mmap call.
103+
unsafe {
104+
_ = libc::munmap(self.mmap_ptr as *mut libc::c_void, u64_to_usize(mmap_len));
105+
}
106+
}
107+
}
108+
98109
impl Pmem {
99110
// Pmem devices need to have address and size to be
100111
// a multiple of 2MB
@@ -130,10 +141,7 @@ impl Pmem {
130141
})
131142
}
132143

133-
pub fn mmap_backing_file(
134-
path: &str,
135-
read_only: bool,
136-
) -> Result<(File, u64, u64, u64), PmemError> {
144+
fn mmap_backing_file(path: &str, read_only: bool) -> Result<(File, u64, u64, u64), PmemError> {
137145
let file = OpenOptions::new()
138146
.read(true)
139147
.write(!read_only)

0 commit comments

Comments
 (0)