perf bpf-filter: Add cgroup filter term (v1)
Namhyung Kim
maintainercover perf bpf-filter: Add cgroup filter term (v1)
Hello,
I'm adding a new 'cgroup' filter term in the filter expression so that it can be used in the BPF.
The cgroup filter takes a pathname and allows either '==' or '!=' operators only. The requires '--all-cgroups' option so it can see the cgroup_id in the sample data. Actually BPF programs can get the cgroup of the current task without looking sample data, but I added it for consistency with other fields.
$ sudo perf record --synth=cgroup -e cycles --filter 'cgroup > /' -a true perf_bpf_filter: cgroup accepts '==' or '!=' only
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
--filter <filter>
event filter
$ sudo perf record -e cycles --filter 'cgroup == /' \
-a --synth=cgroup -o- sleep 1 | perf report -i- -s cgroup
Error: cycles event does not have PERF_SAMPLE_CGROUP
Hint: please add --all-cgroups option to perf record
Failed to process filter entries
failed to set filter "BPF" on event cycles with 2 (No such file or directory)
incompatible file format (rerun with -v to learn more)On the filter syntax, it used to reject arbitrary strings. But it needs to handle strings because of the cgroup paths. For now, the bpf filter parser will reject strings if it's not used for cgroups.
For now, it only supports the exact matching of target cgroups. So specifying the root cgroup ("/") will match tasks in the root cgroup directly (not in the descendants).
$ sudo perf record --all-cgroups -e cycles --filter 'cgroup == /' \
-a --synth=cgroup -o- sleep 1 | perf report -i- -s cgroup
...
#
# Total Lost Samples: 0
#
# Samples: 621 of event 'cycles'
# Event count (approx.): 108101281
#
# Overhead Cgroup
# ........ ......
#
100.00% /Thanks,
Namhyung
Namhyung Kim (4):
perf report: Fix segfault when 'sym' sort key is not used perf bpf-filter: Add build dependency to header files perf bpf-filter: Support filtering on cgroups perf test: Add perf record cgroup filtering test
tools/perf/tests/shell/record_bpf_filter.sh | 39 ++++++++++++++++++-- tools/perf/util/Build | 4 +- tools/perf/util/bpf-filter.c | 1 + tools/perf/util/bpf-filter.l | 28 +++++++++++--- tools/perf/util/bpf-filter.y | 28 +++++++++++++- tools/perf/util/bpf_skel/sample-filter.h | 2 +- tools/perf/util/bpf_skel/sample_filter.bpf.c | 4 +- tools/perf/util/bpf_skel/vmlinux/vmlinux.h | 1 + tools/perf/util/hist.c | 2 +- 9 files changed, 94 insertions(+), 15 deletions(-)
Signature · 1 line
2.46.0.295.g3b9ea8a38a-goog
Namhyung Kim
maintainerPATCH 1/4 perf report: Fix segfault when 'sym' sort key is not used
The fields in the hist_entry are filled on-demand which means they only have meaningful values when relevant sort keys are used. So if neither of 'dso' nor 'sym' sort keys are used, the map/symbols in the hist entry can be garbage. So it shouldn't access it unconditionally.
I got a segfault, when I wanted to see cgroup profiles.
$ sudo perf record -a --all-cgroups --synth=cgroup true
$ sudo perf report -s cgroup
Program received signal SIGSEGV, Segmentation fault. 0x00005555557a8d90 in map__dso (map=0x0) at util/map.h:48
48 return RC_CHK_ACCESS(map)->dso;
(gdb) bt
#0 0x00005555557a8d90 in map__dso (map=0x0) at util/map.h:48
#1 0x00005555557aa39b in map__load (map=0x0) at util/map.c:344
#2 0x00005555557aa592 in map__find_symbol (map=0x0, addr=140736115941088) at util/map.c:385
#3 0x00005555557ef000 in hists__findnew_entry (hists=0x555556039d60, entry=0x7fffffffa4c0, al=0x7fffffffa8c0, sample_self=true)
at util/hist.c:644
#4 0x00005555557ef61c in __hists__add_entry (hists=0x555556039d60, al=0x7fffffffa8c0, sym_parent=0x0, bi=0x0, mi=0x0, ki=0x0,
block_info=0x0, sample=0x7fffffffaa90, sample_self=true, ops=0x0) at util/hist.c:761
#5 0x00005555557ef71f in hists__add_entry (hists=0x555556039d60, al=0x7fffffffa8c0, sym_parent=0x0, bi=0x0, mi=0x0, ki=0x0,
sample=0x7fffffffaa90, sample_self=true) at util/hist.c:779
#6 0x00005555557f00fb in iter_add_single_normal_entry (iter=0x7fffffffa900, al=0x7fffffffa8c0) at util/hist.c:1015
#7 0x00005555557f09a7 in hist_entry_iter__add (iter=0x7fffffffa900, al=0x7fffffffa8c0, max_stack_depth=127, arg=0x7fffffffbce0)
at util/hist.c:1260
#8 0x00005555555ba7ce in process_sample_event (tool=0x7fffffffbce0, event=0x7ffff7c14128, sample=0x7fffffffaa90, evsel=0x555556039ad0,
machine=0x5555560388e8) at builtin-report.c:334
#9 0x00005555557b30c8 in evlist__deliver_sample (evlist=0x555556039010, tool=0x7fffffffbce0, event=0x7ffff7c14128,
sample=0x7fffffffaa90, evsel=0x555556039ad0, machine=0x5555560388e8) at util/session.c:1232
#10 0x00005555557b32bc in machines__deliver_event (machines=0x5555560388e8, evlist=0x555556039010, event=0x7ffff7c14128,
sample=0x7fffffffaa90, tool=0x7fffffffbce0, file_offset=110888, file_path=0x555556038ff0 "perf.data") at util/session.c:1271
#11 0x00005555557b3848 in perf_session__deliver_event (session=0x5555560386d0, event=0x7ffff7c14128, tool=0x7fffffffbce0,
file_offset=110888, file_path=0x555556038ff0 "perf.data") at util/session.c:1354
#12 0x00005555557affaf in ordered_events__deliver_event (oe=0x555556038e60, event=0x555556135aa0) at util/session.c:132
#13 0x00005555557bb605 in do_flush (oe=0x555556038e60, show_progress=false) at util/ordered-events.c:245
#14 0x00005555557bb95c in __ordered_events__flush (oe=0x555556038e60, how=OE_FLUSH__ROUND, timestamp=0) at util/ordered-events.c:324
#15 0x00005555557bba46 in ordered_events__flush (oe=0x555556038e60, how=OE_FLUSH__ROUND) at util/ordered-events.c:342
#16 0x00005555557b1b3b in perf_event__process_finished_round (tool=0x7fffffffbce0, event=0x7ffff7c15bb8, oe=0x555556038e60)
at util/session.c:780
#17 0x00005555557b3b27 in perf_session__process_user_event (session=0x5555560386d0, event=0x7ffff7c15bb8, file_offset=117688,
file_path=0x555556038ff0 "perf.data") at util/session.c:1406As you can see the entry->ms.map was NULL even if he->ms.map has a value. This is because 'sym' sort key is not given, so it cannot assume whether he->ms.sym and entry->ms.sym is the same. I only checked the 'sym' sort key here as it implies 'dso' behavior (so maps are the same).
--- tools/perf/util/hist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/hist.c +1 -1
@@ -638,7 +638,7 @@ static struct hist_entry *hists__findnew_entry(struct hists *hists,* mis-adjust symbol addresses when computing * the history counter to increment. */ -if (he->ms.map != entry->ms.map) { +if (hists__has (hists, sym) && he->ms.map != entry->ms.map) {if (he->ms.sym) {u64 addr = he->ms.sym->start; he->ms.sym =map__find_symbol (entry->ms.map, addr);
Signature · 1 line
2.46.0.295.g3b9ea8a38a-goog
Re: [PATCH 1/4] perf report: Fix segfault when 'sym' sort key is not used
Quoted 6 lines · Namhyung Kim
The fields in the
hist_entryare filled on-demand which means they only have meaningful values when relevant sort keys are used. So if neither of 'dso' nor 'sym' sort keys are used, the map/symbols in the hist entry can be garbage. So it shouldn't access it unconditionally.I got a segfault, when I wanted to see cgroup profiles.
Thanks, applied the series to perf-tools-next,
- Arnaldo
Namhyung Kim
maintainerPATCH 2/4 perf bpf-filter: Add build dependency to header files
In reply to Namhyung KimThe flex and bison files need to be recompiled when one of these header filters are changed.
* util/bpf-filter.h
* util/bpf_skel/sample-filter.h
--- tools/perf/util/Build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/Build +2 -2
@@ -278,12 +278,12 @@ $(OUTPUT)util/pmu-bison.c $(OUTPUT)util/pmu-bison.h: util/pmu.y $(Q)$(call echo-cmd,bison)$(BISON) -v $< -d $(PARSER_DEBUG_BISON) $(BISON_FILE_PREFIX_MAP) \ -o $(OUTPUT)util/pmu-bison.c -p perf_pmu_ -$(OUTPUT)util/bpf-filter-flex.c $(OUTPUT)util/bpf-filter-flex.h: util/bpf-filter.l $(OUTPUT)util/bpf-filter-bison.c +$(OUTPUT)util/bpf-filter-flex.c $(OUTPUT)util/bpf-filter-flex.h: util/bpf-filter.l $(OUTPUT)util/bpf-filter-bison.c util/bpf-filter.h util/bpf_skel/sample-filter.h $(call rule_mkdir) $(Q)$(call echo-cmd,flex)$(FLEX) -o $(OUTPUT)util/bpf-filter-flex.c \ --header-file=$(OUTPUT)util/bpf-filter-flex.h $(PARSER_DEBUG_FLEX) $< -$(OUTPUT)util/bpf-filter-bison.c $(OUTPUT)util/bpf-filter-bison.h: util/bpf-filter.y +$(OUTPUT)util/bpf-filter-bison.c $(OUTPUT)util/bpf-filter-bison.h: util/bpf-filter.y util/bpf-filter.h util/bpf_skel/sample-filter.h $(call rule_mkdir) $(Q)$(call echo-cmd,bison)$(BISON) -v $< -d $(PARSER_DEBUG_BISON) $(BISON_FILE_PREFIX_MAP) \ -o $(OUTPUT)util/bpf-filter-bison.c -p perf_bpf_filter_
Signature · 1 line
2.46.0.295.g3b9ea8a38a-goog
Namhyung Kim
maintainerPATCH 3/4 perf bpf-filter: Support filtering on cgroups
In reply to Namhyung KimThe new cgroup filter can take either of '==' or '!=' operator and a pathname for the target cgroup.
$ perf record -a --all-cgroups -e cycles --filter 'cgroup == /abc/def' -- sleep 1
Users should have --all-cgroups option in the command line to enable cgroup filtering. Technically it doesn't need to have the option as it can get the current task's cgroup info directly from BPF. But I want to follow the convention for the other sample info.
--- tools/perf/util/bpf-filter.c | 1 + tools/perf/util/bpf-filter.l | 28 ++++++++++++++++---- tools/perf/util/bpf-filter.y | 28 ++++++++++++++++++-- tools/perf/util/bpf_skel/sample-filter.h | 2 +- tools/perf/util/bpf_skel/sample_filter.bpf.c | 4 ++- tools/perf/util/bpf_skel/vmlinux/vmlinux.h | 1 + 6 files changed, 55 insertions(+), 9 deletions(-)
tools/perf/util/bpf-filter.c +1 -0
@@ -100,6 +100,7 @@ static const struct perf_sample_info {PERF_SAMPLE_TYPE (TRANSACTION ,"--transaction" ),PERF_SAMPLE_TYPE (CODE_PAGE_SIZE ,"--code-page-size" ),PERF_SAMPLE_TYPE (DATA_PAGE_SIZE ,"--data-page-size" ), + PERF_SAMPLE_TYPE(CGROUP, "--all-cgroups"), };static int get_pinned_fd (const char *name);
tools/perf/util/bpf-filter.l +23 -5
@@ -9,8 +9,11 @@ #include "bpf-filter.h" #include "bpf-filter-bison.h" +extern int perf_bpf_filter_needs_path; + static int sample(enum perf_bpf_filter_term term) { + perf_bpf_filter_needs_path = 0; perf_bpf_filter_lval.sample.term = term; perf_bpf_filter_lval.sample.part = 0; return BFT_SAMPLE;
@@ -18,11 +21,20 @@ static int sample(enum perf_bpf_filter_term term) static int sample_part(enum perf_bpf_filter_term term, int part) { + perf_bpf_filter_needs_path = 0; perf_bpf_filter_lval.sample.term = term; perf_bpf_filter_lval.sample.part = part; return BFT_SAMPLE; } +static int sample_path(enum perf_bpf_filter_term term) +{ + perf_bpf_filter_needs_path = 1; + perf_bpf_filter_lval.sample.term = term; + perf_bpf_filter_lval.sample.part = 0; + return BFT_SAMPLE_PATH; +} + static int operator(enum perf_bpf_filter_op op) { perf_bpf_filter_lval.op = op;
@@ -48,10 +60,15 @@ static int constant(int val) return BFT_NUM; } -static int error(const char *str) +static int path_or_error(void) { - printf("perf_bpf_filter: Unexpected filter %s: %s\n", str, perf_bpf_filter_text); - return BFT_ERROR; + if (!perf_bpf_filter_needs_path) { + printf("perf_bpf_filter: Error: Unexpected item: %s\n", + perf_bpf_filter_text); + return BFT_ERROR; + } + perf_bpf_filter_lval.path = perf_bpf_filter_text; + return BFT_PATH; } %}
@@ -59,6 +76,7 @@ static int error(const char *str) num_dec [0-9]+ num_hex 0[Xx][0-9a-fA-F]+ space [ \t]+ +path [^ \t\n]+ ident [_a-zA-Z][_a-zA-Z0-9]+ %%
@@ -97,6 +115,7 @@ mem_blk { return sample_part(PBF_TERM_DATA_SRC, 7); } mem_hops { return sample_part(PBF_TERM_DATA_SRC, 8); } uid { return sample(PBF_TERM_UID); } gid { return sample(PBF_TERM_GID); } +cgroup { return sample_path(PBF_TERM_CGROUP); } "==" { return operator(PBF_OP_EQ); } "!=" { return operator(PBF_OP_NEQ); }
@@ -155,7 +174,6 @@ hops3 { return constant(PERF_MEM_HOPS_3); } "," { return ','; } "||" { return BFT_LOGICAL_OR; } -{ident} { return error("ident"); } -. { return error("input"); } +{path} { return path_or_error(); } %%
tools/perf/util/bpf-filter.y +26 -2
@@ -12,9 +12,13 @@ #include <linux/compiler.h> #include <linux/list.h> #include "bpf-filter.h" +#include "cgroup.h" int perf_bpf_filter_lex(void); +/* To indicate if the current term needs a pathname or not */ +int perf_bpf_filter_needs_path; + static void perf_bpf_filter_error(struct list_head *expr __maybe_unused, char const *msg) {
@@ -26,6 +30,7 @@ static void perf_bpf_filter_error(struct list_head *expr __maybe_unused, %union { unsigned long num; + char *path; struct { enum perf_bpf_filter_term term; int part;
@@ -34,12 +39,13 @@ static void perf_bpf_filter_error(struct list_head *expr __maybe_unused, struct perf_bpf_filter_expr *expr; } -%token BFT_SAMPLE BFT_OP BFT_ERROR BFT_NUM BFT_LOGICAL_OR +%token BFT_SAMPLE BFT_SAMPLE_PATH BFT_OP BFT_ERROR BFT_NUM BFT_LOGICAL_OR BFT_PATH %type <expr> filter_term filter_expr %destructor { free ($$); } <expr> -%type <sample> BFT_SAMPLE +%type <sample> BFT_SAMPLE BFT_SAMPLE_PATH %type <op> BFT_OP %type <num> BFT_NUM +%type <path> BFT_PATH %%
@@ -81,5 +87,23 @@ BFT_SAMPLE BFT_OP BFT_NUM { $$ = perf_bpf_filter_expr__new($1.term, $1.part, $2, $3); } +| +BFT_SAMPLE_PATH BFT_OP BFT_PATH +{ + struct cgroup *cgrp; + unsigned long cgroup_id = 0; + + if ($2 != PBF_OP_EQ && $2 != PBF_OP_NEQ) { + printf("perf_bpf_filter: cgroup accepts '==' or '!=' only\n"); + YYERROR; + } + + cgrp = cgroup__new($3, /*do_open=*/false); + if (cgrp && read_cgroup_id(cgrp) == 0) + cgroup_id = cgrp->id; + + $$ = perf_bpf_filter_expr__new($1.term, $1.part, $2, cgroup_id); + cgroup__put(cgrp); +} %%
tools/perf/util/bpf_skel/sample-filter.h +1 -1
@@ -45,7 +45,7 @@ enum perf_bpf_filter_term { __PBF_UNUSED_TERM18 =PBF_TERM_SAMPLE_START +18 ,/* SAMPLE_REGS_INTR = 1U << 18 */ PBF_TERM_PHYS_ADDR =PBF_TERM_SAMPLE_START +19 ,/* SAMPLE_PHYS_ADDR = 1U << 19 */ __PBF_UNUSED_TERM20 =PBF_TERM_SAMPLE_START +20 ,/* SAMPLE_AUX = 1U << 20 */ - __PBF_UNUSED_TERM21 =PBF_TERM_SAMPLE_START +21 ,/* SAMPLE_CGROUP = 1U << 21 */ +PBF_TERM_CGROUP =PBF_TERM_SAMPLE_START +21 ,/* SAMPLE_CGROUP = 1U << 21 */ PBF_TERM_DATA_PAGE_SIZE =PBF_TERM_SAMPLE_START +22 ,/* SAMPLE_DATA_PAGE_SIZE = 1U << 22 */ PBF_TERM_CODE_PAGE_SIZE =PBF_TERM_SAMPLE_START +23 ,/* SAMPLE_CODE_PAGE_SIZE = 1U << 23 */ PBF_TERM_WEIGHT_STRUCT = PBF_TERM_SAMPLE_START + 24, /* SAMPLE_WEIGHT_STRUCT = 1U << 24 */
tools/perf/util/bpf_skel/sample_filter.bpf.c +3 -1
@@ -93,6 +93,7 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,BUILD_CHECK_SAMPLE (DATA_SRC );BUILD_CHECK_SAMPLE (TRANSACTION );BUILD_CHECK_SAMPLE (PHYS_ADDR ); +BUILD_CHECK_SAMPLE (CGROUP );BUILD_CHECK_SAMPLE (DATA_PAGE_SIZE );BUILD_CHECK_SAMPLE (CODE_PAGE_SIZE );BUILD_CHECK_SAMPLE (WEIGHT_STRUCT );
@@ -135,6 +136,8 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,return kctx->data->weight.full;case PBF_TERM_PHYS_ADDR :return kctx->data->phys_addr; +case PBF_TERM_CGROUP : +return kctx->data->cgroup;case PBF_TERM_CODE_PAGE_SIZE :return kctx->data->code_page_size;case PBF_TERM_DATA_PAGE_SIZE :
@@ -183,7 +186,6 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,case __PBF_UNUSED_TERM16:case __PBF_UNUSED_TERM18:case __PBF_UNUSED_TERM20: -case __PBF_UNUSED_TERM21:default :break ; }
tools/perf/util/bpf_skel/vmlinux/vmlinux.h +1 -0
@@ -171,6 +171,7 @@ struct perf_sample_data {u32 cpu; } cpu_entry;u64 phys_addr; +u64 cgroup;u64 data_page_size;u64 code_page_size; } __attribute__((__aligned__(64))) __attribute__((preserve_access_index));
Signature · 1 line
2.46.0.295.g3b9ea8a38a-goog
Namhyung Kim
maintainerPATCH 4/4 perf test: Add perf record cgroup filtering test
In reply to Namhyung Kim$ sudo ./perf test filtering -vv 96: perf record sample filtering (by BPF) tests: --- start --- test child forked, pid 2966908 Checking BPF-filter privilege Basic bpf-filter test Basic bpf-filter test [Success] Failing bpf-filter test Failing bpf-filter test [Success] Group bpf-filter test Group bpf-filter test [Success] Multiple bpf-filter test Multiple bpf-filter test [Success] Cgroup bpf-filter test Cgroup bpf-filter test [Success] ---- end(0) ---- 96: perf record sample filtering (by BPF) tests : Ok
--- tools/perf/tests/shell/record_bpf_filter.sh | 39 +++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-)
tools/perf/tests/shell/record_bpf_filter.sh +36 -3
@@ -68,7 +68,7 @@ test_bpf_filter_fail() { # 'cpu' requires PERF_SAMPLE_CPU flag if ! perf record -e task-clock --filter 'cpu > 0' \ - -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU + -o /dev/null true 2>&1 | grep -q PERF_SAMPLE_CPU then echo "Failing bpf-filter test [Failed forbidden CPU]" err=1
@@ -98,7 +98,7 @@ test_bpf_filter_group() { fi if ! perf record -e task-clock --filter 'cpu > 0 || ip > 0' \ - -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU + -o /dev/null true 2>&1 | grep -q PERF_SAMPLE_CPU then echo "Group bpf-filter test [Failed forbidden CPU]" err=1
@@ -106,7 +106,7 @@ test_bpf_filter_group() { fi if ! perf record -e task-clock --filter 'period > 0 || code_pgsz > 4096' \ - -o /dev/null true 2>&1 | grep PERF_SAMPLE_CODE_PAGE_SIZE + -o /dev/null true 2>&1 | grep -q PERF_SAMPLE_CODE_PAGE_SIZE then echo "Group bpf-filter test [Failed forbidden CODE_PAGE_SIZE]" err=1
@@ -147,6 +147,35 @@ test_bpf_filter_multi() { echo "Multiple bpf-filter test [Success]" } +test_bpf_filter_cgroup() { + echo "Cgroup bpf-filter test" + + if ! perf record -e task-clock --filter 'cgroup == /' \ + -a --all-cgroups --synth=cgroup -o "${perfdata}" true 2> /dev/null + then + echo "Cgroup bpf-filter test [Skipped cgroup not supported]" + return + fi + + # 'cgroup' requires PERF_SAMPLE_CGROUP flag + if ! perf record -e task-clock --filter 'cgroup == /' \ + -o /dev/null true 2>&1 | grep -q PERF_SAMPLE_CGROUP + then + echo "Cgroup bpf-filter test [Failed CGROUP requires --all-cgroups]" + err=1 + return + fi + + if ! perf report -i "${perfdata}" -s cgroup -q | grep -q -F '100.00%' + then + echo "Cgroup bpf-filter test [Failed root cgroup does not have 100%]" + err=1 + return + fi + + echo "Cgroup bpf-filter test [Success]" +} + test_bpf_filter_priv if [ $err = 0 ]; then
@@ -165,5 +194,9 @@ if [ $err = 0 ]; then test_bpf_filter_multi fi +if [ $err = 0 ]; then + test_bpf_filter_cgroup +fi + cleanup exit $err
Signature · 1 line
2.46.0.295.g3b9ea8a38a-goog