linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHSET 00/11] perf test: Fix cpu/thread map leaks
@ 2021-03-01 14:03 Namhyung Kim
  2021-03-01 14:03 ` [PATCH 01/11] perf test: Fix cpu and thread map leaks in basic mmap test Namhyung Kim
                   ` (10 more replies)
  0 siblings, 11 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:03 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

Hi,

This patchset fixes memory leaks in the perf test code.  In my company
setup, it runs daily with various sanitizers on, so I want to reduce
the failures due to the leaks not the logic.

This time I've focused on the cpu and thread maps as they are obvious
and easy to fix.  I'll take a look at the rest failures.

I didn't add the Fixes: tags since most changes seem to predate the
libperf change.  I'm not sure if I could just add the original commit
hash as this fix is meaningful only if Asan is enabled..  I'm afraid
the stable tree maintainers will see patches not applied cleanly.  But
I can add them if you want, so please let me know.

It's also available at perf/asan-fix-v1 branch in

  git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung


Namhyung Kim (11):
  perf test: Fix cpu and thread map leaks in basic mmap test
  perf test: Fix a memory leak in attr test
  perf test: Fix cpu and thread map leaks in task_exit test
  perf test: Fix cpu and thread map leaks in sw_clock_freq test
  perf test: Fix cpu and thread map leaks in code_reading test
  perf test: Fix cpu and thread map leaks in keep_tracking test
  perf test: Fix cpu and thread map leaks in switch_tracking test
  perf test: Fix a thread map leak in thread_map_synthesize test
  perf test: Fix a memory leak in thread_map_remove test
  perf test: Fix cpu map leaks in cpu_map_print test
  perf test: Fix cpu and thread map leaks in perf_time_to_tsc test

 tools/perf/tests/attr.c             |  8 +++++++-
 tools/perf/tests/code-reading.c     | 10 +++-------
 tools/perf/tests/cpumap.c           |  2 ++
 tools/perf/tests/keep-tracking.c    |  5 ++---
 tools/perf/tests/mmap-basic.c       |  2 --
 tools/perf/tests/perf-time-to-tsc.c |  2 ++
 tools/perf/tests/sw-clock.c         | 12 ++++--------
 tools/perf/tests/switch-tracking.c  |  5 ++---
 tools/perf/tests/task-exit.c        | 10 +++-------
 tools/perf/tests/thread-map.c       |  8 +++-----
 10 files changed, 28 insertions(+), 36 deletions(-)

-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 01/11] perf test: Fix cpu and thread map leaks in basic mmap test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
@ 2021-03-01 14:03 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 02/11] perf test: Fix a memory leak in attr test Namhyung Kim
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:03 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The evlist has the maps with its own refcounts so we don't need to set
the pointers to NULL.  Otherwise following error was reported by Asan.

  # perf test -v 4
   4: Read samples using the mmap interface      :
  --- start ---
  test child forked, pid 139782
  mmap size 528384B

  =================================================================
  ==139782==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x7f1f76daee8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x564ba21a0fea in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x564ba21a1a0f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x564ba21a21cf in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x564ba21a21cf in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x564ba1e48298 in test__basic_mmap tests/mmap-basic.c:55
    #6 0x564ba1e278fb in run_test tests/builtin-test.c:428
    #7 0x564ba1e278fb in test_and_print tests/builtin-test.c:458
    #8 0x564ba1e29a53 in __cmd_test tests/builtin-test.c:679
    #9 0x564ba1e29a53 in cmd_test tests/builtin-test.c:825
    #10 0x564ba1e95cb4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x564ba1d1fa88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x564ba1d1fa88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x564ba1d1fa88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7f1f768e4d09 in __libc_start_main ../csu/libc-start.c:308

    ...
  test child finished with 1
  ---- end ----
  Read samples using the mmap interface: FAILED!
  failed to open shell test directory: /home/namhyung/libexec/perf-core/tests/shell

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/mmap-basic.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c
index 57093aeacc6f..73ae8f7aa066 100644
--- a/tools/perf/tests/mmap-basic.c
+++ b/tools/perf/tests/mmap-basic.c
@@ -158,8 +158,6 @@ int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unuse
 
 out_delete_evlist:
 	evlist__delete(evlist);
-	cpus	= NULL;
-	threads = NULL;
 out_free_cpus:
 	perf_cpu_map__put(cpus);
 out_free_threads:
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 02/11] perf test: Fix a memory leak in attr test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
  2021-03-01 14:03 ` [PATCH 01/11] perf test: Fix cpu and thread map leaks in basic mmap test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 03/11] perf test: Fix cpu and thread map leaks in task_exit test Namhyung Kim
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The get_argv_exec_path() returns a dynamic memory so it should be
freed after use.

  $ perf test -v 17
  ...
  ==141682==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 33 byte(s) in 1 object(s) allocated from:
    #0 0x7f09107d2e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x7f091035f6a7 in __vasprintf_internal libio/vasprintf.c:71

  SUMMARY: AddressSanitizer: 33 byte(s) leaked in 1 allocation(s).

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/attr.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/perf/tests/attr.c b/tools/perf/tests/attr.c
index ec972e0892ab..dd39ce9b0277 100644
--- a/tools/perf/tests/attr.c
+++ b/tools/perf/tests/attr.c
@@ -182,14 +182,20 @@ int test__attr(struct test *test __maybe_unused, int subtest __maybe_unused)
 	struct stat st;
 	char path_perf[PATH_MAX];
 	char path_dir[PATH_MAX];
+	char *exec_path;
 
 	/* First try development tree tests. */
 	if (!lstat("./tests", &st))
 		return run_dir("./tests", "./perf");
 
+	exec_path = get_argv_exec_path();
+	if (exec_path == NULL)
+		return -1;
+
 	/* Then installed path. */
-	snprintf(path_dir,  PATH_MAX, "%s/tests", get_argv_exec_path());
+	snprintf(path_dir,  PATH_MAX, "%s/tests", exec_path);
 	snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
+	free(exec_path);
 
 	if (!lstat(path_dir, &st) &&
 	    !lstat(path_perf, &st))
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 03/11] perf test: Fix cpu and thread map leaks in task_exit test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
  2021-03-01 14:03 ` [PATCH 01/11] perf test: Fix cpu and thread map leaks in basic mmap test Namhyung Kim
  2021-03-01 14:04 ` [PATCH 02/11] perf test: Fix a memory leak in attr test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test Namhyung Kim
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The evlist has the maps with its own refcounts so we don't need to set
the pointers to NULL.  Otherwise following error was reported by Asan.

Also change the goto label since it doesn't need to have two.

  # perf test -v 24
  24: Number of exit events of a simple workload :
  --- start ---
  test child forked, pid 145915
  mmap size 528384B

  =================================================================
  ==145915==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7fc44e50d1f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x561cf50f4d2e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x561cf4eeb949 in thread_map__new_by_tid util/thread_map.c:63
    #3 0x561cf4db7fd2 in test__task_exit tests/task-exit.c:74
    #4 0x561cf4d798fb in run_test tests/builtin-test.c:428
    #5 0x561cf4d798fb in test_and_print tests/builtin-test.c:458
    #6 0x561cf4d7ba53 in __cmd_test tests/builtin-test.c:679
    #7 0x561cf4d7ba53 in cmd_test tests/builtin-test.c:825
    #8 0x561cf4de7d04 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x561cf4c71a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x561cf4c71a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x561cf4c71a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fc44e042d09 in __libc_start_main ../csu/libc-start.c:308

    ...
  test child finished with 1
  ---- end ----
  Number of exit events of a simple workload: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/task-exit.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/tools/perf/tests/task-exit.c b/tools/perf/tests/task-exit.c
index bbf94e4aa145..4c2969db59b0 100644
--- a/tools/perf/tests/task-exit.c
+++ b/tools/perf/tests/task-exit.c
@@ -75,14 +75,11 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 	if (!cpus || !threads) {
 		err = -ENOMEM;
 		pr_debug("Not enough memory to create thread/cpu maps\n");
-		goto out_free_maps;
+		goto out_delete_evlist;
 	}
 
 	perf_evlist__set_maps(&evlist->core, cpus, threads);
 
-	cpus	= NULL;
-	threads = NULL;
-
 	err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
 	if (err < 0) {
 		pr_debug("Couldn't run the workload!\n");
@@ -137,7 +134,7 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 		if (retry_count++ > 1000) {
 			pr_debug("Failed after retrying 1000 times\n");
 			err = -1;
-			goto out_free_maps;
+			goto out_delete_evlist;
 		}
 
 		goto retry;
@@ -148,10 +145,9 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 		err = -1;
 	}
 
-out_free_maps:
+out_delete_evlist:
 	perf_cpu_map__put(cpus);
 	perf_thread_map__put(threads);
-out_delete_evlist:
 	evlist__delete(evlist);
 	return err;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (2 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 03/11] perf test: Fix cpu and thread map leaks in task_exit test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 17:24   ` Jiri Olsa
  2021-03-01 14:04 ` [PATCH 05/11] perf test: Fix cpu and thread map leaks in code_reading test Namhyung Kim
                   ` (6 subsequent siblings)
  10 siblings, 1 reply; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The evlist has the maps with its own refcounts so we don't need to set
the pointers to NULL.  Otherwise following error was reported by Asan.

Also change the goto label since it doesn't need to have two.

  # perf test -v 25
  25: Software clock events period values        :
  --- start ---
  test child forked, pid 149154
  mmap size 528384B
  mmap size 528384B

  =================================================================
  ==149154==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7fef5cd071f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x56260d5e8b8e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x56260d3df7a9 in thread_map__new_by_tid util/thread_map.c:63
    #3 0x56260d2ac6b2 in __test__sw_clock_freq tests/sw-clock.c:65
    #4 0x56260d26d8fb in run_test tests/builtin-test.c:428
    #5 0x56260d26d8fb in test_and_print tests/builtin-test.c:458
    #6 0x56260d26fa53 in __cmd_test tests/builtin-test.c:679
    #7 0x56260d26fa53 in cmd_test tests/builtin-test.c:825
    #8 0x56260d2dbb64 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x56260d165a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x56260d165a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x56260d165a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fef5c83cd09 in __libc_start_main ../csu/libc-start.c:308

    ...
  test child finished with 1
  ---- end ----
  Software clock events period values      : FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/sw-clock.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/tools/perf/tests/sw-clock.c b/tools/perf/tests/sw-clock.c
index a49c9e23053b..74988846be1d 100644
--- a/tools/perf/tests/sw-clock.c
+++ b/tools/perf/tests/sw-clock.c
@@ -42,8 +42,8 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 		.disabled = 1,
 		.freq = 1,
 	};
-	struct perf_cpu_map *cpus;
-	struct perf_thread_map *threads;
+	struct perf_cpu_map *cpus = NULL;
+	struct perf_thread_map *threads = NULL;
 	struct mmap *md;
 
 	attr.sample_freq = 500;
@@ -66,14 +66,11 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 	if (!cpus || !threads) {
 		err = -ENOMEM;
 		pr_debug("Not enough memory to create thread/cpu maps\n");
-		goto out_free_maps;
+		goto out_delete_evlist;
 	}
 
 	perf_evlist__set_maps(&evlist->core, cpus, threads);
 
-	cpus	= NULL;
-	threads = NULL;
-
 	if (evlist__open(evlist)) {
 		const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
 
@@ -129,10 +126,9 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 		err = -1;
 	}
 
-out_free_maps:
+out_delete_evlist:
 	perf_cpu_map__put(cpus);
 	perf_thread_map__put(threads);
-out_delete_evlist:
 	evlist__delete(evlist);
 	return err;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 05/11] perf test: Fix cpu and thread map leaks in code_reading test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (3 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 06/11] perf test: Fix cpu and thread map leaks in keep_tracking test Namhyung Kim
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The evlist and the cpu/thread maps should be released together.
Otherwise following error was reported by Asan.

Note that this test still has memory leaks in DSOs so it still fails
even after this change.  I'll take a look at that too.

  # perf test -v 26
  26: Object code reading                        :
  --- start ---
  test child forked, pid 154184
  Looking at the vmlinux_path (8 entries long)
  symsrc__init: build id mismatch for vmlinux.
  symsrc__init: cannot get elf header.
  Using /proc/kcore for kernel data
  Using /proc/kallsyms for symbols
  Parsing event 'cycles'
  mmap size 528384B
  ...
  =================================================================
  ==154184==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 439 byte(s) in 1 object(s) allocated from:
    #0 0x7fcb66e77037 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    #1 0x55ad9b7e821e in dso__new_id util/dso.c:1256
    #2 0x55ad9b8cfd4a in __machine__addnew_vdso util/vdso.c:132
    #3 0x55ad9b8cfd4a in machine__findnew_vdso util/vdso.c:347
    #4 0x55ad9b845b7e in map__new util/map.c:176
    #5 0x55ad9b8415a2 in machine__process_mmap2_event util/machine.c:1787
    #6 0x55ad9b8fab16 in perf_tool__process_synth_event util/synthetic-events.c:64
    #7 0x55ad9b8fab16 in perf_event__synthesize_mmap_events util/synthetic-events.c:499
    #8 0x55ad9b8fbfdf in __event__synthesize_thread util/synthetic-events.c:741
    #9 0x55ad9b8ff3e3 in perf_event__synthesize_thread_map util/synthetic-events.c:833
    #10 0x55ad9b738585 in do_test_code_reading tests/code-reading.c:608
    #11 0x55ad9b73b25d in test__code_reading tests/code-reading.c:722
    #12 0x55ad9b6f28fb in run_test tests/builtin-test.c:428
    #13 0x55ad9b6f28fb in test_and_print tests/builtin-test.c:458
    #14 0x55ad9b6f4a53 in __cmd_test tests/builtin-test.c:679
    #15 0x55ad9b6f4a53 in cmd_test tests/builtin-test.c:825
    #16 0x55ad9b760cc4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #17 0x55ad9b5eaa88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #18 0x55ad9b5eaa88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #19 0x55ad9b5eaa88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #20 0x7fcb669acd09 in __libc_start_main ../csu/libc-start.c:308

    ...
  SUMMARY: AddressSanitizer: 471 byte(s) leaked in 2 allocation(s).
  test child finished with 1
  ---- end ----
  Object code reading: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/code-reading.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c
index 280f0348a09c..2fdc7b2f996e 100644
--- a/tools/perf/tests/code-reading.c
+++ b/tools/perf/tests/code-reading.c
@@ -706,13 +706,9 @@ static int do_test_code_reading(bool try_kcore)
 out_put:
 	thread__put(thread);
 out_err:
