Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 42cd165

Browse files
captain5050gregkh
authored andcommitted
perf callchain: Fix stitch LBR memory leaks
[ Upstream commit 599c193 ] The 'struct callchain_cursor_node' has a 'struct map_symbol' whose maps and map members are reference counted. Ensure these values use a _get routine to increment the reference counts and use map_symbol__exit() to release the reference counts. Do similar for 'struct thread's prev_lbr_cursor, but save the size of the prev_lbr_cursor array so that it may be iterated. Ensure that when stitch_nodes are placed on the free list the map_symbols are exited. Fix resolve_lbr_callchain_sample() by replacing list_replace_init() to list_splice_init(), so the whole list is moved and nodes aren't leaked. A reproduction of the memory leaks is possible with a leak sanitizer build in the perf report command of: ``` $ perf record -e cycles --call-graph lbr perf test -w thloop $ perf report --stitch-lbr ``` Reviewed-by: Kan Liang <kan.liang@linux.intel.com> Fixes: ff16562 ("perf callchain: Stitch LBR call stack") Signed-off-by: Ian Rogers <irogers@google.com> [ Basic tests after applying the patch, repeating the example above ] Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Anne Macedo <retpolanne@posteo.net> Cc: Changbin Du <changbin.du@huawei.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20240808054644.1286065-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e2955fb commit 42cd165

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

tools/perf/util/machine.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -2536,8 +2536,12 @@ static void save_lbr_cursor_node(struct thread *thread,
25362536
cursor->curr = cursor->first;
25372537
else
25382538
cursor->curr = cursor->curr->next;
2539+
2540+
map_symbol__exit(&lbr_stitch->prev_lbr_cursor[idx].ms);
25392541
memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
25402542
sizeof(struct callchain_cursor_node));
2543+
lbr_stitch->prev_lbr_cursor[idx].ms.maps = maps__get(cursor->curr->ms.maps);
2544+
lbr_stitch->prev_lbr_cursor[idx].ms.map = map__get(cursor->curr->ms.map);
25412545

25422546
lbr_stitch->prev_lbr_cursor[idx].valid = true;
25432547
cursor->pos++;
@@ -2748,6 +2752,9 @@ static bool has_stitched_lbr(struct thread *thread,
27482752
memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
27492753
sizeof(struct callchain_cursor_node));
27502754

2755+
stitch_node->cursor.ms.maps = maps__get(lbr_stitch->prev_lbr_cursor[i].ms.maps);
2756+
stitch_node->cursor.ms.map = map__get(lbr_stitch->prev_lbr_cursor[i].ms.map);
2757+
27512758
if (callee)
27522759
list_add(&stitch_node->node, &lbr_stitch->lists);
27532760
else
@@ -2771,6 +2778,8 @@ static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
27712778
if (!thread__lbr_stitch(thread)->prev_lbr_cursor)
27722779
goto free_lbr_stitch;
27732780

2781+
thread__lbr_stitch(thread)->prev_lbr_cursor_size = max_lbr + 1;
2782+
27742783
INIT_LIST_HEAD(&thread__lbr_stitch(thread)->lists);
27752784
INIT_LIST_HEAD(&thread__lbr_stitch(thread)->free_lists);
27762785

@@ -2826,8 +2835,12 @@ static int resolve_lbr_callchain_sample(struct thread *thread,
28262835
max_lbr, callee);
28272836

28282837
if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2829-
list_replace_init(&lbr_stitch->lists,
2830-
&lbr_stitch->free_lists);
2838+
struct stitch_list *stitch_node;
2839+
2840+
list_for_each_entry(stitch_node, &lbr_stitch->lists, node)
2841+
map_symbol__exit(&stitch_node->cursor.ms);
2842+
2843+
list_splice_init(&lbr_stitch->lists, &lbr_stitch->free_lists);
28312844
}
28322845
memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
28332846
}

tools/perf/util/thread.c

+4
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ void thread__free_stitch_list(struct thread *thread)
478478
return;
479479

480480
list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
481+
map_symbol__exit(&pos->cursor.ms);
481482
list_del_init(&pos->node);
482483
free(pos);
483484
}
@@ -487,6 +488,9 @@ void thread__free_stitch_list(struct thread *thread)
487488
free(pos);
488489
}
489490

491+
for (unsigned int i = 0 ; i < lbr_stitch->prev_lbr_cursor_size; i++)
492+
map_symbol__exit(&lbr_stitch->prev_lbr_cursor[i].ms);
493+
490494
zfree(&lbr_stitch->prev_lbr_cursor);
491495
free(thread__lbr_stitch(thread));
492496
thread__set_lbr_stitch(thread, NULL);

tools/perf/util/thread.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct lbr_stitch {
2828
struct list_head free_lists;
2929
struct perf_sample prev_sample;
3030
struct callchain_cursor_node *prev_lbr_cursor;
31+
unsigned int prev_lbr_cursor_size;
3132
};
3233

3334
struct thread_rb_node {

0 commit comments

Comments
 (0)