All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix perf stat repeat segfault
@ 2019-07-10 20:45 Numfor Mbiziwo-Tiapo
  2019-07-11  4:44 ` Ravi Bangoria
  2019-07-14 20:44 ` Jiri Olsa
  0 siblings, 2 replies; 12+ messages in thread
From: Numfor Mbiziwo-Tiapo @ 2019-07-10 20:45 UTC (permalink / raw)
  To: peterz, mingo, acme, alexander.shishkin, jolsa, namhyung,
	songliubraving, mbd
  Cc: linux-kernel, irogers, eranian, 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.

Signed-off-by: Numfor Mbiziwo-Tiapo <nums@google.com>
---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 63a3afc7f32b..92d6694367e4 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -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;
 	}
-- 
2.22.0.410.gd8fdbe21b5-goog


^ permalink raw reply related	[flat|nested] 12+ messages in thread
* [PATCH] Fix perf stat repeat segfault
@ 2019-07-11 17:21 Numfor Mbiziwo-Tiapo
  0 siblings, 0 replies; 12+ messages in thread
From: Numfor Mbiziwo-Tiapo @ 2019-07-11 17:21 UTC (permalink / raw)
  To: peterz, mingo, acme, alexander.shishkin, jolsa, namhyung,
	songliubraving, mbd
  Cc: linux-kernel, irogers, eranian, 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.

Signed-off-by: Numfor Mbiziwo-Tiapo <nums@google.com>
---
 tools/perf/builtin-stat.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 63a3afc7f32b..00a13ce17fd9 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -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;
 	}
-- 
2.22.0.410.gd8fdbe21b5-goog


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

end of thread, other threads:[~2019-07-23 21:50 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-10 20:45 [PATCH] Fix perf stat repeat segfault Numfor Mbiziwo-Tiapo
2019-07-11  4:44 ` Ravi Bangoria
2019-07-14 20:44 ` Jiri Olsa
2019-07-14 20:55   ` Jiri Olsa
2019-07-14 21:36     ` Stephane Eranian
2019-07-15  7:59       ` Jiri Olsa
2019-07-15  8:14         ` Stephane Eranian
2019-07-15  8:31           ` Jiri Olsa
2019-07-15 14:21             ` [PATCH] perf stat: Fix segfault for event group in repeat mode Jiri Olsa
2019-07-16 18:48               ` Arnaldo Carvalho de Melo
2019-07-23 21:49               ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2019-07-11 17:21 [PATCH] Fix perf stat repeat segfault Numfor Mbiziwo-Tiapo

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.