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=1796334427; b=k3Q+XzbaFvXwVQzmagFx1qzy0xpFzFGN4ahacFogdpF396lTdpgvxH0HRoStR1/yVkEn NL14678fwd5P4Iw8J4DiskYed7SofDt1dM7+FleKtlrWJtZPkyZC35n6cF0uUDVxWuDsI ksQ54a95XmY1bS+vyBIs+kjPqYLQBtVtC24NgI5iV14CGNWW7xo7FeLWxJ9jEMt4wDkdR y9Y9OSTPjOQFz59DnByP61+pd+++ka8FTKId5yDx3jivzF3Ci3Elg0QBJDVBoOuoJVB/8 MNAPufDMHfHsCF4q2GRjD7VrUNhxQHFfBvFR4pvCN5RaFu6/nE0T9X4m6m4yG17qmbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796334427; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=xHKohF4GKot4jzAwZKetYTKQ0VGFoD/HFRk115cE1Xs=; b=wOT1McrPICiO4/ORZLzawDfuZ6U9Jr9H/E4x4tws6imLSPDpZuQpZCFs3l4J/yPD2GDp bt1UqCwXpULNBrfBPlGSofvASx+GXpECO3BT9p7LBHME7kly8Zze29G0Yfg4d97KXJoQY Tzw63b3ghsu0otUgFlqXxedTfIvnR5ChBs3enKkYRaHveYmtU+v3fOY9ntXzpsNIbs3qz gWG1nG5wRxq3rlL6yGYaAQR2ACnLXMzjPJ7WKebd7Y4TF3JJm5QJKSH/L41dCsJba4WNt W0+LM26KPct5tWIVqFNQyzTzm8e6TOEIq8pZCV+/ysUDr6OGua0q9qnkyIOzqOkBsoQ== 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="Qpc+/s/8"; 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 518E7606A322; Thu, 3 Dec 2026 21:47:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Qpc+/s/8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94B7C1F000C2; Thu, 3 Dec 2026 21:47:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796334426; bh=xHKohF4GKot4jzAwZKetYTKQ0VGFoD/HFRk115cE1Xs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Qpc+/s/8aY7efjM9YYIn+7gAL/HTtcf3s/hDDFVXYVobiAm+8GazHbjD1G6ivcnoc HD+hn5ErJf7egLv2lUHGfRh5QQx1V2bGRXDExmy9VvkE4F6onElkMaepAzmLvGsFCN 5ctfKeNsC2pxCnjBXWvLT5jSKjvnEAGhg6cdJ6eFbH+bPMgM1iqv8w7NujqXYAl7cg jfmrkSc2Uf4TNXb+zf9vnPy7WmYY4WaiK75hka3btlxDQpsGgTatAsyEj+WXtPSbY1 S7PsJcMcjHLfaBub2IBKzI0aug4uS55WWrcl3jBH3xWxslFd1NyKEP5esTRX0wNhPy uIxJr/WGNnThg== Date: Thu, 3 Dec 2026 21:47:05 +0000 From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , Jim Mattson , David Kaplan Subject: Re: [PATCH 2/2] KVM: x86: Emit IBPB on pCPU migration if IBPB is advertised to guest Message-ID: References: <20260128013432.3250805-1-seanjc@google.com> <20260128013432.3250805-3-seanjc@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260128013432.3250805-3-seanjc@google.com> On Tue, Jan 27, 2026 at 05:34:32PM -0800, Sean Christopherson wrote: > Emit an Indirect Branch Prediction Barrier if a vCPU is migrated to a > different pCPU and IBPB support is advertised to the guest, to ensure any > IBPBs performed by the guest are effective across pCPUs. Ideally, KVM > would only emit IBPB if the guest performed an IBPB since the vCPU last > ran on the "new" pCPU, but pCPU migration is a relatively rare/slow path, > and so the cost of tracking which pCPUs a vCPUs has run on, let alone > intercepting PRED_CMD writes, outweighs the potential benefits of > avoiding IBPBs on pCPU migration. > > E.g. if a single vCPU is bouncing between pCPUs A and B, and the guest is > doing IBPBs on context switches to mitigate cross-task attacks, then the > following scenario can occur and needs to be mitigated by KVM: > > 1. vCPU starts on pCPU A. It runs a userspace task (task #1) which > installs various branch predictions into pCPU A's BTB. > 2. The vCPU is migrated to pCPU B. > 3. The guest switches to userspace task #2 and emits an IBPB, on pCPU B. > 4. The vCPU is migrated back to pCPU A. Userspace task (task #2) in the > guest now consumes the potentially dangerous branch predictions > installed in step 1 from task #1. > > Reported-by: David Kaplan > Cc: stable@vger.kernel.org > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/x86.c | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index e5ae655702b4..9d1641c2d83c 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -5201,6 +5201,19 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); > } > > + /* > + * If the vCPU is migrated to a different pCPU than the one on which > + * the vCPU last ran, and IBPB is advertised to the vCPU, then flush > + * indirect branch predictors before the next VM-Enter to ensure the > + * vCPU doesn't consume prediction information from a previous run on > + * the "new" pCPU. > + */ > + if (unlikely(vcpu->arch.last_vmentry_cpu != cpu && > + vcpu->arch.last_vmentry_cpu >= 0) && > + (guest_cpu_cap_has(vcpu, X86_FEATURE_SPEC_CTRL) || > + guest_cpu_cap_has(vcpu, X86_FEATURE_AMD_IBPB))) Should we use guest_has_pred_cmd_msr() here? This is what KVM uses to decide whether to intercept PRED_CMD or not, so the guest could have written PRED_CMD_SBPB to PRED_CMD. > + vcpu->arch.need_ibpb = true; > + > if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { > s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : > rdtsc() - vcpu->arch.last_host_tsc; > -- > 2.52.0.457.g6b5491de43-goog >