Fix perf stat repeat segfault
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
@@ -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
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,
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/perfthen running:
tools/perf/perfstat -e '{cycles,instructions}' -r 10 lsSince
run_idxkeeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (whenrun_idx< 1) fixes this issue.Signed-off-by Numfor Mbiziwo-Tiapo <[email protected]>--- tools/perf/builtin-stat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/perf/builtin-stat.c +0
-0
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
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/perfthen running:
tools/perf/perfstat -e '{cycles,instructions}' -r 10 lsSince
run_idxkeeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (whenrun_idx< 1) fixes this issue.Signed-off-by Numfor Mbiziwo-Tiapo <[email protected]>--- tools/perf/builtin-stat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/perf/builtin-stat.c +0
-0tools/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
@@ -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,
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/perfthen running:
tools/perf/perfstat -e '{cycles,instructions}' -r 10 lsSince
run_idxkeeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (whenrun_idx< 1) fixes this issue.Signed-off-by Numfor Mbiziwo-Tiapo <[email protected]>--- tools/perf/builtin-stat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/perf/builtin-stat.c +0
-0tools/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@@ -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,
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/perfthen running:
tools/perf/perfstat -e '{cycles,instructions}' -r 10 lsSince
run_idxkeeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (whenrun_idx< 1) fixes this issue.Signed-off-by Numfor Mbiziwo-Tiapo <[email protected]>--- tools/perf/builtin-stat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/perf/builtin-stat.c +0
-0tools/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 iterationsnot 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@@ -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,
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/perfthen running:
tools/perf/perfstat -e '{cycles,instructions}' -r 10 lsSince
run_idxkeeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (whenrun_idx< 1) fixes this issue.Signed-off-by Numfor Mbiziwo-Tiapo <[email protected]>--- tools/perf/builtin-stat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/perf/builtin-stat.c +0
-0tools/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 iterationsnot 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@@ -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,
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/perfthen running:
tools/perf/perfstat -e '{cycles,instructions}' -r 10 lsSince
run_idxkeeps track of the current iteration of the repeat, only storing the cpu ids on the first iteration (whenrun_idx< 1) fixes this issue.Signed-off-by Numfor Mbiziwo-Tiapo <[email protected]>--- tools/perf/builtin-stat.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/perf/builtin-stat.c +0
-0tools/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 isSTAT_RECORDmode.
it's for faster reading of group events, see:
82bf311e15d2 perf stat: Use group read for event groups
jirka
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 lsIt'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
@@ -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
@@ -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
Arnaldo Carvalho de Melo
AppliedRe: [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 lsIt'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__closefunction.We also need to keep the
evsel_listalive for stat record (which is disabled in repeat mode).
Thanks, applied.
- Arnaldo
tip-bot for Jiri Olsa
merge bot[tip:perf/urgent] perf stat: Fix segfault for event group in repeat mode
In reply to Jiri Olsaperf 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 lsIt'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
@@ -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
@@ -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,