Skip to content

Commit d25a0ba

Browse files
committed
virt_kvm: refuse to save a VP while KVM holds a queued PIC interrupt
While the VP runs a nested guest, a PIC interrupt is queued with KVM_INTERRUPT. KVM reports a queued interrupt in neither the VP state nor the nested state, and it can keep holding it after a nested exit, until the L1 hypervisor enables interrupts. A save taken in that window lost the interrupt, which the PIC had already acknowledged. Track the queued interrupt in the VP runner until KVM reports the VP ready for another interrupt, which it does only once nothing is queued, and fail the save while it may still be queued.
1 parent be28393 commit d25a0ba

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎vm/kvm/src/lib.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,7 @@ impl<'a> Processor<'a> {
19061906
VpRunner {
19071907
partition: self.0,
19081908
idx: self.1,
1909+
extint_queued: false,
19091910
_not_send_sync: PhantomData,
19101911
}
19111912
}
@@ -1914,6 +1915,8 @@ impl<'a> Processor<'a> {
19141915
pub struct VpRunner<'a> {
19151916
partition: &'a Partition,
19161917
idx: u32,
1918+
/// Whether KVM may still hold an extint queued with `KVM_INTERRUPT`.
1919+
extint_queued: bool,
19171920
// This type stores the current thread in `partition` and removes it in
19181921
// `drop`, so don't allow sending or sharing this.
19191922
_not_send_sync: PhantomData<*const u8>,
@@ -1983,6 +1986,11 @@ impl<'a> VpRunner<'a> {
19831986
// SAFETY: Calling IOCTL as documented, with no special requirements.
19841987
let result = unsafe { ioctl::kvm_run(vp.vcpu.as_raw_fd(), 0) };
19851988
CURRENT_KVM_RUN.with(|r| r.store(NO_KVM_RUN, Ordering::Relaxed));
1989+
// KVM reports the VP ready for an interrupt only once no extint is
1990+
// queued, and updates the field even when it returns early.
1991+
if self.run_data().ready_for_interrupt_injection != 0 {
1992+
self.extint_queued = false;
1993+
}
19861994
match result {
19871995
Ok(_) => Ok(true),
19881996
Err(err) => match err {
@@ -2221,11 +2229,19 @@ impl<'a> VpRunner<'a> {
22212229
/// [`Self::check_or_request_interrupt_window`] has returned `true`.
22222230
pub fn queue_extint_interrupt(&mut self, vector: u8) -> Result<()> {
22232231
self.partition.vp(self.idx).interrupt(vector.into())?;
2232+
self.extint_queued = true;
22242233
// Remember that there is a pending extint interrupt. KVM will update
22252234
// this field again after the VP runs.
22262235
self.run_data().ready_for_interrupt_injection = 0;
22272236
Ok(())
22282237
}
2238+
2239+
/// Returns whether KVM may still hold an extint queued with
2240+
/// [`Self::queue_extint_interrupt`], which is then missing from any saved
2241+
/// VP state.
2242+
pub fn extint_may_be_queued(&self) -> bool {
2243+
self.extint_queued
2244+
}
22292245
}
22302246

22312247
#[derive(Debug)]

‎vmm_core/virt_kvm/src/arch/x86_64/extint.rs‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::KvmRunVpError;
1111
/// KVM cannot turn an interrupt in the slot into an exit from a nested guest,
1212
/// so while the VP runs one, the interrupt is queued with
1313
/// [`kvm::VpRunner::queue_extint_interrupt`] and KVM decides. KVM does not
14-
/// report a queued interrupt, so VP state saved before the guest takes it does
15-
/// not include it.
14+
/// report a queued interrupt, so the VP state cannot be saved until KVM has
15+
/// delivered it (see [`kvm::VpRunner::extint_may_be_queued`]).
1616
///
1717
/// The caller must meet the requirements of both [`inject`] and
1818
/// [`kvm::VpRunner::queue_extint_interrupt`]. Returns whether an interrupt was
@@ -285,6 +285,7 @@ mod tests {
285285
assert_eq!(next_out_port(&mut runner, timed_out), HANDLER_PORT.into());
286286
assert_eq!(vp.get_vcpu_events().unwrap().interrupt.injected, 0);
287287
assert_eq!(next_out_port(&mut runner, timed_out), RESUMED_PORT.into());
288+
assert!(!runner.extint_may_be_queued());
288289
drop(runner);
289290
drop(done);
290291
});
@@ -377,6 +378,23 @@ mod tests {
377378
});
378379
}
379380

381+
#[test]
382+
#[ignore = "requires access to /dev/kvm"]
383+
fn queued_extint_is_tracked_across_a_stop() {
384+
check_extint(KVM_MP_STATE_RUNNABLE, |runner, _vp| {
385+
refresh_interrupt_window(runner);
386+
assert!(runner.check_or_request_interrupt_window());
387+
assert!(!runner.extint_may_be_queued());
388+
runner.queue_extint_interrupt(VECTOR).unwrap();
389+
assert!(runner.extint_may_be_queued());
390+
391+
// A stop completes the exit without running the VP, so KVM still
392+
// holds the interrupt.
393+
refresh_interrupt_window(runner);
394+
assert!(runner.extint_may_be_queued());
395+
});
396+
}
397+
380398
/// Checks that KVM reports the interrupt window for the current state of a
381399
/// VP in `mp_state` once any pending exit is completed.
382400
fn check_stale_interrupt_window(mp_state: u32) {

‎vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ impl AccessVpState for KvmVpStateAccess<'_, '_> {
693693
}
694694

695695
fn nested_state(&mut self) -> Result<vp::NestedState, Self::Error> {
696+
// KVM reports a queued PIC interrupt in neither the VP state nor the
697+
// nested state, so saving now would lose it.
698+
if self.vp.runner.extint_may_be_queued() {
699+
return Err(KvmError::ExtintQueued);
700+
}
696701
// Empty only on a host without KVM_CAP_NESTED_STATE.
697702
let data = self.kvm().get_nested_state()?;
698703
Ok(vp::NestedState { data })

‎vmm_core/virt_kvm/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub enum KvmError {
8888
#[error("nested virtualization was requested but the host does not support it")]
8989
NestedVirtUnsupported,
9090
#[cfg(guest_arch = "x86_64")]
91+
#[error("cannot save the vp while a PIC interrupt is queued in KVM")]
92+
ExtintQueued,
93+
#[cfg(guest_arch = "x86_64")]
9194
#[error("unsupported CPU vendor")]
9295
UnsupportedCpuVendor,
9396
#[cfg(guest_arch = "x86_64")]

0 commit comments

Comments
 (0)