From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3 04/15] perf python: Fix memory leak in pyrf_evlist__get_pollfd
Date: Sun, 09 Aug 2026 07:33:29 +0000 [thread overview]
Message-ID: <20260809073330.403E61F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260809071455.1221514-5-irogers@google.com>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but missing NULL check on the return value of `PyLong_FromLong` in `pyrf_counts_values_get_values` leads to a crash.
- [High] This problem wasn't introduced by this patch, but there is a memory leak of `key` and `value` objects on the success path of `PyDict_SetItem` in `pyrf__metrics_cb`.
--
--- Patch [4]: [PATCH v3 04/15] perf python: Fix memory leak in pyrf_evlist__get_pollfd ---
commit 4d7f49a1892b79c6131283be18ebf7cc1b0fd24f
Author: Ian Rogers <irogers@google.com>
perf python: Fix memory leak in pyrf_evlist__get_pollfd
This patch fixes a Python list object leak in pyrf_evlist__get_pollfd() by
adding a missing Py_DECREF on an error exit path. It also adds a check
for list allocation failure.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index af85c0771c448..c7ae1f85aec6d 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
> @@ -2825,6 +2825,8 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
>
> evlist = pevlist->evlist;
> list = PyList_New(0);
> + if (!list)
> + return NULL;
>
> for (i = 0; i < evlist__core(evlist)->pollfd.nr; ++i) {
> PyObject *file;
> @@ -2843,6 +2845,7 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
>
> return list;
> free_list:
> + Py_XDECREF(list);
> return PyErr_NoMemory();
> }
[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential crash
here in pyrf_counts_values_get_values() when memory is exhausted:
tools/perf/util/python.c:pyrf_counts_values_get_values() {
...
PyObject *vals = PyList_New(5);
if (!vals)
return NULL;
for (int i = 0; i < 5; i++)
PyList_SetItem(vals, i, PyLong_FromLong(self->values.values[i]));
return vals;
}
Can PyLong_FromLong() return a null pointer during memory pressure?
If so, PyList_SetItem() will place the null pointer directly into the list's
internal array without validation.
Could this cause operations like PyObject_Repr to later dereference the
pointer and crash the Python interpreter abruptly instead of raising a
MemoryError?
[Severity: High]
This is a pre-existing issue, but there appears to be a memory leak in
pyrf__metrics_cb():
tools/perf/util/python.c:pyrf__metrics_cb() {
...
PyObject *key = dict ? PyUnicode_FromString("MetricGroup") : NULL;
PyObject *value = key ? pyrf__metrics_groups(pm) : NULL;
if (!value || PyDict_SetItem(dict, key, value) != 0) {
Py_XDECREF(key);
Py_XDECREF(value);
Py_XDECREF(dict);
return -ENOMEM;
}
if (!add_to_dict(dict, "MetricName", pm->metric_name) ||
...
}
Does the success path of PyDict_SetItem leak the key and value objects?
The error path correctly drops the references, but if PyDict_SetItem
succeeds, it does not steal the references.
Since this function is called as a callback in pmu_for_each_sys_metric(),
will this unconditionally leak both objects for every valid system metric
processed?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260809071455.1221514-1-irogers@google.com?part=4
next prev parent reply other threads:[~2026-08-09 7:33 UTC|newest]
Thread overview: 74+ 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-07-04 16:55 ` Namhyung Kim
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
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
2026-07-04 16:54 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Namhyung Kim
2026-07-04 23:03 ` Ian Rogers
2026-07-06 23:49 ` Namhyung Kim
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
2026-08-09 7:14 ` [PATCH v3 01/15] perf script: Fix metric_evlist leak in script_find_metrics Ian Rogers
2026-08-09 7:37 ` sashiko-bot
2026-08-09 7:14 ` [PATCH v3 02/15] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
2026-08-09 7:14 ` [PATCH v3 03/15] perf tools: Fix sb_evlist leaks in top and record Ian Rogers
2026-08-09 7:14 ` [PATCH v3 04/15] perf python: Fix memory leak in pyrf_evlist__get_pollfd Ian Rogers
2026-08-09 7:33 ` sashiko-bot [this message]
2026-08-09 7:14 ` [PATCH v3 05/15] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
2026-08-09 7:14 ` [PATCH v3 06/15] perf test: Fix skiplist leak in cmd_test Ian Rogers
2026-08-09 7:14 ` [PATCH v3 07/15] perf python: Check counts_values size in set_values Ian Rogers
2026-08-09 7:14 ` [PATCH v3 08/15] perf python: Validate CPU and thread maps in pyrf_evsel__open Ian Rogers
2026-08-09 7:14 ` [PATCH v3 09/15] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
2026-08-09 7:14 ` [PATCH v3 10/15] perf python: Zero initialize perf_data in pyrf_data__init Ian Rogers
2026-08-09 7:14 ` [PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks Ian Rogers
2026-08-09 7:28 ` sashiko-bot
2026-08-10 5:02 ` Namhyung Kim
2026-08-10 5:41 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 12/15] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
2026-08-09 7:27 ` sashiko-bot
2026-08-09 7:14 ` [PATCH v3 13/15] perf python: Fix count_values memory leak in pyrf_evsel__read Ian Rogers
2026-08-09 7:14 ` [PATCH v3 14/15] perf python: Fix memory leak in pyrf__metrics_cb Ian Rogers
2026-08-09 7:14 ` [PATCH v3 15/15] 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=20260809073330.403E61F000E9@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