powerpc/perf: Infrastructure to support checking of attr.config*
Madhavan Srinivasan
maintainerPATCH v3 2/2 powerpc/perf: Add platform specific check_attr_config
In reply to Madhavan SrinivasanAdd platform specific attr.config value checks. Patch includes checks for both power9 and power10.
---
Changelog v2:
- Changed function name as suggested.
- Added name of source document referred for reserved values
Changelog v1:
- No changes
arch/powerpc/perf/isa207-common.c | 42 +++++++++++++++++++++++++++++++ arch/powerpc/perf/isa207-common.h | 2 ++ arch/powerpc/perf/power10-pmu.c | 13 ++++++++++ arch/powerpc/perf/power9-pmu.c | 13 ++++++++++ 4 files changed, 70 insertions(+)
arch/powerpc/perf/isa207-common.c +42 -0
@@ -694,3 +694,45 @@ int isa207_get_alternatives(u64 event, u64 alt[], int size, unsigned int flags,return num_alt; } + +int isa3XX_check_attr_config (struct perf_event *ev) +{ +u64 val, sample_mode; +u64 event = ev->attr.config; + + val = (event >>EVENT_SAMPLE_SHIFT ) &EVENT_SAMPLE_MASK ; + sample_mode = val &0x3 ; + +/* +* MMCRA[61:62] is Random Sampling Mode (SM). +* value of 0b11 is reserved. +*/ +if (sample_mode ==0x3 ) +return -EINVAL ; + +/* +* Check for all reserved value +* Source: Performance Monitoring Unit User Guide +*/ +switch (val) { +case 0x5 : +case 0x9 : +case 0xD : +case 0x19 : +case 0x1D : +case 0x1A : +case 0x1E : +return -EINVAL ; + } + +/* +* MMCRA[48:51]/[52:55]) Threshold Start/Stop +* Events Selection. +* 0b11110000/0b00001111 is reserved. +*/ + val = (event >>EVENT_THR_CTL_SHIFT ) &EVENT_THR_CTL_MASK ; +if (((val &0xF0 ) ==0xF0 ) || ((val &0xF ) ==0xF )) +return -EINVAL ; + +return 0 ; +}
arch/powerpc/perf/isa207-common.h +2 -0
@@ -280,4 +280,6 @@ void isa207_get_mem_data_src(union perf_mem_data_src *dsrc, u32 flags, struct pt_regs *regs);void isa207_get_mem_weight (u64 *weight); +int isa3XX_check_attr_config (struct perf_event *ev); +#endif
arch/powerpc/perf/power10-pmu.c +13 -0
@@ -106,6 +106,18 @@ static int power10_get_alternatives(u64 event, unsigned int flags, u64 alt[])return num_alt; } +static int power10_check_attr_config (struct perf_event *ev) +{ +u64 val; +u64 event = ev->attr.config; + + val = (event >>EVENT_SAMPLE_SHIFT ) &EVENT_SAMPLE_MASK ; +if (val ==0x10 ||isa3XX_check_attr_config (ev)) +return -EINVAL ; + +return 0 ; +} +GENERIC_EVENT_ATTR (cpu-cycles,PM_RUN_CYC );GENERIC_EVENT_ATTR (instructions,PM_RUN_INST_CMPL );GENERIC_EVENT_ATTR (branch-instructions,PM_BR_CMPL );
@@ -559,6 +571,7 @@ static struct power_pmu power10_pmu = { .attr_groups = power10_pmu_attr_groups, .bhrb_nr = 32, .capabilities = PERF_PMU_CAP_EXTENDED_REGS, + .check_attr_config = power10_check_attr_config, };int init_power10_pmu (void )
arch/powerpc/perf/power9-pmu.c +13 -0
@@ -151,6 +151,18 @@ static int power9_get_alternatives(u64 event, unsigned int flags, u64 alt[])return num_alt; } +static int power9_check_attr_config (struct perf_event *ev) +{ +u64 val; +u64 event = ev->attr.config; + + val = (event >>EVENT_SAMPLE_SHIFT ) &EVENT_SAMPLE_MASK ; +if (val ==0xC ||isa3XX_check_attr_config (ev)) +return -EINVAL ; + +return 0 ; +} +GENERIC_EVENT_ATTR (cpu-cycles,PM_CYC );GENERIC_EVENT_ATTR (stalled-cycles-frontend,PM_ICT_NOSLOT_CYC );GENERIC_EVENT_ATTR (stalled-cycles-backend,PM_CMPLU_STALL );
@@ -437,6 +449,7 @@ static struct power_pmu power9_pmu = { .attr_groups = power9_pmu_attr_groups, .bhrb_nr = 32, .capabilities = PERF_PMU_CAP_EXTENDED_REGS, + .check_attr_config = power9_check_attr_config, };int init_power9_pmu (void )
Signature · 1 line
2.26.2
Madhavan Srinivasan
maintainerPATCH v3 1/2 powerpc/perf: Infrastructure to support checking of attr.config*
Introduce code to support the checking of attr.config* for values which are reserved for a given platform. Performance Monitoring Unit (PMU) configuration registers have fields that are reserved and some specific values for bit fields are reserved. For ex., MMCRA[61:62] is Random Sampling Mode (SM) and value of 0b11 for this field is reserved.
Writing non-zero or invalid values in these fields will have unknown behaviours.
Patch adds a generic call-back function "check_attr_config" in "struct power_pmu", to be called in event_init to check for attr.config* values for a given platform. "check_attr_config" is valid only for raw event type.
---
Changelog v2:
-Fixed commit message
Changelog v1:
-Fixed commit message and in-code comments
arch/powerpc/include/asm/perf_event_server.h | 6 ++++++ arch/powerpc/perf/core-book3s.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+)
arch/powerpc/include/asm/perf_event_server.h +6 -0
@@ -67,6 +67,12 @@ struct power_pmu {* the pmu supports extended perf regs capability */ int capabilities; +/* +* Function to check event code for values which are +* reserved. Function takes struct perf_event as input, +* since event code could be spread in attr.config* +*/ +int (*check_attr_config)(struct perf_event *ev); };/*
arch/powerpc/perf/core-book3s.c +14 -0
@@ -1958,6 +1958,20 @@ static int power_pmu_event_init(struct perf_event *event)if (ppmu->blacklist_ev &&is_event_blacklisted (ev))return -EINVAL ; +/* +* PMU config registers have fields that are +* reserved and specific value to bit field as reserved. +* For ex., MMCRA[61:62] is Randome Sampling Mode (SM) +* and value of 0b11 to this field is reserved. +* +* This check is needed only for raw event type, +* since tools like fuzzer use raw event type to +* provide randomized event code values for test. +* +*/ +if (ppmu->check_attr_config && + ppmu->check_attr_config (event)) +return -EINVAL ;break ;default :return -ENOENT ;
Signature · 1 line
2.26.2
Quoted 20 lines · Madhavan Srinivasan
Introduce code to support the checking of attr.config* for values which are reserved for a given platform. Performance Monitoring Unit (PMU) configuration registers have fields that are reserved and some specific values for bit fields are reserved. For ex., MMCRA[61:62] is Random Sampling Mode (SM) and value of 0b11 for this field is reserved.
Writing non-zero or invalid values in these fields will have unknown behaviours.
Patch adds a generic call-back function "
check_attr_config" in "structpower_pmu", to be called inevent_initto check for attr.config* values for a given platform. "check_attr_config" is valid only for raw event type.
Quoted 7 lines
Signed-off-by Madhavan Srinivasan <[email protected]>---
Changelog v2:
-Fixed commit messageChangelog v1:
-Fixed commit message and in-code comments
Changes looks fine to me.
Thanks,
Athira
Quoted 50 lines
arch/powerpc/include/asm/perf_event_server.h| 6 ++++++arch/powerpc/perf/core-book3s.c| 14 ++++++++++++++2 files changed, 20 insertions(+)arch/powerpc/include/asm/perf_event_server.h +6
-0@@ -67,6 +67,12 @@ struct power_pmu {* the pmu supports extended perf regs capability */ int capabilities; +/* +* Function to check event code for values which are +* reserved. Function takes struct perf_event as input, +* since event code could be spread in attr.config* +*/ +int (*check_attr_config)(struct perf_event *ev); };/* arch/powerpc/perf/core-book3s.c +14
-0@@ -1958,6 +1958,20 @@ static int power_pmu_event_init(struct perf_event *event)if (ppmu->blacklist_ev &&is_event_blacklisted (ev))return -EINVAL ; +/* +* PMU config registers have fields that are +* reserved and specific value to bit field as reserved. +* For ex., MMCRA[61:62] is Randome Sampling Mode (SM) +* and value of 0b11 to this field is reserved. +* +* This check is needed only for raw event type, +* since tools like fuzzer use raw event type to +* provide randomized event code values for test. +* +*/ +if (ppmu->check_attr_config && + ppmu->check_attr_config (event)) +return -EINVAL ;break ;default :return -ENOENT ;Signature · 1 line
2.26.2
Michael Ellerman
reviewerQuoted 13 lines · Madhavan Srinivasan
arch/powerpc/perf/core-book3s.c +5
-0@@ -1958,6 +1958,20 @@ static int power_pmu_event_init(struct perf_event *event)if (ppmu->blacklist_ev &&is_event_blacklisted (ev))return -EINVAL ; +/* +* PMU config registers have fields that are +* reserved and specific value to bit field as reserved. +* For ex., MMCRA[61:62] is Randome Sampling Mode (SM) +* and value of 0b11 to this field is reserved.
arch/powerpc/perf/core-book3s.c static int power_pmu_event_init(struct perf_event *event) · line 1975
Quoted 10 lines
arch/powerpc/perf/core-book3s.c +9
-0+* +* This check is needed only for raw event type, +* since tools like fuzzer use raw event type to +* provide randomized event code values for test. +* +*/ +if (ppmu->check_attr_config && + ppmu->check_attr_config (event)) +return -EINVAL ;break ;
It's not obvious from the diff, but you're only doing the check for RAW events.
But I think that's wrong, we should always check, even if the event code comes from the kernel we should still apply the same checks. Otherwise we might inadvertently use an invalid event code for a HARDWARE or CACHE event.
cheers
Madhavan Srinivasan
maintainerQuoted 27 lines · Michael Ellerman
Quoted 13 lines · Madhavan Srinivasan
arch/powerpc/perf/core-book3s.c +5
-0@@ -1958,6 +1958,20 @@ static int power_pmu_event_init(struct perf_event *event)if (ppmu->blacklist_ev &&is_event_blacklisted (ev))return -EINVAL ; +/* +* PMU config registers have fields that are +* reserved and specific value to bit field as reserved. +* For ex., MMCRA[61:62] is Randome Sampling Mode (SM) +* and value of 0b11 to this field is reserved. arch/powerpc/perf/core-book3s.c static int power_pmu_event_init(struct perf_event *event) · line 1975
Quoted 10 lines
arch/powerpc/perf/core-book3s.c +9
-0+* +* This check is needed only for raw event type, +* since tools like fuzzer use raw event type to +* provide randomized event code values for test. +* +*/ +if (ppmu->check_attr_config && + ppmu->check_attr_config (event)) +return -EINVAL ;break ;It's not obvious from the diff, but you're only doing the check for RAW events.
Quoted 3 lines
But I think that's wrong, we should always check, even if the event code comes from the kernel we should still apply the same checks. Otherwise we might inadvertently use an invalid event code for a HARDWARE or CACHE
Reason for not including HARDWARE and CACHE events are thats, they are straight forward, meaning they dont use sampling or thresholding features. Currently, checks are mostly in that spaces to check for any invalid values. We could include the check for all types the events.
I will respin the patch with that change
Thanks for review
Maddy
Quoted 3 lines
event.
cheers