From 95885316c25147ffe85700e4db5477aa8e516005 Mon Sep 17 00:00:00 2001 From: Zach Reizner Date: Wed, 29 Jan 2020 18:06:01 -0800 Subject: linux: use ScopedEvent to trigger the `exit_evt` in vcpu threads The VCPU thread can panic when there is a bug, and this should trigger an exit in crosvm instead of hanging the VCPU thread forever. BUG=None TEST=run crosvm with kernel, trigger a vcpu exit using guest Change-Id: I1df0e04eda73ad67fec20adeff893f6c00699318 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2029929 Reviewed-by: Dylan Reid Reviewed-by: Stephen Barber Tested-by: kokoro Tested-by: Zach Reizner Commit-Queue: Zach Reizner --- src/linux.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/linux.rs b/src/linux.rs index 5d060b4..144345e 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -50,8 +50,8 @@ use sys_util::{ self, block_signal, clear_signal, drop_capabilities, error, flock, get_blocked_signals, get_group_id, get_user_id, getegid, geteuid, info, register_rt_signal_handler, set_cpu_affinity, validate_raw_fd, warn, EventFd, FlockOperation, GuestAddress, GuestMemory, - Killable, MemoryMappingArena, PollContext, PollToken, Protection, SignalFd, Terminal, TimerFd, - WatchingEvents, SIGRTMIN, + Killable, MemoryMappingArena, PollContext, PollToken, Protection, ScopedEvent, SignalFd, + Terminal, TimerFd, WatchingEvents, SIGRTMIN, }; use vhost; use vm_control::{ @@ -1325,6 +1325,10 @@ fn run_vcpu( thread::Builder::new() .name(format!("crosvm_vcpu{}", cpu_id)) .spawn(move || { + // The VCPU thread must trigger the `exit_evt` in all paths, and a `ScopedEvent`'s Drop + // implementation accomplishes that. + let _scoped_exit_evt = ScopedEvent::from(exit_evt); + if vcpu_affinity.len() != 0 { if let Err(e) = set_cpu_affinity(vcpu_affinity) { error!("Failed to set CPU affinity: {}", e); @@ -1336,7 +1340,7 @@ fn run_vcpu( start_barrier.wait(); if let Some(vcpu) = vcpu { - 'vcpu_loop: loop { + loop { let mut interrupted_by_signal = false; match vcpu.run() { Ok(VcpuExit::IoIn { port, mut size }) => { @@ -1421,7 +1425,7 @@ fn run_vcpu( } } } - VmRunMode::Exiting => break 'vcpu_loop, + VmRunMode::Exiting => return, } // Give ownership of our exclusive lock to the condition variable that // will block. When the condition variable is notified, `wait` will @@ -1431,9 +1435,6 @@ fn run_vcpu( } } } - exit_evt - .write(1) - .expect("failed to signal vcpu exit eventfd"); }) .map_err(Error::SpawnVcpu) } -- cgit 1.4.1