Skip to contentSkip to conversation

perf session: Fix infinite loop on invalid perf.data file

PATCH In review Namhyung Kim 6 participants 14 messages Sep 30, 2013 – Oct 8, 2013 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/1 perf session: Fix infinite loop on invalid perf.data file

From: Namhyung Kim <[email protected]>

perf-record updates the header in the perf.data file at termination. Without this update perf-report (and other processing built-ins) it caused an infinite loop when perf report (or something like) called.

This is because the algorithm in __perf_session__process_events() depends on the data_size which is read from file header. Use file size directly instead in this case to do the best-effort processing.

---
 tools/perf/util/header.c  | 9 +++++++++
 tools/perf/util/session.c | 2 +-
 2 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/util/header.c +9 -0
index 26441d0e571b..cedccde6a6b2 100644
@@ -2753,6 +2753,15 @@ int perf_session__read_header(struct perf_session *session)
 	if (perf_file_header__read(&f_header, header, fd) < 0)
 		return -EINVAL;
 
+	/*
+	 * Sanity check that perf.data was written cleanly; data size is
+	 * initialized to 0 and updated only if the on_exit function is run.
+	 * If data size is still 0 then the file contains only partial
+	 * information.  Just warn user and process it as much as it can.
+	 */
+	if (f_header.data.size == 0)
+		pr_warning("Data size is 0. Was the record command properly terminated?\n");
+
 	nr_attrs = f_header.attrs.size / f_header.attr_size;
 	lseek(fd, f_header.attrs.offset, SEEK_SET);
 
