@@ -124,26 +124,6 @@ impl Vcpu {
124
124
} )
125
125
}
126
126
127
- /// Deassociates `self` from the current thread.
128
- ///
129
- /// Should be called if the current `self` had called `init_thread_local_data()` and
130
- /// now needs to move to a different thread.
131
- ///
132
- /// Fails if `self` was not previously associated with the current thread.
133
- fn reset_thread_local_data ( & mut self ) -> Result < ( ) , VcpuError > {
134
- // Best-effort to clean up TLS. If the `Vcpu` was moved to another thread
135
- // _before_ running this, then there is nothing we can do.
136
- Self :: TLS_VCPU_PTR . with ( |cell : & VcpuCell | {
137
- if let Some ( vcpu_ptr) = cell. get ( ) {
138
- if std:: ptr:: eq ( vcpu_ptr, self ) {
139
- Self :: TLS_VCPU_PTR . with ( |cell : & VcpuCell | cell. take ( ) ) ;
140
- return Ok ( ( ) ) ;
141
- }
142
- }
143
- Err ( VcpuError :: VcpuTlsNotPresent )
144
- } )
145
- }
146
-
147
127
/// Runs `func` for the `Vcpu` associated with the current thread.
148
128
///
149
129
/// It requires that `init_thread_local_data()` was run on this thread.
@@ -615,7 +595,23 @@ fn handle_kvm_exit(
615
595
616
596
impl Drop for Vcpu {
617
597
fn drop ( & mut self ) {
618
- let _ = self . reset_thread_local_data ( ) ;
598
+ Self :: TLS_VCPU_PTR . with ( |cell| {
599
+ // The reason for not asserting TLS being set here is that
600
+ // it can happen that Vcpu::Drop is called on vcpus which never were
601
+ // put on their threads. This can happen if some error occurs during Vmm
602
+ // setup before `start_threaded` call.
603
+ if let Some ( _vcpu_ptr) = cell. get ( ) {
604
+ // During normal runtime there is a strong assumption that vcpus will be
605
+ // put on their own threads, thus TLS will be initialized with the
606
+ // correct pointer.
607
+ // In test we do not put vcpus on separate threads, so TLS will have a value
608
+ // of the last created vcpu.
609
+ #[ cfg( not( test) ) ]
610
+ assert ! ( std:: ptr:: eq( _vcpu_ptr, self ) ) ;
611
+
612
+ Self :: TLS_VCPU_PTR . take ( ) ;
613
+ }
614
+ } )
619
615
}
620
616
}
621
617
@@ -1039,15 +1035,12 @@ pub(crate) mod tests {
1039
1035
}
1040
1036
1041
1037
// Reset vcpu TLS.
1042
- vcpu . reset_thread_local_data ( ) . unwrap ( ) ;
1038
+ Vcpu :: TLS_VCPU_PTR . with ( |cell| cell . take ( ) ) ;
1043
1039
1044
1040
// Running on the TLS vcpu after TLS reset should fail.
1045
1041
unsafe {
1046
1042
Vcpu :: run_on_thread_local ( |_| ( ) ) . unwrap_err ( ) ;
1047
1043
}
1048
-
1049
- // Second reset should return error.
1050
- vcpu. reset_thread_local_data ( ) . unwrap_err ( ) ;
1051
1044
}
1052
1045
1053
1046
#[ test]
0 commit comments