@@ -67,7 +67,7 @@ use crate::vmm_config::instance_info::InstanceInfo;
6767use crate :: vmm_config:: machine_config:: MachineConfigError ;
6868use crate :: vmm_config:: snapshot:: { LoadSnapshotParams , MemBackendType } ;
6969use crate :: vstate:: kvm:: Kvm ;
70- use crate :: vstate:: memory:: { MaybeBounce , create_memfd} ;
70+ use crate :: vstate:: memory:: { MaybeBounce , MemoryError , create_memfd} ;
7171use crate :: vstate:: vcpu:: { Vcpu , VcpuError } ;
7272use crate :: vstate:: vm:: { GUEST_MEMFD_FLAG_NO_DIRECT_MAP , GUEST_MEMFD_FLAG_SUPPORT_SHARED , Vm } ;
7373use crate :: { EventManager , Vmm , VmmError , device_manager} ;
@@ -482,10 +482,10 @@ pub enum BuildMicrovmFromSnapshotError {
482482 /// Failed to load guest memory: {0}
483483 GuestMemory ( #[ from] BuildMicrovmFromSnapshotErrorGuestMemoryError ) ,
484484 /// Userfault bitmap memfd error: {0}
485- UserfaultBitmapMemfd ( #[ from] crate :: vstate :: memory :: MemoryError ) ,
485+ UserfaultBitmapMemfd ( #[ from] MemoryError ) ,
486486}
487487
488- fn memfd_to_slice ( memfd : & Option < File > ) -> Option < & mut [ u8 ] > {
488+ fn memfd_to_slice ( memfd : & Option < File > ) -> Result < Option < & mut [ u8 ] > , MemoryError > {
489489 if let Some ( bitmap_file) = memfd {
490490 let len = u64_to_usize (
491491 bitmap_file
@@ -507,16 +507,15 @@ fn memfd_to_slice(memfd: &Option<File>) -> Option<&mut [u8]> {
507507 } ;
508508
509509 if bitmap_addr == libc:: MAP_FAILED {
510- panic ! (
511- "Failed to mmap userfault bitmap file: {}" ,
512- std:: io:: Error :: last_os_error( )
513- ) ;
510+ return Err ( MemoryError :: Mmap ( std:: io:: Error :: last_os_error ( ) ) ) ;
514511 }
515512
516513 // SAFETY: `bitmap_addr` is a valid memory address returned by `mmap`.
517- Some ( unsafe { std:: slice:: from_raw_parts_mut ( bitmap_addr. cast ( ) , len) } )
514+ Ok ( Some ( unsafe {
515+ std:: slice:: from_raw_parts_mut ( bitmap_addr. cast ( ) , len)
516+ } ) )
518517 } else {
519- None
518+ Ok ( None )
520519 }
521520}
522521
@@ -616,7 +615,7 @@ pub fn build_microvm_from_snapshot(
616615 }
617616 } ;
618617
619- let mut userfault_bitmap = memfd_to_slice ( & userfault_bitmap_memfd) ;
618+ let mut userfault_bitmap = memfd_to_slice ( & userfault_bitmap_memfd) ? ;
620619 if let Some ( ref mut slice) = userfault_bitmap {
621620 // Set all bits so a fault on any page will cause a VM exit
622621 slice. fill ( 0xffu8 ) ;
0 commit comments