perf/amd: Prevent grouping of IBS events
Subsystems (2)
PATCH 1/1 perf/amd: Prevent grouping of IBS events
IBS PMUs can have only one event active at any point in time. Restrict grouping of multiple IBS events.
--- arch/x86/events/amd/ibs.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
arch/x86/events/amd/ibs.c +26 -0
@@ -247,11 +247,33 @@ int forward_event_to_ibs(struct perf_event *event)return -ENOENT ; } +/* +* Grouping of IBS events is not possible since IBS can have only +* one event active at any point in time. +*/ +static int validate_group (struct perf_event *event) +{ +struct perf_event *sibling; + +if (event->group_leader == event) +return 0 ; + +if (event->group_leader->pmu == event->pmu) +return -EINVAL ; + +for_each_sibling_event (sibling, event->group_leader) { +if (sibling->pmu == event->pmu) +return -EINVAL ; + } +return 0 ; +} +static int perf_ibs_init (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw;struct perf_ibs *perf_ibs;u64 max_cnt, config; +int ret; perf_ibs =get_ibs_pmu (event->attr.type);if (!perf_ibs)
@@ -265,6 +287,10 @@ static int perf_ibs_init(struct perf_event *event)if (config & ~perf_ibs->config_mask)return -EINVAL ; + ret =validate_group (event); +if (ret) +return ret; +if (hwc->sample_period) {if (config & perf_ibs->cnt_mask)/* raw max_cnt may not be set */
Signature · 1 line
2.41.0
Ian Rogers
reviewerQuoted 3 lines · Ravi Bangoria
IBS PMUs can have only one event active at any point in time. Restrict grouping of multiple IBS events.
Thanks Ravi,
can you provide an example/test for this? Should this be a weak group issue?
Thanks,
Ian
Quoted 58 lines
Reported-by Sandipan Das <[email protected]>Signed-off-by Ravi Bangoria <[email protected]>--- arch/x86/events/amd/ibs.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)arch/x86/events/amd/ibs.c +26
-1@@ -247,11 +247,33 @@ int forward_event_to_ibs(struct perf_event *event)return -ENOENT ; } +/* +* Grouping of IBS events is not possible since IBS can have only +* one event active at any point in time. +*/ +static int validate_group (struct perf_event *event) +{ +struct perf_event *sibling; + +if (event->group_leader == event) +return 0 ; + +if (event->group_leader->pmu == event->pmu) +return -EINVAL ; + +for_each_sibling_event (sibling, event->group_leader) { +if (sibling->pmu == event->pmu) +return -EINVAL ; + } +return 0 ; +} +static int perf_ibs_init (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw;struct perf_ibs *perf_ibs;u64 max_cnt, config; +int ret; perf_ibs =get_ibs_pmu (event->attr.type);if (!perf_ibs)@@ -265,6 +287,10 @@ static int perf_ibs_init(struct perf_event *event)if (config & ~perf_ibs->config_mask)return -EINVAL ; + ret =validate_group (event); +if (ret) +return ret; +if (hwc->sample_period) {if (config & perf_ibs->cnt_mask)/* raw max_cnt may not be set */ --2.41.0
Hi Ian,
Quoted 8 lines · Ian Rogers
Quoted 3 lines · Ravi Bangoria
IBS PMUs can have only one event active at any point in time. Restrict grouping of multiple IBS events.
Thanks Ravi,
can you provide an example/test for this? Should this be a weak group issue?
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.540 MB perf.data (531 samples) ]After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0
Error:
AMD IBS may only be available in system-wide/per-cpu mode.
Try using -a, or -C and workload affinityThe error message is stale and misleading. I have a patch to fix it. I'll post it separately.
Thanks,
Ravi
PATCH 1/1 perf evsel amd: Fix IBS error message
AMD IBS can do per-process profiling[1] and is no longer restricted to per-cpu or systemwide only. Remove stale error message.
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0
Error:
AMD IBS may only be available in system-wide/per-cpu mode.
Try using -a, or -C and workload affinityAfter:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0
Error:
The sys_perf_event_open() syscall returned with 22 (Invalid
argument) for event (ibs_op//).
/bin/dmesg | grep -i perf may provide additional information.[1] https://git.kernel.org/torvalds/c/30093056f7b2
--- tools/perf/util/evsel.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
tools/perf/util/evsel.c +3 -7
@@ -3092,14 +3092,10 @@ int evsel__open_strerror(struct evsel *evsel, struct target *target,return scnprintf (msg, size,"Invalid event (%s) in per-thread mode, enable system wide with '-a'." ,evsel__name (evsel)); -if (is_amd (arch, cpuid)) { -if (is_amd_ibs (evsel)) { -if (evsel->core.attr.exclude_kernel) -return scnprintf (msg, size, +if (is_amd (arch, cpuid) &&is_amd_ibs (evsel)) { +if (evsel->core.attr.exclude_kernel) { +return scnprintf (msg, size,"AMD IBS can't exclude kernel events. Try running at a higher privilege level." ); -if (!evsel->core.system_wide) -return scnprintf (msg, size, -"AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinity" ); } }
Signature · 1 line
2.41.0
Namhyung Kim
maintainerRe: [PATCH] perf evsel amd: Fix IBS error message
Hi Ravi,
Quoted 9 lines · Ravi Bangoria
AMD IBS can do per-process profiling[1] and is no longer restricted to per-cpu or systemwide only. Remove stale error message.
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinity
It was strange that the -C option was given already.
Quoted 7 lines
After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (ibs_op//). /bin/dmesg | grep -i perf may provide additional information.
It can run newer perf tools on an old kernel but the old error message seems to be invalid anyway. So I'm ok with removing it.
Quoted 14 lines
[1] https://git.kernel.org/torvalds/c/30093056f7b2
Signed-off-by Ravi Bangoria <[email protected]>--- tools/perf/util/evsel.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)tools/perf/util/evsel.c +0
-0@@ -3092,14 +3092,10 @@ int evsel__open_strerror(struct evsel *evsel, struct target *target, return scnprintf(msg, size,
tools/perf/util/evsel.c int evsel__open_strerror(struct evsel *evsel, struct target *target, · line 3098
Quoted 10 lines
tools/perf/util/evsel.c +3
-4"Invalid event (%s) in per-thread mode, enable system wide with '-a'." , evsel__name(evsel)); -if (is_amd (arch, cpuid)) { -if (is_amd_ibs (evsel)) { -if (evsel->core.attr.exclude_kernel) -return scnprintf (msg, size, +if (is_amd (arch, cpuid) &&is_amd_ibs (evsel)) { +if (evsel->core.attr.exclude_kernel) { +return scnprintf (msg, size,"AMD IBS can't exclude kernel events. Try running at a higher privilege level." );
I'm not sure if it's enough. The IBS PMUs have CAP_NO_EXCLUDE then it can't exclude user events too, right?
Thanks,
Namhyung
Quoted 9 lines
tools/perf/util/evsel.c +0
-4-if (!evsel->core.system_wide) -return scnprintf (msg, size, -"AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinity" ); } } --2.41.0
Re: [PATCH] perf evsel amd: Fix IBS error message
Hi Namhyung,
Quoted 7 lines · Namhyung Kim
Hi Ravi,
Quoted 4 lines · Ravi Bangoria
AMD IBS can do per-process profiling[1] and is no longer restricted to per-cpu or systemwide only. Remove stale error message.
Quoted 7 lines
Quoted 5 lines
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinityIt was strange that the -C option was given already.
Hmm. evsel->core.system_wide is bit confusing for me. A Comment on it's definition says "irrespective of user requested CPUs or threads":
/*
* system_wide is for events that need to be on every CPU, irrespective
* of user requested CPUs or threads. Map propagation will set cpus to
* this event's own_cpus, whereby they will contribute to evlist
* all_cpus.
*/
bool system_wide;
So, I guess evsel->core.system_wide doesn't depend on -a / -C.
Quoted 11 lines
Quoted 8 lines
After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (ibs_op//). /bin/dmesg | grep -i perf may provide additional information.It can run newer perf tools on an old kernel but the old error message seems to be invalid anyway. So I'm ok with removing it.
Right.
Quoted 19 lines
Quoted 19 lines
[1] https://git.kernel.org/torvalds/c/30093056f7b2
Signed-off-by Ravi Bangoria <[email protected]>--- tools/perf/util/evsel.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)tools/perf/util/evsel.c +0
-3@@ -3092,14 +3092,10 @@ int evsel__open_strerror(struct evsel *evsel, struct target *target,return scnprintf (msg, size,"Invalid event (%s) in per-thread mode, enable system wide with '-a'." ,evsel__name (evsel)); -if (is_amd (arch, cpuid)) { -if (is_amd_ibs (evsel)) { -if (evsel->core.attr.exclude_kernel)
Quoted 8 lines
tools/perf/util/evsel.c int evsel__open_strerror(struct evsel *evsel, struct target *target, · line 3098
Quoted 5 lines
tools/perf/util/evsel.c +3
-1-return scnprintf (msg, size, +if (is_amd (arch, cpuid) &&is_amd_ibs (evsel)) { +if (evsel->core.attr.exclude_kernel) { +return scnprintf (msg, size,"AMD IBS can't exclude kernel events. Try running at a higher privilege level." );I'm not sure if it's enough. The IBS PMUs have
CAP_NO_EXCLUDEthen it can't exclude user events too, right?
That's correct. Let me try to fix this whole if (is_amd()) part properly.
Thanks for your feedback,
Ravi
Namhyung Kim
maintainerRe: [PATCH] perf evsel amd: Fix IBS error message
Quoted 30 lines · Ravi Bangoria
Hi Namhyung,
Quoted 7 lines · Namhyung Kim
Hi Ravi,
Quoted 4 lines · Ravi Bangoria
AMD IBS can do per-process profiling[1] and is no longer restricted to per-cpu or systemwide only. Remove stale error message.
Quoted 7 lines
Quoted 5 lines
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinityIt was strange that the -C option was given already.
Hmm.
evsel->core.system_wideis bit confusing for me. A Comment on it's definition says "irrespective of user requested CPUs or threads":/*
*system_wideis for events that need to be on every CPU, irrespective
* of user requested CPUs or threads. Map propagation will set cpus to
* this event'sown_cpus, whereby they will contribute to evlist
*all_cpus.
*/
boolsystem_wide;
Quoted 1 line
So, I guess
evsel->core.system_widedoesn't depend on -a / -C.
Right, you shouldn't check this flag. IIRC It's used by Intel PT for dummy events to get sideband events from every CPU regardless of the target.
The proper check would be target__has_cpu() and it seems the evsel__open_strerror() already checks that.
Thanks,
Namhyung
Quoted 47 lines
Quoted 10 lines
Quoted 9 lines
After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (ibs_op//). /bin/dmesg | grep -i perf may provide additional information.Quoted 2 lines
It can run newer perf tools on an old kernel but the old error message seems to be invalid anyway. So I'm ok with removing it.
Right.
Quoted 19 lines
Quoted 19 lines
[1] https://git.kernel.org/torvalds/c/30093056f7b2
Signed-off-by Ravi Bangoria <[email protected]>--- tools/perf/util/evsel.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)tools/perf/util/evsel.c +0
-3@@ -3092,14 +3092,10 @@ int evsel__open_strerror(struct evsel *evsel, struct target *target,return scnprintf (msg, size,"Invalid event (%s) in per-thread mode, enable system wide with '-a'." ,evsel__name (evsel)); -if (is_amd (arch, cpuid)) { -if (is_amd_ibs (evsel)) { -if (evsel->core.attr.exclude_kernel)Quoted 8 lines
tools/perf/util/evsel.c int evsel__open_strerror(struct evsel *evsel, struct target *target, · line 3098
Quoted 5 lines
tools/perf/util/evsel.c +3
-1-return scnprintf (msg, size, +if (is_amd (arch, cpuid) &&is_amd_ibs (evsel)) { +if (evsel->core.attr.exclude_kernel) { +return scnprintf (msg, size,"AMD IBS can't exclude kernel events. Try running at a higher privilege level." );I'm not sure if it's enough. The IBS PMUs have
CAP_NO_EXCLUDEthen it can't exclude user events too, right?That's correct. Let me try to fix this whole
if (is_amd())part properly.Thanks for your feedback,
Ravi
Namhyung Kim
maintainerQuoted 18 lines · Ravi Bangoria
Hi Ian,
Quoted 8 lines · Ian Rogers
Quoted 3 lines · Ravi Bangoria
IBS PMUs can have only one event active at any point in time. Restrict grouping of multiple IBS events.
Thanks Ravi,
can you provide an example/test for this? Should this be a weak group issue?
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.540 MB perf.data (531 samples) ]
Quoted 8 lines
After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinityThe error message is stale and misleading. I have a patch to fix it. I'll post it separately.
I'm just curious if ibs_fetch and ibs_op can be grouped together.
Thanks,
Namhyung
Hi Namhyung,
Quoted 1 line
I'm just curious if
ibs_fetchandibs_opcan be grouped together.
No. Both are independent hardware pmus and thus can not be grouped together.
Thanks,
Ravi
Ian Rogers
reviewerQuoted 18 lines · Ravi Bangoria
Hi Ian,
Quoted 8 lines · Ian Rogers
Quoted 3 lines · Ravi Bangoria
IBS PMUs can have only one event active at any point in time. Restrict grouping of multiple IBS events.
Thanks Ravi,
can you provide an example/test for this? Should this be a weak group issue?
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.540 MB perf.data (531 samples) ]
Quoted 8 lines
After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinityThe error message is stale and misleading. I have a patch to fix it. I'll post it separately.
Thanks Ravi, so this is a workaround for a PMU driver bug where the perf_event_open should have failed for the sibling event?
The behavior is somewhat reminiscent of arch_evsel__must_be_in_group:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/arch/x86/util/evsel.c?h=perf-tools-next#n41
Normally software events would be valid in the group, should the code ignore these?
Thanks,
Ian
Quoted 2 lines
Thanks,
Ravi
Hi Ian,
Quoted 11 lines
Quoted 11 lines
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.540 MB perf.data (531 samples) ]After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinity
Quoted 5 lines
Quoted 2 lines
The error message is stale and misleading. I have a patch to fix it. I'll post it separately.
Thanks Ravi, so this is a workaround for a PMU driver bug where the
perf_event_openshould have failed for the sibling event?
This is not a workaround. This kernel patch fixes PMU driver bug. With the patch, perf_event_open() will fail for sibling IBS event if either group leader or any other sibling is of the same IBS pmu. Or did I misread your comment?
Quoted 6 lines
The behavior is somewhat reminiscent of
arch_evsel__must_be_in_group:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/arch/x86/util/evsel.c?h=perf-tools-next#n41Normally software events would be valid in the group, should the code ignore these?
Grouping of SW and IBS event will continue to work after this patch.
Thanks,
Ravi
Ian Rogers
reviewerQuoted 26 lines · Ravi Bangoria
Hi Ian,
Quoted 11 lines
Quoted 11 lines
Before:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.540 MB perf.data (531 samples) ]After:
$ sudo ./perf record -e "{ibs_op//,ibs_op//}" -C 0 Error: AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinityQuoted 5 lines
Quoted 2 lines
The error message is stale and misleading. I have a patch to fix it. I'll post it separately.
Thanks Ravi, so this is a workaround for a PMU driver bug where the
perf_event_openshould have failed for the sibling event?This is not a workaround. This kernel patch fixes PMU driver bug. With the patch,
perf_event_open()will fail for sibling IBS event if either group leader or any other sibling is of the same IBS pmu. Or did I misread your comment?Quoted 1 line
Quoted 7 lines
Quoted 5 lines
The behavior is somewhat reminiscent of
arch_evsel__must_be_in_group:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/perf/arch/x86/util/evsel.c?h=perf-tools-next#n41Normally software events would be valid in the group, should the code ignore these?
Grouping of SW and IBS event will continue to work after this patch.
Sorry Ravi, I've got my head in the clouds. I was reading this as a tools patch :-)
Thanks,
Ian
Quoted 2 lines
Thanks,
Ravi
Quoted 2 lines
Sorry Ravi, I've got my head in the clouds. I was reading this as a tools patch :-)
No worries :). And thanks for taking a look at the patch!
Ravi
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 7c2128235eff99b448af8f4b5b2933495bf1a440 Gitweb: https://git.kernel.org/tip/7c2128235eff99b448af8f4b5b2933495bf1a440 Author: Ravi Bangoria <[email protected]> AuthorDate: Tue, 20 Jun 2023 14:46:03 +05:30 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 10 Jul 2023 09:52:34 +02:00
perf/amd: Prevent grouping of IBS events
IBS PMUs can have only one event active at any point in time. Restrict grouping of multiple IBS events.
--- arch/x86/events/amd/ibs.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
arch/x86/events/amd/ibs.c +26 -0
@@ -247,11 +247,33 @@ int forward_event_to_ibs(struct perf_event *event)return -ENOENT ; } +/* +* Grouping of IBS events is not possible since IBS can have only +* one event active at any point in time. +*/ +static int validate_group (struct perf_event *event) +{ +struct perf_event *sibling; + +if (event->group_leader == event) +return 0 ; + +if (event->group_leader->pmu == event->pmu) +return -EINVAL ; + +for_each_sibling_event (sibling, event->group_leader) { +if (sibling->pmu == event->pmu) +return -EINVAL ; + } +return 0 ; +} +static int perf_ibs_init (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw;struct perf_ibs *perf_ibs;u64 max_cnt, config; +int ret; perf_ibs =get_ibs_pmu (event->attr.type);if (!perf_ibs)
@@ -265,6 +287,10 @@ static int perf_ibs_init(struct perf_event *event)if (config & ~perf_ibs->config_mask)return -EINVAL ; + ret =validate_group (event); +if (ret) +return ret; +if (hwc->sample_period) {if (config & perf_ibs->cnt_mask)/* raw max_cnt may not be set */