diff options
author | Stephen Barber <smbarber@chromium.org> | 2017-10-29 23:13:48 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-10-30 23:21:37 -0700 |
commit | ce374793bf1b40838c7588620ac5b3784fa4997a (patch) | |
tree | 71d908d9c27b9b5d1422e45647d90be1e5a3c1a6 | |
parent | d657af628a2f0967d8a004946abde3ed34dd6dd7 (diff) | |
download | crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.tar crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.tar.gz crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.tar.bz2 crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.tar.lz crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.tar.xz crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.tar.zst crosvm-ce374793bf1b40838c7588620ac5b3784fa4997a.zip |
crosvm/devices: set thread names
crosvm spawns a lot of processes/threads, and having these all use the same name as the original process can be confusing. So at least in the instances where Rust threads are spawned (vs. minijail_fork()), use a thread::Builder to allow setting the thread name. BUG=none TEST=start crosvm, check thread names with top Change-Id: I6e55ff5fd60f258880bda8e656ab7f9da82c656e Reviewed-on: https://chromium-review.googlesource.com/742394 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Stephen Barber <smbarber@chromium.org>
-rw-r--r-- | devices/src/virtio/block.rs | 29 | ||||
-rw-r--r-- | devices/src/virtio/net.rs | 55 | ||||
-rw-r--r-- | devices/src/virtio/rng.rs | 29 | ||||
-rw-r--r-- | devices/src/virtio/vhost/net.rs | 51 | ||||
-rw-r--r-- | devices/src/virtio/vhost/vsock.rs | 52 | ||||
-rw-r--r-- | devices/src/virtio/wl.rs | 29 | ||||
-rw-r--r-- | seccomp/x86_64/block_device.policy | 2 | ||||
-rw-r--r-- | seccomp/x86_64/net_device.policy | 2 | ||||
-rw-r--r-- | seccomp/x86_64/rng_device.policy | 2 | ||||
-rw-r--r-- | seccomp/x86_64/vhost_net_device.policy | 2 | ||||
-rw-r--r-- | seccomp/x86_64/vhost_vsock_device.policy | 2 | ||||
-rw-r--r-- | seccomp/x86_64/wl_device.policy | 2 | ||||
-rw-r--r-- | src/main.rs | 11 |
13 files changed, 162 insertions, 106 deletions
diff --git a/devices/src/virtio/block.rs b/devices/src/virtio/block.rs index 1af2693..ed78849 100644 --- a/devices/src/virtio/block.rs +++ b/devices/src/virtio/block.rs @@ -9,7 +9,7 @@ use std::os::unix::io::{AsRawFd, RawFd}; use std::result; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::thread::spawn; +use std::thread; use sys_util::Result as SysResult; use sys_util::{EventFd, GuestAddress, GuestMemory, GuestMemoryError, Poller}; @@ -364,16 +364,23 @@ impl VirtioDevice for Block { self.kill_evt = Some(self_kill_evt); if let Some(disk_image) = self.disk_image.take() { - spawn(move || { - let mut worker = Worker { - queues: queues, - mem: mem, - disk_image: disk_image, - interrupt_status: status, - interrupt_evt: interrupt_evt, - }; - worker.run(queue_evts.remove(0), kill_evt); - }); + let worker_result = thread::Builder::new() + .name("virtio_blk".to_string()) + .spawn(move || { + let mut worker = Worker { + queues: queues, + mem: mem, + disk_image: disk_image, + interrupt_status: status, + interrupt_evt: interrupt_evt, + }; + worker.run(queue_evts.remove(0), kill_evt); + }); + + if let Err(e) = worker_result { + error!("failed to spawn virtio_blk worker: {}", e); + return; + } } } } diff --git a/devices/src/virtio/net.rs b/devices/src/virtio/net.rs index d632dd6..a4df8c9 100644 --- a/devices/src/virtio/net.rs +++ b/devices/src/virtio/net.rs @@ -9,7 +9,7 @@ use std::net::Ipv4Addr; use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::thread::spawn; +use std::thread; use libc::EAGAIN; use net_sys; @@ -403,29 +403,36 @@ impl VirtioDevice for Net { if let Some(tap) = self.tap.take() { if let Some(kill_evt) = self.workers_kill_evt.take() { let acked_features = self.acked_features; - spawn(move || { - // First queue is rx, second is tx. - let rx_queue = queues.remove(0); - let tx_queue = queues.remove(0); - let mut worker = Worker { - mem: mem, - rx_queue: rx_queue, - tx_queue: tx_queue, - tap: tap, - interrupt_status: status, - interrupt_evt: interrupt_evt, - rx_buf: [0u8; MAX_BUFFER_SIZE], - rx_count: 0, - deferred_rx: false, - acked_features: acked_features, - }; - let rx_queue_evt = queue_evts.remove(0); - let tx_queue_evt = queue_evts.remove(0); - let result = worker.run(rx_queue_evt, tx_queue_evt, kill_evt); - if let Err(e) = result { - error!("net worker thread exited with error: {:?}", e); - } - }); + let worker_result = thread::Builder::new() + .name("virtio_net".to_string()) + .spawn(move || { + // First queue is rx, second is tx. + let rx_queue = queues.remove(0); + let tx_queue = queues.remove(0); + let mut worker = Worker { + mem: mem, + rx_queue: rx_queue, + tx_queue: tx_queue, + tap: tap, + interrupt_status: status, + interrupt_evt: interrupt_evt, + rx_buf: [0u8; MAX_BUFFER_SIZE], + rx_count: 0, + deferred_rx: false, + acked_features: acked_features, + }; + let rx_queue_evt = queue_evts.remove(0); + let tx_queue_evt = queue_evts.remove(0); + let result = worker.run(rx_queue_evt, tx_queue_evt, kill_evt); + if let Err(e) = result { + error!("net worker thread exited with error: {:?}", e); + } + }); + + if let Err(e) = worker_result { + error!("failed to spawn virtio_net worker: {}", e); + return; + } } } } diff --git a/devices/src/virtio/rng.rs b/devices/src/virtio/rng.rs index 292d3f1..da5d40b 100644 --- a/devices/src/virtio/rng.rs +++ b/devices/src/virtio/rng.rs @@ -8,7 +8,7 @@ use std::io; use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::thread::spawn; +use std::thread; use sys_util::{EventFd, GuestMemory, Poller}; @@ -172,16 +172,23 @@ impl VirtioDevice for Rng { let queue = queues.remove(0); if let Some(random_file) = self.random_file.take() { - spawn(move || { - let mut worker = Worker { - queue: queue, - mem: mem, - random_file: random_file, - interrupt_status: status, - interrupt_evt: interrupt_evt, - }; - worker.run(queue_evts.remove(0), kill_evt); - }); + let worker_result = thread::Builder::new() + .name("virtio_rng".to_string()) + .spawn(move || { + let mut worker = Worker { + queue: queue, + mem: mem, + random_file: random_file, + interrupt_status: status, + interrupt_evt: interrupt_evt, + }; + worker.run(queue_evts.remove(0), kill_evt); + }); + + if let Err(e) = worker_result { + error!("failed to spawn virtio_rng worker: {}", e); + return; + } } } } diff --git a/devices/src/virtio/vhost/net.rs b/devices/src/virtio/vhost/net.rs index 6fc978e..473c7e8 100644 --- a/devices/src/virtio/vhost/net.rs +++ b/devices/src/virtio/vhost/net.rs @@ -7,7 +7,7 @@ use std::net::Ipv4Addr; use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::Arc; use std::sync::atomic::AtomicUsize; -use std::thread::spawn; +use std::thread; use net_sys; use net_util::Tap; @@ -173,29 +173,34 @@ impl VirtioDevice for Net { if let Some(vhost_interrupt) = self.vhost_interrupt.take() { if let Some(kill_evt) = self.workers_kill_evt.take() { let acked_features = self.acked_features; - spawn(move || { - let mut worker = Worker::new( - queues, - vhost_net_handle, - vhost_interrupt, - status, - interrupt_evt, - acked_features, - ); - let activate_vqs = |handle: &VhostNetHandle| -> Result<()> { - for idx in 0..NUM_QUEUES { - handle - .set_backend(idx, &tap) - .map_err(Error::VhostNetSetBackend)?; + let worker_result = thread::Builder::new() + .name("vhost_net".to_string()) + .spawn(move || { + let mut worker = Worker::new(queues, + vhost_net_handle, + vhost_interrupt, + status, + interrupt_evt, + acked_features); + let activate_vqs = |handle: &VhostNetHandle| -> Result<()> { + for idx in 0..NUM_QUEUES { + handle + .set_backend(idx, &tap) + .map_err(Error::VhostNetSetBackend)?; + } + Ok(()) + }; + let result = + worker.run(queue_evts, QUEUE_SIZES, kill_evt, activate_vqs); + if let Err(e) = result { + error!("net worker thread exited with error: {:?}", e); } - Ok(()) - }; - let result = - worker.run(queue_evts, QUEUE_SIZES, kill_evt, activate_vqs); - if let Err(e) = result { - error!("net worker thread exited with error: {:?}", e); - } - }); + }); + + if let Err(e) = worker_result { + error!("failed to spawn vhost_net worker: {}", e); + return; + } } } } diff --git a/devices/src/virtio/vhost/vsock.rs b/devices/src/virtio/vhost/vsock.rs index a3c24aa..b55b6a0 100644 --- a/devices/src/virtio/vhost/vsock.rs +++ b/devices/src/virtio/vhost/vsock.rs @@ -5,7 +5,7 @@ use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::Arc; use std::sync::atomic::AtomicUsize; -use std::thread::spawn; +use std::thread; use byteorder::{ByteOrder, LittleEndian}; @@ -182,28 +182,34 @@ impl VirtioDevice for Vsock { if let Some(kill_evt) = self.worker_kill_evt.take() { let acked_features = self.acked_features; let cid = self.cid; - spawn(move || { - // The third vq is an event-only vq that is not handled by the vhost - // subsystem (but still needs to exist). Split it off here. - let vhost_queues = queues[..2].to_vec(); - let mut worker = Worker::new( - vhost_queues, - vhost_handle, - interrupt, - status, - interrupt_evt, - acked_features, - ); - let activate_vqs = |handle: &VhostVsockHandle| -> Result<()> { - handle.set_cid(cid).map_err(Error::VhostVsockSetCid)?; - handle.start().map_err(Error::VhostVsockStart)?; - Ok(()) - }; - let result = worker.run(queue_evts, QUEUE_SIZES, kill_evt, activate_vqs); - if let Err(e) = result { - error!("vsock worker thread exited with error: {:?}", e); - } - }); + let worker_result = thread::Builder::new() + .name("vhost_vsock".to_string()) + .spawn(move || { + // The third vq is an event-only vq that is not handled by the vhost + // subsystem (but still needs to exist). Split it off here. + let vhost_queues = queues[..2].to_vec(); + let mut worker = Worker::new(vhost_queues, + vhost_handle, + interrupt, + status, + interrupt_evt, + acked_features); + let activate_vqs = |handle: &VhostVsockHandle| -> Result<()> { + handle.set_cid(cid).map_err(Error::VhostVsockSetCid)?; + handle.start().map_err(Error::VhostVsockStart)?; + Ok(()) + }; + let result = + worker.run(queue_evts, QUEUE_SIZES, kill_evt, activate_vqs); + if let Err(e) = result { + error!("vsock worker thread exited with error: {:?}", e); + } + }); + + if let Err(e) = worker_result { + error!("failed to spawn vhost_vsock worker: {}", e); + return; + } } } } diff --git a/devices/src/virtio/wl.rs b/devices/src/virtio/wl.rs index 1ea384c..c5a67d7 100644 --- a/devices/src/virtio/wl.rs +++ b/devices/src/virtio/wl.rs @@ -44,7 +44,7 @@ use std::rc::Rc; use std::result; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::thread::spawn; +use std::thread; use data_model::*; use data_model::VolatileMemoryError; @@ -1018,16 +1018,23 @@ impl VirtioDevice for Wl { if let Some(vm_socket) = self.vm_socket.take() { let wayland_path = self.wayland_path.clone(); - spawn(move || { - Worker::new(mem, - interrupt_evt, - status, - queues.remove(0), - queues.remove(0), - wayland_path, - vm_socket) - .run(queue_evts, kill_evt); - }); + let worker_result = thread::Builder::new() + .name("virtio_wl".to_string()) + .spawn(move || { + Worker::new(mem, + interrupt_evt, + status, + queues.remove(0), + queues.remove(0), + wayland_path, + vm_socket) + .run(queue_evts, kill_evt); + }); + + if let Err(e) = worker_result { + error!("failed to spawn virtio_wl worker: {}", e); + return; + } } } } diff --git a/seccomp/x86_64/block_device.policy b/seccomp/x86_64/block_device.policy index 581169a..fa35ea3 100644 --- a/seccomp/x86_64/block_device.policy +++ b/seccomp/x86_64/block_device.policy @@ -24,3 +24,5 @@ write: 1 eventfd2: 1 poll: 1 getpid: 1 +# Allow PR_SET_NAME only. +prctl: arg0 == 15 diff --git a/seccomp/x86_64/net_device.policy b/seccomp/x86_64/net_device.policy index 23abcfe..1718f4d 100644 --- a/seccomp/x86_64/net_device.policy +++ b/seccomp/x86_64/net_device.policy @@ -23,3 +23,5 @@ sigaltstack: 1 clone: arg0 & 0x00010000 write: 1 getpid: 1 +# Allow PR_SET_NAME only. +prctl: arg0 == 15 diff --git a/seccomp/x86_64/rng_device.policy b/seccomp/x86_64/rng_device.policy index 429e94d..e320acf 100644 --- a/seccomp/x86_64/rng_device.policy +++ b/seccomp/x86_64/rng_device.policy @@ -23,3 +23,5 @@ write: 1 eventfd2: 1 poll: 1 getpid: 1 +# Allow PR_SET_NAME only. +prctl: arg0 == 15 diff --git a/seccomp/x86_64/vhost_net_device.policy b/seccomp/x86_64/vhost_net_device.policy index 6e61bba..02be1d5 100644 --- a/seccomp/x86_64/vhost_net_device.policy +++ b/seccomp/x86_64/vhost_net_device.policy @@ -40,3 +40,5 @@ sigaltstack: 1 clone: arg0 & 0x00010000 write: 1 getpid: 1 +# Allow PR_SET_NAME only. +prctl: arg0 == 15 diff --git a/seccomp/x86_64/vhost_vsock_device.policy b/seccomp/x86_64/vhost_vsock_device.policy index fe54042..cbbdae0 100644 --- a/seccomp/x86_64/vhost_vsock_device.policy +++ b/seccomp/x86_64/vhost_vsock_device.policy @@ -43,3 +43,5 @@ sigaltstack: 1 clone: arg0 & 0x00010000 write: 1 getpid: 1 +# Allow PR_SET_NAME only. +prctl: arg0 == 15 diff --git a/seccomp/x86_64/wl_device.policy b/seccomp/x86_64/wl_device.policy index 7f1ee1b..7ec8220 100644 --- a/seccomp/x86_64/wl_device.policy +++ b/seccomp/x86_64/wl_device.policy @@ -34,3 +34,5 @@ memfd_create: arg1 == 1 ftruncate: 1 # Used to determine shm size after recvmsg with fd lseek: 1 +# Allow PR_SET_NAME only. +prctl: arg0 == 15 diff --git a/src/main.rs b/src/main.rs index 571d27d..caaf3de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,8 @@ use std::path::{Path, PathBuf}; use std::string::String; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, Barrier}; -use std::thread::{spawn, sleep, JoinHandle}; +use std::thread; +use std::thread::{sleep, JoinHandle}; use std::time::Duration; use io_jail::Minijail; @@ -79,6 +80,7 @@ enum Error { Kvm(sys_util::Error), Vm(sys_util::Error), Vcpu(sys_util::Error), + SpawnVcpu(std::io::Error), Sys(sys_util::Error), } @@ -145,6 +147,7 @@ impl fmt::Display for Error { &Error::Kvm(ref e) => write!(f, "error creating Kvm: {:?}", e), &Error::Vm(ref e) => write!(f, "error creating Vm: {:?}", e), &Error::Vcpu(ref e) => write!(f, "error creating Vcpu: {:?}", e), + &Error::SpawnVcpu(ref e) => write!(f, "error creating spawning Vcpu: {}", e), &Error::Sys(ref e) => write!(f, "error with system call: {:?}", e), } } @@ -598,7 +601,9 @@ fn run_kvm(requests: Vec<VmRequest>, &vcpu, cpu_id as u64, vcpu_count as u64)?; - vcpu_handles.push(spawn(move || { + vcpu_handles.push(thread::Builder::new() + .name(format!("crosvm_vcpu{}", cpu_id)) + .spawn(move || { unsafe { extern "C" fn handle_signal() {} // Our signal handler does nothing and is trivially async signal safe. @@ -642,7 +647,7 @@ fn run_kvm(requests: Vec<VmRequest>, vcpu_exit_evt .write(1) .expect("failed to signal vcpu exit eventfd"); - })); + }).map_err(Error::SpawnVcpu)?); } vcpu_thread_barrier.wait(); |