Skip to contentSkip to conversation

perf/x86: fix wrong assumption that LBR is only useful for sampling events

PATCH In review Andrii Nakryiko 3 participants 6 messages Sep 5, 2024 – Sep 9, 2024

Subsystems (4)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 perf/x86: fix wrong assumption that LBR is only useful for sampling events

It's incorrect to assume that LBR can/should only be used with sampling events. BPF subsystem provides bpf_get_branch_snapshot() BPF helper, which expects a properly setup and activated perf event which allows kernel to capture LBR data.

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;

Commit referenced in Fixes tag broke this setup by making invalid assumption that LBR is useful only for sampling events. Remove that assumption.

Note, earlier we removed a similar assumption on AMD side of LBR support, see [1] for details.

[0] https://github.com/anakryiko/retsnoop
[1] 9794563d4d05 ("perf/x86/amd: Don't reject non-sampling events with configured LBR")

Fixes 85846b27072d ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
---
 arch/x86/events/intel/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +1 -1
index 9e519d8a810a..f82a342b8852 100644
@@ -3972,7 +3972,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
 			x86_pmu.pebs_aliases(event);
 	}
 
-	if (needs_branch_stack(event) && is_sampling_event(event))
+	if (needs_branch_stack(event))
 		event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
 
 	if (branch_sample_counters(event)) {
Signature · 1 line
2.43.5
#1 lore raw
Quoted 38 lines · Andrii Nakryiko

It's incorrect to assume that LBR can/should only be used with sampling events. BPF subsystem provides bpf_get_branch_snapshot() BPF helper, which expects a properly setup and activated perf event which allows kernel to capture LBR data.

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;

Commit referenced in Fixes tag broke this setup by making invalid assumption that LBR is useful only for sampling events. Remove that assumption.

Note, earlier we removed a similar assumption on AMD side of LBR support, see [1] for details.

[0] https://github.com/anakryiko/retsnoop
[1] 9794563d4d05 ("perf/x86/amd: Don't reject non-sampling events with configured LBR")

Fixes 85846b27072d ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
---
 arch/x86/events/intel/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +0 -0
index 9e519d8a810a..f82a342b8852 100644

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event *event) · line 3976

Quoted 6 lines
arch/x86/events/intel/core.c +1 -1
@@ -3972,7 +3972,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
 			x86_pmu.pebs_aliases(event);
 	}
 
-	if (needs_branch_stack(event) && is_sampling_event(event))
+	if (needs_branch_stack(event))
 		event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;

To limit the LBR for a sampling event is to avoid unnecessary branch stack setup for a counting event in the sample read. The above change should break the sample read case.

How about the below patch (not test)? Is it good enough for the BPF usage?

arch/x86/events/intel/core.c +0 -0
index 0c9c2706d4ec..8d67cbda916b 100644
@@ -3972,8 +3972,12 @@ static int intel_pmu_hw_config(struct perf_event

*event)
x86_pmu.pebs_aliases(event);
}

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

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

Thanks,
Kan

Quoted 2 lines
arch/x86/events/intel/core.c +0 -0
 	if (branch_sample_counters(event)) {
#2 lore raw
Quoted 63 lines · Kan
Quoted 38 lines · Andrii Nakryiko

It's incorrect to assume that LBR can/should only be used with sampling events. BPF subsystem provides bpf_get_branch_snapshot() BPF helper, which expects a properly setup and activated perf event which allows kernel to capture LBR data.

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;

Commit referenced in Fixes tag broke this setup by making invalid assumption that LBR is useful only for sampling events. Remove that assumption.

Note, earlier we removed a similar assumption on AMD side of LBR support, see [1] for details.

[0] https://github.com/anakryiko/retsnoop
[1] 9794563d4d05 ("perf/x86/amd: Don't reject non-sampling events with configured LBR")

Fixes 85846b27072d ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
---
 arch/x86/events/intel/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +0 -0
index 9e519d8a810a..f82a342b8852 100644

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event *event) · line 3976

Quoted 6 lines
arch/x86/events/intel/core.c +1 -1
@@ -3972,7 +3972,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
                      x86_pmu.pebs_aliases(event);
      }
 
-     if (needs_branch_stack(event) && is_sampling_event(event))
+     if (needs_branch_stack(event))
              event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;

To limit the LBR for a sampling event is to avoid unnecessary branch stack setup for a counting event in the sample read. The above change should break the sample read case.

How about the below patch (not test)? Is it good enough for the BPF usage?

arch/x86/events/intel/core.c +0 -0
index 0c9c2706d4ec..8d67cbda916b 100644
@@ -3972,8 +3972,12 @@ static int intel_pmu_hw_config(struct perf_event
 *event)
                x86_pmu.pebs_aliases(event);
        }

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event · line 3982

Quoted 10 lines
arch/x86/events/intel/core.c +6 -2
 
-       if (needs_branch_stack(event) && is_sampling_event(event))
-               event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       if (needs_branch_stack(event)) {
+               /* Avoid branch stack setup for counting events in SAMPLE READ */
+               if (is_sampling_event(event) ||
+                   !(event->attr.sample_type & PERF_SAMPLE_READ))
+                       event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       }
 

