perf: Fix memory errors
cover perf: Fix memory errors
Fix several memory errors in perf tool.
Hongbo Yao (1):
perf metrictroup: Fix memory leak of metric_events
Li Bin (2):
perf svghelper: Fix memory leak in svg_build_topology_map perf util: Fix potential segment fault in put_tracepoints_path
Xie XiuQi (1):
perf util: Fix memory leak of prefix_if_not_in
tools/perf/util/metricgroup.c | 3 +++ tools/perf/util/sort.c | 2 +- tools/perf/util/svghelper.c | 10 +++++++--- tools/perf/util/trace-event-info.c | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/4 perf metrictroup: Fix memory leak of metric_events
From: Hongbo Yao <[email protected]>
Fix memory leak of metric_events in function metricgroup__setup_events()
--- tools/perf/util/metricgroup.c | 3 +++ 1 file changed, 3 insertions(+)
tools/perf/util/metricgroup.c +3 -0
@@ -184,6 +184,7 @@ static int metricgroup__setup_events(struct list_head *groups,if (!evsel) {pr_debug ("Cannot resolve %s: %s\n" , eg->metric_name, eg->metric_expr); +free (metric_events);continue ; }for (i =0 ; i < eg->idnum; i++)
@@ -191,11 +192,13 @@ static int metricgroup__setup_events(struct list_head *groups, me =metricgroup__lookup (metric_events_list, evsel, true);if (!me) { ret = -ENOMEM ; +free (metric_events);break ; } expr =malloc (sizeof (struct metric_expr ));if (!expr) { ret = -ENOMEM ; +free (metric_events);break ; } expr->metric_expr = eg->metric_expr;
Signature · 1 line
2.17.1
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 1/4] perf metrictroup: Fix memory leak of metric_events
Em Thu, May 21, 2020 at 09:32:15PM +0800, Wei Li escreveu:
Quoted 3 lines
From: Hongbo Yao <[email protected]>
Fix memory leak of
metric_eventsin functionmetricgroup__setup_events()
The leak is there but this code has changed a lot recently, please take a look at my perf/core branch and keep Ian Rogers in the loop,
- Arnaldo
Quoted 34 lines
Signed-off-by Hongbo Yao <[email protected]>--- tools/perf/util/metricgroup.c | 3 +++ 1 file changed, 3 insertions(+)tools/perf/util/metricgroup.c +3
-0@@ -184,6 +184,7 @@ static int metricgroup__setup_events(struct list_head *groups,if (!evsel) {pr_debug ("Cannot resolve %s: %s\n" , eg->metric_name, eg->metric_expr); +free (metric_events);continue ; }for (i =0 ; i < eg->idnum; i++)@@ -191,11 +192,13 @@ static int metricgroup__setup_events(struct list_head *groups, me =metricgroup__lookup (metric_events_list, evsel, true);if (!me) { ret = -ENOMEM ; +free (metric_events);break ; } expr =malloc (sizeof (struct metric_expr ));if (!expr) { ret = -ENOMEM ; +free (metric_events);break ; } expr->metric_expr = eg->metric_expr;Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
Ian Rogers
reviewerRe: [PATCH 1/4] perf metrictroup: Fix memory leak of metric_events
Quoted 10 lines · Arnaldo Carvalho de Melo
Em Thu, May 21, 2020 at 09:32:15PM +0800, Wei Li escreveu:
Quoted 3 lines
From: Hongbo Yao <[email protected]>
Fix memory leak of
metric_eventsin functionmetricgroup__setup_events()The leak is there but this code has changed a lot recently, please take a look at my perf/core branch and keep Ian Rogers in the loop,
- Arnaldo
Thanks, I'd posted the same change here:
https://lore.kernel.org/lkml/[email protected]/
but it looks like it was mislaid. Arnaldo, I can re-post that patch or Acked-by this patch. I'm easy either way.
Thanks,
Ian
Quoted 38 lines
Quoted 32 lines
Signed-off-by Hongbo Yao <[email protected]>--- tools/perf/util/metricgroup.c | 3 +++ 1 file changed, 3 insertions(+)tools/perf/util/metricgroup.c +3
-1@@ -184,6 +184,7 @@ static int metricgroup__setup_events(struct list_head *groups,if (!evsel) {pr_debug ("Cannot resolve %s: %s\n" , eg->metric_name, eg->metric_expr); +free (metric_events);continue ; }for (i =0 ; i < eg->idnum; i++)@@ -191,11 +192,13 @@ static int metricgroup__setup_events(struct list_head *groups, me =metricgroup__lookup (metric_events_list, evsel, true);if (!me) { ret = -ENOMEM ; +free (metric_events);break ; } expr =malloc (sizeof (struct metric_expr ));if (!expr) { ret = -ENOMEM ; +free (metric_events);break ; } expr->metric_expr = eg->metric_expr; --Quoted 2 lines
2.17.1
--
- Arnaldo
PATCH 2/4 perf svghelper: Fix memory leak in svg_build_topology_map
In reply to Wei LiFrom: Li Bin <[email protected]>
Fix leak of memory pointed to by t.sib_thr and t.sib_core in svg_build_topology_map.
--- tools/perf/util/svghelper.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/svghelper.c +7 -3
@@ -754,6 +754,7 @@ int svg_build_topology_map(struct perf_env *env)int i, nr_cpus;struct topology t;char *sib_core, *sib_thr; +int ret; nr_cpus =min (env->nr_cpus_online,MAX_NR_CPUS );
@@ -767,12 +768,14 @@ int svg_build_topology_map(struct perf_env *env)if (!t.sib_core || !t.sib_thr) {fprintf (stderr,"topology: no memory\n" ); + ret =-1 ; goto exit; }for (i =0 ; i < env->nr_sibling_cores; i++) {if (str_to_bitmap (sib_core, &t.sib_core[i], nr_cpus)) {fprintf (stderr,"topology: can't parse siblings map\n" ); + ret =-1 ; goto exit; }
@@ -782,6 +785,7 @@ int svg_build_topology_map(struct perf_env *env)for (i =0 ; i < env->nr_sibling_threads; i++) {if (str_to_bitmap (sib_thr, &t.sib_thr[i], nr_cpus)) {fprintf (stderr,"topology: can't parse siblings map\n" ); + ret =-1 ; goto exit; }
@@ -791,6 +795,7 @@ int svg_build_topology_map(struct perf_env *env) topology_map =malloc (sizeof (int ) * nr_cpus);if (!topology_map) {fprintf (stderr,"topology: no memory\n" ); + ret =-1 ; goto exit; }
@@ -798,12 +803,11 @@ int svg_build_topology_map(struct perf_env *env) topology_map[i] =-1 ;scan_core_topology (topology_map, &t, nr_cpus); - -return 0 ; + ret =0 ; exit:zfree (&t.sib_core);zfree (&t.sib_thr); -return -1 ; +return ret; }
Signature · 1 line
2.17.1
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 2/4] perf svghelper: Fix memory leak in svg_build_topology_map
Em Thu, May 21, 2020 at 09:32:16PM +0800, Wei Li escreveu:
Quoted 15 lines
From: Li Bin <[email protected]>
Fix leak of memory pointed to by t.sib_thr and t.sib_core in
svg_build_topology_map.Signed-off-by Li Bin <[email protected]>--- tools/perf/util/svghelper.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)tools/perf/util/svghelper.c +0
-0
tools/perf/util/svghelper.c int svg_build_topology_map(struct perf_env *env) · line 757
Quoted 4 lines
tools/perf/util/svghelper.c +1
-0@@ -754,6 +754,7 @@ int svg_build_topology_map(struct perf_env *env)int i, nr_cpus;struct topology t;char *sib_core, *sib_thr; +int ret;
Please set ret to -1 here
int ret = -1;
So that you don't have to add all those lines below...
Quoted 35 lines
tools/perf/util/svghelper.c +4
-0nr_cpus =min (env->nr_cpus_online,MAX_NR_CPUS );@@ -767,12 +768,14 @@ int svg_build_topology_map(struct perf_env *env)if (!t.sib_core || !t.sib_thr) {fprintf (stderr,"topology: no memory\n" ); + ret =-1 ; goto exit; }for (i =0 ; i < env->nr_sibling_cores; i++) {if (str_to_bitmap (sib_core, &t.sib_core[i], nr_cpus)) {fprintf (stderr,"topology: can't parse siblings map\n" ); + ret =-1 ; goto exit; }@@ -782,6 +785,7 @@ int svg_build_topology_map(struct perf_env *env)for (i =0 ; i < env->nr_sibling_threads; i++) {if (str_to_bitmap (sib_thr, &t.sib_thr[i], nr_cpus)) {fprintf (stderr,"topology: can't parse siblings map\n" ); + ret =-1 ; goto exit; }@@ -791,6 +795,7 @@ int svg_build_topology_map(struct perf_env *env) topology_map =malloc (sizeof (int ) * nr_cpus);if (!topology_map) {fprintf (stderr,"topology: no memory\n" ); + ret =-1 ; goto exit; }
tools/perf/util/svghelper.c int svg_build_topology_map(struct perf_env *env) · line 802
Quoted 5 lines
tools/perf/util/svghelper.c +0
-2@@ -798,12 +803,11 @@ int svg_build_topology_map(struct perf_env *env) topology_map[i] =-1 ;scan_core_topology (topology_map, &t, nr_cpus); - -return 0 ;
... as you'll set it to zero here :-)
Quoted 12 lines
tools/perf/util/svghelper.c +2
-1+ ret =0 ; exit:zfree (&t.sib_core);zfree (&t.sib_thr); -return -1 ; +return ret; }Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
Re: [PATCH 2/4] perf svghelper: Fix memory leak in svg_build_topology_map
Quoted 60 lines · Arnaldo Carvalho de Melo
Em Thu, May 21, 2020 at 09:32:16PM +0800, Wei Li escreveu:
Quoted 15 lines
From: Li Bin <[email protected]>
Fix leak of memory pointed to by t.sib_thr and t.sib_core in
svg_build_topology_map.Signed-off-by Li Bin <[email protected]>--- tools/perf/util/svghelper.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)tools/perf/util/svghelper.c +0
-0tools/perf/util/svghelper.c int svg_build_topology_map(struct perf_env *env) · line 757
Quoted 4 lines
tools/perf/util/svghelper.c +1
-0@@ -754,6 +754,7 @@ int svg_build_topology_map(struct perf_env *env)int i, nr_cpus;struct topology t;char *sib_core, *sib_thr; +int ret;Please set ret to -1 here
int ret = -1;So that you don't have to add all those lines below...
Quoted 34 lines
tools/perf/util/svghelper.c +4
-0nr_cpus =min (env->nr_cpus_online,MAX_NR_CPUS );@@ -767,12 +768,14 @@ int svg_build_topology_map(struct perf_env *env)if (!t.sib_core || !t.sib_thr) {fprintf (stderr,"topology: no memory\n" ); + ret =-1 ; goto exit; }for (i =0 ; i < env->nr_sibling_cores; i++) {if (str_to_bitmap (sib_core, &t.sib_core[i], nr_cpus)) {fprintf (stderr,"topology: can't parse siblings map\n" ); + ret =-1 ; goto exit; }@@ -782,6 +785,7 @@ int svg_build_topology_map(struct perf_env *env)for (i =0 ; i < env->nr_sibling_threads; i++) {if (str_to_bitmap (sib_thr, &t.sib_thr[i], nr_cpus)) {fprintf (stderr,"topology: can't parse siblings map\n" ); + ret =-1 ; goto exit; }@@ -791,6 +795,7 @@ int svg_build_topology_map(struct perf_env *env) topology_map =malloc (sizeof (int ) * nr_cpus);if (!topology_map) {fprintf (stderr,"topology: no memory\n" ); + ret =-1 ; goto exit; }
Quoted 7 lines
tools/perf/util/svghelper.c int svg_build_topology_map(struct perf_env *env) · line 802
Quoted 6 lines
tools/perf/util/svghelper.c +0
-2@@ -798,12 +803,11 @@ int svg_build_topology_map(struct perf_env *env) topology_map[i] =-1 ;scan_core_topology (topology_map, &t, nr_cpus); - -return 0 ;... as you'll set it to zero here :-)
Thank you for your comments. I will fix it.
Thanks,
Li Bin
Quoted 12 lines
Quoted 12 lines
tools/perf/util/svghelper.c +2
-1+ ret =0 ; exit:zfree (&t.sib_core);zfree (&t.sib_thr); -return -1 ; +return ret; }Signature · 1 line
2.17.1
PATCH 3/4 perf util: Fix memory leak of prefix_if_not_in
In reply to Wei LiFrom: Xie XiuQi <[email protected]>
Need to free "str" before return when asprintf() failed to avoid memory leak.
--- tools/perf/util/sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/sort.c +1 -1
@@ -2811,7 +2811,7 @@ static char *prefix_if_not_in(const char *pre, char *str)return str;if (asprintf (&n,"%s,%s" , pre, str) <0 ) -return NULL ; + n =NULL ;free (str);return n;
Signature · 1 line
2.17.1
Re: [PATCH 3/4] perf util: Fix memory leak of prefix_if_not_in
Em Thu, May 21, 2020 at 09:32:17PM +0800, Wei Li escreveu:
Quoted 4 lines
From: Xie XiuQi <[email protected]>
Need to free "str" before return when
asprintf()failed to avoid memory leak.
Thanks, applied.
- Arnaldo
Quoted 21 lines
Signed-off-by Xie XiuQi <[email protected]>--- tools/perf/util/sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/sort.c +1
-1@@ -2811,7 +2811,7 @@ static char *prefix_if_not_in(const char *pre, char *str)return str;if (asprintf (&n,"%s,%s" , pre, str) <0 ) -return NULL ; + n =NULL ;free (str);return n;Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
PATCH 4/4 perf util: Fix potential segment fault in put_tracepoints_path
In reply to Wei LiFrom: Li Bin <[email protected]>
This patch fix potential segment fault triggerd in put_tracepoints_path when the address of the local variable 'path' be freed in error path of record_saved_cmdline.
--- tools/perf/util/trace-event-info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/trace-event-info.c +1 -1
@@ -428,7 +428,7 @@ get_tracepoints_path(struct list_head *pattrs)if (!ppath->next) { error:pr_debug ("No memory to alloc tracepoints list\n" ); -put_tracepoints_path (&path); +put_tracepoints_path (path.next);return NULL ; } next:
Signature · 1 line
2.17.1