From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v2 14/16] perf python: Fix count_values memory leak in pyrf_evsel__read
Date: Wed, 24 Jun 2026 05:39:55 +0000 [thread overview]
Message-ID: <20260624053956.21BB51F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260624051535.3584977-16-irogers@google.com>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] A partially failed allocation in `evsel__ensure_counts()` results in a NULL pointer dereference when retrying `pyrf_evsel__read()`.
- [High] The `evsel->prev_raw_counts` array allocated for Python `perf.evsel` objects is unconditionally leaked when the object is destroyed.
- [Medium] String duplication leak in `prepare_metric` on error paths.
--
commit 0bdfb7eb04b252f781d1daa5947d46bec1d9df82
Author: Ian Rogers <irogers@google.com>
perf python: Fix count_values memory leak in pyrf_evsel__read
In pyrf_evsel__read, if PyArg_ParseTuple fails, the allocated count_values
is leaked. Move the allocation of count_values after the PyArg_ParseTuple
call to prevent the memory leak.
> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index b17b191b16255..e76a407cb2bb9 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
> @@ -2204,11 +2204,6 @@ static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel,
>
> CHECK_INITIALIZED(evsel, "evsel");
>
> - count_values = PyObject_New(struct pyrf_counts_values,
> - &pyrf_counts_values__type);
> - if (!count_values)
> - return NULL;
> -
> if (!PyArg_ParseTuple(args, "ii", &cpu, &thread))
> return NULL;
>
> @@ -2227,6 +2222,10 @@ static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel,
> if (evsel__ensure_counts(evsel))
> return PyErr_NoMemory();
>
> + count_values = PyObject_New(struct pyrf_counts_values, &pyrf_counts_values__type);
> + if (!count_values)
> + return NULL;
> +
> /* Set up pointers to the old and newly read counter values. */
> old_count = perf_counts(evsel->prev_raw_counts, cpu_idx, thread_idx);
[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential NULL
pointer dereference here.
If evsel__ensure_counts() successfully allocates evsel->counts but fails to
allocate evsel->prev_raw_counts, it returns -ENOMEM. If a user script
handles the resulting Python MemoryError and retries the read operation,
evsel__ensure_counts() will see that evsel->counts is no longer NULL and
return success immediately, without allocating evsel->prev_raw_counts.
Will this cause perf_counts() to dereference a NULL prev_raw_counts pointer
on the retry?
[Severity: High]
This is also a pre-existing issue, but does evsel->prev_raw_counts leak
when the object is destroyed?
When a standalone Python evsel object is destroyed, its destruction path
goes through pyrf_evsel__delete() -> evsel__put() -> evsel__exit(). While
evsel__exit() properly calls evsel__free_counts() to free evsel->counts,
it seems to miss freeing evsel->prev_raw_counts.
Could this unconditionally drain memory if Python evsel objects are used in
a loop?
> new_count = perf_counts(evsel->counts, cpu_idx, thread_idx);
[ ... ]
[Severity: Medium]
This is another pre-existing issue not caused by this patch, but looking at
prepare_metric() in the same file, does this error path leak the duplicated
string n?
tools/perf/util/python.c:prepare_metric() {
...
char *n = strdup(evsel__metric_id(cur));
if (!n)
return -ENOMEM;
...
ret = evsel__ensure_counts(cur);
if (ret)
return ret;
...
ret = expr__add_id_val_source_count(pctx, n, val, source_count);
if (ret)
return ret;
...
}
If evsel__ensure_counts() or expr__add_id_val_source_count() returns an
error, the function returns early without freeing n. Should we ensure n is
freed on these error paths?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260624051535.3584977-1-irogers@google.com?part=14
next prev parent reply other threads:[~2026-06-24 5:39 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-23 5:35 [PATCH v1 00/13] perf python: Fix python extension bugs and memory leaks Ian Rogers
2026-06-23 5:35 ` [PATCH v1 01/13] perf script: Fix metric_evlist leak in script_find_metrics Ian Rogers
2026-06-23 5:35 ` [PATCH v1 02/13] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
2026-06-23 5:35 ` [PATCH v1 03/13] perf top: Fix sb_evlist leak Ian Rogers
2026-06-23 21:26 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 04/13] perf python: Fix memory leak in pyrf_evlist__get_pollfd Ian Rogers
2026-06-23 21:34 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 05/13] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
2026-06-23 21:41 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 06/13] perf data: Fix directory file descriptor leak in perf_data__switch Ian Rogers
2026-06-23 5:35 ` [PATCH v1 07/13] perf test: Fix skiplist leak in cmd_test Ian Rogers
2026-06-23 5:35 ` [PATCH v1 08/13] perf python: Check counts_values size in set_values Ian Rogers
2026-06-23 22:08 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 09/13] perf python: Validate CPU and thread maps in pyrf_evsel__open Ian Rogers
2026-06-23 22:17 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 10/13] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
2026-06-23 5:35 ` [PATCH v1 11/13] perf python: Zero initialize perf_data in pyrf_data__init Ian Rogers
2026-06-23 22:44 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 12/13] perf python: Add thread uninitialized checks Ian Rogers
2026-06-23 22:49 ` sashiko-bot
2026-06-23 5:35 ` [PATCH v1 13/13] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
2026-06-23 22:41 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
2026-06-24 5:15 ` [PATCH v2 01/16] perf script: Fix metric_evlist leak in script_find_metrics Ian Rogers
2026-06-24 5:15 ` [PATCH v2 02/16] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
2026-06-24 5:15 ` [PATCH v2 03/16] perf tools: Fix sb_evlist leaks in top and record Ian Rogers
2026-06-24 5:15 ` [PATCH v2 03/16] perf top: Fix sb_evlist leak Ian Rogers
2026-06-24 5:15 ` [PATCH v2 04/16] perf python: Fix memory leak in pyrf_evlist__get_pollfd Ian Rogers
2026-06-24 5:28 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 05/16] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
2026-06-24 5:27 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 06/16] perf data: Fix directory file descriptor leak in perf_data__switch Ian Rogers
2026-06-24 5:30 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 07/16] perf test: Fix skiplist leak in cmd_test Ian Rogers
2026-06-24 5:28 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 08/16] perf python: Check counts_values size in set_values Ian Rogers
2026-06-24 7:30 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 09/16] perf python: Validate CPU and thread maps in pyrf_evsel__open Ian Rogers
2026-06-24 5:15 ` [PATCH v2 10/16] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
2026-06-24 5:31 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 11/16] perf python: Zero initialize perf_data in pyrf_data__init Ian Rogers
2026-06-24 5:15 ` [PATCH v2 12/16] perf python: Add thread and PMU uninitialized checks Ian Rogers
2026-06-24 5:34 ` sashiko-bot
2026-06-24 5:15 ` [PATCH v2 13/16] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
2026-06-24 5:15 ` [PATCH v2 14/16] perf python: Fix count_values memory leak in pyrf_evsel__read Ian Rogers
2026-06-24 5:39 ` sashiko-bot [this message]
2026-06-24 5:15 ` [PATCH v2 15/16] perf python: Fix memory leak in pyrf__metrics_cb Ian Rogers
2026-06-24 5:15 ` [PATCH v2 16/16] perf synthetic-events: Fix divide by zero in perf_event__synthesize_threads Ian Rogers
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=20260624053956.21BB51F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=irogers@google.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox