Skip to content

Commit 1151208

Browse files
captain5050namhyung
authored andcommitted
perf record: 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 466db42 commit 1151208

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

tools/perf/builtin-record.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ struct record {
175175
bool timestamp_boundary;
176176
bool off_cpu;
177177
const char *filter_action;
178+
const char *uid_str;
178179
struct switch_output switch_output;
179180
unsigned long long samples;
180181
unsigned long output_max_size; /* = 0: unlimited */
@@ -3513,8 +3514,7 @@ static struct option __record_options[] = {
35133514
"or ranges of time to enable events e.g. '-D 10-20,30-40'",
35143515
record__parse_event_enable_time),
35153516
OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"),
3516-
OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
3517-
"user to profile"),
3517+
OPT_STRING('u', "uid", &record.uid_str, "user", "user to profile"),
35183518

35193519
OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
35203520
"branch any", "sample any taken branches",
@@ -4256,19 +4256,24 @@ int cmd_record(int argc, const char **argv)
42564256
ui__warning("%s\n", errbuf);
42574257
}
42584258

4259-
err = target__parse_uid(&rec->opts.target);
4260-
if (err) {
4261-
int saved_errno = errno;
4259+
if (rec->uid_str) {
4260+
uid_t uid = parse_uid(rec->uid_str);
42624261

4263-
target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
4264-
ui__error("%s", errbuf);
4262+
if (uid == UINT_MAX) {
4263+
ui__error("Invalid User: %s", rec->uid_str);
4264+
err = -EINVAL;
4265+
goto out;
4266+
}
4267+
err = parse_uid_filter(rec->evlist, uid);
4268+
if (err)
4269+
goto out;
42654270

4266-
err = -saved_errno;
4267-
goto out;
4271+
/* User ID filtering implies system wide. */
4272+
rec->opts.target.system_wide = true;
42684273
}
42694274

4270-
/* Enable ignoring missing threads when -u/-p option is defined. */
4271-
rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid;
4275+
/* Enable ignoring missing threads when -p option is defined. */
4276+
rec->opts.ignore_missing_thread = rec->opts.target.pid;
42724277

42734278
evlist__warn_user_requested_cpus(rec->evlist, rec->opts.target.cpu_list);
42744279

0 commit comments

Comments
 (0)