I'm sure it will be fine for my use case, as I set only PERF_SAMPLE_BRANCH_STACK.

But I'll leave it up to perf subsystem experts to decide if this condition makes sense, because looking at what PERF_SAMPLE_READ is:

PERF_SAMPLE_READ
Record counter values for all events in a group, not just the group leader.

It's not clear why this would disable LBR, if specified.

Quoted 8 lines
arch/x86/events/intel/core.c +0 -0
        if (branch_sample_counters(event)) {
                struct perf_event *leader, *sibling;

Thanks,
Kan

Quoted 2 lines
arch/x86/events/intel/core.c +0 -0
      if (branch_sample_counters(event)) {
#3 lore raw
Quoted 78 lines · Andrii Nakryiko
Quoted 63 lines · Kan
Quoted 38 lines · Andrii Nakryiko

It's incorrect to assume that LBR can/should only be used with sampling events. BPF subsystem provides bpf_get_branch_snapshot() BPF helper, which expects a properly setup and activated perf event which allows kernel to capture LBR data.

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;

Commit referenced in Fixes tag broke this setup by making invalid assumption that LBR is useful only for sampling events. Remove that assumption.

Note, earlier we removed a similar assumption on AMD side of LBR support, see [1] for details.

[0] https://github.com/anakryiko/retsnoop
[1] 9794563d4d05 ("perf/x86/amd: Don't reject non-sampling events with configured LBR")

Fixes 85846b27072d ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
---
 arch/x86/events/intel/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +0 -0
index 9e519d8a810a..f82a342b8852 100644

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event *event) · line 3976

Quoted 6 lines
arch/x86/events/intel/core.c +1 -1
@@ -3972,7 +3972,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
                      x86_pmu.pebs_aliases(event);
      }
 
-     if (needs_branch_stack(event) && is_sampling_event(event))
+     if (needs_branch_stack(event))
              event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;

To limit the LBR for a sampling event is to avoid unnecessary branch stack setup for a counting event in the sample read. The above change should break the sample read case.

How about the below patch (not test)? Is it good enough for the BPF usage?

arch/x86/events/intel/core.c +0 -0
index 0c9c2706d4ec..8d67cbda916b 100644
@@ -3972,8 +3972,12 @@ static int intel_pmu_hw_config(struct perf_event
 *event)
                x86_pmu.pebs_aliases(event);
        }

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event · line 3982

Quoted 10 lines
arch/x86/events/intel/core.c +6 -2
 
-       if (needs_branch_stack(event) && is_sampling_event(event))
-               event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       if (needs_branch_stack(event)) {
+               /* Avoid branch stack setup for counting events in SAMPLE READ */
+               if (is_sampling_event(event) ||
+                   !(event->attr.sample_type & PERF_SAMPLE_READ))
+                       event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       }
 

I'm sure it will be fine for my use case, as I set only PERF_SAMPLE_BRANCH_STACK.

Quoted 8 lines

But I'll leave it up to perf subsystem experts to decide if this condition makes sense, because looking at what PERF_SAMPLE_READ is:

PERF_SAMPLE_READ
Record counter values for all events in a group, not just the group leader.

It's not clear why this would disable LBR, if specified.

It only disables the counting event with SAMPLE_READ, since LBR is only read in the sampling event's overflow.

Thanks,
Kan

Quoted 9 lines
Quoted 9 lines
arch/x86/events/intel/core.c +0 -0
        if (branch_sample_counters(event)) {
                struct perf_event *leader, *sibling;

Thanks,
Kan

Quoted 2 lines
arch/x86/events/intel/core.c +0 -0
      if (branch_sample_counters(event)) {
#4 lore raw
Quoted 85 lines · Kan
Quoted 81 lines · Andrii Nakryiko
Quoted 63 lines · Kan
Quoted 38 lines · Andrii Nakryiko

It's incorrect to assume that LBR can/should only be used with sampling events. BPF subsystem provides bpf_get_branch_snapshot() BPF helper, which expects a properly setup and activated perf event which allows kernel to capture LBR data.

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;

Commit referenced in Fixes tag broke this setup by making invalid assumption that LBR is useful only for sampling events. Remove that assumption.

Note, earlier we removed a similar assumption on AMD side of LBR support, see [1] for details.

[0] https://github.com/anakryiko/retsnoop
[1] 9794563d4d05 ("perf/x86/amd: Don't reject non-sampling events with configured LBR")

Fixes 85846b27072d ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
---
 arch/x86/events/intel/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +0 -0
index 9e519d8a810a..f82a342b8852 100644

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event *event) · line 3976

Quoted 6 lines
arch/x86/events/intel/core.c +1 -1
@@ -3972,7 +3972,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
                      x86_pmu.pebs_aliases(event);
      }
 
-     if (needs_branch_stack(event) && is_sampling_event(event))
+     if (needs_branch_stack(event))
              event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;

