Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1629743pxb; Thu, 4 Mar 2021 16:56:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzl/eTfRZUfppZwOc0T9az7KLTJkvjjN7eNSGLabEe3a5FUbZrn4VcQxXIik6di/bq07kYL X-Received: by 2002:a05:6e02:4a4:: with SMTP id e4mr5993402ils.114.1614905816502; Thu, 04 Mar 2021 16:56:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614905816; cv=none; d=google.com; s=arc-20160816; b=cvJ+uuyWQtNt4/xgOdeaW55QDOx9qnWYU/XqNZ6lMeJeZy5HQFTiVAn500UsYqCeru XeDyEbe86KLebEb+06aniGo5CwgWNjktwc/a8OGeLtJzKJJvyz02fM9ZlWD56zrHFrmW 9ImiI1CzRDuEwhVm09Bs9hynYs+wIdJlHQGj00q4rQZidramNVoZPcN9EinIz6TrgEW2 pfY/c5O/edscvAzOcKUChPZl7qb4dlrwhCMOr+Zh0lbWPGpkm8a3hUVxqHFg9vGDMIQ/ utyTE77oVWR0ne8RiDT0epd1p/Rx2CEi2RAN8PN0cobI3OI5m8cxdoVOw0UYpEYIdPMb RRKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=NcEeIM4rSQOyEW+04ey55syfNFH6fXFOkJ0byNZRhLc=; b=wYcjud4xwwV/7N1vFeHjK1lx22GeSeRl0ShDex5Z4Jae8oXL1NZQoNRfEL9NPBeDRT imlizFrmtjgpCkvhD8Jt66PYLYh4fLhE157ufAnyRknFkjVRpbTceHRzSEY2xyAhAgQi Dh/u/dFmSpE1JXRF1BvjnDAf+kYW9loK7Ltzygmu6rsluoFsmQB5lnbxzxmGZFjvDZHb 8oHmWvNzkg+sd1TUHS18HQkHOlfNWNDIcmzgPiruUNiLR88n6Gfrf1GH/34MNVbrZpvX v8nHVn9AESmlH/xbvutcPmMfPMucN7K4PMbtsx914lmdtxl3YMBgHJU5/bhRbEOUxcbQ FPAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JP+q9C4w; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n12si829087ilk.3.2021.03.04.16.56.43; Thu, 04 Mar 2021 16:56:56 -0800 (PST) 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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JP+q9C4w; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238656AbhCDUTi (ORCPT + 99 others); Thu, 4 Mar 2021 15:19:38 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:57293 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236405AbhCDUTZ (ORCPT ); Thu, 4 Mar 2021 15:19:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614889079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NcEeIM4rSQOyEW+04ey55syfNFH6fXFOkJ0byNZRhLc=; b=JP+q9C4wzOq9DtvS0ep5EHBNun3hf1zBVg76i7x9BnDbkqAwVktndhVvdfvP/o/q8XsceR H6WFwUNz9/vV9wM9qnQfbEmhoRfo1g56/bGyQrV3bfgLgPd0QJumA9Af/LbeoWumAUIgKZ 9Jh09UFgQOXBPaEYPLo5a1QCzoXBzk8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-516-yGDbhkT-PQKrKN2212R9TQ-1; Thu, 04 Mar 2021 15:17:55 -0500 X-MC-Unique: yGDbhkT-PQKrKN2212R9TQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3690883DD21; Thu, 4 Mar 2021 20:17:54 +0000 (UTC) Received: from krava (unknown [10.40.196.20]) by smtp.corp.redhat.com (Postfix) with SMTP id 193ED100164C; Thu, 4 Mar 2021 20:17:51 +0000 (UTC) Date: Thu, 4 Mar 2021 21:17:51 +0100 From: Jiri Olsa To: Jin Yao Cc: acme@kernel.org, jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com, alexander.shishkin@linux.intel.com, Linux-kernel@vger.kernel.org, ak@linux.intel.com, kan.liang@intel.com, yao.jin@intel.com Subject: Re: [PATCH] perf pmu: Validate raw event with sysfs exported format bits Message-ID: References: <20210303051736.26974-1-yao.jin@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210303051736.26974-1-yao.jin@linux.intel.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 03, 2021 at 01:17:36PM +0800, Jin Yao wrote: SNIP > The set bits in 'bits' indicate the invalid bits used in config. > Finally use strbuf to report the invalid bits. > > Some architectures may not export supported bits through sysfs, > so if masks is 0, perf_pmu__config_valid just returns true. > > After: > > Single event: > > # ./perf stat -e cpu/r031234/ -a -- sleep 1 > WARNING: event config '31234' not valid (bits 16 17 not supported by kernel)! > > Performance counter stats for 'system wide': > > 0 cpu/r031234/ > > 1.001403757 seconds time elapsed > > Multiple events: > > # ./perf stat -e cpu/rf01234/,cpu/r031234/ -a -- sleep 1 > WARNING: event config 'f01234' not valid (bits 20 22 not supported by kernel)! > WARNING: event config '31234' not valid (bits 16 17 not supported by kernel)! right, seems useful > > Performance counter stats for 'system wide': > > 0 cpu/rf01234/ > 0 cpu/r031234/ > > The warning is reported for invalid bits. > > Signed-off-by: Jin Yao > --- > tools/perf/util/parse-events.c | 11 ++++++++++ > tools/perf/util/pmu.c | 38 ++++++++++++++++++++++++++++++++++ > tools/perf/util/pmu.h | 4 ++++ > 3 files changed, 53 insertions(+) > > diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c > index 42c84adeb2fb..1820b2c0a241 100644 > --- a/tools/perf/util/parse-events.c > +++ b/tools/perf/util/parse-events.c > @@ -356,6 +356,17 @@ __add_event(struct list_head *list, int *idx, > struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : > cpu_list ? perf_cpu_map__new(cpu_list) : NULL; > if we want it just for raw/numeric, we could add it earlier on, like to parse_events_add_numeric call but perhaps it might be helpful to check any pmu event, could perhaps reveal some broken format > + if (pmu && attr->type == PERF_TYPE_RAW) { > + struct strbuf buf = STRBUF_INIT; > + > + if (!perf_pmu__config_valid(pmu, attr->config, &buf)) { > + pr_warning("WARNING: event config '%llx' not valid (" > + "bits%s not supported by kernel)!\n", > + attr->config, buf.buf); > + } > + strbuf_release(&buf); > + } > + > if (init_attr) > event_attr_init(attr); > > diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c > index 44ef28302fc7..5e361adb2698 100644 > --- a/tools/perf/util/pmu.c > +++ b/tools/perf/util/pmu.c > @@ -1812,3 +1812,41 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) > > return nr_caps; > } > + > +bool perf_pmu__config_valid(struct perf_pmu *pmu, __u64 config, > + struct strbuf *buf) > +{ > + struct perf_pmu_format *format; > + __u64 masks = 0, bits; > + int i; > + > + list_for_each_entry(format, &pmu->format, list) { > + /* > + * Skip extra configs such as config1/config2. > + */ > + if (format->value > 0) > + continue; > + > + for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) > + masks |= 1ULL << i; > + } > + > + /* > + * Kernel doesn't export any valid format bits. > + */ > + if (masks == 0) > + return true; > + > + bits = config & ~masks; > + if (bits == 0) > + return true; so in here you now that there's something wrong, so why bother with the outside strbuf, when we can easily just do all the printing in here? > + > + for (i = 0; i < PERF_PMU_FORMAT_BITS; i++) { > + if (bits & 0x1) > + strbuf_addf(buf, " %d", i); > + > + bits >>= 1; could you use the for_each_set_bit in here? thanks, jirka