-
-	if (evlist) {
-		evlist__delete(evlist);
-	} else {
-		perf_cpu_map__put(cpus);
-		perf_thread_map__put(threads);
-	}
+	evlist__delete(evlist);
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 	machine__delete_threads(machine);
 	machine__delete(machine);
 
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 06/11] perf test: Fix cpu and thread map leaks in keep_tracking test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (4 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 05/11] perf test: Fix cpu and thread map leaks in code_reading test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 07/11] perf test: Fix cpu and thread map leaks in switch_tracking test Namhyung Kim
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The evlist and the cpu/thread maps should be released together.
Otherwise following error was reported by Asan.

  $ perf test -v 28
  28: Use a dummy software event to keep tracking:
  --- start ---
  test child forked, pid 156810
  mmap size 528384B

  =================================================================
  ==156810==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x7f637d2bce8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55cc6295cffa in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x55cc6295da1f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x55cc6295e1df in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x55cc6295e1df in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x55cc626287cf in test__keep_tracking tests/keep-tracking.c:84
    #6 0x55cc625e38fb in run_test tests/builtin-test.c:428
    #7 0x55cc625e38fb in test_and_print tests/builtin-test.c:458
    #8 0x55cc625e5a53 in __cmd_test tests/builtin-test.c:679
    #9 0x55cc625e5a53 in cmd_test tests/builtin-test.c:825
    #10 0x55cc62651cc4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x55cc624dba88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x55cc624dba88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x55cc624dba88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7f637cdf2d09 in __libc_start_main ../csu/libc-start.c:308

  SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s).
  test child finished with 1
  ---- end ----
  Use a dummy software event to keep tracking: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/keep-tracking.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tools/perf/tests/keep-tracking.c b/tools/perf/tests/keep-tracking.c
index e6f1b2a38e03..a0438b0f0805 100644
--- a/tools/perf/tests/keep-tracking.c
+++ b/tools/perf/tests/keep-tracking.c
@@ -154,10 +154,9 @@ int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_un
 	if (evlist) {
 		evlist__disable(evlist);
 		evlist__delete(evlist);
-	} else {
-		perf_cpu_map__put(cpus);
-		perf_thread_map__put(threads);
 	}
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 
 	return err;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 07/11] perf test: Fix cpu and thread map leaks in switch_tracking test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (5 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 06/11] perf test: Fix cpu and thread map leaks in keep_tracking test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 08/11] perf test: Fix a thread map leak in thread_map_synthesize test Namhyung Kim
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The evlist and cpu/thread maps should be released together.
Otherwise the following error was reported by Asan.

  $ perf test -v 35
  35: Track with sched_switch                    :
  --- start ---
  test child forked, pid 159287
  Using CPUID GenuineIntel-6-8E-C
  mmap size 528384B
  1295 events recorded

  =================================================================
  ==159287==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x7fa28d9a2e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5652f5a5affa in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x5652f5a5ba1f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x5652f5a5c1df in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x5652f5a5c1df in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x5652f5723bbf in test__switch_tracking tests/switch-tracking.c:350
    #6 0x5652f56e18fb in run_test tests/builtin-test.c:428
    #7 0x5652f56e18fb in test_and_print tests/builtin-test.c:458
    #8 0x5652f56e3a53 in __cmd_test tests/builtin-test.c:679
    #9 0x5652f56e3a53 in cmd_test tests/builtin-test.c:825
    #10 0x5652f574fcc4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x5652f55d9a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x5652f55d9a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x5652f55d9a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7fa28d4d8d09 in __libc_start_main ../csu/libc-start.c:308

  SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s).
  test child finished with 1
  ---- end ----
  Track with sched_switch: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/switch-tracking.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tools/perf/tests/switch-tracking.c b/tools/perf/tests/switch-tracking.c
index 15a2ab765d89..3ebaa758df77 100644
--- a/tools/perf/tests/switch-tracking.c
+++ b/tools/perf/tests/switch-tracking.c
@@ -574,10 +574,9 @@ int test__switch_tracking(struct test *test __maybe_unused, int subtest __maybe_
 	if (evlist) {
 		evlist__disable(evlist);
 		evlist__delete(evlist);
-	} else {
-		perf_cpu_map__put(cpus);
-		perf_thread_map__put(threads);
 	}
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 
 	return err;
 
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 08/11] perf test: Fix a thread map leak in thread_map_synthesize test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (6 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 07/11] perf test: Fix cpu and thread map leaks in switch_tracking test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 09/11] perf test: Fix a memory leak in thread_map_remove test Namhyung Kim
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

It missed to call perf_thread_map__put() after using the map.

  $ perf test -v 43
  43: Synthesize thread map                      :
  --- start ---
  test child forked, pid 162640

  =================================================================
  ==162640==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7fd48cdaa1f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x563e6d5f8d0e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x563e6d3ef69a in thread_map__new_by_pid util/thread_map.c:46
    #3 0x563e6d2cec90 in test__thread_map_synthesize tests/thread-map.c:97
    #4 0x563e6d27d8fb in run_test tests/builtin-test.c:428
    #5 0x563e6d27d8fb in test_and_print tests/builtin-test.c:458
    #6 0x563e6d27fa53 in __cmd_test tests/builtin-test.c:679
    #7 0x563e6d27fa53 in cmd_test tests/builtin-test.c:825
    #8 0x563e6d2ebce4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x563e6d175a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x563e6d175a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x563e6d175a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fd48c8dfd09 in __libc_start_main ../csu/libc-start.c:308

  SUMMARY: AddressSanitizer: 8224 byte(s) leaked in 2 allocation(s).
  test child finished with 1
  ---- end ----
  Synthesize thread map: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/thread-map.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
