Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf bpf: Tighten detection of BPF events

perf stat -e cpu/uops_executed.core,cmask=1/

would be detected as a BPF source event because the .c matches the .c
source BPF pattern.

v2:

Originally I tried to use lex lookahead, but it doesn't seem to work.

This now extends the BPF pattern to match longer events, but then does
an extra check in the C code to reject BPF matches that do not end with
.c/.o/.obj

This uses REJECT, which makes the flex scanner slower, but that
shouldn't be a big problem for the perf events.

Committer testing:

# perf trace -e write -e /home/acme/bpf/tracepoint.c cat /etc/passwd > /dev/null
0.000 ( 0.006 ms): cat/18485 write(fd: 1, buf: 0x7f59eebe1000, count: 3494 ) ...
0.006 ( ): raw_syscalls:sys_enter:NR 1 (1, 7f59eebe1000, da6, 22, 7f59eebe0010, 0))
0.008 ( ): perf_bpf_probe:_write:(ffffffff9626b2c0))
0.000 ( 0.010 ms): cat/18485 ... [continued]: write()) = 3494
#

It continues doing what was expected, i.e. identifying
/home/acme/bpf/tracepoint.c as a BPF event and activates the clang
machinery to build an eBPF object and then uses sys_bpf() to hook it up
to the raw_syscalls:sys_enter tracepoint, etc.

Andi forgot to add Wang to the CC list, fix it.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/20170811232634.30465-4-andi@firstfloor.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Andi Kleen and committed by
Arnaldo Carvalho de Melo
77d0871c 475fb533

+19 -4
+19 -4
tools/perf/util/parse-events.l
··· 53 53 return token; 54 54 } 55 55 56 + static bool isbpf(yyscan_t scanner) 57 + { 58 + char *text = parse_events_get_text(scanner); 59 + int len = strlen(text); 60 + 61 + if (len < 2) 62 + return false; 63 + if ((text[len - 1] == 'c' || text[len - 1] == 'o') && 64 + text[len - 2] == '.') 65 + return true; 66 + if (len > 4 && !strcmp(text + len - 4, ".obj")) 67 + return true; 68 + return false; 69 + } 70 + 56 71 /* 57 72 * This function is called when the parser gets two kind of input: 58 73 * ··· 151 136 group [^,{}/]*[{][^}]*[}][^,{}/]* 152 137 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* 153 138 event [^,{}/]+ 154 - bpf_object [^,{}]+\.(o|bpf) 155 - bpf_source [^,{}]+\.c 139 + bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]* 140 + bpf_source [^,{}]+\.c[a-zA-Z0-9._]* 156 141 157 142 num_dec [0-9]+ 158 143 num_hex 0x[a-fA-F0-9]+ ··· 322 307 {num_hex} { return value(yyscanner, 16); } 323 308 324 309 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } 325 - {bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } 326 - {bpf_source} { return str(yyscanner, PE_BPF_SOURCE); } 310 + {bpf_object} { if (!isbpf(yyscanner)) REJECT; return str(yyscanner, PE_BPF_OBJECT); } 311 + {bpf_source} { if (!isbpf(yyscanner)) REJECT; return str(yyscanner, PE_BPF_SOURCE); } 327 312 {name} { return pmu_str_check(yyscanner); } 328 313 "/" { BEGIN(config); return '/'; } 329 314 - { return '-'; }