Skip to contentSkip to conversation

perf timechart: Fix memory leaks

PATCH Superseded Namhyung Kim 2 participants 6 messages Jun 5 – Jun 6 perf events

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

v2 cover perf timechart: Fix memory leaks

Hello,

This patchset fixes memory leaks in the perf timechart code. I've extended and verified the timechart test with ASAN.

v2)
* use zfree() and explicit clear for potential misuse (Arnaldo)
* fix a wrong condition in the draw_wakeups() (Sashiko)
* move misplaced hunk in the second commit to the first

While it doesn't depend on my other timechart patch series [1], fixing the memory leaks entirely requires some work there. So I'd suggest applying this after the previous work.

Thanks,
Namhyung

[1] https://lore.kernel.org/r/[email protected]

Namhyung Kim (4):
perf timechart: Release event samples at the end perf timechart: Fix memory leaks during record perf timechart: Fix memory leaks in draw_wakeups() perf test: Update perf timechart test

 tools/perf/builtin-timechart.c      | 105 ++++++++++++++++++++++++----
 tools/perf/tests/shell/timechart.sh |  24 ++++---
 2 files changed, 106 insertions(+), 23 deletions(-)
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
author maintainer
#1 lore raw

PATCH v2 1/4 perf timechart: Release event samples at the end

Add timechart__release() to free all data structures added during the sample processing.

---
 tools/perf/builtin-timechart.c | 68 ++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
tools/perf/builtin-timechart.c +68 -0
index 04dbb944a42720b4..27d17268395ed760 100644
@@ -1548,6 +1548,73 @@ static void write_svg_file(struct timechart *tchart, const char *filename)
 	svg_close();
 }
 
+static void timechart__release(struct timechart *tchart)
+{
+	struct per_pid *p = tchart->all_data;
+	struct power_event *pwr = tchart->power_events;
+	struct wake_event *we = tchart->wake_events;
+
+	while (p) {
+		struct per_pid *next_pid = p->next;
+		struct per_pidcomm *c = p->all;
+
+		while (c) {
+			struct per_pidcomm *next_comm = c->next;
+			struct cpu_sample *cs = c->samples;
+			struct io_sample *ios = c->io_samples;
+
+			while (cs) {
+				struct cpu_sample *next = cs->next;
+
+				zfree(&cs->backtrace);
+				cs->next = NULL;
+				free(cs);
+
+				cs = next;
+			}
+
+			while (ios) {
+				struct io_sample *next = ios->next;
+
+				ios->next = NULL;
+				free(ios);
+
+				ios = next;
+			}
+
+			zfree(&c->comm);
+			c->next = NULL;
+			free(c);
+
+			c = next_comm;
+		}
+
+		p->next = NULL;
+		free(p);
+
+		p = next_pid;
+	}
+
+	while (pwr) {
+		struct power_event *next = pwr->next;
+
+		pwr->next = NULL;
+		free(pwr);
+
+		pwr = next;
+	}
+
+	while (we) {
+		struct wake_event *next = we->next;
+
+		zfree(&we->backtrace);
+		we->next = NULL;
+		free(we);
+
+		we = next;
+	}
+}
+
 static int process_header(struct perf_file_section *section __maybe_unused,
 			  struct perf_header *ph,
 			  int feat,
@@ -2079,6 +2146,7 @@ int cmd_timechart(int argc, const char **argv)
 
 	ret = __cmd_timechart(&tchart, output_name);
 out:
+	timechart__release(&tchart);
 	zfree(&cpus_cstate_start_times);
 	zfree(&cpus_cstate_state);
 	zfree(&cpus_pstate_start_times);
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
#2 lore raw

Re: [PATCH v2 1/4] perf timechart: Release event samples at the end

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] The newly added timechart__release() function introduces a double-free vulnerability by unconditionally freeing cs->backtrace for all CPU samples, even though sched_switch() shares the exact same backtrace string pointer between multiple samples.

Pre-existing issues:
- [High] Use-after-free inside cat_backtrace() error path due to freeing the memstream buffer before closing the stream. --

