summary refs log tree commit diff
path: root/sys_util
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2020-03-13 13:39:22 -0700
committerCommit Bot <commit-bot@chromium.org>2020-03-29 08:58:42 +0000
commit31deb9fd456c876c16a624eaf04b23c7887802c6 (patch)
tree343d58423762286ac853987e21dc325a9287bd0f /sys_util
parentd32100205f6a8784c8c60e6e933866ca71b36694 (diff)
downloadcrosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.tar
crosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.tar.gz
crosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.tar.bz2
crosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.tar.lz
crosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.tar.xz
crosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.tar.zst
crosvm-31deb9fd456c876c16a624eaf04b23c7887802c6.zip
sys_util: Allow pre-populating mmaps
io_uring would like to pre-fault the ring pages as they are likely to be
accessed regularly. Allow mmap users to specify the MAP_POPULATE flag
through a new API.

Change-Id: I5fefc13443280fe832c9601fe5a497b572127f0e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2124008
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'sys_util')
-rw-r--r--sys_util/src/mmap.rs52
1 files changed, 42 insertions, 10 deletions
diff --git a/sys_util/src/mmap.rs b/sys_util/src/mmap.rs
index 18414d5..d8ba6b6 100644
--- a/sys_util/src/mmap.rs
+++ b/sys_util/src/mmap.rs
@@ -168,32 +168,64 @@ impl MemoryMapping {
         MemoryMapping::from_fd_offset_protection(fd, size, offset, Protection::read_write())
     }
 
+    /// Maps `size` bytes starting at `offset` from the given `fd` as read/write, and requests
+    /// that the pages are pre-populated.
+    /// # Arguments
+    /// * `fd` - File descriptor to mmap from.
+    /// * `size` - Size of memory region in bytes.
+    /// * `offset` - Offset in bytes from the beginning of `fd` to start the mmap.
+    pub fn from_fd_offset_populate(
+        fd: &dyn AsRawFd,
+        size: usize,
+        offset: usize,
+    ) -> Result<MemoryMapping> {
+        MemoryMapping::from_fd_offset_flags(
+            fd,
+            size,
+            offset,
+            libc::MAP_SHARED | libc::MAP_POPULATE,
+            Protection::read_write(),
+        )
+    }
+
     /// Maps the `size` bytes starting at `offset` bytes of the given `fd` as read/write.
     ///
     /// # Arguments
     /// * `fd` - File descriptor to mmap from.
     /// * `size` - Size of memory region in bytes.
     /// * `offset` - Offset in bytes from the beginning of `fd` to start the mmap.
+    /// * `flags` - flags passed directly to mmap.
     /// * `prot` - Protection (e.g. readable/writable) of the memory region.
-    pub fn from_fd_offset_protection(
+    fn from_fd_offset_flags(
         fd: &dyn AsRawFd,
         size: usize,
         offset: usize,
+        flags: c_int,
         prot: Protection,
     ) -> Result<MemoryMapping> {
-        // This is safe because we are creating an anonymous mapping in a place not already used by
-        // any other area in this process.
         unsafe {
-            MemoryMapping::try_mmap(
-                None,
-                size,
-                prot.into(),
-                libc::MAP_SHARED,
-                Some((fd, offset)),
-            )
+            // This is safe because we are creating an anonymous mapping in a place not already used
+            // by any other area in this process.
+            MemoryMapping::try_mmap(None, size, prot.into(), flags, Some((fd, offset)))
         }
     }
 
+    /// Maps the `size` bytes starting at `offset` bytes of the given `fd` as read/write.
+    ///
+    /// # Arguments
+    /// * `fd` - File descriptor to mmap from.
+    /// * `size` - Size of memory region in bytes.
+    /// * `offset` - Offset in bytes from the beginning of `fd` to start the mmap.
+    /// * `prot` - Protection (e.g. readable/writable) of the memory region.
+    pub fn from_fd_offset_protection(
+        fd: &dyn AsRawFd,
+        size: usize,
+        offset: usize,
+        prot: Protection,
+    ) -> Result<MemoryMapping> {
+        MemoryMapping::from_fd_offset_flags(fd, size, offset, libc::MAP_SHARED, prot)
+    }
+
     /// Creates an anonymous shared mapping of `size` bytes with `prot` protection.
     /// Unsafe: unmaps any mmap'd regions already present at (addr..addr+size).
     ///