All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Riccardo Mancini <rickyman7@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Riccardo Mancini <rickyman7@gmail.com>,
	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@redhat.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Andi Kleen <ak@linux.intel.com>,
	Tommi Rantala <tommi.t.rantala@nokia.com>,
	Jiapeng Chong <jiapeng.chong@linux.alibaba.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] perf ksymbol: fix memory leak: decrease refcount of map and dso
Date: Sat, 12 Jun 2021 19:37:48 +0200	[thread overview]
Message-ID: <20210612173751.188582-1-rickyman7@gmail.com> (raw)
In-Reply-To: <YLpxDf6+YOxYI5z3@kernel.org>

ASan reported a memory leak of BPF-related ksymbols
map and dso. The leak is caused by refount never
reaching 0, due to missing __put calls in the function
machine__process_ksymbol_register.
Once the dso is inserted in the map, dso__put should be
called (map__new2 increases the refcount to 2).
The same thing applies for the map when it's inserted
into maps (maps__insert increases the refcount to 2).

$ sudo ./perf record -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]

=================================================================
==297735==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 6992 byte(s) in 19 object(s) allocated from:
    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8e4e53 in map__new2 /home/user/linux/tools/perf/util/map.c:216:20
    #2 0x8cf68c in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:778:10
    [...]

Indirect leak of 8702 byte(s) in 19 object(s) allocated from:
    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x8728d7 in dso__new_id /home/user/linux/tools/perf/util/dso.c:1256:20
    #2 0x872015 in dso__new /home/user/linux/tools/perf/util/dso.c:1295:9
    #3 0x8cf623 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:774:21
    [...]

Indirect leak of 1520 byte(s) in 19 object(s) allocated from:
    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x888954 in map__process_kallsym_symbol /home/user/linux/tools/perf/util/symbol.c:710:8
    [...]

Indirect leak of 1406 byte(s) in 19 object(s) allocated from:
    #0 0x4f43c7 in calloc (/home/user/linux/tools/perf/perf+0x4f43c7)
    #1 0x87b3da in symbol__new /home/user/linux/tools/perf/util/symbol.c:269:23
    #2 0x8cfbd8 in machine__process_ksymbol_register /home/user/linux/tools/perf/util/machine.c:803:8
    [...]

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 3ff4936a15a4..da19be7da284 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -776,10 +776,10 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		if (dso) {
 			dso->kernel = DSO_SPACE__KERNEL;
 			map = map__new2(0, dso);
+			dso__put(dso);
 		}
 
 		if (!dso || !map) {
-			dso__put(dso);
 			return -ENOMEM;
 		}
 
@@ -792,6 +792,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		map->start = event->ksymbol.addr;
 		map->end = map->start + event->ksymbol.len;
 		maps__insert(&machine->kmaps, map);
+		map__put(map);
 		dso__set_loaded(dso);
 
 		if (is_bpf_image(event->ksymbol.name)) {
-- 
2.23.0


  reply	other threads:[~2021-06-12 17:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-02 23:10 [PATCH] perf ksymbol: fix memory leak: decrease refcount of map and dso Riccardo Mancini
2021-06-04  4:26 ` Ian Rogers
2021-06-04 13:22   ` Arnaldo Carvalho de Melo
2021-06-04 15:16     ` Riccardo Mancini
2021-06-04 18:29       ` Arnaldo Carvalho de Melo
2021-06-12 17:37         ` Riccardo Mancini [this message]
2021-06-16 18:12           ` [PATCH v2] " Arnaldo Carvalho de Melo
2021-06-18 10:01         ` [PATCH] " Riccardo Mancini
2021-06-18 13:25           ` 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=20210612173751.188582-1-rickyman7@gmail.com \
    --to=rickyman7@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jiapeng.chong@linux.alibaba.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tommi.t.rantala@nokia.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 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.