Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3122159pxy; Sun, 25 Apr 2021 14:45:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkwPLbL84wMmKshxSfH2W33zSSGpZocRz84io1nbd1hSbSzHGvBMCs+mABEd7kfynEE6Ly X-Received: by 2002:a17:902:7005:b029:ec:aead:23fa with SMTP id y5-20020a1709027005b02900ecaead23famr15299771plk.30.1619387114801; Sun, 25 Apr 2021 14:45:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619387114; cv=none; d=google.com; s=arc-20160816; b=c6aE+Dn1FtOfc1w+DFp0jBV1HqRsZP/7Y05ta/gSWMWCJNEEawXV/GyTSw+gGo2ERL Ti59S6D0evwPziA6zHtZNe9fTpaKm7rT+ck3g1Td2l5w5QlkbVd5zhd/RyXJO2P3+y9z QY0dHG/9QZbgv/G17dVFi7flP/BA4EvHHKSJ+58WvbV2MqaVqq0ktQAW+2bdtyaVbnX5 Lg+xTOS2ZXp0a2wNbPEAyoG9Y5gN5YpOLou/OiUqGHzuyU6NWtb56vH+JcOCtEIEfhac NQBvkiZpooQQkHxdv4ICX4zWsm5ROInivlofx/x8i71dJmkkw685890wxS2qTPwMwqQT VAYA== 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=lNWfXPfe1zkNggDrhzpJcHTRN4xZq9bYDlBmZSgKVbuDck+N3MZ+PKFe8Gj0rZ4CWt XB/j1saRy+emjQvRc66Izxsx3s23LbSMAiYwrNIJFu2RzTd4CGtSYgXSQb+zp39lCzU/ XairXHAISfT7lNnBj649ZWavxHCPT85AW6kYQVJGxVOZC2WiKEDNs9yUCK67pujI0Wkg PeN6gbzlL6uSi3cecZeEFbkH59GkmFwppTXLBCBEdd/dIcXwAJe9oF6EySmCp6tyx2zv 5RhTdMF3O+A9AZJIGYItDCCvdz0bvYUdqqUpM51kV/mVO1wv18YE9a8TPGp9ZE2ZX0SU OSAA== 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 j6si15091895pgp.46.2021.04.25.14.45.02; Sun, 25 Apr 2021 14:45:14 -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 S231411AbhDYVo3 convert rfc822-to-8bit (ORCPT + 99 others); Sun, 25 Apr 2021 17:44:29 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:55926 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231392AbhDYVo2 (ORCPT ); Sun, 25 Apr 2021 17:44:28 -0400 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 13PLV1of007051 for ; Sun, 25 Apr 2021 14:43:47 -0700 Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 384guxdhf0-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sun, 25 Apr 2021 14:43:47 -0700 Received: from intmgw001.25.frc3.facebook.com (2620:10d:c085:208::11) 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; Sun, 25 Apr 2021 14:43:45 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 53F9662E2330; Sun, 25 Apr 2021 14:43:42 -0700 (PDT) From: Song Liu To: CC: , , , , , , Song Liu Subject: [PATCH v5 2/5] perf bpf: check perf_attr_map is compatible with the perf binary Date: Sun, 25 Apr 2021 14:43:30 -0700 Message-ID: <20210425214333.1090950-3-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210425214333.1090950-1-song@kernel.org> References: <20210425214333.1090950-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: a5BNhpL4Aj_8yM2dnsp8trY0kp-9XYpK X-Proofpoint-ORIG-GUID: a5BNhpL4Aj_8yM2dnsp8trY0kp-9XYpK X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.761 definitions=2021-04-25_09:2021-04-23,2021-04-25 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 impostorscore=0 lowpriorityscore=0 mlxlogscore=999 priorityscore=1501 mlxscore=0 spamscore=0 phishscore=0 bulkscore=0 suspectscore=0 adultscore=8 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104060000 definitions=main-2104250162 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