Move uid filtering to BPF filters
Subsystems (2)
Ian Rogers
reviewerv3 cover Move uid filtering to BPF filters
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in thread_map is racy as the PID may exit before the perf_event_open causing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.
v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.
v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.
v1: https://lore.kernel.org/lkml/[email protected]/
Ian Rogers (10):
perf parse-events filter: Use evsel__find_pmu
perf target: Separate parse_uid into its own function perf parse-events: Add parse_uid_filter helper
perf record: Switch user option to use BPF filter
perf tests record: Add basic uid filtering test
perf top: Switch user option to use BPF filter
perf trace: Switch user option to use BPF filter
perf bench evlist-open-close: Switch user option to use BPF filter perf target: Remove uid from target
perf thread_map: Remove uid options
tools/perf/bench/evlist-open-close.c | 36 ++++++++------ tools/perf/builtin-ftrace.c | 1 - tools/perf/builtin-kvm.c | 2 - tools/perf/builtin-record.c | 27 ++++++----- tools/perf/builtin-stat.c | 4 +- tools/perf/builtin-top.c | 22 +++++---- tools/perf/builtin-trace.c | 27 +++++++---- tools/perf/tests/backward-ring-buffer.c | 1 - tools/perf/tests/event-times.c | 8 ++- tools/perf/tests/keep-tracking.c | 2 +- tools/perf/tests/mmap-basic.c | 2 +- tools/perf/tests/openat-syscall-all-cpus.c | 2 +- tools/perf/tests/openat-syscall-tp-fields.c | 1 - tools/perf/tests/openat-syscall.c | 2 +- tools/perf/tests/perf-record.c | 1 - tools/perf/tests/perf-time-to-tsc.c | 2 +- tools/perf/tests/shell/record.sh | 26 ++++++++++ tools/perf/tests/switch-tracking.c | 2 +- tools/perf/tests/task-exit.c | 1 - tools/perf/tests/thread-map.c | 2 +- tools/perf/util/bpf-filter.c | 2 +- tools/perf/util/evlist.c | 3 +- tools/perf/util/parse-events.c | 33 ++++++++----- tools/perf/util/parse-events.h | 1 + tools/perf/util/python.c | 10 ++-- tools/perf/util/target.c | 54 +++------------------ tools/perf/util/target.h | 15 ++---- tools/perf/util/thread_map.c | 32 ++---------- tools/perf/util/thread_map.h | 6 +-- tools/perf/util/top.c | 4 +- tools/perf/util/top.h | 1 + 31 files changed, 150 insertions(+), 182 deletions(-)
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 1/10 perf parse-events filter: Use evsel__find_pmu
Rather than manually scanning PMUs, use evsel__find_pmu that can use the PMU set during event parsing.
--- tools/perf/util/parse-events.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
tools/perf/util/parse-events.c +4 -10
@@ -2463,9 +2463,8 @@ foreach_evsel_in_last_glob(struct evlist *evlist,static int set_filter (struct evsel *evsel,const void *arg) {const char *str = arg; -bool found = false;int nr_addr_filters =0 ; -struct perf_pmu *pmu =NULL ; +struct perf_pmu *pmu;if (evsel ==NULL ) { fprintf(stderr,
@@ -2483,16 +2482,11 @@ static int set_filter(struct evsel *evsel, const void *arg)return 0 ; } -while ((pmu =perf_pmus__scan (pmu)) !=NULL ) -if (pmu->type == evsel->core.attr.type) { - found = true; -break ; - } - -if (found) + pmu =evsel__find_pmu (evsel); +if (pmu) {perf_pmu__scan_file (pmu,"nr_addr_filters" ,"%d" , &nr_addr_filters); - + }if (!nr_addr_filters)return perf_bpf_filter__parse (&evsel->bpf_filters, str);
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 2/10 perf target: Separate parse_uid into its own function
In reply to Ian RogersAllow parse_uid to be called without a struct target. Rather than have two errors, remove TARGET_ERRNO__USER_NOT_FOUND and use TARGET_ERRNO__INVALID_UID as the handling is identical.
--- tools/perf/util/target.c | 22 ++++++++++++---------- tools/perf/util/target.h | 3 ++- 2 files changed, 14 insertions(+), 11 deletions(-)
tools/perf/util/target.c +12 -10
@@ -94,15 +94,13 @@ enum target_errno target__validate(struct target *target)return ret; } -enum target_errno target__parse_uid (struct target *target) +uid_t parse_uid (const char *str) {struct passwd pwd, *result;char buf[1024 ]; -const char *str = target->uid_str; - target->uid =UINT_MAX ;if (str ==NULL ) -return TARGET_ERRNO__SUCCESS ; +return UINT_MAX ;/* Try user name first */ getpwnam_r (str, &pwd, buf,sizeof (buf), &result);
@@ -115,16 +113,22 @@ enum target_errno target__parse_uid(struct target *target)int uid =strtol (str, &endptr,10 );if (*endptr !='\0' ) -return TARGET_ERRNO__INVALID_UID ; +return UINT_MAX ;getpwuid_r (uid, &pwd, buf,sizeof (buf), &result);if (result ==NULL ) -return TARGET_ERRNO__USER_NOT_FOUND ; +return UINT_MAX ; } - target->uid = result->pw_uid; -return TARGET_ERRNO__SUCCESS ; +return result->pw_uid; +} + +enum target_errno target__parse_uid (struct target *target) +{ + target->uid =parse_uid (target->uid_str); + +return target->uid !=UINT_MAX ?TARGET_ERRNO__SUCCESS :TARGET_ERRNO__INVALID_UID ; }/*
@@ -142,7 +146,6 @@ static const char *target__error_str[] = {"BPF switch overriding UID" ,"BPF switch overriding THREAD" , "Invalid User: %s", - "Problems obtaining information for user %s", }; int target__strerror(struct target *target, int errnum,
@@ -171,7 +174,6 @@ int target__strerror(struct target *target, int errnum,break ;case TARGET_ERRNO__INVALID_UID : -case TARGET_ERRNO__USER_NOT_FOUND :snprintf (buf, buflen, msg, target->uid_str);break ;
tools/perf/util/target.h +2 -1
@@ -48,12 +48,13 @@ enum target_errno {/* for target__parse_uid() */ TARGET_ERRNO__INVALID_UID , -TARGET_ERRNO__USER_NOT_FOUND , __TARGET_ERRNO__END, };enum target_errno target__validate (struct target *target); + +uid_t parse_uid (const char *str);enum target_errno target__parse_uid (struct target *target);int target__strerror (struct target *target,int errnum,char *buf,size_t buflen);
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 3/10 perf parse-events: Add parse_uid_filter helper
In reply to Ian RogersAdd parse_uid_filter filter as a helper to parse_filter, that constructs a uid filter string. As uid filters don't work with tracepoint filters, add a is_possible_tp_filter function so the tracepoint filter isn't attempted for tracepoint evsels.
--- tools/perf/util/parse-events.c | 19 ++++++++++++++++++- tools/perf/util/parse-events.h | 1 + 2 files changed, 19 insertions(+), 1 deletion(-)
tools/perf/util/parse-events.c +18 -1
@@ -2460,6 +2460,12 @@ foreach_evsel_in_last_glob(struct evlist *evlist,return 0 ; } +/* Will a tracepoint filter work for str or should a BPF filter be used? */ +static bool is_possible_tp_filter (const char *str) +{ +return strstr (str,"uid" ) ==NULL ; +} +static int set_filter (struct evsel *evsel,const void *arg) {const char *str = arg;
@@ -2472,7 +2478,7 @@ static int set_filter(struct evsel *evsel, const void *arg)return -1 ; } -if (evsel->core.attr.type ==PERF_TYPE_TRACEPOINT ) { +if (evsel->core.attr.type ==PERF_TYPE_TRACEPOINT &&is_possible_tp_filter (str)) {if (evsel__append_tp_filter (evsel, str) <0 ) {fprintf (stderr,"not enough memory to hold filter string\n" );
@@ -2508,6 +2514,17 @@ int parse_filter(const struct option *opt, const char *str, (const void *)str); } +int parse_uid_filter (struct evlist *evlist,uid_t uid) +{ +struct option opt = { + .value = &evlist, + }; +char buf[128 ]; + +snprintf (buf,sizeof (buf),"uid == %d" , uid); +return parse_filter (&opt, buf,/*unset=*/ 0 ); +} +static int add_exclude_perf_filter (struct evsel *evsel,const void *arg__maybe_unused ) {
tools/perf/util/parse-events.h +1 -0
@@ -45,6 +45,7 @@ static inline int parse_events(struct evlist *evlist, const char *str,int parse_event (struct evlist *evlist,const char *str);int parse_filter (const struct option *opt,const char *str,int unset); +int parse_uid_filter (struct evlist *evlist,uid_t uid);int exclude_perf (const struct option *opt,const char *arg,int unset);enum parse_events__term_val_type {
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 4/10 perf record: Switch user option to use BPF filter
In reply to Ian RogersThe existing --uid option finds user processes by scanning /proc. Scanning /proc is inherently racy due to processes being in /proc but then exiting before perf does the perf_event_open. When the perf_event_open fails perf will terminate reporting errors which is a disappointing user experience. Scanning /proc when perf starts also cannot inform perf of new user processes starting.
The ability to filter perf events with BPF isn't new, and has been in the perf tool for 10 years:
https://lore.kernel.org/all/[email protected]/
An ability to do filtering on the command line with a BPF program that's part of perf was added 2 years ago:
https://lore.kernel.org/all/[email protected]/
This was then extended to support uids as a way of filtering:
https://lore.kernel.org/all/[email protected]/
This change switches the perf record --uid option to use the BPF filter code to avoid the inherent race and existing failures. To ensure all processes are considered by the filter, the change forces system-wide mode.
Using BPF has permission issues in loading the BPF program not present in scanning /proc. As the scanning approach would miss new programs and fail due to the race, this is considered preferable. The change also avoids opening a perf event per PID, which is less overhead in the kernel.
--- tools/perf/builtin-record.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
tools/perf/builtin-record.c +16 -11
@@ -174,6 +174,7 @@ struct record {bool timestamp_boundary;bool off_cpu;const char *filter_action; +const char *uid_str;struct switch_output switch_output;unsigned long long samples;unsigned long output_max_size;/* = 0: unlimited */
@@ -3465,8 +3466,7 @@ static struct option __record_options[] = { "or ranges of time to enable events e.g. '-D 10-20,30-40'", record__parse_event_enable_time),OPT_BOOLEAN (0 ,"kcore" , &record.opts.kcore,"copy /proc/kcore" ), -OPT_STRING ('u' ,"uid" , &record.opts.target.uid_str,"user" , -"user to profile" ), +OPT_STRING ('u' ,"uid" , &record.uid_str,"user" ,"user to profile" ), OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, "branch any", "sample any taken branches",
@@ -4205,19 +4205,24 @@ int cmd_record(int argc, const char **argv)ui__warning ("%s\n" , errbuf); } - err =target__parse_uid (&rec->opts.target); -if (err) { -int saved_errno = errno; +if (rec->uid_str) { +uid_t uid =parse_uid (rec->uid_str); -target__strerror (&rec->opts.target, err, errbuf,BUFSIZ ); -ui__error ("%s" , errbuf); +if (uid ==UINT_MAX ) { +ui__error ("Invalid User: %s" , rec->uid_str); + err = -EINVAL ; + goto out; + } + err =parse_uid_filter (rec->evlist, uid); +if (err) + goto out; - err = -saved_errno; - goto out; +/* User ID filtering implies system wide. */ + rec->opts.target.system_wide = true; } -/* Enable ignoring missing threads when -u/-p option is defined. */ - rec->opts.ignore_missing_thread = rec->opts.target.uid !=UINT_MAX || rec->opts.target.pid; +/* Enable ignoring missing threads when -p option is defined. */ + rec->opts.ignore_missing_thread = rec->opts.target.pid;evlist__warn_user_requested_cpus (rec->evlist, rec->opts.target.cpu_list);
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 5/10 perf tests record: Add basic uid filtering test
In reply to Ian RogersNow the uid option doesn't race and fail, add a test of it. The test is based on the system-wide test with changes around how failure is handled - BPF permissions are a bigger issue than perf event paranoia.
--- tools/perf/tests/shell/record.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
tools/perf/tests/shell/record.sh +26 -0
@@ -231,6 +231,31 @@ test_cgroup() { echo "Cgroup sampling test [Success]" } +test_uid() { + echo "Uid sampling test" + if ! perf record -aB --synth=no --uid "$(id -u)" -o "${perfdata}" ${testprog} \ + > "${script_output}" 2>&1 + then + if grep -q "libbpf.*EPERM" "${script_output}" + then + echo "Uid sampling [Skipped permissions]" + return + else + echo "Uid sampling [Failed to record]" + err=1 + # cat "${script_output}" + return + fi + fi + if ! perf report -i "${perfdata}" -q | grep -q "${testsym}" + then + echo "Uid sampling [Failed missing output]" + err=1 + return + fi + echo "Uid sampling test [Success]" +} + test_leader_sampling() { echo "Basic leader sampling test" if ! perf record -o "${perfdata}" -e "{cycles,cycles}:Su" -- \
@@ -324,6 +349,7 @@ test_system_wide test_workload test_branch_counter test_cgroup +test_uid test_leader_sampling test_topdown_leader_sampling test_precise_max
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 6/10 perf top: Switch user option to use BPF filter
In reply to Ian RogersThe existing --uid option finds user processes by scanning /proc. Scanning /proc is inherently racy due to processes being in /proc but then exiting before perf does the perf_event_open. When the perf_event_open fails perf will terminate reporting errors which is a disappointing user experience. Scanning /proc when perf starts also cannot inform perf of new user processes starting.
The ability to filter perf events with BPF isn't new, and has been in the perf tool for 10 years:
https://lore.kernel.org/all/[email protected]/
An ability to do filtering on the command line with a BPF program that's part of perf was added 2 years ago:
https://lore.kernel.org/all/[email protected]/
This was then extended to support uids as a way of filtering:
https://lore.kernel.org/all/[email protected]/
This change switches the perf top --uid option to use the BPF filter code to avoid the inherent race and existing failures.
Using BPF has permission issues in loading the BPF program not present in scanning /proc. As the scanning approach would miss new programs and fail due to the race, this is considered preferable. The change also avoids opening a perf event per PID, which is less overhead in the kernel.
--- tools/perf/builtin-top.c | 22 ++++++++++++---------- tools/perf/util/top.c | 4 ++-- tools/perf/util/top.h | 1 + 3 files changed, 15 insertions(+), 12 deletions(-)
tools/perf/builtin-top.c +12 -10
@@ -642,7 +642,7 @@ static void *display_thread_tui(void *arg)*/ evlist__for_each_entry (top->evlist, pos) {struct hists *hists =evsel__hists (pos); - hists->uid_filter_str = top->record_opts.target.uid_str; + hists->uid_filter_str = top->uid_str; } ret = evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent,
@@ -1566,7 +1566,7 @@ int cmd_top(int argc, const char **argv) "Add prefix to source file path names in programs (with --prefix-strip)"),OPT_STRING (0 ,"prefix-strip" , &annotate_opts.prefix_strip,"N" ,"Strip first N entries of source file path name in programs (with --prefix)" ), -OPT_STRING ('u' ,"uid" , &target->uid_str,"user" ,"user to profile" ), +OPT_STRING ('u' ,"uid" , &top.uid_str,"user" ,"user to profile" ),OPT_CALLBACK (0 ,"percent-limit" , &top,"percent" ,"Don't show entries under that percent" , parse_percent_limit), OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
@@ -1757,15 +1757,17 @@ int cmd_top(int argc, const char **argv)ui__warning ("%s\n" , errbuf); } - status =target__parse_uid (target); -if (status) { -int saved_errno = errno; - -target__strerror (target, status, errbuf,BUFSIZ ); -ui__error ("%s\n" , errbuf); +if (top.uid_str) { +uid_t uid =parse_uid (top.uid_str); - status = -saved_errno; - goto out_delete_evlist; +if (uid ==UINT_MAX ) { +ui__error ("Invalid User: %s" , top.uid_str); + status = -EINVAL ; + goto out_delete_evlist; + } + status =parse_uid_filter (top.evlist, uid); +if (status) + goto out_delete_evlist; }if (target__none (target))
tools/perf/util/top.c +2 -2
@@ -88,9 +88,9 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)else if (target->tid) ret +=SNPRINTF (bf + ret, size - ret," (target_tid: %s" , target->tid); -else if (target->uid_str !=NULL ) +else if (top->uid_str !=NULL ) ret +=SNPRINTF (bf + ret, size - ret," (uid: %s" , - target->uid_str); + top->uid_str);else ret +=SNPRINTF (bf + ret, size - ret," (all" );
tools/perf/util/top.h +1 -0
@@ -48,6 +48,7 @@ struct perf_top {const char *sym_filter;float min_percent;unsigned int nr_threads_synthesize; +const char *uid_str;struct {struct ordered_events *in;
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 7/10 perf trace: Switch user option to use BPF filter
In reply to Ian RogersThe existing --uid option finds user processes by scanning /proc. Scanning /proc is inherently racy due to processes being in /proc but then exiting before perf does the perf_event_open. When the perf_event_open fails perf will terminate reporting errors which is a disappointing user experience. Scanning /proc when perf starts also cannot inform perf of new user processes starting.
The ability to filter perf events with BPF isn't new, and has been in the perf tool for 10 years:
https://lore.kernel.org/all/[email protected]/
An ability to do filtering on the command line with a BPF program that's part of perf was added 2 years ago:
https://lore.kernel.org/all/[email protected]/
This was then extended to support uids as a way of filtering:
https://lore.kernel.org/all/[email protected]/
This change switches the perf trace --uid option to use the BPF filter code to avoid the inherent race and existing failures. To ensure all processes are considered by the filter, the change forces system-wide mode.
Using BPF has permission issues in loading the BPF program not present in scanning /proc. As the scanning approach would miss new programs and fail due to the race, this is considered preferable. The change also avoids opening a perf event per PID, which is less overhead in the kernel.
--- tools/perf/builtin-trace.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
tools/perf/builtin-trace.c +17 -9
@@ -240,6 +240,7 @@ struct trace {struct ordered_events data;u64 last; } oe; +const char *uid_str; };static void trace__load_vmlinux_btf (struct trace *trace__maybe_unused )
@@ -4401,8 +4402,8 @@ static int trace__run(struct trace *trace, int argc, const char **argv)evlist__add (evlist, pgfault_min); } -/* Enable ignoring missing threads when -u/-p option is defined. */ - trace->opts.ignore_missing_thread = trace->opts.target.uid !=UINT_MAX || trace->opts.target.pid; +/* Enable ignoring missing threads when -p option is defined. */ + trace->opts.ignore_missing_thread = trace->opts.target.pid;if (trace->sched &&evlist__add_newtp (evlist,"sched" ,"sched_stat_runtime" , trace__sched_stat_runtime))
@@ -5420,8 +5421,7 @@ int cmd_trace(int argc, const char **argv) "child tasks do not inherit counters"),OPT_CALLBACK ('m' ,"mmap-pages" , &trace.opts.mmap_pages,"pages" ,"number of mmap data pages" , evlist__parse_mmap_pages), -OPT_STRING ('u' ,"uid" , &trace.opts.target.uid_str,"user" , -"user to profile" ), +OPT_STRING ('u' ,"uid" , &trace.uid_str,"user" ,"user to profile" ),OPT_CALLBACK (0 ,"duration" , &trace,"float" ,"show only events with duration > N.M ms" , trace__set_duration),
@@ -5762,11 +5762,19 @@ int cmd_trace(int argc, const char **argv) goto out_close; } - err =target__parse_uid (&trace.opts.target); -if (err) { -target__strerror (&trace.opts.target, err, bf,sizeof (bf)); -fprintf (trace.output,"%s" , bf); - goto out_close; +if (trace.uid_str) { +uid_t uid =parse_uid (trace.uid_str); + +if (uid ==UINT_MAX ) { +ui__error ("Invalid User: %s" , trace.uid_str); + err = -EINVAL ; + goto out_close; + } + err =parse_uid_filter (trace.evlist, uid); +if (err) + goto out_close; + + trace.opts.target.system_wide = true; }if (!argc &&target__none (&trace.opts.target))
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 8/10 perf bench evlist-open-close: Switch user option to use BPF filter
In reply to Ian RogersThe existing --uid option finds user processes by scanning /proc. Scanning /proc is inherently racy due to processes being in /proc but then exiting before perf does the perf_event_open. When the perf_event_open fails perf will terminate reporting errors which is a disappointing user experience. Scanning /proc when perf starts also cannot inform perf of new user processes starting.
The ability to filter perf events with BPF isn't new, and has been in the perf tool for 10 years:
https://lore.kernel.org/all/[email protected]/
An ability to do filtering on the command line with a BPF program that's part of perf was added 2 years ago:
https://lore.kernel.org/all/[email protected]/
This was then extended to support uids as a way of filtering:
https://lore.kernel.org/all/[email protected]/
This change switches the benchmark's --uid option to use the BPF filter code to avoid the inherent race and existing failures.
Using BPF has permission issues in loading the BPF program not present in scanning /proc. As the scanning approach would miss new programs and fail due to the race, this is considered preferable. The change also avoids opening a perf event per PID, which is less overhead in the kernel.
--- tools/perf/bench/evlist-open-close.c | 36 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-)
tools/perf/bench/evlist-open-close.c +21 -15
@@ -57,7 +57,7 @@ static int evlist__count_evsel_fds(struct evlist *evlist)return cnt; } -static struct evlist *bench__create_evlist (char *evstr) +static struct evlist *bench__create_evlist (char *evstr,const char *uid_str) {struct parse_events_error err;struct evlist *evlist =evlist__new ();
@@ -78,6 +78,18 @@ static struct evlist *bench__create_evlist(char *evstr) goto out_delete_evlist; }parse_events_error__exit (&err); +if (uid_str) { +uid_t uid =parse_uid (uid_str); + +if (uid ==UINT_MAX ) { +pr_err ("Invalid User: %s" , uid_str); + ret = -EINVAL ; + goto out_delete_evlist; + } + ret =parse_uid_filter (evlist, uid); +if (ret) + goto out_delete_evlist; + } ret =evlist__create_maps (evlist, &opts.target);if (ret <0 ) {pr_err ("Not enough memory to create thread/cpu maps\n" );
@@ -117,10 +129,10 @@ static int bench__do_evlist_open_close(struct evlist *evlist)return 0 ; } -static int bench_evlist_open_close__run (char *evstr) +static int bench_evlist_open_close__run (char *evstr,const char *uid_str) {// used to print statistics only -struct evlist *evlist =bench__create_evlist (evstr); +struct evlist *evlist =bench__create_evlist (evstr, uid_str);double time_average, time_stddev;struct timeval start, end, diff;struct stats time_stats;
@@ -142,7 +154,7 @@ static int bench_evlist_open_close__run(char *evstr)for (i =0 ; i < iterations; i++) {pr_debug ("Started iteration %d\n" , i); - evlist =bench__create_evlist (evstr); + evlist =bench__create_evlist (evstr, uid_str);if (!evlist)return -ENOMEM ;
@@ -206,6 +218,7 @@ static char *bench__repeat_event_string(const char *evstr, int n)int bench_evlist_open_close (int argc,const char **argv) { +const char *uid_str =NULL ;const struct option options[] = {OPT_STRING ('e' ,"event" , &event_string,"event" ,"event selector. use 'perf list' to list available events" ),
@@ -221,7 +234,7 @@ int bench_evlist_open_close(int argc, const char **argv) "record events on existing process id"),OPT_STRING ('t' ,"tid" , &opts.target.tid,"tid" ,"record events on existing thread id" ), -OPT_STRING ('u' ,"uid" , &opts.target.uid_str,"user" ,"user to profile" ), +OPT_STRING ('u' ,"uid" , &uid_str,"user" ,"user to profile" ),OPT_BOOLEAN (0 ,"per-thread" , &opts.target.per_thread,"use per-thread mmaps" ), OPT_END() };
@@ -245,15 +258,8 @@ int bench_evlist_open_close(int argc, const char **argv) goto out; } - err =target__parse_uid (&opts.target); -if (err) { -target__strerror (&opts.target, err, errbuf,sizeof (errbuf)); -pr_err ("%s" , errbuf); - goto out; - } - -/* Enable ignoring missing threads when -u/-p option is defined. */ - opts.ignore_missing_thread = opts.target.uid !=UINT_MAX || opts.target.pid; +/* Enable ignoring missing threads when -p option is defined. */ + opts.ignore_missing_thread = opts.target.pid; evstr =bench__repeat_event_string (event_string, nr_events);if (!evstr) {
@@ -261,7 +267,7 @@ int bench_evlist_open_close(int argc, const char **argv) goto out; } - err =bench_evlist_open_close__run (evstr); + err =bench_evlist_open_close__run (evstr, uid_str);free (evstr); out:
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 9/10 perf target: Remove uid from target
In reply to Ian RogersGathering threads with a uid by scanning /proc is inherently racy leading to perf_event_open failures that quit perf. It also misses new processes a user starts. All users of the target functionality now use BPF filters, so remove uid and uid_str from target.
--- tools/perf/builtin-ftrace.c | 1 - tools/perf/builtin-kvm.c | 2 - tools/perf/builtin-stat.c | 4 +- tools/perf/builtin-trace.c | 1 - tools/perf/tests/backward-ring-buffer.c | 1 - tools/perf/tests/event-times.c | 4 +- tools/perf/tests/openat-syscall-tp-fields.c | 1 - tools/perf/tests/perf-record.c | 1 - tools/perf/tests/task-exit.c | 1 - tools/perf/util/bpf-filter.c | 2 +- tools/perf/util/evlist.c | 3 +- tools/perf/util/target.c | 46 +-------------------- tools/perf/util/target.h | 12 +----- 13 files changed, 6 insertions(+), 73 deletions(-)
tools/perf/builtin-ftrace.c +0 -1
@@ -1590,7 +1590,6 @@ int cmd_ftrace(int argc, const char **argv)int (*cmd_func)(struct perf_ftrace *) =NULL ;struct perf_ftrace ftrace = { .tracer =DEFAULT_TRACER , - .target = { .uid =UINT_MAX , }, };const struct option common_options[] = { OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
tools/perf/builtin-kvm.c +0 -2
@@ -1871,8 +1871,6 @@ static int kvm_events_live(struct perf_kvm_stat *kvm, kvm->opts.user_interval =1 ; kvm->opts.mmap_pages =512 ; kvm->opts.target.uses_mmap = false; - kvm->opts.target.uid_str =NULL ; - kvm->opts.target.uid =UINT_MAX ;symbol__init (NULL );disable_buildid_cache ();
tools/perf/builtin-stat.c +1 -3
@@ -108,9 +108,7 @@ static struct parse_events_option_args parse_events_option_args = {static bool all_counters_use_bpf = true; -static struct target target = { - .uid =UINT_MAX , -}; +static struct target target;static volatile sig_atomic_t child_pid =-1 ;static int detailed_run =0 ;
tools/perf/builtin-trace.c +0 -1
@@ -5374,7 +5374,6 @@ int cmd_trace(int argc, const char **argv)struct trace trace = { .opts = { .target = { - .uid =UINT_MAX , .uses_mmap = true, }, .user_freq =UINT_MAX ,
tools/perf/tests/backward-ring-buffer.c +0 -1
@@ -91,7 +91,6 @@ static int test__backward_ring_buffer(struct test_suite *test __maybe_unused, instruct parse_events_error parse_error;struct record_opts opts = { .target = { - .uid =UINT_MAX , .uses_mmap = true, }, .freq =0 ,
tools/perf/tests/event-times.c +1 -3
@@ -17,9 +17,7 @@static int attach__enable_on_exec (struct evlist *evlist) {struct evsel *evsel =evlist__last (evlist); -struct target target = { - .uid =UINT_MAX , - }; +struct target target = {};const char *argv[] = {"true" ,NULL , };char sbuf[STRERR_BUFSIZE ];int err;
tools/perf/tests/openat-syscall-tp-fields.c +0 -1
@@ -28,7 +28,6 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused {struct record_opts opts = { .target = { - .uid =UINT_MAX , .uses_mmap = true, }, .no_buffering = true,
tools/perf/tests/perf-record.c +0 -1
@@ -45,7 +45,6 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest {struct record_opts opts = { .target = { - .uid =UINT_MAX , .uses_mmap = true, }, .no_buffering = true,
tools/perf/tests/task-exit.c +0 -1
@@ -46,7 +46,6 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _struct evsel *evsel;struct evlist *evlist;struct target target = { - .uid =UINT_MAX , .uses_mmap = true, };const char *argv[] = {"true" ,NULL };
tools/perf/util/bpf-filter.c +1 -1
@@ -449,7 +449,7 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)struct bpf_program *prog;struct bpf_link *link;struct perf_bpf_filter_entry *entry; -bool needs_idx_hash = !target__has_cpu (target) && !target->uid_str; +bool needs_idx_hash = !target__has_cpu (target); entry =calloc (MAX_FILTERS ,sizeof (*entry));if (entry ==NULL )
tools/perf/util/evlist.c +1 -2
@@ -1005,8 +1005,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target)* per-thread data. thread_map__new_str will call * thread_map__new_all_cpus to enumerate all threads. */ - threads =thread_map__new_str (target->pid, target->tid, target->uid, - all_threads); + threads =thread_map__new_str (target->pid, target->tid,UINT_MAX , all_threads);if (!threads)return -1 ;
tools/perf/util/target.c +1 -45
88 lines not on this page · raw message
tools/perf/util/target.h +1 -11
@@ -9,9 +9,7 @@ struct target {const char *pid;const char *tid;const char *cpu_list; -const char *uid_str;const char *bpf_str; -uid_t uid;bool system_wide;bool uses_mmap;bool default_per_cpu;
@@ -36,32 +34,24 @@ enum target_errno {/* for target__validate() */ TARGET_ERRNO__PID_OVERRIDE_CPU = __TARGET_ERRNO__START, -TARGET_ERRNO__PID_OVERRIDE_UID , -TARGET_ERRNO__UID_OVERRIDE_CPU ,TARGET_ERRNO__PID_OVERRIDE_SYSTEM , -TARGET_ERRNO__UID_OVERRIDE_SYSTEM ,TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD ,TARGET_ERRNO__BPF_OVERRIDE_CPU ,TARGET_ERRNO__BPF_OVERRIDE_PID , -TARGET_ERRNO__BPF_OVERRIDE_UID ,TARGET_ERRNO__BPF_OVERRIDE_THREAD , -/* for target__parse_uid() */ -TARGET_ERRNO__INVALID_UID , - __TARGET_ERRNO__END, };enum target_errno target__validate (struct target *target);uid_t parse_uid (const char *str); -enum target_errno target__parse_uid (struct target *target);int target__strerror (struct target *target,int errnum,char *buf,size_t buflen);static inline bool target__has_task (struct target *target) { -return target->tid || target->pid || target->uid_str; +return target->tid || target->pid; }static inline bool target__has_cpu (struct target *target)
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerPATCH v3 10/10 perf thread_map: Remove uid options
In reply to Ian RogersNow the target doesn't have a uid, it is handled through BPF filters, remove the uid options to thread_map creation. Tidy up the functions used in tests to avoid passing unused arguments.
--- tools/perf/tests/event-times.c | 4 +-- tools/perf/tests/keep-tracking.c | 2 +- tools/perf/tests/mmap-basic.c | 2 +- tools/perf/tests/openat-syscall-all-cpus.c | 2 +- tools/perf/tests/openat-syscall.c | 2 +- tools/perf/tests/perf-time-to-tsc.c | 2 +- tools/perf/tests/switch-tracking.c | 2 +- tools/perf/tests/thread-map.c | 2 +- tools/perf/util/evlist.c | 2 +- tools/perf/util/python.c | 10 +++---- tools/perf/util/thread_map.c | 32 ++-------------------- tools/perf/util/thread_map.h | 6 ++-- 12 files changed, 20 insertions(+), 48 deletions(-)
tools/perf/tests/event-times.c +2 -2
@@ -62,7 +62,7 @@ static int attach__current_disabled(struct evlist *evlist)pr_debug ("attaching to current thread as disabled\n" ); - threads =thread_map__new (-1 ,getpid (),UINT_MAX ); + threads =thread_map__new_by_tid (getpid ());if (threads ==NULL ) {pr_debug ("thread_map__new\n" );return -1 ;
@@ -88,7 +88,7 @@ static int attach__current_enabled(struct evlist *evlist)pr_debug ("attaching to current thread as enabled\n" ); - threads =thread_map__new (-1 ,getpid (),UINT_MAX ); + threads =thread_map__new_by_tid (getpid ());if (threads ==NULL ) {pr_debug ("failed to call thread_map__new\n" );return -1 ;
tools/perf/tests/keep-tracking.c +1 -1
@@ -78,7 +78,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subteint found, err =-1 ;const char *comm; - threads =thread_map__new (-1 ,getpid (),UINT_MAX ); + threads =thread_map__new_by_tid (getpid ());CHECK_NOT_NULL__ (threads); cpus =perf_cpu_map__new_online_cpus ();
tools/perf/tests/mmap-basic.c +1 -1
@@ -46,7 +46,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtestchar sbuf[STRERR_BUFSIZE ];struct mmap *md; - threads =thread_map__new (-1 ,getpid (),UINT_MAX ); + threads =thread_map__new_by_tid (getpid ());if (threads ==NULL ) {pr_debug ("thread_map__new\n" );return -1 ;
tools/perf/tests/openat-syscall-all-cpus.c +1 -1
@@ -28,7 +28,7 @@ static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybstruct evsel *evsel;unsigned int nr_openat_calls =111 , i;cpu_set_t cpu_set; -struct perf_thread_map *threads =thread_map__new (-1 ,getpid (),UINT_MAX ); +struct perf_thread_map *threads =thread_map__new_by_tid (getpid ());char sbuf[STRERR_BUFSIZE ];char errbuf[BUFSIZ ];
tools/perf/tests/openat-syscall.c +1 -1
@@ -20,7 +20,7 @@ static int test__openat_syscall_event(struct test_suite *test __maybe_unused,int err =TEST_FAIL , fd;struct evsel *evsel;unsigned int nr_openat_calls =111 , i; -struct perf_thread_map *threads =thread_map__new (-1 ,getpid (),UINT_MAX ); +struct perf_thread_map *threads =thread_map__new_by_tid (getpid ());char sbuf[STRERR_BUFSIZE ];char errbuf[BUFSIZ ];
tools/perf/tests/perf-time-to-tsc.c +1 -1
@@ -90,7 +90,7 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int sustruct mmap *md; - threads =thread_map__new (-1 ,getpid (),UINT_MAX ); + threads =thread_map__new_by_tid (getpid ());CHECK_NOT_NULL__ (threads); cpus =perf_cpu_map__new_online_cpus ();
tools/perf/tests/switch-tracking.c +1 -1
@@ -351,7 +351,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int subconst char *comm;int err =-1 ; - threads =thread_map__new (-1 ,getpid (),UINT_MAX ); + threads =thread_map__new_by_tid (getpid ());if (!threads) {pr_debug ("thread_map__new failed!\n" ); goto out_err;
tools/perf/tests/thread-map.c +1 -1
@@ -115,7 +115,7 @@ static int test__thread_map_remove(struct test_suite *test __maybe_unused, int sTEST_ASSERT_VAL ("failed to allocate map string" ,asprintf (&str,"%d,%d" ,getpid (),getppid ()) >=0 ); - threads =thread_map__new_str (str,NULL ,0 , false); + threads =thread_map__new_str (str,/*tid=*/ NULL ,/*all_threads=*/ false);free (str); TEST_ASSERT_VAL("failed to allocate thread_map",
tools/perf/util/evlist.c +1 -1
@@ -1005,7 +1005,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target)* per-thread data. thread_map__new_str will call * thread_map__new_all_cpus to enumerate all threads. */ - threads =thread_map__new_str (target->pid, target->tid,UINT_MAX , all_threads); + threads =thread_map__new_str (target->pid, target->tid, all_threads);if (!threads)return -1 ;
tools/perf/util/python.c +5 -5
@@ -566,14 +566,14 @@ struct pyrf_thread_map {static int pyrf_thread_map__init (struct pyrf_thread_map *pthreads,PyObject *args,PyObject *kwargs) { -static char *kwlist[] = {"pid" ,"tid" ,"uid" ,NULL }; -int pid =-1 , tid =-1 , uid =UINT_MAX ; +static char *kwlist[] = {"pid" ,"tid" ,NULL }; +int pid =-1 , tid =-1 ; -if (!PyArg_ParseTupleAndKeywords (args, kwargs,"|iii" , - kwlist, &pid, &tid, &uid)) +if (!PyArg_ParseTupleAndKeywords (args, kwargs,"|ii" , + kwlist, &pid, &tid))return -1 ; - pthreads->threads =thread_map__new (pid, tid, uid); + pthreads->threads =thread_map__new (pid, tid);if (pthreads->threads ==NULL )return -1 ;return 0 ;
tools/perf/util/thread_map.c +3 -29
@@ -72,7 +72,7 @@ struct perf_thread_map *thread_map__new_by_tid(pid_t tid)return threads; } -static struct perf_thread_map *__thread_map__new_all_cpus (uid_t uid) +static struct perf_thread_map *thread_map__new_all_cpus (void ) {DIR *proc;int max_threads =32 , items, i;
@@ -98,15 +98,6 @@ static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid)if (*end)/* only interested in proper numerical dirents */ continue ; -snprintf (path,sizeof (path),"/proc/%s" , dirent->d_name); - -if (uid !=UINT_MAX ) { -struct stat st; - -if (stat (path, &st) !=0 || st.st_uid != uid) -continue ; - } -snprintf (path,sizeof (path),"/proc/%d/task" , pid); items =scandir (path, &namelist, filter,NULL );if (items <=0 ) {
@@ -157,24 +148,11 @@ static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid) goto out_closedir; } -struct perf_thread_map *thread_map__new_all_cpus (void ) -{ -return __thread_map__new_all_cpus (UINT_MAX ); -} - -struct perf_thread_map *thread_map__new_by_uid (uid_t uid) -{ -return __thread_map__new_all_cpus (uid); -} - -struct perf_thread_map *thread_map__new (pid_t pid,pid_t tid,uid_t uid) +struct perf_thread_map *thread_map__new (pid_t pid,pid_t tid) {if (pid !=-1 )return thread_map__new_by_pid (pid); -if (tid ==-1 && uid !=UINT_MAX ) -return thread_map__new_by_uid (uid); -return thread_map__new_by_tid (tid); }
@@ -289,15 +267,11 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str) goto out; } -struct perf_thread_map *thread_map__new_str (const char *pid,const char *tid, -uid_t uid,bool all_threads) +struct perf_thread_map *thread_map__new_str (const char *pid,const char *tid,bool all_threads) {if (pid)return thread_map__new_by_pid_str (pid); -if (!tid && uid !=UINT_MAX ) -return thread_map__new_by_uid (uid); -if (all_threads)return thread_map__new_all_cpus ();
tools/perf/util/thread_map.h +2 -4
@@ -11,13 +11,11 @@ struct perf_record_thread_map;struct perf_thread_map *thread_map__new_dummy (void );struct perf_thread_map *thread_map__new_by_pid (pid_t pid);struct perf_thread_map *thread_map__new_by_tid (pid_t tid); -struct perf_thread_map *thread_map__new_by_uid (uid_t uid); -struct perf_thread_map *thread_map__new_all_cpus (void ); -struct perf_thread_map *thread_map__new (pid_t pid,pid_t tid,uid_t uid); +struct perf_thread_map *thread_map__new (pid_t pid,pid_t tid);struct perf_thread_map *thread_map__new_event (struct perf_record_thread_map *event);struct perf_thread_map *thread_map__new_str (const char *pid, -const char *tid,uid_t uid,bool all_threads); +const char *tid,bool all_threads);struct perf_thread_map *thread_map__new_by_tid_str (const char *tid_str);
Signature · 1 line
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerQuoted 9 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.
Quoted 8 lines
v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.
Ping. Thanks,
Ian
Quoted 48 lines
Ian Rogers (10):
perf parse-events filter: Useevsel__find_pmu
perf target: Separateparse_uidinto its own function perf parse-events: Addparse_uid_filterhelper
perf record: Switch user option to use BPF filter
perf tests record: Add basic uid filtering test
perf top: Switch user option to use BPF filter
perf trace: Switch user option to use BPF filter
perf bench evlist-open-close: Switch user option to use BPF filter perf target: Remove uid from target
perfthread_map: Remove uid optionstools/perf/bench/evlist-open-close.c | 36 ++++++++------ tools/perf/builtin-ftrace.c | 1 - tools/perf/builtin-kvm.c | 2 - tools/perf/builtin-record.c | 27 ++++++----- tools/perf/builtin-stat.c | 4 +- tools/perf/builtin-top.c | 22 +++++---- tools/perf/builtin-trace.c | 27 +++++++---- tools/perf/tests/backward-ring-buffer.c | 1 - tools/perf/tests/event-times.c | 8 ++- tools/perf/tests/keep-tracking.c | 2 +- tools/perf/tests/mmap-basic.c | 2 +- tools/perf/tests/openat-syscall-all-cpus.c | 2 +- tools/perf/tests/openat-syscall-tp-fields.c | 1 - tools/perf/tests/openat-syscall.c | 2 +- tools/perf/tests/perf-record.c | 1 - tools/perf/tests/perf-time-to-tsc.c | 2 +- tools/perf/tests/shell/record.sh | 26 ++++++++++ tools/perf/tests/switch-tracking.c | 2 +- tools/perf/tests/task-exit.c | 1 - tools/perf/tests/thread-map.c | 2 +- tools/perf/util/bpf-filter.c | 2 +- tools/perf/util/evlist.c | 3 +- tools/perf/util/parse-events.c | 33 ++++++++----- tools/perf/util/parse-events.h | 1 + tools/perf/util/python.c | 10 ++-- tools/perf/util/target.c | 54 +++------------------ tools/perf/util/target.h | 15 ++---- tools/perf/util/thread_map.c | 32 ++---------- tools/perf/util/thread_map.h | 6 +-- tools/perf/util/top.c | 4 +- tools/perf/util/top.h | 1 + 31 files changed, 150 insertions(+), 182 deletions(-)--
2.49.0.850.g28803427d3-goog
Namhyung Kim
maintainerHi Ian,
Quoted 13 lines · Ian Rogers
Quoted 12 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.
Quoted 7 lines
Quoted 5 lines
v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.Ping. Thanks,
I'm ok with preferring BPF over /proc scanning, but still hesitate to remove it since some people don't use BPF. Can you please drop that part and make parse_uid_filter() conditional on BPF?
Thanks,
Namhyung
Quoted 48 lines
Quoted 48 lines
Ian Rogers (10):
perf parse-events filter: Useevsel__find_pmu
perf target: Separateparse_uidinto its own function perf parse-events: Addparse_uid_filterhelper
perf record: Switch user option to use BPF filter
perf tests record: Add basic uid filtering test
perf top: Switch user option to use BPF filter
perf trace: Switch user option to use BPF filter
perf bench evlist-open-close: Switch user option to use BPF filter perf target: Remove uid from target
perfthread_map: Remove uid optionstools/perf/bench/evlist-open-close.c | 36 ++++++++------ tools/perf/builtin-ftrace.c | 1 - tools/perf/builtin-kvm.c | 2 - tools/perf/builtin-record.c | 27 ++++++----- tools/perf/builtin-stat.c | 4 +- tools/perf/builtin-top.c | 22 +++++---- tools/perf/builtin-trace.c | 27 +++++++---- tools/perf/tests/backward-ring-buffer.c | 1 - tools/perf/tests/event-times.c | 8 ++- tools/perf/tests/keep-tracking.c | 2 +- tools/perf/tests/mmap-basic.c | 2 +- tools/perf/tests/openat-syscall-all-cpus.c | 2 +- tools/perf/tests/openat-syscall-tp-fields.c | 1 - tools/perf/tests/openat-syscall.c | 2 +- tools/perf/tests/perf-record.c | 1 - tools/perf/tests/perf-time-to-tsc.c | 2 +- tools/perf/tests/shell/record.sh | 26 ++++++++++ tools/perf/tests/switch-tracking.c | 2 +- tools/perf/tests/task-exit.c | 1 - tools/perf/tests/thread-map.c | 2 +- tools/perf/util/bpf-filter.c | 2 +- tools/perf/util/evlist.c | 3 +- tools/perf/util/parse-events.c | 33 ++++++++----- tools/perf/util/parse-events.h | 1 + tools/perf/util/python.c | 10 ++-- tools/perf/util/target.c | 54 +++------------------ tools/perf/util/target.h | 15 ++---- tools/perf/util/thread_map.c | 32 ++---------- tools/perf/util/thread_map.h | 6 +-- tools/perf/util/top.c | 4 +- tools/perf/util/top.h | 1 + 31 files changed, 150 insertions(+), 182 deletions(-)--
2.49.0.850.g28803427d3-goog
Ian Rogers
reviewerQuoted 21 lines · Namhyung Kim
Hi Ian,
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ping. Thanks,
I'm ok with preferring BPF over /proc scanning, but still hesitate to remove it since some people don't use BPF. Can you please drop that part and make
parse_uid_filter()conditional on BPF?
Hi Namhyung,
The approach of scanning /proc fails as:
1) processes that start after perf starts will be missed,
2) processes that terminate between being scanned in /proc and perf_event_open will cause perf to fail (essentially the -u option is just sugar to scan /proc and then provide the processes as if they were a -p option - such an approach doesn't need building into the tool).
This patch series adds a test [1] and perf test has lots of processes starting and exiting, matching condition (2) above*. If this series were changed to an approach that uses BPF and falls back on /proc scanning then the -u option would be broken for both reasons above but also prove a constant source of test flakes.
Rather than give the users something both frustrating to use (keeps quitting due to failed opens) and broken (missing processes) I think it is better to quit perf at that point informing the user they need more permissions to load the BPF program. This also makes the -u option testable.
So the request for a change I don't think is sensible as it provides a worse user and testing experience. There is also the cognitive load of having the /proc scanning code in the code base, whereas the BPF filter is largely isolated.
Thanks,
Ian
[1] https://lore.kernel.org/lkml/[email protected]/
* rescord.sh is marked as exclusive currently, but this shouldn't really be necessary.
Quoted 52 lines
Thanks,
NamhyungQuoted 48 lines
Quoted 48 lines
Ian Rogers (10):
perf parse-events filter: Useevsel__find_pmu
perf target: Separateparse_uidinto its own function perf parse-events: Addparse_uid_filterhelper
perf record: Switch user option to use BPF filter
perf tests record: Add basic uid filtering test
perf top: Switch user option to use BPF filter
perf trace: Switch user option to use BPF filter
perf bench evlist-open-close: Switch user option to use BPF filter perf target: Remove uid from target
perfthread_map: Remove uid optionstools/perf/bench/evlist-open-close.c | 36 ++++++++------ tools/perf/builtin-ftrace.c | 1 - tools/perf/builtin-kvm.c | 2 - tools/perf/builtin-record.c | 27 ++++++----- tools/perf/builtin-stat.c | 4 +- tools/perf/builtin-top.c | 22 +++++---- tools/perf/builtin-trace.c | 27 +++++++---- tools/perf/tests/backward-ring-buffer.c | 1 - tools/perf/tests/event-times.c | 8 ++- tools/perf/tests/keep-tracking.c | 2 +- tools/perf/tests/mmap-basic.c | 2 +- tools/perf/tests/openat-syscall-all-cpus.c | 2 +- tools/perf/tests/openat-syscall-tp-fields.c | 1 - tools/perf/tests/openat-syscall.c | 2 +- tools/perf/tests/perf-record.c | 1 - tools/perf/tests/perf-time-to-tsc.c | 2 +- tools/perf/tests/shell/record.sh | 26 ++++++++++ tools/perf/tests/switch-tracking.c | 2 +- tools/perf/tests/task-exit.c | 1 - tools/perf/tests/thread-map.c | 2 +- tools/perf/util/bpf-filter.c | 2 +- tools/perf/util/evlist.c | 3 +- tools/perf/util/parse-events.c | 33 ++++++++----- tools/perf/util/parse-events.h | 1 + tools/perf/util/python.c | 10 ++-- tools/perf/util/target.c | 54 +++------------------ tools/perf/util/target.h | 15 ++---- tools/perf/util/thread_map.c | 32 ++---------- tools/perf/util/thread_map.h | 6 +-- tools/perf/util/top.c | 4 +- tools/perf/util/top.h | 1 + 31 files changed, 150 insertions(+), 182 deletions(-)--
2.49.0.850.g28803427d3-goog
Namhyung Kim
maintainerQuoted 32 lines · Ian Rogers
Quoted 21 lines · Namhyung Kim
Hi Ian,
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ping. Thanks,
I'm ok with preferring BPF over /proc scanning, but still hesitate to remove it since some people don't use BPF. Can you please drop that part and make
parse_uid_filter()conditional on BPF?Hi Namhyung,
Quoted 7 lines
The approach of scanning /proc fails as:
1) processes that start after perf starts will be missed,
2) processes that terminate between being scanned in /proc andperf_event_openwill cause perf to fail (essentially the -u option is just sugar to scan /proc and then provide the processes as if they were a -p option - such an approach doesn't need building into the tool).
Yeah, I remember we had this discussion before. I think (1) is not true as perf events will be inherited to children (but there is a race). And
(2) is a real problem but it's also about a race and it can succeed.
Maybe we could change it to skip failed events when the target is a user but that's not the direction you want.
Quoted 13 lines
This patch series adds a test [1] and perf test has lots of processes starting and exiting, matching condition (2) above*. If this series were changed to an approach that uses BPF and falls back on /proc scanning then the -u option would be broken for both reasons above but also prove a constant source of test flakes.
Rather than give the users something both frustrating to use (keeps quitting due to failed opens) and broken (missing processes) I think it is better to quit perf at that point informing the user they need more permissions to load the BPF program. This also makes the -u option testable.
Quoted 4 lines
So the request for a change I don't think is sensible as it provides a worse user and testing experience. There is also the cognitive load of having the /proc scanning code in the code base, whereas the BPF filter is largely isolated.
But I think the problem is that it has different requirements - BPF and root privilege. So it should be used after checking the requirements and fail or fallback.
Does it print proper error messages if not? With that we can deprecate the existing behavior and remove it later.
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 42 lines · Namhyung Kim
Quoted 32 lines · Ian Rogers
Quoted 21 lines · Namhyung Kim
Hi Ian,
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ping. Thanks,
I'm ok with preferring BPF over /proc scanning, but still hesitate to remove it since some people don't use BPF. Can you please drop that part and make
parse_uid_filter()conditional on BPF?Hi Namhyung,
Quoted 7 lines
The approach of scanning /proc fails as:
1) processes that start after perf starts will be missed,
2) processes that terminate between being scanned in /proc andperf_event_openwill cause perf to fail (essentially the -u option is just sugar to scan /proc and then provide the processes as if they were a -p option - such an approach doesn't need building into the tool).
Quoted 2 lines
Yeah, I remember we had this discussion before. I think (1) is not true as perf events will be inherited to children (but there is a race).
If you log in from another terminal? Anything that creates a new process for that user but isn't inherited will be missed, which isn't merely a race.
Quoted 5 lines
And
(2) is a real problem but it's also about a race and it can succeed.Maybe we could change it to skip failed events when the target is a user but that's not the direction you want.
We could have other events and try to discover new processes via them, do things like dummy events to cover races. It is just a lot of complexity for something that is a trivial amount of BPF. In something like 10 years nobody has bothered to fix this up.
Quoted 18 lines
Quoted 13 lines
This patch series adds a test [1] and perf test has lots of processes starting and exiting, matching condition (2) above*. If this series were changed to an approach that uses BPF and falls back on /proc scanning then the -u option would be broken for both reasons above but also prove a constant source of test flakes.
Rather than give the users something both frustrating to use (keeps quitting due to failed opens) and broken (missing processes) I think it is better to quit perf at that point informing the user they need more permissions to load the BPF program. This also makes the -u option testable.
Quoted 4 lines
So the request for a change I don't think is sensible as it provides a worse user and testing experience. There is also the cognitive load of having the /proc scanning code in the code base, whereas the BPF filter is largely isolated.
Quoted 6 lines
But I think the problem is that it has different requirements - BPF and root privilege. So it should be used after checking the requirements and fail or fallback.
Does it print proper error messages if not? With that we can deprecate the existing behavior and remove it later.
For perf top with TUI you get an error message in a box of:
``` failed to set filter "BPF" on event cpu_atom/cycles/P with 1 (Operation not permitted) ``` With --stdio you get: ``` libbpf: Error in bpf_object__probe_loading(): -EPERM. Couldn't load trivial BPF program. Make sure your kernel supports BPF (CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is set to big enough value. libbpf: failed to load object 'sample_filter_bpf' libbpf: failed to load BPF skeleton 'sample_filter_bpf': -EPERM Failed to load perf sample-filter BPF skeleton failed to set filter "BPF" on event cpu_atom/cycles/P with 1 (Operation not permitted) ```
This matches the existing behavior if you put a filter on an event.
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 43 lines · Ian Rogers
Quoted 42 lines · Namhyung Kim
Quoted 39 lines · Ian Rogers
Quoted 21 lines · Namhyung Kim
Hi Ian,
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ping. Thanks,
I'm ok with preferring BPF over /proc scanning, but still hesitate to remove it since some people don't use BPF. Can you please drop that part and make
parse_uid_filter()conditional on BPF?Hi Namhyung,
The approach of scanning /proc fails as:
1) processes that start after perf starts will be missed,
2) processes that terminate between being scanned in /proc andperf_event_openwill cause perf to fail (essentially the -u option is just sugar to scan /proc and then provide the processes as if they were a -p option - such an approach doesn't need building into the tool).
Quoted 6 lines
Quoted 2 lines
Yeah, I remember we had this discussion before. I think (1) is not true as perf events will be inherited to children (but there is a race).
If you log in from another terminal? Anything that creates a new process for that user but isn't inherited will be missed, which isn't merely a race.
As long as the another terminal is owned by the same user, any new process from the terminal will inherit events, no?
Quoted 11 lines
Quoted 6 lines
And
(2) is a real problem but it's also about a race and it can succeed.Maybe we could change it to skip failed events when the target is a user but that's not the direction you want.
We could have other events and try to discover new processes via them, do things like dummy events to cover races. It is just a lot of complexity for something that is a trivial amount of BPF. In something like 10 years nobody has bothered to fix this up.
I don't want any complex solution for this. Let's not touch this.
Quoted 43 lines
Quoted 19 lines
Quoted 14 lines
This patch series adds a test [1] and perf test has lots of processes starting and exiting, matching condition (2) above*. If this series were changed to an approach that uses BPF and falls back on /proc scanning then the -u option would be broken for both reasons above but also prove a constant source of test flakes.
Rather than give the users something both frustrating to use (keeps quitting due to failed opens) and broken (missing processes) I think it is better to quit perf at that point informing the user they need more permissions to load the BPF program. This also makes the -u option testable.
Quoted 4 lines
So the request for a change I don't think is sensible as it provides a worse user and testing experience. There is also the cognitive load of having the /proc scanning code in the code base, whereas the BPF filter is largely isolated.
Quoted 6 lines
But I think the problem is that it has different requirements - BPF and root privilege. So it should be used after checking the requirements and fail or fallback.
Does it print proper error messages if not? With that we can deprecate the existing behavior and remove it later.
For
perf topwith TUI you get an error message in a box of:``` failed to set filter "BPF" on event cpu_atom/cycles/P with 1 (Operation not permitted) ``` With --stdio you get: ``` libbpf: Error in bpf_object__probe_loading(): -EPERM. Couldn't load trivial BPF program. Make sure your kernel supports BPF (CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is set to big enough value. libbpf: failed to load object 'sample_filter_bpf' libbpf: failed to load BPF skeleton 'sample_filter_bpf': -EPERM Failed to load perf sample-filter BPF skeleton failed to set filter "BPF" on event cpu_atom/cycles/P with 1 (Operation not permitted) ```
Quoted 1 line
This matches the existing behavior if you put a filter on an event.
But that's different as user directly asked the BPF filter. The following message would be better (unless you fallback to the old behavior).
"-u/--uid option is using BPF filter but perf is not built with BPF. Please make sure to build with libbpf and bpf skeletons."
and/or
"-u/--uid option is using BPF filter which requires root privilege."
You may check if the filter program and map is pinned already.
Thanks,
Namhyung
Ian Rogers
reviewerQuoted 118 lines · Namhyung Kim
Quoted 43 lines · Ian Rogers
Quoted 42 lines · Namhyung Kim
Quoted 39 lines · Ian Rogers
Quoted 21 lines · Namhyung Kim
Hi Ian,
Quoted 17 lines · Ian Rogers
Quoted 16 lines · Ian Rogers
Rather than scanning /proc and skipping PIDs based on their UIDs, use BPF filters for uid filtering. The /proc scanning in
thread_mapis racy as the PID may exit before theperf_event_opencausing perf to abort. BPF UID filters are more robust as they avoid the race. The /proc scanning also misses processes starting after the perf command. Add a helper for commands that support UID filtering and wire up. Remove the non-BPF UID filtering support given it doesn't work.v3: Add lengthier commit messages as requested by Arnaldo. Rebase on
tmp.perf-tools-next.v2: Add a perf record uid test (Namhyung) and force setting
system-wide for perf trace and perf record (Namhyung). Ensure the uid filter isn't set on tracepoint evsels.Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ping. Thanks,
I'm ok with preferring BPF over /proc scanning, but still hesitate to remove it since some people don't use BPF. Can you please drop that part and make
parse_uid_filter()conditional on BPF?Hi Namhyung,
The approach of scanning /proc fails as:
1) processes that start after perf starts will be missed,
2) processes that terminate between being scanned in /proc andperf_event_openwill cause perf to fail (essentially the -u option is just sugar to scan /proc and then provide the processes as if they were a -p option - such an approach doesn't need building into the tool).Quoted 6 lines
Quoted 2 lines
Yeah, I remember we had this discussion before. I think (1) is not true as perf events will be inherited to children (but there is a race).
If you log in from another terminal? Anything that creates a new process for that user but isn't inherited will be missed, which isn't merely a race.
As long as the another terminal is owned by the same user, any new process from the terminal will inherit events, no?
Quoted 11 lines
Quoted 6 lines
And
(2) is a real problem but it's also about a race and it can succeed.Maybe we could change it to skip failed events when the target is a user but that's not the direction you want.
We could have other events and try to discover new processes via them, do things like dummy events to cover races. It is just a lot of complexity for something that is a trivial amount of BPF. In something like 10 years nobody has bothered to fix this up.
I don't want any complex solution for this. Let's not touch this.
Quoted 43 lines
Quoted 19 lines
Quoted 14 lines
This patch series adds a test [1] and perf test has lots of processes starting and exiting, matching condition (2) above*. If this series were changed to an approach that uses BPF and falls back on /proc scanning then the -u option would be broken for both reasons above but also prove a constant source of test flakes.
Rather than give the users something both frustrating to use (keeps quitting due to failed opens) and broken (missing processes) I think it is better to quit perf at that point informing the user they need more permissions to load the BPF program. This also makes the -u option testable.
Quoted 4 lines
So the request for a change I don't think is sensible as it provides a worse user and testing experience. There is also the cognitive load of having the /proc scanning code in the code base, whereas the BPF filter is largely isolated.
Quoted 6 lines
But I think the problem is that it has different requirements - BPF and root privilege. So it should be used after checking the requirements and fail or fallback.
Does it print proper error messages if not? With that we can deprecate the existing behavior and remove it later.
For
perf topwith TUI you get an error message in a box of:``` failed to set filter "BPF" on event cpu_atom/cycles/P with 1 (Operation not permitted) ``` With --stdio you get: ``` libbpf: Error in bpf_object__probe_loading(): -EPERM. Couldn't load trivial BPF program. Make sure your kernel supports BPF (CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is set to big enough value. libbpf: failed to load object 'sample_filter_bpf' libbpf: failed to load BPF skeleton 'sample_filter_bpf': -EPERM Failed to load perf sample-filter BPF skeleton failed to set filter "BPF" on event cpu_atom/cycles/P with 1 (Operation not permitted) ```Quoted 1 line
This matches the existing behavior if you put a filter on an event.
But that's different as user directly asked the BPF filter. The following message would be better (unless you fallback to the old behavior).
Quoted 8 lines
"-u/--uid option is using BPF filter but perf is not built with BPF. Please make sure to build with libbpf and bpf skeletons."
and/or
"-u/--uid option is using BPF filter which requires root privilege."
You may check if the filter program and map is pinned already.
I don't disagree that these error messages would be better, shouldn't the existing BPF filter code also produce these more user friendly error messages? Once it does that we can adapt it when the caller is the '-u' option so that the error message doesn't imply '--filter' was used?
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung