]> git.hungrycats.org Git - linux/commitdiff
KVM: x86: Clear hardware HLT state when userspace makes a vCPU not-halted
authorKeqiang Duan <duankeqiangcym@gmail.com>
Thu, 20 Aug 2026 12:35:12 +0000 (20:35 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:10:48 +0000 (12:10 -0500)
commit 883719f57f5d3898637b130d6db4742471cad618 upstream.

Force a vCPU out of its hardware-tracked halted state when userspace puts
the vCPU into any state other than HALTED via KVM_SET_MP_STATE, i.e. clear
VMCS.GUEST_ACTIVITY_STATE if it says the vCPU is halted.  Add an optional
kvm_x86_ops hook to do the clearing, as SVM has no equivalent VMCB field.

When HLT-exiting is disabled for a VM (KVM_CAP_X86_DISABLE_EXITS with
KVM_X86_DISABLE_EXITS_HLT, e.g. QEMU's "-overcommit cpu-pm=on"), a guest
HLT halts the physical CPU instead of exiting to KVM, and hardware saves
GUEST_ACTIVITY_STATE=HLT into the VMCS on the next VM-Exit.  That field is
sticky: it survives VM-Exit/VM-Enter and is only cleared by vmx_clear_hlt()
on event injection, or by vmx_vcpu_reset() on INIT / vCPU creation.

Nothing clears it on a userspace-driven state change.  KVM_SET_REGS only
writes the software register cache and KVM_SET_MP_STATE only writes
vcpu->arch.mp_state; kvm_vcpu_running() likewise consults software state
only.  A VMM that emulates a machine reset therefore ends up with a vCPU
that KVM happily VM-Enters while hardware refuses to fetch instructions.

Reproduce with a Linux guest by triggering a panic/kdump on a non-boot
vCPU: nmi_shootdown_cpus() parks the other vCPUs -- including vCPU0 -- in
crash_nmi_callback(), which does local_irq_disable() followed by a bare
HLT.  The capture kernel then resets the machine via port 0xCF9.  QEMU
rewrites RIP to 0xfff0 and sets mp_state to RUNNABLE, but vCPU0's
GUEST_ACTIVITY_STATE is still HLT, so the BSP never executes the reset
vector, never sends SIPIs, and the entire VM hangs at "reboot: machine
restart" forever.  Only destroying and recreating the VM recovers it.

Key off "not HALTED" rather than "is RUNNABLE" as HALTED is the only target
state for which retaining the hardware halted state is meaningful; every
other transition either can't be reached on VMX, e.g. AP_RESET_HOLD, or
goes through vmx_vcpu_reset(), which stuffs GUEST_ACTIVITY_ACTIVE anyways.
This also captures the intent more directly: KVM only ever drops the
hardware halted state, it never installs it.

Hook KVM_SET_MP_STATE rather than the VM-Enter path so that the clearing
is driven by an explicit userspace declaration, and so that no work is
added to vmx_vcpu_run().  Gate the call on kvm_hlt_in_guest() even though
vmx_clear_hlt() checks it as well, as the check is cheap and makes it
obvious in common code when the hardware state can exist at all.

Do nothing for TDX vCPUs as TDX doesn't support disabling HLT-exiting and
KVM cannot access a TD's VMCS, i.e. the VMCS used to run the vCPU can't
in HALTED state, and even if it were, KVM can't change it.

Fixes: caa057a2cad6 ("KVM: X86: Provide a capability to disable HLT intercepts")
Cc: stable@vger.kernel.org
Tested-by: Qinguang Chen <chenqinguang@kuaishou.com>
Signed-off-by: Zhiping Du <duzhiping@kuaishou.com>
Signed-off-by: Keqiang Duan <duankeqiangcym@gmail.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Link: https://patch.msgid.link/20260820123512.87236-1-duankeqiangcym@gmail.com
[sean: drop claim that spurious wakeups are ok, massage changelog]
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/include/asm/kvm-x86-ops.h
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/vmx/main.c
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/vmx/x86_ops.h
arch/x86/kvm/x86.c

index e213c9ae3e301dd9a773a46f1d7c42a24bdad661..dd9026c1071cee9b528eefcd9a51391d00642dcf 100644 (file)
@@ -82,6 +82,7 @@ KVM_X86_OP(interrupt_allowed)
 KVM_X86_OP(nmi_allowed)
 KVM_X86_OP(get_nmi_mask)
 KVM_X86_OP(set_nmi_mask)
+KVM_X86_OP_OPTIONAL(clear_hlt)
 KVM_X86_OP(enable_nmi_window)
 KVM_X86_OP(enable_irq_window)
 KVM_X86_OP_OPTIONAL(update_cr8_intercept)
index 683bb8bf43a94540b34472a432ddecfb1cbceb1f..e97f2ef6d27825e26adabdba2ca688e396ff043e 100644 (file)
@@ -1607,6 +1607,13 @@ struct kvm_x86_ops {
        int (*nmi_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
        bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
        void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
+       /*
+        * Force the vCPU out of any hardware-tracked halted/inactive state so
+        * that it will fetch and execute instructions on the next VM-Enter.
+        * Only needed by VMX, where VMCS.GUEST_ACTIVITY_STATE persists across
+        * VM-Exit/VM-Enter; SVM has no equivalent VMCB field.
+        */
+       void (*clear_hlt)(struct kvm_vcpu *vcpu);
        /* Whether or not a virtual NMI is pending in hardware. */
        bool (*is_vnmi_pending)(struct kvm_vcpu *vcpu);
        /*
index 835de375e8c8c761a5ff13947aabfbb877ca6166..75e5ca47a57bec6ff8b28e91b966b237eb7c7ede 100644 (file)
@@ -603,6 +603,18 @@ static void vt_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
        vmx_set_nmi_mask(vcpu, masked);
 }
 
+static void vt_clear_hlt(struct kvm_vcpu *vcpu)
+{
+       /*
+        * TDX doesn't support disabling HLT-exiting, and KVM can't access a
+        * TD's VMCS, so there is never any hardware halted state to clear.
+        */
+       if (is_td_vcpu(vcpu))
+               return;
+
+       vmx_clear_hlt(vcpu);
+}
+
 static void vt_enable_nmi_window(struct kvm_vcpu *vcpu)
 {
        /* Refer to the comments in tdx_inject_nmi(). */
@@ -977,6 +989,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
        .nmi_allowed = vt_op(nmi_allowed),
        .get_nmi_mask = vt_op(get_nmi_mask),
        .set_nmi_mask = vt_op(set_nmi_mask),
+       .clear_hlt = vt_op(clear_hlt),
        .enable_nmi_window = vt_op(enable_nmi_window),
        .enable_irq_window = vt_op(enable_irq_window),
        .update_cr8_intercept = vt_op(update_cr8_intercept),
index 156db15db998e4827bddd14905e6c36aeb73e21c..9de089ac3ecc09cfad7dbeb44ccf228c113da9af 100644 (file)
@@ -1909,7 +1909,7 @@ int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
        return skip_emulated_instruction(vcpu);
 }
 
-static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
+void vmx_clear_hlt(struct kvm_vcpu *vcpu)
 {
        /*
         * Ensure that we clear the HLT state in the VMCS.  We don't need to
index 054fd14bb2e16b66ebf16cc830dd7d2520c3dd85..0b8f364621abcf9db836fcae3e6bfabd8c6b86c2 100644 (file)
@@ -95,6 +95,7 @@ int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection);
 int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection);
 bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
 void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
+void vmx_clear_hlt(struct kvm_vcpu *vcpu);
 void vmx_enable_nmi_window(struct kvm_vcpu *vcpu);
 void vmx_enable_irq_window(struct kvm_vcpu *vcpu);
 void vmx_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr);
index aad065d035fbb195fa83e47a8e3a25065ca52977..82e01b32d25d6cfaae67b9a93b535185c414194c 100644 (file)
@@ -9062,6 +9062,19 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
        }
 
        kvm_set_mp_state(vcpu, mp_state->mp_state);
+
+       /*
+        * Force the vCPU out of any hardware-tracked halted state, e.g. VMX's
+        * GUEST_ACTIVITY_STATE=HLT, when userspace puts the vCPU into a state
+        * other than HALTED.  The hardware state is sticky across VM-Exit and
+        * VM-Enter and is not touched by any other ioctl, so a vCPU that halted
+        * with HLT-exiting disabled stays wedged even after userspace rewrites
+        * its registers, e.g. when a VMM emulates a machine reset.
+        */
+       if (kvm_hlt_in_guest(vcpu->kvm) &&
+           mp_state->mp_state != KVM_MP_STATE_HALTED)
+               kvm_x86_call(clear_hlt)(vcpu);
+
        kvm_make_request(KVM_REQ_EVENT, vcpu);
 
        ret = 0;