Skip to content

Commit

Permalink
perf bpf: check perf_attr_map is compatible with the perf binary
Browse files Browse the repository at this point in the history
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 <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Song Liu <[email protected]>
Cc: [email protected]
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
liu-song-6 authored and acmel committed Apr 29, 2021
1 parent ec8149f commit fe3dd82
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tools/perf/util/bpf_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,20 @@ static __u32 bpf_map_get_id(int fd)
return map_info.id;
}

static bool bperf_attr_map_compatible(int attr_map_fd)
{
struct bpf_map_info map_info = {0};
__u32 map_info_len = sizeof(map_info);
int err;

err = bpf_obj_get_info_by_fd(attr_map_fd, &map_info, &map_info_len);

if (err)
return false;
return (map_info.key_size == sizeof(struct perf_event_attr)) &&
(map_info.value_size == sizeof(struct perf_event_attr_map_entry));
}

static int bperf_lock_attr_map(struct target *target)
{
char path[PATH_MAX];
Expand Down Expand Up @@ -346,6 +360,11 @@ static int bperf_lock_attr_map(struct target *target)
return -1;
}

if (!bperf_attr_map_compatible(map_fd)) {
close(map_fd);
return -1;

}
err = flock(map_fd, LOCK_EX);
if (err) {
close(map_fd);
Expand Down

0 comments on commit fe3dd82

Please sign in to comment.