From: Dongli Zhang Date: Fri, 9 Jan 2026 07:53:56 +0000 (-0800) Subject: target/i386/kvm: set KVM_PMU_CAP_DISABLE if "-pmu" is configured X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=383374465155c682ddd6bb4002cdcd09995c9b7f;p=thirdparty%2Fqemu.git target/i386/kvm: set KVM_PMU_CAP_DISABLE if "-pmu" is configured Although AMD PERFCORE and PerfMonV2 are removed when "-pmu" is configured, there is no way to fully disable KVM AMD PMU virtualization. Neither "-cpu host,-pmu" nor "-cpu EPYC" achieves this. As a result, the following message still appears in the VM dmesg: [ 0.263615] Performance Events: AMD PMU driver. However, the expected output should be: [ 0.596381] Performance Events: PMU not available due to virtualization, using software events only. [ 0.600972] NMI watchdog: Perf NMI watchdog permanently disabled This occurs because AMD does not use any CPUID bit to indicate PMU availability. To address this, KVM_CAP_PMU_CAPABILITY is used to set KVM_PMU_CAP_DISABLE when "-pmu" is configured. Signed-off-by: Dongli Zhang Reviewed-by: Xiaoyao Li Reviewed-by: Zhao Liu Reviewed-by: Dapeng Mi Reviewed-by: Zide Chen Link: https://lore.kernel.org/r/20260109075508.113097-2-dongli.zhang@oracle.com Signed-off-by: Paolo Bonzini --- diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 0c940d4b64..69748420fd 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -178,6 +178,8 @@ static int has_triple_fault_event; static bool has_msr_mcg_ext_ctl; +static int pmu_cap; + static struct kvm_cpuid2 *cpuid_cache; static struct kvm_cpuid2 *hv_cpuid_cache; static struct kvm_msr_list *kvm_feature_msrs; @@ -2079,6 +2081,33 @@ full: int kvm_arch_pre_create_vcpu(CPUState *cpu, Error **errp) { + static bool first = true; + int ret; + + if (first) { + first = false; + + /* + * Since Linux v5.18, KVM provides a VM-level capability to easily + * disable PMUs; however, QEMU has been providing PMU property per + * CPU since v1.6. In order to accommodate both, have to configure + * the VM-level capability here. + * + * KVM_PMU_CAP_DISABLE doesn't change the PMU + * behavior on Intel platform because current "pmu" property works + * as expected. + */ + if ((pmu_cap & KVM_PMU_CAP_DISABLE) && !X86_CPU(cpu)->enable_pmu) { + ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PMU_CAPABILITY, 0, + KVM_PMU_CAP_DISABLE); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to set KVM_PMU_CAP_DISABLE"); + return ret; + } + } + } + if (is_tdx_vm()) { return tdx_pre_create_vcpu(cpu, errp); } @@ -3390,6 +3419,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } } + pmu_cap = kvm_check_extension(s, KVM_CAP_PMU_CAPABILITY); + return 0; }