perf session: fix decompression of PERF_RECORD_COMPRESSED records
PATCH 1/1 perf session: fix decompression of PERF_RECORD_COMPRESSED records
Avoid termination of trace loading in case the last record in the decompressed buffer partly resides in the following mmaped PERF_RECORD_COMPRESSED record. In this case NULL value returned by fetch_mmaped_event() means to proceed to the next mmaped record then decompress it and load compressed events.
The issue can be reproduced like this:
$ perf record -z -- some_long_running_workload $ perf report --stdio -vv decomp (B): 44519 to 163000 decomp (B): 48119 to 174800 decomp (B): 65527 to 131072 fetch_mmaped_event: head=0x1ffe0 event->header_size=0x28, mmap_size=0x20000: fuzzed perf.data? Error: failed to process sample ...
Testing:
71: Zstd perf.data compression/decompression : Ok
--- tools/perf/util/session.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-)
tools/perf/util/session.c +33 -14
@@ -1957,9 +1957,31 @@ static int __perf_session__process_pipe_events(struct perf_session *session)return err; } +static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret); +static union perf_event *fetch_mmaped_event (struct perf_session *session,u64 head,size_t mmap_size,char *buf) +{ +return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +ERR_PTR (-EINVAL )); +} + +static union perf_event * +fetch_decomp_event (struct perf_session *session, +u64 head,size_t mmap_size,char *buf) +{ +return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +NULL ); +} + +static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret) {union perf_event *event;
@@ -1971,20 +1993,20 @@ fetch_mmaped_event(struct perf_session *session,return NULL ; event = (union perf_event *)(buf + head); +if (needs_swap) +perf_event_header__bswap (&event->header); -if (session->header.needs_swap) +if (head + event->header.size <= mmap_size) +return event; + +/* We're not fetching the event so swap back again */ +if (needs_swap)perf_event_header__bswap (&event->header); -if (head + event->header.size > mmap_size) { -/* We're not fetching the event so swap back again */ -if (session->header.needs_swap) -perf_event_header__bswap (&event->header); -pr_debug ("%s: head=%#" PRIx64" event->header_size=%#x, mmap_size=%#zx: fuzzed perf.data?\n" , - __func__, head, event->header.size, mmap_size); -return ERR_PTR (-EINVAL ); - } +pr_debug ("%s: head=%#" PRIx64" event->header_size=%#x, mmap_size=%#zx\n" , + __func__, head, event->header.size, mmap_size); -return event; +return ret; }static int __perf_session__process_decomp_events (struct perf_session *session)
@@ -1997,10 +2019,7 @@ static int __perf_session__process_decomp_events(struct perf_session *session)return 0 ;while (decomp->head < decomp->size && !session_done ()) { -union perf_event *event =fetch_mmaped_event (session, decomp->head, decomp->size, decomp->data); - -if (IS_ERR (event)) -return PTR_ERR (event); +union perf_event *event =fetch_decomp_event (session, decomp->head, decomp->size, decomp->data);if (!event)break ;
Quoted 35 lines · Alexey Budankov
Avoid termination of trace loading in case the last record in the decompressed buffer partly resides in the following mmaped
PERF_RECORD_COMPRESSEDrecord. In this case NULL value returned byfetch_mmaped_event()means to proceed to the next mmaped record then decompress it and load compressed events.The issue can be reproduced like this:
$ perf record -z -- some_long_running_workload $ perf report --stdio -vv decomp (B): 44519 to 163000 decomp (B): 48119 to 174800 decomp (B): 65527 to 131072 fetch_mmaped_event: head=0x1ffe0 event->header_size=0x28, mmap_size=0x20000: fuzzed perf.data? Error: failed to process sample ...Testing:
71: Zstd perf.data compression/decompression : OkFixes 57fc032ad643 ("perf session: Avoid infinite loop when seeing invalid header.size")Co-developed-by Jiri Olsa <[email protected]>Signed-off-by Alexey Budankov <[email protected]>--- tools/perf/util/session.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-)tools/perf/util/session.c +0
-0
tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1962
Quoted 6 lines
tools/perf/util/session.c +3
-0@@ -1957,9 +1957,31 @@ static int __perf_session__process_pipe_events(struct perf_session *session)return err; } +static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret);
why not move prefetch_event definition in here?
I don't see any need for the static declaration..
Quoted 4 lines
tools/perf/util/session.c +1
-0+static union perf_event *fetch_mmaped_event (struct perf_session *session,u64 head,size_t mmap_size,char *buf)
tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1976
Quoted 10 lines
tools/perf/util/session.c +10
-0+{ +return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +ERR_PTR (-EINVAL )); +} + +static union perf_event * +fetch_decomp_event (struct perf_session *session, +u64 head,size_t mmap_size,char *buf) +{
if this is decomp specific, it could take 'struct decomp*' as argument
tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1985
Quoted 9 lines
tools/perf/util/session.c +8
-0+return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +NULL ); +} + +static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret) {
'error' might be more suitable then ret in here
thanks,
jirka
Quoted 1 line · Jiri Olsa
On Fri, Nov 15, 2019 at 12:05:14PM +0300, Alexey Budankov wrote:
<SNIP>
Quoted 8 lines
Quoted 8 lines
tools/perf/util/session.c +0
-0@@ -1957,9 +1957,31 @@ static int __perf_session__process_pipe_events(struct perf_session *session)return err; }
Quoted 6 lines
tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1962
Quoted 3 lines
tools/perf/util/session.c +3
-0+static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret);why not move
prefetch_eventdefinition in here?
I don't see any need for the static declaration..
It is just for the sake of more readable patch formatting and, yes, could be avoided and replaced by the definition.
Quoted 17 lines
Quoted 5 lines
tools/perf/util/session.c +1
-0+static union perf_event *fetch_mmaped_event (struct perf_session *session,u64 head,size_t mmap_size,char *buf)tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1976
Quoted 10 lines
tools/perf/util/session.c +10
-0+{ +return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +ERR_PTR (-EINVAL )); +} + +static union perf_event * +fetch_decomp_event (struct perf_session *session, +u64 head,size_t mmap_size,char *buf) +{if this is decomp specific, it could take 'struct decomp*' as argument
Well, it makes sense. whole session object is not required here. Just session->header.needs_swap could be passed as a param. Shall we make it like this?
static union perf_event *fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
Quoted 12 lines
tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1985
Quoted 10 lines
tools/perf/util/session.c +8
-0+return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +NULL ); +} + +static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret) {'error' might be more suitable then ret in here
Accepted for v2.
~Alexey
Quoted 5 lines
thanks,
jirka
Quoted 19 lines · Alexey Budankov
Quoted 1 line · Jiri Olsa
On Fri, Nov 15, 2019 at 12:05:14PM +0300, Alexey Budankov wrote:
<SNIP>
Quoted 8 lines
Quoted 8 lines
tools/perf/util/session.c +0
-0@@ -1957,9 +1957,31 @@ static int __perf_session__process_pipe_events(struct perf_session *session)return err; }Quoted 6 lines
tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1962
Quoted 3 lines
tools/perf/util/session.c +3
-0+static union perf_event * +prefetch_event (char *buf,u64 head,size_t mmap_size, +bool needs_swap,union perf_event *ret);why not move
prefetch_eventdefinition in here?
I don't see any need for the static declaration..
Quoted 2 lines
It is just for the sake of more readable patch formatting and, yes, could be avoided and replaced by the definition.
I think we're trying to avoid static declarations
Quoted 19 lines
Quoted 18 lines
Quoted 6 lines
tools/perf/util/session.c +1
-0+static union perf_event *fetch_mmaped_event (struct perf_session *session,u64 head,size_t mmap_size,char *buf)tools/perf/util/session.c static int __perf_session__process_pipe_events(struct perf_session *session) · line 1976
Quoted 10 lines
tools/perf/util/session.c +10
-0+{ +return prefetch_event (buf, head, mmap_size, + session->header.needs_swap, +ERR_PTR (-EINVAL )); +} + +static union perf_event * +fetch_decomp_event (struct perf_session *session, +u64 head,size_t mmap_size,char *buf) +{if this is decomp specific, it could take 'struct decomp*' as argument
Quoted 6 lines
Well, it makes sense. whole session object is not required here. Just
session->header.needs_swapcould be passed as a param. Shall we make it like this?static union
perf_event*fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
ok, I just saw the call passing all the stuff from 'struct decomp' so I thought it'd save some arguments if we pass the struct itself instead
jirka