perf hisi-ptt: Fix memory leak in lseek failure handling
Subsystems (2)
PATCH 1/1 perf hisi-ptt: Fix memory leak in lseek failure handling
In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed lseek operation. This patch addresses the problem by releasing the old memory before returning -errno in case of a lseek failure. This ensures that memory is properly managed and avoids potential memory leaks.
--- tools/perf/util/hisi-ptt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/hisi-ptt.c +3 -1
@@ -108,8 +108,10 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session, data_offset =0 ; }else { data_offset =lseek (fd,0 ,SEEK_CUR ); -if (data_offset ==-1 ) +if (data_offset ==-1 ) { +free (data);return -errno; + } } err =readn (fd, data, size);
Signature · 1 line
2.25.1
Hello,
Quoted 8 lines · Kuan-Wei Chiu
In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed lseek operation. This patch addresses the problem by releasing the old memory before returning -errno in case of a lseek failure. This ensures that memory is properly managed and avoids potential memory leaks.
Signed-off-by Kuan-Wei Chiu <[email protected]>
Thanks,
Namhyung
Quoted 23 lines
--- tools/perf/util/hisi-ptt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/perf/util/hisi-ptt.c +3
-2@@ -108,8 +108,10 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session, data_offset =0 ; }else { data_offset =lseek (fd,0 ,SEEK_CUR ); -if (data_offset ==-1 ) +if (data_offset ==-1 ) { +free (data);return -errno; + } } err =readn (fd, data, size); --2.25.1
Quoted 10 lines · On Mon, 2 Oct 2023 21:48:14 -0700 Namhyung Kim <[email protected]> wrote:
Hello,
Quoted 7 lines · Kuan-Wei Chiu
In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed lseek operation. This patch addresses the problem by releasing the old memory before returning -errno in case of a lseek failure. This ensures that memory is properly managed and avoids potential memory leaks.
Quoted 4 lines
Quoted 1 line
Signed-off-by Kuan-Wei Chiu <[email protected]>Acked-by Namhyung Kim <[email protected]>
FYI, Ian Rogers has a series with a similar fix that goes a little further and drops the assignment of data_offset seen just at the top of the context below as it is unused.
https://lore.kernel.org/all/[email protected]/
I haven't replied to this yet as want to check some related handling with one of my China based colleagues and it's a festival this week.
One of us will get back on this next week,
Jonathan
Quoted 28 lines
Thanks,
NamhyungQuoted 23 lines
--- tools/perf/util/hisi-ptt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/perf/util/hisi-ptt.c +3
-2@@ -108,8 +108,10 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session, data_offset =0 ; }else { data_offset =lseek (fd,0 ,SEEK_CUR ); -if (data_offset ==-1 ) +if (data_offset ==-1 ) { +free (data);return -errno; + } } err =readn (fd, data, size); --2.25.1
Quoted 11 lines · Namhyung Kim
Hello,
Quoted 7 lines · Kuan-Wei Chiu
In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed lseek operation. This patch addresses the problem by releasing the old memory before returning -errno in case of a lseek failure. This ensures that memory is properly managed and avoids potential memory leaks.
Quoted 3 lines
Quoted 1 line
Signed-off-by Kuan-Wei Chiu <[email protected]>Acked-by Namhyung Kim <[email protected]>
Applied to perf-tools-next, thanks!
Quoted 16 lines · Kuan-Wei Chiu
In the previous code, there was a memory leak issue where the previously allocated memory was not freed upon a failed lseek operation. This patch addresses the problem by releasing the old memory before returning -errno in case of a lseek failure. This ensures that memory is properly managed and avoids potential memory leaks.
Signed-off-by Kuan-Wei Chiu <[email protected]>--- tools/perf/util/hisi-ptt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/perf/util/hisi-ptt.c +0
-0
tools/perf/util/hisi-ptt.c static int hisi_ptt_process_auxtrace_event(struct perf_session *session, · line 114
Quoted 8 lines
tools/perf/util/hisi-ptt.c +3
-1@@ -108,8 +108,10 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session, data_offset =0 ; }else { data_offset =lseek (fd,0 ,SEEK_CUR ); -if (data_offset ==-1 ) +if (data_offset ==-1 ) { +free (data);return -errno; + }
Sorry for the late reply, this looks correct to me:
This patch makes me think whether data_offset is necessary for PTT. The PTT only supports dump raw trace data and since this is an uncore PMU we only have one single data source so we are not using the auxtrace_queues for sorting or ordering the buffers. It seems there's no need to record the data_offset for later mmap(), just read trace data out and dump them is enough. I'll further check and have some tests on this.
I see Namhyung has already taken this. Thanks for the fix.
Quoted 4 lines
tools/perf/util/hisi-ptt.c +0
-0} err =readn (fd, data, size);