From: Ian Rogers <irogers@google.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
Ian Rogers <irogers@google.com>
Subject: [PATCH v1 3/5] perf trace-event: Free the global trace_event when a command ends
Date: Thu, 17 Sep 2026 23:32:47 -0700 [thread overview]
Message-ID: <20260918063249.2172589-4-irogers@google.com> (raw)
In-Reply-To: <20260918063249.2172589-1-irogers@google.com>
The tep handle behind trace_event__tp_format() is a process wide cache
of the running kernel's tracepoint formats and nothing ever released
it, as the comment above it had noted since the code was added.
Add trace_event__exit() and call it from run_builtin() next to the
existing perf_config__exit(). By then the builtin has returned, so the
tep_event pointers it handed out, such as the ones cached in
evsel->tp_format, are no longer reachable.
Note that this does not silence every libtraceevent report under
leak sanitizer. Parsing a print fmt drops a token inside the library
itself, and as those allocations are not linked into the handle,
freeing the handle cannot reclaim them.
Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/perf.c | 6 ++++++
tools/perf/util/trace-event.c | 22 +++++++++++++++++-----
tools/perf/util/trace-event.h | 1 +
3 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 6c5baa285b13..d60bc929c257 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -32,6 +32,9 @@
#include "util/build-id.h"
#include "util/config.h"
#include "util/debug.h"
+#ifdef HAVE_LIBTRACEEVENT
+#include "util/trace-event.h"
+#endif
const char perf_usage_string[] =
"perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
@@ -379,6 +382,9 @@ static int run_builtin(const struct cmd_struct *p, int argc, const char **argv)
status = p->fn(argc, argv);
perf_config__exit();
+#ifdef HAVE_LIBTRACEEVENT
+ trace_event__exit();
+#endif
exit_browser(status);
if (status)
diff --git a/tools/perf/util/trace-event.c b/tools/perf/util/trace-event.c
index 826f75464171..819542a73f55 100644
--- a/tools/perf/util/trace-event.c
+++ b/tools/perf/util/trace-event.c
@@ -14,11 +14,9 @@
#include "machine.h"
/*
- * global trace_event object used by trace_event__tp_format
- *
- * TODO There's no cleanup call for this. Add some sort of
- * __exit function support and call trace_event__cleanup
- * there.
+ * Global trace_event object used by trace_event__tp_format. It caches the
+ * tracepoint formats of the running kernel for the lifetime of the command
+ * and is released by trace_event__exit.
*/
static struct trace_event tevent;
static bool tevent_initialized;
@@ -75,6 +73,20 @@ void trace_event__cleanup(struct trace_event *t)
t->plugin_list = NULL;
}
+/*
+ * Release the global trace_event. Called once the command is done, when the
+ * tep_event pointers handed out by trace_event__tp_format are no longer in
+ * use.
+ */
+void trace_event__exit(void)
+{
+ if (!tevent_initialized)
+ return;
+
+ trace_event__cleanup(&tevent);
+ tevent_initialized = false;
+}
+
/*
* Returns NULL and sets errno on failure.
*/
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index 720121c74f1d..1c342fce36bb 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -32,6 +32,7 @@ bool have_tracepoints(struct list_head *evlist);
int trace_event__init(struct trace_event *t);
void trace_event__cleanup(struct trace_event *t);
+void trace_event__exit(void);
int trace_event__register_resolver(struct machine *machine,
tep_func_resolver_t *func);
struct tep_event*
--
2.55.0.1082.g2b9226bbc0-goog
next prev parent reply other threads:[~2026-09-18 6:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 6:32 [PATCH v1 0/5] perf trace: Tracepoint format error handling and leak fixes Ian Rogers
2026-09-18 6:32 ` [PATCH v1 1/5] perf trace-event: Report tracepoint format errors with NULL and errno Ian Rogers
2026-09-18 6:41 ` sashiko-bot
2026-09-18 6:32 ` [PATCH v1 2/5] perf trace-event: Reuse an already parsed tracepoint format Ian Rogers
2026-09-18 6:38 ` sashiko-bot
2026-09-18 6:32 ` Ian Rogers [this message]
2026-09-18 6:41 ` [PATCH v1 3/5] perf trace-event: Free the global trace_event when a command ends sashiko-bot
2026-09-18 6:32 ` [PATCH v1 4/5] perf trace: Free the host machine allocation Ian Rogers
2026-09-18 6:45 ` sashiko-bot
2026-09-18 6:32 ` [PATCH v1 5/5] perf thread: Free the comm read from procfs Ian Rogers
2026-09-18 6:38 ` sashiko-bot
2026-09-25 13:00 ` [PATCH v1 0/5] perf trace: Tracepoint format error handling and leak fixes Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260918063249.2172589-4-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.