From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793981248; b=AuIWvs84xO+cZB1ePVVYXbQH/FY7ZW6OIrWqZNi7ic8H/8xjEATwfv09JtAJIK+iuxGi OfHSJLnUnrCtUr+6xTjwLI5lr9k4sIkb96mQ4zaBypP9K/0wErXVolmJApmqVGl+Kqs0E gHLwyyY6tmlibMyjOu5hUJyo6HNhkXtw8rqsF4tJdhNaiz17aQyQzyl5tRId2mpcw2k6K nAAfv0Aunqt7Mr66sLKRX5j/ryoYahhonJabRxCfC6iirYWukBj2vWVwubV1OQtpDyBvT 97dUNthuyuY4wZtxylMy/ABOeclLVVA4a140LIBQTyz/LysRs2agykV5CmOaLo4hUjA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793981248; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=00+S1ncG6YAwmfnUVxPizp2qUUiAsuEdaMz3ipr8jcQ=; b=wzNAFhvoUc23kNOxgL7T5avqT+45efPhlwpWrd5AraBEx03t8q5LUEGxUsWg3l+Dx340 pVEtm5XOXdkaBxzX5l+36MaEFEWIT+9cU7HQeagrK2VZ4rKLnWd1PMNRHOw5EiKDFIJEN Ylc75oXwljCcqSmgjgWbxgJbPEqIaOhnMKQSd7pxPtDyoF7moNYAhaXbM58uG5uEVT5s4 URv1nUt8WO5RSen+vQnOCM8s0bKjInKHAZIWAMs5Tdcwm9C4WLs15Q2LW1vexRiTa7n0g zmTwuMfcG5rA8dCe9a+uxaeGnysfMzDuvVIKMa+3g35HZQdn9qeRHFv+H+IdHfze9HQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=D7hgOwqT; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F383A606A326 for ; Fri, 6 Nov 2026 16:07:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=D7hgOwqT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 13EA21F0089F; Fri, 6 Nov 2026 16:07:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793981247; bh=00+S1ncG6YAwmfnUVxPizp2qUUiAsuEdaMz3ipr8jcQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=D7hgOwqTX4ImYRgjca121GTm0/Rdf+pVSJbGn0UyatDk/w5KQIzAVAtVSmCItRSH4 Rwmx+qjsU7J5SuIGoKJyVz7/iz7XwJ3sWI0JaJYru7mkmDKl5Azz4iX8e0QQatjJy9 k9CUl63o5k15SM6IEKzUaEjr4T9LXo4uxpRRYpoiMKNj5BemfVmft/s0+goQm+YCHL 7VbYNUKm6uMqNt0J4gyVaiTKJ+8vry41iH6PP9wRHZXmh/LVrEAL7PuGud0Vyn+7Mb /GdZVomLq1cmRsFsIRCJ1xk60UEI7qocKbrHnKWWoQ1modZMmciJld25+3Bi2bF9EQ zq4aA5kbDSumA== From: Sasha Levin To: stable@vger.kernel.org Cc: Keqiang Duan , Qinguang Chen , Zhiping Du , Chao Gao , Sean Christopherson , Sasha Levin Subject: [PATCH 5.15.y] KVM: x86: Clear hardware HLT state when userspace makes a vCPU not-halted Date: Fri, 6 Nov 2026 11:07:25 -0500 Message-ID: <20261106160636.402535-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026110436-molecule-hardness-92c9@gregkh> References: <2026110436-molecule-hardness-92c9@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Keqiang Duan [ Upstream commit 883719f57f5d3898637b130d6db4742471cad618 ] 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 Signed-off-by: Zhiping Du Signed-off-by: Keqiang Duan Reviewed-by: Chao Gao 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 [ sashal: Adapted clear_hlt to the existing VMX callback interface using KVM_X86_OP_NULL and static_call_cond(). ] Signed-off-by: Sasha Levin --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 13 +++++++++++++ 4 files changed, 22 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 9e00275576b1f..dae111324c0e6 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -71,6 +71,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_NULL(clear_hlt) KVM_X86_OP(enable_nmi_window) KVM_X86_OP(enable_irq_window) KVM_X86_OP(update_cr8_intercept) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d31817fb39620..8bdf591d93c17 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1394,6 +1394,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); void (*enable_nmi_window)(struct kvm_vcpu *vcpu); void (*enable_irq_window)(struct kvm_vcpu *vcpu); void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 77b1161e36e74..c234c6ff88490 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7810,6 +7810,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .nmi_allowed = vmx_nmi_allowed, .get_nmi_mask = vmx_get_nmi_mask, .set_nmi_mask = vmx_set_nmi_mask, + .clear_hlt = vmx_clear_hlt, .enable_nmi_window = vmx_enable_nmi_window, .enable_irq_window = vmx_enable_irq_window, .update_cr8_intercept = vmx_update_cr8_intercept, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bbfc8ccf4fcd9..99f58a57280a4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10666,6 +10666,19 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); } else vcpu->arch.mp_state = 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) + static_call_cond(kvm_x86_clear_hlt)(vcpu); + kvm_make_request(KVM_REQ_EVENT, vcpu); ret = 0; -- 2.53.0