Skip to content

Commit 6aa6527

Browse files
Dapeng Migregkh
authored andcommitted
perf/x86/intel: Add missing branch counters constraint apply
commit 1d07bbd upstream. When running the command: 'perf record -e "{instructions,instructions:p}" -j any,counter sleep 1', a "shift-out-of-bounds" warning is reported on CWF. UBSAN: shift-out-of-bounds in /kbuild/src/consumer/arch/x86/events/intel/lbr.c:970:15 shift exponent 64 is too large for 64-bit type 'long long unsigned int' ...... intel_pmu_lbr_counters_reorder.isra.0.cold+0x2a/0xa7 intel_pmu_lbr_save_brstack+0xc0/0x4c0 setup_arch_pebs_sample_data+0x114b/0x2400 The warning occurs because the second "instructions:p" event, which involves branch counters sampling, is incorrectly programmed to fixed counter 0 instead of the general-purpose (GP) counters 0-3 that support branch counters sampling. Currently only GP counters 0-3 support branch counters sampling on CWF, any event involving branch counters sampling should be programed on GP counters 0-3. Since the counter index of fixed counter 0 is 32, it leads to the "src" value in below code is right shifted 64 bits and trigger the "shift-out-of-bounds" warning. cnt = (src >> (order[j] * LBR_INFO_BR_CNTR_BITS)) & LBR_INFO_BR_CNTR_MASK; The root cause is the loss of the branch counters constraint for the new event in the branch counters sampling event group. Since it isn't yet part of the sibling list. This results in the second "instructions:p" event being programmed on fixed counter 0 incorrectly instead of the appropriate GP counters 0-3. To address this, we apply the missing branch counters constraint for the last event in the group. Additionally, we introduce a new function, `intel_set_branch_counter_constr()`, to apply the branch counters constraint and avoid code duplication. Fixes: 3374491 ("perf/x86/intel: Support branch counters logging") Reported-by: Xudong Hao <xudong.hao@intel.com> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://patch.msgid.link/20260228053320.140406-2-dapeng1.mi@linux.intel.com Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bdfc097 commit 6aa6527

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

arch/x86/events/intel/core.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,19 @@ static inline void intel_pmu_set_acr_caused_constr(struct perf_event *event,
43674367
event->hw.dyn_constraint &= hybrid(event->pmu, acr_cause_mask64);
43684368
}
43694369

4370+
static inline int intel_set_branch_counter_constr(struct perf_event *event,
4371+
int *num)
4372+
{
4373+
if (branch_sample_call_stack(event))
4374+
return -EINVAL;
4375+
if (branch_sample_counters(event)) {
4376+
(*num)++;
4377+
event->hw.dyn_constraint &= x86_pmu.lbr_counters;
4378+
}
4379+
4380+
return 0;
4381+
}
4382+
43704383
static int intel_pmu_hw_config(struct perf_event *event)
43714384
{
43724385
int ret = x86_pmu_hw_config(event);
@@ -4437,21 +4450,19 @@ static int intel_pmu_hw_config(struct perf_event *event)
44374450
* group, which requires the extra space to store the counters.
44384451
*/
44394452
leader = event->group_leader;
4440-
if (branch_sample_call_stack(leader))
4453+
if (intel_set_branch_counter_constr(leader, &num))
44414454
return -EINVAL;
4442-
if (branch_sample_counters(leader)) {
4443-
num++;
4444-
leader->hw.dyn_constraint &= x86_pmu.lbr_counters;
4445-
}
44464455
leader->hw.flags |= PERF_X86_EVENT_BRANCH_COUNTERS;
44474456

44484457
for_each_sibling_event(sibling, leader) {
4449-
if (branch_sample_call_stack(sibling))
4458+
if (intel_set_branch_counter_constr(sibling, &num))
4459+
return -EINVAL;
4460+
}
4461+
4462+
/* event isn't installed as a sibling yet. */
4463+
if (event != leader) {
4464+
if (intel_set_branch_counter_constr(event, &num))
44504465
return -EINVAL;
4451-
if (branch_sample_counters(sibling)) {
4452-
num++;
4453-
sibling->hw.dyn_constraint &= x86_pmu.lbr_counters;
4454-
}
44554466
}
44564467

44574468
if (num > fls(x86_pmu.lbr_counters))

0 commit comments

Comments
 (0)