提交 1b849a1b 编写于 作者: J Jin Yao 提交者: Lipeng Sang

perf pmu: Validate raw event with sysfs exported format bits

stable inclusion
from stable-v5.10.152
commit dea47fefa6aa87256c8a46137c742060940a4197
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I73HJ0

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=dea47fefa6aa87256c8a46137c742060940a4197

--------------------------------

[ Upstream commit e4064776 ]

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: NJiri Olsa <jolsa@redhat.com>
Signed-off-by: NJin Yao <yao.jin@linux.intel.com>
Reviewed-by: NJiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Jin Yao <yao.jin@intel.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20210310051138.12154-1-yao.jin@linux.intel.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
Stable-dep-of: e552b7be ("perf: Skip and warn on unknown format 'configN' attrs")
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NLipeng Sang <sanglipeng1@jd.com>

Conflicts:
	tools/perf/util/pmu.c
	tools/perf/util/pmu.h
上级 689ffc56
...@@ -356,6 +356,9 @@ __add_event(struct list_head *list, int *idx, ...@@ -356,6 +356,9 @@ __add_event(struct list_head *list, int *idx,
struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
cpu_list ? perf_cpu_map__new(cpu_list) : NULL; cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
if (pmu && attr->type == PERF_TYPE_RAW)
perf_pmu__warn_invalid_config(pmu, attr->config, name);
if (init_attr) if (init_attr)
event_attr_init(attr); event_attr_init(attr);
......
...@@ -1871,3 +1871,36 @@ int perf_pmu__match(char *pattern, char *name, char *tok) ...@@ -1871,3 +1871,36 @@ int perf_pmu__match(char *pattern, char *name, char *tok)
return 0; return 0;
} }
void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
char *name)
{
struct perf_pmu_format *format;
__u64 masks = 0, bits;
char buf[100];
unsigned int i;
list_for_each_entry(format, &pmu->format, list) {
if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
continue;
for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
masks |= 1ULL << i;
}
/*
* Kernel doesn't export any valid format bits.
*/
if (masks == 0)
return;
bits = config & ~masks;
if (bits == 0)
return;
bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
pr_warning("WARNING: event '%s' not valid (bits %s of config "
"'%llx' not supported by kernel)!\n",
name ?: "N/A", buf, config);
}
...@@ -128,4 +128,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu); ...@@ -128,4 +128,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu);
int perf_pmu__match(char *pattern, char *name, char *tok); int perf_pmu__match(char *pattern, char *name, char *tok);
void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
char *name);
#endif /* __PMU_H */ #endif /* __PMU_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册