* [PATCH v2 01/16] perf script: Fix metric_evlist leak in script_find_metrics
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 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 02/16] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
` (17 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in script_find_metrics() where metric_evlist is leaked
when returning early on error paths.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 3622990efaab ("perf script: Change metric format to use json metrics")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-script.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 47afd8cdc2b7..6b69e982ba62 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -2275,8 +2275,8 @@ static int script_find_metrics(const struct pmu_metric *pm,
}
pr_debug("Found metric '%s' whose evsels match those of in the perf data\n",
pm->metric_name);
- evlist__put(metric_evlist);
out:
+ evlist__put(metric_evlist);
return 0;
}
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 02/16] perf stat: Fix evsel_list leak in cmd_stat
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 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 03/16] perf tools: Fix sb_evlist leaks in top and record Ian Rogers
` (16 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_stat() where evsel_list is leaked if an error
occurs while opening the output file.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 361c99a661a7 ("perf evsel: Introduce perf_evlist")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-stat.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 3f897b2e8638..7cb6ea937e87 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -2718,7 +2718,8 @@ int cmd_stat(int argc, const char **argv)
output = fopen(output_name, mode);
if (!output) {
perror("failed to create output file");
- return -1;
+ status = -1;
+ goto out;
}
if (!stat_config.json_output) {
clock_gettime(CLOCK_REALTIME, &tm);
@@ -2729,7 +2730,8 @@ int cmd_stat(int argc, const char **argv)
output = fdopen(output_fd, mode);
if (!output) {
perror("Failed opening logfd");
- return -errno;
+ status = -errno;
+ goto out;
}
}
@@ -2738,7 +2740,8 @@ int cmd_stat(int argc, const char **argv)
parse_options_usage(stat_usage, stat_options, "o", 1);
parse_options_usage(NULL, stat_options, "log-fd", 0);
parse_options_usage(NULL, stat_options, "interval-clear", 0);
- return -1;
+ status = -1;
+ goto out;
}
stat_config.output = output;
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 03/16] perf tools: Fix sb_evlist leaks in top and record
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 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 03/16] perf top: Fix sb_evlist leak Ian Rogers
` (15 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_top() where top.sb_evlist was not freed if
evlist__add_bpf_sb_event() fails. Note that evlist__start_sb_thread() and
evlist__stop_sb_thread() take ownership of the evlist and free it, so
we must only free it if we fail before starting the thread. Also set
top.sb_evlist to NULL to prevent use-after-free bugs.
Apply the same fix to builtin-record.c to avoid leaking rec->sb_evlist
and calling pthread_join on uninitialized threads in the error path.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: b38d85ef49cf ("perf bpf: Decouple creating the evlist from adding the SB event")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-record.c | 2 ++
tools/perf/builtin-top.c | 2 ++
2 files changed, 4 insertions(+)
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index ebd3ed0c9b3e..b106d50f6d97 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -2247,6 +2247,8 @@ static int record__setup_sb_evlist(struct record *rec)
if (evlist__add_bpf_sb_event(rec->sb_evlist, perf_session__env(rec->session))) {
pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
+ evlist__put(rec->sb_evlist);
+ rec->sb_evlist = NULL;
return -1;
}
}
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 5933c46ee137..570410599f1b 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1881,6 +1881,8 @@ int cmd_top(int argc, const char **argv)
if (evlist__add_bpf_sb_event(top.sb_evlist, &host_env)) {
pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
status = -EINVAL;
+ evlist__put(top.sb_evlist);
+ top.sb_evlist = NULL;
goto out_put_evlist;
}
}
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 03/16] perf top: Fix sb_evlist leak
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (2 preceding siblings ...)
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 ` 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
` (14 subsequent siblings)
18 siblings, 1 reply; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_top() where top.sb_evlist was not freed if
evlist__add_bpf_sb_event() fails. Note that evlist__start_sb_thread() and
evlist__stop_sb_thread() take ownership of the evlist and free it, so
we must only free it if we fail before starting the thread.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: b38d85ef49cf ("perf bpf: Decouple creating the evlist from adding the SB event")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-top.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 5933c46ee137..abc74b8ab759 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1881,6 +1881,7 @@ int cmd_top(int argc, const char **argv)
if (evlist__add_bpf_sb_event(top.sb_evlist, &host_env)) {
pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
status = -EINVAL;
+ evlist__put(top.sb_evlist);
goto out_put_evlist;
}
}
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* Re: [PATCH v2 03/16] perf top: Fix sb_evlist leak
2026-06-24 5:15 ` [PATCH v2 03/16] perf top: Fix sb_evlist leak Ian Rogers
@ 2026-07-04 16:55 ` Namhyung Kim
0 siblings, 0 replies; 53+ messages in thread
From: Namhyung Kim @ 2026-07-04 16:55 UTC (permalink / raw)
To: Ian Rogers
Cc: acme, adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
On Tue, Jun 23, 2026 at 10:15:21PM -0700, Ian Rogers wrote:
> Fix a memory leak in cmd_top() where top.sb_evlist was not freed if
> evlist__add_bpf_sb_event() fails. Note that evlist__start_sb_thread() and
> evlist__stop_sb_thread() take ownership of the evlist and free it, so
> we must only free it if we fail before starting the thread.
>
> Assisted-by: Antigravity:gemini-3.1-pro
> Fixes: b38d85ef49cf ("perf bpf: Decouple creating the evlist from adding the SB event")
> Signed-off-by: Ian Rogers <irogers@google.com>
Looks like a leftover from v1. There are two 03/16 patches. :)
Thanks,
Namhyung
^ permalink raw reply [flat|nested] 53+ messages in thread
* [PATCH v2 04/16] perf python: Fix memory leak in pyrf_evlist__get_pollfd
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (3 preceding siblings ...)
2026-06-24 5:15 ` [PATCH v2 03/16] perf top: Fix sb_evlist leak Ian Rogers
@ 2026-06-24 5:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 05/16] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
` (13 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a Python list object leak in pyrf_evlist__get_pollfd() by adding
a missing Py_DECREF on the error exit path.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 11e1f39c1bea..550f1e7d1449 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();
}
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 05/16] perf synthetic-events: Fix uninitialized pthread_join
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (4 preceding siblings ...)
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:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 06/16] perf data: Fix directory file descriptor leak in perf_data__switch Ian Rogers
` (12 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
In perf_event__synthesize_threads(), fix an uninitialized pthread_join()
call when thread creation fails by only joining the successfully
created threads.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 340b47f510bb ("perf top: Implement multithreading for perf_event__synthesize_threads")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/synthetic-events.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index b75f9dcf4dbf..35de41ad14d3 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1104,8 +1104,8 @@ int perf_event__synthesize_threads(const struct perf_tool *tool,
}
err = 0;
out_join:
- for (i = 0; i < thread_nr; i++)
- pthread_join(synthesize_threads[i], NULL);
+ for (j = 0; j < i; j++)
+ pthread_join(synthesize_threads[j], NULL);
free(args);
free_threads:
free(synthesize_threads);
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 06/16] perf data: Fix directory file descriptor leak in perf_data__switch
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (5 preceding siblings ...)
2026-06-24 5:15 ` [PATCH v2 05/16] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
@ 2026-06-24 5:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 07/16] perf test: Fix skiplist leak in cmd_test Ian Rogers
` (11 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a file descriptor leak in perf_data__switch() when running in
directory mode. By substituting perf_data__close() for
perf_data_file__close(), any thread-local directory files are properly
closed during file rotation.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 040f9915e99e ("perf data: Add perf_data_file__switch() helper")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/data.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index 33fd1b82019e..32d4d908ad38 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -472,8 +472,7 @@ int perf_data__switch(struct perf_data *data,
pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
if (!at_exit) {
- perf_data_file__close(&data->file);
- data->open = false;
+ perf_data__close(data);
ret = perf_data__open(data);
if (ret < 0)
goto out;
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 07/16] perf test: Fix skiplist leak in cmd_test
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (6 preceding siblings ...)
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:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 08/16] perf python: Check counts_values size in set_values Ian Rogers
` (10 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_test() where skiplist was not freed on
exit paths.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 2ae828786c65 ("perf test: Allow skipping tests")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/builtin-test.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 7e75f590f225..cf5625cf4f11 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -1781,9 +1781,12 @@ int cmd_test(int argc, const char **argv)
rlimit__bump_memlock();
suites = build_suites();
- if (!suites)
+ if (!suites) {
+ intlist__delete(skiplist);
return errno ? -errno : -ENOMEM;
+ }
ret = __cmd_test(suites, argc, argv, skiplist);
free(suites);
+ intlist__delete(skiplist);
return ret;
}
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 08/16] perf python: Check counts_values size in set_values
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (7 preceding siblings ...)
2026-06-24 5:15 ` [PATCH v2 07/16] perf test: Fix skiplist leak in cmd_test Ian Rogers
@ 2026-06-24 5:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 09/16] perf python: Validate CPU and thread maps in pyrf_evsel__open Ian Rogers
` (9 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
The set_values function incorrectly assumed the list contained exactly
5 elements. Add a check to prevent out-of-bounds access.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 33 +++++++++++++++++++++++++--------
1 file changed, 25 insertions(+), 8 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 550f1e7d1449..c6c729808a0f 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1881,11 +1881,11 @@ static void pyrf_counts_values__delete(struct pyrf_counts_values *pcounts_values
0, help }
static PyMemberDef pyrf_counts_values_members[] = {
- counts_values_member_def(val, T_ULONG, "Value of event"),
- counts_values_member_def(ena, T_ULONG, "Time for which enabled"),
- counts_values_member_def(run, T_ULONG, "Time for which running"),
- counts_values_member_def(id, T_ULONG, "Unique ID for an event"),
- counts_values_member_def(lost, T_ULONG, "Num of lost samples"),
+ counts_values_member_def(val, T_ULONGLONG, "Value of event"),
+ counts_values_member_def(ena, T_ULONGLONG, "Time for which enabled"),
+ counts_values_member_def(run, T_ULONGLONG, "Time for which running"),
+ counts_values_member_def(id, T_ULONGLONG, "Unique ID for an event"),
+ counts_values_member_def(lost, T_ULONGLONG, "Num of lost samples"),
{ .name = NULL, },
};
@@ -1895,8 +1895,15 @@ static PyObject *pyrf_counts_values_get_values(struct pyrf_counts_values *self,
if (!vals)
return NULL;
- for (int i = 0; i < 5; i++)
- PyList_SetItem(vals, i, PyLong_FromLong(self->values.values[i]));
+ for (int i = 0; i < 5; i++) {
+ PyObject *val = PyLong_FromUnsignedLongLong(self->values.values[i]);
+
+ if (!val) {
+ Py_DECREF(vals);
+ return NULL;
+ }
+ PyList_SetItem(vals, i, val);
+ }
return vals;
}
@@ -1913,13 +1920,23 @@ static int pyrf_counts_values_set_values(struct pyrf_counts_values *self, PyObje
}
size = PyList_Size(list);
+ if (size != 5) {
+ PyErr_SetString(PyExc_ValueError, "List must have exactly 5 entries");
+ return -1;
+ }
+
for (Py_ssize_t i = 0; i < size; i++) {
+ unsigned long long val;
+
item = PyList_GetItem(list, i);
if (!PyLong_Check(item)) {
PyErr_SetString(PyExc_TypeError, "List members should be numbers");
return -1;
}
- self->values.values[i] = PyLong_AsLong(item);
+ val = PyLong_AsUnsignedLongLong(item);
+ if (val == (unsigned long long)-1 && PyErr_Occurred())
+ return -1;
+ self->values.values[i] = val;
}
return 0;
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 09/16] perf python: Validate CPU and thread maps in pyrf_evsel__open
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (8 preceding siblings ...)
2026-06-24 5:15 ` [PATCH v2 08/16] perf python: Check counts_values size in set_values Ian Rogers
@ 2026-06-24 5:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 10/16] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
` (8 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Add explicit Py_TYPE checks to ensure the arguments passed are
actually of the correct pyrf_thread_map and pyrf_cpu_map types.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index c6c729808a0f..4bf37e2afb18 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2108,11 +2108,21 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
&pcpus, &pthreads, &group, &inherit))
return NULL;
- if (pthreads != NULL && pthreads != Py_None)
+ if (pthreads != NULL && pthreads != Py_None) {
+ if (!PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) {
+ PyErr_SetString(PyExc_TypeError, "threads must be a thread_map");
+ return NULL;
+ }
threads = ((struct pyrf_thread_map *)pthreads)->threads;
+ }
- if (pcpus != NULL && pcpus != Py_None)
+ if (pcpus != NULL && pcpus != Py_None) {
+ if (!PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) {
+ PyErr_SetString(PyExc_TypeError, "cpus must be a cpu_map");
+ return NULL;
+ }
cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
+ }
evsel->core.attr.inherit = inherit;
/*
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 10/16] perf python: Validate attribute setters in pyrf_evsel
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (9 preceding siblings ...)
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 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 11/16] perf python: Zero initialize perf_data in pyrf_data__init Ian Rogers
` (7 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
If val is NULL when setting an attribute, PyErr_SetString should be
called as deleting the attribute isn't supported. In addition, ensure
PyErr_Occurred is checked before setting the attribute to avoid setting
a garbage value.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 89 ++++++++++++++++++++++++++++++++++------
1 file changed, 77 insertions(+), 12 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 4bf37e2afb18..657b054f5e51 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2296,6 +2296,11 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
is_true = PyObject_IsTrue(val);
if (is_true < 0)
return -1;
@@ -2307,11 +2312,21 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure
static int pyrf_evsel__set_attr_config(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.config = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.config = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __maybe_unused)
@@ -2326,11 +2341,21 @@ static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __may
static int pyrf_evsel__set_attr_read_format(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.read_format = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.read_format = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure __maybe_unused)
@@ -2345,11 +2370,21 @@ static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure
static int pyrf_evsel__set_attr_sample_period(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.sample_period = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.sample_period = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closure __maybe_unused)
@@ -2364,11 +2399,21 @@ static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closur
static int pyrf_evsel__set_attr_sample_type(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.sample_type = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.sample_type = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_sample_type(PyObject *self, void *closure __maybe_unused)
@@ -2392,11 +2437,21 @@ static PyObject *pyrf_evsel__get_attr_size(PyObject *self, void *closure __maybe
static int pyrf_evsel__set_attr_type(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.type = PyLong_AsUnsignedLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.type = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe_unused)
@@ -2411,11 +2466,21 @@ static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe
static int pyrf_evsel__set_attr_wakeup_events(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.wakeup_events = PyLong_AsUnsignedLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.wakeup_events = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void *closure __maybe_unused)
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 11/16] perf python: Zero initialize perf_data in pyrf_data__init
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (10 preceding siblings ...)
2026-06-24 5:15 ` [PATCH v2 10/16] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
@ 2026-06-24 5:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 12/16] perf python: Add thread and PMU uninitialized checks Ian Rogers
` (6 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Replace path clearing with memset so the entire struct is zeroed,
preventing uninitialized fields from causing errors later.
Fixes: 4cd0142f7dec ("perf python: Add wrapper for perf_data file abstraction")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 657b054f5e51..71be130b38c5 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -3,6 +3,7 @@
#include <Python.h>
#include <inttypes.h>
+#include <string.h>
#include <linux/err.h>
#include <poll.h>
@@ -3621,7 +3622,7 @@ static int pyrf_data__init(struct pyrf_data *pdata, PyObject *args, PyObject *kw
if (pdata->data.open)
perf_data__close(&pdata->data);
free((char *)pdata->data.path);
- pdata->data.path = NULL;
+ memset(&pdata->data, 0, sizeof(pdata->data));
if (fd != -1) {
struct stat st;
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 12/16] perf python: Add thread and PMU uninitialized checks
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (11 preceding siblings ...)
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 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 13/16] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
` (5 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Add CHECK_INITIALIZED checks to the thread attribute getters
(get_pid, get_tid, get_ppid) to prevent crashes if they are accessed
before being properly initialized.
Fixes: 3b96bf7af60d ("perf python: Add python session abstraction wrapping perf's session")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 71be130b38c5..b17b191b1625 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1678,6 +1678,7 @@ static PyObject *pyrf_pmu__name(PyObject *self)
{
struct pyrf_pmu *ppmu = (void *)self;
+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
return PyUnicode_FromString(ppmu->pmu->name);
}
@@ -1730,6 +1731,8 @@ static PyObject *pyrf_pmu__events(PyObject *self)
PyObject *py_list = PyList_New(0);
int ret;
+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
+
if (!py_list)
return NULL;
@@ -3758,21 +3761,25 @@ static PyMethodDef pyrf_thread__methods[] = {
static PyObject *pyrf_thread__get_pid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__pid(pthread->thread));
}
static PyObject *pyrf_thread__get_tid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__tid(pthread->thread));
}
static PyObject *pyrf_thread__get_ppid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__ppid(pthread->thread));
}
static PyObject *pyrf_thread__get_cpu(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__cpu(pthread->thread));
}
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 13/16] perf python: Fix MetricGroup return type in perf.pyi
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (12 preceding siblings ...)
2026-06-24 5:15 ` [PATCH v2 12/16] perf python: Add thread and PMU uninitialized checks Ian Rogers
@ 2026-06-24 5:15 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 14/16] perf python: Fix count_values memory leak in pyrf_evsel__read Ian Rogers
` (4 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
The metrics() function can return a dictionary where the value is either
a string or a list of strings, so the type signature in the stub file
should be Union[str, List[str]].
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/python/perf.pyi | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/perf/python/perf.pyi b/tools/perf/python/perf.pyi
index f266c9baffd0..58b0b3ed819d 100644
--- a/tools/perf/python/perf.pyi
+++ b/tools/perf/python/perf.pyi
@@ -1,5 +1,5 @@
"""Type stubs for the perf Python module."""
-from typing import Callable, Dict, List, Optional, Any, Iterator
+from typing import Callable, Dict, List, Optional, Any, Iterator, Union
def config_get(name: str) -> Optional[str]:
"""Get a configuration value from perf config.
@@ -12,7 +12,7 @@ def config_get(name: str) -> Optional[str]:
"""
...
-def metrics() -> List[Dict[str, str]]:
+def metrics() -> List[Dict[str, Union[str, List[str]]]]:
"""Get a list of available metrics.
Returns:
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 14/16] perf python: Fix count_values memory leak in pyrf_evsel__read
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (13 preceding siblings ...)
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 ` Ian Rogers
2026-06-24 5:15 ` [PATCH v2 15/16] perf python: Fix memory leak in pyrf__metrics_cb Ian Rogers
` (3 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index b17b191b1625..e76a407cb2bb 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);
new_count = perf_counts(evsel->counts, cpu_idx, thread_idx);
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 15/16] perf python: Fix memory leak in pyrf__metrics_cb
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (14 preceding siblings ...)
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:15 ` 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
` (2 subsequent siblings)
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
In pyrf__metrics_cb, PyDict_SetItem does not steal the reference of the
key and value, so they need to be decref'ed after successful insertion
to avoid memory leaks.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index e76a407cb2bb..5471037d04c5 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -3567,6 +3567,8 @@ static int pyrf__metrics_cb(const struct pmu_metric *pm,
Py_XDECREF(dict);
return -ENOMEM;
}
+ Py_DECREF(key);
+ Py_DECREF(value);
if (!add_to_dict(dict, "MetricName", pm->metric_name) ||
!add_to_dict(dict, "PMU", pm->pmu) ||
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v2 16/16] perf synthetic-events: Fix divide by zero in perf_event__synthesize_threads
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (15 preceding siblings ...)
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 ` Ian Rogers
2026-07-04 16:54 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Namhyung Kim
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
18 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-06-24 5:15 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
If scandir() finds no matching tasks in /proc, n is 0. If thread_nr is > 1,
we bypass the single-thread fast path and then clamp thread_nr to n, making
it 0. This results in a divide by zero when calculating num_per_thread.
Handle n <= 1 early to use the single-thread fast path and prevent the
crash.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/synthetic-events.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 35de41ad14d3..19c1f3eaf58d 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1058,7 +1058,7 @@ int perf_event__synthesize_threads(const struct perf_tool *tool,
else
thread_nr = nr_threads_synthesize;
- if (thread_nr <= 1) {
+ if (thread_nr <= 1 || n <= 1) {
err = __perf_event__synthesize_threads(tool, process,
machine,
needs_mmap, mmap_data,
--
2.55.0.rc0.799.gd6f94ed593-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* Re: [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (16 preceding siblings ...)
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 ` Namhyung Kim
2026-07-04 23:03 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
18 siblings, 1 reply; 53+ messages in thread
From: Namhyung Kim @ 2026-07-04 16:54 UTC (permalink / raw)
To: Ian Rogers
Cc: acme, adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Hi Ian,
On Tue, Jun 23, 2026 at 10:15:17PM -0700, Ian Rogers wrote:
> Phase 1 of splitting the v19 review patches into smaller fixes.
> These patches address issues identified in the python extension during
> the review of the v19 script porting series, as well as fixing several
> pre-existing bugs and leaks.
>
> v2:
> - Addressed sashiko review feedback:
> - Fixed 64-bit casting for counts_values by using T_ULONGLONG and PyLong_FromUnsignedLongLong.
> - Replaced strict Py_TYPE checks with PyObject_TypeCheck for thread/cpu maps.
> - Fixed a missing CHECK_INITIALIZED for pyrf_thread__get_cpu, pyrf_pmu__name, pyrf_pmu__events.
> - Included <string.h> where memset was used.
> - Fixed line splitting formatting on PyObject_New in pyrf_evsel__read.
> - Added fixes for memory leaks and issues discovered during review:
> - Fix memory leak of key and value in pyrf__metrics_cb
> - Fix count_values memory leak in pyrf_evsel__read
> - Fix sb_evlist leaks in both top and record paths.
> - Fix potential divide by zero in perf_event__synthesize_threads
>
> v1:
> - Decomposed the single large "Fix python extension bugs" patch into 13 individual fixes.
> - Added Fixes: tags for issues that date back to earlier commits.
> - Addressed checkpatch warnings (line lengths, Signed-off-by, etc).
>
>
> Ian Rogers (16):
> perf script: Fix metric_evlist leak in script_find_metrics
> perf stat: Fix evsel_list leak in cmd_stat
> perf tools: Fix sb_evlist leaks in top and record
> perf python: Fix memory leak in pyrf_evlist__get_pollfd
> perf synthetic-events: Fix uninitialized pthread_join
> perf data: Fix directory file descriptor leak in perf_data__switch
> perf test: Fix skiplist leak in cmd_test
> perf python: Check counts_values size in set_values
> perf python: Validate CPU and thread maps in pyrf_evsel__open
> perf python: Validate attribute setters in pyrf_evsel
> perf python: Zero initialize perf_data in pyrf_data__init
> perf python: Add thread and PMU uninitialized checks
> perf python: Fix MetricGroup return type in perf.pyi
> perf python: Fix count_values memory leak in pyrf_evsel__read
> perf python: Fix memory leak in pyrf__metrics_cb
> perf synthetic-events: Fix divide by zero in
> perf_event__synthesize_threads
Will you send v3? You don't need to fix all pre-existing issues but we
should not introduce new bugs or regressions. I think some of Sashiko
comments found them like data switch and pmu init checks.
Thanks,
Namhyung
>
> tools/perf/builtin-record.c | 2 +
> tools/perf/builtin-script.c | 2 +-
> tools/perf/builtin-stat.c | 9 +-
> tools/perf/builtin-top.c | 2 +
> tools/perf/python/perf.pyi | 4 +-
> tools/perf/tests/builtin-test.c | 5 +-
> tools/perf/util/data.c | 3 +-
> tools/perf/util/python.c | 160 ++++++++++++++++++++++++-----
> tools/perf/util/synthetic-events.c | 6 +-
> 9 files changed, 153 insertions(+), 40 deletions(-)
>
> --
> 2.55.0.rc0.799.gd6f94ed593-goog
>
^ permalink raw reply [flat|nested] 53+ messages in thread* Re: [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review
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
0 siblings, 1 reply; 53+ messages in thread
From: Ian Rogers @ 2026-07-04 23:03 UTC (permalink / raw)
To: Namhyung Kim
Cc: acme, adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
On Sat, Jul 4, 2026 at 9:54 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> Hi Ian,
>
> On Tue, Jun 23, 2026 at 10:15:17PM -0700, Ian Rogers wrote:
> > Phase 1 of splitting the v19 review patches into smaller fixes.
> > These patches address issues identified in the python extension during
> > the review of the v19 script porting series, as well as fixing several
> > pre-existing bugs and leaks.
> >
> > v2:
> > - Addressed sashiko review feedback:
> > - Fixed 64-bit casting for counts_values by using T_ULONGLONG and PyLong_FromUnsignedLongLong.
> > - Replaced strict Py_TYPE checks with PyObject_TypeCheck for thread/cpu maps.
> > - Fixed a missing CHECK_INITIALIZED for pyrf_thread__get_cpu, pyrf_pmu__name, pyrf_pmu__events.
> > - Included <string.h> where memset was used.
> > - Fixed line splitting formatting on PyObject_New in pyrf_evsel__read.
> > - Added fixes for memory leaks and issues discovered during review:
> > - Fix memory leak of key and value in pyrf__metrics_cb
> > - Fix count_values memory leak in pyrf_evsel__read
> > - Fix sb_evlist leaks in both top and record paths.
> > - Fix potential divide by zero in perf_event__synthesize_threads
> >
> > v1:
> > - Decomposed the single large "Fix python extension bugs" patch into 13 individual fixes.
> > - Added Fixes: tags for issues that date back to earlier commits.
> > - Addressed checkpatch warnings (line lengths, Signed-off-by, etc).
> >
> >
> > Ian Rogers (16):
> > perf script: Fix metric_evlist leak in script_find_metrics
> > perf stat: Fix evsel_list leak in cmd_stat
> > perf tools: Fix sb_evlist leaks in top and record
> > perf python: Fix memory leak in pyrf_evlist__get_pollfd
> > perf synthetic-events: Fix uninitialized pthread_join
> > perf data: Fix directory file descriptor leak in perf_data__switch
> > perf test: Fix skiplist leak in cmd_test
> > perf python: Check counts_values size in set_values
> > perf python: Validate CPU and thread maps in pyrf_evsel__open
> > perf python: Validate attribute setters in pyrf_evsel
> > perf python: Zero initialize perf_data in pyrf_data__init
> > perf python: Add thread and PMU uninitialized checks
> > perf python: Fix MetricGroup return type in perf.pyi
> > perf python: Fix count_values memory leak in pyrf_evsel__read
> > perf python: Fix memory leak in pyrf__metrics_cb
> > perf synthetic-events: Fix divide by zero in
> > perf_event__synthesize_threads
>
> Will you send v3? You don't need to fix all pre-existing issues but we
> should not introduce new bugs or regressions. I think some of Sashiko
> comments found them like data switch and pmu init checks.
Thanks Namhyung, I have a v3 coming. I'm working with Sashiko locally
and trying to address as many issues as possible, including minor
pre-existing ones—it's easier than remembering to clean them up later.
Ian
> Thanks,
> Namhyung
>
> >
> > tools/perf/builtin-record.c | 2 +
> > tools/perf/builtin-script.c | 2 +-
> > tools/perf/builtin-stat.c | 9 +-
> > tools/perf/builtin-top.c | 2 +
> > tools/perf/python/perf.pyi | 4 +-
> > tools/perf/tests/builtin-test.c | 5 +-
> > tools/perf/util/data.c | 3 +-
> > tools/perf/util/python.c | 160 ++++++++++++++++++++++++-----
> > tools/perf/util/synthetic-events.c | 6 +-
> > 9 files changed, 153 insertions(+), 40 deletions(-)
> >
> > --
> > 2.55.0.rc0.799.gd6f94ed593-goog
> >
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review
2026-07-04 23:03 ` Ian Rogers
@ 2026-07-06 23:49 ` Namhyung Kim
0 siblings, 0 replies; 53+ messages in thread
From: Namhyung Kim @ 2026-07-06 23:49 UTC (permalink / raw)
To: Ian Rogers
Cc: acme, adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
On Sat, Jul 04, 2026 at 04:03:18PM -0700, Ian Rogers wrote:
> On Sat, Jul 4, 2026 at 9:54 AM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > Hi Ian,
> >
> > On Tue, Jun 23, 2026 at 10:15:17PM -0700, Ian Rogers wrote:
> > > Phase 1 of splitting the v19 review patches into smaller fixes.
> > > These patches address issues identified in the python extension during
> > > the review of the v19 script porting series, as well as fixing several
> > > pre-existing bugs and leaks.
> > >
> > > v2:
> > > - Addressed sashiko review feedback:
> > > - Fixed 64-bit casting for counts_values by using T_ULONGLONG and PyLong_FromUnsignedLongLong.
> > > - Replaced strict Py_TYPE checks with PyObject_TypeCheck for thread/cpu maps.
> > > - Fixed a missing CHECK_INITIALIZED for pyrf_thread__get_cpu, pyrf_pmu__name, pyrf_pmu__events.
> > > - Included <string.h> where memset was used.
> > > - Fixed line splitting formatting on PyObject_New in pyrf_evsel__read.
> > > - Added fixes for memory leaks and issues discovered during review:
> > > - Fix memory leak of key and value in pyrf__metrics_cb
> > > - Fix count_values memory leak in pyrf_evsel__read
> > > - Fix sb_evlist leaks in both top and record paths.
> > > - Fix potential divide by zero in perf_event__synthesize_threads
> > >
> > > v1:
> > > - Decomposed the single large "Fix python extension bugs" patch into 13 individual fixes.
> > > - Added Fixes: tags for issues that date back to earlier commits.
> > > - Addressed checkpatch warnings (line lengths, Signed-off-by, etc).
> > >
> > >
> > > Ian Rogers (16):
> > > perf script: Fix metric_evlist leak in script_find_metrics
> > > perf stat: Fix evsel_list leak in cmd_stat
> > > perf tools: Fix sb_evlist leaks in top and record
> > > perf python: Fix memory leak in pyrf_evlist__get_pollfd
> > > perf synthetic-events: Fix uninitialized pthread_join
> > > perf data: Fix directory file descriptor leak in perf_data__switch
> > > perf test: Fix skiplist leak in cmd_test
> > > perf python: Check counts_values size in set_values
> > > perf python: Validate CPU and thread maps in pyrf_evsel__open
> > > perf python: Validate attribute setters in pyrf_evsel
> > > perf python: Zero initialize perf_data in pyrf_data__init
> > > perf python: Add thread and PMU uninitialized checks
> > > perf python: Fix MetricGroup return type in perf.pyi
> > > perf python: Fix count_values memory leak in pyrf_evsel__read
> > > perf python: Fix memory leak in pyrf__metrics_cb
> > > perf synthetic-events: Fix divide by zero in
> > > perf_event__synthesize_threads
> >
> > Will you send v3? You don't need to fix all pre-existing issues but we
> > should not introduce new bugs or regressions. I think some of Sashiko
> > comments found them like data switch and pmu init checks.
>
> Thanks Namhyung, I have a v3 coming. I'm working with Sashiko locally
> and trying to address as many issues as possible, including minor
> pre-existing ones—it's easier than remembering to clean them up later.
Sounds good. Looking forward to v3.
Thanks,
Namhyung
^ permalink raw reply [flat|nested] 53+ messages in thread
* [PATCH v3 00/15] perf python: Fix python extension bugs from v19 review
2026-06-24 5:15 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Ian Rogers
` (17 preceding siblings ...)
2026-07-04 16:54 ` [PATCH v2 00/16] perf python: Fix python extension bugs from v19 review Namhyung Kim
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 01/15] perf script: Fix metric_evlist leak in script_find_metrics Ian Rogers
` (15 more replies)
18 siblings, 16 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Phase 1 of splitting the v19 review patches into smaller fixes.
These patches address issues identified in the python extension during
the review of the v19 script porting series, as well as fixing several
pre-existing bugs and leaks.
v3:
- Addressed review feedback from Namhyung Kim and Sashiko AI:
- Dropped "perf data: Fix directory file descriptor leak in perf_data__switch"
to avoid Use-After-Free during directory mode file rotation.
- Added list == NULL check in pyrf_counts_values_set_values to safely
handle attribute deletion (del obj.values).
- Preserved errno before calling intlist__delete(skiplist) in cmd_test
error path to prevent clobbering the return error code.
- Added CHECK_INITIALIZED to pyrf_pmu__repr to prevent NULL pointer
dereferences on uninitialized PMU objects.
- Fixed duplicate patch title from v2.
v2:
- Addressed sashiko review feedback:
- Fixed 64-bit casting for counts_values by using T_ULONGLONG and PyLong_FromUnsignedLongLong.
- Replaced strict Py_TYPE checks with PyObject_TypeCheck for thread/cpu maps.
- Fixed a missing CHECK_INITIALIZED for pyrf_thread__get_cpu, pyrf_pmu__name, pyrf_pmu__events.
- Included <string.h> where memset was used.
- Fixed line splitting formatting on PyObject_New in pyrf_evsel__read.
- Added fixes for memory leaks and issues discovered during review:
- Fix memory leak of key and value in pyrf__metrics_cb
- Fix count_values memory leak in pyrf_evsel__read
- Fix sb_evlist leaks in both top and record paths.
- Fix potential divide by zero in perf_event__synthesize_threads
v1:
- Decomposed the single large "Fix python extension bugs" patch into 13 individual fixes.
- Added Fixes: tags for issues that date back to earlier commits.
- Addressed checkpatch warnings (line lengths, Signed-off-by, etc).
Ian Rogers (15):
perf script: Fix metric_evlist leak in script_find_metrics
perf stat: Fix evsel_list leak in cmd_stat
perf tools: Fix sb_evlist leaks in top and record
perf python: Fix memory leak in pyrf_evlist__get_pollfd
perf synthetic-events: Fix uninitialized pthread_join
perf test: Fix skiplist leak in cmd_test
perf python: Check counts_values size in set_values
perf python: Validate CPU and thread maps in pyrf_evsel__open
perf python: Validate attribute setters in pyrf_evsel
perf python: Zero initialize perf_data in pyrf_data__init
perf python: Add thread and PMU uninitialized checks
perf python: Fix MetricGroup return type in perf.pyi
perf python: Fix count_values memory leak in pyrf_evsel__read
perf python: Fix memory leak in pyrf__metrics_cb
perf synthetic-events: Fix divide by zero in
perf_event__synthesize_threads
tools/perf/builtin-record.c | 2 +
tools/perf/builtin-script.c | 2 +-
tools/perf/builtin-stat.c | 9 +-
tools/perf/builtin-top.c | 2 +
tools/perf/python/perf.pyi | 4 +-
tools/perf/tests/builtin-test.c | 9 +-
tools/perf/util/python.c | 166 ++++++++++++++++++++++++-----
tools/perf/util/synthetic-events.c | 6 +-
8 files changed, 161 insertions(+), 39 deletions(-)
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 01/15] perf script: Fix metric_evlist leak in script_find_metrics
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 02/15] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
` (14 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in script_find_metrics() where metric_evlist is leaked
when returning early on error paths.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 3622990efaab ("perf script: Change metric format to use json metrics")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-script.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index f91d8b1fbd01..ad8ca08ceb5f 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -2276,8 +2276,8 @@ static int script_find_metrics(const struct pmu_metric *pm,
}
pr_debug("Found metric '%s' whose evsels match those of in the perf data\n",
pm->metric_name);
- evlist__put(metric_evlist);
out:
+ evlist__put(metric_evlist);
return 0;
}
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 02/15] perf stat: Fix evsel_list leak in cmd_stat
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:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 03/15] perf tools: Fix sb_evlist leaks in top and record Ian Rogers
` (13 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_stat() where evsel_list is leaked if an error
occurs while opening the output file.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 361c99a661a7 ("perf evsel: Introduce perf_evlist")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-stat.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index d68a2617a920..c50cf230f16e 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -2735,7 +2735,8 @@ int cmd_stat(int argc, const char **argv)
output = fopen(output_name, mode);
if (!output) {
perror("failed to create output file");
- return -1;
+ status = -1;
+ goto out;
}
if (!stat_config.json_output) {
clock_gettime(CLOCK_REALTIME, &tm);
@@ -2746,7 +2747,8 @@ int cmd_stat(int argc, const char **argv)
output = fdopen(output_fd, mode);
if (!output) {
perror("Failed opening logfd");
- return -errno;
+ status = -errno;
+ goto out;
}
}
@@ -2755,7 +2757,8 @@ int cmd_stat(int argc, const char **argv)
parse_options_usage(stat_usage, stat_options, "o", 1);
parse_options_usage(NULL, stat_options, "log-fd", 0);
parse_options_usage(NULL, stat_options, "interval-clear", 0);
- return -1;
+ status = -1;
+ goto out;
}
stat_config.output = output;
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 03/15] perf tools: Fix sb_evlist leaks in top and record
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:14 ` [PATCH v3 02/15] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 04/15] perf python: Fix memory leak in pyrf_evlist__get_pollfd Ian Rogers
` (12 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_top() where top.sb_evlist was not freed if
evlist__add_bpf_sb_event() fails. Note that evlist__start_sb_thread() and
evlist__stop_sb_thread() take ownership of the evlist and free it, so
we must only free it if we fail before starting the thread. Also set
top.sb_evlist to NULL to prevent use-after-free bugs.
Apply the same fix to builtin-record.c to avoid leaking rec->sb_evlist
and calling pthread_join on uninitialized threads in the error path.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: b38d85ef49cf ("perf bpf: Decouple creating the evlist from adding the SB event")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-record.c | 2 ++
tools/perf/builtin-top.c | 2 ++
2 files changed, 4 insertions(+)
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index f58d7e3c7879..a57987851cf0 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -2258,6 +2258,8 @@ static int record__setup_sb_evlist(struct record *rec)
if (evlist__add_bpf_sb_event(rec->sb_evlist, perf_session__env(rec->session))) {
pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
+ evlist__put(rec->sb_evlist);
+ rec->sb_evlist = NULL;
return -1;
}
}
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 5933c46ee137..570410599f1b 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1881,6 +1881,8 @@ int cmd_top(int argc, const char **argv)
if (evlist__add_bpf_sb_event(top.sb_evlist, &host_env)) {
pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
status = -EINVAL;
+ evlist__put(top.sb_evlist);
+ top.sb_evlist = NULL;
goto out_put_evlist;
}
}
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 04/15] perf python: Fix memory leak in pyrf_evlist__get_pollfd
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (2 preceding siblings ...)
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 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 05/15] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
` (11 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a Python list object leak in pyrf_evlist__get_pollfd() by adding
a missing Py_DECREF on the error exit path.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index af85c0771c44..c7ae1f85aec6 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();
}
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 05/15] perf synthetic-events: Fix uninitialized pthread_join
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (3 preceding siblings ...)
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:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 06/15] perf test: Fix skiplist leak in cmd_test Ian Rogers
` (10 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
In perf_event__synthesize_threads(), fix an uninitialized pthread_join()
call when thread creation fails by only joining the successfully
created threads.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 340b47f510bb ("perf top: Implement multithreading for perf_event__synthesize_threads")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/synthetic-events.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 75a32ae8ef62..3583a60cc487 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1221,8 +1221,8 @@ int perf_event__synthesize_threads(const struct perf_tool *tool,
}
err = 0;
out_join:
- for (i = 0; i < thread_nr; i++)
- pthread_join(synthesize_threads[i], NULL);
+ for (j = 0; j < i; j++)
+ pthread_join(synthesize_threads[j], NULL);
free(args);
free_threads:
free(synthesize_threads);
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 06/15] perf test: Fix skiplist leak in cmd_test
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (4 preceding siblings ...)
2026-08-09 7:14 ` [PATCH v3 05/15] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 07/15] perf python: Check counts_values size in set_values Ian Rogers
` (9 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Fix a memory leak in cmd_test() where skiplist was not freed on
exit paths.
Assisted-by: Antigravity:gemini-3.1-pro
Fixes: 2ae828786c65 ("perf test: Allow skipping tests")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/builtin-test.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 41dcfbf874f0..4d0784b16723 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -1802,9 +1802,14 @@ int cmd_test(int argc, const char **argv)
rlimit__bump_memlock();
suites = build_suites();
- if (!suites)
- return errno ? -errno : -ENOMEM;
+ if (!suites) {
+ int err = errno;
+
+ intlist__delete(skiplist);
+ return err ? -err : -ENOMEM;
+ }
ret = __cmd_test(suites, argc, argv, skiplist);
free(suites);
+ intlist__delete(skiplist);
return ret;
}
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 07/15] perf python: Check counts_values size in set_values
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (5 preceding siblings ...)
2026-08-09 7:14 ` [PATCH v3 06/15] perf test: Fix skiplist leak in cmd_test Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 08/15] perf python: Validate CPU and thread maps in pyrf_evsel__open Ian Rogers
` (8 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
The set_values function incorrectly assumed the list contained exactly
5 elements. Add a check to prevent out-of-bounds access.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 38 ++++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index c7ae1f85aec6..f64d662bd3ee 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1881,11 +1881,11 @@ static void pyrf_counts_values__delete(struct pyrf_counts_values *pcounts_values
0, help }
static PyMemberDef pyrf_counts_values_members[] = {
- counts_values_member_def(val, T_ULONG, "Value of event"),
- counts_values_member_def(ena, T_ULONG, "Time for which enabled"),
- counts_values_member_def(run, T_ULONG, "Time for which running"),
- counts_values_member_def(id, T_ULONG, "Unique ID for an event"),
- counts_values_member_def(lost, T_ULONG, "Num of lost samples"),
+ counts_values_member_def(val, T_ULONGLONG, "Value of event"),
+ counts_values_member_def(ena, T_ULONGLONG, "Time for which enabled"),
+ counts_values_member_def(run, T_ULONGLONG, "Time for which running"),
+ counts_values_member_def(id, T_ULONGLONG, "Unique ID for an event"),
+ counts_values_member_def(lost, T_ULONGLONG, "Num of lost samples"),
{ .name = NULL, },
};
@@ -1895,8 +1895,15 @@ static PyObject *pyrf_counts_values_get_values(struct pyrf_counts_values *self,
if (!vals)
return NULL;
- for (int i = 0; i < 5; i++)
- PyList_SetItem(vals, i, PyLong_FromLong(self->values.values[i]));
+ for (int i = 0; i < 5; i++) {
+ PyObject *val = PyLong_FromUnsignedLongLong(self->values.values[i]);
+
+ if (!val) {
+ Py_DECREF(vals);
+ return NULL;
+ }
+ PyList_SetItem(vals, i, val);
+ }
return vals;
}
@@ -1907,19 +1914,34 @@ static int pyrf_counts_values_set_values(struct pyrf_counts_values *self, PyObje
Py_ssize_t size;
PyObject *item = NULL;
+ if (list == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "Value assigned must be a list");
return -1;
}
size = PyList_Size(list);
+ if (size != 5) {
+ PyErr_SetString(PyExc_ValueError, "List must have exactly 5 entries");
+ return -1;
+ }
+
for (Py_ssize_t i = 0; i < size; i++) {
+ unsigned long long val;
+
item = PyList_GetItem(list, i);
if (!PyLong_Check(item)) {
PyErr_SetString(PyExc_TypeError, "List members should be numbers");
return -1;
}
- self->values.values[i] = PyLong_AsLong(item);
+ val = PyLong_AsUnsignedLongLong(item);
+ if (val == (unsigned long long)-1 && PyErr_Occurred())
+ return -1;
+ self->values.values[i] = val;
}
return 0;
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 08/15] perf python: Validate CPU and thread maps in pyrf_evsel__open
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (6 preceding siblings ...)
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 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 09/15] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
` (7 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Add explicit Py_TYPE checks to ensure the arguments passed are
actually of the correct pyrf_thread_map and pyrf_cpu_map types.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index f64d662bd3ee..a1334400874c 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2113,11 +2113,21 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
&pcpus, &pthreads, &group, &inherit))
return NULL;
- if (pthreads != NULL && pthreads != Py_None)
+ if (pthreads != NULL && pthreads != Py_None) {
+ if (!PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) {
+ PyErr_SetString(PyExc_TypeError, "threads must be a thread_map");
+ return NULL;
+ }
threads = ((struct pyrf_thread_map *)pthreads)->threads;
+ }
- if (pcpus != NULL && pcpus != Py_None)
+ if (pcpus != NULL && pcpus != Py_None) {
+ if (!PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) {
+ PyErr_SetString(PyExc_TypeError, "cpus must be a cpu_map");
+ return NULL;
+ }
cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
+ }
evsel->core.attr.inherit = inherit;
/*
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 09/15] perf python: Validate attribute setters in pyrf_evsel
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (7 preceding siblings ...)
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 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 10/15] perf python: Zero initialize perf_data in pyrf_data__init Ian Rogers
` (6 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
If val is NULL when setting an attribute, PyErr_SetString should be
called as deleting the attribute isn't supported. In addition, ensure
PyErr_Occurred is checked before setting the attribute to avoid setting
a garbage value.
Fixes: 877108e42b1b ("perf tools: Initial python binding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 89 ++++++++++++++++++++++++++++++++++------
1 file changed, 77 insertions(+), 12 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index a1334400874c..848912401c4f 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2301,6 +2301,11 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
is_true = PyObject_IsTrue(val);
if (is_true < 0)
return -1;
@@ -2312,11 +2317,21 @@ static int pyrf_evsel__set_tracking(PyObject *self, PyObject *val, void *closure
static int pyrf_evsel__set_attr_config(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.config = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.config = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __maybe_unused)
@@ -2331,11 +2346,21 @@ static PyObject *pyrf_evsel__get_attr_config(PyObject *self, void *closure __may
static int pyrf_evsel__set_attr_read_format(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.read_format = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.read_format = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure __maybe_unused)
@@ -2350,11 +2375,21 @@ static PyObject *pyrf_evsel__get_attr_read_format(PyObject *self, void *closure
static int pyrf_evsel__set_attr_sample_period(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.sample_period = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.sample_period = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closure __maybe_unused)
@@ -2369,11 +2404,21 @@ static PyObject *pyrf_evsel__get_attr_sample_period(PyObject *self, void *closur
static int pyrf_evsel__set_attr_sample_type(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.sample_type = PyLong_AsUnsignedLongLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLongLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.sample_type = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_sample_type(PyObject *self, void *closure __maybe_unused)
@@ -2397,11 +2442,21 @@ static PyObject *pyrf_evsel__get_attr_size(PyObject *self, void *closure __maybe
static int pyrf_evsel__set_attr_type(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.type = PyLong_AsUnsignedLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.type = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe_unused)
@@ -2416,11 +2471,21 @@ static PyObject *pyrf_evsel__get_attr_type(PyObject *self, void *closure __maybe
static int pyrf_evsel__set_attr_wakeup_events(PyObject *self, PyObject *val, void *closure __maybe_unused)
{
struct pyrf_evsel *pevsel = (void *)self;
+ unsigned long new_val;
CHECK_INITIALIZED_INT(pevsel->evsel, "evsel");
- pevsel->evsel->core.attr.wakeup_events = PyLong_AsUnsignedLong(val);
- return PyErr_Occurred() ? -1 : 0;
+ if (val == NULL) {
+ PyErr_SetString(PyExc_TypeError, "cannot delete attribute");
+ return -1;
+ }
+
+ new_val = PyLong_AsUnsignedLong(val);
+ if (PyErr_Occurred())
+ return -1;
+
+ pevsel->evsel->core.attr.wakeup_events = new_val;
+ return 0;
}
static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void *closure __maybe_unused)
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 10/15] perf python: Zero initialize perf_data in pyrf_data__init
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (8 preceding siblings ...)
2026-08-09 7:14 ` [PATCH v3 09/15] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks Ian Rogers
` (5 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Replace path clearing with memset so the entire struct is zeroed,
preventing uninitialized fields from causing errors later.
Fixes: 4cd0142f7dec ("perf python: Add wrapper for perf_data file abstraction")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 848912401c4f..c70b151428f7 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -3,6 +3,7 @@
#include <Python.h>
#include <inttypes.h>
+#include <string.h>
#include <linux/err.h>
#include <poll.h>
@@ -3627,7 +3628,7 @@ static int pyrf_data__init(struct pyrf_data *pdata, PyObject *args, PyObject *kw
if (pdata->data.open)
perf_data__close(&pdata->data);
free((char *)pdata->data.path);
- pdata->data.path = NULL;
+ memset(&pdata->data, 0, sizeof(pdata->data));
if (fd != -1) {
struct stat st;
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (9 preceding siblings ...)
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 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 12/15] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
` (4 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
Add CHECK_INITIALIZED checks to the thread attribute getters
(get_pid, get_tid, get_ppid) to prevent crashes if they are accessed
before being properly initialized.
Fixes: 3b96bf7af60d ("perf python: Add python session abstraction wrapping perf's session")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index c70b151428f7..fe5afce17333 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1678,6 +1678,7 @@ static PyObject *pyrf_pmu__name(PyObject *self)
{
struct pyrf_pmu *ppmu = (void *)self;
+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
return PyUnicode_FromString(ppmu->pmu->name);
}
@@ -1730,6 +1731,8 @@ static PyObject *pyrf_pmu__events(PyObject *self)
PyObject *py_list = PyList_New(0);
int ret;
+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
+
if (!py_list)
return NULL;
@@ -1750,6 +1753,7 @@ static PyObject *pyrf_pmu__repr(PyObject *self)
{
struct pyrf_pmu *ppmu = (void *)self;
+ CHECK_INITIALIZED(ppmu->pmu, "pmu");
return PyUnicode_FromFormat("pmu(%s)", ppmu->pmu->name);
}
@@ -3764,21 +3768,25 @@ static PyMethodDef pyrf_thread__methods[] = {
static PyObject *pyrf_thread__get_pid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__pid(pthread->thread));
}
static PyObject *pyrf_thread__get_tid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__tid(pthread->thread));
}
static PyObject *pyrf_thread__get_ppid(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__ppid(pthread->thread));
}
static PyObject *pyrf_thread__get_cpu(struct pyrf_thread *pthread, void *closure __maybe_unused)
{
+ CHECK_INITIALIZED(pthread->thread, "thread");
return PyLong_FromLong(thread__cpu(pthread->thread));
}
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 12/15] perf python: Fix MetricGroup return type in perf.pyi
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (10 preceding siblings ...)
2026-08-09 7:14 ` [PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 13/15] perf python: Fix count_values memory leak in pyrf_evsel__read Ian Rogers
` (3 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
The metrics() function can return a dictionary where the value is either
a string or a list of strings, so the type signature in the stub file
should be Union[str, List[str]].
Fixes: 430da3cd03b4 ("perf python: Add perf.pyi stubs file")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/python/perf.pyi | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/perf/python/perf.pyi b/tools/perf/python/perf.pyi
index f266c9baffd0..58b0b3ed819d 100644
--- a/tools/perf/python/perf.pyi
+++ b/tools/perf/python/perf.pyi
@@ -1,5 +1,5 @@
"""Type stubs for the perf Python module."""
-from typing import Callable, Dict, List, Optional, Any, Iterator
+from typing import Callable, Dict, List, Optional, Any, Iterator, Union
def config_get(name: str) -> Optional[str]:
"""Get a configuration value from perf config.
@@ -12,7 +12,7 @@ def config_get(name: str) -> Optional[str]:
"""
...
-def metrics() -> List[Dict[str, str]]:
+def metrics() -> List[Dict[str, Union[str, List[str]]]]:
"""Get a list of available metrics.
Returns:
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 13/15] perf python: Fix count_values memory leak in pyrf_evsel__read
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (11 preceding siblings ...)
2026-08-09 7:14 ` [PATCH v3 12/15] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
@ 2026-08-09 7:14 ` Ian Rogers
2026-08-09 7:14 ` [PATCH v3 14/15] perf python: Fix memory leak in pyrf__metrics_cb Ian Rogers
` (2 subsequent siblings)
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
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.
Fixes: 739621f65702 ("perf python: Add evsel read method")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index fe5afce17333..71431c9c2e76 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -2210,11 +2210,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;
@@ -2233,6 +2228,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);
new_count = perf_counts(evsel->counts, cpu_idx, thread_idx);
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 14/15] perf python: Fix memory leak in pyrf__metrics_cb
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (12 preceding siblings ...)
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 ` 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
2026-08-12 0:52 ` [PATCH v3 00/15] perf python: Fix python extension bugs from v19 review Namhyung Kim
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
In pyrf__metrics_cb, PyDict_SetItem does not steal the reference of the
key and value, so they need to be decref'ed after successful insertion
to avoid memory leaks.
Fixes: 47b3e95728eb ("perf python: Add metrics function")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 71431c9c2e76..077e0f5d9fcd 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -3574,6 +3574,8 @@ static int pyrf__metrics_cb(const struct pmu_metric *pm,
Py_XDECREF(dict);
return -ENOMEM;
}
+ Py_DECREF(key);
+ Py_DECREF(value);
if (!add_to_dict(dict, "MetricName", pm->metric_name) ||
!add_to_dict(dict, "PMU", pm->pmu) ||
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* [PATCH v3 15/15] perf synthetic-events: Fix divide by zero in perf_event__synthesize_threads
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (13 preceding siblings ...)
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 ` Ian Rogers
2026-08-12 0:52 ` [PATCH v3 00/15] perf python: Fix python extension bugs from v19 review Namhyung Kim
15 siblings, 0 replies; 53+ messages in thread
From: Ian Rogers @ 2026-08-09 7:14 UTC (permalink / raw)
To: irogers, acme, namhyung
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
If scandir() finds no matching tasks in /proc, n is 0. If thread_nr is > 1,
we bypass the single-thread fast path and then clamp thread_nr to n, making
it 0. This results in a divide by zero when calculating num_per_thread.
Handle n <= 1 early to use the single-thread fast path and prevent the
crash.
Fixes: 340b47f510bb ("perf top: Implement multithreading for perf_event__synthesize_threads")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/synthetic-events.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 3583a60cc487..0c150193cca8 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -1175,7 +1175,7 @@ int perf_event__synthesize_threads(const struct perf_tool *tool,
else
thread_nr = nr_threads_synthesize;
- if (thread_nr <= 1) {
+ if (thread_nr <= 1 || n <= 1) {
err = __perf_event__synthesize_threads(tool, process,
machine,
needs_mmap, mmap_data,
--
2.55.0.679.g6767b8d81c-goog
^ permalink raw reply [flat|nested] 53+ messages in thread* Re: [PATCH v3 00/15] perf python: Fix python extension bugs from v19 review
2026-08-09 7:14 ` [PATCH v3 00/15] " Ian Rogers
` (14 preceding siblings ...)
2026-08-09 7:14 ` [PATCH v3 15/15] perf synthetic-events: Fix divide by zero in perf_event__synthesize_threads Ian Rogers
@ 2026-08-12 0:52 ` Namhyung Kim
15 siblings, 0 replies; 53+ messages in thread
From: Namhyung Kim @ 2026-08-12 0:52 UTC (permalink / raw)
To: acme, Ian Rogers
Cc: adrian.hunter, alice.mei.rogers, dapeng1.mi, james.clark,
leo.yan, linux-kernel, linux-perf-users, mingo, peterz, tmricht
On Sun, 09 Aug 2026 00:14:39 -0700, Ian Rogers wrote:
> Phase 1 of splitting the v19 review patches into smaller fixes.
> These patches address issues identified in the python extension during
> the review of the v19 script porting series, as well as fixing several
> pre-existing bugs and leaks.
>
> v3:
> - Addressed review feedback from Namhyung Kim and Sashiko AI:
> - Dropped "perf data: Fix directory file descriptor leak in perf_data__switch"
> to avoid Use-After-Free during directory mode file rotation.
> - Added list == NULL check in pyrf_counts_values_set_values to safely
> handle attribute deletion (del obj.values).
> - Preserved errno before calling intlist__delete(skiplist) in cmd_test
> error path to prevent clobbering the return error code.
> - Added CHECK_INITIALIZED to pyrf_pmu__repr to prevent NULL pointer
> dereferences on uninitialized PMU objects.
> - Fixed duplicate patch title from v2.
>
> [...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung
^ permalink raw reply [flat|nested] 53+ messages in thread