KVM: x86/pmu: WARN and bug the VM if PMU is refreshed after vCPU has run
authorSean Christopherson <seanjc@google.com>
Sat, 11 Mar 2023 00:46:04 +0000 (16:46 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 6 Apr 2023 21:58:43 +0000 (14:58 -0700)
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
feb627e8d6f6 ("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>
arch/x86/kvm/pmu.c
arch/x86/kvm/x86.c

index 29492c2a0c8261b124a899dedc75fff539142975..136184fe9e9236d3b641bdd0c169c099860d5a66 100644 (file)
@@ -589,6 +589,9 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  */
 void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 {
+       if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm))
+               return;
+
        bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX);
        static_call(kvm_x86_pmu_refresh)(vcpu);
 }
index cf33b7d910a6cb3af8d4602f1384ab2c0a79aed5..a86ad45a53b881ad5aaca5522f57882c931c874c 100644 (file)
@@ -3632,9 +3632,17 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (data & ~kvm_caps.supported_perf_cap)
                        return 1;
 
+               /*
+                * Note, this is not just a performance optimization!  KVM
+                * disallows changing feature MSRs after the vCPU has run; PMU
+                * refresh will bug the VM if called after the vCPU has run.
+                */
+               if (vcpu->arch.perf_capabilities == data)
+                       break;
+
                vcpu->arch.perf_capabilities = data;
                kvm_pmu_refresh(vcpu);
-               return 0;
+               break;
        case MSR_EFER:
                return set_efer(vcpu, msr_info);
        case MSR_K7_HWCR: