Skip to content

Commit 278538d

Browse files
captain5050namhyung
authored andcommitted
perf bench evlist-open-close: 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 bf1976d commit 278538d

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

tools/perf/bench/evlist-open-close.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static int evlist__count_evsel_fds(struct evlist *evlist)
5757
return cnt;
5858
}
5959

60-
static struct evlist *bench__create_evlist(char *evstr)
60+
static struct evlist *bench__create_evlist(char *evstr, const char *uid_str)
6161
{
6262
struct parse_events_error err;
6363
struct evlist *evlist = evlist__new();
@@ -78,6 +78,18 @@ static struct evlist *bench__create_evlist(char *evstr)
7878
goto out_delete_evlist;
7979
}
8080
parse_events_error__exit(&err);
81+
if (uid_str) {
82+
uid_t uid = parse_uid(uid_str);
83+
84+
if (uid == UINT_MAX) {
85+
pr_err("Invalid User: %s", uid_str);
86+
ret = -EINVAL;
87+
goto out_delete_evlist;
88+
}
89+
ret = parse_uid_filter(evlist, uid);
90+
if (ret)
91+
goto out_delete_evlist;
92+
}
8193
ret = evlist__create_maps(evlist, &opts.target);
8294
if (ret < 0) {
8395
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)
117129
return 0;
118130
}
119131

120-
static int bench_evlist_open_close__run(char *evstr)
132+
static int bench_evlist_open_close__run(char *evstr, const char *uid_str)
121133
{
122134
// used to print statistics only
123-
struct evlist *evlist = bench__create_evlist(evstr);
135+
struct evlist *evlist = bench__create_evlist(evstr, uid_str);
124136
double time_average, time_stddev;
125137
struct timeval start, end, diff;
126138
struct stats time_stats;
@@ -142,7 +154,7 @@ static int bench_evlist_open_close__run(char *evstr)
142154

143155
for (i = 0; i < iterations; i++) {
144156
pr_debug("Started iteration %d\n", i);
145-
evlist = bench__create_evlist(evstr);
157+
evlist = bench__create_evlist(evstr, uid_str);
146158
if (!evlist)
147159
return -ENOMEM;
148160

@@ -206,6 +218,7 @@ static char *bench__repeat_event_string(const char *evstr, int n)
206218

207219
int bench_evlist_open_close(int argc, const char **argv)
208220
{
221+
const char *uid_str = NULL;
209222
const struct option options[] = {
210223
OPT_STRING('e', "event", &event_string, "event",
211224
"event selector. use 'perf list' to list available events"),
@@ -221,7 +234,7 @@ int bench_evlist_open_close(int argc, const char **argv)
221234
"record events on existing process id"),
222235
OPT_STRING('t', "tid", &opts.target.tid, "tid",
223236
"record events on existing thread id"),
224-
OPT_STRING('u', "uid", &opts.target.uid_str, "user", "user to profile"),
237+
OPT_STRING('u', "uid", &uid_str, "user", "user to profile"),
225238
OPT_BOOLEAN(0, "per-thread", &opts.target.per_thread, "use per-thread mmaps"),
226239
OPT_END()
227240
};
@@ -245,23 +258,16 @@ int bench_evlist_open_close(int argc, const char **argv)
245258
goto out;
246259
}
247260

248-
err = target__parse_uid(&opts.target);
249-
if (err) {
250-
target__strerror(&opts.target, err, errbuf, sizeof(errbuf));
251-
pr_err("%s", errbuf);
252-
goto out;
253-
}
254-
255-
/* Enable ignoring missing threads when -u/-p option is defined. */
256-
opts.ignore_missing_thread = opts.target.uid != UINT_MAX || opts.target.pid;
261+
/* Enable ignoring missing threads when -p option is defined. */
262+
opts.ignore_missing_thread = opts.target.pid;
257263

258264
evstr = bench__repeat_event_string(event_string, nr_events);
259265
if (!evstr) {
260266
err = -ENOMEM;
261267
goto out;
262268
}
263269

264-
err = bench_evlist_open_close__run(evstr);
270+
err = bench_evlist_open_close__run(evstr, uid_str);
265271

266272
free(evstr);
267273
out:

0 commit comments

Comments
 (0)