Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5145220pxj; Wed, 9 Jun 2021 10:08:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwStH4JfHCBxSeVKfGPuc8cgGEMWFdCOKn9E89pZtz1PO35fO/n1esViGpjEy99DdQn+yR2 X-Received: by 2002:a05:6402:51d0:: with SMTP id r16mr491824edd.138.1623258498614; Wed, 09 Jun 2021 10:08:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623258498; cv=none; d=google.com; s=arc-20160816; b=Uia8/Xod1DOXAZ/7XlMDCcki5w4lhfjnlZAwn+U14imiThMdicaWfXOxfu9UEO341f 6mJxW5EaSh29LgBfnEDhbHe/V1YiW9xrJKaA7qV8goWFyt4eBiRaFVccJhlaywGqYHUn vMya/JKkVoMYEYJoE3J5ubIfRts8Y7IOsbyHyJPRCKNv58GKUjj5Ab4DyiAcmdv3nYiX hxbtb3TdX2JFvZ797JYufhQIieso8Htk6X88X+eY6ldsStKkSBzrJ8aCbMLLf2KA86h1 Beqx1ZR4Q4ys2y1TZWJn6WZBebe9tCRWYkl1PahVOOB5s7W2Y8c2778WY/1HQOnpT3wy a7fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:ironport-sdr :ironport-sdr; bh=Tv7qiNULmQCIgumEEDABn0n0X6gE9coPJMi9CqzheDw=; b=G5j+Ffm5nObf/e1lWQrJFYw09Hrq70nNffWUJUVMgKDctLPaWB6usOAyTWbd8zC8d2 L7ff5cBYRcRVve3xI07SZMHgrcKjQC2F/YE9RaTGA26wA+TseAce5DsQ7TQd/xMeRRcX VbAnuaO3Ovr6lHsIr7gGerjoZcv0cg0JFC9DVE54Y5THuHbxZAOChNvFNrXmOGFcip3E hFf5V6MZ417Iy6INnpzDH8lK1YXzOJRKbPNSJePMHZyYUEsvyKBvenyyapqfcmuqMWwv pNhu0gTAHugoRf5Hwkak+wgj/VaPOy/QRRvmFb/4WHmIALdkrjsVyt3PVTyhxRzrPRJz Nzgw== 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n21si236085edy.22.2021.06.09.10.07.54; Wed, 09 Jun 2021 10:08:18 -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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230298AbhFIFAf (ORCPT + 99 others); Wed, 9 Jun 2021 01:00:35 -0400 Received: from mga02.intel.com ([134.134.136.20]:24557 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229792AbhFIFAe (ORCPT ); Wed, 9 Jun 2021 01:00:34 -0400 IronPort-SDR: 4BRBdw8GXfMuYpmcVIEpt3O0GG0uZAwQwlZ879WE0vDHJJmWazbIcJzCvf7CTJMvRFEjzJZBM9 JLPO+F6MEOow== X-IronPort-AV: E=McAfee;i="6200,9189,10009"; a="192111653" X-IronPort-AV: E=Sophos;i="5.83,260,1616482800"; d="scan'208";a="192111653" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jun 2021 21:58:40 -0700 IronPort-SDR: kymJqfXxEZAvsDP2HquRRN4m+j4rcSiU+QfQenxQ593h845nNfs1PxI1/eTFlFO6ZTDhIL8S1Y zkrwmlpu6RoA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,260,1616482800"; d="scan'208";a="476843351" Received: from kbl-ppc.sh.intel.com ([10.239.159.163]) by FMSMGA003.fm.intel.com with ESMTP; 08 Jun 2021 21:58:37 -0700 From: Jin Yao To: acme@kernel.org, jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com, alexander.shishkin@linux.intel.com Cc: Linux-kernel@vger.kernel.org, ak@linux.intel.com, kan.liang@intel.com, yao.jin@intel.com, Jin Yao Subject: [PATCH v1] perf tools: Fix pattern matching for same substring used in different pmu type Date: Wed, 9 Jun 2021 12:57:38 +0800 Message-Id: <20210609045738.1051-1-yao.jin@linux.intel.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some different pmu types may have same substring. For example, on Icelake server, we have pmu types "uncore_imc" and "uncore_imc_free_running". Both pmu types have substring "uncore_imc". But the parser would wrongly think they are the same pmu type. We enable an imc event, perf stat -e uncore_imc/event=0xe3/ -a -- sleep 1 Perf actually expands the event to: uncore_imc_0/event=0xe3/ uncore_imc_1/event=0xe3/ uncore_imc_2/event=0xe3/ uncore_imc_3/event=0xe3/ uncore_imc_4/event=0xe3/ uncore_imc_5/event=0xe3/ uncore_imc_6/event=0xe3/ uncore_imc_7/event=0xe3/ uncore_imc_free_running_0/event=0xe3/ uncore_imc_free_running_1/event=0xe3/ uncore_imc_free_running_3/event=0xe3/ uncore_imc_free_running_4/event=0xe3/ That's because the "uncore_imc_free_running" matches the pattern "uncore_imc*". Now we check that the last characters of pmu name is '_'. Fixes: b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in dynamic pmu events") Signed-off-by: Jin Yao --- tools/perf/util/parse-events.y | 2 ++ tools/perf/util/pmu.c | 25 ++++++++++++++++++++++++- tools/perf/util/pmu.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y index aba12a4d488e..7a694c7f7f1a 100644 --- a/tools/perf/util/parse-events.y +++ b/tools/perf/util/parse-events.y @@ -317,6 +317,8 @@ event_pmu_name opt_pmu_config strncmp($1, "uncore_", 7)) name += 7; if (!fnmatch(pattern, name, 0)) { + if (!perf_pmu__valid_suffix($1, name)) + continue; if (parse_events_copy_term_list(orig_terms, &terms)) CLEANUP_YYABORT; if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true, false)) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 88c8ecdc60b0..78af01959830 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -768,7 +769,7 @@ bool pmu_uncore_alias_match(const char *pmu_name, const char *name) */ for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) { name = strstr(name, tok); - if (!name) { + if (!name || !perf_pmu__valid_suffix(tok, (char *)name)) { res = false; goto out; } @@ -1872,3 +1873,25 @@ bool perf_pmu__has_hybrid(void) return !list_empty(&perf_pmu__hybrid_pmus); } + +bool perf_pmu__valid_suffix(char *tok, char *pmu_name) +{ + char *p; + + /* + * The pmu_name has substring tok. If the format of + * pmu_name is or _, return true. + */ + p = pmu_name + strlen(tok); + if (*p == 0) + return true; + + if (*p != '_') + return false; + + ++p; + if (*p == 0 || !isdigit(*p)) + return false; + + return true; +} diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index a790ef758171..ebfd2b71532b 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -133,5 +133,6 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, char *name); bool perf_pmu__has_hybrid(void); +bool perf_pmu__valid_suffix(char *tok, char *pmu_name); #endif /* __PMU_H */ -- 2.17.1