From e1980a9c360b04705a16434bdaf1a56161dafb56 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Fri, 7 Feb 2020 11:00:55 -0800 Subject: devices: pmem: implement flush using msync() Previously, writable pmem devices implemented the flush command using fsync(); however, this does not guarantee synchronization of memory mappings via mmap() to the file on disk. What we actually need is msync() on the pmem file mapping, but we don't have access to that mapping in the pmem child process, and it isn't trivial to pass it along since it is owned by the Vm object once it has been added as a mmap_arena. In order to call msync() on the mapping, add a new VmControl socket so that the pmem device can request that the main process issues an msync() on the MemoryMappingArena identified by its slot number. BUG=chromium:1007535 TEST=mount filesystem on /dev/pmem0 and sync; verify msync in strace Change-Id: Id0484757c422cf81d454fd54012a12dbcc1baaf6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2044365 Reviewed-by: Stephen Barber Tested-by: kokoro Commit-Queue: Daniel Verkamp --- vm_control/src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'vm_control') diff --git a/vm_control/src/lib.rs b/vm_control/src/lib.rs index 4d48ff4..4035c1c 100644 --- a/vm_control/src/lib.rs +++ b/vm_control/src/lib.rs @@ -408,6 +408,50 @@ pub enum VmIrqResponse { Err(SysError), } +#[derive(MsgOnSocket, Debug)] +pub enum VmMsyncRequest { + /// Flush the content of a memory mapping to its backing file. + /// `slot` selects the arena (as returned by `Vm::add_mmap_arena`). + /// `offset` is the offset of the mapping to sync within the arena. + MsyncArena { slot: u32, offset: usize }, +} + +#[derive(MsgOnSocket, Debug)] +pub enum VmMsyncResponse { + Ok, + Err(SysError), +} + +impl VmMsyncRequest { + /// Executes this request on the given Vm. + /// + /// # Arguments + /// * `vm` - The `Vm` to perform the request on. + /// + /// This does not return a result, instead encapsulating the success or failure in a + /// `VmMsyncResponse` with the intended purpose of sending the response back over the socket + /// that received this `VmMsyncResponse`. + pub fn execute(&self, vm: &mut Vm) -> VmMsyncResponse { + use self::VmMsyncRequest::*; + match *self { + MsyncArena { slot, offset } => { + if let Some(arena) = vm.get_mmap_arena(slot) { + match arena.msync(offset) { + Ok(true) => VmMsyncResponse::Ok, + Ok(false) => VmMsyncResponse::Err(SysError::new(EINVAL)), + Err(e) => match e { + MmapError::SystemCallFailed(errno) => VmMsyncResponse::Err(errno), + _ => VmMsyncResponse::Err(SysError::new(EINVAL)), + }, + } + } else { + VmMsyncResponse::Err(SysError::new(EINVAL)) + } + } + } + } +} + pub type BalloonControlRequestSocket = MsgSocket; pub type BalloonControlResponseSocket = MsgSocket; @@ -422,6 +466,9 @@ pub type VmMemoryControlResponseSocket = MsgSocket; pub type VmIrqResponseSocket = MsgSocket; +pub type VmMsyncRequestSocket = MsgSocket; +pub type VmMsyncResponseSocket = MsgSocket; + pub type VmControlRequestSocket = MsgSocket; pub type VmControlResponseSocket = MsgSocket; -- cgit 1.4.1