index 28f51c4bd373..9e1cf11149ef 100644
--- a/tools/perf/tests/thread-map.c
+++ b/tools/perf/tests/thread-map.c
@@ -102,6 +102,7 @@ int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __
 	TEST_ASSERT_VAL("failed to synthesize map",
 		!perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
 
+	perf_thread_map__put(threads);
 	return 0;
 }
 
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 09/11] perf test: Fix a memory leak in thread_map_remove test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (7 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 08/11] perf test: Fix a thread map leak in thread_map_synthesize test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 10/11] perf test: Fix cpu map leaks in cpu_map_print test Namhyung Kim
  2021-03-01 14:04 ` [PATCH 11/11] perf test: Fix cpu and thread map leaks in perf_time_to_tsc test Namhyung Kim
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

The str should be freed after creating a thread map.  Also change the
open-coded thread map deletion to a call to perf_thread_map__put().

  $ perf test -v 44
  44: Remove thread map                          :
  --- start ---
  test child forked, pid 165536
  2 threads: 165535, 165536
  1 thread: 165536
  0 thread:

  =================================================================
  ==165536==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 14 byte(s) in 1 object(s) allocated from:
    #0 0x7f54453ffe8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x7f5444f8c6a7 in __vasprintf_internal libio/vasprintf.c:71

  SUMMARY: AddressSanitizer: 14 byte(s) leaked in 1 allocation(s).
  test child finished with 1
  ---- end ----
  Remove thread map: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/thread-map.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
index 9e1cf11149ef..d1e208b4a571 100644
--- a/tools/perf/tests/thread-map.c
+++ b/tools/perf/tests/thread-map.c
@@ -110,12 +110,12 @@ int test__thread_map_remove(struct test *test __maybe_unused, int subtest __mayb
 {
 	struct perf_thread_map *threads;
 	char *str;
-	int i;
 
 	TEST_ASSERT_VAL("failed to allocate map string",
 			asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
 
 	threads = thread_map__new_str(str, NULL, 0, false);
+	free(str);
 
 	TEST_ASSERT_VAL("failed to allocate thread_map",
 			threads);
@@ -142,9 +142,6 @@ int test__thread_map_remove(struct test *test __maybe_unused, int subtest __mayb
 	TEST_ASSERT_VAL("failed to not remove thread",
 			thread_map__remove(threads, 0));
 
-	for (i = 0; i < threads->nr; i++)
-		zfree(&threads->map[i].comm);
-
-	free(threads);
+	perf_thread_map__put(threads);
 	return 0;
 }
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 10/11] perf test: Fix cpu map leaks in cpu_map_print test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (8 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 09/11] perf test: Fix a memory leak in thread_map_remove test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  2021-03-01 14:04 ` [PATCH 11/11] perf test: Fix cpu and thread map leaks in perf_time_to_tsc test Namhyung Kim
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

It should be released after printing the map.

  $ perf test -v 52
  52: Print cpu map                              :
  --- start ---
  test child forked, pid 172233

  =================================================================
  ==172233==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 156 byte(s) in 1 object(s) allocated from:
    #0 0x7fc472518e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55e63b378f7a in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x55e63b37a05c in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:237
    #3 0x55e63b056d16 in cpu_map_print tests/cpumap.c:102
    #4 0x55e63b056d16 in test__cpu_map_print tests/cpumap.c:120
    #5 0x55e63afff8fb in run_test tests/builtin-test.c:428
    #6 0x55e63afff8fb in test_and_print tests/builtin-test.c:458
    #7 0x55e63b001a53 in __cmd_test tests/builtin-test.c:679
    #8 0x55e63b001a53 in cmd_test tests/builtin-test.c:825
    #9 0x55e63b06dc44 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #10 0x55e63aef7a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #11 0x55e63aef7a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #12 0x55e63aef7a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #13 0x7fc47204ed09 in __libc_start_main ../csu/libc-start.c:308
  ...

  SUMMARY: AddressSanitizer: 448 byte(s) leaked in 7 allocation(s).
  test child finished with 1
  ---- end ----
  Print cpu map: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/cpumap.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c
index 29c793ac7d10..0472b110fe65 100644
--- a/tools/perf/tests/cpumap.c
+++ b/tools/perf/tests/cpumap.c
@@ -106,6 +106,8 @@ static int cpu_map_print(const char *str)
 		return -1;
 
 	cpu_map__snprint(map, buf, sizeof(buf));
+	perf_cpu_map__put(map);
+
 	return !strcmp(buf, str);
 }
 
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 11/11] perf test: Fix cpu and thread map leaks in perf_time_to_tsc test
  2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
                   ` (9 preceding siblings ...)
  2021-03-01 14:04 ` [PATCH 10/11] perf test: Fix cpu map leaks in cpu_map_print test Namhyung Kim
