[Patch v8 02/23] perf/x86: Move hybrid PMU initialization before x86_pmu_starting_cpu()

From: Dapeng Mi

Date: Fri May 29 2026 - 04:04:07 EST


The current approach initializes hybrid PMU structures immediately before
registering them. This is risky as it can lead to key fields, such as
'capabilities', being inadvertently overwritten.

Although no issues have arisen so far, this method is not ideal. It makes
the PMU structure fields susceptible to being overwritten, especially with
future changes that might initialize fields like 'capabilities' within
init_hybrid_pmu() called by x86_pmu_starting_cpu().

To mitigate this potential problem, move the default hybrid structure
initialization before calling x86_pmu_starting_cpu().

Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
arch/x86/events/core.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 4b9e105309c6..17e122e27e0b 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2195,8 +2195,20 @@ static int __init init_hw_perf_events(void)

pmu.attr_update = x86_pmu.attr_update;

- if (!is_hybrid())
+ if (!is_hybrid()) {
x86_pmu_show_pmu_cap(NULL);
+ } else {
+ int i;
+
+ /*
+ * Init default ops.
+ * Must be called before registering x86_pmu_starting_cpu(),
+ * otherwise some key PMU fields, e.g., capabilities
+ * initialized in x86_pmu_starting_cpu(), would be overwritten.
+ */
+ for (i = 0; i < x86_pmu.num_hybrid_pmus; i++)
+ x86_pmu.hybrid_pmu[i].pmu = pmu;
+ }

if (!x86_pmu.read)
x86_pmu.read = _x86_pmu_read;
@@ -2243,7 +2255,6 @@ static int __init init_hw_perf_events(void)
for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) {
hybrid_pmu = &x86_pmu.hybrid_pmu[i];

- hybrid_pmu->pmu = pmu;
hybrid_pmu->pmu.type = -1;
hybrid_pmu->pmu.attr_update = x86_pmu.attr_update;
hybrid_pmu->pmu.capabilities |= PERF_PMU_CAP_EXTENDED_HW_TYPE;
--
2.34.1