tools/perf/util/session.c +1 -1
index 6c1d4447c5b4..7bbd60c0a0ed 100644
@@ -1323,7 +1323,7 @@ int __perf_session__process_events(struct perf_session *session,
 	file_offset = page_offset;
 	head = data_offset - page_offset;
 
-	if (data_offset + data_size < file_size)
+	if (data_size && (data_offset + data_size < file_size))
 		file_size = data_offset + data_size;
 
 	progress_next = file_size / 16;
Signature · 1 line
1.7.11.7
#1 lore raw
Quoted 6 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

perf-record updates the header in the perf.data file at termination. Without this update perf-report (and other processing built-ins) it caused an infinite loop when perf report (or something like) called.

Quoted 8 lines

This is because the algorithm in __perf_session__process_events() depends on the data_size which is read from file header. Use file size directly instead in this case to do the best-effort processing.

worked ok for me. Sonny can you verify?

David

#2 lore raw
Quoted 12 lines · David Ahern
Quoted 11 lines · Namhyung Kim

From: Namhyung Kim <[email protected]>

perf-record updates the header in the perf.data file at termination. Without this update perf-report (and other processing built-ins) it caused an infinite loop when perf report (or something like) called.

This is because the algorithm in __perf_session__process_events() depends on the data_size which is read from file header. Use file size directly instead in this case to do the best-effort processing.

Quoted 7 lines
Quoted 4 lines

worked ok for me. Sonny can you verify?

Yes, it works for me as well, thanks!

Quoted 2 lines

David

#3 lore raw
In reply to Namhyung Kim
Quoted 4 lines · Namhyung Kim
tools/perf/util/header.c +0 -0
@@ -2753,6 +2753,15 @@ int perf_session__read_header(struct perf_session *session)
 	if (perf_file_header__read(&f_header, header, fd) < 0)

tools/perf/util/header.c int perf_session__read_header(struct perf_session *session) · line 2763

Quoted 10 lines
tools/perf/util/header.c +8 -0
 		return -EINVAL;
 
+	/*
+	 * Sanity check that perf.data was written cleanly; data size is
+	 * initialized to 0 and updated only if the on_exit function is run.
+	 * If data size is still 0 then the file contains only partial
+	 * information.  Just warn user and process it as much as it can.
+	 */
+	if (f_header.data.size == 0)
+		pr_warning("Data size is 0. Was the record command properly terminated?\n");

Just a detail: it would be nice to make all the user facing messages in tools/perf/util/header.c more specific and more structured. For example prefixing it with 'perf header:' would be fine:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

or something like that.

etc.

Ingo

#4 lore raw
Quoted 6 lines · Ingo Molnar

Just a detail: it would be nice to make all the user facing messages in tools/perf/util/header.c more specific and more structured. For example prefixing it with 'perf header:' would be fine:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

Why put code references in the messages? The message is all on one line so grep finds it quickly for people working on the code.

David

#5 lore raw
Quoted 10 lines · David Ahern
Quoted 6 lines · Ingo Molnar

Just a detail: it would be nice to make all the user facing messages in tools/perf/util/header.c more specific and more structured. For example prefixing it with 'perf header:' would be fine:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

Why put code references in the messages? The message is all on one line so grep finds it quickly for people working on the code.

I'd agree if this was some internal error that should never really trigger (so at most developers see it).

But if I understood it correctly this particular message could trigger for regular users of perf as well, of the perf record is terminated in some unusual fashion. Regular users might not have the perf code handy (and might not know about git grep either).

Thanks,

Ingo

#6 lore raw
Quoted 4 lines · Ingo Molnar

But if I understood it correctly this particular message could trigger for regular users of perf as well, of the perf record is terminated in some unusual fashion. Regular users might not have the perf code handy (and might not know about git grep either).

This is the case I was referring to -- normal users don't care about the code reference, hence the more specific question about how the perf-record session ended.

#7 lore raw
Quoted 9 lines · David Ahern
Quoted 4 lines · Ingo Molnar

But if I understood it correctly this particular message could trigger for regular users of perf as well, of the perf record is terminated in some unusual fashion. Regular users might not have the perf code handy (and might not know about git grep either).

This is the case I was referring to -- normal users don't care about the code reference, hence the more specific question about how the perf-record session ended.

Hm, what do you call 'code reference'?

The message I suggested is:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

I didn't intend 'perf/header' to be a code reference - it wanted to refer to the perf.data header. Maybe that should be formulated in a less confusing manner? Something like:

WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?

Thanks,

Ingo

#8 lore raw

Em Tue, Oct 01, 2013 at 05:33:09PM +0200, Ingo Molnar escreveu:

Quoted 15 lines · David Ahern
Quoted 10 lines · Ingo Molnar
Quoted 4 lines

But if I understood it correctly this particular message could trigger for regular users of perf as well, of the perf record is terminated in some unusual fashion. Regular users might not have the perf code handy (and might not know about git grep either).

This is the case I was referring to -- normal users don't care about the code reference, hence the more specific question about how the perf-record session ended.

Hm, what do you call 'code reference'?

Quoted 7 lines

The message I suggested is:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

I didn't intend 'perf/header' to be a code reference - it wanted to refer to the perf.data header. Maybe that should be formulated in a less confusing manner? Something like:

I liked this last one:

Quoted 2 lines

WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?

Can I patch that up into Namhyung's latest patch?

Sonny, David, from your replies I think I can add Tested-by: tags for both of you?

Thanks,

- Arnaldo

#9 lore raw
Quoted 26 lines · Arnaldo Carvalho de Melo

Em Tue, Oct 01, 2013 at 05:33:09PM +0200, Ingo Molnar escreveu:

Quoted 15 lines · David Ahern
Quoted 10 lines · Ingo Molnar
Quoted 4 lines

But if I understood it correctly this particular message could trigger for regular users of perf as well, of the perf record is terminated in some unusual fashion. Regular users might not have the perf code handy (and might not know about git grep either).

This is the case I was referring to -- normal users don't care about the code reference, hence the more specific question about how the perf-record session ended.

Hm, what do you call 'code reference'?

Quoted 7 lines

The message I suggested is:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

I didn't intend 'perf/header' to be a code reference - it wanted to refer to the perf.data header. Maybe that should be formulated in a less confusing manner? Something like:

I liked this last one:

Quoted 7 lines
Quoted 2 lines

WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?

Can I patch that up into Namhyung's latest patch?

Sonny, David, from your replies I think I can add Tested-by: tags for both of you?

I'm fine with the last message. And yes a Tested by

#10 lore raw

Em Tue, Oct 01, 2013 at 10:59:06AM -0600, David Ahern escreveu:

Quoted 9 lines · Arnaldo Carvalho de Melo
Quoted 8 lines

Em Tue, Oct 01, 2013 at 05:33:09PM +0200, Ingo Molnar escreveu:

Quoted 2 lines · David Ahern
Quoted 1 line · Ingo Molnar

I liked this last one:

Quoted 2 lines

WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?

Quoted 6 lines
Quoted 4 lines

Can I patch that up into Namhyung's latest patch?

Sonny, David, from your replies I think I can add Tested-by: tags for both of you?

I'm fine with the last message. And yes a Tested by

Got to process this now, changing it a bit to replace "perf.data" with "%s" + session->filename, ack?

Otherwise the user may have a 'perf.data' file and despite having informed -i with a different file name still get confused... ;-)