@ 2021-03-01 14:04 ` Namhyung Kim
  10 siblings, 0 replies; 16+ messages in thread
From: Namhyung Kim @ 2021-03-01 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, Mark Rutland, Alexander Shishkin,
	LKML, Stephane Eranian, Andi Kleen, Adrian Hunter, Ian Rogers,
	Leo Yan

It should release the maps at the end.

  $ perf test -v 71
  71: Convert perf time to TSC                   :
  --- start ---
  test child forked, pid 178744
  mmap size 528384B
  1st event perf time 59207256505278 tsc 13187166645142
  rdtsc          time 59207256542151 tsc 13187166723020
  2nd event perf time 59207256543749 tsc 13187166726393

  =================================================================
  ==178744==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x7faf601f9e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55b620cfc00a in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x55b620cfca2f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x55b620cfd1ef in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x55b620cfd1ef in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x55b6209ef1b2 in test__perf_time_to_tsc tests/perf-time-to-tsc.c:73
    #6 0x55b6209828fb in run_test tests/builtin-test.c:428
    #7 0x55b6209828fb in test_and_print tests/builtin-test.c:458
    #8 0x55b620984a53 in __cmd_test tests/builtin-test.c:679
    #9 0x55b620984a53 in cmd_test tests/builtin-test.c:825
    #10 0x55b6209f0cd4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x55b62087aa88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x55b62087aa88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x55b62087aa88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7faf5fd2fd09 in __libc_start_main ../csu/libc-start.c:308

  SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s).
  test child finished with 1
  ---- end ----
  Convert perf time to TSC: FAILED!

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/perf-time-to-tsc.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/perf/tests/perf-time-to-tsc.c b/tools/perf/tests/perf-time-to-tsc.c
index 7cff02664d0e..680c3cffb128 100644
--- a/tools/perf/tests/perf-time-to-tsc.c
+++ b/tools/perf/tests/perf-time-to-tsc.c
@@ -167,6 +167,8 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe
 
 out_err:
 	evlist__delete(evlist);
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 	return err;
 }
 
-- 
2.30.1.766.gb4fecdf3b7-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test
  2021-03-01 14:04 ` [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test Namhyung Kim
@ 2021-03-01 17:24   ` Jiri Olsa
  2021-03-02  1:50     ` Namhyung Kim
  0 siblings, 1 reply; 16+ messages in thread
From: Jiri Olsa @ 2021-03-01 17:24 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Mark Rutland, Alexander Shishkin, LKML, Stephane Eranian,
	Andi Kleen, Adrian Hunter, Ian Rogers, Leo Yan

On Mon, Mar 01, 2021 at 11:04:02PM +0900, Namhyung Kim wrote:
> The evlist has the maps with its own refcounts so we don't need to set
> the pointers to NULL.  Otherwise following error was reported by Asan.
> 
> Also change the goto label since it doesn't need to have two.
> 
>   # perf test -v 25
>   25: Software clock events period values        :
>   --- start ---
>   test child forked, pid 149154
>   mmap size 528384B
>   mmap size 528384B
> 
>   =================================================================
>   ==149154==ERROR: LeakSanitizer: detected memory leaks
> 
>   Direct leak of 32 byte(s) in 1 object(s) allocated from:
>     #0 0x7fef5cd071f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
>     #1 0x56260d5e8b8e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
>     #2 0x56260d3df7a9 in thread_map__new_by_tid util/thread_map.c:63
>     #3 0x56260d2ac6b2 in __test__sw_clock_freq tests/sw-clock.c:65
>     #4 0x56260d26d8fb in run_test tests/builtin-test.c:428
>     #5 0x56260d26d8fb in test_and_print tests/builtin-test.c:458
>     #6 0x56260d26fa53 in __cmd_test tests/builtin-test.c:679
>     #7 0x56260d26fa53 in cmd_test tests/builtin-test.c:825
>     #8 0x56260d2dbb64 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
>     #9 0x56260d165a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
>     #10 0x56260d165a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
>     #11 0x56260d165a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
>     #12 0x7fef5c83cd09 in __libc_start_main ../csu/libc-start.c:308
> 
>     ...
>   test child finished with 1
>   ---- end ----
>   Software clock events period values      : FAILED!
> 
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/tests/sw-clock.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/perf/tests/sw-clock.c b/tools/perf/tests/sw-clock.c
> index a49c9e23053b..74988846be1d 100644
> --- a/tools/perf/tests/sw-clock.c
> +++ b/tools/perf/tests/sw-clock.c
> @@ -42,8 +42,8 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
>  		.disabled = 1,
>  		.freq = 1,
>  	};
> -	struct perf_cpu_map *cpus;
> -	struct perf_thread_map *threads;
> +	struct perf_cpu_map *cpus = NULL;
> +	struct perf_thread_map *threads = NULL;
>  	struct mmap *md;
>  
>  	attr.sample_freq = 500;
> @@ -66,14 +66,11 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
>  	if (!cpus || !threads) {
>  		err = -ENOMEM;
>  		pr_debug("Not enough memory to create thread/cpu maps\n");
> -		goto out_free_maps;
> +		goto out_delete_evlist;
>  	}
>  
>  	perf_evlist__set_maps(&evlist->core, cpus, threads);
>  
> -	cpus	= NULL;
> -	threads = NULL;

