All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dongli Zhang <dongli.zhang@oracle.com>
To: kvm@vger.kernel.org, qemu-devel@nongnu.org, qemu-arm@nongnu.org,
	qemu-ppc@nongnu.org, qemu-riscv@nongnu.org,
	qemu-s390x@nongnu.org
Cc: pbonzini@redhat.com, peter.maydell@linaro.org,
	mtosatti@redhat.com, chenhuacai@kernel.org, philmd@linaro.org,
	aurelien@aurel32.net, jiaxun.yang@flygoat.com,
	aleksandar.rikalo@syrmia.com, danielhb413@gmail.com,
	clg@kaod.org, david@gibson.dropbear.id.au, groug@kaod.org,
	palmer@dabbelt.com, alistair.francis@wdc.com,
	bin.meng@windriver.com, pasic@linux.ibm.com,
	borntraeger@linux.ibm.com, richard.henderson@linaro.org,
	david@redhat.com, iii@linux.ibm.com, thuth@redhat.com,
	joe.jin@oracle.com, likexu@tencent.com
Subject: [PATCH 2/3] i386: kvm: disable KVM_CAP_PMU_CAPABILITY if "pmu" is disabled
Date: Sat, 19 Nov 2022 04:29:00 -0800	[thread overview]
Message-ID: <20221119122901.2469-3-dongli.zhang@oracle.com> (raw)
In-Reply-To: <20221119122901.2469-1-dongli.zhang@oracle.com>

The "perf stat" at the VM side still works even we set "-cpu host,-pmu" in
the QEMU command line. That is, neither "-cpu host,-pmu" nor "-cpu EPYC"
could disable the pmu virtualization in an AMD environment.

We still see below at VM kernel side ...

[    0.510611] Performance Events: Fam17h+ core perfctr, AMD PMU driver.

... although we expect something like below.

[    0.596381] Performance Events: PMU not available due to virtualization, using software events only.
[    0.600972] NMI watchdog: Perf NMI watchdog permanently disabled

This is because the AMD pmu (v1) does not rely on cpuid to decide if the
pmu virtualization is supported.

We disable KVM_CAP_PMU_CAPABILITY if the 'pmu' is disabled in the vcpu
properties.

Cc: Joe Jin <joe.jin@oracle.com>
Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
---
 target/i386/kvm/kvm.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 8fec0bc5b5..0b1226ff7f 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -137,6 +137,8 @@ static int has_triple_fault_event;
 
 static bool has_msr_mcg_ext_ctl;
 
+static int has_pmu_cap;
+
 static struct kvm_cpuid2 *cpuid_cache;
 static struct kvm_cpuid2 *hv_cpuid_cache;
 static struct kvm_msr_list *kvm_feature_msrs;
@@ -1725,6 +1727,19 @@ static void kvm_init_nested_state(CPUX86State *env)
 
 void kvm_arch_pre_create_vcpu(CPUState *cs)
 {
+    X86CPU *cpu = X86_CPU(cs);
+    int ret;
+
+    if (has_pmu_cap && !cpu->enable_pmu) {
+        ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PMU_CAPABILITY, 0,
+                                KVM_PMU_CAP_DISABLE);
+        if (ret < 0) {
+            error_report("kvm: Failed to disable pmu cap: %s",
+                         strerror(-ret));
+        }
+
+        has_pmu_cap = 0;
+    }
 }
 
 int kvm_arch_init_vcpu(CPUState *cs)
@@ -2517,6 +2532,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    has_pmu_cap = kvm_check_extension(s, KVM_CAP_PMU_CAPABILITY);
+
     ret = kvm_get_supported_msrs(s);
     if (ret < 0) {
         return ret;
-- 
2.34.1


  parent reply	other threads:[~2022-11-19 12:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-19 12:28 [PATCH 0/3] kvm: fix two svm pmu virtualization bugs Dongli Zhang
2022-11-19 12:28 ` [PATCH 1/3] kvm: introduce a helper before creating the 1st vcpu Dongli Zhang
2022-11-19 12:29 ` Dongli Zhang [this message]
2022-11-21 11:03   ` [PATCH 2/3] i386: kvm: disable KVM_CAP_PMU_CAPABILITY if "pmu" is disabled Greg Kurz
2022-11-21 14:23     ` Liang Yan
2022-11-21 21:11       ` Dongli Zhang
2023-11-13 16:39   ` Denis V. Lunev
2022-11-19 12:29 ` [PATCH 3/3] target/i386/kvm: get and put AMD pmu registers Dongli Zhang
2022-11-21 14:28   ` Liang Yan
2022-11-21 21:33     ` Dongli Zhang
2022-11-21  6:42 ` [PATCH 0/3] kvm: fix two svm pmu virtualization bugs Like Xu
2022-11-21  7:38   ` Dongli Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221119122901.2469-3-dongli.zhang@oracle.com \
    --to=dongli.zhang@oracle.com \
    --cc=aleksandar.rikalo@syrmia.com \
    --cc=alistair.francis@wdc.com \
    --cc=aurelien@aurel32.net \
    --cc=bin.meng@windriver.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=chenhuacai@kernel.org \
    --cc=clg@kaod.org \
    --cc=danielhb413@gmail.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=david@redhat.com \
    --cc=groug@kaod.org \
    --cc=iii@linux.ibm.com \
    --cc=jiaxun.yang@flygoat.com \
    --cc=joe.jin@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=likexu@tencent.com \
    --cc=mtosatti@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=pasic@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.