perf/x86/intel: Fix lbr event can placed into non lbr group
Subsystems (2)
PATCH 1/1 perf/x86/intel: Fix lbr event can placed into non lbr group
The following perf command can trigger a warning on intel_pmu_lbr_counters_reorder.
# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this case. And is_branch_counters_group will check if the group_leader supports BRANCH_COUNTERS. So if a software event becomes a group_leader, which hw.flags is -1, this check will alway pass.
To fix this problem, using has_branch_stack to judge if leader is lbr event.
--- arch/x86/events/intel/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/events/intel/core.c +7 -7
@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |=PERF_X86_EVENT_NEEDS_BRANCH_STACK ; } +/* +* Force the leader to be a LBR event. So LBRs can be reset +* with the leader event. See intel_pmu_lbr_del() for details. +*/ +if (has_branch_stack (event) && !has_branch_stack (event->group_leader)) +return -EINVAL ; +if (branch_sample_counters (event)) {struct perf_event *leader, *sibling;int num =0 ;
@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event) ~(PERF_SAMPLE_BRANCH_PLM_ALL | PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; - -/* -* Force the leader to be a LBR event. So LBRs can be reset -* with the leader event. See intel_pmu_lbr_del() for details. -*/ -if (!intel_pmu_needs_branch_stack (leader)) -return -EINVAL ; }if (intel_pmu_needs_branch_stack (event)) {
Signature · 1 line
2.34.1
Quoted 8 lines · Luo Gengkun
The following perf command can trigger a warning on
intel_pmu_lbr_counters_reorder.# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this case.
Perf should only force the LBR leader for the branch counters case, so perf only needs to reset the LBRs for the leader.
I don't think the leader restriction should be applied to other cases.
Quoted 4 lines
And
is_branch_counters_groupwill check if thegroup_leadersupportsBRANCH_COUNTERS.
So if a software event becomes agroup_leader, which hw.flags is -1, this check will alway pass.
I think the default flags for all events is 0. Can you point me to where it is changed to -1?
Thanks,
Kan>
Quoted 41 lines
To fix this problem, using
has_branch_stackto judge if leader is lbr event.Fixes 33744916196b ("perf/x86/intel: Support branch counters logging")Signed-off-by Luo Gengkun <[email protected]>--- arch/x86/events/intel/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/events/intel/core.c +7
-7@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |=PERF_X86_EVENT_NEEDS_BRANCH_STACK ; } +/* +* Force the leader to be a LBR event. So LBRs can be reset +* with the leader event. See intel_pmu_lbr_del() for details. +*/ +if (has_branch_stack (event) && !has_branch_stack (event->group_leader)) +return -EINVAL ; +if (branch_sample_counters (event)) {struct perf_event *leader, *sibling;int num =0 ;@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event) ~(PERF_SAMPLE_BRANCH_PLM_ALL | PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; - -/* -* Force the leader to be a LBR event. So LBRs can be reset -* with the leader event. See intel_pmu_lbr_del() for details. -*/ -if (!intel_pmu_needs_branch_stack (leader)) -return -EINVAL ; }if (intel_pmu_needs_branch_stack (event)) {
Quoted 5 lines · Kan
Quoted 3 lines · Luo Gengkun
The following perf command can trigger a warning on
intel_pmu_lbr_counters_reorder.
Quoted 8 lines
Quoted 5 lines
# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this case.
Perf should only force the LBR leader for the branch counters case, so perf only needs to reset the LBRs for the leader.
I don't think the leader restriction should be applied to other cases.
Yes, the commit message should be updated. The code implementation only
restricts the leader to be an LBRs.
Quoted 9 lines
Quoted 4 lines
And
is_branch_counters_groupwill check if thegroup_leadersupportsBRANCH_COUNTERS.
So if a software event becomes agroup_leader, which hw.flags is -1, this check will alway pass.I think the default flags for all events is 0. Can you point me to where it is changed to -1?
Thanks,
Kan>
The hw_perf_event contains a union, hw.flags is used only for hardware events.
For the software events, it uses hrtimer. Therefor, when perf_swevent_init_hrtimer
is called, it changes the value of hw.flags too.
Thanks,
Gengkun
Quoted 41 lines
Quoted 41 lines
To fix this problem, using
has_branch_stackto judge if leader is lbr event.Fixes 33744916196b ("perf/x86/intel: Support branch counters logging")Signed-off-by Luo Gengkun <[email protected]>--- arch/x86/events/intel/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/events/intel/core.c +7
-7@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |=PERF_X86_EVENT_NEEDS_BRANCH_STACK ; } +/* +* Force the leader to be a LBR event. So LBRs can be reset +* with the leader event. See intel_pmu_lbr_del() for details. +*/ +if (has_branch_stack (event) && !has_branch_stack (event->group_leader)) +return -EINVAL ; +if (branch_sample_counters (event)) {struct perf_event *leader, *sibling;int num =0 ;@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event) ~(PERF_SAMPLE_BRANCH_PLM_ALL | PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; - -/* -* Force the leader to be a LBR event. So LBRs can be reset -* with the leader event. See intel_pmu_lbr_del() for details. -*/ -if (!intel_pmu_needs_branch_stack (leader)) -return -EINVAL ; }if (intel_pmu_needs_branch_stack (event)) {
Quoted 32 lines · Luo Gengkun
Quoted 5 lines · Kan
Quoted 3 lines · Luo Gengkun
The following perf command can trigger a warning on
intel_pmu_lbr_counters_reorder.Quoted 8 lines
Quoted 5 lines
# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this case.
Perf should only force the LBR leader for the branch counters case, so perf only needs to reset the LBRs for the leader.
I don't think the leader restriction should be applied to other cases.Yes, the commit message should be updated. The code implementation only
restricts the leader to be an LBRs.
Quoted 9 lines
Quoted 4 lines
And
is_branch_counters_groupwill check if thegroup_leadersupportsBRANCH_COUNTERS.
So if a software event becomes agroup_leader, which hw.flags is -1, this check will alway pass.I think the default flags for all events is 0. Can you point me to where it is changed to -1?
Thanks,
Kan>The
hw_perf_eventcontains a union, hw.flags is used only for hardware events.
Quoted 8 lines
For the software events, it uses hrtimer. Therefor, when
perf_swevent_init_hrtimeris called, it changes the value of hw.flags too.
Thanks,
Gengkun
It seems that using union is dangerous because different types of perf_events can be placed in the same group. Currently, a large number of codes directly access the hw of the leader, which is insecure. This part of the logic needs to be redesigned to void similar problems. And I am happy to work for this.
Thanks,
Gengkun
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
To fix this problem, using
has_branch_stackto judge if leader is lbr event.Fixes 33744916196b ("perf/x86/intel: Support branch counters logging")Signed-off-by Luo Gengkun <[email protected]>---
arch/x86/events/intel/core.c| 14 +++++++-------1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/events/intel/core.c +0
-0@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK; }+ /*
+ * Force the leader to be a LBR event. So LBRs can be reset + * with the leader event. Seeintel_pmu_lbr_del()for details. + */
+ if (has_branch_stack(event)&& !has_branch_stack(event->group_leader))
+ return-EINVAL;
+
if (branch_sample_counters(event)) {
structperf_event*leader, *sibling;
int num = 0;arch/x86/events/intel/core.c +0
-0@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event)~(PERF_SAMPLE_BRANCH_PLM_ALL |
PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; -
- /*
- * Force the leader to be a LBR event. So LBRs can be reset - * with the leader event. Seeintel_pmu_lbr_del()for details. - */
- if (!intel_pmu_needs_branch_stack(leader))
- return-EINVAL;
}
if (intel_pmu_needs_branch_stack(event)) {
Quoted 42 lines · Luo Gengkun
Quoted 40 lines · Luo Gengkun
Quoted 7 lines · Kan
Quoted 5 lines · Luo Gengkun
The following perf command can trigger a warning on
intel_pmu_lbr_counters_reorder.# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1Quoted 7 lines
Quoted 4 lines
The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this
case.Perf should only force the LBR leader for the branch counters case, so perf only needs to reset the LBRs for the leader.
I don't think the leader restriction should be applied to other cases.Yes, the commit message should be updated. The code implementation only
restricts the leader to be an LBRs.
Quoted 9 lines
Quoted 4 lines
And
is_branch_counters_groupwill check if thegroup_leadersupportsBRANCH_COUNTERS.
So if a software event becomes agroup_leader, which hw.flags is -1, this check will alway pass.I think the default flags for all events is 0. Can you point me to where it is changed to -1?
Thanks,
Kan>The
hw_perf_eventcontains a union, hw.flags is used only for hardware events.For the software events, it uses hrtimer. Therefor, when
perf_swevent_init_hrtimeris called, it changes the value of hw.flags too.
Quoted 8 lines
Quoted 3 lines
Thanks,
Gengkun
It seems that using union is dangerous because different types of
perf_eventscan be
placed in the same group.
Only the PMU with perf_sw_context can be placed in the same group with other types.
Quoted 3 lines
Currently, a large number of codes directly
access the hw
of the leader, which is insecure.
For X86, the topdown, ACR and branch counters will touch the leader.hw->flags. The topdown and ACR have already checked the leader before updating the flags. The branch counters missed it. I think a check is required for the branch counters as well, which should be good enough to address the issue.
arch/x86/events/intel/core.c +0 -0
@@ -4256,6 +4256,12 @@ static int intel_pmu_hw_config(struct perf_event
*event)
* group, which requires the extra space to store the counters. */
leader = event->group_leader;
+ /*
+ * The leader's hw.flags will be used to determine a
+ * branch counter logging group. Force it a X86 event.
+ */
+ if (!is_x86_event(leader))
+ return -EINVAL;
if (branch_sample_call_stack(leader))
return -EINVAL;
if (branch_sample_counters(leader)) {
Quoted 4 lines
This part of the logic needs to be redesigned to void
similar problems. And I am happy to work for this.
OK. Please share your idea.
Thanks,
Kan
Quoted 47 lines
Thanks,
GengkunQuoted 43 lines
Quoted 43 lines
Quoted 43 lines
To fix this problem, using
has_branch_stackto judge if leader is lbr event.Fixes 33744916196b ("perf/x86/intel: Support branch counters logging")Signed-off-by Luo Gengkun <[email protected]>---
arch/x86/events/intel/core.c| 14 +++++++-------1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/events/intel/ renamed from arch/x86/events/intel/core.c +0
-0
core.c
index09d2d66c9f21..c6b394019e54100644arch/x86/events/intel/core.c +0
-0@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK; }+ /*
+ * Force the leader to be a LBR event. So LBRs can be reset + * with the leader event. Seeintel_pmu_lbr_del()for details. + */
+ if (has_branch_stack(event)&& !has_branch_stack(event-Quoted 1 line
group_leader))+ return
-EINVAL;
+
if (branch_sample_counters(event)) {
structperf_event*leader, *sibling;
int num = 0;arch/x86/events/intel/core.c +0
-0@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event)~(PERF_SAMPLE_BRANCH_PLM_ALL |
PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; -
- /*
- * Force the leader to be a LBR event. So LBRs can be reset - * with the leader event. Seeintel_pmu_lbr_del()for details. - */
- if (!intel_pmu_needs_branch_stack(leader))
- return-EINVAL;
}
if (intel_pmu_needs_branch_stack(event)) {
Quoted 84 lines · Kan
Quoted 42 lines · Luo Gengkun
Quoted 40 lines · Luo Gengkun
Quoted 7 lines · Kan
Quoted 5 lines · Luo Gengkun
The following perf command can trigger a warning on
intel_pmu_lbr_counters_reorder.# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1Quoted 7 lines
Quoted 4 lines
The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this
case.Perf should only force the LBR leader for the branch counters case, so perf only needs to reset the LBRs for the leader.
I don't think the leader restriction should be applied to other cases.Yes, the commit message should be updated. The code implementation only
restricts the leader to be an LBRs.
Quoted 9 lines
Quoted 4 lines
And
is_branch_counters_groupwill check if thegroup_leadersupportsBRANCH_COUNTERS.
So if a software event becomes agroup_leader, which hw.flags is -1, this check will alway pass.I think the default flags for all events is 0. Can you point me to where it is changed to -1?
Thanks,
Kan>The
hw_perf_eventcontains a union, hw.flags is used only for hardware events.For the software events, it uses hrtimer. Therefor, when
perf_swevent_init_hrtimeris called, it changes the value of hw.flags too.
Quoted 8 lines
Quoted 3 lines
Thanks,
Gengkun
It seems that using union is dangerous because different types of
perf_eventscan be
placed in the same group.Only the PMU with
perf_sw_contextcan be placed in the same group with other types.Quoted 3 lines
Currently, a large number of codes directly
access the hw
of the leader, which is insecure.For X86, the topdown, ACR and branch counters will touch the
leader.hw->flags. The topdown and ACR have already checked the leader before updating the flags. The branch counters missed it. I think a check is required for the branch counters as well, which should be good enough to address the issue.arch/x86/events/intel/core.c +6
-0@@ -4256,6 +4256,12 @@ static int intel_pmu_hw_config(struct perf_event*event) * group, which requires the extra space to store the counters. */ leader = event->group_leader; +/* +* The leader's hw.flags will be used to determine a +* branch counter logging group. Force it a X86 event. +*/ +if (!is_x86_event (leader)) +return -EINVAL ;if (branch_sample_call_stack (leader))return -EINVAL ;
Quoted 2 lines
if (
branch_sample_counters(leader)) {
The above check may not enough, since the intel_pmu_lbr_counters_reorder() can be invoked without branch counters event.
I've posted a fix to address the issue. Please take a look.
https://lore.kernel.org/lkml/[email protected]/
Thanks,
Kan
Quoted 58 lines
Quoted 4 lines
This part of the logic needs to be redesigned to void
similar problems. And I am happy to work for this.OK. Please share your idea.
Thanks,
KanQuoted 47 lines
Thanks,
GengkunQuoted 43 lines
Quoted 43 lines
Quoted 43 lines
To fix this problem, using
has_branch_stackto judge if leader is lbr event.Fixes 33744916196b ("perf/x86/intel: Support branch counters logging")Signed-off-by Luo Gengkun <[email protected]>---
arch/x86/events/intel/core.c| 14 +++++++-------1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/events/intel/ renamed from arch/x86/events/intel/core.c +0
-0
core.c
index09d2d66c9f21..c6b394019e54100644arch/x86/events/intel/core.c +0
-0@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK; }+ /*
+ * Force the leader to be a LBR event. So LBRs can be reset + * with the leader event. Seeintel_pmu_lbr_del()for details. + */
+ if (has_branch_stack(event)&& !has_branch_stack(event-Quoted 1 line
group_leader))+ return
-EINVAL;
+
if (branch_sample_counters(event)) {
structperf_event*leader, *sibling;
int num = 0;arch/x86/events/intel/core.c +0
-0@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event)~(PERF_SAMPLE_BRANCH_PLM_ALL |
PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; -
- /*
- * Force the leader to be a LBR event. So LBRs can be reset - * with the leader event. Seeintel_pmu_lbr_del()for details. - */
- if (!intel_pmu_needs_branch_stack(leader))
- return-EINVAL;
}
if (intel_pmu_needs_branch_stack(event)) {
Quoted 83 lines · Kan
Quoted 75 lines · Kan
Quoted 37 lines · Luo Gengkun
Quoted 36 lines · Luo Gengkun
Quoted 6 lines · Kan
Quoted 5 lines · Luo Gengkun
The following perf command can trigger a warning on
intel_pmu_lbr_counters_reorder.# perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1Quoted 7 lines
Quoted 4 lines
The reason is that a lbr event are placed in non lbr group. And the previous implememtation cannot force the leader to be a lbr event in this
case.Perf should only force the LBR leader for the branch counters case, so perf only needs to reset the LBRs for the leader.
I don't think the leader restriction should be applied to other cases.Yes, the commit message should be updated. The code implementation only
restricts the leader to be an LBRs.
Quoted 9 lines
Quoted 4 lines
And
is_branch_counters_groupwill check if thegroup_leadersupportsBRANCH_COUNTERS.
So if a software event becomes agroup_leader, which hw.flags is -1, this check will alway pass.I think the default flags for all events is 0. Can you point me to where it is changed to -1?
Thanks,
Kan>The
hw_perf_eventcontains a union, hw.flags is used only for hardware events.For the software events, it uses hrtimer. Therefor, when
perf_swevent_init_hrtimeris called, it changes the value of hw.flags too.
Quoted 7 lines
Quoted 3 lines
Thanks,
Gengkun
It seems that using union is dangerous because different types of
perf_eventscan be
placed in the same group.Only the PMU with
perf_sw_contextcan be placed in the same group with other types.Quoted 3 lines
Currently, a large number of codes directly
access the hw
of the leader, which is insecure.For X86, the topdown, ACR and branch counters will touch the
leader.hw->flags. The topdown and ACR have already checked the leader before updating the flags. The branch counters missed it. I think a check is required for the branch counters as well, which should be good enough to address the issue.arch/x86/events/intel/core.c +6
-0@@ -4256,6 +4256,12 @@ static int intel_pmu_hw_config(struct perf_event*event) * group, which requires the extra space to store the counters. */ leader = event->group_leader; +/* +* The leader's hw.flags will be used to determine a +* branch counter logging group. Force it a X86 event. +*/ +if (!is_x86_event (leader)) +return -EINVAL ;if (branch_sample_call_stack (leader))return -EINVAL ;Quoted 2 lines
if (branch_sample_counters(leader)) {The above check may not enough, since the
intel_pmu_lbr_counters_reorder()can be invoked without branch counters event.
Quoted 5 lines
I've posted a fix to address the issue. Please take a look.
https://lore.kernel.org/lkml/[email protected]/Thanks,
Kan
LGTM.
Thanks,
Gengkun
Quoted 54 lines
Quoted 54 lines
Quoted 4 lines
This part of the logic needs to be redesigned to void
similar problems. And I am happy to work for this.OK. Please share your idea.
Thanks,
KanQuoted 45 lines
Thanks,
GengkunQuoted 43 lines
Quoted 43 lines
Quoted 43 lines
To fix this problem, using
has_branch_stackto judge if leader is lbr event.Fixes 33744916196b ("perf/x86/intel: Support branch counters logging")Signed-off-by Luo Gengkun <[email protected]>--- arch/x86/events/intel/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/events/intel/ renamed from arch/x86/events/intel/core.c +0
-0
core.c
index09d2d66c9f21..c6b394019e54100644arch/x86/events/intel/core.c +6
-7@@ -4114,6 +4114,13 @@ static int intel_pmu_hw_config(struct perf_event *event) event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK; } + /* + * Force the leader to be a LBR event. So LBRs can be reset + * with the leader event. See intel_pmu_lbr_del() for details. + */ + if (has_branch_stack(event) && !has_branch_stack(event- > group_leader)) + return -EINVAL; + if (branch_sample_counters(event)) { struct perf_event *leader, *sibling; int num = 0;@@ -4157,13 +4164,6 @@ static int intel_pmu_hw_config(struct perf_event *event) ~(PERF_SAMPLE_BRANCH_PLM_ALL | PERF_SAMPLE_BRANCH_COUNTERS))) event->hw.flags &= ~PERF_X86_EVENT_NEEDS_BRANCH_STACK; - - /* - * Force the leader to be a LBR event. So LBRs can be reset - * with the leader event. See intel_pmu_lbr_del() for details. - */ - if (!intel_pmu_needs_branch_stack(leader)) - return -EINVAL; } if (intel_pmu_needs_branch_stack(event)) {