public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Borislav Petkov <bp@alien8.de>
Cc: Yosry Ahmed <yosry.ahmed@linux.dev>,
	Patrick Bellasi <derkling@google.com>,
	 Paolo Bonzini <pbonzini@redhat.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	 Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
	x86@kernel.org, kvm@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Patrick Bellasi <derkling@matbug.net>,
	 Brendan Jackman <jackmanb@google.com>,
	David Kaplan <David.Kaplan@amd.com>,
	 Michael Larabel <Michael@michaellarabel.com>
Subject: Re: x86/bugs: KVM: Add support for SRSO_MSR_FIX, back for moar
Date: Wed, 30 Apr 2025 16:33:19 -0700	[thread overview]
Message-ID: <aBKzPyqNTwogNLln@google.com> (raw)
In-Reply-To: <20250429132546.GAaBDTWqOsWX8alox2@fat_crate.local>

On Tue, Apr 29, 2025, Borislav Petkov wrote:
> On Tue, Feb 18, 2025 at 12:13:33PM +0100, Borislav Petkov wrote:
> > So,
> > 
> > in the interest of finally making some progress here I'd like to commit this
> > below (will test it one more time just in case but it should work :-P). It is
> > simple and straight-forward and doesn't need an IBPB when the bit gets
> > cleared.
> > 
> > A potential future improvement is David's suggestion that there could be a way
> > for tracking when the first guest gets started, we set the bit then, we make
> > sure the bit gets set on each logical CPU when the guests migrate across the
> > machine and when the *last* guest exists, that bit gets cleared again.
> 
> Well, that "simplicity" was short-lived:
> 
> https://www.phoronix.com/review/linux-615-amd-regression

LOL.

> Sean, how about this below?

Eww.  That's quite painful, and completely disallowing enable_virt_on_load is
undesirable, e.g. for use cases where the host is (almost) exclusively running
VMs.

Best idea I have is to throw in the towel on getting fancy, and just maintain a
dedicated count in SVM.

Alternatively, we could plumb an arch hook into kvm_create_vm() and kvm_destroy_vm()
that's called when KVM adds/deletes a VM from vm_list, and key off vm_list being
empty.  But that adds a lot of boilerplate just to avoid a mutex+count.

I haven't tested on a system with X86_FEATURE_SRSO_BP_SPEC_REDUCE, but did verify
the mechanics by inverting the flag.

--
From: Sean Christopherson <seanjc@google.com>
Date: Wed, 30 Apr 2025 15:34:50 -0700
Subject: [PATCH] KVM: SVM: Set/clear SRSO's BP_SPEC_REDUCE on 0 <=> 1 VM count
 transitions

Set the magic BP_SPEC_REDUCE bit to mitigate SRSO when running VMs if and
only if KVM has at least one active VM.  Leaving the bit set at all times
unfortunately degrades performance by a wee bit more than expected.

Use a dedicated mutex and counter instead of hooking virtualization
enablement, as changing the behavior of kvm.enable_virt_at_load based on
SRSO_BP_SPEC_REDUCE is painful, and has its own drawbacks, e.g. could
result in performance issues for flows that are sensity to VM creation
latency.

Fixes: 8442df2b49ed ("x86/bugs: KVM: Add support for SRSO_MSR_FIX")
Reported-by: Michael Larabel <Michael@michaellarabel.com>
Closes: https://www.phoronix.com/review/linux-615-amd-regression
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/svm.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index d5d0c5c3300b..fe8866572218 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -607,9 +607,6 @@ static void svm_disable_virtualization_cpu(void)
 	kvm_cpu_svm_disable();
 
 	amd_pmu_disable_virt();
-
-	if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
-		msr_clear_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
 }
 
 static int svm_enable_virtualization_cpu(void)
@@ -687,9 +684,6 @@ static int svm_enable_virtualization_cpu(void)
 		rdmsr(MSR_TSC_AUX, sev_es_host_save_area(sd)->tsc_aux, msr_hi);
 	}
 
-	if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
-		msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
-
 	return 0;
 }
 
@@ -5032,10 +5026,42 @@ static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
 	sev_vcpu_deliver_sipi_vector(vcpu, vector);
 }
 
+static DEFINE_MUTEX(srso_lock);
+static int srso_nr_vms;
+
+static void svm_toggle_srso_spec_reduce(void *set)
+{
+	if (set)
+		msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
+	else
+		msr_clear_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
+}
+
+static void svm_srso_add_remove_vm(int count)
+{
+	bool set;
+
+	if (!cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
+		return;
+
+	guard(mutex)(&srso_lock);
+
+	set = !srso_nr_vms;
+	srso_nr_vms += count;
+
+	WARN_ON_ONCE(srso_nr_vms < 0);
+	if (!set && srso_nr_vms)
+		return;
+
+	on_each_cpu(svm_toggle_srso_spec_reduce, (void *)set, 1);
+}
+
 static void svm_vm_destroy(struct kvm *kvm)
 {
 	avic_vm_destroy(kvm);
 	sev_vm_destroy(kvm);
+
+	svm_srso_add_remove_vm(-1);
 }
 
 static int svm_vm_init(struct kvm *kvm)
@@ -5061,6 +5087,7 @@ static int svm_vm_init(struct kvm *kvm)
 			return ret;
 	}
 
