All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] perf metric: Fix some memory leaks
@ 2020-09-05  2:19 Namhyung Kim
  2020-09-05  2:19 ` [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2 Namhyung Kim
  2020-09-06 12:41 ` [PATCH v2 1/2] perf metric: Fix some memory leaks Jiri Olsa
  0 siblings, 2 replies; 5+ messages in thread
From: Namhyung Kim @ 2020-09-05  2:19 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	Stephane Eranian, LKML, Andi Kleen, Kajol Jain, John Garry,
	Ian Rogers

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 <kjain@linux.ibm.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Ian Rogers <irogers@google.com>
Fixes: b18f3e365019d ("perf stat: Support JSON metrics in perf stat")
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/metricgroup.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 8831b964288f..af664d6218d6 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -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;
 }
 
-- 
2.28.0.526.ge36021eeef-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2
  2020-09-05  2:19 [PATCH v2 1/2] perf metric: Fix some memory leaks Namhyung Kim
@ 2020-09-05  2:19 ` Namhyung Kim
  2020-09-06 12:41   ` Jiri Olsa
  2020-09-06 12:41 ` [PATCH v2 1/2] perf metric: Fix some memory leaks Jiri Olsa
  1 sibling, 1 reply; 5+ messages in thread
From: Namhyung Kim @ 2020-09-05  2:19 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	Stephane Eranian, LKML, Andi Kleen, Kajol Jain, John Garry,
	Ian Rogers

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

Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Ian Rogers <irogers@google.com>
Fixes: 4ea2896715e67 ("perf metric: Collect referenced metrics in struct metric_expr")
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/metricgroup.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index af664d6218d6..b28c09447c10 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -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;
 			}
 
-- 
2.28.0.526.ge36021eeef-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] perf metric: Fix some memory leaks
  2020-09-05  2:19 [PATCH v2 1/2] perf metric: Fix some memory leaks Namhyung Kim
  2020-09-05  2:19 ` [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2 Namhyung Kim
@ 2020-09-06 12:41 ` Jiri Olsa
  2020-09-07  0:27   ` Namhyung Kim
  1 sibling, 1 reply; 5+ messages in thread
From: Jiri Olsa @ 2020-09-06 12:41 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Mark Rutland, Alexander Shishkin, Stephane Eranian, LKML,
	Andi Kleen, Kajol Jain, John Garry, Ian Rogers

On Sat, Sep 05, 2020 at 11:19:11AM +0900, Namhyung Kim wrote:
> 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 <kjain@linux.ibm.com>
> Cc: John Garry <john.garry@huawei.com>
> Cc: Ian Rogers <irogers@google.com>
> Fixes: b18f3e365019d ("perf stat: Support JSON metrics in perf stat")
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Acked-by: Jiri Olsa <jolsa@redhat.com>

thanks,
jirka

> ---
>  tools/perf/util/metricgroup.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 8831b964288f..af664d6218d6 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -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;
>  }
>  
> -- 
> 2.28.0.526.ge36021eeef-goog
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2
  2020-09-05  2:19 ` [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2 Namhyung Kim
@ 2020-09-06 12:41   ` Jiri Olsa
  0 siblings, 0 replies; 5+ messages in thread
From: Jiri Olsa @ 2020-09-06 12:41 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Mark Rutland, Alexander Shishkin, Stephane Eranian, LKML,
	Andi Kleen, Kajol Jain, John Garry, Ian Rogers

On Sat, Sep 05, 2020 at 11:19:12AM +0900, Namhyung Kim wrote:
> The metric_event_delete() missed to free expr->metric_events and it
> should free an expr when metric_refs allocation failed.
> 
> Cc: Kajol Jain <kjain@linux.ibm.com>
> Cc: John Garry <john.garry@huawei.com>
> Cc: Ian Rogers <irogers@google.com>
> Fixes: 4ea2896715e67 ("perf metric: Collect referenced metrics in struct metric_expr")
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Acked-by: Jiri Olsa <jolsa@redhat.com>

thanks,
jirka

> ---
>  tools/perf/util/metricgroup.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index af664d6218d6..b28c09447c10 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -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;
>  			}
>  
> -- 
> 2.28.0.526.ge36021eeef-goog
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] perf metric: Fix some memory leaks
  2020-09-06 12:41 ` [PATCH v2 1/2] perf metric: Fix some memory leaks Jiri Olsa
@ 2020-09-07  0:27   ` Namhyung Kim
  0 siblings, 0 replies; 5+ messages in thread
From: Namhyung Kim @ 2020-09-07  0:27 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Mark Rutland, Alexander Shishkin, Stephane Eranian, LKML,
	Andi Kleen, Kajol Jain, John Garry, Ian Rogers

Hi Jiri,

On Sun, Sep 6, 2020 at 9:41 PM Jiri Olsa <jolsa@redhat.com> wrote:
>
> On Sat, Sep 05, 2020 at 11:19:11AM +0900, Namhyung Kim wrote:
> > 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 <kjain@linux.ibm.com>
> > Cc: John Garry <john.garry@huawei.com>
> > Cc: Ian Rogers <irogers@google.com>
> > Fixes: b18f3e365019d ("perf stat: Support JSON metrics in perf stat")
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>
> Acked-by: Jiri Olsa <jolsa@redhat.com>

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-09-07  0:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-05  2:19 [PATCH v2 1/2] perf metric: Fix some memory leaks Namhyung Kim
2020-09-05  2:19 ` [PATCH v2 2/2] perf metric: Fix some memory leaks - part 2 Namhyung Kim
2020-09-06 12:41   ` Jiri Olsa
2020-09-06 12:41 ` [PATCH v2 1/2] perf metric: Fix some memory leaks Jiri Olsa
2020-09-07  0:27   ` Namhyung Kim

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.