Skip to contentSkip to conversation

Fix perf stat repeat segfault

PATCH In review Numfor Mbiziwo-Tiapo 6 participants 11 messages Jul 10, 2019 – Jul 23, 2019 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix perf stat repeat segfault

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +4 -3
index 63a3afc7f32b..92d6694367e4 100644
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
 	workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+		&& run_idx < 1;
 }
 
 static bool is_target_alive(struct target *_target,
@@ -503,7 +504,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		if (l > stat_config.unit_width)
 			stat_config.unit_width = l;
 
-		if (perf_evsel__should_store_id(counter) &&
+		if (perf_evsel__should_store_id(counter, run_idx) &&
 		    perf_evsel__store_ids(counter, evsel_list))
 			return -1;
 	}
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#1 lore raw

Hi Numfor,

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 385

Quoted 7 lines · Numfor Mbiziwo-Tiapo
tools/perf/builtin-stat.c +3 -2
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+		&& run_idx < 1;
 }

Build fails for me:

builtin-stat.c: In function ‘perf_evsel__should_store_id’:
builtin-stat.c:395:3: error: suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]
return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID

                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

&& run_idx < 1;
^~~~~~~~~~~~~~
cc1: all warnings being treated as errors

And probably,

Fixes 82bf311e15d2 ("perf stat: Use group read for event groups")
#2 lore raw
In reply to Numfor Mbiziwo-Tiapo
Quoted 27 lines · Numfor Mbiziwo-Tiapo

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 63a3afc7f32b..92d6694367e4 100644

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 384

Quoted 9 lines
tools/perf/builtin-stat.c +3 -2
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
 	workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+		&& run_idx < 1;

we create counters for every iteration, so this can't be based on iteration

I think that's just a workaround for memory corruption, that's happening for repeating groupped events stats, I'll check on this

jirka

Quoted 15 lines
tools/perf/builtin-stat.c +1 -1
 }
 
 static bool is_target_alive(struct target *_target,
@@ -503,7 +504,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		if (l > stat_config.unit_width)
 			stat_config.unit_width = l;
 
-		if (perf_evsel__should_store_id(counter) &&
+		if (perf_evsel__should_store_id(counter, run_idx) &&
 		    perf_evsel__store_ids(counter, evsel_list))
 			return -1;
 	}
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
#3 lore raw
Quoted 41 lines · Jiri Olsa
Quoted 27 lines · Numfor Mbiziwo-Tiapo

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 63a3afc7f32b..92d6694367e4 100644

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 384

Quoted 9 lines
tools/perf/builtin-stat.c +3 -2
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
 	workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+	return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+		&& run_idx < 1;

we create counters for every iteration, so this can't be based on iteration

Quoted 3 lines

I think that's just a workaround for memory corruption, that's happening for repeating groupped events stats, I'll check on this

how about something like this? we did not cleanup
ids on evlist close, so it kept on raising and
causing corruption in next iterations

jirka

---

tools/perf/util/evsel.c +2 -0
index ebb46da4dfe5..52459dd5ad0c 100644
@@ -1291,6 +1291,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel)
 	xyarray__delete(evsel->sample_id);
 	evsel->sample_id = NULL;
 	zfree(&evsel->id);
+	evsel->ids = 0;
 }
 
 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
@@ -2077,6 +2078,7 @@ void perf_evsel__close(struct perf_evsel *evsel)
 
 	perf_evsel__close_fd(evsel);
 	perf_evsel__free_fd(evsel);
+	perf_evsel__free_id(evsel);
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
#4 lore raw
Quoted 43 lines · Jiri Olsa
Quoted 41 lines · Jiri Olsa
Quoted 27 lines · Numfor Mbiziwo-Tiapo

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 63a3afc7f32b..92d6694367e4 100644

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 384

Quoted 9 lines
tools/perf/builtin-stat.c +3 -2
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
    workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+           && run_idx < 1;

