Skip to contentSkip to conversation

perf hisi-ptt: Fix memory leak in lseek failure handling

PATCH Applied Kuan-Wei Chiu 4 participants 5 messages Sep 30, 2023 – Oct 9, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 45b614bb73bf..43bd1ca62d58 100644
@@ -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
maintainer Acked-by on v1 1/1
#1 lore raw

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.

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
index 45b614bb73bf..43bd1ca62d58 100644
@@ -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

#2 lore raw
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

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,
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
index 45b614bb73bf..43bd1ca62d58 100644
@@ -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

maintainer Applied
#3 lore raw
In reply to Namhyung Kim
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

Applied to perf-tools-next, thanks!

Acked-by on v1 1/1
#4 lore raw
In reply to Kuan-Wei Chiu
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.

---
 tools/perf/util/hisi-ptt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/hisi-ptt.c +0 -0
index 45b614bb73bf..43bd1ca62d58 100644

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);