From: tip-bot for Stephane Eranian <eranian@google.com>
To: linux-tip-commits@vger.kernel.org
Cc: acme@redhat.com, linux-kernel@vger.kernel.org,
eranian@google.com, hpa@zytor.com, mingo@kernel.org,
peterz@infradead.org, dsahern@gmail.com, tglx@linutronix.de,
mingo@elte.hu
Subject: [tip:perf/core] perf tools: Fix piped mode read code
Date: Wed, 23 May 2012 08:29:51 -0700 [thread overview]
Message-ID: <tip-444d28663936e286c752f05feca44d6041b1fce4@git.kernel.org> (raw)
In-Reply-To: <1337081295-10303-3-git-send-email-eranian@google.com>
Commit-ID: 444d28663936e286c752f05feca44d6041b1fce4
Gitweb: http://git.kernel.org/tip/444d28663936e286c752f05feca44d6041b1fce4
Author: Stephane Eranian <eranian@google.com>
AuthorDate: Tue, 15 May 2012 13:28:12 +0200
Committer: Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Tue, 22 May 2012 12:59:52 -0300
perf tools: Fix piped mode read code
In __perf_session__process_pipe_events(), there was a risk we would read
more than what a union perf_event struct can hold. this could happen in
case, perf is reading a file which contains new record types it does not
know about and which are larger than anything it knows about.
In general, perf is supposed to skip records it does not understand, but
in pipe mode, those have to be read and ignored. The fixed size header
contains the size of the record, but that size may be larger than union
perf_event, yet it was used as the backing to the read in:
union perf_event event;
void *p;
size = event->header.size;
p = &event;
p += sizeof(struct perf_event_header);
if (size - sizeof(struct perf_event_header)) {
err = readn(self->fd, p, size - sizeof(struct perf_event_header));
We fix this by allocating a buffer based on the size reported in the
header. We reuse the buffer as much as we can. We realloc in case it
becomes too small. In the common case, the performance impact is
negligible.
Signed-off-by: Stephane Eranian <eranian@google.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1337081295-10303-3-git-send-email-eranian@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/session.c | 34 ++++++++++++++++++++++++++--------
1 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 17c9ace..93d355d 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1098,8 +1098,9 @@ volatile int session_done;
static int __perf_session__process_pipe_events(struct perf_session *self,
struct perf_tool *tool)
{
- union perf_event event;
- uint32_t size;
+ union perf_event *event;
+ uint32_t size, cur_size = 0;
+ void *buf = NULL;
int skip = 0;
u64 head;
int err;
@@ -1108,8 +1109,14 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
perf_tool__fill_defaults(tool);
head = 0;
+ cur_size = sizeof(union perf_event);
+
+ buf = malloc(cur_size);
+ if (!buf)
+ return -errno;
more:
- err = readn(self->fd, &event, sizeof(struct perf_event_header));
+ event = buf;
+ err = readn(self->fd, event, sizeof(struct perf_event_header));
if (err <= 0) {
if (err == 0)
goto done;
@@ -1119,13 +1126,23 @@ more:
}
if (self->header.needs_swap)
- perf_event_header__bswap(&event.header);
+ perf_event_header__bswap(&event->header);
- size = event.header.size;
+ size = event->header.size;
if (size == 0)
size = 8;
- p = &event;
+ if (size > cur_size) {
+ void *new = realloc(buf, size);
+ if (!new) {
+ pr_err("failed to allocate memory to read event\n");
+ goto out_err;
+ }
+ buf = new;
+ cur_size = size;
+ event = buf;
+ }
+ p = event;
p += sizeof(struct perf_event_header);
if (size - sizeof(struct perf_event_header)) {
@@ -1141,9 +1158,9 @@ more:
}
}
- if ((skip = perf_session__process_event(self, &event, tool, head)) < 0) {
+ if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
- head, event.header.size, event.header.type);
+ head, event->header.size, event->header.type);
err = -EINVAL;
goto out_err;
}
@@ -1158,6 +1175,7 @@ more:
done:
err = 0;
out_err:
+ free(buf);
perf_session__warn_about_errors(self, tool);
perf_session_free_sample_buffers(self);
return err;
next prev parent reply other threads:[~2012-05-23 15:30 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-15 11:28 [PATCH v2 0/5] perf tools: add meta-data header support in pipe mode Stephane Eranian
2012-05-15 11:28 ` [PATCH v2 1/5] perf inject: fix broken perf inject -b Stephane Eranian
2012-05-16 1:58 ` David Ahern
2012-05-23 15:29 ` [tip:perf/core] perf inject: Fix " tip-bot for Stephane Eranian
2012-05-15 11:28 ` [PATCH v2 2/5] perf tools: fix piped mode read code Stephane Eranian
2012-05-16 2:24 ` David Ahern
2012-05-23 15:29 ` tip-bot for Stephane Eranian [this message]
2012-05-15 11:28 ` [PATCH v2 3/5] perf tools: rename HEADER_TRACE_INFO to HEADER_TRACING_DATA Stephane Eranian
2012-05-16 2:34 ` David Ahern
2012-05-23 15:28 ` [tip:perf/core] " tip-bot for Stephane Eranian
2012-05-15 11:28 ` [PATCH v2 4/5] perf record: add meta-data support for pipe-mode Stephane Eranian
2012-05-16 3:34 ` David Ahern
2012-05-16 7:41 ` Stephane Eranian
2012-05-18 16:50 ` David Ahern
2012-05-18 17:19 ` Arnaldo Carvalho de Melo
2012-05-22 17:33 ` Peter Zijlstra
2012-05-22 17:51 ` Stephane Eranian
2012-05-23 0:45 ` Namhyung Kim
2012-05-23 1:01 ` Arnaldo Carvalho de Melo
2012-05-23 8:10 ` Peter Zijlstra
2012-05-23 8:21 ` Peter Zijlstra
2012-05-23 13:06 ` Stephane Eranian
2012-05-24 15:36 ` David Ahern
2012-05-24 16:19 ` Arnaldo Carvalho de Melo
2012-05-24 16:22 ` David Ahern
2012-05-24 16:44 ` Arnaldo Carvalho de Melo
2012-05-15 11:28 ` [PATCH v2 5/5] perf: make perf buildid-list work better with pipe mode Stephane Eranian
2012-05-16 3:55 ` David Ahern
2012-05-23 15:30 ` [tip:perf/core] perf buildid-list: Work " tip-bot for Stephane Eranian
2012-05-16 1:34 ` [PATCH v2 0/5] perf tools: add meta-data header support in " Namhyung Kim
2012-05-16 2:05 ` David Ahern
2012-05-16 2:32 ` Namhyung Kim
2012-05-16 2:38 ` David Ahern
2012-05-16 2:50 ` Namhyung Kim
2012-05-16 15:03 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tip-444d28663936e286c752f05feca44d6041b1fce4@git.kernel.org \
--to=eranian@google.com \
--cc=acme@redhat.com \
--cc=dsahern@gmail.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.