[PATCH v2 2/7] perf/x86/core: Remove unnecessary stubs provided for KVM-only helpers

Sean Christopherson posted 7 patches 3 years, 8 months ago
[PATCH v2 2/7] perf/x86/core: Remove unnecessary stubs provided for KVM-only helpers
Posted by Sean Christopherson 3 years, 8 months ago
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.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/include/asm/perf_event.h | 53 ++++++++-----------------------
 1 file changed, 13 insertions(+), 40 deletions(-)

diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index cc47044401ff..aba196172500 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -509,46 +509,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
 
@@ -566,12 +538,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
 
-- 
2.37.1.559.g78731f0fdb-goog
Re: [PATCH v2 2/7] perf/x86/core: Remove unnecessary stubs provided for KVM-only helpers
Posted by Like Xu 3 years, 8 months ago
On 4/8/2022 3:26 am, Sean Christopherson wrote:
> 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()

Giggling, I used to have a similar cleanup patch sitting in a corner somewhere.

> 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.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>   arch/x86/include/asm/perf_event.h | 53 ++++++++-----------------------
>   1 file changed, 13 insertions(+), 40 deletions(-)
> 
> diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
> index cc47044401ff..aba196172500 100644
> --- a/arch/x86/include/asm/perf_event.h
> +++ b/arch/x86/include/asm/perf_event.h
> @@ -509,46 +509,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
>   
> @@ -566,12 +538,13 @@ static inline void perf_lopwr_cb(bool lopwr_in)
>   	static_call_mod(perf_lopwr_cb)(lopwr_in);
>   }
>   
> -#endif /* PERF_NEEDS_LOPWR_CB */

Oops, now the definition of PERF_NEEDS_LOPWR_CB will not be unset.
This is not mentioned in the commit message and may cause trouble.

> +#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
>
Re: [PATCH v2 2/7] perf/x86/core: Remove unnecessary stubs provided for KVM-only helpers
Posted by Sean Christopherson 3 years, 8 months ago
On Thu, Aug 04, 2022, Like Xu wrote:
> On 4/8/2022 3:26 am, Sean Christopherson wrote:
> > -#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
> > @@ -566,12 +538,13 @@ static inline void perf_lopwr_cb(bool lopwr_in)
> >   	static_call_mod(perf_lopwr_cb)(lopwr_in);
> >   }
> > -#endif /* PERF_NEEDS_LOPWR_CB */
> 
> Oops, now the definition of PERF_NEEDS_LOPWR_CB will not be unset.
> This is not mentioned in the commit message and may cause trouble.

PERF_NEEDS_LOPWR_CB isn't being "unset" in the existing code, the comment is simply
wrong.  The #endif pairs with CONFIG_PERF_EVENTS_AMD_BRS.

  #if defined(CONFIG_PERF_EVENTS_AMD_BRS)

  #define PERF_NEEDS_LOPWR_CB 1

  /*
   * architectural low power callback impacts
   * drivers/acpi/processor_idle.c
   * drivers/acpi/acpi_pad.c
   */
  extern void perf_amd_brs_lopwr_cb(bool lopwr_in);

  DECLARE_STATIC_CALL(perf_lopwr_cb, perf_amd_brs_lopwr_cb);

  static inline void perf_lopwr_cb(bool lopwr_in)
  {
  	static_call_mod(perf_lopwr_cb)(lopwr_in);
  }

  #endif /* PERF_NEEDS_LOPWR_CB */ <=== should be /* CONFIG_PERF_EVENTS_AMD_BRS */