Skip to content

Commit bf1976d

Browse files
captain5050namhyung
authored andcommitted
perf trace: 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. Ensure adding the BPF filter forces system-wide. Signed-off-by: Ian Rogers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 38f83cc commit bf1976d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

tools/perf/builtin-trace.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ struct trace {
236236
struct ordered_events data;
237237
u64 last;
238238
} oe;
239+
const char *uid_str;
239240
};
240241

241242
static void trace__load_vmlinux_btf(struct trace *trace __maybe_unused)
@@ -4412,8 +4413,8 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
44124413
evlist__add(evlist, pgfault_min);
44134414
}
44144415

4415-
/* Enable ignoring missing threads when -u/-p option is defined. */
4416-
trace->opts.ignore_missing_thread = trace->opts.target.uid != UINT_MAX || trace->opts.target.pid;
4416+
/* Enable ignoring missing threads when -p option is defined. */
4417+
trace->opts.ignore_missing_thread = trace->opts.target.pid;
44174418

44184419
if (trace->sched &&
44194420
evlist__add_newtp(evlist, "sched", "sched_stat_runtime", trace__sched_stat_runtime))
@@ -5445,8 +5446,7 @@ int cmd_trace(int argc, const char **argv)
54455446
"child tasks do not inherit counters"),
54465447
OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
54475448
"number of mmap data pages", evlist__parse_mmap_pages),
5448-
OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
5449-
"user to profile"),
5449+
OPT_STRING('u', "uid", &trace.uid_str, "user", "user to profile"),
54505450
OPT_CALLBACK(0, "duration", &trace, "float",
54515451
"show only events with duration > N.M ms",
54525452
trace__set_duration),
@@ -5804,11 +5804,19 @@ int cmd_trace(int argc, const char **argv)
58045804
goto out_close;
58055805
}
58065806

5807-
err = target__parse_uid(&trace.opts.target);
5808-
if (err) {
5809-
target__strerror(&trace.opts.target, err, bf, sizeof(bf));
5810-
fprintf(trace.output, "%s", bf);
5811-
goto out_close;
5807+
if (trace.uid_str) {
5808+
uid_t uid = parse_uid(trace.uid_str);
5809+
5810+
if (uid == UINT_MAX) {
5811+
ui__error("Invalid User: %s", trace.uid_str);
5812+
err = -EINVAL;
5813+
goto out_close;
5814+
}
5815+
err = parse_uid_filter(trace.evlist, uid);
5816+
if (err)
5817+
goto out_close;
5818+
5819+
trace.opts.target.system_wide = true;
58125820
}
58135821

58145822
if (!argc && target__none(&trace.opts.target))

0 commit comments

Comments
 (0)