Skip to content

Commit e406477

Browse files
Jin Yaoacmel
authored andcommitted
perf pmu: Validate raw event with sysfs exported format bits
A raw PMU event (eventsel+umask) in the form of rNNN is supported by perf but lacks of checking for the validity of raw encoding. For example, bit 16 and bit 17 are not valid on KBL but perf doesn't report warning when encoding with these bits. Before: # ./perf stat -e cpu/r031234/ -a -- sleep 1 Performance counter stats for 'system wide': 0 cpu/r031234/ 1.003798924 seconds time elapsed It may silently measure the wrong event! The kernel supported bits have been exported through /sys/devices/<pmu>/format/. Perf collects the information to 'struct perf_pmu_format' and links it to 'pmu->format' list. The 'struct perf_pmu_format' has a bitmap which records the valid bits for this format. For example, root@kbl-ppc:/sys/devices/cpu/format# cat umask config:8-15 The valid bits (bit8-bit15) are recorded in bitmap of format 'umask'. We collect total valid bits of all formats, save to a local variable 'masks' and reverse it. Now '~masks' represents total invalid bits. bits = config & ~masks; The set bits in 'bits' indicate the invalid bits used in config. Finally we use bitmap_scnprintf to report the invalid bits. Some architectures may not export supported bits through sysfs, so if masks is 0, perf_pmu__warn_invalid_config directly returns. After: Single event without name: # ./perf stat -e cpu/r031234/ -a -- sleep 1 WARNING: event 'N/A' not valid (bits 16-17 of config '31234' not supported by kernel)! Performance counter stats for 'system wide': 0 cpu/r031234/ 1.001597373 seconds time elapsed Multiple events with names: # ./perf stat -e cpu/rf01234,name=aaa/,cpu/r031234,name=bbb/ -a -- sleep 1 WARNING: event 'aaa' not valid (bits 20,22 of config 'f01234' not supported by kernel)! WARNING: event 'bbb' not valid (bits 16-17 of config '31234' not supported by kernel)! Performance counter stats for 'system wide': 0 aaa 0 bbb 1.001573787 seconds time elapsed Warnings are reported for invalid bits. Co-developed-by: Jiri Olsa <[email protected]> Signed-off-by: Jin Yao <[email protected]> Reviewed-by: Jiri Olsa <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Jin Yao <[email protected]> Cc: Kan Liang <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 689bb69 commit e406477

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

tools/perf/util/parse-events.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ __add_event(struct list_head *list, int *idx,
356356
struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
357357
cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
358358

359+
if (pmu && attr->type == PERF_TYPE_RAW)
360+
perf_pmu__warn_invalid_config(pmu, attr->config, name);
361+
359362
if (init_attr)
360363
event_attr_init(attr);
361364

tools/perf/util/pmu.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,3 +1812,36 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
18121812

18131813
return nr_caps;
18141814
}
1815+
1816+
void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1817+
char *name)
1818+
{
1819+
struct perf_pmu_format *format;
1820+
__u64 masks = 0, bits;
1821+
char buf[100];
1822+
unsigned int i;
1823+
1824+
list_for_each_entry(format, &pmu->format, list) {
1825+
if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1826+
continue;
1827+
1828+
for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1829+
masks |= 1ULL << i;
1830+
}
1831+
1832+
/*
1833+
* Kernel doesn't export any valid format bits.
1834+
*/
1835+
if (masks == 0)
1836+
return;
1837+
1838+
bits = config & ~masks;
1839+
if (bits == 0)
1840+
return;
1841+
1842+
bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1843+
1844+
pr_warning("WARNING: event '%s' not valid (bits %s of config "
1845+
"'%llx' not supported by kernel)!\n",
1846+
name ?: "N/A", buf, config);
1847+
}

tools/perf/util/pmu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@ int perf_pmu__convert_scale(const char *scale, char **end, double *sval);
123123

124124
int perf_pmu__caps_parse(struct perf_pmu *pmu);
125125

126+
void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
127+
char *name);
128+
126129
#endif /* __PMU_H */

0 commit comments

Comments
 (0)