diff mbox series

[v2,26/26] perf hist: Fix srcline memory leak

Message ID 20230608232823.4027869-27-irogers@google.com (mailing list archive)
State New, archived
Headers show
Series Fix memory leaks (was reference count checking for thread) | expand

Commit Message

Ian Rogers June 8, 2023, 11:28 p.m. UTC
srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
case as such strdups are redundant and leak memory.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/hist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Arnaldo Carvalho de Melo June 12, 2023, 2:13 p.m. UTC | #1
Em Thu, Jun 08, 2023 at 04:28:23PM -0700, Ian Rogers escreveu:
> srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
> case as such strdups are redundant and leak memory.

The patch is ok as its what the rest of the code is doing, i.e. strcmp()
to check if a srcline is the unknown one, but how about the following
patch on top of yours?

From 5163e54c1ed3d476f6b4e7f938861039bd4eec7c Mon Sep 17 00:00:00 2001
From: Arnaldo Carvalho de Melo <acme@redhat.com>
Date: Mon, 12 Jun 2023 11:10:46 -0300
Subject: [PATCH 1/1] perf srcline: Optimize comparision against
 SRCLINE_UNKNOWN

This is a string constant that gets returned and then strcmp() around,
we can instead just do a pointer comparision.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-diff.c    | 4 ++--
 tools/perf/util/block-info.c | 4 ++--
 tools/perf/util/hist.c       | 2 +-
 tools/perf/util/map.c        | 2 +-
 tools/perf/util/sort.c       | 2 +-
 tools/perf/util/srcline.c    | 2 +-
 6 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index eec89567ae483604..e8a1b16aa5f83f4f 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -1378,8 +1378,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
 	end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
 				he->ms.sym);
 
-	if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
-	    (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
+	if (start_line != SRCLINE_UNKNOWN &&
+	    end_line != SRCLINE_UNKNOWN) {
 		scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
 			  start_line, end_line, block_he->diff.cycles);
 	} else {
diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c
index 08279b1b65e5a4b0..fe4c17248799f0a2 100644
--- a/tools/perf/util/block-info.c
+++ b/tools/perf/util/block-info.c
@@ -296,8 +296,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
 				he->ms.sym);
 
-	if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
-	    (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
+	if (start_line != SRCLINE_UNKNOWN) &&
+	    end_line != SRCLINE_UNKNOWN) {
 		scnprintf(buf, sizeof(buf), "[%s -> %s]",
 			  start_line, end_line);
 	} else {
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 0a10bcc6ec95b5e0..3dc8a4968beb9c01 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he,
 			goto err_infos;
 	}
 
-	if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
+	if (he->srcline && he->srcline != SRCLINE_UNKNOWN) {
 		he->srcline = strdup(he->srcline);
 		if (he->srcline == NULL)
 			goto err_rawdata;
diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index c77e2fce6a379e7f..f30d34903aa4eabe 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -496,7 +496,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
 
 	if (dso) {
 		char *srcline = map__srcline(map, addr, NULL);
-		if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)
+		if (srcline != SRCLINE_UNKNOWN)
 			ret = fprintf(fp, "%s%s", prefix, srcline);
 		zfree_srcline(&srcline);
 	}
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 047c3606802f5b7f..6aa1c7f2b4448b30 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -643,7 +643,7 @@ static char *hist_entry__get_srcfile(struct hist_entry *e)
 
 	sf = __get_srcline(map__dso(map), map__rip_2objdump(map, e->ip),
 			 e->ms.sym, false, true, true, e->ip);
