Skip to contentSkip to conversation

KVM: x86: Intel LBR related perf cleanups.

PATCH Superseded Sean Christopherson 2 participants 7 messages Aug 31, 2022

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v3 cover KVM: x86: Intel LBR related perf cleanups.

WAS: Bug fixes and cleanups related to KVM's handling of PERF_CAPABILITIES.

Fix a bug where KVM incorrectly advertises PMU_CAP_LBR_FMT to userspace if perf has disabled LBRs, e.g. because probing one or more LBR MSRs during setup hit a #GP.

The non-KVM patches remove unnecessary stubs and unreachable error paths, which allows for a cleaner fix for said bug.

v3:
- Drop patches for bug #1 (already merged).
- Drop misguided "clean up the capability check" patch. [Like]

v2:
- https://lore.kernel.org/all/[email protected]
- Add patches to fix bug #2. [Like]
- Add a patch to clean up the capability check.
- Tweak the changelog for the PMU refresh bug fix to call out that KVM should disallow changing feature MSRs after KVM_RUN. [Like]

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (3):
perf/x86/core: Remove unnecessary stubs provided for KVM-only helpers perf/x86/core: Drop the unnecessary return value from

    x86_perf_get_lbr()

KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs

 arch/x86/events/intel/lbr.c       |  6 +---
 arch/x86/include/asm/perf_event.h | 55 ++++++++-----------------------
 arch/x86/kvm/vmx/capabilities.h   |  5 ++-
 3 files changed, 19 insertions(+), 47 deletions(-)

base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2

Signature · 1 line
2.37.2.672.g94769d06f0-goog
author maintainer
#1 lore raw

PATCH v3 1/3 perf/x86/core: Remove unnecessary stubs provided for KVM-only helpers

Remove CONFIG_PERF_EVENT=n stubs for functions that are effectively KVM-only. KVM selects PERF_EVENT and will never consume the stubs. Dropping the unnecessary stubs will allow simplifying x86_perf_get_lbr() by getting rid of the impossible-to-hit error path (which KVM doesn't even check).

Opportunstically reorganize the declarations to collapse multiple CONFIG_PERF_EVENTS #ifdefs.

---
 arch/x86/include/asm/perf_event.h | 53 ++++++++-----------------------
 1 file changed, 13 insertions(+), 40 deletions(-)
arch/x86/include/asm/perf_event.h +13 -40
index f6fc8dd51ef4..f839eb55f298 100644
@@ -525,46 +525,18 @@ extern u64 perf_get_hw_event_config(int hw_event);
 extern void perf_check_microcode(void);
 extern void perf_clear_dirty_counters(void);
 extern int x86_perf_rdpmc_index(struct perf_event *event);
-#else
-static inline void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
-{
-	memset(cap, 0, sizeof(*cap));
-}
 
-static inline u64 perf_get_hw_event_config(int hw_event)
-{
-	return 0;
-}
-
-static inline void perf_events_lapic_init(void)	{ }
-static inline void perf_check_microcode(void) { }
-#endif
-
-#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
+#ifdef CONFIG_CPU_SUP_INTEL
 extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data);
 extern int x86_perf_get_lbr(struct x86_pmu_lbr *lbr);
-#else
-struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data);
-static inline int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
-{
-	return -1;
-}
-#endif
+extern void intel_pt_handle_vmx(int on);
+#endif /* CONFIG_CPU_SUP_INTEL */
 
-#ifdef CONFIG_CPU_SUP_INTEL
- extern void intel_pt_handle_vmx(int on);
-#else
-static inline void intel_pt_handle_vmx(int on)
-{
+#ifdef CONFIG_CPU_SUP_AMD
+extern void amd_pmu_enable_virt(void);
+extern void amd_pmu_disable_virt(void);
 
-}
-#endif
-
-#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
- extern void amd_pmu_enable_virt(void);
- extern void amd_pmu_disable_virt(void);
-
-#if defined(CONFIG_PERF_EVENTS_AMD_BRS)
+#ifdef CONFIG_PERF_EVENTS_AMD_BRS
 
 #define PERF_NEEDS_LOPWR_CB 1
 
@@ -582,12 +554,13 @@ static inline void perf_lopwr_cb(bool lopwr_in)
 	static_call_mod(perf_lopwr_cb)(lopwr_in);
 }
 
-#endif /* PERF_NEEDS_LOPWR_CB */
+#endif /* CONFIG_PERF_EVENTS_AMD_BRS */
+#endif /* CONFIG_CPU_SUP_AMD */
 
-#else
- static inline void amd_pmu_enable_virt(void) { }
- static inline void amd_pmu_disable_virt(void) { }
-#endif
+#else  /* !CONFIG_PERF_EVENTS */
+static inline void perf_events_lapic_init(void)	{ }
+static inline void perf_check_microcode(void) { }
+#endif /* CONFIG_PERF_EVENTS */
 
 #define arch_perf_out_copy_user copy_from_user_nmi
 
