linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/4] perf test: Directory file descriptor leak
@ 2025-06-17 22:33 Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 2/4] perf evsel: Missed close when probing hybrid core PMUs Ian Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ian Rogers @ 2025-06-17 22:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, Michael Petlan,
	Jiapeng Chong, Andi Kleen, Tiezhu Yang, linux-perf-users,
	linux-kernel

Add missed close when iterating over the script directories.

Fixes: f3295f5b067d ("perf tests: Use scandirat for shell script finding")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/tests-scripts.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/tests/tests-scripts.c b/tools/perf/tests/tests-scripts.c
index 1d5759d08141..3a2a8438f9af 100644
--- a/tools/perf/tests/tests-scripts.c
+++ b/tools/perf/tests/tests-scripts.c
@@ -260,6 +260,7 @@ static void append_scripts_in_dir(int dir_fd,
 			continue; /* Skip scripts that have a separate driver. */
 		fd = openat(dir_fd, ent->d_name, O_PATH);
 		append_scripts_in_dir(fd, result, result_sz);
+		close(fd);
 	}
 	for (i = 0; i < n_dirs; i++) /* Clean up */
 		zfree(&entlist[i]);
-- 
2.50.0.rc2.761.g2dc52ea45b-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/4] perf evsel: Missed close when probing hybrid core PMUs
  2025-06-17 22:33 [PATCH v2 1/4] perf test: Directory file descriptor leak Ian Rogers
@ 2025-06-17 22:33 ` Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 3/4] perf trace: Add missed freeing of ordered events and thread Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 4/4] libperf evsel: In exit add missed puts and assert close, etc. were called Ian Rogers
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2025-06-17 22:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, Michael Petlan,
	Jiapeng Chong, Andi Kleen, Tiezhu Yang, linux-perf-users,
	linux-kernel

Add missing close to avoid leaking perf events. In past perfs this
mattered little as the function was just used by perf list. As the
function is now used to detect hybrid PMUs leaking the perf event is
somewhat more painful.

Fixes: b41f1cec91c3 ("perf list: Skip unsupported events")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/print-events.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c
index a786cbfb0ff5..83aaf7cda635 100644
--- a/tools/perf/util/print-events.c
+++ b/tools/perf/util/print-events.c
@@ -268,6 +268,7 @@ bool is_event_supported(u8 type, u64 config)
 			ret = evsel__open(evsel, NULL, tmap) >= 0;
 		}
 
+		evsel__close(evsel);
 		evsel__delete(evsel);
 	}
 
-- 
2.50.0.rc2.761.g2dc52ea45b-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 3/4] perf trace: Add missed freeing of ordered events and thread
  2025-06-17 22:33 [PATCH v2 1/4] perf test: Directory file descriptor leak Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 2/4] perf evsel: Missed close when probing hybrid core PMUs Ian Rogers
@ 2025-06-17 22:33 ` Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 4/4] libperf evsel: In exit add missed puts and assert close, etc. were called Ian Rogers
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2025-06-17 22:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, Michael Petlan,
	Jiapeng Chong, Andi Kleen, Tiezhu Yang, linux-perf-users,
	linux-kernel

Caught by leak sanitizer running "perf trace BTF general tests". Make
the ordered_events initialization unconditional and early so that
trace__exit cleanup is simple - ordered_events__init doesn't allocate
and just sets up 4 values and inits 3 list heads.

Signed-off-by: Ian Rogers <irogers@google.com>
---
v2: Move the init code to avoid segv found by Arnaldo.
---
 tools/perf/builtin-trace.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index bf9b5d0630d3..d50e86fccf60 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -5359,6 +5359,7 @@ static int trace__config(const char *var, const char *value, void *arg)
 
 static void trace__exit(struct trace *trace)
 {
+	thread__zput(trace->current);
 	strlist__delete(trace->ev_qualifier);
 	zfree(&trace->ev_qualifier_ids.entries);
 	if (trace->syscalls.table) {
@@ -5369,6 +5370,7 @@ static void trace__exit(struct trace *trace)
 	zfree(&trace->perfconfig_events);
 	evlist__delete(trace->evlist);
 	trace->evlist = NULL;
+	ordered_events__free(&trace->oe.data);
 #ifdef HAVE_LIBBPF_SUPPORT
 	btf__free(trace->btf);
 	trace->btf = NULL;
@@ -5518,6 +5520,9 @@ int cmd_trace(int argc, const char **argv)
 	sigchld_act.sa_sigaction = sighandler_chld;
 	sigaction(SIGCHLD, &sigchld_act, NULL);
 
+	ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
+	ordered_events__set_copy_on_queue(&trace.oe.data, true);
+
 	trace.evlist = evlist__new();
 
 	if (trace.evlist == NULL) {
@@ -5676,11 +5681,6 @@ int cmd_trace(int argc, const char **argv)
 			trace__load_vmlinux_btf(&trace);
 	}
 
-	if (trace.sort_events) {
-		ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
-		ordered_events__set_copy_on_queue(&trace.oe.data, true);
-	}
-
 	/*
 	 * If we are augmenting syscalls, then combine what we put in the
 	 * __augmented_syscalls__ BPF map with what is in the
-- 
2.50.0.rc2.761.g2dc52ea45b-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 4/4] libperf evsel: In exit add missed puts and assert close, etc. were called
  2025-06-17 22:33 [PATCH v2 1/4] perf test: Directory file descriptor leak Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 2/4] perf evsel: Missed close when probing hybrid core PMUs Ian Rogers
  2025-06-17 22:33 ` [PATCH v2 3/4] perf trace: Add missed freeing of ordered events and thread Ian Rogers
@ 2025-06-17 22:33 ` Ian Rogers
  2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2025-06-17 22:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, Michael Petlan,
	Jiapeng Chong, Andi Kleen, Tiezhu Yang, linux-perf-users,
	linux-kernel

A missed evsel__close before evsel__delete was the source of leaking
perf events due to a hybrid test. Add asserts in debug builds so that
this shouldn't happen in the future. Add puts missing on the cpu map
and thread maps.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/perf/evsel.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c
index c475319e2e41..2a85e0bfee1e 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -42,6 +42,12 @@ struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
 
 void perf_evsel__delete(struct perf_evsel *evsel)
 {
+	assert(evsel->fd == NULL);  /* If not fds were not closed. */
+	assert(evsel->mmap == NULL); /* If not munmap wasn't called. */
+	assert(evsel->sample_id == NULL); /* If not free_id wasn't called. */
+	perf_cpu_map__put(evsel->cpus);
+	perf_cpu_map__put(evsel->own_cpus);
+	perf_thread_map__put(evsel->threads);
 	free(evsel);
 }
 
-- 
2.50.0.rc2.761.g2dc52ea45b-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-17 22:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-17 22:33 [PATCH v2 1/4] perf test: Directory file descriptor leak Ian Rogers
2025-06-17 22:33 ` [PATCH v2 2/4] perf evsel: Missed close when probing hybrid core PMUs Ian Rogers
2025-06-17 22:33 ` [PATCH v2 3/4] perf trace: Add missed freeing of ordered events and thread Ian Rogers
2025-06-17 22:33 ` [PATCH v2 4/4] libperf evsel: In exit add missed puts and assert close, etc. were called Ian Rogers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).