From: Xiong Zhang <xiong.y.zhang@xxxxxxxxxxxxxxx> When passthrough PMU is enabled by kvm and perf, KVM call perf_get_mediated_pmu() to exclusive own x86 core PMU at VM creation, KVM call perf_put_mediated_pmu() to return x86 core PMU to host perf at VM destroy. When perf_get_mediated_pmu() fail, the host has system wide perf events without exclude_guest = 1 which must be disabled to enable VM with passthrough PMU. Once VM with passthrough PMU starts, perf will refuse to create system wide perf event without exclude_guest = 1 until the vm is closed. Signed-off-by: Xiong Zhang <xiong.y.zhang@xxxxxxxxxxxxxxx> --- arch/x86/kvm/x86.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index db395c00955f..3152587eca5b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6674,8 +6674,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, if (!kvm->created_vcpus) { kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); /* Disable passthrough PMU if enable_pmu is false. */ - if (!kvm->arch.enable_pmu) + if (!kvm->arch.enable_pmu) { + if (kvm->arch.enable_passthrough_pmu) + perf_put_mediated_pmu(); kvm->arch.enable_passthrough_pmu = false; + } r = 0; } mutex_unlock(&kvm->lock); @@ -12578,6 +12581,14 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.guest_can_read_msr_platform_info = true; kvm->arch.enable_pmu = enable_pmu; kvm->arch.enable_passthrough_pmu = enable_passthrough_pmu; + if (kvm->arch.enable_passthrough_pmu) { + ret = perf_get_mediated_pmu(); + if (ret < 0) { + kvm_err("failed to enable mediated passthrough pmu, please disable system wide perf events\n"); + goto out_uninit_mmu; + } + } + #if IS_ENABLED(CONFIG_HYPERV) spin_lock_init(&kvm->arch.hv_root_tdp_lock); @@ -12726,6 +12737,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm) __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); mutex_unlock(&kvm->slots_lock); } + if (kvm->arch.enable_passthrough_pmu) + perf_put_mediated_pmu(); kvm_unload_vcpu_mmus(kvm); static_call_cond(kvm_x86_vm_destroy)(kvm); kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); -- 2.45.0.rc1.225.g2a3ae87e7f-goog