]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86: Always set mp_state to RUNNABLE on wakeup from HLT
authorSean Christopherson <seanjc@google.com>
Mon, 24 Feb 2025 17:41:56 +0000 (09:41 -0800)
committerSean Christopherson <seanjc@google.com>
Fri, 28 Feb 2025 23:43:18 +0000 (15:43 -0800)
When emulating HLT and a wake event is already pending, explicitly mark
the vCPU RUNNABLE (via kvm_set_mp_state()) instead of assuming the vCPU is
already in the appropriate state.  Barring a KVM bug, it should be
impossible for the vCPU to be in a non-RUNNABLE state, but there is no
advantage to relying on that to hold true, and ensuring the vCPU is made
RUNNABLE avoids non-deterministic behavior with respect to pv_unhalted.

E.g. if the vCPU is not already RUNNABLE, then depending on when
pv_unhalted is set, KVM could either leave the vCPU in the non-RUNNABLE
state (set before __kvm_emulate_halt()), or transition the vCPU to HALTED
and then RUNNABLE (pv_unhalted set after the kvm_vcpu_has_events() check).

Link: https://lore.kernel.org/r/20250224174156.2362059-1-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/x86.c

index 2231a2cd8489237a0c2b2ae91ae72e4585585ff3..bef63fd46d5a397f0293d48fe61698100db43829 100644 (file)
@@ -11297,9 +11297,8 @@ 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))
-                       vcpu->arch.pv.pv_unhalted = false;
-               else
-                       kvm_set_mp_state(vcpu, state);
+                       state = KVM_MP_STATE_RUNNABLE;
+               kvm_set_mp_state(vcpu, state);
                return 1;
        } else {
                vcpu->run->exit_reason = reason;