@@ -44,31 +44,37 @@ use crate::HyperlightError;
4444use crate :: { log_then_return, new_error, Result } ;
4545
4646/// 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 ( ) ;
4848
4949/// Get the global KVM handle, initializing it if needed
5050#[ 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+ }
6470 }
6571 }
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+ }
7278}
7379
7480/// Return `true` if the KVM API is available, version 12, and has UserMemory capability, or `false` otherwise
@@ -288,7 +294,7 @@ mod debug {
288294
289295/// A Hypervisor driver for KVM on Linux
290296pub ( super ) struct KVMDriver {
291- _kvm : Kvm ,
297+ _kvm : & ' static Kvm ,
292298 _vm_fd : VmFd ,
293299 vcpu_fd : VcpuFd ,
294300 entrypoint : u64 ,
@@ -314,7 +320,7 @@ impl KVMDriver {
314320 #[ cfg( gdb) ] gdb_conn : Option < DebugCommChannel < DebugResponse , DebugMsg > > ,
315321 ) -> Result < Self > {
316322 let kvm = match get_kvm_handle ( ) {
317- Some ( kvm) => kvm. clone ( ) ,
323+ Some ( kvm) => kvm,
318324 None => return Err ( new_error ! ( "KVM is not available on this system" ) ) ,
319325 } ;
320326
0 commit comments