linux-stable/tools/perf
Riccardo Mancini cf96b8e45a perf session: Add missing evlist__delete when deleting a session
ASan reports a memory leak caused by evlist not being deleted on exit in
perf-report, perf-script and perf-data.
The problem is caused by evlist->session not being deleted, which is
allocated in perf_session__read_header, called in perf_session__new if
perf_data is in read mode.
In case of write mode, the session->evlist is filled by the caller.
This patch solves the problem by calling evlist__delete in
perf_session__delete if perf_data is in read mode.

Changes in v2:
 - call evlist__delete from within perf_session__delete

v1: https://lore.kernel.org/lkml/20210621234317.235545-1-rickyman7@gmail.com/

ASan report follows:

$ ./perf script report flamegraph
=================================================================
==227640==ERROR: LeakSanitizer: detected memory leaks

<SNIP unrelated>

Indirect leak of 2704 byte(s) in 1 object(s) allocated from:
    #0 0x4f4137 in calloc (/home/user/linux/tools/perf/perf+0x4f4137)
    #1 0xbe3d56 in zalloc /home/user/linux/tools/lib/perf/../../lib/zalloc.c:8:9
    #2 0x7f999e in evlist__new /home/user/linux/tools/perf/util/evlist.c:77:26
    #3 0x8ad938 in perf_session__read_header /home/user/linux/tools/perf/util/header.c:3797:20
    #4 0x8ec714 in perf_session__open /home/user/linux/tools/perf/util/session.c:109:6
    #5 0x8ebe83 in perf_session__new /home/user/linux/tools/perf/util/session.c:213:10
    #6 0x60c6de in cmd_script /home/user/linux/tools/perf/builtin-script.c:3856:12
    #7 0x7b2930 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
    #8 0x7b120f in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
    #9 0x7b2493 in run_argv /home/user/linux/tools/perf/perf.c:409:2
    #10 0x7b0c89 in main /home/user/linux/tools/perf/perf.c:539:3
    #11 0x7f5260654b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 568 byte(s) in 1 object(s) allocated from:
    #0 0x4f4137 in calloc (/home/user/linux/tools/perf/perf+0x4f4137)
    #1 0xbe3d56 in zalloc /home/user/linux/tools/lib/perf/../../lib/zalloc.c:8:9
    #2 0x80ce88 in evsel__new_idx /home/user/linux/tools/perf/util/evsel.c:268:24
    #3 0x8aed93 in evsel__new /home/user/linux/tools/perf/util/evsel.h:210:9
    #4 0x8ae07e in perf_session__read_header /home/user/linux/tools/perf/util/header.c:3853:11
    #5 0x8ec714 in perf_session__open /home/user/linux/tools/perf/util/session.c:109:6
    #6 0x8ebe83 in perf_session__new /home/user/linux/tools/perf/util/session.c:213:10
    #7 0x60c6de in cmd_script /home/user/linux/tools/perf/builtin-script.c:3856:12
    #8 0x7b2930 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
    #9 0x7b120f in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
    #10 0x7b2493 in run_argv /home/user/linux/tools/perf/perf.c:409:2
    #11 0x7b0c89 in main /home/user/linux/tools/perf/perf.c:539:3
    #12 0x7f5260654b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 264 byte(s) in 1 object(s) allocated from:
    #0 0x4f4137 in calloc (/home/user/linux/tools/perf/perf+0x4f4137)
    #1 0xbe3d56 in zalloc /home/user/linux/tools/lib/perf/../../lib/zalloc.c:8:9
    #2 0xbe3e70 in xyarray__new /home/user/linux/tools/lib/perf/xyarray.c:10:23
    #3 0xbd7754 in perf_evsel__alloc_id /home/user/linux/tools/lib/perf/evsel.c:361:21
    #4 0x8ae201 in perf_session__read_header /home/user/linux/tools/perf/util/header.c:3871:7
    #5 0x8ec714 in perf_session__open /home/user/linux/tools/perf/util/session.c:109:6
    #6 0x8ebe83 in perf_session__new /home/user/linux/tools/perf/util/session.c:213:10
    #7 0x60c6de in cmd_script /home/user/linux/tools/perf/builtin-script.c:3856:12
    #8 0x7b2930 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
    #9 0x7b120f in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
    #10 0x7b2493 in run_argv /home/user/linux/tools/perf/perf.c:409:2
    #11 0x7b0c89 in main /home/user/linux/tools/perf/perf.c:539:3
    #12 0x7f5260654b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x4f4137 in calloc (/home/user/linux/tools/perf/perf+0x4f4137)
    #1 0xbe3d56 in zalloc /home/user/linux/tools/lib/perf/../../lib/zalloc.c:8:9
    #2 0xbd77e0 in perf_evsel__alloc_id /home/user/linux/tools/lib/perf/evsel.c:365:14
    #3 0x8ae201 in perf_session__read_header /home/user/linux/tools/perf/util/header.c:3871:7
    #4 0x8ec714 in perf_session__open /home/user/linux/tools/perf/util/session.c:109:6
    #5 0x8ebe83 in perf_session__new /home/user/linux/tools/perf/util/session.c:213:10
    #6 0x60c6de in cmd_script /home/user/linux/tools/perf/builtin-script.c:3856:12
    #7 0x7b2930 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
    #8 0x7b120f in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
    #9 0x7b2493 in run_argv /home/user/linux/tools/perf/perf.c:409:2
    #10 0x7b0c89 in main /home/user/linux/tools/perf/perf.c:539:3
    #11 0x7f5260654b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x4b8207 in strdup (/home/user/linux/tools/perf/perf+0x4b8207)
    #1 0x8b4459 in evlist__set_event_name /home/user/linux/tools/perf/util/header.c:2292:16
    #2 0x89d862 in process_event_desc /home/user/linux/tools/perf/util/header.c:2313:3
    #3 0x8af319 in perf_file_section__process /home/user/linux/tools/perf/util/header.c:3651:9
    #4 0x8aa6e9 in perf_header__process_sections /home/user/linux/tools/perf/util/header.c:3427:9
    #5 0x8ae3e7 in perf_session__read_header /home/user/linux/tools/perf/util/header.c:3886:2
    #6 0x8ec714 in perf_session__open /home/user/linux/tools/perf/util/session.c:109:6
    #7 0x8ebe83 in perf_session__new /home/user/linux/tools/perf/util/session.c:213:10
    #8 0x60c6de in cmd_script /home/user/linux/tools/perf/builtin-script.c:3856:12
    #9 0x7b2930 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
    #10 0x7b120f in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
    #11 0x7b2493 in run_argv /home/user/linux/tools/perf/perf.c:409:2
    #12 0x7b0c89 in main /home/user/linux/tools/perf/perf.c:539:3
    #13 0x7f5260654b74  (/lib64/libc.so.6+0x27b74)

