summary refs log tree commit diff
path: root/sys_util
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2018-01-26 17:37:57 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-03-30 21:53:32 -0700
commit5d586b73a4c78f4118b97e65a229cbb7e99bf963 (patch)
treeb12bc9fde02864a8788788790c97b5dce206ce0f /sys_util
parent3e40b51a62b08dc27dcaa7fbec630e047713aba1 (diff)
downloadcrosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.tar
crosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.tar.gz
crosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.tar.bz2
crosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.tar.lz
crosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.tar.xz
crosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.tar.zst
crosvm-5d586b73a4c78f4118b97e65a229cbb7e99bf963.zip
sys_util: use MADV_DONTDUMP for new mmaps
The mmaps made through the sys_util API are usually for guest memory or
other large shared memory chunks that will pollute the file system with
huge dumps on crash. By using MADV_DONTDUMP, we save the file system
from storing these useless data segments when crosvm crashes.

TEST=./build_test
BUG=None

Change-Id: I2041523648cd7c150bbdbfceef589f42d3f9c2b9
Reviewed-on: https://chromium-review.googlesource.com/890279
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Diffstat (limited to 'sys_util')
-rw-r--r--sys_util/src/mmap.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/sys_util/src/mmap.rs b/sys_util/src/mmap.rs
index 2813234..4ce72b6 100644
--- a/sys_util/src/mmap.rs
+++ b/sys_util/src/mmap.rs
@@ -69,6 +69,12 @@ impl MemoryMapping {
         if addr == libc::MAP_FAILED {
             return Err(Error::SystemCallFailed(errno::Error::last()));
         }
+        // This is safe because we call madvise with a valid address and size, and we check the
+        // return value. We only warn about an error because failure here is not fatal to the mmap.
+        if unsafe { libc::madvise(addr, size, libc::MADV_DONTDUMP) } == -1 {
+            warn!("failed madvise(MADV_DONTDUMP) on mmap: {:?}",
+                  errno::Error::last());
+        }
         Ok(MemoryMapping {
                addr: addr as *mut u8,
                size: size,
@@ -107,6 +113,12 @@ impl MemoryMapping {
         if addr == libc::MAP_FAILED {
             return Err(Error::SystemCallFailed(errno::Error::last()));
         }
+        // This is safe because we call madvise with a valid address and size, and we check the
+        // return value. We only warn about an error because failure here is not fatal to the mmap.
+        if unsafe { libc::madvise(addr, size, libc::MADV_DONTDUMP) } == -1 {
+            warn!("failed madvise(MADV_DONTDUMP) on mmap: {:?}",
+                  errno::Error::last());
+        }
         Ok(MemoryMapping {
                addr: addr as *mut u8,
                size: size,