hum, so IIUC we added these and the other you remove in your patches long time ago,
because there was no refcounting at that time, right?

jirka

> -
>  	if (evlist__open(evlist)) {
>  		const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
>  
> @@ -129,10 +126,9 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
>  		err = -1;
>  	}
>  
> -out_free_maps:
> +out_delete_evlist:
>  	perf_cpu_map__put(cpus);
>  	perf_thread_map__put(threads);
> -out_delete_evlist:
>  	evlist__delete(evlist);
>  	return err;
>  }
> -- 
> 2.30.1.766.gb4fecdf3b7-goog
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test
  2021-03-01 17:24   ` Jiri Olsa
@ 2021-03-02  1:50     ` Namhyung Kim
  2021-03-02 13:03       ` Jiri Olsa
  0 siblings, 1 reply; 16+ messages in thread
From: Namhyung Kim @ 2021-03-02  1:50 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Mark Rutland, Alexander Shishkin, LKML, Stephane Eranian,
	Andi Kleen, Adrian Hunter, Ian Rogers, Leo Yan

Hi Jiri,

On Tue, Mar 2, 2021 at 2:24 AM Jiri Olsa <jolsa@redhat.com> wrote:
>
> On Mon, Mar 01, 2021 at 11:04:02PM +0900, Namhyung Kim wrote:
> > The evlist has the maps with its own refcounts so we don't need to set
> > the pointers to NULL.  Otherwise following error was reported by Asan.
> >
> > Also change the goto label since it doesn't need to have two.
> >
> >   # perf test -v 25
> >   25: Software clock events period values        :
> >   --- start ---
> >   test child forked, pid 149154
> >   mmap size 528384B
> >   mmap size 528384B
> >
> >   =================================================================
> >   ==149154==ERROR: LeakSanitizer: detected memory leaks
> >
> >   Direct leak of 32 byte(s) in 1 object(s) allocated from:
> >     #0 0x7fef5cd071f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
> >     #1 0x56260d5e8b8e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
> >     #2 0x56260d3df7a9 in thread_map__new_by_tid util/thread_map.c:63
> >     #3 0x56260d2ac6b2 in __test__sw_clock_freq tests/sw-clock.c:65
> >     #4 0x56260d26d8fb in run_test tests/builtin-test.c:428
> >     #5 0x56260d26d8fb in test_and_print tests/builtin-test.c:458
> >     #6 0x56260d26fa53 in __cmd_test tests/builtin-test.c:679
> >     #7 0x56260d26fa53 in cmd_test tests/builtin-test.c:825
> >     #8 0x56260d2dbb64 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
> >     #9 0x56260d165a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
> >     #10 0x56260d165a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
> >     #11 0x56260d165a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
> >     #12 0x7fef5c83cd09 in __libc_start_main ../csu/libc-start.c:308
> >
> >     ...
> >   test child finished with 1
> >   ---- end ----
> >   Software clock events period values      : FAILED!
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> >  tools/perf/tests/sw-clock.c | 12 ++++--------
> >  1 file changed, 4 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/perf/tests/sw-clock.c b/tools/perf/tests/sw-clock.c
> > index a49c9e23053b..74988846be1d 100644
> > --- a/tools/perf/tests/sw-clock.c
> > +++ b/tools/perf/tests/sw-clock.c
> > @@ -42,8 +42,8 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
> >               .disabled = 1,
> >               .freq = 1,
> >       };
> > -     struct perf_cpu_map *cpus;
> > -     struct perf_thread_map *threads;
> > +     struct perf_cpu_map *cpus = NULL;
> > +     struct perf_thread_map *threads = NULL;
> >       struct mmap *md;
> >
> >       attr.sample_freq = 500;
> > @@ -66,14 +66,11 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
> >       if (!cpus || !threads) {
> >               err = -ENOMEM;
> >               pr_debug("Not enough memory to create thread/cpu maps\n");
> > -             goto out_free_maps;
> > +             goto out_delete_evlist;
> >       }
> >
> >       perf_evlist__set_maps(&evlist->core, cpus, threads);
> >
> > -     cpus    = NULL;
> > -     threads = NULL;
>
> hum, so IIUC we added these and the other you remove in your patches long time ago,
> because there was no refcounting at that time, right?

It seems my original patch just set the maps directly.

  bc96b361cbf9 perf tests: Add a test case for checking sw clock event frequency

And after that Adrian changed it to use the set_maps() helper.

  c5e6bd2ed3e8 perf tests: Fix software clock events test setting maps

It seems we already had the refcounting at the moment.  And then the libperf
renaming happened later.

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test
  2021-03-02  1:50     ` Namhyung Kim
