@@ -44,31 +44,37 @@ use crate::HyperlightError;
44
44
use crate :: { log_then_return, new_error, Result } ;
45
45
46
46
/// Static global KVM handle to avoid reopening /dev/kvm for every sandbox
47
- static KVM_HANDLE : OnceLock < Option < Kvm > > = OnceLock :: new ( ) ;
47
+ static KVM_HANDLE : OnceLock < Kvm > = OnceLock :: new ( ) ;
48
48
49
49
/// Get the global KVM handle, initializing it if needed
50
50
#[ instrument( skip_all, parent = Span :: current( ) , level = "Trace" ) ]
51
- pub ( crate ) fn get_kvm_handle ( ) -> & ' static Option < Kvm > {
52
- KVM_HANDLE . get_or_init ( || match Kvm :: new ( ) {
53
- Ok ( kvm) => {
54
- let api_version = kvm. get_api_version ( ) ;
55
- match api_version {
56
- version if version == 12 && kvm. check_extension ( UserMemory ) => Some ( kvm) ,
57
- 12 => {
58
- log:: info!( "KVM does not have KVM_CAP_USER_MEMORY capability" ) ;
59
- None
60
- }
61
- version => {
62
- log:: info!( "KVM GET_API_VERSION returned {}, expected 12" , version) ;
63
- None
51
+ pub ( crate ) fn get_kvm_handle ( ) -> Option < & ' static Kvm > {
52
+ match KVM_HANDLE . get ( ) {
53
+ Some ( kvm) => Some ( kvm) ,
54
+ None => match Kvm :: new ( ) {
55
+ Ok ( kvm) => {
56
+ let api_version = kvm. get_api_version ( ) ;
57
+ match api_version {
58
+ version if version == 12 && kvm. check_extension ( UserMemory ) => {
59
+ let _ = KVM_HANDLE . set ( kvm) ;
60
+ KVM_HANDLE . get ( )
61
+ }
62
+ 12 => {
63
+ log:: info!( "KVM does not have KVM_CAP_USER_MEMORY capability" ) ;
64
+ None
65
+ }
66
+ version => {
67
+ log:: info!( "KVM GET_API_VERSION returned {}, expected 12" , version) ;
68
+ None
69
+ }
64
70
}
65
71
}
66
- }
67
- Err ( e ) => {
68
- log :: info! ( "KVM is not available on this system: {}" , e ) ;
69
- None
70
- }
71
- } )
72
+ Err ( e ) => {
73
+ log :: info! ( "KVM is not available on this system: {}" , e ) ;
74
+ None
75
+ }
76
+ } ,
77
+ }
72
78
}
73
79
74
80
/// Return `true` if the KVM API is available, version 12, and has UserMemory capability, or `false` otherwise
@@ -288,7 +294,7 @@ mod debug {
288
294
289
295
/// A Hypervisor driver for KVM on Linux
290
296
pub ( super ) struct KVMDriver {
291
- _kvm : Kvm ,
297
+ _kvm : & ' static Kvm ,
292
298
_vm_fd : VmFd ,
293
299
vcpu_fd : VcpuFd ,
294
300
entrypoint : u64 ,
@@ -314,7 +320,7 @@ impl KVMDriver {
314
320
#[ cfg( gdb) ] gdb_conn : Option < DebugCommChannel < DebugResponse , DebugMsg > > ,
315
321
) -> Result < Self > {
316
322
let kvm = match get_kvm_handle ( ) {
317
- Some ( kvm) => kvm. clone ( ) ,
323
+ Some ( kvm) => kvm,
318
324
None => return Err ( new_error ! ( "KVM is not available on this system" ) ) ,
319
325
} ;
320
326
0 commit comments