From f1e8f25980968f7c18f1eb977bd1d5fd716325a5 Mon Sep 17 00:00:00 2001 From: Leo Yan Date: Mon, 20 Mar 2023 14:16:18 +0800 Subject: perf kvm: Reference count 'struct kvm_info' hists__add_entry_ops() doesn't allocate a new histogram entry if it has an existing entry for a KVM event, in this case, find_create_kvm_event() allocates a 'struct kvm_info' but it's not used by any histograms and never freed. To fix the memory leak, this patch first introduces a refcnt and a set of functions for refcnt operations on 'struct kvm_info'. When the data structure is not anymore used (the refcnt hits zero) kvm_info__zput() will free the memory used. Committer: Provide a nop version of kvm_info__zput() to be used when HAVE_KVM_STAT_SUPPORT isn't defined as it is used unconditionally in hists__findnew_entry() and hist_entry__delete(). Signed-off-by: Leo Yan Acked-by: Ian Rogers Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20230320061619.29520-2-leo.yan@linaro.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tools/perf/builtin-kvm.c') diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c index 4c205df5106f..1e1cb5a9d0a2 100644 --- a/tools/perf/builtin-kvm.c +++ b/tools/perf/builtin-kvm.c @@ -768,7 +768,6 @@ static void kvm_he_free(void *he) { struct kvm_event *kvm_ev; - free(((struct hist_entry *)he)->kvm_info); kvm_ev = container_of(he, struct kvm_event, he); free(kvm_ev); } @@ -788,7 +787,7 @@ static struct kvm_event *find_create_kvm_event(struct perf_kvm_stat *kvm, BUG_ON(key->key == INVALID_KEY); - ki = zalloc(sizeof(*ki)); + ki = kvm_info__new(); if (!ki) { pr_err("Failed to allocate kvm info\n"); return NULL; -- cgit v1.2.3