From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
Kan Liang <kan.liang@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
linux-perf-users@vger.kernel.org,
Ravi Bangoria <ravi.bangoria@amd.com>
Subject: Re: [PATCH] perf report: Fix a memory leak for perf_env on AMD
Date: Tue, 11 Mar 2025 19:16:46 -0700 [thread overview]
Message-ID: <Z9DujuhfE19eeXEj@google.com> (raw)
In-Reply-To: <CAP-5=fWSU8m1eae=5GFTpVTz1CPtwv=eDmivT1v14VjT40SptA@mail.gmail.com>
On Mon, Mar 10, 2025 at 05:49:48PM -0700, Ian Rogers wrote:
> On Mon, Mar 10, 2025 at 5:04 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > The env.pmu_mapping can be leaked when it reads data from a pipe on AMD.
> > For a pipe data, it reads the header data including pmu_mapping from
> > PERF_RECORD_HEADER_FEATURE runtime. But it's already set in:
> >
> > perf_session__new()
> > __perf_session__new()
> > evlist__init_trace_event_sample_raw()
> > evlist__has_amd_ibs()
> > perf_env__nr_pmu_mappings()
> >
> > Then it'll overwrite that when it processes the HEADER_FEATURE record.
> > Here's a report from address sanitizer.
> >
> > Direct leak of 2689 byte(s) in 1 object(s) allocated from:
> > #0 0x7fed8f814596 in realloc ../../../../src/libsanitizer/lsan/lsan_interceptors.cpp:98
> > #1 0x5595a7d416b1 in strbuf_grow util/strbuf.c:64
> > #2 0x5595a7d414ef in strbuf_init util/strbuf.c:25
> > #3 0x5595a7d0f4b7 in perf_env__read_pmu_mappings util/env.c:362
> > #4 0x5595a7d12ab7 in perf_env__nr_pmu_mappings util/env.c:517
> > #5 0x5595a7d89d2f in evlist__has_amd_ibs util/amd-sample-raw.c:315
> > #6 0x5595a7d87fb2 in evlist__init_trace_event_sample_raw util/sample-raw.c:23
> > #7 0x5595a7d7f893 in __perf_session__new util/session.c:179
> > #8 0x5595a7b79572 in perf_session__new util/session.h:115
> > #9 0x5595a7b7e9dc in cmd_report builtin-report.c:1603
> > #10 0x5595a7c019eb in run_builtin perf.c:351
> > #11 0x5595a7c01c92 in handle_internal_command perf.c:404
> > #12 0x5595a7c01deb in run_argv perf.c:448
> > #13 0x5595a7c02134 in main perf.c:556
> > #14 0x7fed85833d67 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
> >
> > Let's free the existing pmu_mapping data if any.
>
> Can we simplify `evlist__has_amd_ibs` as there seems no reason to be
> creating/parsing strings. For example:
> ```
> bool evlist__has_amd_ibs(struct evlist *evlist)
> {
> return perf_pmus__find("ibs_op") != NULL ||
> perf_pmus__find("ibs_fetch") != NULL;
> }
> ```
I think the idea is to find the pmu from the recorded data file not
at runtime.
> Similarly the variables `ibs_fetch_type` and `ibs_op_type` can be
> found with `perf_pmus__find("ibs_op")->type` and
> `perf_pmus__find("ibs_op")->type` respectively. It probably makes more
> sense to just cache access to the PMUs rather than do the finds all
> the time.
>
> I wonder there is a problem with the pmus alternative that currently
> the pmus are global and not part of say the session, not loaded from
> the perf.data file, etc. So use on a different machine than the
> recording could be broken by what I suggest. I guess then, just
> throwing in the free is simplest.
Agreed. It could be saved in a session or env.
For now, let's free the string to fix the leak quickly.
Thanks,
Namhyung
>
>
> > Cc: Ravi Bangoria <ravi.bangoria@amd.com>
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> > tools/perf/util/header.c | 2 ++
> > 1 file changed, 2 insertions(+)
> >
> > diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> > index 1900965f87527948..e3cdc3b7b4ab2409 100644
> > --- a/tools/perf/util/header.c
> > +++ b/tools/perf/util/header.c
> > @@ -2770,6 +2770,8 @@ static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
> > free(name);
> > pmu_num--;
> > }
> > + /* AMD may set it by evlist__has_amd_ibs() from perf_session__new() */
> > + free(ff->ph->env.pmu_mappings);
> > ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
> > return 0;
> >
> > --
> > 2.49.0.rc0.332.g42c0ae87b1-goog
> >
next prev parent reply other threads:[~2025-03-12 2:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-11 0:04 [PATCH] perf report: Fix a memory leak for perf_env on AMD Namhyung Kim
2025-03-11 0:49 ` Ian Rogers
2025-03-12 2:16 ` Namhyung Kim [this message]
2025-03-25 19:49 ` Namhyung Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z9DujuhfE19eeXEj@google.com \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.