SUMMARY: AddressSanitizer: 3728 byte(s) leaked in 7 allocation(s).

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
Acked-by: Ian Rogers <irogers@google.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20210624231926.212208-1-rickyman7@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-07-01 16:14:38 -03:00
..
arch perf cs-etm: Remove callback cs_etm_find_snapshot() 2021-07-01 16:14:36 -03:00
bench perf tools: Fix various typos in comments 2021-03-23 17:13:43 -03:00
Documentation perf dlfilter: Add object_code() to perf_dlfilter_fns 2021-07-01 16:14:38 -03:00
examples/bpf perf tools: Fix various typos in comments 2021-03-23 17:13:43 -03:00
include/bpf
jvmti perf tools: Fix various typos in comments 2021-03-23 17:13:43 -03:00
pmu-events Revert "perf vendor events intel: Add metrics for Icelake Server" 2021-06-02 08:27:00 -03:00
python
scripts perf scripting python: intel-pt-events.py: Add --insn-trace and --src-trace 2021-06-01 10:05:20 -03:00
tests perf test: Pass the verbose option to shell tests 2021-06-22 14:52:09 -03:00
trace perf beauty: Update copy of linux/socket.h with the kernel sources 2021-06-19 10:09:08 -03:00
ui perf annotate: Allow 's' on source code lines 2021-07-01 16:14:38 -03:00
util perf session: Add missing evlist__delete when deleting a session 2021-07-01 16:14:38 -03:00
.gitignore perf: Update .gitignore file 2021-04-20 08:40:20 -03:00
Build perf daemon: Add daemon command 2021-02-09 15:42:57 -03:00
builtin-annotate.c perf annotate: Add itrace options support 2021-06-16 15:07:42 -03:00
builtin-bench.c
builtin-buildid-cache.c perf buildid-cache: Add --debuginfod option to specify a server to fetch debug files 2020-12-28 12:20:39 -03:00
builtin-buildid-list.c perf buildid-list: Initialize zstd_data 2021-05-17 20:39:17 -03:00
builtin-c2c.c perf c2c: Support record for hybrid platform 2021-06-01 11:06:35 -03:00
builtin-config.c
builtin-daemon.c Merge remote-tracking branch 'torvalds/master' into perf/core 2021-03-29 10:39:10 -03:00
builtin-data.c perf data: Add JSON export 2021-04-29 10:30:58 -03:00
builtin-diff.c perf tools: Fix various typos in comments 2021-03-23 17:13:43 -03:00
builtin-evlist.c perf evlist: Support pipe mode display 2020-12-17 14:36:17 -03:00
builtin-ftrace.c perf ftrace: Fix access to pid in array when setting a pid filter 2021-04-23 15:58:10 -03:00
builtin-help.c
builtin-inject.c perf inject: Do not inject BUILD_ID record if MMAP2 has it 2021-05-26 09:38:53 -03:00
builtin-kallsyms.c
builtin-kmem.c
builtin-kvm.c
builtin-list.c
builtin-lock.c perf tools: Fix various typos in comments 2021-03-23 17:13:43 -03:00
builtin-mem.c perf mem: Fix wrong verbose output for recording events 2021-06-01 11:05:30 -03:00
builtin-probe.c perf probe: Add --bootconfig to output definition in bootconfig format 2021-06-18 13:50:05 -03:00
builtin-record.c Merge remote-tracking branch 'torvalds/master' into perf/core 2021-06-22 13:56:50 -03:00
builtin-report.c perf mem: Disable 'mem-loads-aux' group before reporting 2021-06-01 11:06:01 -03:00
builtin-sched.c perf tools: Fix various typos in comments 2021-03-23 17:13:43 -03:00
builtin-script.c perf script: Add option to pass arguments to dlfilters 2021-07-01 16:14:37 -03:00
builtin-stat.c perf stat: Skip evlist__[enable|disable] when all events uses BPF 2021-05-21 16:50:17 -03:00
builtin-timechart.c
builtin-top.c perf top: Add cgroup support for perf top (-G) 2021-06-24 15:33:35 -03:00
builtin-trace.c perf trace: Fix race in signal handling 2021-03-06 16:54:32 -03:00
builtin-version.c
builtin.h perf daemon: Add daemon command 2021-02-09 15:42:57 -03:00
check-headers.sh perf tools: Copy uapi/asm/perf_regs.h from the kernel for MIPS 2021-06-01 10:07:46 -03:00
command-list.txt perf stat: Enable iostat mode for x86 platforms 2021-04-20 08:40:20 -03:00
CREDITS
design.txt
Makefile perf tools: Add a build-test variant to use in builds from a tarball 2021-04-20 08:43:58 -03:00
Makefile.config perf build: Install perf_dlfilter.h 2021-07-01 16:14:38 -03:00
Makefile.perf perf build: Install perf_dlfilter.h 2021-07-01 16:14:38 -03:00
MANIFEST scripts/bpf: Abstract eBPF API target parameter 2021-03-04 18:39:45 -08:00
perf-archive.sh perf archive: Fix filtering of empty build-ids 2021-03-06 16:54:31 -03:00
perf-completion.sh
perf-iostat.sh perf stat: Enable iostat mode for x86 platforms 2021-04-20 08:40:20 -03:00
perf-read-vdso.c
perf-sys.h
perf-with-kcore.sh
perf.c perf debug: Move debug initialization earlier 2021-05-27 13:24:22 -03:00
perf.h