Signature · 1 line
2.37.2.672.g94769d06f0-goog
author maintainer
#2 lore raw

PATCH v3 2/3 perf/x86/core: Drop the unnecessary return value from x86_perf_get_lbr()

In reply to Sean Christopherson

Drop the return value from x86_perf_get_lbr() now that there's no stub, i.e. now that success is guaranteed (which is a bit of a lie since success was always guaranteed, it's just more obvious now).

---
 arch/x86/events/intel/lbr.c       | 6 +-----
 arch/x86/include/asm/perf_event.h | 2 +-
 2 files changed, 2 insertions(+), 6 deletions(-)
arch/x86/events/intel/lbr.c +1 -5
index 4f70fb6c2c1e..b8ad31c52cf0 100644
@@ -1868,10 +1868,8 @@ void __init intel_pmu_arch_lbr_init(void)
  * x86_perf_get_lbr - get the LBR records information
  *
  * @lbr: the caller's memory to store the LBR records information
- *
- * Returns: 0 indicates the LBR info has been successfully obtained
  */
-int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
+void x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
 {
 	int lbr_fmt = x86_pmu.intel_cap.lbr_format;
 
@@ -1879,8 +1877,6 @@ int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
 	lbr->from = x86_pmu.lbr_from;
 	lbr->to = x86_pmu.lbr_to;
 	lbr->info = (lbr_fmt == LBR_FORMAT_INFO) ? x86_pmu.lbr_info : 0;
-
-	return 0;
 }
 EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
 
arch/x86/include/asm/perf_event.h +1 -1
index f839eb55f298..f6d9230cdfab 100644
@@ -528,7 +528,7 @@ extern int x86_perf_rdpmc_index(struct perf_event *event);
 
 #ifdef CONFIG_CPU_SUP_INTEL
 extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data);
-extern int x86_perf_get_lbr(struct x86_pmu_lbr *lbr);
+extern void x86_perf_get_lbr(struct x86_pmu_lbr *lbr);
 extern void intel_pt_handle_vmx(int on);
 #endif /* CONFIG_CPU_SUP_INTEL */
 
Signature · 1 line
2.37.2.672.g94769d06f0-goog
author maintainer
#3 lore raw

PATCH v3 3/3 KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs

In reply to Sean Christopherson

Advertise LBR support to userspace via MSR_IA32_PERF_CAPABILITIES if and only if perf fully supports LBRs. Perf may disable LBRs (by zeroing the number of LBRs) even on platforms the allegedly support LBRs, e.g. if probing any LBR MSRs during setup fails.

Fixes be635e34c284 ("KVM: vmx/pmu: Expose LBR_FMT in the MSR_IA32_PERF_CAPABILITIES")
---
 arch/x86/kvm/vmx/capabilities.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/capabilities.h +4 -1
