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>
This commit is contained in:
Stephane Eranian 2012-05-15 13:28:12 +02:00 committed by Arnaldo Carvalho de Melo
parent 1a1ed1ba67
commit 444d286639

View File

@ -1098,8 +1098,9 @@ volatile int session_done;
static int __perf_session__process_pipe_events(struct perf_session *self, static int __perf_session__process_pipe_events(struct perf_session *self,
struct perf_tool *tool) struct perf_tool *tool)
{ {
union perf_event event; union perf_event *event;
uint32_t size; uint32_t size, cur_size = 0;
void *buf = NULL;
int skip = 0; int skip = 0;
u64 head; u64 head;
int err; int err;
@ -1108,8 +1109,14 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
perf_tool__fill_defaults(tool); perf_tool__fill_defaults(tool);
head = 0; head = 0;
cur_size = sizeof(union perf_event);
buf = malloc(cur_size);
if (!buf)
return -errno;
more: 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) {
if (err == 0) if (err == 0)
goto done; goto done;
@ -1119,13 +1126,23 @@ more:
} }
if (self->header.needs_swap) 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) if (size == 0)
size = 8; 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); p += sizeof(struct perf_event_header);
if (size - 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", 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; err = -EINVAL;
goto out_err; goto out_err;
} }
@ -1158,6 +1175,7 @@ more:
done: done:
err = 0; err = 0;
out_err: out_err:
free(buf);
perf_session__warn_about_errors(self, tool); perf_session__warn_about_errors(self, tool);
perf_session_free_sample_buffers(self); perf_session_free_sample_buffers(self);
return err; return err;