Skip to contentSkip to conversation

perf metric: Fix some memory leaks

PATCH In review Namhyung Kim 2 participants 5 messages Sep 5, 2020 – Sep 7, 2020 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 1/2 perf metric: Fix some memory leaks

I found some memory leaks while reading the metric code. Some are real and others only occur in the error path. When it failed during metric or event parsing, it should release all resources properly.

Cc Kajol Jain <[email protected]>
Cc John Garry <[email protected]>
Cc Ian Rogers <[email protected]>
Fixes b18f3e365019d ("perf stat: Support JSON metrics in perf stat")
---
 tools/perf/util/metricgroup.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/metricgroup.c +5 -2
index 8831b964288f..af664d6218d6 100644
@@ -530,6 +530,9 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
 						continue;
 					strlist__add(me->metrics, s);
 				}
+
+				if (!raw)
+					free(s);
 			}
 			free(omg);
 		}
@@ -1040,7 +1043,7 @@ static int parse_groups(struct evlist *perf_evlist, const char *str,
 	ret = metricgroup__add_metric_list(str, metric_no_group,
 					   &extra_events, &metric_list, map);
 	if (ret)
-		return ret;
+		goto out;
 	pr_debug("adding %s\n", extra_events.buf);
 	bzero(&parse_error, sizeof(parse_error));
 	ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
@@ -1048,11 +1051,11 @@ static int parse_groups(struct evlist *perf_evlist, const char *str,
 		parse_events_print_error(&parse_error, extra_events.buf);
 		goto out;
 	}
-	strbuf_release(&extra_events);
 	ret = metricgroup__setup_events(&metric_list, metric_no_merge,
 					perf_evlist, metric_events);
 out:
 	metricgroup__free_metrics(&metric_list);
+	strbuf_release(&extra_events);
 	return ret;
 }
 
Signature · 1 line
2.28.0.526.ge36021eeef-goog
author maintainer
#1 lore raw

PATCH v2 2/2 perf metric: Fix some memory leaks - part 2

The metric_event_delete() missed to free expr->metric_events and it should free an expr when metric_refs allocation failed.

Cc Kajol Jain <[email protected]>
Cc John Garry <[email protected]>
Cc Ian Rogers <[email protected]>
Fixes 4ea2896715e67 ("perf metric: Collect referenced metrics in struct metric_expr")
---
 tools/perf/util/metricgroup.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/metricgroup.c +2 -0
index af664d6218d6..b28c09447c10 100644
@@ -85,6 +85,7 @@ static void metric_event_delete(struct rblist *rblist __maybe_unused,
 
 	list_for_each_entry_safe(expr, tmp, &me->head, nd) {
 		free(expr->metric_refs);
+		free(expr->metric_events);
 		free(expr);
 	}
 
@@ -316,6 +317,7 @@ static int metricgroup__setup_events(struct list_head *groups,
 			if (!metric_refs) {
 				ret = -ENOMEM;
 				free(metric_events);
+				free(expr);
 				break;
 			}
 
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Acked-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2

Quoted 8 lines · Namhyung Kim

The metric_event_delete() missed to free expr->metric_events and it should free an expr when metric_refs allocation failed.

Cc Kajol Jain <[email protected]>
Cc John Garry <[email protected]>
Cc Ian Rogers <[email protected]>
Fixes 4ea2896715e67 ("perf metric: Collect referenced metrics in struct metric_expr")

thanks,
jirka

Quoted 27 lines
---
 tools/perf/util/metricgroup.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/metricgroup.c +2 -0
index af664d6218d6..b28c09447c10 100644
@@ -85,6 +85,7 @@ static void metric_event_delete(struct rblist *rblist __maybe_unused,
 
 	list_for_each_entry_safe(expr, tmp, &me->head, nd) {
 		free(expr->metric_refs);
+		free(expr->metric_events);
 		free(expr);
 	}
 
@@ -316,6 +317,7 @@ static int metricgroup__setup_events(struct list_head *groups,
 			if (!metric_refs) {
 				ret = -ENOMEM;
 				free(metric_events);
+				free(expr);
 				break;
 			}
 
Signature · 1 line
2.28.0.526.ge36021eeef-goog
Acked-by on v2 1/2
#3 lore raw
In reply to Namhyung Kim
Quoted 9 lines · Namhyung Kim

I found some memory leaks while reading the metric code. Some are real and others only occur in the error path. When it failed during metric or event parsing, it should release all resources properly.

Cc Kajol Jain <[email protected]>
Cc John Garry <[email protected]>
Cc Ian Rogers <[email protected]>
Fixes b18f3e365019d ("perf stat: Support JSON metrics in perf stat")

thanks,
jirka

Quoted 43 lines
---
 tools/perf/util/metricgroup.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/metricgroup.c +5 -2
index 8831b964288f..af664d6218d6 100644
@@ -530,6 +530,9 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
 						continue;
 					strlist__add(me->metrics, s);
 				}
+
+				if (!raw)
+					free(s);
 			}
 			free(omg);
 		}
@@ -1040,7 +1043,7 @@ static int parse_groups(struct evlist *perf_evlist, const char *str,
 	ret = metricgroup__add_metric_list(str, metric_no_group,
 					   &extra_events, &metric_list, map);
 	if (ret)
-		return ret;
+		goto out;
 	pr_debug("adding %s\n", extra_events.buf);
 	bzero(&parse_error, sizeof(parse_error));
 	ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
@@ -1048,11 +1051,11 @@ static int parse_groups(struct evlist *perf_evlist, const char *str,
 		parse_events_print_error(&parse_error, extra_events.buf);
 		goto out;
 	}
-	strbuf_release(&extra_events);
 	ret = metricgroup__setup_events(&metric_list, metric_no_merge,
 					perf_evlist, metric_events);
 out:
 	metricgroup__free_metrics(&metric_list);
+	strbuf_release(&extra_events);
 	return ret;
 }
 
Signature · 1 line
2.28.0.526.ge36021eeef-goog
author maintainer
#4 lore raw

Hi Jiri,

Quoted 6 lines · Jiri Olsa
Quoted 4 lines · Namhyung Kim

I found some memory leaks while reading the metric code. Some are real and others only occur in the error path. When it failed during metric or event parsing, it should release all resources properly.

Quoted 7 lines
Quoted 5 lines
Cc Kajol Jain <[email protected]>
Cc John Garry <[email protected]>
Cc Ian Rogers <[email protected]>
Fixes b18f3e365019d ("perf stat: Support JSON metrics in perf stat")

Thanks for the review. Actually I have another batch of leakage fixes for the metric code found by ASAN. Will post it soon..

Thanks
Namhyung