From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Brian Robbins <brianrob@linux.microsoft.com>,
Yuan Can <yuancan@huawei.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] perf jit: Fix a few memory leaks
Date: Mon, 3 Apr 2023 18:50:10 -0300 [thread overview]
Message-ID: <ZCtKEuFCPGvHX1FI@kernel.org> (raw)
In-Reply-To: <20230403203545.1872196-1-irogers@google.com>
Em Mon, Apr 03, 2023 at 01:35:45PM -0700, Ian Rogers escreveu:
> As reported by leak sanitizer.
Thanks, applied.
- Arnaldo
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tools/perf/util/genelf_debug.c | 45 +++++++++++++++++++++-------------
> tools/perf/util/jitdump.c | 7 +++++-
> 2 files changed, 34 insertions(+), 18 deletions(-)
>
> diff --git a/tools/perf/util/genelf_debug.c b/tools/perf/util/genelf_debug.c
> index dd40683bd4c0..8786c366566e 100644
> --- a/tools/perf/util/genelf_debug.c
> +++ b/tools/perf/util/genelf_debug.c
> @@ -87,6 +87,12 @@ buffer_ext_init(struct buffer_ext *be)
> be->max_sz = 0;
> }
>
> +static void
> +buffer_ext_exit(struct buffer_ext *be)
> +{
> + free(be->data);
> +}
> +
> static inline size_t
> buffer_ext_size(struct buffer_ext *be)
> {
> @@ -487,28 +493,28 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> Elf_Scn *scn;
> Elf_Shdr *shdr;
> struct buffer_ext dl, di, da;
> - int ret;
> + int ret = -1;
>
> buffer_ext_init(&dl);
> buffer_ext_init(&di);
> buffer_ext_init(&da);
>
> - ret = jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di);
> - if (ret)
> - return -1;
> + if (jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di))
> + goto out;
> +
> /*
> * setup .debug_line section
> */
> scn = elf_newscn(e);
> if (!scn) {
> warnx("cannot create section");
> - return -1;
> + goto out;
> }
>
> d = elf_newdata(scn);
> if (!d) {
> warnx("cannot get new data");
> - return -1;
> + goto out;
> }
>
> d->d_align = 1;
> @@ -521,7 +527,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> shdr = elf_getshdr(scn);
> if (!shdr) {
> warnx("cannot get section header");
> - return -1;
> + goto out;
> }
>
> shdr->sh_name = 52; /* .debug_line */
> @@ -536,13 +542,13 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> scn = elf_newscn(e);
> if (!scn) {
> warnx("cannot create section");
> - return -1;
> + goto out;
> }
>
> d = elf_newdata(scn);
> if (!d) {
> warnx("cannot get new data");
> - return -1;
> + goto out;
> }
>
> d->d_align = 1;
> @@ -555,7 +561,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> shdr = elf_getshdr(scn);
> if (!shdr) {
> warnx("cannot get section header");
> - return -1;
> + goto out;
> }
>
> shdr->sh_name = 64; /* .debug_info */
> @@ -570,13 +576,13 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> scn = elf_newscn(e);
> if (!scn) {
> warnx("cannot create section");
> - return -1;
> + goto out;
> }
>
> d = elf_newdata(scn);
> if (!d) {
> warnx("cannot get new data");
> - return -1;
> + goto out;
> }
>
> d->d_align = 1;
> @@ -589,7 +595,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> shdr = elf_getshdr(scn);
> if (!shdr) {
> warnx("cannot get section header");
> - return -1;
> + goto out;
> }
>
> shdr->sh_name = 76; /* .debug_info */
> @@ -601,9 +607,14 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
> /*
> * now we update the ELF image with all the sections
> */
> - if (elf_update(e, ELF_C_WRITE) < 0) {
> + if (elf_update(e, ELF_C_WRITE) < 0)
> warnx("elf_update debug failed");
> - return -1;
> - }
> - return 0;
> + else
> + ret = 0;
> +
> +out:
> + buffer_ext_exit(&dl);
> + buffer_ext_exit(&di);
> + buffer_ext_exit(&da);
> + return ret;
> }
> diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
> index 0e033278fa12..28e49502db5e 100644
> --- a/tools/perf/util/jitdump.c
> +++ b/tools/perf/util/jitdump.c
> @@ -235,9 +235,11 @@ jit_open(struct jit_buf_desc *jd, const char *name)
> */
> strcpy(jd->dir, name);
> dirname(jd->dir);
> + free(buf);
>
> return 0;
> error:
> + free(buf);
> funlockfile(jd->in);
> fclose(jd->in);
> return retval;
> @@ -523,7 +525,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
>
> ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
> if (ret)
> - return ret;
> + goto out;
>
> ret = jit_inject_event(jd, event);
> /*
> @@ -532,6 +534,8 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
> if (!ret)
> build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
>
> +out:
> + free(event);
> return ret;
> }
>
> @@ -874,6 +878,7 @@ jit_process(struct perf_session *session,
> }
>
> nsinfo__put(jd.nsi);
> + free(jd.buf);
>
> return ret;
> }
> --
> 2.40.0.348.gf938b09366-goog
>
--
- Arnaldo
prev parent reply other threads:[~2023-04-03 21:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-03 20:35 [PATCH v2] perf jit: Fix a few memory leaks Ian Rogers
2023-04-03 21:50 ` Arnaldo Carvalho de Melo [this message]
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=ZCtKEuFCPGvHX1FI@kernel.org \
--to=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=brianrob@linux.microsoft.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=yuancan@huawei.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.