From nobody Thu Dec 18 08:29:17 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3FFA170A13 for ; Mon, 24 Feb 2025 17:41:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740418921; cv=none; b=TS9PPHEOxIQYVB9+DWwlXOZlXr6kOl5Z04AHojC2Aix7+M6a73QvrAi127l1SkwbM08xDe+IIKnSR78knR8iqSqaC6k/FCDp4PJw8kWJlgl2OWLo7xmzNAMbEBOcCN67oC1t/5GEbGA5toG4slPK4mbgtB5CXiTPpg+yZiOw55c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740418921; c=relaxed/simple; bh=3IFadXaUYj7sdZ/YoFBbGyfHNpr1yxv29eaIHoixx3w=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=R2G2gxpH8dY69tUAvhafKd1UbMjqJoeyglc8y0iieKbsz3YrlQpCyZLwaratckCoWamP+0hkCuU62OWjn7lSMcqoxAneJ3A13yGM16zfA406d4KDvlGzaNaObo9E7G+zIdFzlMcNxpcAIeTXm86o8W15AM4AlauCzTkiUa2zl9U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j+ULiqkz; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j+ULiqkz" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fc318bd470so9042578a91.0 for ; Mon, 24 Feb 2025 09:41:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740418919; x=1741023719; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=e1EZBmSe2im3juqrZK3CYkAhncBM5TtZhAUdFg3KflY=; b=j+ULiqkzVJUHePkxzUuurPaTkSW8k50eUp1nwfd6iH50R/+0UbpZBByCxvYGL3+ESb lL6r0QWIzUvgOl55jUUxk+/2DhKzCcY2EfOLspVHlxTaomWZT5IP44Roqndyp/9Sloiz 5bA0Zj4WeMjKR5+H/GsEvqKjhueQ/hpoW9HP0WjSgEPWPPqS8KsdBMs6GaZ3FAvzPkQd 23J4CIz4aeNCbPRvnjffGm5zeGOt06AnW5y5lqMLm/yARDFRGwxYwAAdwc3mXEDzRYj7 /7UWlbQwAzK8au19wc9yxOrHO/VDjT9adz7KHNxAvfCsTPiU2tNflbnu4Z0PX3uyCweG J9QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740418919; x=1741023719; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e1EZBmSe2im3juqrZK3CYkAhncBM5TtZhAUdFg3KflY=; b=RypwHtsvwISWGlYUSvISndAsfmzdFd7TvAYx3w+EFWg6pOSPYVK/h64DrDeSExciYI TwxWSWUmx/daClMplaZItGdahOztNVTdvdG+9C4hAdBod1SIjRwDxCxr6pmRpRsmZ3Py NyLmdSs0naS7T5hCQ7yXDTiLLZeb1DDckoe3WEW0otmItNSPyajiTsfWg43VnvBXUxE0 A6nu7W5nYEN+PY1wWlg9Uw01KfJN3hEL3b1cJgj5/tTbFiC2p3BK4E/y/pzHGZEe3D/5 8Akd6kn6z+PNW79JkspGmQyFk2uJV57In+IrGaEjHdSpG8VPavoGCzGnfoop9BRbnJd2 En3Q== X-Forwarded-Encrypted: i=1; AJvYcCW2OG27JeMTlrCajOCb7xTQ8w32yBHRSaUX0/7UiwjAWQcTzt8HxX2W6qKgug3MpBlhCU0/z0fRo3JEsGU=@vger.kernel.org X-Gm-Message-State: AOJu0YwEPb0e3mjIs0BF4274JooBUF21PwNqlMsFTT7RgXTtL946MLS4 EmEKZ7Fh9ugHQ9Sd+Peb0ToutlYmUn1WMjQObuQHsNN/zyVodpgAGNTFZ1TyQhyXPc6MF2/arcR Ixg== X-Google-Smtp-Source: AGHT+IHFioBGXOxzm41NcBknr4shVziaEDjiaSXZYk4bXNvmJxj5hrZDjUahvq4dTClGOKbh+vDVlxvAwxw= X-Received: from pjbpt3.prod.google.com ([2002:a17:90b:3d03:b0:2fc:13d6:b4cb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a41:b0:2f4:47fc:7f18 with SMTP id 98e67ed59e1d1-2fce78a2965mr24749658a91.10.1740418919237; Mon, 24 Feb 2025 09:41:59 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 24 Feb 2025 09:41:56 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Message-ID: <20250224174156.2362059-1-seanjc@google.com> Subject: [PATCH] KVM: x86: Always set mp_state to RUNNABLE on wakeup from HLT From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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). Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 58b82d6fd77c..7f5abdaab935 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11310,9 +11310,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 =3D false; - else - kvm_set_mp_state(vcpu, state); + state =3D KVM_MP_STATE_RUNNABLE; + kvm_set_mp_state(vcpu, state); return 1; } else { vcpu->run->exit_reason =3D reason; base-commit: fed48e2967f402f561d80075a20c5c9e16866e53 --=20 2.48.1.658.g4767266eb4-goog