Skip to content

Commit ef493f4

Browse files
Kan LiangPeter Zijlstra
authored andcommitted
perf/x86/intel: Allow to setup LBR for counting event for BPF
The BPF subsystem may capture LBR data on a counting event. However, the current implementation assumes that LBR can/should only be used with sampling events. For instance, retsnoop tool ([0]) makes an extensive use of this functionality and sets up perf event as follows: struct perf_event_attr attr; memset(&attr, 0, sizeof(attr)); attr.size = sizeof(attr); attr.type = PERF_TYPE_HARDWARE; attr.config = PERF_COUNT_HW_CPU_CYCLES; attr.sample_type = PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_KERNEL; To limit the LBR for a sampling event is to avoid unnecessary branch stack setup for a counting event in the sample read. Because LBR is only read in the sampling event's overflow. Although in most cases LBR is used in sampling, there is no HW limit to bind LBR to the sampling mode. Allow an LBR setup for a counting event unless in the sample read mode. Fixes: 85846b2 ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag") Closes: https://lore.kernel.org/lkml/20240905180055.1221620-1-andrii@kernel.org/ Reported-by: Andrii Nakryiko <andrii.nakryiko@gmail.com> Signed-off-by: Kan Liang <kan.liang@linux.intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Tested-by: Andrii Nakryiko <andrii@kernel.org> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20240909155848.326640-1-kan.liang@linux.intel.com
1 parent 8d72eba commit ef493f4

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

arch/x86/events/intel/core.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,8 +3972,12 @@ static int intel_pmu_hw_config(struct perf_event *event)
39723972
x86_pmu.pebs_aliases(event);
39733973
}
39743974

3975-
if (needs_branch_stack(event) && is_sampling_event(event))
3976-
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
3975+
if (needs_branch_stack(event)) {
3976+
/* Avoid branch stack setup for counting events in SAMPLE READ */
3977+
if (is_sampling_event(event) ||
3978+
!(event->attr.sample_type & PERF_SAMPLE_READ))
3979+
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
3980+
}
39773981

39783982
if (branch_sample_counters(event)) {
39793983
struct perf_event *leader, *sibling;

0 commit comments

Comments
 (0)