Skip to content

Commit 7e2239a

Browse files
jakecorrentislp
authored andcommitted
vmm: fix worker thread panic
If the worker thread panics when trying to convert memory to or from private, it leaves the VMM process waiting indefinitely for the sender to send some sort of message over the channel. Rather than panicking, we should print an error and send a message back over the channel to stop the VM. Signed-off-by: Jake Correnti <[email protected]>
1 parent 0be2d43 commit 7e2239a

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/vmm/src/worker.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use libc::{fallocate, madvise, FALLOC_FL_KEEP_SIZE, FALLOC_FL_PUNCH_HOLE, MADV_D
1515
#[cfg(feature = "tee")]
1616
use std::ffi::c_void;
1717
#[cfg(feature = "tee")]
18-
use vm_memory::{guest_memory::GuestMemory, GuestAddress, GuestMemoryRegion, MemoryRegionAddress};
18+
use vm_memory::{
19+
guest_memory::GuestMemory, Address, GuestAddress, GuestMemoryRegion, MemoryRegionAddress,
20+
};
1921

2022
pub fn start_worker_thread(
2123
vmm: Arc<Mutex<super::Vmm>>,
@@ -67,15 +69,15 @@ impl super::Vmm {
6769

6870
#[cfg(feature = "tee")]
6971
fn convert_memory(&self, sender: Sender<bool>, properties: MemoryProperties) {
70-
let (guest_memfd, region_start) = self
71-
.kvm_vm()
72-
.guest_memfd_get(properties.gpa)
73-
.unwrap_or_else(|| {
74-
panic!(
75-
"unable to find KVM guest_memfd for memory region corresponding to GPA 0x{:x}",
76-
properties.gpa
77-
)
78-
});
72+
let Some((guest_memfd, region_start)) = self.kvm_vm().guest_memfd_get(properties.gpa)
73+
else {
74+
error!(
75+
"unable to find KVM guest_memfd for memory region corresponding to GPA 0x{:x}",
76+
properties.gpa
77+
);
78+
sender.send(false).unwrap();
79+
return;
80+
};
7981

8082
let attributes: u64 = if properties.private {
8183
KVM_MEMORY_ATTRIBUTE_PRIVATE as u64
@@ -90,10 +92,11 @@ impl super::Vmm {
9092
flags: 0,
9193
};
9294

93-
self.kvm_vm()
94-
.fd()
95-
.set_memory_attributes(attr)
96-
.unwrap_or_else(|_| panic!("unable to set memory attributes for memory region corresponding to guest address 0x{:x}", properties.gpa));
95+
if self.kvm_vm().fd().set_memory_attributes(attr).is_err() {
96+
error!("unable to set memory attributes for memory region corresponding to guest address 0x{:x}", properties.gpa);
97+
sender.send(false).unwrap();
98+
return;
99+
}
97100

98101
let region = self
99102
.guest_memory()
@@ -112,10 +115,14 @@ impl super::Vmm {
112115
if properties.private {
113116
let region_addr = MemoryRegionAddress(offset);
114117

115-
let host_startaddr = region
116-
.unwrap()
117-
.get_host_address(region_addr)
118-
.expect("host address corresponding to memory region address 0x{:x} not found");
118+
let Ok(host_startaddr) = region.unwrap().get_host_address(region_addr) else {
119+
error!(
120+
"host address corresponding to memory region address 0x{:x} not found",
121+
region_addr.raw_value()
122+
);
123+
sender.send(false).unwrap();
124+
return;
125+
};
119126

120127
let ret = unsafe {
121128
madvise(

0 commit comments

Comments
 (0)