KVM: x86: INIT leaves a stale RTC pending EOI
Hi Sean, Paolo,
I hit a corner case in the in-kernel IOAPIC RTC EOI tracking. It needs a vCPU to get INIT before it EOIs an RTC interrupt, APICv to be inactive, and a VMM that injects GSI 8 with KVM_IRQ_LINE_STATUS, so it may be rare in practice.
When a vCPU accepts the RTC interrupt, ioapic->rtc_status records it in map and pending_eoi. If that vCPU then processes INIT, kvm_lapic_reset() clears IRR and ISR but does not touch rtc_status. The guest can no longer EOI that vector, so pending_eoi stays above 0 and rtc_irq_check_coalesced() treats every later GSI 8 injection as coalesced. Only KVM_SET_LAPIC or KVM_SET_IRQCHIP clear it, through kvm_rtc_eoi_tracking_restore_*(). The lazy repair in ioapic_lazy_update_eoi() only runs with APICv active, so nothing repairs the stale entry when APICv is inactive.
On v7.3 with kvm-amd.avic=N, running nested (the KVM under test is inside a QEMU guest), with IOAPIC pin 8 edge-triggered, vector 0x50, physical dest 0, and GSI 8 routed only to the IOAPIC, 6 out of 6 runs gave:
KVM_IRQ_LINE_STATUS(GSI 8) -> status 1
LAPIC: SVR=0x1ff, IRR[0x50]=1, ISR[0x50]=0
INIT through KVM_SIGNAL_MSI, processed by KVM_RUN
LAPIC: SVR=0xff, IRR[0x50]=0, ISR[0x50]=0
guest re-enables the LAPIC: a stub in 32-bit protected mode
(set with KVM_SET_SREGS/KVM_SET_REGS) writes SVR=0x1ff through the
APIC MMIO page and exits with "out"
LAPIC: SVR=0x1ff, IRR[0x50]=0, ISR[0x50]=0 deassert, KVM_IRQ_LINE_STATUS(GSI 8) -> status 0 (coalesced)
control: KVM_SET_LAPIC with the same LAPIC state, so kvm_rtc_eoi_tracking_restore_one() drops the stale entry
deassert, KVM_IRQ_LINE_STATUS(GSI 8) -> status 1
The entry is edge-triggered, so remote_irr is not involved, and GSI 8 goes only to the IOAPIC, so the status does not come from the PIC. In the 0 and the 1 case the LAPIC is enabled and its IRR is empty; the only difference is the stale rtc_status entry.
The code is unchanged in next-20261113. Xiang Zheng's "KVM: x86: Fix stale RTC pending-EOI tracking under AVIC" only changes ioapic_lazy_update_eoi(), so it does not cover the APICv-inactive case.
One option is to call kvm_rtc_eoi_tracking_restore_one() from the INIT path of kvm_lapic_reset(), as kvm_apic_set_state() already does. I have not tested that.
Thanks,
Cheng Yongkang