we create counters for every iteration, so this can't be based on iteration

Quoted 8 lines
Quoted 3 lines

I think that's just a workaround for memory corruption, that's happening for repeating groupped events stats, I'll check on this

how about something like this? we did not cleanup
ids on evlist close, so it kept on raising and
causing corruption in next iterations

not sure, that would realloc on each iteration of the repeats.

Quoted 25 lines

jirka

---

tools/perf/util/evsel.c +2 -0
index ebb46da4dfe5..52459dd5ad0c 100644
@@ -1291,6 +1291,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel)
        xyarray__delete(evsel->sample_id);
        evsel->sample_id = NULL;
        zfree(&evsel->id);
+       evsel->ids = 0;
 }
 
 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
@@ -2077,6 +2078,7 @@ void perf_evsel__close(struct perf_evsel *evsel)
 
        perf_evsel__close_fd(evsel);
        perf_evsel__free_fd(evsel);
+       perf_evsel__free_id(evsel);
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
#5 lore raw
Quoted 48 lines · Stephane Eranian
Quoted 47 lines · Jiri Olsa
Quoted 44 lines · Jiri Olsa
Quoted 27 lines · Numfor Mbiziwo-Tiapo

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 63a3afc7f32b..92d6694367e4 100644

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 384

Quoted 9 lines
tools/perf/builtin-stat.c +3 -2
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
    workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+           && run_idx < 1;

we create counters for every iteration, so this can't be based on iteration

I think that's just a workaround for memory corruption, that's happening for repeating groupped events stats, I'll check on this

Quoted 5 lines
Quoted 4 lines

how about something like this? we did not cleanup
ids on evlist close, so it kept on raising and
causing corruption in next iterations

not sure, that would realloc on each iteration of the repeats.

well, we need new ids, because we create new events every iteration

jirka

Quoted 26 lines
Quoted 26 lines

jirka

---

tools/perf/util/evsel.c +2 -0
index ebb46da4dfe5..52459dd5ad0c 100644
@@ -1291,6 +1291,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel)
        xyarray__delete(evsel->sample_id);
        evsel->sample_id = NULL;
        zfree(&evsel->id);
+       evsel->ids = 0;
 }
 
 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
@@ -2077,6 +2078,7 @@ void perf_evsel__close(struct perf_evsel *evsel)
 
        perf_evsel__close_fd(evsel);
        perf_evsel__free_fd(evsel);
+       perf_evsel__free_id(evsel);
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
#6 lore raw
Quoted 50 lines · Jiri Olsa
Quoted 48 lines · Stephane Eranian
Quoted 47 lines · Jiri Olsa
Quoted 44 lines · Jiri Olsa
Quoted 27 lines · Numfor Mbiziwo-Tiapo

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 63a3afc7f32b..92d6694367e4 100644

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 384

Quoted 9 lines
tools/perf/builtin-stat.c +3 -2
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
    workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+           && run_idx < 1;

we create counters for every iteration, so this can't be based on iteration

I think that's just a workaround for memory corruption, that's happening for repeating groupped events stats, I'll check on this

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

how about something like this? we did not cleanup
ids on evlist close, so it kept on raising and
causing corruption in next iterations

not sure, that would realloc on each iteration of the repeats.

well, we need new ids, because we create new events every iteration

If you recreate them, then agreed.
It is not clear to me why you need ids when not running is STAT_RECORD mode.

Quoted 28 lines

jirka

Quoted 26 lines
Quoted 26 lines

jirka

---

tools/perf/util/evsel.c +2 -0
index ebb46da4dfe5..52459dd5ad0c 100644
@@ -1291,6 +1291,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel)
        xyarray__delete(evsel->sample_id);
        evsel->sample_id = NULL;
        zfree(&evsel->id);
+       evsel->ids = 0;
 }
 
 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
@@ -2077,6 +2078,7 @@ void perf_evsel__close(struct perf_evsel *evsel)
 
        perf_evsel__close_fd(evsel);
        perf_evsel__free_fd(evsel);
