From: Namhyung Kim <namhyung@kernel.org>
To: Wang Yan <wangyan01@kylinos.cn>
Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
james.clark@linaro.org, leo.yan@arm.com, xiaqinxin@huawei.com,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] perf metricgroup: Fix memory leak of metric_name in metricgroup__copy_metric_events
Date: Tue, 14 Jul 2026 11:06:12 -0700 [thread overview]
Message-ID: <alZ6lOYQpwD5mweL@google.com> (raw)
In-Reply-To: <20260710032241.119031-1-wangyan01@kylinos.cn>
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
>
prev parent reply other threads:[~2026-07-14 18:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alZ6lOYQpwD5mweL@google.com \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=leo.yan@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=wangyan01@kylinos.cn \
--cc=xiaqinxin@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.