perf mem_info: Add and use map_symbol__exit and addr_map_symbol__exit
authorIan Rogers <irogers@google.com>
Tue, 24 Oct 2023 22:23:14 +0000 (15:23 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Wed, 25 Oct 2023 20:39:58 +0000 (13:39 -0700)
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>
tools/perf/util/Build
tools/perf/util/callchain.c
tools/perf/util/hist.c
tools/perf/util/machine.c
tools/perf/util/map_symbol.c [new file with mode: 0644]
tools/perf/util/map_symbol.h
tools/perf/util/symbol.c

index 0ea5a9d368d49c7d29cacc96ba23d1d9a4ccd173..96058f949ec9fcef5781dcb09664ded909185e70 100644 (file)
@@ -49,6 +49,7 @@ perf-y += dso.o
 perf-y += dsos.o
 perf-y += symbol.o
 perf-y += symbol_fprintf.o
+perf-y += map_symbol.o
 perf-y += color.o
 perf-y += color_config.o
 perf-y += metricgroup.o
index 5349c6a218491df61804ce4b6c9a0af12be34453..99cce43ba152bca434210d49dce5c810d83aac99 100644 (file)
@@ -659,8 +659,7 @@ add_child(struct callchain_node *parent,
 
                list_for_each_entry_safe(call, tmp, &new->val, list) {
                        list_del_init(&call->list);
-                       map__zput(call->ms.map);
-                       maps__zput(call->ms.maps);
+                       map_symbol__exit(&call->ms);
                        zfree(&call->brtype_stat);
                        free(call);
                }
@@ -1040,10 +1039,8 @@ merge_chain_branch(struct callchain_cursor *cursor,
                };
                callchain_cursor_append(cursor, list->ip, &ms, false, NULL, 0, 0, 0, list->srcline);
                list_del_init(&list->list);
-               map__zput(ms.map);
-               maps__zput(ms.maps);
-               map__zput(list->ms.map);
-               maps__zput(list->ms.maps);
+               map_symbol__exit(&ms);
+               map_symbol__exit(&list->ms);
                zfree(&list->brtype_stat);
                free(list);
        }
@@ -1096,8 +1093,7 @@ int callchain_cursor_append(struct callchain_cursor *cursor,
        }
 
        node->ip = ip;
-       maps__zput(node->ms.maps);
-       map__zput(node->ms.map);
+       map_symbol__exit(&node->ms);
        node->ms = *ms;
        node->ms.maps = maps__get(ms->maps);
        node->ms.map = map__get(ms->map);
@@ -1496,16 +1492,14 @@ static void free_callchain_node(struct callchain_node *node)
 
        list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
                list_del_init(&list->list);
-               map__zput(list->ms.map);
-               maps__zput(list->ms.maps);
+               map_symbol__exit(&list->ms);
                zfree(&list->brtype_stat);
                free(list);
        }
 
        list_for_each_entry_safe(list, tmp, &node->val, list) {
                list_del_init(&list->list);
-               map__zput(list->ms.map);
-               maps__zput(list->ms.maps);
+               map_symbol__exit(&list->ms);
                zfree(&list->brtype_stat);
                free(list);
        }
@@ -1591,8 +1585,7 @@ int callchain_node__make_parent_list(struct callchain_node *node)
 out:
        list_for_each_entry_safe(chain, new, &head, list) {
                list_del_init(&chain->list);
-               map__zput(chain->ms.map);
-               maps__zput(chain->ms.maps);
+               map_symbol__exit(&chain->ms);
                zfree(&chain->brtype_stat);
                free(chain);
        }
@@ -1676,10 +1669,8 @@ void callchain_cursor_reset(struct callchain_cursor *cursor)
        cursor->nr = 0;
        cursor->last = &cursor->first;
 
-       for (node = cursor->first; node != NULL; node = node->next) {
-               map__zput(node->ms.map);
-               maps__zput(node->ms.maps);
-       }
+       for (node = cursor->first; node != NULL; node = node->next)
+               map_symbol__exit(&node->ms);
 }
 
 void callchain_param_setup(u64 sample_type, const char *arch)
index 0aa7e231172acbdd0b50904fb3817af8c0538932..0888b7163b7cc25c33724f4e61099ab16a2ab60a 100644 (file)
@@ -515,17 +515,16 @@ err_rawdata:
 
 err_infos:
        if (he->branch_info) {
-               map__put(he->branch_info->from.ms.map);
-               map__put(he->branch_info->to.ms.map);
+               map_symbol__exit(&he->branch_info->from.ms);
+               map_symbol__exit(&he->branch_info->to.ms);
                zfree(&he->branch_info);
        }
        if (he->mem_info) {
-               map__put(he->mem_info->iaddr.ms.map);
-               map__put(he->mem_info->daddr.ms.map);
+               map_symbol__exit(&he->mem_info->iaddr.ms);
+               map_symbol__exit(&he->mem_info->daddr.ms);
        }
 err:
