Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Retry KVM_CREATE_VM on EINTR #5046

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vmm/src/vstate/vm/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum ArchVmError {
impl ArchVm {
/// Create a new `Vm` struct.
pub fn new(kvm: &Kvm) -> Result<ArchVm, VmError> {
let fd = kvm.fd.create_vm().map_err(VmError::VmFd)?;
let fd = Self::create_vm(kvm)?;
Ok(ArchVm {
fd,
irqchip_handle: None,
Expand Down
41 changes: 39 additions & 2 deletions src/vmm/src/vstate/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use kvm_ioctls::VmFd;
use vmm_sys_util::eventfd::EventFd;

use crate::logger::info;
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};

#[cfg(target_arch = "x86_64")]
Expand All @@ -30,8 +31,8 @@
pub enum VmError {
/// Cannot set the memory regions: {0}
SetUserMemoryRegion(kvm_ioctls::Error),
/// Cannot open the VM file descriptor: {0}
VmFd(kvm_ioctls::Error),
/// Failed to create VM: {0}
CreateVm(kvm_ioctls::Error),
/// {0}
Arch(#[from] ArchVmError),
/// Error during eventfd operations: {0}
Expand All @@ -42,6 +43,42 @@

/// Contains Vm functions that are usable across CPU architectures
impl Vm {
fn create_vm(kvm: &crate::vstate::kvm::Kvm) -> Result<VmFd, VmError> {
// It is known that KVM_CREATE_VM occasionally fails with EINTR on heavily loaded machines
// with many VMs.
//
// The behavior itself that KVM_CREATE_VM can return EINTR is intentional. This is because
// the KVM_CREATE_VM path includes mm_take_all_locks() that is CPU intensive and all CPU
// intensive syscalls should check for pending signals and return EINTR immediately to allow
// userland to remain interactive.
// https://lists.nongnu.org/archive/html/qemu-devel/2014-01/msg01740.html
//
// However, it is empirically confirmed that, even though there is no pending signal,
// KVM_CREATE_VM returns EINTR.
// https://lore.kernel.org/qemu-devel/[email protected]/
//
// To mitigate it, QEMU does an inifinite retry on EINTR that greatly improves reliabiliy:
// - https://github.com/qemu/qemu/commit/94ccff133820552a859c0fb95e33a539e0b90a75
// - https://github.com/qemu/qemu/commit/bbde13cd14ad4eec18529ce0bf5876058464e124
//
// Similarly, we do retries up to 5 times. Although Firecracker clients are also able to
// retry, they have to start Firecracker from scratch. Doing retries in Firecracker makes
// recovery faster and improves reliability.
const MAX_ATTEMPTS: u32 = 5;
for attempt in 1..=MAX_ATTEMPTS {
match kvm.fd.create_vm() {
Ok(fd) => return Ok(fd),
Err(e) if e.errno() == libc::EINTR && attempt < MAX_ATTEMPTS => {
info!("Attemp #{attempt} of KVM_CREATE_VM returned EINTR");

Check warning on line 72 in src/vmm/src/vstate/vm/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/vstate/vm/mod.rs#L71-L72

Added lines #L71 - L72 were not covered by tests
// Exponential backoff (1us, 2us, 4us, and 8us => 15us in total)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be 16us in the end? 2^4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sleep duration is 2^(attempt - 1). The attempt starts from 1 to 5, but if attempt == 5 it goes to the third hand of the match expression. So 2^(1 - 1) = 2^0 = 1, 2^(2 - 1) = 2^1 = 2, 2^(3 - 1) = 2^2 = 4, and 2^(4 - 1) = 2^3 = 8.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so it is the sum.
Btw, why do we even need the backoff?

std::thread::sleep(std::time::Duration::from_micros(2u64.pow(attempt - 1)));

Check warning on line 74 in src/vmm/src/vstate/vm/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/vstate/vm/mod.rs#L74

Added line #L74 was not covered by tests
}
Err(e) => return Err(VmError::CreateVm(e)),

Check warning on line 76 in src/vmm/src/vstate/vm/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/vstate/vm/mod.rs#L76

Added line #L76 was not covered by tests
}
}
unreachable!();

Check warning on line 79 in src/vmm/src/vstate/vm/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/vstate/vm/mod.rs#L79

Added line #L79 was not covered by tests
}

/// Creates the specified number of [`Vcpu`]s.
///
/// The returned [`EventFd`] is written to whenever any of the vcpus exit.
Expand Down
3 changes: 2 additions & 1 deletion src/vmm/src/vstate/vm/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ pub struct ArchVm {
impl ArchVm {
/// Create a new `Vm` struct.
pub fn new(kvm: &crate::vstate::kvm::Kvm) -> Result<ArchVm, VmError> {
let fd = kvm.fd.create_vm().map_err(VmError::VmFd)?;
let fd = Self::create_vm(kvm)?;

let msrs_to_save = kvm.msrs_to_save().map_err(ArchVmError::GetMsrsToSave)?;

fd.set_tss_address(u64_to_usize(crate::arch::x86_64::layout::KVM_TSS_ADDRESS))
Expand Down