Skip to content

Commit

Permalink
Merge commit '3fb06e940d8ad0508c31984cf0f8cb947282430b' into main-liv…
Browse files Browse the repository at this point in the history
…e-migration-pvm
  • Loading branch information
pojntfx committed Feb 4, 2025
2 parents 33ee81c + 3fb06e9 commit 725c1cc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/firecracker/examples/uffd/fault_all_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn main() {
let (stream, _) = listener.accept().expect("Cannot listen on UDS socket");

let mut runtime = Runtime::new(stream, file);
runtime.install_panic_hook();
runtime.run(|uffd_handler: &mut UffdHandler| {
// Read an event from the userfaultfd.
let event = uffd_handler
Expand Down
37 changes: 37 additions & 0 deletions src/firecracker/examples/uffd/uffd_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,43 @@ impl Runtime {
}
}

fn peer_process_credentials(&self) -> libc::ucred {
let mut creds: libc::ucred = libc::ucred {
pid: 0,
gid: 0,
uid: 0,
};
let mut creds_size = size_of::<libc::ucred>() as u32;
let ret = unsafe {
libc::getsockopt(
self.stream.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_PEERCRED,
&mut creds as *mut _ as *mut _,
&mut creds_size as *mut libc::socklen_t,
)
};
if ret != 0 {
panic!("Failed to get peer process credentials");
}
creds
}

pub fn install_panic_hook(&self) {
let peer_creds = self.peer_process_credentials();

let default_panic_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
let r = unsafe { libc::kill(peer_creds.pid, libc::SIGKILL) };

if r != 0 {
eprintln!("Failed to kill Firecracker process from panic hook");
}

default_panic_hook(panic_info);
}));
}

/// Polls the `UnixStream` and UFFD fds in a loop.
/// When stream is polled, new uffd is retrieved.
/// When uffd is polled, page fault is handled by
Expand Down
1 change: 1 addition & 0 deletions src/firecracker/examples/uffd/valid_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn main() {
let (stream, _) = listener.accept().expect("Cannot listen on UDS socket");

let mut runtime = Runtime::new(stream, file);
runtime.install_panic_hook();
runtime.run(|uffd_handler: &mut UffdHandler| {
// Read an event from the userfaultfd.
let event = uffd_handler
Expand Down
6 changes: 3 additions & 3 deletions src/vmm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ pub fn build_microvm_from_snapshot(
let (mut vmm, mut vcpus) = create_vmm_and_vcpus(
instance_info,
event_manager,
guest_memory.clone(),
guest_memory,
uffd,
vm_resources.machine_config.track_dirty_pages,
vm_resources.machine_config.vcpu_count,
Expand Down Expand Up @@ -517,7 +517,7 @@ pub fn build_microvm_from_snapshot(

// Restore devices states.
let mmio_ctor_args = MMIODevManagerConstructorArgs {
mem: &guest_memory,
mem: &vmm.guest_memory,
vm: vmm.vm.fd(),
event_manager,
resource_allocator: &mut vmm.resource_allocator,
Expand All @@ -532,7 +532,7 @@ pub fn build_microvm_from_snapshot(

{
let acpi_ctor_args = ACPIDeviceManagerConstructorArgs {
mem: &guest_memory,
mem: &vmm.guest_memory,
resource_allocator: &mut vmm.resource_allocator,
vm: vmm.vm.fd(),
};
Expand Down
3 changes: 3 additions & 0 deletions tests/framework/microvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ def kill(self):
if self.screen_pid:
os.kill(self.screen_pid, signal.SIGKILL)
except:
LOG.error(
"Failed to kill Firecracker Process. Did it already die (or did the UFFD handler process die and take it down)?"
)
LOG.error(self.log_data)
raise

Expand Down

0 comments on commit 725c1cc

Please sign in to comment.