index c5e5dfef69c7..d2fdaf888d33 100644
@@ -404,6 +404,7 @@ static inline bool vmx_pebs_supported(void)
 static inline u64 vmx_get_perf_capabilities(void)
 {
 	u64 perf_cap = PMU_CAP_FW_WRITES;
+	struct x86_pmu_lbr lbr;
 	u64 host_perf_cap = 0;
 
 	if (!enable_pmu)
@@ -412,7 +413,9 @@ static inline u64 vmx_get_perf_capabilities(void)
 	if (boot_cpu_has(X86_FEATURE_PDCM))
 		rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
 
-	perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
+	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;
Signature · 1 line
2.37.2.672.g94769d06f0-goog
#4 lore raw

Re: [PATCH v3 3/3] KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs

I love your patch! Yet something to improve:

Hi Sean,

I love your patch! Yet something to improve:

[auto build test ERROR on 372d07084593dc7a399bf9bee815711b1fb1bcf2]

url:    https://github.com/intel-lab-lkp/linux/commits/Sean-Christopherson/KVM-x86-Intel-LBR-related-perf-cleanups/20220831-080309
base:   372d07084593dc7a399bf9bee815711b1fb1bcf2
config: x86_64-randconfig-a012 (https://download.01.org/0day-ci/archive/20220831/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/094f42374997562fff3f9f9637ec9aa8257490a0
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Sean-Christopherson/KVM-x86-Intel-LBR-related-perf-cleanups/20220831-080309 git checkout 094f42374997562fff3f9f9637ec9aa8257490a0
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 prepare

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

In file included from arch/x86/kernel/asm-offsets.c:22:
In file included from arch/x86/kernel/../kvm/vmx/vmx.h:11:

>> arch/x86/kernel/../kvm/vmx/capabilities.h:416:2: error: implicit declaration of function 'x86_perf_get_lbr' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           x86_perf_get_lbr(&lbr);
           ^

1 error generated.
make[2]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1207: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:222: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/x86_perf_get_lbr +416 arch/x86/kernel/../kvm/vmx/capabilities.h

403
404 static inline u64 vmx_get_perf_capabilities(void)
405 {
406 u64 perf_cap = PMU_CAP_FW_WRITES;
407 struct x86_pmu_lbr lbr;
408 u64 host_perf_cap = 0;
409
410 if (!enable_pmu)
411 return 0;
412
413 if (boot_cpu_has(X86_FEATURE_PDCM))
414 rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
415

 > 416		x86_perf_get_lbr(&lbr);

417 if (lbr.nr)
418 perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
419
420 if (vmx_pebs_supported()) {
421 perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;
422 if ((perf_cap & PERF_CAP_PEBS_FORMAT) < 4)
423 perf_cap &= ~PERF_CAP_PEBS_BASELINE;
424 }
425
426 return perf_cap;
427 }
428

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#5 lore raw

Re: [PATCH v3 3/3] KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs

In reply to Sean Christopherson
I love your patch! Yet something to improve:

Hi Sean,

I love your patch! Yet something to improve:

[auto build test ERROR on 372d07084593dc7a399bf9bee815711b1fb1bcf2]

url:    https://github.com/intel-lab-lkp/linux/commits/Sean-Christopherson/KVM-x86-Intel-LBR-related-perf-cleanups/20220831-080309
base:   372d07084593dc7a399bf9bee815711b1fb1bcf2
config: i386-randconfig-s001 (https://download.01.org/0day-ci/archive/20220831/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/094f42374997562fff3f9f9637ec9aa8257490a0
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Sean-Christopherson/KVM-x86-Intel-LBR-related-perf-cleanups/20220831-080309
        git checkout 094f42374997562fff3f9f9637ec9aa8257490a0
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 prepare

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

In file included from arch/x86/kernel/../kvm/vmx/vmx.h:11,

                    from arch/x86/kernel/asm-offsets.c:22:
   arch/x86/kernel/../kvm/vmx/capabilities.h: In function 'vmx_get_perf_capabilities':
>> arch/x86/kernel/../kvm/vmx/capabilities.h:416:9: error: implicit declaration of function 'x86_perf_get_lbr' [-Werror=implicit-function-declaration]
     416 |         x86_perf_get_lbr(&lbr);
         |         ^~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1207: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:222: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/x86_perf_get_lbr +416 arch/x86/kernel/../kvm/vmx/capabilities.h

403
404 static inline u64 vmx_get_perf_capabilities(void)
405 {
406 u64 perf_cap = PMU_CAP_FW_WRITES;
407 struct x86_pmu_lbr lbr;
408 u64 host_perf_cap = 0;
409
410 if (!enable_pmu)
411 return 0;
412
413 if (boot_cpu_has(X86_FEATURE_PDCM))
414 rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
415

 > 416		x86_perf_get_lbr(&lbr);

417 if (lbr.nr)
418 perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
419
420 if (vmx_pebs_supported()) {
421 perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;
422 if ((perf_cap & PERF_CAP_PEBS_FORMAT) < 4)
423 perf_cap &= ~PERF_CAP_PEBS_BASELINE;
424 }
425
426 return perf_cap;
427 }
428

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
author maintainer
#6 lore raw

Re: [PATCH v3 3/3] KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs

Quoted 21 lines · kernel test robot

Hi Sean,

I love your patch! Yet something to improve:

[auto build test ERROR on 372d07084593dc7a399bf9bee815711b1fb1bcf2]

url:    https://github.com/intel-lab-lkp/linux/commits/Sean-Christopherson/KVM-x86-Intel-LBR-related-perf-cleanups/20220831-080309
base:   372d07084593dc7a399bf9bee815711b1fb1bcf2
config: i386-randconfig-s001 (https://download.01.org/0day-ci/archive/20220831/202208311831.zQ4oCG1b-lkp(a)intel.com/config)
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/094f42374997562fff3f9f9637ec9aa8257490a0
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Sean-Christopherson/KVM-x86-Intel-LBR-related-perf-cleanups/20220831-080309
        git checkout 094f42374997562fff3f9f9637ec9aa8257490a0
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 prepare
Quoted 7 lines

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

   In file included from arch/x86/kernel/../kvm/vmx/vmx.h:11,
                    from arch/x86/kernel/asm-offsets.c:22:

Argh, KVM's "internal" vmx.h gets included by arch/x86/kernel/asm-offsets.c even when KVM is disabled, and vmx_get_perf_capabilities() is oddly inlined there.

The simple fix is to move the definition of vmx_get_perf_capabilities() into vmx.c.

Long term, I want to figure out a way to break the dependency on asm-offsets. I ran afoul of this a week ago for something else, but couldn't figure an easy solution and it wasn't (yet) a blocking issue.