-	if (!strcmp(sf, SRCLINE_UNKNOWN))
+	if (sf == SRCLINE_UNKNOWN)
 		return no_srcfile;
 	p = strchr(sf, ':');
 	if (p && *sf) {
diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index b8e596528d7e7e5e..48a04f42b308b080 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -809,7 +809,7 @@ void zfree_srcline(char **srcline)
 	if (*srcline == NULL)
 		return;
 
-	if (strcmp(*srcline, SRCLINE_UNKNOWN))
+	if (*srcline != SRCLINE_UNKNOWN)
 		free(*srcline);
 
 	*srcline = NULL;
Arnaldo Carvalho de Melo June 12, 2023, 2:16 p.m. UTC | #2
Em Mon, Jun 12, 2023 at 11:13:59AM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Thu, Jun 08, 2023 at 04:28:23PM -0700, Ian Rogers escreveu:
> > srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
> > case as such strdups are redundant and leak memory.
> 
> The patch is ok as its what the rest of the code is doing, i.e. strcmp()
> to check if a srcline is the unknown one, but how about the following
> patch on top of yours?

[acme@quaco perf-tools-next]$ strings ~/bin/perf | grep '??:0'
??:0
SRCLINE_UNKNOWN ((char *) "??:0")
[acme@quaco perf-tools-next]$
 
> From 5163e54c1ed3d476f6b4e7f938861039bd4eec7c Mon Sep 17 00:00:00 2001
> From: Arnaldo Carvalho de Melo <acme@redhat.com>
> Date: Mon, 12 Jun 2023 11:10:46 -0300
> Subject: [PATCH 1/1] perf srcline: Optimize comparision against
>  SRCLINE_UNKNOWN
> 
> This is a string constant that gets returned and then strcmp() around,
> we can instead just do a pointer comparision.
> 
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Link: https://lore.kernel.org/lkml/
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/builtin-diff.c    | 4 ++--
>  tools/perf/util/block-info.c | 4 ++--
>  tools/perf/util/hist.c       | 2 +-
>  tools/perf/util/map.c        | 2 +-
>  tools/perf/util/sort.c       | 2 +-
>  tools/perf/util/srcline.c    | 2 +-
>  6 files changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
> index eec89567ae483604..e8a1b16aa5f83f4f 100644
> --- a/tools/perf/builtin-diff.c
> +++ b/tools/perf/builtin-diff.c
> @@ -1378,8 +1378,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
>  	end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
>  				he->ms.sym);
>  
> -	if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
> -	    (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
> +	if (start_line != SRCLINE_UNKNOWN &&
> +	    end_line != SRCLINE_UNKNOWN) {
>  		scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
>  			  start_line, end_line, block_he->diff.cycles);
>  	} else {
> diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c
> index 08279b1b65e5a4b0..fe4c17248799f0a2 100644
> --- a/tools/perf/util/block-info.c
> +++ b/tools/perf/util/block-info.c
> @@ -296,8 +296,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
>  	end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
>  				he->ms.sym);
>  
> -	if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
> -	    (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
> +	if (start_line != SRCLINE_UNKNOWN) &&
> +	    end_line != SRCLINE_UNKNOWN) {
>  		scnprintf(buf, sizeof(buf), "[%s -> %s]",
>  			  start_line, end_line);
>  	} else {
> diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
> index 0a10bcc6ec95b5e0..3dc8a4968beb9c01 100644
> --- a/tools/perf/util/hist.c
> +++ b/tools/perf/util/hist.c
> @@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he,
>  			goto err_infos;
>  	}
>  
> -	if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
> +	if (he->srcline && he->srcline != SRCLINE_UNKNOWN) {
>  		he->srcline = strdup(he->srcline);
>  		if (he->srcline == NULL)
>  			goto err_rawdata;
> diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
> index c77e2fce6a379e7f..f30d34903aa4eabe 100644
> --- a/tools/perf/util/map.c
> +++ b/tools/perf/util/map.c
> @@ -496,7 +496,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
>  
>  	if (dso) {
>  		char *srcline = map__srcline(map, addr, NULL);
> -		if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)
> +		if (srcline != SRCLINE_UNKNOWN)
>  			ret = fprintf(fp, "%s%s", prefix, srcline);
>  		zfree_srcline(&srcline);
>  	}
> diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
> index 047c3606802f5b7f..6aa1c7f2b4448b30 100644
> --- a/tools/perf/util/sort.c
> +++ b/tools/perf/util/sort.c
> @@ -643,7 +643,7 @@ static char *hist_entry__get_srcfile(struct hist_entry *e)
>  
>  	sf = __get_srcline(map__dso(map), map__rip_2objdump(map, e->ip),
>  			 e->ms.sym, false, true, true, e->ip);
> -	if (!strcmp(sf, SRCLINE_UNKNOWN))
> +	if (sf == SRCLINE_UNKNOWN)
>  		return no_srcfile;
>  	p = strchr(sf, ':');
>  	if (p && *sf) {
> diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
> index b8e596528d7e7e5e..48a04f42b308b080 100644
> --- a/tools/perf/util/srcline.c
> +++ b/tools/perf/util/srcline.c
> @@ -809,7 +809,7 @@ void zfree_srcline(char **srcline)
>  	if (*srcline == NULL)
>  		return;
>  
> -	if (strcmp(*srcline, SRCLINE_UNKNOWN))
> +	if (*srcline != SRCLINE_UNKNOWN)
>  		free(*srcline);
>  
>  	*srcline = NULL;
> -- 
> 2.39.2
> 
>  
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/util/hist.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
> > index 77cb2cc83bb9..cc6f7f51faa5 100644
> > --- a/tools/perf/util/hist.c
> > +++ b/tools/perf/util/hist.c
> > @@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he,
> >  			goto err_infos;
> >  	}
> >  
> > -	if (he->srcline) {
> > +	if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
> >  		he->srcline = strdup(he->srcline);
> >  		if (he->srcline == NULL)
> >  			goto err_rawdata;
> > -- 
> > 2.41.0.162.gfafddb0af9-goog
> > 
> 
> -- 
> 
> - Arnaldo
Ian Rogers June 12, 2023, 2:46 p.m. UTC | #3
On Mon, Jun 12, 2023 at 7:16 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Mon, Jun 12, 2023 at 11:13:59AM -0300, Arnaldo Carvalho de Melo escreveu:
> > Em Thu, Jun 08, 2023 at 04:28:23PM -0700, Ian Rogers escreveu:
> > > srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
> > > case as such strdups are redundant and leak memory.
> >
> > The patch is ok as its what the rest of the code is doing, i.e. strcmp()
> > to check if a srcline is the unknown one, but how about the following
> > patch on top of yours?
>
> [acme@quaco perf-tools-next]$ strings ~/bin/perf | grep '??:0'
> ??:0
> SRCLINE_UNKNOWN ((char *) "??:0")
> [acme@quaco perf-tools-next]$

Agreed, the strcmps make me nervous as they won't distinguish heap
from a global meaning we could end up with things like pointers to
freed memory. The comparison with the global is always going to be
same imo.

Acked-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> > From 5163e54c1ed3d476f6b4e7f938861039bd4eec7c Mon Sep 17 00:00:00 2001
> > From: Arnaldo Carvalho de Melo <acme@redhat.com>
> > Date: Mon, 12 Jun 2023 11:10:46 -0300
> > Subject: [PATCH 1/1] perf srcline: Optimize comparision against
> >  SRCLINE_UNKNOWN
> >
> > This is a string constant that gets returned and then strcmp() around,
> > we can instead just do a pointer comparision.
> >
> > Cc: Adrian Hunter <adrian.hunter@intel.com>
> > Cc: Ian Rogers <irogers@google.com>
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > Cc: Namhyung Kim <namhyung@kernel.org>
> > Link: https://lore.kernel.org/lkml/
> > Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> > ---
> >  tools/perf/builtin-diff.c    | 4 ++--
> >  tools/perf/util/block-info.c | 4 ++--
> >  tools/perf/util/hist.c       | 2 +-
> >  tools/perf/util/map.c        | 2 +-
> >  tools/perf/util/sort.c       | 2 +-
> >  tools/perf/util/srcline.c    | 2 +-
> >  6 files changed, 8 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
> > index eec89567ae483604..e8a1b16aa5f83f4f 100644
> > --- a/tools/perf/builtin-diff.c
> > +++ b/tools/perf/builtin-diff.c
> > @@ -1378,8 +1378,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
> >       end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
> >                               he->ms.sym);
> >
> > -     if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
> > -         (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
> > +     if (start_line != SRCLINE_UNKNOWN &&
> > +         end_line != SRCLINE_UNKNOWN) {
> >               scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
> >                         start_line, end_line, block_he->diff.cycles);
> >       } else {
> > diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c
> > index 08279b1b65e5a4b0..fe4c17248799f0a2 100644
> > --- a/tools/perf/util/block-info.c
> > +++ b/tools/perf/util/block-info.c
> > @@ -296,8 +296,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
> >       end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
> >                               he->ms.sym);
> >
> > -     if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
> > -         (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
> > +     if (start_line != SRCLINE_UNKNOWN) &&
> > +         end_line != SRCLINE_UNKNOWN) {
> >               scnprintf(buf, sizeof(buf), "[%s -> %s]",
> >                         start_line, end_line);
> >       } else {
> > diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
> > index 0a10bcc6ec95b5e0..3dc8a4968beb9c01 100644
> > --- a/tools/perf/util/hist.c
> > +++ b/tools/perf/util/hist.c
> > @@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he,
> >                       goto err_infos;
> >       }
> >
> > -     if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
> > +     if (he->srcline && he->srcline != SRCLINE_UNKNOWN) {
> >               he->srcline = strdup(he->srcline);
> >               if (he->srcline == NULL)
> >                       goto err_rawdata;
> > diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
> > index c77e2fce6a379e7f..f30d34903aa4eabe 100644
> > --- a/tools/perf/util/map.c
> > +++ b/tools/perf/util/map.c
> > @@ -496,7 +496,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
> >
> >       if (dso) {
> >               char *srcline = map__srcline(map, addr, NULL);
> > -             if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)
> > +             if (srcline != SRCLINE_UNKNOWN)
> >                       ret = fprintf(fp, "%s%s", prefix, srcline);
> >               zfree_srcline(&srcline);
> >       }
> > diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
> > index 047c3606802f5b7f..6aa1c7f2b4448b30 100644
> > --- a/tools/perf/util/sort.c
> > +++ b/tools/perf/util/sort.c
> > @@ -643,7 +643,7 @@ static char *hist_entry__get_srcfile(struct hist_entry *e)
> >
> >       sf = __get_srcline(map__dso(map), map__rip_2objdump(map, e->ip),
> >                        e->ms.sym, false, true, true, e->ip);
> > -     if (!strcmp(sf, SRCLINE_UNKNOWN))
> > +     if (sf == SRCLINE_UNKNOWN)
> >               return no_srcfile;
> >       p = strchr(sf, ':');
> >       if (p && *sf) {
> > diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
> > index b8e596528d7e7e5e..48a04f42b308b080 100644
> > --- a/tools/perf/util/srcline.c
> > +++ b/tools/perf/util/srcline.c
> > @@ -809,7 +809,7 @@ void zfree_srcline(char **srcline)
> >       if (*srcline == NULL)
> >               return;
> >
> > -     if (strcmp(*srcline, SRCLINE_UNKNOWN))
> > +     if (*srcline != SRCLINE_UNKNOWN)
> >               free(*srcline);
> >
> >       *srcline = NULL;
> > --
> > 2.39.2
> >
> >
> > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > ---
> > >  tools/perf/util/hist.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
> > > index 77cb2cc83bb9..cc6f7f51faa5 100644
> > > --- a/tools/perf/util/hist.c
> > > +++ b/tools/perf/util/hist.c
> > > @@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he,
> > >                     goto err_infos;
> > >     }
> > >
> > > -   if (he->srcline) {
> > > +   if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
> > >             he->srcline = strdup(he->srcline);
> > >             if (he->srcline == NULL)
> > >                     goto err_rawdata;
> > > --
> > > 2.41.0.162.gfafddb0af9-goog
> > >
> >
> > --
> >
> > - Arnaldo
>
> --
>
> - Arnaldo
Arnaldo Carvalho de Melo June 12, 2023, 5:23 p.m. UTC | #4
Em Mon, Jun 12, 2023 at 07:46:14AM -0700, Ian Rogers escreveu:
> On Mon, Jun 12, 2023 at 7:16 AM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > Em Mon, Jun 12, 2023 at 11:13:59AM -0300, Arnaldo Carvalho de Melo escreveu:
> > > Em Thu, Jun 08, 2023 at 04:28:23PM -0700, Ian Rogers escreveu:
> > > > srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
> > > > case as such strdups are redundant and leak memory.
> > >
> > > The patch is ok as its what the rest of the code is doing, i.e. strcmp()
> > > to check if a srcline is the unknown one, but how about the following
> > > patch on top of yours?
> >
> > [acme@quaco perf-tools-next]$ strings ~/bin/perf | grep '??:0'
> > ??:0
> > SRCLINE_UNKNOWN ((char *) "??:0")
> > [acme@quaco perf-tools-next]$
> 
> Agreed, the strcmps make me nervous as they won't distinguish heap
> from a global meaning we could end up with things like pointers to
> freed memory. The comparison with the global is always going to be
> same imo.
> 
> Acked-by: Ian Rogers <irogers@google.com>

Thanks, applied and added your Acked-by.

- Arnaldo
Andi Kleen June 12, 2023, 9:16 p.m. UTC | #5
On Mon, Jun 12, 2023 at 02:23:41PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Mon, Jun 12, 2023 at 07:46:14AM -0700, Ian Rogers escreveu:
> > On Mon, Jun 12, 2023 at 7:16 AM Arnaldo Carvalho de Melo
> > <acme@kernel.org> wrote:
> > >
> > > Em Mon, Jun 12, 2023 at 11:13:59AM -0300, Arnaldo Carvalho de Melo escreveu:
> > > > Em Thu, Jun 08, 2023 at 04:28:23PM -0700, Ian Rogers escreveu:
> > > > > srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
> > > > > case as such strdups are redundant and leak memory.
> > > >
> > > > The patch is ok as its what the rest of the code is doing, i.e. strcmp()
> > > > to check if a srcline is the unknown one, but how about the following
> > > > patch on top of yours?
> > >
> > > [acme@quaco perf-tools-next]$ strings ~/bin/perf | grep '??:0'
> > > ??:0
> > > SRCLINE_UNKNOWN ((char *) "??:0")
> > > [acme@quaco perf-tools-next]$
> > 
> > Agreed, the strcmps make me nervous as they won't distinguish heap
> > from a global meaning we could end up with things like pointers to
> > freed memory. The comparison with the global is always going to be
> > same imo.
> > 
> > Acked-by: Ian Rogers <irogers@google.com>
> 
> Thanks, applied and added your Acked-by.

Actually was there another patch that turned it into a explicit global? 

At least in my tree it isn't:

util/srcline.h
28:#define SRCLINE_UNKNOWN  ((char *) "??:0")

Without any explicit global it's a bit dangerous because you rely on the
linker doing string deduplication.  While it normally does that there might be
odd corner cases where it doesn't.

-Andi
Arnaldo Carvalho de Melo June 12, 2023, 9:30 p.m. UTC | #6
Em Mon, Jun 12, 2023 at 02:16:34PM -0700, Andi Kleen escreveu:
> On Mon, Jun 12, 2023 at 02:23:41PM -0300, Arnaldo Carvalho de Melo wrote:
> > Em Mon, Jun 12, 2023 at 07:46:14AM -0700, Ian Rogers escreveu:
> > > On Mon, Jun 12, 2023 at 7:16 AM Arnaldo Carvalho de Melo
> > > <acme@kernel.org> wrote:
> > > >
> > > > Em Mon, Jun 12, 2023 at 11:13:59AM -0300, Arnaldo Carvalho de Melo escreveu:
> > > > > Em Thu, Jun 08, 2023 at 04:28:23PM -0700, Ian Rogers escreveu:
> > > > > > srcline isn't freed if it is SRCLINE_UNKNOWN. Avoid strduping in this
> > > > > > case as such strdups are redundant and leak memory.
> > > > >
> > > > > The patch is ok as its what the rest of the code is doing, i.e. strcmp()
> > > > > to check if a srcline is the unknown one, but how about the following
> > > > > patch on top of yours?
> > > >
> > > > [acme@quaco perf-tools-next]$ strings ~/bin/perf | grep '??:0'
> > > > ??:0
> > > > SRCLINE_UNKNOWN ((char *) "??:0")
> > > > [acme@quaco perf-tools-next]$
> > > 
> > > Agreed, the strcmps make me nervous as they won't distinguish heap
> > > from a global meaning we could end up with things like pointers to
> > > freed memory. The comparison with the global is always going to be
> > > same imo.
> > > 
> > > Acked-by: Ian Rogers <irogers@google.com>
> > 
> > Thanks, applied and added your Acked-by.
> 
> Actually was there another patch that turned it into a explicit global? 
> 
> At least in my tree it isn't:
> 
> util/srcline.h
> 28:#define SRCLINE_UNKNOWN  ((char *) "??:0")
> 
> Without any explicit global it's a bit dangerous because you rely on the
> linker doing string deduplication.  While it normally does that there might be
> odd corner cases where it doesn't.

Yeah, several gcc and clang versions complained with:

2    65.38 almalinux:9                   : FAIL clang version 15.0.7 (Red Hat 15.0.7-2.el9)
    builtin-diff.c:1381:17: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
            if (start_line != SRCLINE_UNKNOWN &&

So I added the following on top:

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index 48a04f42b308b080..aec596a0b0bbec0f 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -23,6 +23,8 @@
 
 bool srcline_full_filename;
 
+char *srcline__unknown = (char *)"??:0";
+
 static const char *dso__name(struct dso *dso)
 {
 	const char *dso_name;
diff --git a/tools/perf/util/srcline.h b/tools/perf/util/srcline.h
index a15c7db9058ece96..167645bcff0755e2 100644
--- a/tools/perf/util/srcline.h
+++ b/tools/perf/util/srcline.h
@@ -25,7 +25,8 @@ char *srcline__tree_find(struct rb_root_cached *tree, u64 addr);
 /* delete all srclines within the tree */
 void srcline__tree_delete(struct rb_root_cached *tree);
 
-#define SRCLINE_UNKNOWN  ((char *) "??:0")
+extern char *srcline__unknown;
+#define SRCLINE_UNKNOWN srcline__unknown
 
 struct inline_list {
 	struct symbol		*symbol;
diff mbox series

Patch

diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 77cb2cc83bb9..cc6f7f51faa5 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -484,7 +484,7 @@  static int hist_entry__init(struct hist_entry *he,
 			goto err_infos;
 	}
 
-	if (he->srcline) {
+	if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
 		he->srcline = strdup(he->srcline);
 		if (he->srcline == NULL)
 			goto err_rawdata;