LKML Archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: John Garry <john.g.garry@oracle.com>,
	Will Deacon <will@kernel.org>, James Clark <james.clark@arm.com>,
	Mike Leach <mike.leach@linaro.org>, Leo Yan <leo.yan@linaro.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	"Naveen N. Rao" <naveen.n.rao@linux.vnet.ibm.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	German Gomez <german.gomez@arm.com>,
	Ali Saidi <alisaidi@amazon.com>,
	Jing Zhang <renyu.zj@linux.alibaba.com>,
	Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	ye xingchen <ye.xingchen@zte.com.cn>,
	Liam Howlett <liam.howlett@oracle.com>,
	Dmitrii Dolgov <9erthalion6@gmail.com>,
	Yang Jihong <yangjihong1@huawei.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Changbin Du <changbin.du@huawei.com>,
	Ravi Bangoria <ravi.bangoria@amd.com>,
	Sean Christopherson <seanjc@google.com>,
	Andi Kleen <ak@linux.intel.com>,
	"Steinar H. Gunderson" <sesse@google.com>,
	Yuan Can <yuancan@huawei.com>,
	Brian Robbins <brianrob@linux.microsoft.com>,
	liuwenyu <liuwenyu7@huawei.com>,
	Ivan Babrou <ivan@cloudflare.com>,
	Fangrui Song <maskray@google.com>,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-perf-users@vger.kernel.org, coresight@lists.linaro.org
Subject: Re: [PATCH v2 26/26] perf hist: Fix srcline memory leak
Date: Mon, 12 Jun 2023 11:16:13 -0300	[thread overview]
Message-ID: <ZIcorZVu1z8jaPL/@kernel.org> (raw)
In-Reply-To: <ZIcoJytUEz4UgQYR@kernel.org>

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

-- 

- Arnaldo

  reply	other threads:[~2023-06-12 14:20 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 23:27 [PATCH v2 00/26] Fix memory leaks (was reference count checking for thread) Ian Rogers
2023-06-08 23:27 ` [PATCH v2 01/26] perf thread: Remove notion of dead threads Ian Rogers
2023-06-08 23:27 ` [PATCH v2 02/26] perf thread: Make threads rbtree non-invasive Ian Rogers
2023-06-09 14:13   ` Arnaldo Carvalho de Melo
2023-06-08 23:28 ` [PATCH v2 03/26] perf thread: Add accessor functions for thread Ian Rogers
2023-06-09 14:15   ` Arnaldo Carvalho de Melo
2023-06-09 14:50   ` Arnaldo Carvalho de Melo
2023-06-08 23:28 ` [PATCH v2 04/26] perf maps: Make delete static, always use put Ian Rogers
2023-06-09 14:17   ` Arnaldo Carvalho de Melo
2023-06-08 23:28 ` [PATCH v2 05/26] perf addr_location: Move to its own header Ian Rogers
2023-06-09 14:18   ` Arnaldo Carvalho de Melo
2023-06-08 23:28 ` [PATCH v2 06/26] perf addr_location: Add init/exit/copy functions Ian Rogers
2023-06-09 19:48   ` Arnaldo Carvalho de Melo
2023-06-08 23:28 ` [PATCH v2 07/26] perf thread: Add reference count checking Ian Rogers
2023-06-08 23:28 ` [PATCH v2 08/26] perf machine: Make delete_threads part of machine__exit Ian Rogers
2023-06-08 23:28 ` [PATCH v2 09/26] perf report: Avoid thread leak Ian Rogers
2023-06-08 23:28 ` [PATCH v2 10/26] perf header: Ensure bitmaps are freed Ian Rogers
2023-06-08 23:28 ` [PATCH v2 11/26] perf stat: Avoid evlist leak Ian Rogers
2023-06-08 23:28 ` [PATCH v2 12/26] perf intel-pt: Fix missed put and leak Ian Rogers
2023-06-08 23:28 ` [PATCH v2 13/26] perf evlist: Free stats in all evlist destruction Ian Rogers
2023-06-08 23:28 ` [PATCH v2 14/26] perf python: Avoid 2 leak sanitizer issues Ian Rogers
2023-06-08 23:28 ` [PATCH v2 15/26] perf jit: Fix two thread leaks Ian Rogers
2023-06-08 23:28 ` [PATCH v2 16/26] perf symbol-elf: Correct holding a reference Ian Rogers
2023-06-08 23:28 ` [PATCH v2 17/26] perf maps: Fix overlapping memory leak Ian Rogers
2023-06-08 23:28 ` [PATCH v2 18/26] perf machine: Fix leak of kernel dso Ian Rogers
2023-06-08 23:28 ` [PATCH v2 19/26] perf machine: Don't leak module maps Ian Rogers
2023-06-08 23:28 ` [PATCH v2 20/26] perf map/maps/thread: Changes to reference counting Ian Rogers
2023-06-08 23:28 ` [PATCH v2 21/26] perf annotate: Fix parse_objdump_line memory leak Ian Rogers
2023-06-08 23:28 ` [PATCH v2 22/26] perf top: Add exit routine for main thread Ian Rogers
2023-06-08 23:28 ` [PATCH v2 23/26] perf header: Avoid out-of-bounds read Ian Rogers
2023-06-08 23:28 ` [PATCH v2 24/26] perf callchain: Use pthread keys for tls callchain_cursor Ian Rogers
2023-06-09 19:49   ` Arnaldo Carvalho de Melo
2023-06-08 23:28 ` [PATCH v2 25/26] perf srcline: Change free_srcline to zfree_srcline Ian Rogers
2023-06-08 23:28 ` [PATCH v2 26/26] perf hist: Fix srcline memory leak Ian Rogers
2023-06-12 14:13   ` Arnaldo Carvalho de Melo
2023-06-12 14:16     ` Arnaldo Carvalho de Melo [this message]
2023-06-12 14:46       ` Ian Rogers
2023-06-12 17:23         ` Arnaldo Carvalho de Melo
2023-06-12 21:16           ` Andi Kleen
2023-06-12 21:30             ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZIcorZVu1z8jaPL/@kernel.org \
    --to=acme@kernel.org \
    --cc=9erthalion6@gmail.com \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=alisaidi@amazon.com \
    --cc=atrajeev@linux.vnet.ibm.com \
    --cc=brianrob@linux.microsoft.com \
    --cc=changbin.du@huawei.com \
    --cc=coresight@lists.linaro.org \
    --cc=german.gomez@arm.com \
    --cc=irogers@google.com \
    --cc=ivan@cloudflare.com \
    --cc=james.clark@arm.com \
    --cc=john.g.garry@oracle.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=kprateek.nayak@amd.com \
    --cc=leo.yan@linaro.org \
    --cc=liam.howlett@oracle.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=liuwenyu7@huawei.com \
    --cc=mark.rutland@arm.com \
    --cc=maskray@google.com \
    --cc=mike.leach@linaro.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=naveen.n.rao@linux.vnet.ibm.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=ravi.bangoria@amd.com \
    --cc=renyu.zj@linux.alibaba.com \
    --cc=seanjc@google.com \
    --cc=sesse@google.com \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --cc=yangjihong1@huawei.com \
    --cc=ye.xingchen@zte.com.cn \
    --cc=yuancan@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).