From 71469a97f9b50ec8dcd23ddfe6bf492d996ae338 Mon Sep 17 00:00:00 2001
From: Namhyung Kim <namhyung@kernel.org>
Date: Tue, 15 Sep 2020 12:18:11 +0900
Subject: [PATCH 137/201] perf evlist: Fix cpu/thread map leak

mainline inclusion
from mainline-v5.9-rc6
commit bfd1b83d75e44a9f65de30accb3dd3b5940bd3ac
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I8C0CX

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=bfd1b83d75e44a9f65de30accb3dd3b5940bd3ac

----------------------------------------------------------------------

Asan reported leak of cpu and thread maps as they have one more refcount
than released.  I found that after setting evlist maps it should release
it's refcount.

It seems to be broken from the beginning so I chose the original commit
as the culprit.  But not sure how it's applied to stable trees since
there are many changes in the code after that.

Fixes: 7e2ed097538c5 ("perf evlist: Store pointer to the cpu and thread maps")
Fixes: 4112eb1899c0e ("perf evlist: Default to syswide target when no thread/cpu maps set")
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20200915031819.386559-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: hongrongxuan <hongrongxuan@huawei.com>

  Conflicts:
	tools/perf/util/evlist.c
---
 tools/perf/util/evlist.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 18c61f77cc2c..bd791f9af49b 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1123,6 +1123,10 @@ int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
 
 	perf_evlist__set_maps(evlist, cpus, threads);
 
+	/* as evlist now has references, put count here */
+	cpu_map__put(cpus);
+	thread_map__put(threads);
+
 	return 0;
 
 out_delete_threads:
@@ -1406,11 +1410,12 @@ static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
 		goto out_put;
 
 	perf_evlist__set_maps(evlist, cpus, threads);
-out:
-	return err;
+
+	thread_map__put(threads);
 out_put:
 	cpu_map__put(cpus);
-	goto out;
+out:
+	return err;
 }
 
 int perf_evlist__open(struct perf_evlist *evlist)
-- 
2.27.0