commit ee81a934b42b228370b97d5f0e7e208b04ba839a
Author: Namhyung Kim <[email protected]>

perf timechart: Release event samples at the end

Add timechart__release() to free all data structures added during the sample processing.

Quoted 4 lines
tools/perf/builtin-timechart.c +0 -0
index 28f33e39895d3..99f5102239963 100644

[ ... ]

Quoted 7 lines
tools/perf/builtin-timechart.c +3 -0
@@ -1509,6 +1509,73 @@ static void write_svg_file(struct timechart *tchart, const char *filename)
 	svg_close();
 }
 
+static void timechart__release(struct timechart *tchart)
+{
+	struct per_pid *p = tchart->all_data;

[ ... ]

tools/perf/builtin-timechart.c static void write_svg_file(struct timechart *tchart, const char *filename) · line 1569

Quoted 9 lines
tools/perf/builtin-timechart.c +9 -0
+		while (c) {
+			struct per_pidcomm *next_comm = c->next;
+			struct cpu_sample *cs = c->samples;
+			struct io_sample *ios = c->io_samples;
+
+			while (cs) {
+				struct cpu_sample *next = cs->next;
+
+				zfree(&cs->backtrace);

[Severity: High]
Does unconditionally freeing cs->backtrace cause a double-free?

Looking at sched_switch() in tools/perf/builtin-timechart.c, it passes the same dynamically allocated backtrace string pointer to pid_put_sample() for both prev_pid and next_pid:

tools/perf/builtin-timechart.c:sched_switch() {
    ...
    pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
                   prev_p->current->state_since, timestamp,
                   backtrace);
    ...
    pid_put_sample(tchart, next_pid, p->current->state, cpu,
                   p->current->state_since, timestamp,
                   backtrace);
    ...
}

Then pid_put_sample() stores this raw pointer into sample->backtrace without duplicating it:

tools/perf/builtin-timechart.c:pid_put_sample() {
    ...
    sample->backtrace = backtrace;
    ...
}

Because the pointer is shared among multiple CPU samples, freeing it unconditionally for every sample here could result in a double-free memory corruption.

Quoted 5 lines
tools/perf/builtin-timechart.c +5 -0
+				cs->next = NULL;
+				free(cs);
+
+				cs = next;
+			}

[ ... ]

tools/perf/builtin-timechart.c int cmd_timechart(int argc, const char **argv) · line 2107

Quoted 5 lines
tools/perf/builtin-timechart.c +1 -0
@@ -2037,6 +2104,7 @@ int cmd_timechart(int argc, const char **argv)
 
 	ret = __cmd_timechart(&tchart, output_name);
 out:
+	timechart__release(&tchart);

[Severity: High]
This isn't a bug introduced by this patch, but looking at cat_backtrace() in the same file, is there a use-after-free in its error path?

When the callchain context is invalid, it attempts to discard the buffer by freeing p:

tools/perf/builtin-timechart.c:cat_backtrace() {
    ...
    zfree(&p);
    goto exit;
    ...

But the goto exit path closes the stream:

    exit:
        addr_location__exit(&al);
        fclose(f);

        return p;
}

Because fclose(f) operates on the open stream, it accesses its internal buffer to append a null byte. If p was flushed and freed, wouldn't this cause a use-after-free?

Additionally, fclose(f) overwrites p with the buffer address, which means the function might return a freed pointer to the caller.

Quoted 2 lines
tools/perf/builtin-timechart.c +0 -0
 	zfree(&cpus_cstate_start_times);
 	zfree(&cpus_cstate_state);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v2 2/4 perf timechart: Fix memory leaks during record

In reply to Namhyung Kim

The command line options can be passed without copy as their lifetime is limited within the same function.

---
 tools/perf/builtin-timechart.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
tools/perf/builtin-timechart.c +20 -11
index 27d17268395ed760..a73f0ab22fd64241 100644
@@ -1834,6 +1834,7 @@ static int timechart__io_record(int argc, const char **argv, const char *output_
 		"syscalls:sys_exit_select",
 	};
 	unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
+	int ret;
 
 	rec_argc = common_args_nr +
 		disk_events_nr * 4 +
@@ -1852,7 +1853,7 @@ static int timechart__io_record(int argc, const char **argv, const char *output_
 
 	p = rec_argv;
 	for (i = 0; i < common_args_nr; i++)
-		*p++ = strdup(common_args[i]);
+		*p++ = common_args[i];
 
 	for (i = 0; i < disk_events_nr; i++) {
 		if (!is_valid_tracepoint(disk_events[i])) {
@@ -1861,7 +1862,7 @@ static int timechart__io_record(int argc, const char **argv, const char *output_
 		}
 
 		*p++ = "-e";
-		*p++ = strdup(disk_events[i]);
+		*p++ = disk_events[i];
 		*p++ = "--filter";
 		*p++ = filter;
 	}
@@ -1872,7 +1873,7 @@ static int timechart__io_record(int argc, const char **argv, const char *output_
 		}
 
 		*p++ = "-e";
-		*p++ = strdup(net_events[i]);
+		*p++ = net_events[i];
 		*p++ = "--filter";
 		*p++ = filter;
 	}
@@ -1883,7 +1884,7 @@ static int timechart__io_record(int argc, const char **argv, const char *output_
 		}
 
 		*p++ = "-e";
-		*p++ = strdup(poll_events[i]);
+		*p++ = poll_events[i];
 		*p++ = "--filter";
 		*p++ = filter;
 	}
@@ -1891,7 +1892,11 @@ static int timechart__io_record(int argc, const char **argv, const char *output_
 	for (i = 0; i < (unsigned int)argc; i++)
 		*p++ = argv[i];
 
-	return cmd_record(rec_argc, rec_argv);
+	ret = cmd_record(rec_argc, rec_argv);
+
+	free(rec_argv);
+	free(filter);
+	return ret;
 }
 
 
@@ -1902,6 +1907,7 @@ static int timechart__record(struct timechart *tchart, int argc, const char **ar
 	const char **rec_argv;
 	const char **p;
 	unsigned int record_elems;
+	int ret;
 
 	const char * const common_args[] = {
 		"record", "-a", "-R", "-c", "1", "-o", output_data,
@@ -1966,24 +1972,27 @@ static int timechart__record(struct timechart *tchart, int argc, const char **ar
 
 	p = rec_argv;
 	for (i = 0; i < common_args_nr; i++)
-		*p++ = strdup(common_args[i]);
+		*p++ = common_args[i];
 
 	for (i = 0; i < backtrace_args_no; i++)
-		*p++ = strdup(backtrace_args[i]);
+		*p++ = backtrace_args[i];
 
 	for (i = 0; i < tasks_args_nr; i++)
-		*p++ = strdup(tasks_args[i]);
+		*p++ = tasks_args[i];
 
 	for (i = 0; i < power_args_nr; i++)
-		*p++ = strdup(power_args[i]);
+		*p++ = power_args[i];
 
 	for (i = 0; i < old_power_args_nr; i++)
-		*p++ = strdup(old_power_args[i]);
+		*p++ = old_power_args[i];
 
 	for (j = 0; j < (unsigned int)argc; j++)
 		*p++ = argv[j];
 
-	return cmd_record(rec_argc, rec_argv);
+	ret = cmd_record(rec_argc, rec_argv);
+
+	free(rec_argv);
+	return ret;
 }
 
 static int
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
author maintainer
#4 lore raw

PATCH v2 3/4 perf timechart: Fix memory leaks in draw_wakeups()

In reply to Namhyung Kim

The second loop for per_pidcommd is meaningful only when it doesn't have from and to tasks. Also make sure c->Y is set before copying the comm string otherwise it will be overwritten by next one.

---
 tools/perf/builtin-timechart.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/builtin-timechart.c +3 -3
index a73f0ab22fd64241..3f9153d5ecfb79b0 100644
@@ -1110,12 +1110,12 @@ static void draw_wakeups(struct timechart *tchart)
 					c = c->next;
 				}
 				c = p->all;
-				while (c) {
-					if (p->pid == we->waker && !from) {
+				while (c && (!from || !to)) {
+					if (c->Y && p->pid == we->waker && !from) {
 						from = c->Y;
 						task_from = strdup(c->comm);
 					}
-					if (p->pid == we->wakee && !to) {
+					if (c->Y && p->pid == we->wakee && !to) {
 						to = c->Y;
 						task_to = strdup(c->comm);
 					}
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
author maintainer
#5 lore raw

PATCH v2 4/4 perf test: Update perf timechart test

In reply to Namhyung Kim

To include IO-only and backtrace modes to test different code paths.

  $ sudo perf test -vv timechart
  135: perf timechart tests                        : Running
  135: perf timechart tests:
  ---- start ----
  test child forked, pid 2413665
  perf timechart Basic test
  perf timechart Basic test [Success]
  perf timechart IO-only test
  perf timechart IO-only test [Success]
  perf timechart Backtrace test
  perf timechart Backtrace test [Success]
  ---- end(0) ----
  135: perf timechart tests                        : Ok

=== Test Summary ===
Passed main tests : 1

  Passed subtests   : 0
  Skipped tests     : 0
  Failed tests      : 0
---
 tools/perf/tests/shell/timechart.sh | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
tools/perf/tests/shell/timechart.sh +15 -9
index b14b3472c2846b32..40072b32171a9a1c 100755
@@ -9,7 +9,7 @@ perfdata=$(mktemp /tmp/__perf_timechart_test.perf.data.XXXXX)
 output=$(mktemp /tmp/__perf_timechart_test.output.XXXXX.svg)
 
 cleanup() {
-	rm -f "${perfdata}"
+	rm -f "${perfdata}"*
 	rm -f "${output}"
 	trap - EXIT TERM INT
 }
@@ -22,38 +22,41 @@ trap_cleanup() {
 trap trap_cleanup EXIT TERM INT
 
 test_timechart() {
-	echo "Basic perf timechart test"
+	NAME=$1
+	OPTION=$2
+
+	echo "perf timechart ${NAME} test"
 
 	# Try to record timechart data.
 	# perf timechart record uses system-wide recording and specific tracepoints.
 	# If it fails (e.g. permissions, missing tracepoints), skip the test.
-	if ! perf timechart record -o "${perfdata}" true > /dev/null 2>&1; then
-		echo "Basic perf timechart test [Skipped: perf timechart record failed (permissions/events?)]"
+	if ! perf timechart record -o "${perfdata}" ${OPTION} true > /dev/null 2>&1; then
+		echo "perf timechart ${NAME} test [Skipped: perf timechart record failed (permissions/events?)]"
 		return
 	fi
 
 	# Generate the timechart
 	if ! perf timechart -i "${perfdata}" -o "${output}" > /dev/null 2>&1; then
-		echo "Basic perf timechart test [Failed: perf timechart command failed]"
+		echo "perf timechart ${NAME} test [Failed: perf timechart command failed]"
 		err=1
 		return
 	fi
 
 	# Check if output file exists and is not empty
 	if [ ! -s "${output}" ]; then
-		echo "Basic perf timechart test [Failed: output file is empty or missing]"
+		echo "perf timechart ${NAME} test [Failed: output file is empty or missing]"
 		err=1
 		return
 	fi
 
 	# Check if it looks like an SVG
 	if ! grep -q "svg" "${output}"; then
-		echo "Basic perf timechart test [Failed: output doesn't look like SVG]"
+		echo "perf timechart ${NAME} test [Failed: output doesn't look like SVG]"
 		err=1
 		return
 	fi
 
-	echo "Basic perf timechart test [Success]"
+	echo "perf timechart ${NAME} test [Success]"
 }
 
 if ! perf check feature -q libtraceevent ; then
@@ -62,6 +65,9 @@ if ! perf check feature -q libtraceevent ; then
 	exit 2
 fi
 
-test_timechart
+test_timechart "Basic" ""
+test_timechart "IO-only" "-I"
+test_timechart "Backtrace" "-g"
+
 cleanup
 exit $err
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog