Skip to contentSkip to conversation

KVM: x86/pmu: Disallow legacy LBRs if architectural LBRs are available

PATCH Applied Sean Christopherson 2 participants 6 messages Jan 28, 2023 – Apr 6, 2023

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86/pmu: Disallow legacy LBRs if architectural LBRs are available

Disallow enabling LBR support if the CPU supports architectural LBRs. Traditional LBR support is absent on CPU models that have architectural LBRs, and KVM doesn't yet support arch LBRs, i.e. KVM will pass through non-existent MSRs if userspace enables LBRs for the guest.

---

Am I missing something that would prevent this scenario?

 arch/x86/kvm/vmx/vmx.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +5 -3
index 8f0f67c75f35..77ee6b4a5ec4 100644
@@ -7761,9 +7761,11 @@ static u64 vmx_get_perf_capabilities(void)
 	if (boot_cpu_has(X86_FEATURE_PDCM))
 		rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
 
-	x86_perf_get_lbr(&lbr);
-	if (lbr.nr)
-		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {
+		x86_perf_get_lbr(&lbr);
+		if (lbr.nr)
+			perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	}
 
 	if (vmx_pebs_supported()) {
 		perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;

base-commit: 2de154f541fc5b9f2aed3fe06e218130718ce320

Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
Tested-by on v1 1/1
#1 lore raw
Quoted 4 lines · Sean Christopherson

Disallow enabling LBR support if the CPU supports architectural LBRs. Traditional LBR support is absent on CPU models that have architectural LBRs, and KVM doesn't yet support arch LBRs, i.e. KVM will pass through non-existent MSRs if userspace enables LBRs for the guest.

True, we have call_trace due to MSR_ARCH_LBR_FROM_0 (0x1500) for example.

Quoted 4 lines
Quoted 1 line
Fixes 145dfad998ea ("KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs")
Quoted 13 lines

---

Am I missing something that would prevent this scenario?

  arch/x86/kvm/vmx/vmx.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +0 -0
index 8f0f67c75f35..77ee6b4a5ec4 100644

arch/x86/kvm/vmx/vmx.c static u64 vmx_get_perf_capabilities(void) · line 7764

Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +1 -3
@@ -7761,9 +7761,11 @@ static u64 vmx_get_perf_capabilities(void)
  	if (boot_cpu_has(X86_FEATURE_PDCM))
  		rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
 
-	x86_perf_get_lbr(&lbr);
-	if (lbr.nr)
-		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {

To avoid changing this again in the Arch lbr enabling part, how about:

	x86_perf_get_lbr(&lbr);
	if (lbr.nr && cpu_feature_enabled(X86_FEATURE_ARCH_LBR) ==
	    kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR))
		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;

?

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +4 -0
+		x86_perf_get_lbr(&lbr);
+		if (lbr.nr)
+			perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	}
 
  	if (vmx_pebs_supported()) {
  		perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;

base-commit: 2de154f541fc5b9f2aed3fe06e218130718ce320

author maintainer
#2 lore raw
Quoted 13 lines · Like Xu
Quoted 4 lines · Sean Christopherson

Disallow enabling LBR support if the CPU supports architectural LBRs. Traditional LBR support is absent on CPU models that have architectural LBRs, and KVM doesn't yet support arch LBRs, i.e. KVM will pass through non-existent MSRs if userspace enables LBRs for the guest.

True, we have call_trace due to MSR_ARCH_LBR_FROM_0 (0x1500) for example.

Quoted 4 lines
Quoted 6 lines
Quoted 1 line
Fixes 145dfad998ea ("KVM: VMX: Advertise PMU LBRs if and only if perf

supports LBRs")

If we want a fixes, I'd argue this is more appropriate:

Fixes be635e34c284 ("KVM: vmx/pmu: Expose LBR_FMT in the MSR_IA32_PERF_CAPABILITIES")

Though I'd prefer not to blame KVM, there's not much we could have done in KVM to know that Intel would effectively break backwards compatibility.

Quoted 22 lines
Quoted 13 lines

---

Am I missing something that would prevent this scenario?

  arch/x86/kvm/vmx/vmx.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +0 -0
index 8f0f67c75f35..77ee6b4a5ec4 100644

arch/x86/kvm/vmx/vmx.c static u64 vmx_get_perf_capabilities(void) · line 7763

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1 -3
@@ -7761,9 +7761,11 @@ static u64 vmx_get_perf_capabilities(void)
  	if (boot_cpu_has(X86_FEATURE_PDCM))
  		rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
-	x86_perf_get_lbr(&lbr);
-	if (lbr.nr)
-		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {

To avoid changing this again in the Arch lbr enabling part, how about:

Quoted 6 lines
	x86_perf_get_lbr(&lbr);
	if (lbr.nr && cpu_feature_enabled(X86_FEATURE_ARCH_LBR) ==
	    kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR))
		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;

?

I'd rather force arch LBR enabling to explicitly update this code. And I'd prefer that KVM explicitly clear PMU_CAP_LBR_FMT when KVM can't use arch LBRs for whatever reason, both for documentation purposes and to avoid ordering dependencies between consuming vmx_get_perf_capabilities() and updating kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR).

#3 lore raw
Quoted 21 lines · Sean Christopherson
Quoted 13 lines · Like Xu
Quoted 4 lines · Sean Christopherson

Disallow enabling LBR support if the CPU supports architectural LBRs. Traditional LBR support is absent on CPU models that have architectural LBRs, and KVM doesn't yet support arch LBRs, i.e. KVM will pass through non-existent MSRs if userspace enables LBRs for the guest.

True, we have call_trace due to MSR_ARCH_LBR_FROM_0 (0x1500) for example.

Quoted 4 lines
Quoted 6 lines
Quoted 1 line
Fixes 145dfad998ea ("KVM: VMX: Advertise PMU LBRs if and only if perf

supports LBRs")

Quoted 6 lines

If we want a fixes, I'd argue this is more appropriate:

   Fixes: be635e34c284 ("KVM: vmx/pmu: Expose LBR_FMT in the MSR_IA32_PERF_CAPABILITIES")

Though I'd prefer not to blame KVM, there's not much we could have done in KVM to know that Intel would effectively break backwards compatibility.

Personally, I assume the bigger role of the Fix tag is to help the stable tree's bots make it easier to back port patches automatically, and there will be less sense of blame for the developers. In pmu scope, if a feature is not "architecture", I'm not surprised that a new arrival will break compatibility, and sometimes kernel developers need to plan ahead.

Quoted 28 lines
Quoted 28 lines
Quoted 14 lines

---

Am I missing something that would prevent this scenario?

   arch/x86/kvm/vmx/vmx.c | 8 +++++---
   1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +0 -0
index 8f0f67c75f35..77ee6b4a5ec4 100644

arch/x86/kvm/vmx/vmx.c static u64 vmx_get_perf_capabilities(void) · line 7763

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1 -3
@@ -7761,9 +7761,11 @@ static u64 vmx_get_perf_capabilities(void)
   	if (boot_cpu_has(X86_FEATURE_PDCM))
   		rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
-	x86_perf_get_lbr(&lbr);
-	if (lbr.nr)
-		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {

To avoid changing this again in the Arch lbr enabling part, how about:

	x86_perf_get_lbr(&lbr);
	if (lbr.nr && cpu_feature_enabled(X86_FEATURE_ARCH_LBR) ==
	    kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR))
		perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
Quoted 6 lines
Quoted 1 line

?

I'd rather force arch LBR enabling to explicitly update this code. And I'd prefer that KVM explicitly clear PMU_CAP_LBR_FMT when KVM can't use arch LBRs for whatever reason, both for documentation purposes and to avoid ordering dependencies between consuming vmx_get_perf_capabilities() and updating kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR).

Indeed, we have too many assumptions about the order of function calls in the kernel world. "Avoid ordering dependencies" looks good to me. Thanks.

author maintainer
#4 lore raw
Quoted 24 lines · Like Xu
Quoted 23 lines · Sean Christopherson
Quoted 13 lines · Like Xu
Quoted 4 lines · Sean Christopherson

Disallow enabling LBR support if the CPU supports architectural LBRs. Traditional LBR support is absent on CPU models that have architectural LBRs, and KVM doesn't yet support arch LBRs, i.e. KVM will pass through non-existent MSRs if userspace enables LBRs for the guest.

True, we have call_trace due to MSR_ARCH_LBR_FROM_0 (0x1500) for example.

Quoted 4 lines
Quoted 6 lines
Quoted 1 line
Fixes 145dfad998ea ("KVM: VMX: Advertise PMU LBRs if and only if perf

supports LBRs")

If we want a fixes, I'd argue this is more appropriate:

Quoted 8 lines
Quoted 4 lines
   Fixes: be635e34c284 ("KVM: vmx/pmu: Expose LBR_FMT in the MSR_IA32_PERF_CAPABILITIES")

Though I'd prefer not to blame KVM, there's not much we could have done in KVM to know that Intel would effectively break backwards compatibility.

Personally, I assume the bigger role of the Fix tag is to help the stable tree's bots make it easier to back port patches automatically, and there will be less sense of blame for the developers.

I don't mind adding a Fixes to aid stable, but then

Fixes be635e34c284 ("KVM: vmx/pmu: Expose LBR_FMT in the MSR_IA32_PERF_CAPABILITIES")

is still more correct, e.g. if there are kernel's that didn't get 145dfad998ea ("KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs") for whatever reason.

Quoted 3 lines

In pmu scope, if a feature is not "architecture", I'm not surprised that a new arrival will break compatibility, and sometimes kernel developers need to plan ahead.

Hrm, true, compatibility is usually a non-goal for uarch stuff. I'll try to keep that in mind for future vPMU code.

Thanks!

author maintainer Applied
#5 lore raw
In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson

Disallow enabling LBR support if the CPU supports architectural LBRs. Traditional LBR support is absent on CPU models that have architectural LBRs, and KVM doesn't yet support arch LBRs, i.e. KVM will pass through non-existent MSRs if userspace enables LBRs for the guest.

Applied to kvm-x86 pmu, with a Fixes tag.

[1/1] KVM: x86/pmu: Disallow legacy LBRs if architectural LBRs are available

      https://github.com/kvm-x86/linux/commit/098f4c061ea1

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes