summary refs log tree commit diff
path: root/sys_util
Commit message (Collapse)AuthorAge
* memory: Add methods to return error on short writes and readsDavid Tolnay2019-01-13
| | | | | | | | | | | | | | | | | | | | Add GuestMemory::write_all_at_addr, GuestMemory::read_exact_at_addr which return error if the entire write or read cannot be completed. Also rename write_slice_at_addr to write_at_addr, read_slice_at_addr to read_at_addr to make the entire set of four methods consistent in naming with the methods of std::io::Write and std::io::Read. Context: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1387624/16/devices/src/virtio/tpm.rs#75 TEST=cargo test Change-Id: Ia0775b75281ccf8030c84b41f9018a511204b8c9 Reviewed-on: https://chromium-review.googlesource.com/1407156 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: Add net module includes UnixSeqpacketpaulhsia2019-01-10
| | | | | | | | | | | | | | | | | std::os::net only supprts UnixDatagram and UnixStream, so we need this to support the connection to socket opened with SOCK_SEQPACKET flag. It only supports public API connect, write, try_clone, and read now. BUG=chromium:907520 TEST=Use $ FEATURES=test emerge-eve sys_util to run unit tests. Change-Id: I61a9acd4fa2e601e93b1f76dc1758cb61a433205 Reviewed-on: https://chromium-review.googlesource.com/1390077 Commit-Ready: Chih-Yang Hsia <paulhsia@chromium.org> Tested-by: Chih-Yang Hsia <paulhsia@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* Move validate_raw_fd to sys_utilChirantan Ekbote2019-01-07
| | | | | | | | | | | | | | validate_raw_fd is needed for the plugin crate. Move it into a common location so that it can be shared by both the linux and plugin code. BUG=b:80150167 TEST=manual Change-Id: I427e10716e75b2619fd0f4ba6725fa40446db4af Signed-off-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1341101 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* Add "Fd" wrapper for RawFd type.Miriam Zimmerman2019-01-05
| | | | | | | | | | | | | | This allows more type-safe usage of RawFds (preventing confusion with other c_ints) and provides a lightweight type that is usable in arguments to methods that take parameters of type AsRawFd. BUG=None TEST=Built. Change-Id: Ibdeb03b0e759577385b05acb25ce76d51f2188c6 Reviewed-on: https://chromium-review.googlesource.com/1396495 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Miriam Zimmerman <mutexlox@chromium.org> Reviewed-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add set_len() traitDaniel Verkamp2019-01-05
| | | | | | | | | | | | | | | | | Generalize file_sync into file_traits so that we can add another wrapper, this time for the set_len() method implemented directly on File. This will also be implemented on QcowFile. BUG=chromium:858815 TEST=build_test Change-Id: I43fbd1968a844c8cac359973a63babcc26942204 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1394148 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* crosvm: Split sub-crates into independent workspacepaulhsia2019-01-03
| | | | | | | | | | | | | | | | | | | Split sub-crates under crosvm root directory into several independent workspaces for adding ebuild files for those crates. data_model and sys_util could only be built by emerge after creating their ebuilds. BUG=chromium:916921 TEST='emerge-eve crosvm' TEST=Run 'cargo build' under crosvm directory Change-Id: I2dddbbb7c7344e643183a5885e867f134b299591 Reviewed-on: https://chromium-review.googlesource.com/1385972 Commit-Ready: Chih-Yang Hsia <paulhsia@chromium.org> Tested-by: Chih-Yang Hsia <paulhsia@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: add debug to eventfdJingkui Wang2018-12-25
| | | | | | | | | | | | BUG=None TEST=build Change-Id: I37a19b56876a5b24caca413d33fde8330b12b187 Reviewed-on: https://chromium-review.googlesource.com/1327514 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Jingkui Wang <jkwang@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: Combine GuestMemory::with_regions and with_regions_mutDavid Tolnay2018-12-18
| | | | | | | | | | | | | | | | | Every implementation of Fn also implements FnMut, so if some callback can be passed to GuestMemory::with_regions then it could also have been passed to GuestMemory::with_regions_mut. This CL removes GuestMemory::with_regions and renames with_regions_mut to with_regions. TEST=cargo check Change-Id: Ia4f168ff4eb4d45a5ee8f9413821ae244fb72ee1 Reviewed-on: https://chromium-review.googlesource.com/1378688 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org>
* toolchain: Update to Rust 1.31.0David Tolnay2018-12-13
| | | | | | | | | | | | | | | | | | We updated the production toolchain from 1.30 to 1.31 in CL:1366446. This CL does the same upgrade for the local developer toolchain and Kokoro. The relevant changes are in rust-toolchain and kokoro/Dockerfile. The rest are from rustfmt. TEST=cargo fmt --all -- --check TEST=as described in kokoro/README.md Change-Id: I3b4913f3e237baa36c664b4953be360c09efffd4 Reviewed-on: https://chromium-review.googlesource.com/1374376 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: Add ability to set real time thread priorityDylan Reid2018-12-09
| | | | | | | | | | | Add the minimal amount of functionality needed for audio threads that need to run with real time priority. Change-Id: I7052e0f2ba6b9179229fc4568b332952ee32f076 Signed-off-by: Dylan Reid <dgreid@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1366542 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
* poll_token: Use syn to simplify poll token deriveDavid Tolnay2018-12-09
| | | | | | | | | | | | | | | This CL removes 300 lines of parsing code and 200 lines of tests of parsing code by using the parsers provided by Syn, which we already use in implementing our other custom derives. TEST=cargo test poll_token_derive TEST=cargo check crosvm Change-Id: Ie2743b1bbb1b374326f9845fc37fc578b178c53d Reviewed-on: https://chromium-review.googlesource.com/1365112 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sync: Mutex type with methods that panic instead of return errorDavid Tolnay2018-12-05
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This CL adds a crate `sync` containing a type sync::Mutex which wraps the standard library Mutex and mirrors the same methods, except that they panic where the standard library would return a PoisonError. This API codifies our error handling strategy around poisoned mutexes in crosvm. - Crosvm releases are built with panic=abort so poisoning never occurs. A panic while a mutex is held (or ever) takes down the entire process. Thus we would like for code not to have to consider the possibility of poison. - We could ask developers to always write `.lock().unwrap()` on a standard library mutex. However, we would like to stigmatize the use of unwrap. It is confusing to permit unwrap but only on mutex lock results. During code review it may not always be obvious whether a particular unwrap is unwrapping a mutex lock result or a different error that should be handled in a more principled way. Developers should feel free to use sync::Mutex anywhere in crosvm that they would otherwise be using std::sync::Mutex. TEST=boot linux Change-Id: I9727b6f8fee439edb4a8d52cf19d59acf04d990f Reviewed-on: https://chromium-review.googlesource.com/1359923 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* lint: Resolve the easier clippy lintsDavid Tolnay2018-12-03
| | | | | | | | | | | | | | | | | | | | Hopefully the changes are self-explanatory and uncontroversial. This eliminates much of the noise from `cargo clippy` and, for my purposes, gives me a reasonable way to use it as a tool when writing and reviewing code. Here is the Clippy invocation I was using: cargo +nightly clippy -- -W clippy::correctness -A renamed_and_removed_lints -Aclippy::{blacklisted_name,borrowed_box,cast_lossless,cast_ptr_alignment,enum_variant_names,identity_op,if_same_then_else,mut_from_ref,needless_pass_by_value,new_without_default,new_without_default_derive,or_fun_call,ptr_arg,should_implement_trait,single_match,too_many_arguments,trivially_copy_pass_by_ref,unreadable_literal,unsafe_vector_initialization,useless_transmute} TEST=cargo check --features wl-dmabuf,gpu,usb-emulation TEST=boot linux Change-Id: I55eb1b4a72beb2f762480e3333a921909314a0a2 Reviewed-on: https://chromium-review.googlesource.com/1356911 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: replace fallocate64 with libc callDaniel Verkamp2018-11-30
| | | | | | | | | | | | | | Now that libc includes the fallocate64 function declaration that we need, we can drop our own declaration and resolve the TODOs. BUG=None TEST=cargo build Change-Id: I7548a561d672739fa7cdd7eb996ad2b2e307d69a Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1352866 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Stephen Barber <smbarber@chromium.org>
* sys_util: add trait to fsync File and QcowFileDaniel Verkamp2018-10-26
| | | | | | | | | | | | | | | | | | | | | | | | | File exposes sync_all() and sync_data() functions, which map to fsync() and fdatasync(), but these functions are not in a trait (they are just implemented directly on File), so they can't be implemented and used in a generic way for QcowFile. Add a new trait, FileSync, that exposes a fsync() function that may be used in the virtio block model. Previously, we were translating a block flush request into a call to File's flush() function, but this just flushes internal Rust library buffers to the file descriptor; it didn't actually result in a fsync() call. Using the new trait, we can cause an actual fsync() to occur for raw files, as intended. QcowFile was already safe, since its flush() function actually calls sync_all() under the hood. BUG=None TEST=sync with raw disk and verify fsync() in strace output Change-Id: I9bee2c0d2df3747aac1e7d9ec7d9b46a7862dc48 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1297839 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Zach Reizner <zachr@chromium.org>
* devices: virtio: block: ignore Discard failuresDaniel Verkamp2018-10-19
| | | | | | | | | | | | | | | | | | | | | Our branch of the 3.18 kernel has FALLOC_FL_PUNCH_HOLE disabled for the ext4 filesystem, which means that systems running that kernel always take the fallback path of writing buffers full of zeroes. This is not necessary for the Discard command, since it is just a hint and is not required to actually zero the blocks. Split the WriteZeroes trait up into a new PunchHole trait, which corresponds to fallocate() with FALLOC_FL_PUNCH_HOLE, and use the new trait to implement the virtio block Discard command. BUG=chromium:896314 TEST=`mkfs.btrfs /dev/vdb` and verify the desired fallocate() is used and no write() calls are issued when inducing a failure Change-Id: I67fd9a132758d8d766531ccca8358c7fe67b0460 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1286224 Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add SeekHole traitDaniel Verkamp2018-10-18
| | | | | | | | | | | | | Allow seeking to the next hole or data region in File and QcowFile. BUG=None TEST=None Change-Id: I16e77e4791aa85b4cc96f38327026cd93f02b7e1 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1274147 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: use PUNCH_HOLE to implement write_zeroesDaniel Verkamp2018-10-13
| | | | | | | | | | | | | | | | | Some filesystems do not support FALLOC_FL_ZERO_RANGE; in particular, encrypted files on ext4 fail this request with -EOPNOTSUPP. Use fallocate with FALLOC_FL_PUNCH_HOLE instead, which is more widely supported. BUG=None TEST=strace crosvm using qcow files on encrypted ext4 and verify that fallocate(FALLOC_FL_PUNCH_HOLE) is issued when required. Change-Id: Idffabc75ea0e1153efbb13cec8b4a25570427235 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1250022 Reviewed-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: use checked arithmetic and alignment in sock_ctrl_msgZach Reizner2018-10-12
| | | | | | | | Change-Id: I45125fc3a6af83e7f7ec791a2f9c5daef723ec89 Reviewed-on: https://chromium-review.googlesource.com/1277872 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: simplify format string for Display impl for PriorityZach Reizner2018-10-12
| | | | | | | | Change-Id: I2c08c707b0e5c8d576aeade94265434f126c6927 Reviewed-on: https://chromium-review.googlesource.com/1277870 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* rustfmt wire_format_derive and poll_token_deriveDaniel Verkamp2018-10-10
| | | | | | | | | | BUG=None TEST=cargo test Change-Id: I62f00a71ed3693352de648bb8ee576335b32019f Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1273688 Reviewed-by: Zach Reizner <zachr@chromium.org>
* cargo fmt all source codeZach Reizner2018-10-09
| | | | | | | | | | | | | | Now that cargo fmt has landed, run it over everything at once to bring rust source to the standard formatting. TEST=cargo test BUG=None Change-Id: Ic95a48725e5a40dcbd33ba6d5aef2bd01e91865b Reviewed-on: https://chromium-review.googlesource.com/1259287 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: use fallocate64 for large file supportDaniel Verkamp2018-09-26
| | | | | | | | | | | | | | | | | | | | Rust's libc crate exports the default off_t definition on 32-bit platforms, rather than the _FILE_OFFSET_BITS=64 variant, so we need to explicitly use the 64-bit API to get support for files larger than 2 GB. The Rust libc crate does not currently export fallocate64, so declare it ourselves for now. This declaration can be removed once fallocate64 is added upstream. BUG=chromium:850998 TEST=Run fstrim on Kevin (32-bit ARM) and verify it works Change-Id: Id0aa7a6e7e6080f4c53e10c3ad1d105f15ee2549 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1238850 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: remove Scm struct and sock_ctrl_msg C libraryZach Reizner2018-09-17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The Scm object was made to reduce the number of heap allocations in the hot paths of poll loops, at the cost of some code complexity. As it turns out, the number of file descriptors being sent or received is usually just one or limited to a fixed amount that can easily be covered with a fixed size stack allocated buffer. This change implements that solution, with heap allocation as a backup in the rare case that many file descriptors must be sent or received. This change also moves the msg and cmsg manipulation code out of C and into pure Rust. The move was necessary to allocate the correct amount of buffer space at compile time. It also improves safety by reducing the scope of unsafe code. Deleting the code for building the C library is also a nice bonus. Finally, the removal of the commonly used Scm struct required transitioning existing usage to the ScmSocket trait based methods. This includes all those changes. TEST=cargo test BUG=None Change-Id: If27ba297f5416dd9b8bc686ce740866912fa0aa0 Reviewed-on: https://chromium-review.googlesource.com/1186146 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: timerfd: Add ability to check if the timer is armedDylan Reid2018-09-17
| | | | | | | | | | This allows users to only arm timers if not already armed. Signed-off-by: Dylan Reid <dgreid@chromium.org> Change-Id: I8d7c6a7643a2ae2ce4b5679107bfd2be6e4adf3a Reviewed-on: https://chromium-review.googlesource.com/1214442 Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
* Arch: Big refactor and add an empty PCI busDylan Reid2018-09-10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When setting up IO, accept an optional PciRoot device to put on the IO bus. For aarch64, it's currently ignored. For x86_64, it will be added at 0xcf8. break up mmio device creation and registration Moving forward registration will be handled by the architecture specific code. However, creation will be handled by the common code. To make that easier split up the two steps so a list of devices is created, then each is registered later. Start moving to a model where the configuration generates a set of components that are passed to the architecture. The architecture will crate a VM from the components. Break up the big run_config function and move architecture specific parts to the various architectures. This doesn't refactor the function calls each architecture makes, but moves the setup flow in to the arch impls so that they can diverge in the future. Change-Id: I5b10d092896606796dc0c9afc5e34a1b288b867b Signed-off-by: Dylan Reid <dgreid@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1099860 Commit-Ready: Daniel Verkamp <dverkamp@chromium.org> Tested-by: Daniel Verkamp <dverkamp@chromium.org>
* sys_util: add WriteZeroes trait and impl for FileDaniel Verkamp2018-09-05
| | | | | | | | | | BUG=chromium:850998 TEST=cargo test -p sys_util write_zeroes Change-Id: I90f670fffed055f39e141b26819d56948b1bf30d Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1187017 Reviewed-by: Stephen Barber <smbarber@chromium.org>
* sys_util: add safe wrapper for fallocate()Daniel Verkamp2018-09-05
| | | | | | | | | | | BUG=chromium:850998 TEST=None Change-Id: I1b6864f7d508cf7f24248a8cc9783af2d8b00891 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1187016 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Stephen Barber <smbarber@chromium.org>
* sys_util: enable custom events in pollcontextJingkui Wang2018-08-06
| | | | | | | | | | | | | Can specify events for epoll now. BUG=chromium:831850 TEST=local build Change-Id: Iad418b5e9570b38fd8033276bf02ee3eb96d0d77 Reviewed-on: https://chromium-review.googlesource.com/1125293 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Jingkui Wang <jkwang@google.com> Reviewed-by: Zach Reizner <zachr@chromium.org>
* balloon: Implement device policyChirantan Ekbote2018-07-27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Implement a policy for the balloon device so that it starts taking memory away from the VM when the system is under low memory conditions. There are a few pieces here: * Change the madvise call in MemoryMapping::dont_need_range to use MADV_REMOVE instead of MADV_DONTNEED. The latter does nothing when the memory mapping is shared across multiple processes while the former immediately gives the pages in the specified range back to the kernel. Subsequent accesses to memory in that range returns zero pages. * Change the protocol between the balloon device process and the main crosvm process. Previously, the device process expected the main process to send it increments in the amount of memory consumed by the balloon device. Now, it instead just expects the absolute value of the memory that should be consumed. To properly implement the policy the main process needs to keep track of the total memory consumed by the balloon device so this makes it easier to handle all the policy in one place. * Add a policy for dealing with low memory situations. When the VM starts up, we determine the maximum amount of memory that the balloon device should consume: * If the VM has more than 1.5GB of memory, the balloon device max is the size of the VM memory minus 1GB. * Otherwise, if the VM has at least 500MB, the balloon device max is 50% of the size of the VM memory. * Otherwise, the max is 0. The increment used to change the size of the balloon is defined as 1/16 of the max memory that the balloon device will consume. When the crosvm main process detects that the system is low on memory, it immediately increases the balloon size by the increment (unless it has already reached the max). It then starts 2 timers: one to check for low memory conditions again in 1 seconds (+ jitter) and another to check if the system is no longer low on memory in 1 minute (+ jitter) with a subsequent interval of 30 seconds (+ jitter). Under persistent low memory conditions the balloon device will consume the maximum memory after 16 seconds. Once there is enough available memory the balloon size will shrink back down to 0 after at most 9 minutes. BUG=chromium:866193 TEST=manual Start 2 VMs and write out a large file (size > system RAM) in each. Observe /sys/kernel/mm/chromeos-low_mem/available and see that the available memory steadily decreases until it goes under the low memory margin at which point the available memory bounces back up as crosvm frees up pages. CQ-DEPEND=CL:1152214 Change-Id: I2046729683aa081c9d7ed039d902ad11737c1d52 Signed-off-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1149155 Reviewed-by: Sonny Rao <sonnyrao@chromium.org>
* sys_util: move round_to_page_size to sys_utilZach Reizner2018-06-21
| | | | | | | | | | | | | This function will be used elsewhere in gpu_display. TEST=None BUG=None Change-Id: I58b820511ea5a55a53ad640fdfe7c96d2dbdc73b Reviewed-on: https://chromium-review.googlesource.com/1105481 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* crosvm: Switch to cc crate.Manoj Gupta2018-05-18
| | | | | | | | | | | | | | | | We do not want to add dependencies on GCC. Switch to cc crate instead of gcc to honor CC setting. CQ-DEPEND=CL:1066462 BUG=chromium:814480 TEST=emerge-{eve,kevin} crosvm works. Change-Id: I4e846b2080503e5617ed66e709f7af5263c98fba Reviewed-on: https://chromium-review.googlesource.com/1066461 Commit-Ready: Manoj Gupta <manojgupta@chromium.org> Tested-by: Manoj Gupta <manojgupta@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: SharedMemory: fix signature for memfd_create syscallSonny Rao2018-05-09
| | | | | | | | | | | | | | | | The signature for this system call was wrong, but somehow managed to work on x86_64. This should fix it to work on all architectures. BUG=chromium:840048 TEST=build_test passes TEST=virtio wayland works on ARM Change-Id: I295548357f688be4772e65991fb65178ead3b1e8 Reviewed-on: https://chromium-review.googlesource.com/1050915 Commit-Ready: Sonny Rao <sonnyrao@chromium.org> Tested-by: Sonny Rao <sonnyrao@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* Fix signal handling in VCPU threadsMark Ryan2018-04-23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit addresses a number of issues with the way in which the SIGRTMIN() + 0 signal is used to kick VCPU threads. It 1. Moves the registration of the signal handler to the main thread. There's no need to register the handler once for each VCPU as there's one handler per process, rather than one per thread. 2. Ensures expect is not called in the VCPU thread before start_barrier.wait() is called. In the current code, failure to register the signal handler causes crosvm to hang rather than to exit as the VCPU thread panics before calling start_barrier.wait(). The main thread then blocks forever while waiting on the barrier. 3. Uses the KVM_SET_SIGNAL_MASK ioctl to remove a race condition in the current code. In the current code, a SIGRTMIN() + 0 signal, received during a vm exit, would be consumed before the next call to KVM_RUN, which would execute as normal and not be interrupted. This could delay the VM from stopping when requested to do so. Note that the new code doesn't unblock all signals during the call to KVM_RUN. It only unblocks SIGRTMIN() + 0. This is important as SIGCHILD is blocked at the start of run_config, and we probably don't want this unblocked periodically in each of the VCPU threads. TEST=run crosvm and stop it in both single and multi-process mode. BUG=none Signed-off-by: Mark Ryan <mark.d.ryan@intel.com> Change-Id: Ibda7d6220482aa11b2f5feee410d1d2b67a7e774 Reviewed-on: https://chromium-review.googlesource.com/1019443 Commit-Ready: Mark D Ryan <mark.d.ryan@intel.com> Tested-by: Mark D Ryan <mark.d.ryan@intel.com> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: handle EINTR return from epoll_waitZach Reizner2018-04-19
| | | | | | | | | | | | | | | The PollContext::wait returns Error on EINTR, which often happens during suspend/resume cycles. Because this Error is transient, this should be handled internally with a retry until a fatal error is encountered. BUG=chromium:834558 TEST=run crosvm, suspend, resume, observe crosvm still running Change-Id: I75469e261ddf28f025a3b3b93612538ccf1230b9 Reviewed-on: https://chromium-review.googlesource.com/1018527 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: remove deprecated Poller/Pollable interfaceZach Reizner2018-04-06
| | | | | | | | | | | | | Now that there are no users of that interface, we should remove it. TEST=./build_test BUG=chromium:816692 Change-Id: Ifdbde22984f557b945e49559ba47076e99db923b Reviewed-on: https://chromium-review.googlesource.com/1000103 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add method for copying PollEventsZach Reizner2018-04-06
| | | | | | | | | | | | | | | | Making a copy of PollEvents is useful to drop the PollEvents structure which borrows from a PollContext. Even though immutably borrowing from a PollContext does not prevent any operations on a PollContext, it does prevent mutable method calls on any structure that owns PollContext. TEST=None BUG=chromium:816692 Change-Id: I9527fd5c122a703933deb973ad549b792226e4c6 Reviewed-on: https://chromium-review.googlesource.com/1000101 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: use MADV_DONTDUMP for new mmapsZach Reizner2018-03-30
| | | | | | | | | | | | | | | | 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>
* sys_util: impl Pollable for FileZach Reizner2018-03-29
| | | | | | | | | | | | | | | Files are Pollable because they have an FD. Whether this makes sense for any specific `File` is not enforced, but it will never be unsafe or undefined when used with Poller. BUG=chromium:793688 TEST=None Change-Id: I2ce7ffd1b408bcee5ffbb3738d26339aa0c466e0 Reviewed-on: https://chromium-review.googlesource.com/985617 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org>
* sys_util: have Poller return token on POLLHUPZach Reizner2018-03-29
| | | | | | | | | | | | | | | | | | | | | If POLLHUP is filtered out of the returned tokens, the caller of Poller::poll will likely just put the same (token, fd) in the next call to poll which will return instantly. This degrades into a busy poll loop without the chance for the caller to change the poll list. Instead, this change changes the filter to return tokens on POLLHUP so that the caller will hopefully notice the FD associated with the token has been hungup and will close it. BUG=chromium:816692 TEST=None Change-Id: Ie36d8a647a5fd7faabfd57a562205f75c77991e7 Reviewed-on: https://chromium-review.googlesource.com/985616 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add type to get open file flagsZach Reizner2018-03-29
| | | | | | | | | | | | | | | The only instance of libstd getting file flags is the debug formatter for `File` which would be hacky to depend on. This change adds a type and method to directly get open file flags. TEST=cargo test -p sys_util BUG=chromium:793688 Change-Id: I9fe411d8cb45d2993e2334ffe41f2eb6ec48de70 Reviewed-on: https://chromium-review.googlesource.com/985615 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org>
* sys_util: add function for creating pipe pairsZach Reizner2018-03-29
| | | | | | | | | | | | | | | | | Rust's libstd only supports creating socket pairs or pipes for spawning processes. This change supports creating a unidirectional pipe pair for any purpose. BUG=chromium:793688 TEST=None Change-Id: Ie148735f18c5b8859d8981b9035d87f806a487ff Reviewed-on: https://chromium-review.googlesource.com/985614 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* crosvm: guest_memory: add memory_size methodSonny Rao2018-03-27
| | | | | | | | | | | | | | | This is useful to get the total size of memory without having to write something that iterates over the regions explicitly. BUG=chromium:797868 TEST=./build_test passes on all architectures TEST=crosvm runs on caroline Change-Id: Iac9a341b4c41d6462cf731f6267b92a0169578e4 Reviewed-on: https://chromium-review.googlesource.com/977565 Commit-Ready: Sonny Rao <sonnyrao@chromium.org> Tested-by: Sonny Rao <sonnyrao@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* poll_token_derive: Calculate variant bits without sizeof_valChirantan Ekbote2018-03-20
| | | | | | | | | | | | | | | | | Calculate the number of bits necessary to represent the enum variant using the next_power_of_two() and trailing_zeros() functions from the primitive usize type. Also add a test to ensure that the returned value is correct when there is only one variant in the enum. BUG=none TEST=unit tests Change-Id: Ibd15efd4f06e17a74489fee04ff19aca0dde68b2 Signed-off-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/959624 Reviewed-by: Zach Reizner <zachr@chromium.org>
* crosvm/plugin: refactor poll loop to use PollContextZach Reizner2018-03-08
| | | | | | | | | | | | | | | | | This change simplifies plugin processing by removing the awkward run_until_started loop. This also switches to use PollContext instead of the Poller/Pollable interface, which required reallocating a Vec every loop to satisfy the borrow checker. TEST=cargo test --features plugin BUG=chromium:816692 Change-Id: Iedf26a32840a9a038205c4be8d1adb2f1b565a5c Reviewed-on: https://chromium-review.googlesource.com/938653 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: custom derive for PollTokenZach Reizner2018-03-07
| | | | | | | | | | | | | | | | Using an enum implementing PollToken is the recommended way to use PollContext, but writing the trait impls for each enum is mechanical yet error prone. This is a perfect candidate for a custom derive, which automates away the process using a simple derive attribute on an enum. BUG=chromium:816692 TEST=cargo test -p sys_util Change-Id: If21d0f94f9af4b4f6cef1f24c78fc36b50471053 Reviewed-on: https://chromium-review.googlesource.com/940865 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
* sys_util: add PollContext interface for using epollZach Reizner2018-03-07
| | | | | | | | | | | | | | | | | | | A common cause of silent 100% CPU usage on otherwise idle VMs is because some poll loop is waiting on sockets that were hung up on. An unrelated issue is that using the Poller interface requires dynamic allocation on every poll call for dynamically sized poll lists. The PollContext struct detects and warns about the first problem at runtime and solves the latter problem. TEST=cargo test -p sys_util BUG=chromium:816692 Change-Id: I42a9c961db07191d25bcba77c5136f5729400ec9 Reviewed-on: https://chromium-review.googlesource.com/933870 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
* sys_util: register_signal_handler should use SA_RESTARTDmitry Torokhov2018-03-02
| | | | | | | | | | | | | | | | | On Linux, signal handlers installed with signal() will restart interrupted system calls. When we moved to using sigaction() we forgot to specify SA_RESTART and so we started experiencing returns from read write system calls with EINTR, which throws off some of the code. Instead of sprinkling "handle_eintr" everywhere, let's restore the old behavior. TEST=cargo test --features plugin; cargo test -p sys_util BUG=chromium:800626 Change-Id: I24c23069ad4c9e7be8c484ee4c57f67451a2944d Signed-off-by: Dmitry Torokhov <dtor@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/944848 Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: fix issue in ppoll on 32-bit architecturesSonny Rao2018-03-02
| | | | | | | | | | | | | | | | | I ran into an issue on ARM where ppoll() was returning EINVAL and it was becuase our timespec value sent to ppoll contained a negative value for tv_sec. We need to use the correct type when determining the max value. BUG=chromium:797868 TEST=./build_test passes on all architectures TEST=crosvm runs on caroline Change-Id: I7f8818e5f93e0327fd9facefb5032f7c5fb00ea0 Reviewed-on: https://chromium-review.googlesource.com/945111 Commit-Ready: Sonny Rao <sonnyrao@chromium.org> Tested-by: Sonny Rao <sonnyrao@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: fix handling EINTR of C system functionsDmitry Torokhov2018-03-01
| | | | | | | | | | | | | | | | | | System functions have 2 ways of signalling errors, either via returning -1 as result, and setting errno, or directly returning error code, and we can not distinguish automatically between the 2 options when using InterruptibleResult trait for i32 values. Let's remove this trait for i32 and create 2 explicit macros: handle_eintr_rc and handle_eintr_errno. TEST=cargo test --features plugin; cargo test -p sys_util BUG=None Change-Id: I1dc8e3c023e7bf7875ac3536703eb71fa3206b7b Signed-off-by: Dmitry Torokhov <dtor@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/940612 Reviewed-by: Zach Reizner <zachr@chromium.org>