[PATCH v1 2/2] perf/x86: Reduce is_hybrid calls and aid ellision of BUG_ON in hybrid_pmu

From: Ian Rogers

Date: Thu Mar 12 2026 - 01:49:02 EST


Use the capabilities of the PMU rather than the global variable
perf_is_hybrid to determine if a hybrid pmu has been passed to the
main accessors. As the pmu capabilities check mirrors that in
is_x86_pmu, the BUG_ON(!is_x86_pmu...) in hybrid_pmu can be elided as
it is provably always false (with sufficient function inlining, common
sub-expression elimination, etc.) in its most common uses.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
Only build tested.
---
arch/x86/events/perf_event.h | 52 +++++++++++++++++++-----------------
1 file changed, 28 insertions(+), 24 deletions(-)

diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
index f1123c95d174..7990d86ef233 100644
--- a/arch/x86/events/perf_event.h
+++ b/arch/x86/events/perf_event.h
@@ -802,34 +802,38 @@ static __always_inline struct x86_hybrid_pmu *hybrid_pmu(struct pmu *pmu)
extern struct static_key_false perf_is_hybrid;
#define is_hybrid() static_branch_unlikely(&perf_is_hybrid)

-#define hybrid(_pmu, _field) \
-(*({ \
- typeof(&x86_pmu._field) __Fp = &x86_pmu._field; \
- \
- if (is_hybrid() && (_pmu)) \
- __Fp = &hybrid_pmu(_pmu)->_field; \
- \
- __Fp; \
+
+#define hybrid(_pmu, _field) \
+(*({ \
+ typeof(&x86_pmu._field) __Fp = &x86_pmu._field; \
+ struct pmu *__pmu = _pmu; \
+ \
+ if (__pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE) \
+ __Fp = &hybrid_pmu(__pmu)->_field; \
+ \
+ __Fp; \
}))

-#define hybrid_var(_pmu, _var) \
-(*({ \
- typeof(&_var) __Fp = &_var; \
- \
- if (is_hybrid() && (_pmu)) \
- __Fp = &hybrid_pmu(_pmu)->_var; \
- \
- __Fp; \
+#define hybrid_var(_pmu, _var) \
+(*({ \
+ typeof(&_var) __Fp = &_var; \
+ struct pmu *__pmu = _pmu; \
+ \
+ if (__pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE) \
+ __Fp = &hybrid_pmu(__pmu)->_var; \
+ \
+ __Fp; \
}))

-#define hybrid_bit(_pmu, _field) \
-({ \
- bool __Fp = x86_pmu._field; \
- \
- if (is_hybrid() && (_pmu)) \
- __Fp = hybrid_pmu(_pmu)->_field; \
- \
- __Fp; \
+#define hybrid_bit(_pmu, _field) \
+({ \
+ bool __Fp = x86_pmu._field; \
+ struct pmu *__pmu = _pmu; \
+ \
+ if (__pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE) \
+ __Fp = hybrid_pmu(__pmu)->_field; \
+ \
+ __Fp; \
})

/*
--
2.53.0.851.ga537e3e6e9-goog