Skip to content

Commit 4f74f18

Browse files
captain5050acmel
authored andcommitted
perf symbols: Factor out annotation init/exit
The exit function fixes a memory leak with the src field as detected by leak sanitizer. An example of which is: Indirect leak of 25133184 byte(s) in 207 object(s) allocated from: #0 0x7f199ecfe987 in __interceptor_calloc libsanitizer/asan/asan_malloc_linux.cpp:154 Rust-for-Linux#1 0x55defe638224 in annotated_source__alloc_histograms util/annotate.c:803 Rust-for-Linux#2 0x55defe6397e4 in symbol__hists util/annotate.c:952 Rust-for-Linux#3 0x55defe639908 in symbol__inc_addr_samples util/annotate.c:968 Rust-for-Linux#4 0x55defe63aa29 in hist_entry__inc_addr_samples util/annotate.c:1119 Rust-for-Linux#5 0x55defe499a79 in hist_iter__report_callback tools/perf/builtin-report.c:182 Rust-for-Linux#6 0x55defe7a859d in hist_entry_iter__add util/hist.c:1236 Rust-for-Linux#7 0x55defe49aa63 in process_sample_event tools/perf/builtin-report.c:315 Rust-for-Linux#8 0x55defe731bc8 in evlist__deliver_sample util/session.c:1473 Rust-for-Linux#9 0x55defe731e38 in machines__deliver_event util/session.c:1510 Rust-for-Linux#10 0x55defe732a23 in perf_session__deliver_event util/session.c:1590 Rust-for-Linux#11 0x55defe72951e in ordered_events__deliver_event util/session.c:183 Rust-for-Linux#12 0x55defe740082 in do_flush util/ordered-events.c:244 Rust-for-Linux#13 0x55defe7407cb in __ordered_events__flush util/ordered-events.c:323 Rust-for-Linux#14 0x55defe740a61 in ordered_events__flush util/ordered-events.c:341 Rust-for-Linux#15 0x55defe73837f in __perf_session__process_events util/session.c:2390 Rust-for-Linux#16 0x55defe7385ff in perf_session__process_events util/session.c:2420 ... Signed-off-by: Ian Rogers <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Martin Liška <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 4270456 commit 4f74f18

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

tools/perf/util/annotate.c

+11
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,17 @@ int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool r
12551255
return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
12561256
}
12571257

1258+
void annotation__init(struct annotation *notes)
1259+
{
1260+
pthread_mutex_init(&notes->lock, NULL);
1261+
}
1262+
1263+
void annotation__exit(struct annotation *notes)
1264+
{
1265+
annotated_source__delete(notes->src);
1266+
pthread_mutex_destroy(&notes->lock);
1267+
}
1268+
12581269
static void annotation_line__add(struct annotation_line *al, struct list_head *head)
12591270
{
12601271
list_add_tail(&al->node, head);

tools/perf/util/annotate.h

+3
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ struct annotation {
299299
struct annotated_source *src;
300300
};
301301

302+
void annotation__init(struct annotation *notes);
303+
void annotation__exit(struct annotation *notes);
304+
302305
static inline int annotation__cycles_width(struct annotation *notes)
303306
{
304307
if (notes->have_cycles && notes->options->show_minmax_cycle)

tools/perf/util/symbol.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
274274
if (symbol_conf.priv_size) {
275275
if (symbol_conf.init_annotation) {
276276
struct annotation *notes = (void *)sym;
277-
pthread_mutex_init(&notes->lock, NULL);
277+
annotation__init(notes);
278278
}
279279
sym = ((void *)sym) + symbol_conf.priv_size;
280280
}
@@ -294,6 +294,13 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
294294

295295
void symbol__delete(struct symbol *sym)
296296
{
297+
if (symbol_conf.priv_size) {
298+
if (symbol_conf.init_annotation) {
299+
struct annotation *notes = symbol__annotation(sym);
300+
301+
annotation__exit(notes);
302+
}
303+
}
297304
free(((void *)sym) - symbol_conf.priv_size);
298305
}
299306

0 commit comments

Comments
 (0)