Skip to content

Commit 56e144f

Browse files
captain5050namhyung
authored andcommitted
perf mem_info: Add and use map_symbol__exit and addr_map_symbol__exit
Fix leak where mem_info__put wouldn't release the maps/map as used by perf mem. Add exit functions and use elsewhere that the maps and map are released. Signed-off-by: Ian Rogers <irogers@google.com> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: German Gomez <german.gomez@arm.com> Cc: James Clark <james.clark@arm.com> Cc: Nick Terrell <terrelln@fb.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Changbin Du <changbin.du@huawei.com> Cc: liuwenyu <liuwenyu7@huawei.com> Cc: Yang Jihong <yangjihong1@huawei.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Song Liu <song@kernel.org> Cc: Leo Yan <leo.yan@linaro.org> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Yanteng Si <siyanteng@loongson.cn> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Link: https://lore.kernel.org/r/20231024222353.3024098-12-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent dec07fe commit 56e144f

7 files changed

Lines changed: 47 additions & 39 deletions

File tree

tools/perf/util/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ perf-y += dso.o
4949
perf-y += dsos.o
5050
perf-y += symbol.o
5151
perf-y += symbol_fprintf.o
52+
perf-y += map_symbol.o
5253
perf-y += color.o
5354
perf-y += color_config.o
5455
perf-y += metricgroup.o

tools/perf/util/callchain.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,7 @@ add_child(struct callchain_node *parent,
659659

660660
list_for_each_entry_safe(call, tmp, &new->val, list) {
661661
list_del_init(&call->list);
662-
map__zput(call->ms.map);
663-
maps__zput(call->ms.maps);
662+
map_symbol__exit(&call->ms);
664663
zfree(&call->brtype_stat);
665664
free(call);
666665
}
@@ -1040,10 +1039,8 @@ merge_chain_branch(struct callchain_cursor *cursor,
10401039
};
10411040
callchain_cursor_append(cursor, list->ip, &ms, false, NULL, 0, 0, 0, list->srcline);
10421041
list_del_init(&list->list);
1043-
map__zput(ms.map);
1044-
maps__zput(ms.maps);
1045-
map__zput(list->ms.map);
1046-
maps__zput(list->ms.maps);
1042+
map_symbol__exit(&ms);
1043+
map_symbol__exit(&list->ms);
10471044
zfree(&list->brtype_stat);
10481045
free(list);
10491046
}
@@ -1096,8 +1093,7 @@ int callchain_cursor_append(struct callchain_cursor *cursor,
10961093
}
10971094

10981095
node->ip = ip;
1099-
maps__zput(node->ms.maps);
1100-
map__zput(node->ms.map);
1096+
map_symbol__exit(&node->ms);
11011097
node->ms = *ms;
11021098
node->ms.maps = maps__get(ms->maps);
11031099
node->ms.map = map__get(ms->map);
@@ -1496,16 +1492,14 @@ static void free_callchain_node(struct callchain_node *node)
14961492

14971493
list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
14981494
list_del_init(&list->list);
1499-
map__zput(list->ms.map);
1500-
maps__zput(list->ms.maps);
1495+
map_symbol__exit(&list->ms);
15011496
zfree(&list->brtype_stat);
15021497
free(list);
15031498
}
15041499

15051500
list_for_each_entry_safe(list, tmp, &node->val, list) {
15061501
list_del_init(&list->list);
1507-
map__zput(list->ms.map);
1508-
maps__zput(list->ms.maps);
1502+
map_symbol__exit(&list->ms);
15091503
zfree(&list->brtype_stat);
15101504
free(list);
15111505
}
@@ -1591,8 +1585,7 @@ int callchain_node__make_parent_list(struct callchain_node *node)
15911585
out:
15921586
list_for_each_entry_safe(chain, new, &head, list) {
15931587
list_del_init(&chain->list);
1594-
map__zput(chain->ms.map);
1595-
maps__zput(chain->ms.maps);
1588+
map_symbol__exit(&chain->ms);
15961589
zfree(&chain->brtype_stat);
15971590
free(chain);
15981591
}
@@ -1676,10 +1669,8 @@ void callchain_cursor_reset(struct callchain_cursor *cursor)
16761669
cursor->nr = 0;
16771670
cursor->last = &cursor->first;
16781671

1679-
for (node = cursor->first; node != NULL; node = node->next) {
1680-
map__zput(node->ms.map);
1681-
maps__zput(node->ms.maps);
1682-
}
1672+
for (node = cursor->first; node != NULL; node = node->next)
1673+
map_symbol__exit(&node->ms);
16831674
}
16841675

16851676
void callchain_param_setup(u64 sample_type, const char *arch)

