From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry.ahmed@linux.dev>,
Jim Mattson <jmattson@google.com>,
David Kaplan <david.kaplan@amd.com>
Subject: Re: [PATCH 2/2] KVM: x86: Emit IBPB on pCPU migration if IBPB is advertised to guest
Date: Thu, 3 Dec 2026 21:47:05 +0000 [thread overview]
Message-ID: <axHiuX73TxOUB3fN@google.com> (raw)
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 <david.kaplan@amd.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> 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
>
prev parent reply other threads:[~2026-12-03 21:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-28 1:34 [PATCH 0/2] KVM: x86: Plug an intra-guest Spectre v2 hole Sean Christopherson
2026-01-28 1:34 ` [PATCH 1/2] KVM: x86: Defer IBPBs for vCPU and nested transitions until core run loop Sean Christopherson
2026-01-30 18:21 ` Yosry Ahmed
2026-12-03 20:55 ` Yosry Ahmed
2026-01-28 1:34 ` [PATCH 2/2] KVM: x86: Emit IBPB on pCPU migration if IBPB is advertised to guest Sean Christopherson
2026-01-30 18:22 ` Yosry Ahmed
2026-12-03 21:47 ` Yosry Ahmed [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axHiuX73TxOUB3fN@google.com \
--to=yosry@kernel.org \
--cc=david.kaplan@amd.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=yosry.ahmed@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox