On Fri, Mar 04, 2022, isaku.yamahata@xxxxxxxxx wrote: > From: Sean Christopherson <sean.j.christopherson@xxxxxxxxx> > > Add an option to skip the IRR check-in kvm_wait_lapic_expire(). This > will be used by TDX to wait if there is an outstanding notification for > a TD, i.e. a virtual interrupt is being triggered via posted interrupt > processing. KVM TDX doesn't emulate PI processing, i.e. there will > never be a bit set in IRR/ISR, so the default behavior for APICv of > querying the IRR doesn't work as intended. > > Signed-off-by: Sean Christopherson <sean.j.christopherson@xxxxxxxxx> > Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx> > --- > arch/x86/kvm/lapic.c | 4 ++-- > arch/x86/kvm/lapic.h | 2 +- > arch/x86/kvm/svm/svm.c | 2 +- > arch/x86/kvm/vmx/vmx.c | 2 +- > 4 files changed, 5 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 9322e6340a74..d49f029ef0e3 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -1620,12 +1620,12 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) > __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc); > } > > -void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) > +void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu, bool force_wait) > { > if (lapic_in_kernel(vcpu) && > vcpu->arch.apic->lapic_timer.expired_tscdeadline && > vcpu->arch.apic->lapic_timer.timer_advance_ns && > - lapic_timer_int_injected(vcpu)) > + (force_wait || lapic_timer_int_injected(vcpu))) > __kvm_wait_lapic_expire(vcpu); If the guest_apic_protected idea works, rather than require TDX to tell the local APIC that it should wait, the common code can instead assume a timer IRQ is pending if the IRR holds garbage. Again, compile tested only... From: Sean Christopherson <seanjc@xxxxxxxxxx> Date: Fri, 8 Apr 2022 09:24:39 -0700 Subject: [PATCH] KVM: x86: Assume timer IRQ was injected if APIC state is proteced If APIC state is protected, i.e. the vCPU is a TDX guest, assume a timer IRQ was injected when deciding whether or not to busy wait in the "timer advanced" path. The "real" vIRR is not readable/writable, so trying to query for a pending timer IRQ will return garbage. Note, TDX can scour the PIR if it wants to be more precise and skip the "wait" call entirely. Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx> --- arch/x86/kvm/lapic.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 50a483abc0fe..e5555dce8db8 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1531,8 +1531,17 @@ static void apic_update_lvtt(struct kvm_lapic *apic) static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu) { struct kvm_lapic *apic = vcpu->arch.apic; - u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT); + u32 reg; + /* + * Assume a timer IRQ was "injected" if the APIC is protected. KVM's + * copy of the vIRR is bogus, it's the responsibility of the caller to + * precisely check whether or not a timer IRQ is pending. + */ + if (apic->guest_apic_protected) + return true; + + reg = kvm_lapic_get_reg(apic, APIC_LVTT); if (kvm_apic_hw_enabled(apic)) { int vec = reg & APIC_VECTOR_MASK; void *bitmap = apic->regs + APIC_ISR; base-commit: 33f2439cd63c84fcbc8b4cdd4eb731e83deead90 --