-       maps__zput(he->ms.maps);
-       map__zput(he->ms.map);
+       map_symbol__exit(&he->ms);
        zfree(&he->stat_acc);
        return -ENOMEM;
 }
@@ -1317,20 +1316,19 @@ void hist_entry__delete(struct hist_entry *he)
        struct hist_entry_ops *ops = he->ops;
 
        thread__zput(he->thread);
-       maps__zput(he->ms.maps);
-       map__zput(he->ms.map);
+       map_symbol__exit(&he->ms);
 
        if (he->branch_info) {
-               map__zput(he->branch_info->from.ms.map);
-               map__zput(he->branch_info->to.ms.map);
+               map_symbol__exit(&he->branch_info->from.ms);
+               map_symbol__exit(&he->branch_info->to.ms);
                zfree_srcline(&he->branch_info->srcline_from);
                zfree_srcline(&he->branch_info->srcline_to);
                zfree(&he->branch_info);
        }
 
        if (he->mem_info) {
-               map__zput(he->mem_info->iaddr.ms.map);
-               map__zput(he->mem_info->daddr.ms.map);
+               map_symbol__exit(&he->mem_info->iaddr.ms);
+               map_symbol__exit(&he->mem_info->daddr.ms);
                mem_info__zput(he->mem_info);
        }
 
@@ -2700,10 +2698,8 @@ void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
                                        *total_cycles += bi[i].flags.cycles;
                        }
                        for (unsigned int i = 0; i < bs->nr; i++) {
-                               map__put(bi[i].to.ms.map);
-                               maps__put(bi[i].to.ms.maps);
-                               map__put(bi[i].from.ms.map);
-                               maps__put(bi[i].from.ms.maps);
+                               map_symbol__exit(&bi[i].to.ms);
+                               map_symbol__exit(&bi[i].from.ms);
                        }
                        free(bi);
                }
index 31fdbb0f27afccfe37fe6aa7e07be5abab3681b9..90c750150b19bdbc0192c5bdc0fd306a2554aeca 100644 (file)
@@ -2389,8 +2389,7 @@ static int add_callchain_ip(struct thread *thread,
                                      iter_cycles, branch_from, srcline);
 out:
        addr_location__exit(&al);
-       maps__put(ms.maps);
-       map__put(ms.map);
+       map_symbol__exit(&ms);
        return err;
 }
 
@@ -3116,8 +3115,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms
                if (ret != 0)
                        return ret;
        }
-       map__put(ilist_ms.map);
-       maps__put(ilist_ms.maps);
+       map_symbol__exit(&ilist_ms);
 
        return ret;
 }
diff --git a/tools/perf/util/map_symbol.c b/tools/perf/util/map_symbol.c
new file mode 100644 (file)
index 0000000..bef5079
--- /dev/null
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "map_symbol.h"
+#include "maps.h"
+#include "map.h"
+
+void map_symbol__exit(struct map_symbol *ms)
+{
+       maps__zput(ms->maps);
+       map__zput(ms->map);
+}
+
+void addr_map_symbol__exit(struct addr_map_symbol *ams)
+{
+       map_symbol__exit(&ams->ms);
+}
index e08817b0c30fdb6bd2294958e8942834b59ce0c4..72d5ed938ed66e7f5e5d4cf7a95328a8406084a9 100644 (file)
@@ -22,4 +22,8 @@ struct addr_map_symbol {
        u64           phys_addr;
        u64           data_page_size;
 };
+
+void map_symbol__exit(struct map_symbol *ms);
+void addr_map_symbol__exit(struct addr_map_symbol *ams);
+
 #endif // __PERF_MAP_SYMBOL
index 822f4dcebfe66d8f7ca6b28e3f8992d6a76d1067..82cc74b9358e0de701622378b1cffb9c0047adb6 100644 (file)
@@ -2791,8 +2791,11 @@ struct mem_info *mem_info__get(struct mem_info *mi)
 
 void mem_info__put(struct mem_info *mi)
 {
-       if (mi && refcount_dec_and_test(&mi->refcnt))
+       if (mi && refcount_dec_and_test(&mi->refcnt)) {
+               addr_map_symbol__exit(&mi->iaddr);
+               addr_map_symbol__exit(&mi->daddr);
                free(mi);
+       }
 }
 
 struct mem_info *mem_info__new(void)