Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp613661ybc; Tue, 19 Nov 2019 06:36:54 -0800 (PST) X-Google-Smtp-Source: APXvYqyDhloZzTqcuFPBPWDmXb+8hL7/EuQAfXCnyrYQ2ApF8WvjEHGJnpO/rBUWNXtHC50IHTRi X-Received: by 2002:a1c:2e0f:: with SMTP id u15mr6111903wmu.47.1574174214564; Tue, 19 Nov 2019 06:36:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574174214; cv=none; d=google.com; s=arc-20160816; b=eupOROb57jZP+2WfhjsVJ6YDa4mhTIwA2mMlvYX38w+VGC7T41r1+G9YEiV1bD4PCy pATV1yXcwOLUyfFPr4eMKXPPEa9Qs9cV170cnZKNnNz5qORI1G6nJoZA0aVqyJpUXa2a e7IXuMo8Vte8f/6tZ2oqugi4U7ikv2m7zBrscc1Aw3/iCkp1Q4WFiVNpliedBWM4chm2 eCy4aidbzNGwObcBQxSVDa4fnAmwqmRh5pEl/jZFjRVnB1Fu9cPB6gtIUSR0MDKCF7HM IocCxZ3Bsm7XjlbO852n1U9pakAqBP/92bbVYh1Qb8KYgNy9bJnUZJUGyryj3FMc1Q7Z tpog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=L9RnIAkBlvPVnpLKFUa9IjLUaJhGZb85PjRtex37VSY=; b=iFPkbY1PimgaOLoUXH1xIT2WUDEkA3S8+8yTIV1MtF6udx5e8hzRK3gizQ+iAHqEkh VQcunu9PHnCwxrqtOCf+FvySwHyj9OGFCe8VTmNYJ2Jai0JA1ibEAZRUz7pH8ZewZu5I f2Nav5TvmG3eS+5a0bhDJNwSLDORwje+rvtkNY50kquYlMjVAFMC/yg6VtPLZ+WDUjoS PB/7SuLqO6ZXu9YmBgUHis29wJlG1vh7crPveCMpztiG2VAqcYMPnPdbh11+mEf4SJoe T5BtNIQwHKsgTG4zJCpBivHJjoujr4WWRQAhzLq2Q0j0ufQm5XIX3WCNTIZdTJzIRdWq SqGA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d6si15879752ede.119.2019.11.19.06.36.30; Tue, 19 Nov 2019 06:36:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728251AbfKSOfL (ORCPT + 99 others); Tue, 19 Nov 2019 09:35:11 -0500 Received: from mga04.intel.com ([192.55.52.120]:64767 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726637AbfKSOfJ (ORCPT ); Tue, 19 Nov 2019 09:35:09 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Nov 2019 06:35:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,324,1569308400"; d="scan'208";a="215552384" Received: from labuser-ice-lake-client-platform.jf.intel.com ([10.54.55.50]) by fmsmga001.fm.intel.com with ESMTP; 19 Nov 2019 06:35:08 -0800 From: kan.liang@linux.intel.com To: peterz@infradead.org, acme@redhat.com, mingo@kernel.org, linux-kernel@vger.kernel.org Cc: jolsa@kernel.org, namhyung@kernel.org, vitaly.slobodskoy@intel.com, pavel.gerasimov@intel.com, ak@linux.intel.com, eranian@google.com, mpe@ellerman.id.au, Kan Liang Subject: [PATCH V4 04/13] perf header: Add check for event attr Date: Tue, 19 Nov 2019 06:34:02 -0800 Message-Id: <20191119143411.3482-5-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191119143411.3482-1-kan.liang@linux.intel.com> References: <20191119143411.3482-1-kan.liang@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kan Liang The perf.data may be generated by a newer version of perf tool, which support new input bits in attr, e.g. new bit for branch_sample_type. The perf.data may be parsed by an older version of perf tool later. The old perf tool may parse the perf.data incorrectly. There is no warning message for this case. Current perf header never check for unknown input bits in attr. When read the event desc from header, check the stored event attr. The reserved bits, sample type, read format and branch sample type will be checked. Signed-off-by: Kan Liang --- tools/perf/util/header.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index becc2d109423..7ed481c9bcdf 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -1599,6 +1599,41 @@ static void free_event_desc(struct evsel *events) free(events); } +static bool perf_attr_check(struct perf_event_attr *attr) +{ + if (attr->__reserved_1) { + pr_warning("Unexpected reserved bits (0x%x) are detected. " + "Please update perf tool.\n", + attr->__reserved_1); + return false; + } + + if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) { + pr_warning("Unknown sample type (0x%llx) is detected. " + "Please update perf tool.\n", + attr->sample_type); + return false; + } + + if (attr->read_format & ~(PERF_FORMAT_MAX-1)) { + pr_warning("Unknown read format (0x%llx) is detected. " + "Please update perf tool.\n", + attr->read_format); + return false; + } + + if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) && + (attr->branch_sample_type & ~PERF_SAMPLE_BRANCH_MASK)) { + pr_warning("Unknown branch sample type (0x%llx) is detected. " + "Please update perf tool.\n", + attr->branch_sample_type); + + return false; + } + + return true; +} + static struct evsel *read_event_desc(struct feat_fd *ff) { struct evsel *evsel, *events = NULL; @@ -1643,6 +1678,9 @@ static struct evsel *read_event_desc(struct feat_fd *ff) memcpy(&evsel->core.attr, buf, msz); + if (!perf_attr_check(&evsel->core.attr)) + goto error; + if (do_read_u32(ff, &nr)) goto error; -- 2.17.1