- Arnaldo

#11 lore raw
Quoted 6 lines · Arnaldo Carvalho de Melo

Got to process this now, changing it a bit to replace "perf.data" with "%s" + session->filename, ack?

Otherwise the user may have a 'perf.data' file and despite having informed -i with a different file name still get confused... ;-)

ack. agreed that's the better way to go.

David

#12 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 24 lines · Arnaldo Carvalho de Melo

Em Tue, Oct 01, 2013 at 05:33:09PM +0200, Ingo Molnar escreveu:

Quoted 15 lines · David Ahern
Quoted 10 lines · Ingo Molnar
Quoted 4 lines

But if I understood it correctly this particular message could trigger for regular users of perf as well, of the perf record is terminated in some unusual fashion. Regular users might not have the perf code handy (and might not know about git grep either).

This is the case I was referring to -- normal users don't care about the code reference, hence the more specific question about how the perf-record session ended.

Hm, what do you call 'code reference'?

Quoted 7 lines

The message I suggested is:

WARNING: perf/header: Data size is 0. Was the 'perf record' command properly terminated?

I didn't intend 'perf/header' to be a code reference - it wanted to refer to the perf.data header. Maybe that should be formulated in a less confusing manner? Something like:

Quoted 6 lines

I liked this last one:

Quoted 2 lines

WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?

Can I patch that up into Namhyung's latest patch?

Sure, feel free!

Thanks,

Ingo

#13 lore raw
In reply to Namhyung Kim
perf session: Fix infinite loop on invalid perf.data file
Commit-ID:  b314e5cfd11fd78545ce6c2be42646254390c1aa
Gitweb:     http://git.kernel.org/tip/b314e5cfd11fd78545ce6c2be42646254390c1aa
Author:     Namhyung Kim <[email protected]>
AuthorDate: Mon, 30 Sep 2013 17:19:48 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 4 Oct 2013 15:17:46 -0300

perf session: Fix infinite loop on invalid perf.data file

perf-record updates the header in the perf.data file at termination. Without this update perf-report (and other processing built-ins) it caused an infinite loop when perf report (or something like) called.

This is because the algorithm in __perf_session__process_events() depends on the data_size which is read from file header. Use file size directly instead in this case to do the best-effort processing.

[ Reworded warning as per Ingo Molnar suggestion, replaces 'perf.data'
with session->filename, to precisely identify the data file involved ]

---
 tools/perf/util/header.c  | 12 ++++++++++++
 tools/perf/util/session.c |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)
tools/perf/util/header.c +12 -0
index ce69901..c3e5a3b 100644
@@ -2768,6 +2768,18 @@ int perf_session__read_header(struct perf_session *session)
 	if (perf_file_header__read(&f_header, header, fd) < 0)
 		return -EINVAL;
 
+	/*
+	 * Sanity check that perf.data was written cleanly; data size is
+	 * initialized to 0 and updated only if the on_exit function is run.
+	 * If data size is still 0 then the file contains only partial
+	 * information.  Just warn user and process it as much as it can.
+	 */
+	if (f_header.data.size == 0) {
+		pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
+			   "Was the 'perf record' command properly terminated?\n",
+			   session->filename);
+	}
+
 	nr_attrs = f_header.attrs.size / f_header.attr_size;
 	lseek(fd, f_header.attrs.offset, SEEK_SET);
 
tools/perf/util/session.c +1 -1
index 37c4718..568b750 100644
@@ -1312,7 +1312,7 @@ int __perf_session__process_events(struct perf_session *session,
 	file_offset = page_offset;
 	head = data_offset - page_offset;
 
-	if (data_offset + data_size < file_size)
+	if (data_size && (data_offset + data_size < file_size))
 		file_size = data_offset + data_size;
 
 	progress_next = file_size / 16;