Re: [PATCH 1/4] perf timechart: Release event samples at the end
From: Namhyung Kim
Date: Fri Jun 05 2026 - 19:34:23 EST
On Fri, Jun 05, 2026 at 08:21:49PM -0300, Arnaldo Carvalho de Melo wrote:
> On Fri, Jun 05, 2026 at 04:11:52PM -0700, Namhyung Kim wrote:
> > Add timechart__release() to free all data structures added during the
> > sample processing.
> >
> > Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
> > ---
> > tools/perf/builtin-timechart.c | 54 ++++++++++++++++++++++++++++++++++
> > 1 file changed, 54 insertions(+)
> >
> > diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
> > index 04dbb944a42720b4..7c4ace51302e260c 100644
> > --- a/tools/perf/builtin-timechart.c
> > +++ b/tools/perf/builtin-timechart.c
> > @@ -1548,6 +1548,59 @@ static void write_svg_file(struct timechart *tchart, const char *filename)
> > svg_close();
> > }
> >
> > +static void timechart__release(struct timechart *tchart)
> > +{
> > + struct per_pid *p = tchart->all_data;
> > + struct power_event *pwr = tchart->power_events;
> > + struct wake_event *we = tchart->wake_events;
> > +
> > + while (p) {
> > + struct per_pid *next_pid = p->next;
> > + struct per_pidcomm *c = p->all;
> > +
> > + while (c) {
> > + struct per_pidcomm *next_comm = c->next;
> > + struct cpu_sample *cs = c->samples;
> > + struct io_sample *ios = c->io_samples;
> > +
> > + while (cs) {
> > + struct cpu_sample *next = cs->next;
> > +
> > + free((char *)cs->backtrace);
>
> zfree()? This way if somebody has a
> pointer to cs and tries to use
> cs->backtrace after this free, it will
> segfault instead of using garbage or
> worse, setting some other unrelated
> struct to some cpu_sample field supposed
> value.
Ok, will do here and others below.
Thanks,
Namhyung
>
> > + free(cs);
> > + cs = next;
> > + }
> > +
> > + while (ios) {
> > + struct io_sample *next = ios->next;
> > +
> > + free(ios);
>
> io->next = NULL;
>
> > + ios = next;
> > + }
> > +
> > + free(c->comm);
>
> zfree
>
> > + free(c);
> > + c = next_comm;
> > + }
> > + p = next_pid;
> > + }
> > +
> > + while (pwr) {
> > + struct power_event *next = pwr->next;
> > +
> > + free(pwr);
> pwr->next = NULL;
> > + pwr = next;
> > + }
> > +
> > + while (we) {
> > + struct wake_event *next = we->next;
> > +
> > + free((char *)we->backtrace);
> zfree
> > + free(we);
> > + we = next;
> > + }
> > +}
> > +
> > static int process_header(struct perf_file_section *section __maybe_unused,
> > struct perf_header *ph,
> > int feat,
> > @@ -2079,6 +2132,7 @@ int cmd_timechart(int argc, const char **argv)
> >
> > ret = __cmd_timechart(&tchart, output_name);
> > out:
> > + timechart__release(&tchart);
> > zfree(&cpus_cstate_start_times);
> > zfree(&cpus_cstate_state);
> > zfree(&cpus_pstate_start_times);
> > --
> > 2.54.0.1032.g2f8565e1d1-goog