tools/perf/util/hist.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -515,17 +515,16 @@ static int hist_entry__init(struct hist_entry *he,
515515

516516
err_infos:
517517
if (he->branch_info) {
518-
map__put(he->branch_info->from.ms.map);
519-
map__put(he->branch_info->to.ms.map);
518+
map_symbol__exit(&he->branch_info->from.ms);
519+
map_symbol__exit(&he->branch_info->to.ms);
520520
zfree(&he->branch_info);
521521
}
522522
if (he->mem_info) {
523-
map__put(he->mem_info->iaddr.ms.map);
524-
map__put(he->mem_info->daddr.ms.map);
523+
map_symbol__exit(&he->mem_info->iaddr.ms);
524+
map_symbol__exit(&he->mem_info->daddr.ms);
525525
}
526526
err:
527-
maps__zput(he->ms.maps);
528-
map__zput(he->ms.map);
527+
map_symbol__exit(&he->ms);
529528
zfree(&he->stat_acc);
530529
return -ENOMEM;
531530
}
@@ -1317,20 +1316,19 @@ void hist_entry__delete(struct hist_entry *he)
13171316
struct hist_entry_ops *ops = he->ops;
13181317

13191318
thread__zput(he->thread);
1320-
maps__zput(he->ms.maps);
1321-
map__zput(he->ms.map);
1319+
map_symbol__exit(&he->ms);
13221320

13231321
if (he->branch_info) {
1324-
map__zput(he->branch_info->from.ms.map);
1325-
map__zput(he->branch_info->to.ms.map);
1322+
map_symbol__exit(&he->branch_info->from.ms);
1323+
map_symbol__exit(&he->branch_info->to.ms);
13261324
zfree_srcline(&he->branch_info->srcline_from);
13271325
zfree_srcline(&he->branch_info->srcline_to);
13281326
zfree(&he->branch_info);
13291327
}
13301328

13311329
if (he->mem_info) {
1332-
map__zput(he->mem_info->iaddr.ms.map);
1333-
map__zput(he->mem_info->daddr.ms.map);
1330+
map_symbol__exit(&he->mem_info->iaddr.ms);
1331+
map_symbol__exit(&he->mem_info->daddr.ms);
13341332
mem_info__zput(he->mem_info);
13351333
}
13361334

@@ -2700,10 +2698,8 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
27002698
*total_cycles += bi[i].flags.cycles;
27012699
}
27022700
for (unsigned int i = 0; i < bs->nr; i++) {
2703-
map__put(bi[i].to.ms.map);
2704-
maps__put(bi[i].to.ms.maps);
2705-
map__put(bi[i].from.ms.map);
2706-
maps__put(bi[i].from.ms.maps);
2701+
map_symbol__exit(&bi[i].to.ms);
2702+
map_symbol__exit(&bi[i].from.ms);
27072703
}
27082704
free(bi);
27092705
}

tools/perf/util/machine.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,8 +2389,7 @@ static int add_callchain_ip(struct thread *thread,
23892389
iter_cycles, branch_from, srcline);
23902390
out:
23912391
addr_location__exit(&al);
2392-
maps__put(ms.maps);
2393-
map__put(ms.map);
2392+
map_symbol__exit(&ms);
23942393
return err;
23952394
}
23962395

@@ -3116,8 +3115,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms
31163115
if (ret != 0)
31173116
return ret;
31183117
}
3119-
map__put(ilist_ms.map);
3120-
maps__put(ilist_ms.maps);
3118+
map_symbol__exit(&ilist_ms);
31213119

31223120
return ret;
31233121
}

tools/perf/util/map_symbol.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "map_symbol.h"
3+
#include "maps.h"
4+
#include "map.h"
5+
6+
void map_symbol__exit(struct map_symbol *ms)
7+
{
8+
maps__zput(ms->maps);
9+
map__zput(ms->map);
10+
}
11+
12+
void addr_map_symbol__exit(struct addr_map_symbol *ams)
13+
{
14+
map_symbol__exit(&ams->ms);
15+
}

tools/perf/util/map_symbol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ struct addr_map_symbol {
2222
u64 phys_addr;
2323
u64 data_page_size;
2424
};
25+
26+
void map_symbol__exit(struct map_symbol *ms);
27+
void addr_map_symbol__exit(struct addr_map_symbol *ams);
28+
2529
#endif // __PERF_MAP_SYMBOL

tools/perf/util/symbol.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2791,8 +2791,11 @@ struct mem_info *mem_info__get(struct mem_info *mi)
27912791

27922792
void mem_info__put(struct mem_info *mi)
27932793
{
2794-
if (mi && refcount_dec_and_test(&mi->refcnt))
2794+
if (mi && refcount_dec_and_test(&mi->refcnt)) {
2795+
addr_map_symbol__exit(&mi->iaddr);
2796+
addr_map_symbol__exit(&mi->daddr);
27952797
free(mi);
2798+
}
27962799
}
27972800

27982801
struct mem_info *mem_info__new(void)

0 commit comments

Comments
 (0)