From: Namhyung Kim <namhyung@kernel.org>
To: Zhongqiu Han <quic_zhonhan@quicinc.com>
Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
kan.liang@linux.intel.com, james.clark@linaro.org,
yangyicong@hisilicon.com, song@kernel.org,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
bpf@vger.kernel.org
Subject: Re: [PATCH 3/3] perf bpf: Fix two memory leakages when calling perf_env__insert_bpf_prog_info()
Date: Mon, 2 Dec 2024 14:02:17 -0800 [thread overview]
Message-ID: <Z04uaWQxI3LXfAtg@google.com> (raw)
In-Reply-To: <20241128125432.2748981-4-quic_zhonhan@quicinc.com>
Hello,
On Thu, Nov 28, 2024 at 08:54:32PM +0800, Zhongqiu Han wrote:
> If perf_env__insert_bpf_prog_info() returns false due to a duplicate bpf
> prog info node insertion, the temporary info_node and info_linear memory
> will leak. Add a check to ensure the memory is freed if the function
> returns false.
>
> Fixes: 9c51f8788b5d ("perf env: Avoid recursively taking env->bpf_progs.lock")
> Signed-off-by: Zhongqiu Han <quic_zhonhan@quicinc.com>
> ---
> tools/perf/util/bpf-event.c | 10 ++++++++--
> tools/perf/util/env.c | 7 +++++--
> tools/perf/util/env.h | 2 +-
> 3 files changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c
> index 13608237c50e..c81444059ad0 100644
> --- a/tools/perf/util/bpf-event.c
> +++ b/tools/perf/util/bpf-event.c
> @@ -289,7 +289,10 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
> }
>
> info_node->info_linear = info_linear;
> - perf_env__insert_bpf_prog_info(env, info_node);
> + if (!perf_env__insert_bpf_prog_info(env, info_node)) {
> + free(info_linear);
> + free(info_node);
> + }
> info_linear = NULL;
>
> /*
> @@ -480,7 +483,10 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
> info_node = malloc(sizeof(struct bpf_prog_info_node));
> if (info_node) {
> info_node->info_linear = info_linear;
> - perf_env__insert_bpf_prog_info(env, info_node);
> + if (!perf_env__insert_bpf_prog_info(env, info_node)) {
> + free(info_linear);
> + free(info_node);
> + }
> } else
> free(info_linear);
>
> diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
> index d7865ae5f8f5..38401a289c24 100644
> --- a/tools/perf/util/env.c
> +++ b/tools/perf/util/env.c
> @@ -24,12 +24,15 @@ struct perf_env perf_env;
> #include "bpf-utils.h"
> #include <bpf/libbpf.h>
>
> -void perf_env__insert_bpf_prog_info(struct perf_env *env,
> +bool perf_env__insert_bpf_prog_info(struct perf_env *env,
> struct bpf_prog_info_node *info_node)
> {
> + bool ret = true;
Please add a blank line between declaration and the other statements.
Also I think you can just use the return value of the internal function
instead of initializaing it to true.
Thanks,
Namhyung
> down_write(&env->bpf_progs.lock);
> - __perf_env__insert_bpf_prog_info(env, info_node);
> + if (!__perf_env__insert_bpf_prog_info(env, info_node))
> + ret = false;
> up_write(&env->bpf_progs.lock);
> + return ret;
> }
>
> bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node)
> diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h
> index 9db2e5a625ed..da11add761d0 100644
> --- a/tools/perf/util/env.h
> +++ b/tools/perf/util/env.h
> @@ -178,7 +178,7 @@ int perf_env__nr_cpus_avail(struct perf_env *env);
> void perf_env__init(struct perf_env *env);
> bool __perf_env__insert_bpf_prog_info(struct perf_env *env,
> struct bpf_prog_info_node *info_node);
> -void perf_env__insert_bpf_prog_info(struct perf_env *env,
> +bool perf_env__insert_bpf_prog_info(struct perf_env *env,
> struct bpf_prog_info_node *info_node);
> struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
> __u32 prog_id);
> --
> 2.25.1
>
next prev parent reply other threads:[~2024-12-02 22:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-28 12:54 [PATCH 0/3] perf tool: Fix multiple memory leakages Zhongqiu Han
2024-11-28 12:54 ` [PATCH 1/3] perf header: Fix one memory leakage in process_bpf_btf() Zhongqiu Han
2024-11-28 12:54 ` [PATCH 2/3] perf header: Fix one memory leakage in process_bpf_prog_info() Zhongqiu Han
2024-11-28 12:54 ` [PATCH 3/3] perf bpf: Fix two memory leakages when calling perf_env__insert_bpf_prog_info() Zhongqiu Han
2024-12-02 22:02 ` Namhyung Kim [this message]
2024-12-03 10:45 ` Zhongqiu Han
2024-12-02 22:04 ` [PATCH 0/3] perf tool: Fix multiple memory leakages Namhyung Kim
2024-12-03 10:41 ` Zhongqiu Han
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=Z04uaWQxI3LXfAtg@google.com \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bpf@vger.kernel.org \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=quic_zhonhan@quicinc.com \
--cc=song@kernel.org \
--cc=yangyicong@hisilicon.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.