+       perf_evsel__free_id(evsel);
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
#7 lore raw
Quoted 55 lines · Stephane Eranian
Quoted 54 lines · Jiri Olsa
Quoted 52 lines · Stephane Eranian
Quoted 51 lines · Jiri Olsa
Quoted 41 lines · Jiri Olsa
Quoted 27 lines · Numfor Mbiziwo-Tiapo

When perf stat is called with event groups and the repeat option, a segfault occurs because the cpu ids are stored on each iteration of the repeat, when they should only be stored on the first iteration, which causes a buffer overflow.

This can be replicated by running (from the tip directory):

make -C tools/perf

then running:

tools/perf/perf stat -e '{cycles,instructions}' -r 10 ls

Since run_idx keeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (when run_idx < 1) fixes this issue.

---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 63a3afc7f32b..92d6694367e4 100644

tools/perf/builtin-stat.c static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf · line 384

Quoted 9 lines
tools/perf/builtin-stat.c +3 -2
@@ -378,9 +378,10 @@ static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *inf
    workload_exec_errno = info->si_value.sival_int;
 }
 
-static bool perf_evsel__should_store_id(struct perf_evsel *counter)
+static bool perf_evsel__should_store_id(struct perf_evsel *counter, int run_idx)
 {
-   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
+   return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID
+           && run_idx < 1;

we create counters for every iteration, so this can't be based on iteration

Quoted 3 lines

I think that's just a workaround for memory corruption, that's happening for repeating groupped events stats, I'll check on this

how about something like this? we did not cleanup
ids on evlist close, so it kept on raising and
causing corruption in next iterations

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

not sure, that would realloc on each iteration of the repeats.

well, we need new ids, because we create new events every iteration

If you recreate them, then agreed.
It is not clear to me why you need ids when not running is STAT_RECORD mode.

it's for faster reading of group events, see:
82bf311e15d2 perf stat: Use group read for event groups

jirka

#8 lore raw

PATCH 1/1 perf stat: Fix segfault for event group in repeat mode

Numfor Mbiziwo-Tiapo reported segfault on stat of event group in repeat mode:

  # perf stat -e '{cycles,instructions}' -r 10 ls

It's caused by memory corruption due to not cleaned evsel's id array and index, which needs to be rebuilt in every stat iteration. Currently the ids index grows, while the array (which is also not freed) has the same size.

Fixing this by releasing id array and zeroing ids index in perf_evsel__close function.

We also need to keep the evsel_list alive for stat record (which is disabled in repeat mode).

---
 tools/perf/builtin-stat.c | 9 ++++++++-
 tools/perf/util/evsel.c   | 2 ++
 2 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/builtin-stat.c +8 -1
index b55a534b4de0..352cf39d7c2f 100644
@@ -607,7 +607,13 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	 * group leaders.
 	 */
 	read_counters(&(struct timespec) { .tv_nsec = t1-t0 });
-	perf_evlist__close(evsel_list);
+
+	/*
+	 * We need to keep evsel_list alive, because it's processed
+	 * later the evsel_list will be closed after.
+	 */
+	if (!STAT_RECORD)
+		perf_evlist__close(evsel_list);
 
 	return WEXITSTATUS(status);
 }
@@ -1997,6 +2003,7 @@ int cmd_stat(int argc, const char **argv)
 			perf_session__write_header(perf_stat.session, evsel_list, fd, true);
 		}
 
+		perf_evlist__close(evsel_list);
 		perf_session__delete(perf_stat.session);
 	}
 
tools/perf/util/evsel.c +2 -0
index ebb46da4dfe5..52459dd5ad0c 100644
@@ -1291,6 +1291,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel)
 	xyarray__delete(evsel->sample_id);
 	evsel->sample_id = NULL;
 	zfree(&evsel->id);
+	evsel->ids = 0;
 }
 
 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