To limit the LBR for a sampling event is to avoid unnecessary branch stack setup for a counting event in the sample read. The above change should break the sample read case.

How about the below patch (not test)? Is it good enough for the BPF usage?

arch/x86/events/intel/core.c +0 -0
index 0c9c2706d4ec..8d67cbda916b 100644
@@ -3972,8 +3972,12 @@ static int intel_pmu_hw_config(struct perf_event
 *event)
                x86_pmu.pebs_aliases(event);
        }

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event · line 3982

Quoted 10 lines
arch/x86/events/intel/core.c +6 -2
 
-       if (needs_branch_stack(event) && is_sampling_event(event))
-               event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       if (needs_branch_stack(event)) {
+               /* Avoid branch stack setup for counting events in SAMPLE READ */
+               if (is_sampling_event(event) ||
+                   !(event->attr.sample_type & PERF_SAMPLE_READ))
+                       event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       }
 

I'm sure it will be fine for my use case, as I set only PERF_SAMPLE_BRANCH_STACK.

But I'll leave it up to perf subsystem experts to decide if this condition makes sense, because looking at what PERF_SAMPLE_READ is:

Quoted 9 lines
Quoted 5 lines

PERF_SAMPLE_READ
Record counter values for all events in a group, not just the group leader.

It's not clear why this would disable LBR, if specified.

It only disables the counting event with SAMPLE_READ, since LBR is only read in the sampling event's overflow.

Ok, sounds good! Would you like to send a proper patch with your proposed changes?

Quoted 11 lines

Thanks,
Kan

Quoted 9 lines
Quoted 9 lines
arch/x86/events/intel/core.c +0 -0
        if (branch_sample_counters(event)) {
                struct perf_event *leader, *sibling;

Thanks,
Kan

Quoted 2 lines
arch/x86/events/intel/core.c +0 -0
      if (branch_sample_counters(event)) {
#5 lore raw
Quoted 90 lines · Andrii Nakryiko
Quoted 89 lines · Kan
Quoted 85 lines · Andrii Nakryiko
Quoted 63 lines · Kan
Quoted 38 lines · Andrii Nakryiko

It's incorrect to assume that LBR can/should only be used with sampling events. BPF subsystem provides bpf_get_branch_snapshot() BPF helper, which expects a properly setup and activated perf event which allows kernel to capture LBR data.

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;

Commit referenced in Fixes tag broke this setup by making invalid assumption that LBR is useful only for sampling events. Remove that assumption.

Note, earlier we removed a similar assumption on AMD side of LBR support, see [1] for details.

[0] https://github.com/anakryiko/retsnoop
[1] 9794563d4d05 ("perf/x86/amd: Don't reject non-sampling events with configured LBR")

Fixes 85846b27072d ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
---
 arch/x86/events/intel/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/core.c +0 -0
index 9e519d8a810a..f82a342b8852 100644

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event *event) · line 3976

Quoted 6 lines
arch/x86/events/intel/core.c +1 -1
@@ -3972,7 +3972,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
                      x86_pmu.pebs_aliases(event);
      }
 
-     if (needs_branch_stack(event) && is_sampling_event(event))
+     if (needs_branch_stack(event))
              event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;

To limit the LBR for a sampling event is to avoid unnecessary branch stack setup for a counting event in the sample read. The above change should break the sample read case.

How about the below patch (not test)? Is it good enough for the BPF usage?

arch/x86/events/intel/core.c +0 -0
index 0c9c2706d4ec..8d67cbda916b 100644
@@ -3972,8 +3972,12 @@ static int intel_pmu_hw_config(struct perf_event
 *event)
                x86_pmu.pebs_aliases(event);
        }

arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event · line 3982

Quoted 10 lines
arch/x86/events/intel/core.c +6 -2
 
-       if (needs_branch_stack(event) && is_sampling_event(event))
-               event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       if (needs_branch_stack(event)) {
+               /* Avoid branch stack setup for counting events in SAMPLE READ */
+               if (is_sampling_event(event) ||
+                   !(event->attr.sample_type & PERF_SAMPLE_READ))
+                       event->hw.flags  |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
+       }
 

I'm sure it will be fine for my use case, as I set only PERF_SAMPLE_BRANCH_STACK.

But I'll leave it up to perf subsystem experts to decide if this condition makes sense, because looking at what PERF_SAMPLE_READ is:

PERF_SAMPLE_READ
Record counter values for all events in a group, not just the group leader.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

It's not clear why this would disable LBR, if specified.

It only disables the counting event with SAMPLE_READ, since LBR is only read in the sampling event's overflow.

Ok, sounds good! Would you like to send a proper patch with your proposed changes?

The patch has been posted. Please give it a try.
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Kan

Quoted 13 lines
Quoted 12 lines

Thanks,
Kan

Quoted 9 lines
Quoted 9 lines
arch/x86/events/intel/core.c +0 -0
        if (branch_sample_counters(event)) {
                struct perf_event *leader, *sibling;

Thanks,
Kan

Quoted 2 lines
arch/x86/events/intel/core.c +0 -0
      if (branch_sample_counters(event)) {