Re: [PATCH 2/5] perf tools: fix piped mode read code

From: Stephane Eranian
Date: Mon May 14 2012 - 16:24:56 EST


On Fri, May 11, 2012 at 2:11 AM, David Ahern <dsahern@xxxxxxxxx> wrote:
> Hi Stephane:
>
> This patch no longer applies cleanly. Can you update the series?
>
Will post the series again tomorrow.
Arnaldo, you need to take a look at this, it's been posted weeks ago.
We need this patch to make perf in pipe mode behave like perf in
file mode.
Thanks
> David
>
>
> On 4/11/12 3:01 AM, Stephane Eranian wrote:
>>
>> 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@xxxxxxxxxx>
>> ---
>> Âtools/perf/util/session.c | Â 35 +++++++++++++++++++++++++++--------
>> Â1 files changed, 27 insertions(+), 8 deletions(-)
>>
>> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
>> index 9412e3b..d13e915 100644
>> --- a/tools/perf/util/session.c
>> +++ b/tools/perf/util/session.c
>> @@ -1056,8 +1056,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;
>> @@ -1066,8 +1067,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;
>> @@ -1077,13 +1084,23 @@ static int
>> __perf_session__process_pipe_events(struct perf_session *self,
>> Â Â Â Â}
>>
>> Â Â Â Â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)) {
>> @@ -1099,9 +1116,10 @@ static int
>> __perf_session__process_pipe_events(struct perf_session *self,
>> Â Â Â Â Â Â Â Â}
>> Â Â Â Â}
>>
>> - Â Â Â if ((skip = perf_session__process_event(self,&event, tool, head))<
>> Â0) {
>>
>> + Â Â Â skip = perf_session__process_event(self, event, tool, head);
>> + Â Â Â if (skip< Â0) {
>> Â Â Â Â Â Â Â Âdump_printf("%#" PRIx64 " [%#x]: skipping unknown header
>> type: %d\n",
>> - Â Â Â Â Â Â Â Â Â Â Â Â Â head, event.header.size, event.header.type);
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â head, event->header.size, event->header.type);
>> Â Â Â Â Â Â Â Â/*
>> Â Â Â Â Â Â Â Â * assume we lost track of the stream, check alignment, and
>> Â Â Â Â Â Â Â Â * increment a single u64 in the hope to catch on again
>> 'soon'.
>> @@ -1122,6 +1140,7 @@ static int
>> __perf_session__process_pipe_events(struct perf_session *self,
>> Âdone:
>> Â Â Â Âerr = 0;
>> Âout_err:
>> + Â Â Â free(buf);
>> Â Â Â Âperf_session__warn_about_errors(self, tool);
>> Â Â Â Âperf_session_free_sample_buffers(self);
>> Â Â Â Âreturn err;
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/