All of lore.kernel.org
 help / color / mirror / Atom feed
From: Song Liu <song@kernel.org>
To: <linux-kernel@vger.kernel.org>
Cc: <kernel-team@fb.com>, <acme@kernel.org>, <acme@redhat.com>,
	<namhyung@kernel.org>, <jolsa@kernel.org>,
	<songliubraving@fb.com>, Song Liu <song@kernel.org>
Subject: [PATCH v3 2/4] perf bpf: check perf_attr_map is compatible with the perf binary
Date: Fri, 16 Apr 2021 15:13:23 -0700	[thread overview]
Message-ID: <20210416221325.2373497-3-song@kernel.org> (raw)
In-Reply-To: <20210416221325.2373497-1-song@kernel.org>

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>
---
 tools/perf/util/bpf_counter.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c
index be484ddbbd5be..5de991ab46af9 100644
--- a/tools/perf/util/bpf_counter.c
+++ b/tools/perf/util/bpf_counter.c
@@ -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];
@@ -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);
-- 
2.30.2


  parent reply	other threads:[~2021-04-16 22:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-16 22:13 [PATCH v3 0/4] perf util: bpf perf improvements Song Liu
2021-04-16 22:13 ` [PATCH v3 1/4] perf util: move bpf_perf definitions to a libperf header Song Liu
2021-04-16 22:13 ` Song Liu [this message]
2021-04-16 22:13 ` [PATCH v3 3/4] perf-stat: introduce config stat.bpf-counter-events Song Liu
2021-04-17 16:45   ` Namhyung Kim
2021-04-19 20:26     ` Song Liu
2021-04-20 17:30       ` Jiri Olsa
2021-04-19 14:26   ` Jiri Olsa
2021-04-19 20:27     ` Song Liu
2021-04-16 22:13 ` [PATCH v3 4/4] perf-stat: introduce ':b' modifier Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210416221325.2373497-3-song@kernel.org \
    --to=song@kernel.org \
    --cc=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=songliubraving@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.