Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2828740pxb; Mon, 19 Apr 2021 15:20:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXJWxhR9uXb2gMf18TwhNoJRTcl60wCY9dM5sxN23nAaO9BePMe3lnFETyFhwU+HEPaXT4 X-Received: by 2002:a17:906:cd17:: with SMTP id oz23mr16579560ejb.375.1618870817878; Mon, 19 Apr 2021 15:20:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618870817; cv=none; d=google.com; s=arc-20160816; b=Y1xyLRfkmolHihgrBb308k6DLnzv5AfwZDUIYkhUrx/7AKTbjGzvwVGq37JUFp7VMW 6gRPdilNoEeEKRFSlnQag5UGpoTeyTtiH8r0s+aECMjBgZD8psdZ1YF2KsTf+1t5GHWQ TDYhQMsr5JWk9dqmEI6Ijc+Ly43Hy36E6FjXlLut1iG8/AUx8xan2Iff2f6uv4CdXNgy 0vdLU71s21fee6S1a1QH7B9IwCNwqIYVCbM2i/SBEUl94hksYZXHldKV1D77AWIZpmFP 0DYb41bC3m9KFrgeJ313bkrhEWB2ERM6Fx2+P1LxugX5XrIcHn2GRdPFfx5kle6mRnzF 0yHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=DmMej12b3RmWSlFwd84pzn56S2iKO1GW6+Q+/qOCqVE=; b=s1fgc8K2by/PlcyDvb3DVDEf8Nbl1O6dhDUkl1kcWB8kwruGVV+uRqcReDdySr9/Ch tosz7vz3KVwmx6f+8oleIZ5CBVxTCpeBMk6v6OavEe+WYsRM6drrx50ZHH/0EW5Gdg6m tEanenA3YyZy7gfCxb50s8aQiG/88vr5UK4UGloFclXUPtjAv8fnoqfN65oVqNNXpyyi O75sUIktM/rWvSNybi99rHOSQ8lHSdDb8Qy/KiC7+IAHl7J3W8yU8uJS2H2yNauf29nV nmz8IPGf0J7/ObJb0uYL8146ekkiAESvOHoxbUp51Du3n8jbkEDygvmYB3Mpc9XoPkCt OzRg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n4si11125679eji.301.2021.04.19.15.19.55; Mon, 19 Apr 2021 15:20:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242180AbhDSUhp convert rfc822-to-8bit (ORCPT + 99 others); Mon, 19 Apr 2021 16:37:45 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:25030 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242155AbhDSUhl (ORCPT ); Mon, 19 Apr 2021 16:37:41 -0400 Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 13JKUM0O006778 for ; Mon, 19 Apr 2021 13:37:11 -0700 Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 380fbaq70f-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 19 Apr 2021 13:37:11 -0700 Received: from intmgw001.25.frc3.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Mon, 19 Apr 2021 13:37:10 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id AA43B62E1A1C; Mon, 19 Apr 2021 13:37:05 -0700 (PDT) From: Song Liu To: CC: , , , , , , Song Liu Subject: [PATCH v4 2/4] perf bpf: check perf_attr_map is compatible with the perf binary Date: Mon, 19 Apr 2021 13:36:47 -0700 Message-ID: <20210419203649.164121-3-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210419203649.164121-1-song@kernel.org> References: <20210419203649.164121-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: kvHToDNGobPh2iY5L3CUyOshuukWnKS6 X-Proofpoint-ORIG-GUID: kvHToDNGobPh2iY5L3CUyOshuukWnKS6 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.761 definitions=2021-04-19_11:2021-04-19,2021-04-19 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 suspectscore=0 spamscore=0 clxscore=1034 phishscore=0 malwarescore=0 bulkscore=0 lowpriorityscore=0 priorityscore=1501 impostorscore=0 adultscore=9 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104060000 definitions=main-2104190141 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.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 --- 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