@@ -2077,6 +2078,7 @@ void perf_evsel__close(struct perf_evsel *evsel)
 
 	perf_evsel__close_fd(evsel);
 	perf_evsel__free_fd(evsel);
+	perf_evsel__free_id(evsel);
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Signature · 1 line
2.21.0
#9 lore raw

Re: [PATCH] perf stat: Fix segfault for event group in repeat mode

Em Mon, Jul 15, 2019 at 04:21:21PM +0200, Jiri Olsa escreveu:

Quoted 11 lines

Numfor Mbiziwo-Tiapo reported segfault on stat of event group in repeat mode:

  # perf stat -e '{cycles,instructions}' -r 10 ls

It's caused by memory corruption due to not cleaned evsel's id array and index, which needs to be rebuilt in every stat iteration. Currently the ids index grows, while the array (which is also not freed) has the same size.

Quoted 5 lines

Fixing this by releasing id array and zeroing ids index in perf_evsel__close function.

We also need to keep the evsel_list alive for stat record (which is disabled in repeat mode).

Thanks, applied.

- Arnaldo

#10 lore raw

[tip:perf/urgent] perf stat: Fix segfault for event group in repeat mode

In reply to Jiri Olsa
perf stat: Fix segfault for event group in repeat mode
Commit-ID:  08ef3af1579d0446db1c1bd08e2c42565addf10f
Gitweb:     https://git.kernel.org/tip/08ef3af1579d0446db1c1bd08e2c42565addf10f
Author:     Jiri Olsa <[email protected]>
AuthorDate: Mon, 15 Jul 2019 16:21:21 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 23 Jul 2019 09:00:05 -0300

perf stat: Fix segfault for event group in repeat mode

Numfor Mbiziwo-Tiapo reported segfault on stat of event group in repeat mode:

  # perf stat -e '{cycles,instructions}' -r 10 ls

It's caused by memory corruption due to not cleaned evsel's id array and index, which needs to be rebuilt in every stat iteration. Currently the ids index grows, while the array (which is also not freed) has the same size.

Fixing this by releasing id array and zeroing ids index in perf_evsel__close function.

We also need to keep the evsel_list alive for stat record (which is disabled in repeat mode).

---
 tools/perf/builtin-stat.c | 9 ++++++++-
 tools/perf/util/evsel.c   | 2 ++
 2 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/builtin-stat.c +8 -1
index b55a534b4de0..352cf39d7c2f 100644
@@ -607,7 +607,13 @@ try_again:
 	 * group leaders.
 	 */
 	read_counters(&(struct timespec) { .tv_nsec = t1-t0 });
-	perf_evlist__close(evsel_list);
+
+	/*
+	 * We need to keep evsel_list alive, because it's processed
+	 * later the evsel_list will be closed after.
+	 */
+	if (!STAT_RECORD)
+		perf_evlist__close(evsel_list);
 
 	return WEXITSTATUS(status);
 }
@@ -1997,6 +2003,7 @@ int cmd_stat(int argc, const char **argv)
 			perf_session__write_header(perf_stat.session, evsel_list, fd, true);
 		}
 
+		perf_evlist__close(evsel_list);
 		perf_session__delete(perf_stat.session);
 	}
 
tools/perf/util/evsel.c +2 -0
index ebb46da4dfe5..52459dd5ad0c 100644
@@ -1291,6 +1291,7 @@ static void perf_evsel__free_id(struct perf_evsel *evsel)
 	xyarray__delete(evsel->sample_id);
 	evsel->sample_id = NULL;
 	zfree(&evsel->id);
+	evsel->ids = 0;
 }
 
 static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
@@ -2077,6 +2078,7 @@ void perf_evsel__close(struct perf_evsel *evsel)
 
 	perf_evsel__close_fd(evsel);
 	perf_evsel__free_fd(evsel);
+	perf_evsel__free_id(evsel);
 }
 
 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,