Skip to content

Commit fe3dd82

Browse files
liu-song-6acmel
authored andcommitted
perf bpf: check perf_attr_map is compatible with the perf binary
perf_attr_map could be shared among different version of perf binary. Add bperf_attr_map_compatible() to check whether the existing attr_map is compatible with current perf binary. Signed-off-by: Song Liu <song@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Song Liu <songliubraving@fb.com> Cc: kernel-team@fb.com Link: https://lore.kernel.org/r/20210425214333.1090950-3-song@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent ec8149f commit fe3dd82

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tools/perf/util/bpf_counter.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ static __u32 bpf_map_get_id(int fd)
312312
return map_info.id;
313313
}
314314

315+
static bool bperf_attr_map_compatible(int attr_map_fd)
316+
{
317+
struct bpf_map_info map_info = {0};
318+
__u32 map_info_len = sizeof(map_info);
319+
int err;
320+
321+
err = bpf_obj_get_info_by_fd(attr_map_fd, &map_info, &map_info_len);
322+
323+
if (err)
324+
return false;
325+
return (map_info.key_size == sizeof(struct perf_event_attr)) &&
326+
(map_info.value_size == sizeof(struct perf_event_attr_map_entry));
327+
}
328+
315329
static int bperf_lock_attr_map(struct target *target)
316330
{
317331
char path[PATH_MAX];
@@ -346,6 +360,11 @@ static int bperf_lock_attr_map(struct target *target)
346360
return -1;
347361
}
348362

363+
if (!bperf_attr_map_compatible(map_fd)) {
364+
close(map_fd);
365+
return -1;
366+
367+
}
349368
err = flock(map_fd, LOCK_EX);
350369
if (err) {
351370
close(map_fd);

0 commit comments

Comments
 (0)