Re: [PATCH] KVM: VMX: Clear GUEST_ACTIVITY_STATE when userspace makes a vCPU RUNNABLE

From: keqiang duan

Date: Thu Aug 20 2026 - 07:41:24 EST


On Wed, Aug 19, 2026, Sean Christopherson wrote:
> Thanks Chao, I knew this sounded familiar!
> As suggested in the above link, the call into vendor code should be gated on
> kvm_hlt_in_guest(). Yes, vmx_clear_hlt() already does that, but it's a cheap
> check and makes the common x86 code easier to understand.

Done in v2.

> The other difference between my suggestion and this patch is clearing HLT if and
> only if the vCPU is being made RUNNABLE, versus clearing HLT if the vCPU is being
> put into any state other than HALTED. In practice it probably doesn't matter?
>
> AP_RESET_HOLD is unsupported for VMX, SIPI_RECEIVED is deprecated and is now just
> a variant off INIT_RECEIVED, and getting out of INIT_RECIEVED and UNINITIALIZED
> requires stuffing RUNNABLE or going through vmx_vcpu_reset(), which explicitly
> sets the activity state to GUEST_ACTIVITY_ACTIVE.
>
> But I think my vote would still be to do:
>
> mp_state->mp_state != KVM_MP_STATE_HALTED
>
> because I can't think of any reason not to?

Agreed, and I can't think of one either. HALTED is the only target state for
which retaining the hardware halted state is meaningful, and as you note, every
other transition either can't be reached on VMX or goes through
vmx_vcpu_reset(), which stuffs GUEST_ACTIVITY_ACTIVE anyways.

Keying off "not HALTED" also states the intent more directly, i.e. that KVM
only ever drops the hardware halted state and never installs it.

Switched to "mp_state->mp_state != KVM_MP_STATE_HALTED" in v2.

> Side topic, I think we should officially take an erratum in
> Documentation/virt/kvm/x86/errata.rst to state that KVM doesn't support save/restore
> of HLT state when HLT-exiting is disabled. AFAIK, there's simply no sane solution
> for SVM, and that would justify why the kvm_x86_ops would be clear_hlt() as opposed
> to set_mp_state(), i.e. why KVM only handles taking vCPUs out of HLT, not putting
> them into HLT.

Agreed, documenting the limitation makes sense, and it does explain why the hook
only takes vCPUs out of HLT.

Thanks for the quick review!