-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
zulinx86
wants to merge
2
commits into
firecracker-microvm:main
Choose a base branch
from
zulinx86:EINTR_on_KVM_CREATE_VM
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+42
−4
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")] | ||
|
@@ -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"); | ||
// Exponential backoff (1us, 2us, 4us, and 8us => 15us in total) | ||
std::thread::sleep(std::time::Duration::from_micros(2u64.pow(attempt - 1))); | ||
} | ||
Err(e) => return Err(VmError::CreateVm(e)), | ||
} | ||
} | ||
unreachable!(); | ||
} | ||
|
||
/// Creates the specified number of [`Vcpu`]s. | ||
/// | ||
/// The returned [`EventFd`] is written to whenever any of the vcpus exit. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
. Theattempt
starts from 1 to 5, but ifattempt == 5
it goes to the third hand of thematch
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.There was a problem hiding this comment.
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?