Re: [PATCH v1 1/2] perf/x86: Avoid inadvertent casts to x86_hybrid_pmu
From: Peter Zijlstra
Date: Thu Mar 12 2026 - 04:32:12 EST
On Wed, Mar 11, 2026 at 10:48:09PM -0700, Ian Rogers wrote:
> The patch:
> https://lore.kernel.org/lkml/20260311075201.2951073-2-dapeng1.mi@xxxxxxxxxxxxxxx/
> showed it was pretty easy to accidentally cast non-x86 PMUs to
> x86_hybrid_pmus. Add a BUG_ON for that case. Restructure is_x86_event
> and add an is_x86_pmu to facilitate this.
>
> @@ -779,6 +795,7 @@ struct x86_hybrid_pmu {
>
> static __always_inline struct x86_hybrid_pmu *hybrid_pmu(struct pmu *pmu)
> {
> + BUG_ON(!is_x86_pmu(pmu));
> return container_of(pmu, struct x86_hybrid_pmu, pmu);
> }
Given that hybrid_pmu will have PERF_PMU_CAP_EXTENDED_HW_TYPE, and we
should really only use hyrid_pmu() on one of those, would not the
simpler patch be so?
diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
index fad87d3c8b2c..13ec623617a9 100644
--- a/arch/x86/events/perf_event.h
+++ b/arch/x86/events/perf_event.h
@@ -779,6 +779,7 @@ struct x86_hybrid_pmu {
static __always_inline struct x86_hybrid_pmu *hybrid_pmu(struct pmu *pmu)
{
+ BUG_ON(!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE));
return container_of(pmu, struct x86_hybrid_pmu, pmu);
}