Skip to content

Commit b5f4bc1

Browse files
committed
refactor: assert on vcpu TLS init
Vcpu TLS must only be initialized once. Enforce this with an assertion. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 22d85da commit b5f4bc1

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/vmm/src/vstate/vcpu.rs

Lines changed: 10 additions & 17 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,12 +1050,11 @@ 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]
@@ -1080,7 +1073,7 @@ pub(crate) mod tests {
10801073
let handle = std::thread::Builder::new()
10811074
.name("test_vcpu_kick".to_string())
10821075
.spawn(move || {
1083-
vcpu.init_thread_local_data().unwrap();
1076+
vcpu.init_thread_local_data();
10841077
// Notify TLS was populated.
10851078
vcpu_barrier.wait();
10861079
// Loop for max 1 second to check if the signal handler has run.

0 commit comments

Comments
 (0)