+	svm_srso_add_remove_vm(1);
 	return 0;
 }
 

base-commit: f158e1b145f73aae1d3b7e756eb129a15b2b7a90
--

  reply	other threads:[~2025-04-30 23:33 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-02 12:04 [PATCH v2 0/4] x86/bugs: Adjust SRSO mitigation to new features Borislav Petkov
2024-12-02 12:04 ` [PATCH v2 1/4] x86/bugs: Add SRSO_USER_KERNEL_NO support Borislav Petkov
2024-12-10  6:53   ` Josh Poimboeuf
2024-12-10 15:37     ` Borislav Petkov
2024-12-11  7:53       ` Josh Poimboeuf
2024-12-11 20:38         ` Borislav Petkov
2024-12-11 22:35           ` Sean Christopherson
2024-12-16 17:21             ` Borislav Petkov
2024-12-02 12:04 ` [PATCH v2 2/4] KVM: x86: Advertise SRSO_USER_KERNEL_NO to userspace Borislav Petkov
2024-12-02 12:04 ` [PATCH v2 3/4] x86/bugs: KVM: Add support for SRSO_MSR_FIX Borislav Petkov
2024-12-11 22:27   ` Sean Christopherson
2024-12-16 17:31     ` Borislav Petkov
2024-12-16 18:51       ` Sean Christopherson
2024-12-17  9:34         ` Borislav Petkov
2024-12-30 11:14         ` Borislav Petkov
2025-01-08 13:38           ` Sean Christopherson
2025-01-08 15:49             ` Borislav Petkov
2025-01-08 17:18               ` Sean Christopherson
2025-01-08 18:14                 ` Borislav Petkov
2025-01-08 18:37                   ` Jim Mattson
2025-01-08 19:14                     ` Borislav Petkov
2025-01-08 19:43                       ` Jim Mattson
2025-01-08 19:45                         ` Borislav Petkov
2025-01-11 12:52                   ` [PATCH] " Borislav Petkov
2025-01-17 18:56                     ` Sean Christopherson
2025-01-18 15:26                       ` Borislav Petkov
2025-01-23 16:25                         ` Sean Christopherson
2025-01-23 17:01                           ` Borislav Petkov
2025-01-23 18:04                             ` Sean Christopherson
2025-01-24 12:58                               ` Borislav Petkov
2025-02-11 19:19                                 ` Jim Mattson
2025-02-11 20:51                                   ` Borislav Petkov
2025-02-13 10:53                             ` Patrick Bellasi
2025-02-13 13:44                               ` Patrick Bellasi
2025-02-13 14:28                                 ` Borislav Petkov
2025-02-13 17:50                                   ` Patrick Bellasi
2025-02-14 20:10                                     ` Borislav Petkov
2025-02-15  0:57                                       ` Yosry Ahmed
2025-02-15  9:15                                         ` Borislav Petkov
2025-02-17  5:47                                           ` Yosry Ahmed
2025-02-17 15:26                                             ` Borislav Petkov
2025-02-15 12:53                                       ` Borislav Petkov
2025-02-17  5:59                                         ` Yosry Ahmed
2025-02-17 16:07                                           ` Borislav Petkov
2025-02-17 19:56                                             ` Yosry Ahmed
2025-02-17 20:20                                               ` Borislav Petkov
2025-02-17 20:32                                                 ` Yosry Ahmed
2025-02-18 11:13                                                   ` [PATCH final?] " Borislav Petkov
2025-02-18 14:42                                                     ` Patrick Bellasi
2025-02-18 15:34                                                       ` Borislav Petkov
2025-04-29 13:25                                                     ` x86/bugs: KVM: Add support for SRSO_MSR_FIX, back for moar Borislav Petkov
2025-04-30 23:33                                                       ` Sean Christopherson [this message]
2025-05-01  0:42                                                         ` Michael Larabel
2025-05-01  8:19                                                         ` Borislav Petkov
2025-05-01 16:56                                                           ` Sean Christopherson
2025-05-05 15:25                                                             ` Borislav Petkov
2025-05-05 15:40                                                               ` Kaplan, David
2025-05-05 15:47                                                                 ` Borislav Petkov
2025-05-05 16:30                                                                 ` Sean Christopherson
2025-05-05 16:42                                                                   ` Kaplan, David
2025-05-05 18:03                                                                     ` Sean Christopherson
2025-05-05 18:25                                                                       ` Kaplan, David
2024-12-02 12:04 ` [PATCH v2 4/4] Documentation/kernel-parameters: Fix a typo in kvm.enable_virt_at_load text Borislav Petkov
2024-12-03 14:30 ` [PATCH v2 0/4] x86/bugs: Adjust SRSO mitigation to new features Nikolay Borisov
  -- strict thread matches above, loose matches on Subject: below --
2025-05-01 15:03 x86/bugs: KVM: Add support for SRSO_MSR_FIX, back for moar Patrick Bellasi

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=aBKzPyqNTwogNLln@google.com \
    --to=seanjc@google.com \
    --cc=David.Kaplan@amd.com \
    --cc=Michael@michaellarabel.com \
    --cc=bp@alien8.de \
    --cc=derkling@google.com \
    --cc=derkling@matbug.net \
    --cc=jackmanb@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=pbonzini@redhat.com \
    --cc=x86@kernel.org \
    --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