Skip to content

Commit e3417ab

Browse files
committed
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 spinlock 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 sensitive to VM creation latency. Defer setting BP_SPEC_REDUCE until VMRUN is imminent to avoid impacting performance on CPUs that aren't running VMs, e.g. if a setup is using housekeeping CPUs. Setting BP_SPEC_REDUCE in task context, i.e. without blasting IPIs to all CPUs, also helps avoid serializing 1<=>N transitions without incurring a gross amount of complexity (see the Link for details on how ugly coordinating via IPIs gets). Link: https://lore.kernel.org/all/aBOnzNCngyS_pQIW@google.com Fixes: 8442df2 ("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 Cc: Borislav Petkov <bp@alien8.de> Tested-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20250505180300.973137-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9129633 commit e3417ab

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,6 @@ static void svm_disable_virtualization_cpu(void)
607607
kvm_cpu_svm_disable();
608608

609609
amd_pmu_disable_virt();
610-
611-
if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
612-
msr_clear_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
613610
}
614611

615612
static int svm_enable_virtualization_cpu(void)
@@ -687,9 +684,6 @@ static int svm_enable_virtualization_cpu(void)
687684
rdmsr(MSR_TSC_AUX, sev_es_host_save_area(sd)->tsc_aux, msr_hi);
688685
}
689686

690-
if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
691-
msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
692-
693687
return 0;
694688
}
695689

@@ -1518,6 +1512,63 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
15181512
__free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
15191513
}
15201514

1515+
#ifdef CONFIG_CPU_MITIGATIONS
1516+
static DEFINE_SPINLOCK(srso_lock);
1517+
static atomic_t srso_nr_vms;
1518+
1519+
static void svm_srso_clear_bp_spec_reduce(void *ign)
1520+
{
1521+
struct svm_cpu_data *sd = this_cpu_ptr(&svm_data);
1522+
1523+
if (!sd->bp_spec_reduce_set)
1524+
return;
1525+
1526+
msr_clear_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
1527+
sd->bp_spec_reduce_set = false;
1528+
}
1529+
1530+
static void svm_srso_vm_destroy(void)
1531+
{
1532+
if (!cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
1533+
return;
1534+
1535+
if (atomic_dec_return(&srso_nr_vms))
1536+
return;
1537+
1538+
guard(spinlock)(&srso_lock);
1539+
1540+
/*
1541+
* Verify a new VM didn't come along, acquire the lock, and increment
1542+
* the count before this task acquired the lock.
1543+
*/
1544+
if (atomic_read(&srso_nr_vms))
1545+
return;
1546+
1547+
on_each_cpu(svm_srso_clear_bp_spec_reduce, NULL, 1);
1548+
}
1549+
1550+
static void svm_srso_vm_init(void)
1551+
{
1552+
if (!cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE))
1553+
return;
1554+
1555+
/*
1556+
* Acquire the lock on 0 => 1 transitions to ensure a potential 1 => 0
1557+
* transition, i.e. destroying the last VM, is fully complete, e.g. so
1558+
* that a delayed IPI doesn't clear BP_SPEC_REDUCE after a vCPU runs.
1559+
*/
1560+
if (atomic_inc_not_zero(&srso_nr_vms))
1561+
return;
1562+
1563+
guard(spinlock)(&srso_lock);
1564+
1565+
atomic_inc(&srso_nr_vms);
1566+
}
1567+
#else
1568+
static void svm_srso_vm_init(void) { }
1569+
static void svm_srso_vm_destroy(void) { }
1570+
#endif
1571+
15211572
static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
15221573
{
15231574
struct vcpu_svm *svm = to_svm(vcpu);
@@ -1550,6 +1601,11 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
15501601
(!boot_cpu_has(X86_FEATURE_V_TSC_AUX) || !sev_es_guest(vcpu->kvm)))
15511602
kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
15521603

1604+
if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE) &&
1605+
!sd->bp_spec_reduce_set) {
1606+
sd->bp_spec_reduce_set = true;
1607+
msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
1608+
}
15531609
svm->guest_state_loaded = true;
15541610
}
15551611

@@ -5040,6 +5096,8 @@ static void svm_vm_destroy(struct kvm *kvm)
50405096
{
50415097
avic_vm_destroy(kvm);
50425098
sev_vm_destroy(kvm);
5099+
5100+
svm_srso_vm_destroy();
50435101
}
50445102

50455103
static int svm_vm_init(struct kvm *kvm)
@@ -5065,6 +5123,7 @@ static int svm_vm_init(struct kvm *kvm)
50655123
return ret;
50665124
}
50675125

5126+
svm_srso_vm_init();
50685127
return 0;
50695128
}
50705129

arch/x86/kvm/svm/svm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ struct svm_cpu_data {
335335
u32 next_asid;
336336
u32 min_asid;
337337

338+
bool bp_spec_reduce_set;
339+
338340
struct vmcb *save_area;
339341
unsigned long save_area_pa;
340342

0 commit comments

Comments
 (0)