* [PATCH] perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events
@ 2026-07-10 3:22 Wang Yan
2026-07-14 18:06 ` Namhyung Kim
0 siblings, 1 reply; 2+ messages in thread
From: Wang Yan @ 2026-07-10 3:22 UTC (permalink / raw)
To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
jolsa, irogers, adrian.hunter, james.clark
Cc: leo.yan, wangyan01, xiaqinxin, linux-perf-users, linux-kernel
In metricgroup__copy_metric_events(), new_expr->metric_name is allocated
via strdup() but is not freed in all error paths, leading to a memory
leak when subsequent allocations or evsel lookups fail.
Add the missing zfree() calls:
- On strdup() failure, free the already allocated new_expr.
- On metric_refs allocation failure, free metric_name before freeing
new_expr.
- On metric_events allocation failure, free metric_name in addition
to existing freeing of metric_refs and new_expr.
- On evsel lookup failure, free metric_name along with the other
resources already freed.
Fixes: b214ba8c4275 ("perf tools: Copy metric events properly when expand cgroups")
Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
---
tools/perf/util/metricgroup.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 69bfa2a723b2..96dd780a5601 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -1693,8 +1693,10 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
new_expr->metric_expr = old_expr->metric_expr;
new_expr->metric_threshold = old_expr->metric_threshold;
new_expr->metric_name = strdup(old_expr->metric_name);
- if (!new_expr->metric_name)
+ if (!new_expr->metric_name) {
+ free(new_expr);
return -ENOMEM;
+ }
new_expr->metric_unit = old_expr->metric_unit;
new_expr->runtime = old_expr->runtime;
@@ -1707,6 +1709,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
alloc_size = sizeof(*new_expr->metric_refs);
new_expr->metric_refs = calloc(nr + 1, alloc_size);
if (!new_expr->metric_refs) {
+ zfree(&new_expr->metric_name);
free(new_expr);
return -ENOMEM;
}
@@ -1724,6 +1727,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
new_expr->metric_events = calloc(nr + 1, alloc_size);
if (!new_expr->metric_events) {
zfree(&new_expr->metric_refs);
+ zfree(&new_expr->metric_name);
free(new_expr);
return -ENOMEM;
}
@@ -1735,6 +1739,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
if (evsel == NULL) {
zfree(&new_expr->metric_events);
zfree(&new_expr->metric_refs);
+ zfree(&new_expr->metric_name);
free(new_expr);
return -EINVAL;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events
2026-07-10 3:22 [PATCH] perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events Wang Yan
@ 2026-07-14 18:06 ` Namhyung Kim
0 siblings, 0 replies; 2+ messages in thread
From: Namhyung Kim @ 2026-07-14 18:06 UTC (permalink / raw)
To: Wang Yan
Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
irogers, adrian.hunter, james.clark, leo.yan, xiaqinxin,
linux-perf-users, linux-kernel
Hello,
On Fri, Jul 10, 2026 at 11:22:41AM +0800, Wang Yan wrote:
> In metricgroup__copy_metric_events(), new_expr->metric_name is allocated
> via strdup() but is not freed in all error paths, leading to a memory
> leak when subsequent allocations or evsel lookups fail.
>
> Add the missing zfree() calls:
>
> - On strdup() failure, free the already allocated new_expr.
> - On metric_refs allocation failure, free metric_name before freeing
> new_expr.
> - On metric_events allocation failure, free metric_name in addition
> to existing freeing of metric_refs and new_expr.
> - On evsel lookup failure, free metric_name along with the other
> resources already freed.
>
> Fixes: b214ba8c4275 ("perf tools: Copy metric events properly when expand cgroups")
> Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
It's already fixed in
https://lore.kernel.org/r/20260602081104.271325-1-pengyu@kylinos.cn
Thanks,
Namhyung
> ---
> tools/perf/util/metricgroup.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 69bfa2a723b2..96dd780a5601 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -1693,8 +1693,10 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
> new_expr->metric_expr = old_expr->metric_expr;
> new_expr->metric_threshold = old_expr->metric_threshold;
> new_expr->metric_name = strdup(old_expr->metric_name);
> - if (!new_expr->metric_name)
> + if (!new_expr->metric_name) {
> + free(new_expr);
> return -ENOMEM;
> + }
>
> new_expr->metric_unit = old_expr->metric_unit;
> new_expr->runtime = old_expr->runtime;
> @@ -1707,6 +1709,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
> alloc_size = sizeof(*new_expr->metric_refs);
> new_expr->metric_refs = calloc(nr + 1, alloc_size);
> if (!new_expr->metric_refs) {
> + zfree(&new_expr->metric_name);
> free(new_expr);
> return -ENOMEM;
> }
> @@ -1724,6 +1727,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
> new_expr->metric_events = calloc(nr + 1, alloc_size);
> if (!new_expr->metric_events) {
> zfree(&new_expr->metric_refs);
> + zfree(&new_expr->metric_name);
> free(new_expr);
> return -ENOMEM;
> }
> @@ -1735,6 +1739,7 @@ int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
> if (evsel == NULL) {
> zfree(&new_expr->metric_events);
> zfree(&new_expr->metric_refs);
> + zfree(&new_expr->metric_name);
> free(new_expr);
> return -EINVAL;
> }
> --
> 2.25.1
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-14 18:06 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-10 3:22 [PATCH] perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events Wang Yan
2026-07-14 18:06 ` Namhyung Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox