Skip to content

Commit 3a6de51

Browse files
committed
KVM: x86/pmu: WARN and bug the VM if PMU is refreshed after vCPU has run
Now that KVM disallows changing feature MSRs, i.e. PERF_CAPABILITIES, after running a vCPU, WARN and bug the VM if the PMU is refreshed after the vCPU has run. Note, KVM has disallowed CPUID updates after running a vCPU since commit feb627e ("KVM: x86: Forbid KVM_SET_CPUID{,2} after KVM_RUN"), i.e. PERF_CAPABILITIES was the only remaining way to trigger a PMU refresh after KVM_RUN. Cc: Like Xu <like.xu.linux@gmail.com> Link: https://lore.kernel.org/r/20230311004618.920745-8-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 0094f62 commit 3a6de51

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

arch/x86/kvm/pmu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,9 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
589589
*/
590590
void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
591591
{
592+
if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm))
593+
return;
594+
592595
bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX);
593596
static_call(kvm_x86_pmu_refresh)(vcpu);
594597
}

arch/x86/kvm/x86.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3632,9 +3632,17 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
36323632
if (data & ~kvm_caps.supported_perf_cap)
36333633
return 1;
36343634

3635+
/*
3636+
* Note, this is not just a performance optimization! KVM
3637+
* disallows changing feature MSRs after the vCPU has run; PMU
3638+
* refresh will bug the VM if called after the vCPU has run.
3639+
*/
3640+
if (vcpu->arch.perf_capabilities == data)
3641+
break;
3642+
36353643
vcpu->arch.perf_capabilities = data;
36363644
kvm_pmu_refresh(vcpu);
3637-
return 0;
3645+
break;
36383646
case MSR_EFER:
36393647
return set_efer(vcpu, msr_info);
36403648
case MSR_K7_HWCR:

0 commit comments

Comments
 (0)