Skip to content

Commit 38f83cc

Browse files
captain5050namhyung
authored andcommitted
perf top: Switch user option to use BPF filter
Finding user processes by scanning /proc is inherently racy and results in perf_event_open failures. Use a BPF filter to drop samples where the uid doesn't match. Signed-off-by: Ian Rogers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent c54e2f8 commit 38f83cc

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

tools/perf/builtin-top.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ static void *display_thread_tui(void *arg)
643643
*/
644644
evlist__for_each_entry(top->evlist, pos) {
645645
struct hists *hists = evsel__hists(pos);
646-
hists->uid_filter_str = top->record_opts.target.uid_str;
646+
hists->uid_filter_str = top->uid_str;
647647
}
648648

649649
ret = evlist__tui_browse_hists(top->evlist, help, &hbt, top->min_percent,
@@ -1571,7 +1571,7 @@ int cmd_top(int argc, const char **argv)
15711571
"Add prefix to source file path names in programs (with --prefix-strip)"),
15721572
OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
15731573
"Strip first N entries of source file path name in programs (with --prefix)"),
1574-
OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
1574+
OPT_STRING('u', "uid", &top.uid_str, "user", "user to profile"),
15751575
OPT_CALLBACK(0, "percent-limit", &top, "percent",
15761576
"Don't show entries under that percent", parse_percent_limit),
15771577
OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
@@ -1762,15 +1762,17 @@ int cmd_top(int argc, const char **argv)
17621762
ui__warning("%s\n", errbuf);
17631763
}
17641764

1765-
status = target__parse_uid(target);
1766-
if (status) {
1767-
int saved_errno = errno;
1768-
1769-
target__strerror(target, status, errbuf, BUFSIZ);
1770-
ui__error("%s\n", errbuf);
1765+
if (top.uid_str) {
1766+
uid_t uid = parse_uid(top.uid_str);
17711767

1772-
status = -saved_errno;
1773-
goto out_delete_evlist;
1768+
if (uid == UINT_MAX) {
1769+
ui__error("Invalid User: %s", top.uid_str);
1770+
status = -EINVAL;
1771+
goto out_delete_evlist;
1772+
}
1773+
status = parse_uid_filter(top.evlist, uid);
1774+
if (status)
1775+
goto out_delete_evlist;
17741776
}
17751777

17761778
if (target__none(target))

tools/perf/util/top.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
8888
else if (target->tid)
8989
ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s",
9090
target->tid);
91-
else if (target->uid_str != NULL)
91+
else if (top->uid_str != NULL)
9292
ret += SNPRINTF(bf + ret, size - ret, " (uid: %s",
93-
target->uid_str);
93+
top->uid_str);
9494
else
9595
ret += SNPRINTF(bf + ret, size - ret, " (all");
9696

tools/perf/util/top.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct perf_top {
4848
const char *sym_filter;
4949
float min_percent;
5050
unsigned int nr_threads_synthesize;
51+
const char *uid_str;
5152

5253
struct {
5354
struct ordered_events *in;

0 commit comments

Comments
 (0)