Skip to content

Commit 7ee2175

Browse files
committed
refactor: move vcpu signal handler setup to vcpu init
Move setting of signal handler into vcpu init to prevent race condition between setting TLS and signal handler using TLS. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent ebfe1c6 commit 7ee2175

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/vmm/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,6 @@ impl Vmm {
375375
})?;
376376
}
377377

378-
Vcpu::register_kick_signal_handler();
379-
380378
self.vcpus_handles.reserve(vcpu_count);
381379

382380
for mut vcpu in vcpus.drain(..) {

src/vmm/src/vstate/vcpu.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ impl Vcpu {
173173

174174
/// Registers a signal handler which makes use of TLS and kvm immediate exit to
175175
/// kick the vcpu running on the current thread, if there is one.
176-
pub fn register_kick_signal_handler() {
176+
fn register_kick_signal_handler(&mut self) {
177+
self.init_thread_local_data();
178+
177179
extern "C" fn handle_signal(_: c_int, _: *mut siginfo_t, _: *mut c_void) {
178180
// SAFETY: This is safe because it's temporarily aliasing the `Vcpu` object, but we are
179181
// only reading `vcpu.fd` which does not change for the lifetime of the `Vcpu`.
@@ -249,7 +251,7 @@ impl Vcpu {
249251
.name(format!("fc_vcpu {}", self.kvm_vcpu.index))
250252
.spawn(move || {
251253
let filter = &*seccomp_filter;
252-
self.init_thread_local_data();
254+
self.register_kick_signal_handler();
253255
// Synchronization to make sure thread local data is initialized.
254256
barrier.wait();
255257
self.run(filter);
@@ -953,7 +955,6 @@ pub(crate) mod tests {
953955
}
954956

955957
fn vcpu_configured_for_boot() -> (Vm, VcpuHandle, EventFd) {
956-
Vcpu::register_kick_signal_handler();
957958
// Need enough mem to boot linux.
958959
let mem_size = mib_to_bytes(64);
959960
let (kvm, vm, mut vcpu) = setup_vcpu(mem_size);
@@ -1059,7 +1060,6 @@ pub(crate) mod tests {
10591060

10601061
#[test]
10611062
fn test_vcpu_kick() {
1062-
Vcpu::register_kick_signal_handler();
10631063
let (_, vm, mut vcpu) = setup_vcpu(0x1000);
10641064

10651065
let mut kvm_run =
@@ -1073,7 +1073,7 @@ pub(crate) mod tests {
10731073
let handle = std::thread::Builder::new()
10741074
.name("test_vcpu_kick".to_string())
10751075
.spawn(move || {
1076-
vcpu.init_thread_local_data();
1076+
vcpu.register_kick_signal_handler();
10771077
// Notify TLS was populated.
10781078
vcpu_barrier.wait();
10791079
// Loop for max 1 second to check if the signal handler has run.

0 commit comments

Comments
 (0)