diff mbox series

kvm: limit the maximum CPUID.0xA.edx[0..4] to 3

Message ID 20230602073857.96790-1-gaoshiyuan@baidu.com (mailing list archive)
State New, archived
Headers show
Series kvm: limit the maximum CPUID.0xA.edx[0..4] to 3 | expand

Commit Message

Gao Shiyuan June 2, 2023, 7:38 a.m. UTC
Now, the CPUID.0xA depends on the KVM report. The value of CPUID.0xA.edx[0..4]
and num_architectural_pmu_fixed_counters are inconsistent when the host kernel
before this commit 2e8cd7a3b828 ("kvm: x86: limit the maximum number of vPMU
fixed counters to 3") on icelake microarchitecture.

This also break the live-migration between source host kernel before commit
2e8cd7a3b828 and dest host kernel after the commit on icelake microarchitecture.

Signed-off-by: Shiyuan Gao <gaoshiyuan@baidu.com>
---
 target/i386/kvm/kvm.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Gao Shiyuan Jan. 10, 2024, 10:13 a.m. UTC | #1
Anyone has suggestion?

When the host kernel before this commit 2e8cd7a3b828 ("kvm: x86: limit the maximum number of vPMU
fixed counters to 3") on icelake microarchitecture and newer, execute cpuid in the Guest:

Architecture Performance Monitoring Features (0xa/edx):
    number of fixed counters    = 0x4 (4)

This is not inconsistent with num_architectural_pmu_fixed_counters in QEMU.
Zhijian Li (Fujitsu)" via Jan. 10, 2024, 11:26 a.m. UTC | #2
> Anyone has suggestion?
>
> When the host kernel before this commit 2e8cd7a3b828 ("kvm: x86: limit the maximum number of vPMU
> fixed counters to 3") on icelake microarchitecture and newer, execute cpuid in the Guest:
>
> Architecture Performance Monitoring Features (0xa/edx):
>     number of fixed counters    = 0x4 (4)
>
> This is not inconsistent with num_architectural_pmu_fixed_counters in QEMU.

This is the orginal patch.

kvm: limit the maximum CPUID.0xA.edx[0..4] to 3

Now, the CPUID.0xA depends on the KVM report. The value of CPUID.0xA.edx[0..4]
and num_architectural_pmu_fixed_counters are inconsistent when the host kernel
before this commit 2e8cd7a3b828 ("kvm: x86: limit the maximum number of vPMU
fixed counters to 3") on icelake microarchitecture.

This also break the live-migration between source host kernel before commit
2e8cd7a3b828 and dest host kernel after the commit on icelake microarchitecture.

Signed-off-by: Shiyuan Gao <gaoshiyuan@baidu.com>
---
 target/i386/kvm/kvm.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index de531842f6..e77129b737 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -1761,7 +1761,7 @@ int kvm_arch_init_vcpu(CPUState *cs)

     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
-    uint32_t limit, i, j, cpuid_i;
+    uint32_t limit, i, j, cpuid_i, cpuid_0xa;
     uint32_t unused;
     struct kvm_cpuid_entry2 *c;
     uint32_t signature[3];
@@ -1773,6 +1773,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     memset(&cpuid_data, 0, sizeof(cpuid_data));

     cpuid_i = 0;
+    cpuid_0xa = 0;

     has_xsave2 = kvm_check_extension(cs->kvm_state, KVM_CAP_XSAVE2);

@@ -2045,6 +2046,9 @@ int kvm_arch_init_vcpu(CPUState *cs)
             c->function = i;
             c->flags = 0;
             cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
+            if (0x0a == i) {
+                cpuid_0xa = cpuid_i - 1;
+            }
             if (!c->eax && !c->ebx && !c->ecx && !c->edx) {
                 /*
                  * KVM already returns all zeroes if a CPUID entry is missing,
@@ -2059,7 +2063,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
     if (limit >= 0x0a) {
         uint32_t eax, edx;

-        cpu_x86_cpuid(env, 0x0a, 0, &eax, &unused, &unused, &edx);
+        assert(cpuid_0xa >= 0x0a);
+
+        c = &cpuid_data.entries[cpuid_0xa];
+        eax = c->eax;
+        edx = c->edx;

         has_architectural_pmu_version = eax & 0xff;
         if (has_architectural_pmu_version > 0) {
@@ -2078,6 +2086,7 @@ int kvm_arch_init_vcpu(CPUState *cs)

                 if (num_architectural_pmu_fixed_counters > MAX_FIXED_COUNTERS) {
                     num_architectural_pmu_fixed_counters = MAX_FIXED_COUNTERS;
+                    c->edx = (edx & ~0x1f) | num_architectural_pmu_fixed_counters;
                 }
             }
         }
diff mbox series

Patch

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index de531842f6..e77129b737 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -1761,7 +1761,7 @@  int kvm_arch_init_vcpu(CPUState *cs)
 
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
-    uint32_t limit, i, j, cpuid_i;
+    uint32_t limit, i, j, cpuid_i, cpuid_0xa;
     uint32_t unused;
     struct kvm_cpuid_entry2 *c;
     uint32_t signature[3];
@@ -1773,6 +1773,7 @@  int kvm_arch_init_vcpu(CPUState *cs)
     memset(&cpuid_data, 0, sizeof(cpuid_data));
 
     cpuid_i = 0;
+    cpuid_0xa = 0;
 
     has_xsave2 = kvm_check_extension(cs->kvm_state, KVM_CAP_XSAVE2);
 
@@ -2045,6 +2046,9 @@  int kvm_arch_init_vcpu(CPUState *cs)
             c->function = i;
             c->flags = 0;
             cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
+            if (0x0a == i) {
+                cpuid_0xa = cpuid_i - 1;
+            }
             if (!c->eax && !c->ebx && !c->ecx && !c->edx) {
                 /*
                  * KVM already returns all zeroes if a CPUID entry is missing,
@@ -2059,7 +2063,11 @@  int kvm_arch_init_vcpu(CPUState *cs)
     if (limit >= 0x0a) {
         uint32_t eax, edx;
 
-        cpu_x86_cpuid(env, 0x0a, 0, &eax, &unused, &unused, &edx);
+        assert(cpuid_0xa >= 0x0a);
+
+        c = &cpuid_data.entries[cpuid_0xa];
+        eax = c->eax;
+        edx = c->edx;
 
         has_architectural_pmu_version = eax & 0xff;
         if (has_architectural_pmu_version > 0) {
@@ -2078,6 +2086,7 @@  int kvm_arch_init_vcpu(CPUState *cs)
 
                 if (num_architectural_pmu_fixed_counters > MAX_FIXED_COUNTERS) {
                     num_architectural_pmu_fixed_counters = MAX_FIXED_COUNTERS;
+                    c->edx = (edx & ~0x1f) | num_architectural_pmu_fixed_counters;
                 }
             }
         }