Re: [PATCH v2 5/8] KVM: TDX: Handle TDG.VP.VMCALL

Binbin Wu
2025-02-13T03:23:05Z

On 2/13/2025 2:56 AM, Sean Christopherson wrote:
> On Wed, Feb 12, 2025, Binbin Wu wrote:
>> On 2/12/2025 8:46 AM, Sean Christopherson wrote:
>>> I am completely comfortable saying that KVM doesn't care about STI/SS shadows
>>> outside of the HALTED case, and so unless I'm missing something, I think it makes
>>> sense for tdx_protected_apic_has_interrupt() to not check RVI outside of the HALTED
>>> case, because it's impossible to know if the interrupt is actually unmasked, and
>>> statistically it's far, far more likely that it _is_ masked.
>> OK. Will update tdx_protected_apic_has_interrupt() in "TDX interrupts" part.
>> And use kvm_vcpu_has_events() to replace the open code in this patch.
> Something to keep an eye on: kvm_vcpu_has_events() returns true if pv_unhalted
> is set, and pv_unhalted is only cleared on transitions KVM_MP_STATE_RUNNABLE.
> If the guest initiates a spurious wakeup, pv_unhalted could be left set in
> perpetuity.

Oh, yes.
KVM_HC_KICK_CPU is allowed in TDX guests.

The change below looks good to me.

One minor issue is when guest initiates a spurious wakeup, pv_unhalted is
left set, then later when the guest want to halt the vcpu, in
__kvm_emulate_halt(), since pv_unhalted is still set and the state will not
transit to KVM_MP_STATE_HALTED.
But I guess it's guests' responsibility to not initiate spurious wakeup,
guests need to bear the fact that HLT could fail due to a previous
spurious wakeup?

>
> I _think_ this would work and is generally desirable?
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 8e77e61d4fbd..435ca2782c3c 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -11114,9 +11114,6 @@ static bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
> kvm_apic_init_sipi_allowed(vcpu))
> return true;
>
> - if (vcpu->arch.pv.pv_unhalted)
> - return true;
> -
> if (kvm_is_exception_pending(vcpu))
> return true;
>
> @@ -11157,7 +11154,8 @@ static bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
>
> int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
> {
> - return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
> + return kvm_vcpu_running(vcpu) || vcpu->arch.pv.pv_unhalted ||
> + kvm_vcpu_has_events(vcpu);
> }
>
> /* Called within kvm->srcu read side. */
> @@ -11293,7 +11291,7 @@ static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason)
> */
> ++vcpu->stat.halt_exits;
> if (lapic_in_kernel(vcpu)) {
> - if (kvm_vcpu_has_events(vcpu))
> + if (kvm_vcpu_has_events(vcpu) || vcpu->arch.pv.pv_unhalted)
> vcpu->arch.pv.pv_unhalted = false;
> else
> vcpu->arch.mp_state = state;
>
>