Skip to content

Commit 1e8164f

Browse files
committed
refactor: assert on vcpu TLS init
Vcpu TLS must only be initialized once, so convert the error to the assert. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 9b2be6d commit 1e8164f

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/vmm/src/vstate/vcpu.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ pub enum VcpuError {
5151
VcpuResponse(KvmVcpuError),
5252
/// Cannot spawn a new vCPU thread: {0}
5353
VcpuSpawn(io::Error),
54-
/// Cannot clean init vcpu TLS
55-
VcpuTlsInit,
5654
/// Vcpu not present in TLS
5755
VcpuTlsNotPresent,
5856
/// Error with gdb request sent
@@ -118,14 +116,11 @@ impl Vcpu {
118116
///
119117
/// It is a prerequisite to successfully run `init_thread_local_data()` before using
120118
/// `run_on_thread_local()` on the current thread.
121-
/// This function will return an error if there already is a `Vcpu` present in the TLS.
122-
fn init_thread_local_data(&mut self) -> Result<(), VcpuError> {
119+
/// This function will panic if there already is a `Vcpu` present in the TLS.
120+
fn init_thread_local_data(&mut self) {
123121
Self::TLS_VCPU_PTR.with(|cell: &VcpuCell| {
124-
if cell.get().is_some() {
125-
return Err(VcpuError::VcpuTlsInit);
126-
}
122+
assert!(cell.get().is_none());
127123
cell.set(Some(self as *mut Vcpu));
128-
Ok(())
129124
})
130125
}
131126

@@ -254,8 +249,7 @@ impl Vcpu {
254249
.name(format!("fc_vcpu {}", self.kvm_vcpu.index))
255250
.spawn(move || {
256251
let filter = &*seccomp_filter;
257-
self.init_thread_local_data()
258-
.expect("Cannot cleanly initialize vcpu TLS.");
252+
self.init_thread_local_data();
259253
// Synchronization to make sure thread local data is initialized.
260254
barrier.wait();
261255
self.run(filter);
@@ -1034,7 +1028,7 @@ pub(crate) mod tests {
10341028
}
10351029

10361030
// Initialize vcpu TLS.
1037-
vcpu.init_thread_local_data().unwrap();
1031+
vcpu.init_thread_local_data();
10381032

10391033
// Validate TLS vcpu is the local vcpu by changing the `id` then validating against
10401034
// the one in TLS.
@@ -1056,17 +1050,15 @@ pub(crate) mod tests {
10561050
}
10571051

10581052
#[test]
1059-
fn test_invalid_tls() {
1053+
#[should_panic]
1054+
fn test_tls_double_init() {
10601055
let (_, _, mut vcpu) = setup_vcpu(0x1000);
1061-
// Initialize vcpu TLS.
1062-
vcpu.init_thread_local_data().unwrap();
1063-
// Trying to initialize non-empty TLS should error.
1064-
vcpu.init_thread_local_data().unwrap_err();
1056+
vcpu.init_thread_local_data();
1057+
vcpu.init_thread_local_data();
10651058
}
10661059

10671060
#[test]
10681061
fn test_vcpu_kick() {
1069-
Vcpu::register_kick_signal_handler();
10701062
let (_, vm, mut vcpu) = setup_vcpu(0x1000);
10711063

10721064
let mut kvm_run =
@@ -1080,7 +1072,7 @@ pub(crate) mod tests {
10801072
let handle = std::thread::Builder::new()
10811073
.name("test_vcpu_kick".to_string())
10821074
.spawn(move || {
1083-
vcpu.init_thread_local_data().unwrap();
1075+
vcpu.init_thread_local_data();
10841076
// Notify TLS was populated.
10851077
vcpu_barrier.wait();
10861078
// Loop for max 1 second to check if the signal handler has run.

0 commit comments

Comments
 (0)