summary refs log tree commit diff
path: root/sys_util/src/lib.rs
Commit message (Collapse)AuthorAge
* Add explicit `dyn` for trait objectsDaniel Verkamp2019-10-17
| | | | | | | | | | | | | | | Fix "trait objects without an explicit `dyn` are deprecated" warnings introduced in Rust 1.38. BUG=None TEST=emerge-nami crosvm Change-Id: I8ca6aa747475268ae898adddd5d091d401326ceb Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1862999 Reviewed-by: Dylan Reid <dgreid@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com>
* devices: jail serial deviceZach Reizner2019-10-10
| | | | | | | | | | | | | | | | | | This change plumbs the jail throughout the arch specific device creation process. It also adds a custom callback support for the ProxyDevice so that the main process can interrupt the child serial process when it has incoming bytes. TEST=crosvm run BUG=None Change-Id: I6af7d2cb0acbba9bf42eaeeb294cee2bce4a1f36 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1752589 Reviewed-by: Dylan Reid <dgreid@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Tested-by: Zach Reizner <zachr@chromium.org> Commit-Queue: Zach Reizner <zachr@chromium.org>
* sys_util: Add ReadWriteAtVolatile trait and `*vectored` functionsChirantan Ekbote2019-10-07
| | | | | | | | | | | | | | | | | | | | | | | | | | Add the FileReadWriteAtVolatile trait, which is basically the same as the FileReadWriteVolatile trait but additionally takes an offest. This is only useful for types that are seekable and can allow concurrent operations on the same underlying type. Also add `*_vectored` versions of all the functions. These match the `*_vectored` functions in the standard library and can reduce the number of system calls needed to read or write a whole buffer. Implement both traits for `&mut T` if `T` implements them. Change the trait implementation for `File` to a macro so that we can also implement it for `GuestMemory`. BUG=b:136128319 TEST=unit tests Change-Id: I3d8eb7bba17fe3247e18649b1b04e21a91a841e2 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1724229 Auto-Submit: Chirantan Ekbote <chirantan@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
* Replace "AsRawFd" with "AsRawFds" for disks.Cody Schuffelen2019-08-30
| | | | | | | | | | | | This supports virtio disks that depend on multiple file descriptors. All of the file descriptors are passed to the jail when relevant. Bug: b/133432409 Change-Id: Idf2e24cd2984c0d12a47a523c13d24c1ba8d173e Signed-off-by: Cody Schuffelen <schuffelen@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1691761 Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com>
* tempfile: Unify the two tempdir implementationsDavid Tolnay2019-07-11
| | | | | | | | | | | | | | | | | | | | | | Looks like we ended up with two totally different tempdir implementations: one from CL:520706 and the other from CL:1409705. This CL consolidates them into one implementation. BUG=chromium:974059 TEST=tempfile: cargo test TEST=crosvm: cargo check --all-features TEST=devices: cargo check --tests TEST=sys_util: cargo check --tests TEST=local kokoro TEST=./build_test Cq-Depend: chromium:1574668 Change-Id: Id70e963c9986ed2fc5f160819c4a7f9f16092b3b Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1573227 Tested-by: kokoro <noreply+kokoro@google.com> Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
* eliminate mut from non-mut referencesZach Reizner2019-06-04
| | | | | | | | | | | | | | | | | | | | | | | This manifested itself in a couple places that were turning shared memory buffers into slices for the purposes of passing these slices to `Read` and `Write` trait methods. However, this required the removal of the methods that took `Read` and `Write` instances. This was a convenient interface but impossible to implement safely because making slices from raw pointers without enforcing safety guarantees causes undefined behaviour in Rust. It turns out lots of code in crosvm was using these interfaces indirectly, which explains why this CL touches so much. TEST=crosvm run BUG=chromium:938767 Change-Id: I4ff40c98da6ed08a4a42f4c31f0717f81b1c5863 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1636685 Reviewed-by: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Zach Reizner <zachr@chromium.org>
* clippy: Resolve cast_ptr_alignmentDavid Tolnay2019-04-18
| | | | | | | | | | | | | | | | | | | | | | | | | | This CL fixes four cases of what I believe are undefined behavior: - In vhost where the original code allocates a Vec<u8> with 1-byte alignment and casts the Vec's data pointer to a &mut vhost_memory which is required to be 8-byte aligned. Underaligned references of type &T or &mut T are always undefined behavior in Rust. - Same pattern in x86_64. - Same pattern in plugin::vcpu. - Code in crosvm_plugin that dereferences a potentially underaligned pointer. This is always undefined behavior in Rust. TEST=bin/clippy TEST=cargo test sys_util Change-Id: I926f17b1fe022a798f69d738f9990d548f40c59b Reviewed-on: https://chromium-review.googlesource.com/1566736 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
* edition: Remove extern crate linesDavid Tolnay2019-04-15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In Rust 2018 edition, `extern crate` is no longer required for importing from other crates. Instead of writing: extern crate dep; use dep::Thing; we write: use dep::Thing; In this approach, macros are imported individually from the declaring crate rather than through #[macro_use]. Before: #[macro_use] extern crate sys_util; After: use sys_util::{debug, error}; The only place that `extern crate` continues to be required is in importing the compiler's proc_macro API into a procedural macro crate. This will hopefully be fixed in a future Rust release. extern crate proc_macro; TEST=cargo check TEST=cargo check --all-features TEST=cargo check --target aarch64-unknown-linux-gnu TEST=local kokoro Change-Id: I0b43768c0d81f2a250b1959fb97ba35cbac56293 Reviewed-on: https://chromium-review.googlesource.com/1565302 Commit-Ready: David Tolnay <dtolnay@chromium.org> Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
* main: add --cpu-affinity option to pin VCPUsDaniel Verkamp2019-04-09
| | | | | | | | | | | | | | | | | | This allows setting the affinity of the VCPU threads to specific host CPUs. Note that each individual CPU has its affinity set to the full set of CPUs specified, so the host kernel may still reschedule VCPU threads on whichever host CPUs it sees fit (within the specified set). BUG=chromium:909793 TEST=build_test Change-Id: I09b893901caf91368b64f5329a6e9f39027fef23 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1554865 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* edition: Use dyn syntax for trait objectsDavid Tolnay2019-04-08
| | | | | | | | | | | | | | | | | | | | | Found by running: `cargo rustc -- -D bare_trait_objects` Bare trait objects like `&Trait` and `Box<Trait>` are soft-deprecated in 2018 edition and will start warning at some point. As part of this, I replaced `Box<Trait + 'static>` with `Box<dyn Trait>` because the 'static bound is implied for boxed trait objects. TEST=cargo check --all-features TEST=cargo check --target aarch64-unknown-linux-gnu TEST=local kokoro Change-Id: I41c4f13530bece8a34a8ed1c1afd7035b8f86f19 Reviewed-on: https://chromium-review.googlesource.com/1513059 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
* edition: Update absolute paths to 2018 styleDavid Tolnay2019-03-13
| | | | | | | | | | | | | | | | | | | | | | | | | | This is an easy step toward adopting 2018 edition eventually, and will make any future CL that sets `edition = "2018"` this much smaller. The module system changes in Rust 2018 are described here: https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html Generated by running: cargo fix --edition --all in each workspace, followed by bin/fmt. TEST=cargo check TEST=cargo check --all-features TEST=cargo check --target aarch64-unknown-linux-gnu Change-Id: I000ab5e69d69aa222c272fae899464bbaf65f6d8 Reviewed-on: https://chromium-review.googlesource.com/1513054 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org>
* Drop capabilities before spawning any vcpu threadDmitry Torokhov2019-03-08
| | | | | | | | | | | | | | | | | | In case crosvm starts with elevated capabilities (for example, we need to start with CAP_SETGID to be able to map additional gids into plugin jail), we should drop them before spawning VCPU threads. BUG=b:117989168 TEST=Start plugin via concierge_client and verify the process does not have any effective or permitted privileges. tast run [] 'vm.*' Change-Id: Ia1e80bfe19b296936d77fe9ffeda361211b41eed Reviewed-on: https://chromium-review.googlesource.com/1506296 Commit-Ready: Dmitry Torokhov <dtor@chromium.org> Tested-by: Dmitry Torokhov <dtor@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
* error: Print errors using Display implDavid Tolnay2019-02-20
| | | | | | | | | | | | | | | | | | | | | | | | | I have been running into Debug-printed error messages too often and needing to look up in the source code each level of nested errors to find out from the comment on the error variant what the short name of the variant means in human terms. Worse, many errors (like the one shown below) already had error strings written but were being printed from the calling code in the less helpful Debug representation anyway. Before: [ERROR:src/main.rs:705] The architecture failed to build the vm: NoVarEmpty After: [ERROR:src/main.rs:705] The architecture failed to build the vm: /var/empty doesn't exist, can't jail devices. TEST=cargo check --all-features TEST=FEATURES=test emerge-amd64-generic crosvm Change-Id: I77122c7d6861b2d610de2fff718896918ab21e10 Reviewed-on: https://chromium-review.googlesource.com/1469225 Commit-Ready: David Tolnay <dtolnay@chromium.org> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
* Add FakeClock and FakeTimerFd for use in tests.Miriam Zimmerman2019-01-24
| | | | | | | | | | | | | | | | | Together, these allow tests to create a FakeTimerFd that they can trigger at a particular point in the test code, without having to rely on sleep()s or other racy methods. BUG=None TEST=Unit tests for FakeTimerFd + dependent CL. Change-Id: I14381272a6d75bebcdedb0a329a017a2131a3482 Reviewed-on: https://chromium-review.googlesource.com/1413830 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Miriam Zimmerman <mutexlox@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: David Tolnay <dtolnay@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* 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>
* 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>
* 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>
* 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>
* 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>
* 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>
* 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>
* 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>
* 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>
* kvm: plumb in KVM_SET_SIGNAL_MASK ioctlDmitry Torokhov2018-02-26
| | | | | | | | | | | | We need this ioctl to implement race-free support for kicking/pausing VCPUs. TEST=cargo test --features plugin; cargo test -p kvm; ./build_test BUG=chromium:800626 Change-Id: I5dcff54f7eb34568a8d8503e0dde86b6a36ac693 Signed-off-by: Dmitry Torokhov <dtor@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/932443 Reviewed-by: Zach Reizner <zachr@chromium.org>
* crosvm: add advisory locking for disk imagesStephen Barber2018-02-21
| | | | | | | | | | | | | | | Disk images should never be mounted as writable by multiple VMs at once. Add advisory locking to prevent this. BUG=chromium:810576 TEST=run crosvm twice with same rwdisk, check that second VM fails to start Change-Id: I5e6c178515eafa570812a093449eef5a4edc1740 Reviewed-on: https://chromium-review.googlesource.com/929994 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: replace sysconf(_SC_PAGESIZE) with a safe wrapperZach Reizner2018-01-10
| | | | | | | | | | | | | | | | There were a few places that used this to get the page size inside of an unsafe block, For convenience, this adds a safe wrapper in sys_util and replaces all extant usage of sysconf with the wrapper version. BUG=chromium:800626 TEST=./build_test Change-Id: Ic65bf72aea90eabd4158fbdcdbe25c3f13ca93ac Reviewed-on: https://chromium-review.googlesource.com/857907 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 chown wrapperStephen Barber2017-10-26
| | | | | | | | | | | BUG=none TEST=compile Change-Id: I3ae66955b48c8cd7ae2f468da8780c85fce539d2 Reviewed-on: https://chromium-review.googlesource.com/733731 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org>
* sys_util: add get_user_id and get_group_id functionsStephen Barber2017-10-26
| | | | | | | | | | | | | Add safe wrappers for getpwnam_r and getgrnam_r. BUG=none TEST=./build_test Change-Id: I737b4d264334ed788884a7320f5649cfc2266709 Reviewed-on: https://chromium-review.googlesource.com/733730 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* fix armv7a and aarch64 build errors and warningsZach Reizner2017-09-01
| | | | | | | | | | | | | | BUG=None TEST=cargo build --target=armv7a-cros-linux-gnueabi && cargo build --target=aarch64-cros-linux-gnu Change-Id: I954c152f3c8086e24c4809dd5aabb5043fdd63af Reviewed-on: https://chromium-review.googlesource.com/644408 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Jason Clinton <jclinton@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add safe wrappers getpid,geteuid,getguid,waitpid,killZach Reizner2017-08-28
| | | | | | | | | | | | | | | | | | These functions are trivially safe and by adding them to sys_util, we can remove some unsafe blocks from crosvm. This CL also replaces the unsafe call sites with the safe alternatives. There are no previous usages of gete{g,u}id(2), but they will be needed in a future change. TEST=None BUG=None Change-Id: Ief8787b298cfaa5b7fd1b83f0eba6660369e687d Reviewed-on: https://chromium-review.googlesource.com/634268 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* crosvm: refactor and expand vm control socket IPCZach Reizner2017-08-25
| | | | | | | | | | | | | | | | This CL adds VM request capabilities to the control socket. These requests include the basic exit as well as the essential ioeventfd and irqfd requests. For virtio wayland, the register/unregister device memory request was added. TEST=cargo test BUG=chromium:738638 Change-Id: I0cbf62d85a299cf454bcf6924a4e1d52d5b7183f Reviewed-on: https://chromium-review.googlesource.com/602593 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add sock_ctrl_msg module for transmitting messages with FDZach Reizner2017-07-20
| | | | | | | | | | | | | | | This CL also includes the gcc build time dependency for building the sock_ctrl_msg.c helper code. TEST=cargo test BUG=chromium:738638 Change-Id: I4adc2360b7fab4ed7d557603aa7bad2e738b69b4 Reviewed-on: https://chromium-review.googlesource.com/562574 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add ioctl moduleStephen Barber2017-07-13
| | | | | | | | | | | | | | | | Factor out common ioctl wrappers and macros into their own module. Signed-off-by: Stephen Barber <smbarber@chromium.org> BUG=none TEST=cargo test Change-Id: Ibede8a853f5cc6c6d62565930f312b11024cc5b5 Reviewed-on: https://chromium-review.googlesource.com/566540 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: add signalfd moduleStephen Barber2017-07-13
| | | | | | | | | | | | | | | | Dealing with signals is unpleasant business. SignalFd wraps a kernel signalfd for a signal, and blocks the default handling for the signal. Signed-off-by: Stephen Barber <smbarber@chromium.org> BUG=none TEST=cargo test Change-Id: I161c992b65b98ffa5c07d546f13efa6b56890df4 Reviewed-on: https://chromium-review.googlesource.com/557459 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: add fork module for cloning processZach Reizner2017-07-13
| | | | | | | | | | | | | | | This module is designed as a safe wrapper around clone for use by the proxy device. It includes safe guards that prevent errors with typical forking applications in rust. TEST=cargo test BUG=None Change-Id: I09132a4cae61ebdaa97ec3b95d22567c36c5f15d Reviewed-on: https://chromium-review.googlesource.com/518446 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add shared memory moduleZach Reizner2017-07-10
| | | | | | | | | | | | | | This CL adds the SharedMemory structure for managing memory attached to a file descriptor, which can be sent to external processes. TEST=cargo test BUG=chromium:738638 Change-Id: I2b6a10e0e7275367ddb578831a73de6956d231bb Reviewed-on: https://chromium-review.googlesource.com/562509 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>
* sys_util: add syslog module for logging to syslogZach Reizner2017-07-05
| | | | | | | | | | | TEST=cargo test BUG=None Change-Id: Ia7ae04976d0ccbabd6d1656ddd9275f16cce218a Reviewed-on: https://chromium-review.googlesource.com/553741 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
* sys_util: add signal module for killing threadsZach Reizner2017-06-30
| | | | | | | | | | | | | | | | The signal module is used for registering signal handlers and for signalling threads. Normally signals would be a method of last resort, but in this case it's the only possible way to trigger a VM exit on a thread currently inside of a KVM_RUN call. BUG=None TEST=None Change-Id: If1db1e17937d1af08fc24b422c460be754cf9d22 Reviewed-on: https://chromium-review.googlesource.com/514415 Commit-Ready: Zach Reizner <zachr@chromium.org> Tested-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org>