summary refs log tree commit diff
path: root/vm_control
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2020-02-07 11:00:55 -0800
committerCommit Bot <commit-bot@chromium.org>2020-04-08 06:09:25 +0000
commite1980a9c360b04705a16434bdaf1a56161dafb56 (patch)
tree95944d7bfa87505050c2716fd764ffd1699d737e /vm_control
parentf3081b120e0934539f6f3f2c60c9ff26c801c0ea (diff)
downloadcrosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.tar
crosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.tar.gz
crosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.tar.bz2
crosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.tar.lz
crosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.tar.xz
crosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.tar.zst
crosvm-e1980a9c360b04705a16434bdaf1a56161dafb56.zip
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 <smbarber@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'vm_control')
-rw-r--r--vm_control/src/lib.rs47
1 files changed, 47 insertions, 0 deletions
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<BalloonControlCommand, BalloonControlResult>;
 pub type BalloonControlResponseSocket = MsgSocket<BalloonControlResult, BalloonControlCommand>;
 
@@ -422,6 +466,9 @@ pub type VmMemoryControlResponseSocket = MsgSocket<VmMemoryResponse, VmMemoryReq
 pub type VmIrqRequestSocket = MsgSocket<VmIrqRequest, VmIrqResponse>;
 pub type VmIrqResponseSocket = MsgSocket<VmIrqResponse, VmIrqRequest>;
 
+pub type VmMsyncRequestSocket = MsgSocket<VmMsyncRequest, VmMsyncResponse>;
+pub type VmMsyncResponseSocket = MsgSocket<VmMsyncResponse, VmMsyncRequest>;
+
 pub type VmControlRequestSocket = MsgSocket<VmRequest, VmResponse>;
 pub type VmControlResponseSocket = MsgSocket<VmResponse, VmRequest>;