perf/ibs: Fix interface via core pmu events
Subsystems (2)
cover perf/ibs: Fix interface via core pmu events
IBS used to allow event creation via core pmu events with precise_ip attribute set, but it's broken since late 2019. 1st patch fixes the issue and 2nd patch adds simple perf test for the same.
Ravi Bangoria (2):
perf/ibs: Fix interface via core pmu events
perf test: Add selftest to test IBS invocation via core pmu events
arch/x86/events/amd/core.c | 5 +++ arch/x86/events/core.c | 2 + arch/x86/events/perf_event.h | 3 ++ include/linux/perf_event.h | 1 + kernel/events/core.c | 11 +++-- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ibs-via-core-pmu.c | 70 +++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 9 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 tools/perf/tests/ibs-via-core-pmu.c
Signature · 1 line
2.39.2
PATCH 1/2 perf/ibs: Fix interface via core pmu events
Although, IBS pmu can be invoked via it's own interface, indirect IBS invocation via core pmu event is also supported with fixed set of events: cpu-cycles:p, r076:p (same as cpu-cycles:p) and r0C1:p (micro-ops) for user convenience.
This indirect IBS invocation is broken since commit 66d258c5b048 ("perf/core: Optimize perf_init_event()"), which added RAW pmu under pmu_idr list and thus if event_init() fails with RAW pmu, it started returning error instead of trying other pmus.
Fix it by introducing new pmu capability PERF_PMU_CAP_FORWARD_EVENT. Kernel will try to open event on other pmus if user requested pmu, having this capability, fails to open event.
Without patch:
$ sudo ./perf record -C 0 -e r076:p -- sleep 1 Error: The r076:p event is not supported.
With patch:
$ sudo ./perf record -C 0 -e r076:p -- sleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.341 MB perf.data (37 samples) ]
This new capability does not have a notion of forward pmu mapping. i.e. it doesn't know which pmu(or set of pmus) the event should be forwarded to. As of now, only AMD core pmu forwards a set of events to IBS pmu when precise_ip attribute is set and thus trying with all pmus works. But if more pmus start using this capability, some sort of forward pmu mapping needs to be introduced through which the event can directly get forwarded to only mapped pmus. Otherwise, trying all pmus can inadvertently open event on wrong pmu.
--- arch/x86/events/amd/core.c | 5 +++++ arch/x86/events/core.c | 2 ++ arch/x86/events/perf_event.h | 3 +++ include/linux/perf_event.h | 1 + kernel/events/core.c | 11 ++++++++--- 5 files changed, 19 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +5 -0
@@ -1264,6 +1264,11 @@ static __initconst const struct x86_pmu amd_pmu = { .cpu_dead = amd_pmu_cpu_dead, .amd_nb_constraints = 1, +/* +* Raw events with precise attribute set needs to be +* forwarded to IBS pmu. +*/ + .capabilities = PERF_PMU_CAP_FORWARD_EVENT, }; static ssize_t branches_show(struct device *cdev,
arch/x86/events/core.c +2 -0
@@ -2156,6 +2156,8 @@ static int __init init_hw_perf_events(void)if (err) goto out1; + pmu.capabilities |= x86_pmu.capabilities; +if (!is_hybrid ()) { err =perf_pmu_register (&pmu,"cpu" ,PERF_TYPE_RAW );if (err)
arch/x86/events/perf_event.h +3 -0
@@ -941,6 +941,9 @@ struct x86_pmu {int num_hybrid_pmus;struct x86_hybrid_pmu *hybrid_pmu;u8 (*get_hybrid_cpu_type) (void); + +/* Capabilities that needs to be forwarded to pmu->capabilities */ +int capabilities; };struct x86_perf_task_context_opt {
include/linux/perf_event.h +1 -0
@@ -292,6 +292,7 @@ struct perf_event_pmu_context;#define PERF_PMU_CAP_NO_EXCLUDE 0x0080#define PERF_PMU_CAP_AUX_OUTPUT 0x0100#define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0200 +#define PERF_PMU_CAP_FORWARD_EVENT 0x0400struct perf_output_handle ;
kernel/events/core.c +8 -3
@@ -11633,9 +11633,13 @@ static struct pmu *perf_init_event(struct perf_event *event) goto fail; ret =perf_try_init_event (pmu, event); -if (ret == -ENOENT && event->attr.type != type && !extended_type) { - type = event->attr.type; - goto again; +if (ret == -ENOENT ) { +if (event->attr.type != type && !extended_type) { + type = event->attr.type; + goto again; + } +if (pmu->capabilities &PERF_PMU_CAP_FORWARD_EVENT ) + goto try_all; }if (ret)
@@ -11644,6 +11648,7 @@ static struct pmu *perf_init_event(struct perf_event *event) goto unlock; } +try_all:list_for_each_entry_rcu (pmu, &pmus, entry,lockdep_is_held (&pmus_srcu)) { ret =perf_try_init_event (pmu, event);if (!ret)
Signature · 1 line
2.39.2
Namhyung Kim
maintainerHi Ravi,
Quoted 108 lines · Ravi Bangoria
Although, IBS pmu can be invoked via it's own interface, indirect IBS invocation via core pmu event is also supported with fixed set of events: cpu-cycles:p, r076:p (same as cpu-cycles:p) and r0C1:p (micro-ops) for user convenience.
This indirect IBS invocation is broken since commit
66d258c5b048("perf/core: Optimizeperf_init_event()"), which added RAW pmu underpmu_idrlist and thus ifevent_init()fails with RAW pmu, it started returning error instead of trying other pmus.Fix it by introducing new pmu capability
PERF_PMU_CAP_FORWARD_EVENT. Kernel will try to open event on other pmus if user requested pmu, having this capability, fails to open event.Without patch:
$ sudo ./perf record -C 0 -e r076:p -- sleep 1 Error: The r076:p event is not supported.With patch:
$ sudo ./perf record -C 0 -e r076:p -- sleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.341 MB perf.data (37 samples) ]This new capability does not have a notion of forward pmu mapping. i.e. it doesn't know which pmu(or set of pmus) the event should be forwarded to. As of now, only AMD core pmu forwards a set of events to IBS pmu when
precise_ipattribute is set and thus trying with all pmus works. But if more pmus start using this capability, some sort of forward pmu mapping needs to be introduced through which the event can directly get forwarded to only mapped pmus. Otherwise, trying all pmus can inadvertently open event on wrong pmu.Fixes 66d258c5b048 ("perf/core: Optimize perf_init_event()")Reported-by Stephane Eranian <[email protected]>Signed-off-by Ravi Bangoria <[email protected]>--- arch/x86/events/amd/core.c | 5 +++++ arch/x86/events/core.c | 2 ++ arch/x86/events/perf_event.h | 3 +++ include/linux/perf_event.h | 1 + kernel/events/core.c | 11 ++++++++--- 5 files changed, 19 insertions(+), 3 deletions(-)arch/x86/events/amd/core.c +5
-0@@ -1264,6 +1264,11 @@ static __initconst const struct x86_pmu amd_pmu = { .cpu_dead = amd_pmu_cpu_dead, .amd_nb_constraints = 1, +/* +* Raw events with precise attribute set needs to be +* forwarded to IBS pmu. +*/ + .capabilities = PERF_PMU_CAP_FORWARD_EVENT, }; static ssize_t branches_show(struct device *cdev,arch/x86/events/core.c +2
-0@@ -2156,6 +2156,8 @@ static int __init init_hw_perf_events(void)if (err) goto out1; + pmu.capabilities |= x86_pmu.capabilities; +if (!is_hybrid ()) { err =perf_pmu_register (&pmu,"cpu" ,PERF_TYPE_RAW );if (err)arch/x86/events/perf_event.h +3
-0@@ -941,6 +941,9 @@ struct x86_pmu {int num_hybrid_pmus;struct x86_hybrid_pmu *hybrid_pmu;u8 (*get_hybrid_cpu_type) (void); + +/* Capabilities that needs to be forwarded to pmu->capabilities */ +int capabilities; };struct x86_perf_task_context_opt {include/linux/perf_event.h +1
-0@@ -292,6 +292,7 @@ struct perf_event_pmu_context;#define PERF_PMU_CAP_NO_EXCLUDE 0x0080#define PERF_PMU_CAP_AUX_OUTPUT 0x0100#define PERF_PMU_CAP_EXTENDED_HW_TYPE 0x0200 +#define PERF_PMU_CAP_FORWARD_EVENT 0x0400struct perf_output_handle ;kernel/events/core.c +0
-0@@ -11633,9 +11633,13 @@ static struct pmu *perf_init_event(struct perf_event *event) goto fail; ret =perf_try_init_event (pmu, event);
kernel/events/core.c static struct pmu *perf_init_event(struct perf_event *event) · line 11642
Quoted 10 lines
kernel/events/core.c +7
-3-if (ret == -ENOENT && event->attr.type != type && !extended_type) { - type = event->attr.type; - goto again; +if (ret == -ENOENT ) { +if (event->attr.type != type && !extended_type) { + type = event->attr.type; + goto again; + } +if (pmu->capabilities &PERF_PMU_CAP_FORWARD_EVENT ) + goto try_all;
Wouldn't it be better to use a different error code to indicate it's about precise_ip (or forwarding in general)? Otherwise other invalid config might cause the forwarding unnecessarily..
Thanks,
Namhyung
Quoted 14 lines
kernel/events/core.c +1
-1}if (ret)@@ -11644,6 +11648,7 @@ static struct pmu *perf_init_event(struct perf_event *event) goto unlock; } +try_all:list_for_each_entry_rcu (pmu, &pmus, entry,lockdep_is_held (&pmus_srcu)) { ret =perf_try_init_event (pmu, event);if (!ret) --2.39.2
Hi Namhyung,
Quoted 7 lines
Quoted 7 lines
kernel/events/core.c +0
-0@@ -11633,9 +11633,13 @@ static struct pmu *perf_init_event(struct perf_event *event) goto fail;
Quoted 15 lines
kernel/events/core.c static struct pmu *perf_init_event(struct perf_event *event) · line 11642
Quoted 11 lines
kernel/events/core.c +7
-3ret =perf_try_init_event (pmu, event); -if (ret == -ENOENT && event->attr.type != type && !extended_type) { - type = event->attr.type; - goto again; +if (ret == -ENOENT ) { +if (event->attr.type != type && !extended_type) { + type = event->attr.type; + goto again; + } +if (pmu->capabilities &PERF_PMU_CAP_FORWARD_EVENT ) + goto try_all;Wouldn't it be better to use a different error code to indicate it's about
precise_ip(or forwarding in general)? Otherwise other invalid config might cause the forwarding unnecessarily..
That would make things easier and we might not need this new capability. Most appropriate error codes seems ENOENT, EOPNOTSUPP and EINVAL but all are already used for other purposes. Any other suggestions?
Thanks,
Ravi
Namhyung Kim
maintainerQuoted 26 lines · Ravi Bangoria
Hi Namhyung,
Quoted 7 lines
Quoted 7 lines
kernel/events/core.c +0
-0@@ -11633,9 +11633,13 @@ static struct pmu *perf_init_event(struct perf_event *event) goto fail;Quoted 15 lines
kernel/events/core.c static struct pmu *perf_init_event(struct perf_event *event) · line 11642
Quoted 11 lines
kernel/events/core.c +7
-3ret =perf_try_init_event (pmu, event); -if (ret == -ENOENT && event->attr.type != type && !extended_type) { - type = event->attr.type; - goto again; +if (ret == -ENOENT ) { +if (event->attr.type != type && !extended_type) { + type = event->attr.type; + goto again; + } +if (pmu->capabilities &PERF_PMU_CAP_FORWARD_EVENT ) + goto try_all;Wouldn't it be better to use a different error code to indicate it's about
precise_ip(or forwarding in general)? Otherwise other invalid config might cause the forwarding unnecessarily..
Quoted 3 lines
That would make things easier and we might not need this new capability. Most appropriate error codes seems ENOENT, EOPNOTSUPP and EINVAL but all are already used for other purposes. Any other suggestions?
Maybe we can have more liberty for the error code since it's not returned to the user. How about ESRCH, EIO or ENXIO?
Thanks,
Namhyung
Quoted 27 lines · Namhyung Kim
Quoted 26 lines · Ravi Bangoria
Hi Namhyung,
Quoted 22 lines
Quoted 8 lines
kernel/events/core.c +0
-0@@ -11633,9 +11633,13 @@ static struct pmu *perf_init_event(struct perf_event *event) goto fail; ret =perf_try_init_event (pmu, event);kernel/events/core.c static struct pmu *perf_init_event(struct perf_event *event) · line 11642
Quoted 10 lines
kernel/events/core.c +7
-3-if (ret == -ENOENT && event->attr.type != type && !extended_type) { - type = event->attr.type; - goto again; +if (ret == -ENOENT ) { +if (event->attr.type != type && !extended_type) { + type = event->attr.type; + goto again; + } +if (pmu->capabilities &PERF_PMU_CAP_FORWARD_EVENT ) + goto try_all;Wouldn't it be better to use a different error code to indicate it's about
precise_ip(or forwarding in general)? Otherwise other invalid config might cause the forwarding unnecessarily..
Quoted 6 lines
Quoted 3 lines
That would make things easier and we might not need this new capability. Most appropriate error codes seems ENOENT, EOPNOTSUPP and EINVAL but all are already used for other purposes. Any other suggestions?
Maybe we can have more liberty for the error code since it's not returned to the user. How about ESRCH, EIO or ENXIO?
Ok. We can probably use ESRCH, although it's meaning is little different:
$ errno -l | grep ESRCH ESRCH 3 No such process
Thanks,
Ravi
PATCH 2/2 perf test: Add selftest to test IBS invocation via core pmu events
In reply to Ravi BangoriaIBS pmu can be invoked via fixed set of core pmu events. Add simple event open test for all these events.
Without kernel fix:
$ sudo ./perf test -vv 76 76: IBS via core pmu : --- start --- test child forked, pid 6553 Using CPUID AuthenticAMD-25-1-1 type: 0x0, config: 0x0, fd: 3 - Pass type: 0x0, config: 0x1, fd: -1 - Pass type: 0x4, config: 0x76, fd: -1 - Fail type: 0x4, config: 0xc1, fd: -1 - Fail type: 0x4, config: 0x12, fd: -1 - Pass test child finished with -1 ---- end ---- IBS via core pmu: FAILED!
With kernel fix:
$ sudo ./perf test -vv 76 76: IBS via core pmu : --- start --- test child forked, pid 7526 Using CPUID AuthenticAMD-25-1-1 type: 0x0, config: 0x0, fd: 3 - Pass type: 0x0, config: 0x1, fd: -1 - Pass type: 0x4, config: 0x76, fd: 3 - Pass type: 0x4, config: 0xc1, fd: 3 - Pass type: 0x4, config: 0x12, fd: -1 - Pass test child finished with 0 ---- end ---- IBS via core pmu: Ok
--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ibs-via-core-pmu.c | 70 +++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 73 insertions(+) create mode 100644 tools/perf/tests/ibs-via-core-pmu.c
tools/perf/tests/Build +1 -0
@@ -69,6 +69,7 @@ perf-y += dlfilter-test.o perf-y += sigtrap.o perf-y += event_groups.o perf-y += symbols.o +perf-y += ibs-via-core-pmu.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +1 -0
@@ -119,6 +119,7 @@ static struct test_suite *generic_tests[] = { &suite__sigtrap, &suite__event_groups, &suite__symbols, + &suite__ibs_via_core_pmu, NULL, };
tools/perf/tests/ibs-via-core-pmu.c new file +70 -0
70 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -149,6 +149,7 @@ DECLARE_SUITE(dlfilter);DECLARE_SUITE (sigtrap);DECLARE_SUITE (event_groups);DECLARE_SUITE (symbols); +DECLARE_SUITE (ibs_via_core_pmu);/* * PowerPC and S390 do not support creation of instruction breakpoints using the
Signature · 1 line
2.39.2
Namhyung Kim
maintainerRe: [PATCH 2/2] perf test: Add selftest to test IBS invocation via core pmu events
Quoted 123 lines · Ravi Bangoria
IBS pmu can be invoked via fixed set of core pmu events. Add simple event open test for all these events.
Without kernel fix:
$ sudo ./perf test -vv 76 76: IBS via core pmu : --- start --- test child forked, pid 6553 Using CPUID AuthenticAMD-25-1-1 type: 0x0, config: 0x0, fd: 3 - Pass type: 0x0, config: 0x1, fd: -1 - Pass type: 0x4, config: 0x76, fd: -1 - Fail type: 0x4, config: 0xc1, fd: -1 - Fail type: 0x4, config: 0x12, fd: -1 - Pass test child finished with -1 ---- end ---- IBS via core pmu: FAILED!With kernel fix:
$ sudo ./perf test -vv 76 76: IBS via core pmu : --- start --- test child forked, pid 7526 Using CPUID AuthenticAMD-25-1-1 type: 0x0, config: 0x0, fd: 3 - Pass type: 0x0, config: 0x1, fd: -1 - Pass type: 0x4, config: 0x76, fd: 3 - Pass type: 0x4, config: 0xc1, fd: 3 - Pass type: 0x4, config: 0x12, fd: -1 - Pass test child finished with 0 ---- end ---- IBS via core pmu: OkSigned-off-by Ravi Bangoria <[email protected]>--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ibs-via-core-pmu.c | 70 +++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 73 insertions(+) create mode 100644 tools/perf/tests/ibs-via-core-pmu.ctools/perf/tests/Build +1
-0@@ -69,6 +69,7 @@ perf-y += dlfilter-test.o perf-y += sigtrap.o perf-y += event_groups.o perf-y += symbols.o +perf-y += ibs-via-core-pmu.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)tools/perf/tests/builtin-test.c +1
-0@@ -119,6 +119,7 @@ static struct test_suite *generic_tests[] = { &suite__sigtrap, &suite__event_groups, &suite__symbols, + &suite__ibs_via_core_pmu, NULL, };tools/perf/tests/ibs-via-core-pmu.c new file +50
-0@@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "linux/perf_event.h" +#include "tests.h" +#include "pmu.h" +#include "pmus.h" +#include "../perf-sys.h" +#include "debug.h" + +#define NR_SUB_TESTS 5 + +static struct sub_tests { +int type; +unsigned long config; +bool valid; +} sub_tests[NR_SUB_TESTS ] = { + {PERF_TYPE_HARDWARE ,PERF_COUNT_HW_CPU_CYCLES , true }, + {PERF_TYPE_HARDWARE ,PERF_COUNT_HW_INSTRUCTIONS , false }, + {PERF_TYPE_RAW ,0x076 , true }, + {PERF_TYPE_RAW ,0x0C1 , true }, + {PERF_TYPE_RAW ,0x012 , false }, +}; + +static int event_open (int type,unsigned long config) +{ +struct perf_event_attr attr; + +memset (&attr,0 ,sizeof (struct perf_event_attr )); + attr.type = type; + attr.size =sizeof (struct perf_event_attr ); + attr.config = config; + attr.disabled =1 ; + attr.precise_ip =1 ; + attr.sample_type =PERF_SAMPLE_IP |PERF_SAMPLE_TID ; + attr.sample_period =100000 ; + +return sys_perf_event_open (&attr,-1 ,0 ,-1 ,0 ); +} + +static int test__ibs_via_core_pmu (struct test_suite *text__maybe_unused , +int subtest__maybe_unused ) +{ +struct perf_pmu *ibs_pmu; +int ret =TEST_OK ; +int fd, i; + +if (list_empty (&pmus)) +perf_pmu__scan (NULL ); + + ibs_pmu =perf_pmu__find ("ibs_op" ); +if (!ibs_pmu)
tools/perf/tests/ibs-via-core-pmu.c line 60
Quoted 10 lines
tools/perf/tests/ibs-via-core-pmu.c new file +10
-0+return TEST_SKIP ; + +for (i =0 ; i <NR_SUB_TESTS ; i++) { + fd =event_open (sub_tests[i].type, sub_tests[i].config); +pr_debug ("type: 0x%x, config: 0x%lx, fd: %d - " , sub_tests[i].type, + sub_tests[i].config, fd); +if ((sub_tests[i].valid && fd ==-1 ) || + (!sub_tests[i].valid && fd >0 )) { +pr_debug ("Fail\n" ); + ret =TEST_FAIL ;
You need to close(fd) when it's succeeded for invalid one.
Thanks,
Namhyung
Quoted 25 lines
tools/perf/tests/ibs-via-core-pmu.c new file +10
-0+ }else { +pr_debug ("Pass\n" ); +close (fd); + } + } + +return ret; +} + +DEFINE_SUITE ("IBS via core pmu" , ibs_via_core_pmu);tools/perf/tests/tests.h +1
-1@@ -149,6 +149,7 @@ DECLARE_SUITE(dlfilter);DECLARE_SUITE (sigtrap);DECLARE_SUITE (event_groups);DECLARE_SUITE (symbols); +DECLARE_SUITE (ibs_via_core_pmu);/* * PowerPC and S390 do not support creation of instruction breakpoints using the -- 2.39.2
Re: [PATCH 2/2] perf test: Add selftest to test IBS invocation via core pmu events
Hi Namhyung,
Quoted 16 lines
Quoted 16 lines
tools/perf/tests/ibs-via-core-pmu.c new file +16
-0+static int test__ibs_via_core_pmu (struct test_suite *text__maybe_unused , +int subtest__maybe_unused ) +{ +struct perf_pmu *ibs_pmu; +int ret =TEST_OK ; +int fd, i; + +if (list_empty (&pmus)) +perf_pmu__scan (NULL ); + + ibs_pmu =perf_pmu__find ("ibs_op" ); +if (!ibs_pmu) +return TEST_SKIP ; + +for (i =0 ; i <NR_SUB_TESTS ; i++) { + fd =event_open (sub_tests[i].type, sub_tests[i].config);
Quoted 8 lines
tools/perf/tests/ibs-via-core-pmu.c line 60
Quoted 6 lines
tools/perf/tests/ibs-via-core-pmu.c new file +6
-0+pr_debug ("type: 0x%x, config: 0x%lx, fd: %d - " , sub_tests[i].type, + sub_tests[i].config, fd); +if ((sub_tests[i].valid && fd ==-1 ) || + (!sub_tests[i].valid && fd >0 )) { +pr_debug ("Fail\n" ); + ret =TEST_FAIL ;You need to close(fd) when it's succeeded for invalid one.
Yup. Will fix it.
Thanks,
Ravi