Skip to contentSkip to conversation

target/i386/cpu: disable PERFCORE for AMD when cpu.pmu is off

PATCH In review Liang Yan 2 participants 3 messages Oct 28, 2022 – Nov 1, 2022

lore
Share & tools
lore

Liang Yan

author
#0 lore raw

PATCH 1/1 target/i386/cpu: disable PERFCORE for AMD when cpu.pmu is off

With cpu.pmu=off, perfctr_core could still be seen in an AMD guest cpuid. By further digging, I found cpu.perfctr_core did the trick. However, considering the 'enable_pmu' in KVM could work on both Intel and AMD, we may add AMD PMU control under 'enabe_pmu' in QEMU too.

This change will overide the property 'perfctr_ctr' and change the AMD PMU to off by default.

---
 target/i386/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target/i386/cpu.c +4 -0
index 22b681ca37..edf5413c90 100644
@@ -5706,6 +5706,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
                 *ecx |= 1 << 1;    /* CmpLegacy bit */
             }
         }
+
+        if (!cpu->enable_pmu) {
+            *ecx &= ~CPUID_EXT3_PERFCORE;
+        }
         break;
     case 0x80000002:
     case 0x80000003:
Signature · 1 line
2.34.1
#1 lore raw
Quoted 18 lines · Liang Yan

With cpu.pmu=off, perfctr_core could still be seen in an AMD guest cpuid. By further digging, I found cpu.perfctr_core did the trick. However, considering the 'enable_pmu' in KVM could work on both Intel and AMD, we may add AMD PMU control under 'enabe_pmu' in QEMU too.

This change will overide the property 'perfctr_ctr' and change the AMD PMU to off by default.

---
 target/i386/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target/i386/cpu.c +0 -0
index 22b681ca37..edf5413c90 100644

target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 5715

Quoted 10 lines
target/i386/cpu.c +4 -0
@@ -5706,6 +5706,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
                 *ecx |= 1 << 1;    /* CmpLegacy bit */
             }
         }
+
+        if (!cpu->enable_pmu) {
+            *ecx &= ~CPUID_EXT3_PERFCORE;
+        }
         break;
     case 0x80000002:
     case 0x80000003:

I may be missing something but my first impression is that this will make CPUID_EXT3_PERFCORE bit disappear when a !enable_pmu VM is migrated from an old QEMU (pre-patch) to a new one. If so, then additional precautions should be taking against that (e.g. tying the change to CPU/machine model versions, for example).

Signature · 1 line
Vitaly
author
#2 lore raw

Hey Vitaly,

Quoted 27 lines · Vitaly Kuznetsov
Quoted 25 lines · Liang Yan

With cpu.pmu=off, perfctr_core could still be seen in an AMD guest cpuid. By further digging, I found cpu.perfctr_core did the trick. However, considering the 'enable_pmu' in KVM could work on both Intel and AMD, we may add AMD PMU control under 'enabe_pmu' in QEMU too.

This change will overide the property 'perfctr_ctr' and change the AMD PMU to off by default.

---
  target/i386/cpu.c | 4 ++++
  1 file changed, 4 insertions(+)
target/i386/cpu.c +4 -0
index 22b681ca37..edf5413c90 100644
@@ -5706,6 +5706,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
                  *ecx |= 1 << 1;    /* CmpLegacy bit */
              }
          }
+
+        if (!cpu->enable_pmu) {
+            *ecx &= ~CPUID_EXT3_PERFCORE;
+        }
Quoted 9 lines

target/i386/cpu.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 5715

Quoted 3 lines
target/i386/cpu.c +0 -0
          break;
      case 0x80000002:
      case 0x80000003:

I may be missing something but my first impression is that this will make CPUID_EXT3_PERFCORE bit disappear when a !enable_pmu VM is migrated from an old QEMU (pre-patch) to a new one. If so, then additional precautions should be taking against that (e.g. tying the change to CPU/machine model versions, for example).

Thanks for the reply, it is a quite good point. I was struggled with it a little bit earlier because cpu.pmu has such operation for Intel CPU. After further talk with AMD people, I noticed that AMD PMU is more than perfctr_core, it has some legacy counters in use. I will dig a little further and send a v2 with extra cpu counters and live migration compatibility.

Regards,

Liang