Repository navigation
Description
KVM_GET_LAPIC saves a one-shot LAPIC timer that expired and whose interrupt the guest already took with the same bytes as one that expired while KVM had not yet injected its interrupt: TMICT nonzero, current count 0, the vector clear in IRR and ISR. Since Linux 6.3, KVM_SET_LAPIC fires such a timer at once, which the second case needs, so in the first case the restored guest takes a timer interrupt it never programmed. This is a limit of the uAPI in every current kernel, not a one-line bug: an exact restore needs an opt-in change. It is the KVM half of #1366.
Affected host kernels
| Host kernel | KVM_SET_LAPIC of a one-shot timer with TMICT nonzero and current count 0 |
|---|---|
| 6.3 and later (8e6ed96cdd50), 7.3 included | fires it at once: an extra interrupt if it was already delivered |
| 5.8 to 6.2 | restarts it for a full TMICT period: an extra, late interrupt |
TSC-deadline mode is exact, since MSR_IA32_TSC_DEADLINE reads 0 once the interrupt has been injected. Periodic mode coalesces expiries by design.
Reproduction
Arm a one-shot timer in a guest, let it take and EOI the interrupt, stop the vCPU, and save and restore its LAPIC with KVM_GET_LAPIC and KVM_SET_LAPIC. Expected: no further timer interrupt until the guest reprograms the timer. Actual: one more. #1366 shows this through OpenVMM's restore on KVM.
Cause
kvm_apic_set_state cancels the timer and restarts it from the saved current count (__start_apic_timer(apic, APIC_TMCCT)), and a one-shot timer whose count is 0 gets a deadline of now:
// arch/x86/kvm/lapic.c (Linux 7.3, set_target_expiration)
if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
if (unlikely(count_reg != APIC_TMICT)) {
deadline = tmict_to_ns(apic,
kvm_lapic_get_reg(apic, count_reg));
if (unlikely(deadline <= 0)) {
if (apic_lvtt_period(apic))
deadline = apic->lapic_timer.period;
else
deadline = 0;
}KVM's only marker of a delivered timer interrupt is lapic_timer.target_expiration, which it zeroes on injection and does not export. KVM_GET_LAPIC also does not move an expiry that KVM recorded but has not injected into the exported IRR: one in lapic_timer.pending, or a VMX preemption timer that expired while the vCPU was stopped. So KVM_SET_LAPIC cannot tell the two states apart, and firing at once is the choice that loses no interrupt. A KVM_SET_LAPIC that stopped firing such a timer would lose the interrupt of every snapshot taken in the second state by a kernel that does not report it in the IRR, which is every kernel today; so an exact restore has to be opt-in.
Fix
KVM has no fix yet. An RFC sketch is attached below; it has not been sent upstream. It adds a quirk, KVM_X86_QUIRK_LAPIC_ONESHOT_REFIRE (bit 12 of KVM_CAP_DISABLE_QUIRKS2), enabled by default, which keeps today's behaviour. A VMM that disables it gets an exact restore: KVM_GET_LAPIC sets the timer vector in the exported IRR for an expired one-shot timer whose interrupt is not yet injected, and KVM_SET_LAPIC leaves a one-shot timer with count 0 expired instead of firing it. OpenVMM would disable the quirk where KVM offers it. The sketch changes 4 files (+70/−2) and applies to kvm-x86 next and v7.3; it is compile-tested only.
Activity
tangruize commentedon Nov 8, 2026
RFC sketch for KVM
An RFC for discussion, not a patch to apply as is: it adds
KVM_X86_QUIRK_LAPIC_ONESHOT_REFIRE. Against kvm-x86 next dea4f6380f4ec96e8f3b0aaf2343764bd0d25ef4; it also applies to v7.3. Compile-tested only, and not yet sent upstream.linux.patchvirt_kvm: do not fire an expired LAPIC timer whose interrupt is pending
virt_kvm: do not fire an expired LAPIC timer whose interrupt is pending
openvmm: pin captures that keep a taken LAPIC timer from firing again
doc: specify how capture tells a taken LAPIC timer apart on KVM
openvmm: pin LAPIC timer restores without drift or a downtime refire
doc: specify how a restore runs a one-shot LAPIC timer
ppenna commentedon Nov 12, 2026
Status after #1514, merged into
devasf7720fb6e8cand pinning microsoft/openvmm#5055 (c72fc56a3). Together with #1505, which closed #1366, it does everything that OpenVMM can do here without a KVM change:KVM_RUNthat a signal ends before the guest is entered.KVM_SET_LAPICfires neither again.This issue stays open for the two limits that only KVM's opt-in quirk, as sketched above, can lift:
kvm_intelpreemption_timerparameter is notNor cannot be read, such as bare-metal Intel hosts. AMD hosts, and the nested KVM of the CI runners and the test VM, are not affected.Section 3.2.5 of the design documents both.
virt_kvm: do not fire an expired LAPIC timer whose interrupt is pending
virt_kvm: keep a restore from firing a taken one-shot LAPIC timer again
virt_kvm: do not fire an expired LAPIC timer whose interrupt is pending
virt_kvm: keep a restore from firing a taken one-shot LAPIC timer again
ppenna commentedon Nov 14, 2026
Status check on
dev(6e7a1320047, OpenVMMefe6bc26734). Nothing in OpenVMM or NVX can lift the two limits, so this issue stays open for KVM's quirk.nextstill endKVM_X86_QUIRK_*at bit 11 (KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT), and I found no posting of the RFC (lore.kernel.org was not reachable from here). On nvx-kvm-test (Linux 6.18.57, nested KVM on a Xeon Platinum 8573C),KVM_CHECK_EXTENSION(KVM_CAP_DISABLE_QUIRKS2)returns0x7ff, bits 0 to 10. OpenVMM has no bit to disable, and must not code against an unallocated one.KVM_GET_LAPICandKVM_SET_LAPICalone give the guest one more timer interrupt after a restore of a one-shot timer that it already took, halted or spinning, as this issue describes. With the stop sequence of openvmm: keep a restore from firing a taken one-shot LAPIC timer again #1514, the tests ofvirt_kvmandkvmpass there,/dev/kvmones included:cargo nextest run --profile agent --run-ignored all -p virt_kvm -p kvm, 72 passed.lapic_timer.target_expirationorpending, which no uAPI exports, and a longer wait would only stall every capture whose guest left an expired timer, for an expiry that this host never recorded late.kvm_intelpreemption_timerisNhere), so any change for it would go unvalidated, and a wrong guess loses an interrupt where today's choice only adds a spurious one, which Linux ignores.When a kernel offers the quirk, OpenVMM can enable it at VM creation, which covers both the save and the restore, and then drop the stop-time delivery and the 20 ms wait on that kernel. The restored guest would also read its timer's exact initial count.