@ 2021-03-02 13:03       ` Jiri Olsa
  2021-03-03 15:43         ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 16+ messages in thread
From: Jiri Olsa @ 2021-03-02 13:03 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Mark Rutland, Alexander Shishkin, LKML, Stephane Eranian,
	Andi Kleen, Adrian Hunter, Ian Rogers, Leo Yan

On Tue, Mar 02, 2021 at 10:50:15AM +0900, Namhyung Kim wrote:

SNIP

> > >               err = -ENOMEM;
> > >               pr_debug("Not enough memory to create thread/cpu maps\n");
> > > -             goto out_free_maps;
> > > +             goto out_delete_evlist;
> > >       }
> > >
> > >       perf_evlist__set_maps(&evlist->core, cpus, threads);
> > >
> > > -     cpus    = NULL;
> > > -     threads = NULL;
> >
> > hum, so IIUC we added these and the other you remove in your patches long time ago,
> > because there was no refcounting at that time, right?
> 
> It seems my original patch just set the maps directly.
> 
>   bc96b361cbf9 perf tests: Add a test case for checking sw clock event frequency
> 
> And after that Adrian changed it to use the set_maps() helper.
> 
>   c5e6bd2ed3e8 perf tests: Fix software clock events test setting maps

ok, and after that there's this one:
  a55e56637613 perf evlist: Reference count the cpu and thread maps at set_maps()

forcing the get calls when storing cpus and threads

for the patchset

Acked-by: Jiri Olsa <jolsa@redhat.com>

thanks,
jirka

> 
> It seems we already had the refcounting at the moment.  And then the libperf
> renaming happened later.
> 
> Thanks,
> Namhyung
> 


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test
  2021-03-02 13:03       ` Jiri Olsa
@ 2021-03-03 15:43         ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 16+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-03-03 15:43 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Namhyung Kim, Ingo Molnar, Peter Zijlstra, Mark Rutland,
	Alexander Shishkin, LKML, Stephane Eranian, Andi Kleen,
	Adrian Hunter, Ian Rogers, Leo Yan

Em Tue, Mar 02, 2021 at 02:03:05PM +0100, Jiri Olsa escreveu:
> On Tue, Mar 02, 2021 at 10:50:15AM +0900, Namhyung Kim wrote:
> 
> SNIP
> 
> > > >               err = -ENOMEM;
> > > >               pr_debug("Not enough memory to create thread/cpu maps\n");
> > > > -             goto out_free_maps;
> > > > +             goto out_delete_evlist;
> > > >       }
> > > >
> > > >       perf_evlist__set_maps(&evlist->core, cpus, threads);
> > > >
> > > > -     cpus    = NULL;
> > > > -     threads = NULL;
> > >
> > > hum, so IIUC we added these and the other you remove in your patches long time ago,
> > > because there was no refcounting at that time, right?
> > 
> > It seems my original patch just set the maps directly.
> > 
> >   bc96b361cbf9 perf tests: Add a test case for checking sw clock event frequency
> > 
> > And after that Adrian changed it to use the set_maps() helper.
> > 
> >   c5e6bd2ed3e8 perf tests: Fix software clock events test setting maps
> 
> ok, and after that there's this one:
>   a55e56637613 perf evlist: Reference count the cpu and thread maps at set_maps()
> 
> forcing the get calls when storing cpus and threads
> 
> for the patchset
> 
> Acked-by: Jiri Olsa <jolsa@redhat.com>

Thanks, applied.

- Arnaldo

 
> thanks,
> jirka
> 
> > 
> > It seems we already had the refcounting at the moment.  And then the libperf
> > renaming happened later.
> > 
> > Thanks,
> > Namhyung
> > 
> 

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-03-03 18:52 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-01 14:03 [PATCHSET 00/11] perf test: Fix cpu/thread map leaks Namhyung Kim
2021-03-01 14:03 ` [PATCH 01/11] perf test: Fix cpu and thread map leaks in basic mmap test Namhyung Kim
2021-03-01 14:04 ` [PATCH 02/11] perf test: Fix a memory leak in attr test Namhyung Kim
2021-03-01 14:04 ` [PATCH 03/11] perf test: Fix cpu and thread map leaks in task_exit test Namhyung Kim
2021-03-01 14:04 ` [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test Namhyung Kim
2021-03-01 17:24   ` Jiri Olsa
2021-03-02  1:50     ` Namhyung Kim
2021-03-02 13:03       ` Jiri Olsa
2021-03-03 15:43         ` Arnaldo Carvalho de Melo
2021-03-01 14:04 ` [PATCH 05/11] perf test: Fix cpu and thread map leaks in code_reading test Namhyung Kim
2021-03-01 14:04 ` [PATCH 06/11] perf test: Fix cpu and thread map leaks in keep_tracking test Namhyung Kim
2021-03-01 14:04 ` [PATCH 07/11] perf test: Fix cpu and thread map leaks in switch_tracking test Namhyung Kim
2021-03-01 14:04 ` [PATCH 08/11] perf test: Fix a thread map leak in thread_map_synthesize test Namhyung Kim
2021-03-01 14:04 ` [PATCH 09/11] perf test: Fix a memory leak in thread_map_remove test Namhyung Kim
2021-03-01 14:04 ` [PATCH 10/11] perf test: Fix cpu map leaks in cpu_map_print test Namhyung Kim
2021-03-01 14:04 ` [PATCH 11/11] perf test: Fix cpu and thread map leaks in perf_time_to_tsc test Namhyung Kim

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 NNTP newsgroup(s).