* [PATCH v1] perf parse: Avoid false uncore matches
@ 2020-04-16 16:21 Andi Kleen
2020-04-16 22:22 ` Jiri Olsa
0 siblings, 1 reply; 2+ messages in thread
From: Andi Kleen @ 2020-04-16 16:21 UTC (permalink / raw)
To: acme; +Cc: jolsa, linux-kernel, Andi Kleen
From: Andi Kleen <ak@linux.intel.com>
The PMU matching adds an implicit '*' to match multiple instances of PMUs.
But this causes problems when the user specified an explicit number,
in this case longer numbers with the same prefix could be matched too,
which is not intended.
Check for the number case and avoid the extra wildcard.
Also this fixes a memory leak with the pattern match. We would not
free the allocated pattern.
Before:
$ perf stat --no-merge -e cha_1// -a sleep 1
Performance counter stats for 'system wide':
698,393,431 uncore_cha_1//
698,395,491 uncore_cha_14//
698,393,873 uncore_cha_12//
698,392,975 uncore_cha_10//
698,391,167 uncore_cha_17//
698,390,371 uncore_cha_15//
698,389,007 uncore_cha_13//
698,388,069 uncore_cha_11//
698,386,991 uncore_cha_16//
1.001343184 seconds time elapsed
After
$ ./perf stat --no-merge -e cha_1// -a sleep 1
Performance counter stats for 'system wide':
741,490,611 uncore_cha_1//
1.000847326 seconds time elapsed
$
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
tools/perf/util/parse-events.y | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
index 94f8bcd83582..f38c90756476 100644
--- a/tools/perf/util/parse-events.y
+++ b/tools/perf/util/parse-events.y
@@ -286,7 +286,8 @@ PE_NAME opt_pmu_config
parse_events_terms__delete(orig_terms); \
free(list); \
free($1); \
- free(pattern); \
+ if ($1 != pattern) \
+ free(pattern); \
YYABORT; \
} while(0)
@@ -303,7 +304,9 @@ PE_NAME opt_pmu_config
struct perf_pmu *pmu = NULL;
int ok = 0;
- if (asprintf(&pattern, "%s*", $1) < 0)
+ if ($1[0] && isdigit($1[strlen($1) - 1]))
+ pattern = $1;
+ else if (asprintf(&pattern, "%s*", $1) < 0)
CLEANUP_YYABORT;
while ((pmu = perf_pmu__scan(pmu)) != NULL) {
@@ -320,6 +323,8 @@ PE_NAME opt_pmu_config
parse_events_terms__delete(terms);
}
}
+ if ($1 != pattern)
+ free(pattern);
if (!ok)
CLEANUP_YYABORT;
--
2.24.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v1] perf parse: Avoid false uncore matches
2020-04-16 16:21 [PATCH v1] perf parse: Avoid false uncore matches Andi Kleen
@ 2020-04-16 22:22 ` Jiri Olsa
0 siblings, 0 replies; 2+ messages in thread
From: Jiri Olsa @ 2020-04-16 22:22 UTC (permalink / raw)
To: Andi Kleen; +Cc: acme, jolsa, linux-kernel, Andi Kleen
On Thu, Apr 16, 2020 at 09:21:19AM -0700, Andi Kleen wrote:
SNIP
> ---
> tools/perf/util/parse-events.y | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
> index 94f8bcd83582..f38c90756476 100644
> --- a/tools/perf/util/parse-events.y
> +++ b/tools/perf/util/parse-events.y
> @@ -286,7 +286,8 @@ PE_NAME opt_pmu_config
> parse_events_terms__delete(orig_terms); \
> free(list); \
> free($1); \
> - free(pattern); \
> + if ($1 != pattern) \
> + free(pattern); \
> YYABORT; \
> } while(0)
>
> @@ -303,7 +304,9 @@ PE_NAME opt_pmu_config
> struct perf_pmu *pmu = NULL;
> int ok = 0;
>
> - if (asprintf(&pattern, "%s*", $1) < 0)
> + if ($1[0] && isdigit($1[strlen($1) - 1]))
> + pattern = $1;
> + else if (asprintf(&pattern, "%s*", $1) < 0)
> CLEANUP_YYABORT;
>
> while ((pmu = perf_pmu__scan(pmu)) != NULL) {
> @@ -320,6 +323,8 @@ PE_NAME opt_pmu_config
> parse_events_terms__delete(terms);
> }
> }
> + if ($1 != pattern)
> + free(pattern);
good catch
Acked-by: Jiri Olsa <jolsa@kernel.org>
thanks,
jirka
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-04-16 22:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-16 16:21 [PATCH v1] perf parse: Avoid false uncore matches Andi Kleen
2020-04-16 22:22 ` Jiri Olsa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).