Re: [PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks
Ian Rogers <[email protected]>
| Newsgroups | org.kernel.vger.linux-perf-users,dev.linux.lists.sashiko-reviews |
|---|---|
| Message-ID | <CAP-5=fWbwZWppYLcuFhxJ18BtXJaS1+DDo-U2C1UD5zKF+828w@mail.gmail.com> |
On Sun, Aug 9, 2026 at 10:02 PM Namhyung Kim <[email protected]> wrote: > > Hello, > > On Sun, Aug 09, 2026 at 07:28:09AM +0000, [email protected] wrote: > > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: > > - [Medium] The newly added `CHECK_INITIALIZED(ppmu->pmu, "pmu")` in `pyrf_pmu__events()` causes a Python object memory leak of `py_list` on the error path. > > -- > > > > --- Patch [11]: [PATCH v3 11/15] perf python: Add thread and PMU uninitialized checks --- > > commit a381b82bb0eb00b93be525bfa1c265b574c03a5a > > Author: Ian Rogers <[email protected]> > > > > perf python: Add thread and PMU uninitialized checks > > > > This commit adds CHECK_INITIALIZED guards to thread and PMU getters to > > prevent crashes when accessed before proper initialization. > > > > > diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c > > > index c70b151428f76..fe5afce173330 100644 > > > --- a/tools/perf/util/python.c > > > +++ b/tools/perf/util/python.c > > > > [ ... ] > > > > > @@ -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; > > > > [Severity: Medium] > > Does this code leak py_list if CHECK_INITIALIZED fails and returns early? > > > > Since py_list is allocated with PyList_New(0) just before the initialization > > check, it appears this Python object is left orphaned if the macro evaluates > > to true and forces an early return. > > > > Could the CHECK_INITIALIZED macro be moved to the beginning of > > pyrf_pmu__events() before the PyList_New(0) allocation to avoid this leak? > > It seems this is the only issue introduced in this series. > As it's trivial, I'll apply the following change for Ian. Looks good to me. Thanks, Ian > Thanks, > Namhyung > > > diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c > index fe5afce1733306be..36c5f4b0d0bb15e7 100644 > --- a/tools/perf/util/python.c > +++ b/tools/perf/util/python.c > @@ -1728,11 +1728,12 @@ static int pyrf_pmu__events_cb(void *state, struct pmu_event_info *info) > static PyObject *pyrf_pmu__events(PyObject *self) > { > struct pyrf_pmu *ppmu = (void *)self; > - PyObject *py_list = PyList_New(0); > + PyObject *py_list; > int ret; > > CHECK_INITIALIZED(ppmu->pmu, "pmu"); > > + py_list = PyList_New(0); > if (!py_list) > return NULL; > >