[PATCH 01/15] perf/x86/intel: Guard leader sibling walk on nr_siblings
From: Dapeng Mi
Date: Mon Sep 28 2026 - 03:51:39 EST
The leader event's ctx mutex is only held before calling
pmu->event_init() for sibling events, not for a newly-created group
leader.
Some paths in intel_pmu_hw_config() call for_each_sibling_event() on the
leader itself. At that point, leader->ctx is not yet initialized, so the
lockdep assertion in for_each_sibling_event() can trigger WARN_ON_ONCE(),
and the access to leader->ctx is unsafe.
This loop is unnecessary when the event is the group leader, because the
leader has no siblings yet. Guard the iteration with leader->nr_siblings
before calling for_each_sibling_event().
Also move setting PERF_X86_EVENT_BRANCH_COUNTERS to the end of the
branch-counter validation path so the flag is set only after all checks
succeed.
Fixes: 33744916196b ("perf/x86/intel: Support branch counters logging")
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
arch/x86/events/intel/core.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 0a34d674df59..65815d13ae3f 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -5046,11 +5046,12 @@ static int intel_pmu_hw_config(struct perf_event *event)
leader = event->group_leader;
if (intel_set_branch_counter_constr(leader, &num))
return -EINVAL;
- leader->hw.flags |= PERF_X86_EVENT_BRANCH_COUNTERS;
- for_each_sibling_event(sibling, leader) {
- if (intel_set_branch_counter_constr(sibling, &num))
- return -EINVAL;
+ if (leader->nr_siblings) {
+ for_each_sibling_event(sibling, leader) {
+ if (intel_set_branch_counter_constr(sibling, &num))
+ return -EINVAL;
+ }
}
/* event isn't installed as a sibling yet. */
@@ -5069,7 +5070,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
if (0 == (event->attr.branch_sample_type &
~(PERF_SAMPLE_BRANCH_PLM_ALL |
PERF_SAMPLE_BRANCH_COUNTERS)))
- event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+ event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK;
/*
* Force the leader to be a LBR event. So LBRs can be reset
@@ -5077,6 +5078,8 @@ static int intel_pmu_hw_config(struct perf_event *event)
*/
if (!intel_pmu_needs_branch_stack(leader))
return -EINVAL;
+
+ leader->hw.flags |= PERF_X86_EVENT_BRANCH_COUNTERS;
}
if (intel_pmu_needs_branch_stack(event)) {
--
2.34.1