Commit 4924b1f7 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf bpf: Avoid memory leak from perf_env__insert_btf()

perf_env__insert_btf() doesn't insert if a duplicate BTF id is
encountered and this causes a memory leak. Modify the function to return
a success/error value and then free the memory if insertion didn't
happen.

v2. Adds a return -1 when the insertion error occurs in
    perf_env__fetch_btf. This doesn't affect anything as the result is
    never checked.

Fixes: 3792cb2f ("perf bpf: Save BTF in a rbtree in perf_env")
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org
Link: http://lore.kernel.org/lkml/20211112074525.121633-1-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 4f74f187
...@@ -119,7 +119,11 @@ static int perf_env__fetch_btf(struct perf_env *env, ...@@ -119,7 +119,11 @@ static int perf_env__fetch_btf(struct perf_env *env,
node->data_size = data_size; node->data_size = data_size;
memcpy(node->data, data, data_size); memcpy(node->data, data, data_size);
perf_env__insert_btf(env, node); if (!perf_env__insert_btf(env, node)) {
/* Insertion failed because of a duplicate. */
free(node);
return -1;
}
return 0; return 0;
} }
......
...@@ -75,12 +75,13 @@ struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, ...@@ -75,12 +75,13 @@ struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
return node; return node;
} }
void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
{ {
struct rb_node *parent = NULL; struct rb_node *parent = NULL;
__u32 btf_id = btf_node->id; __u32 btf_id = btf_node->id;
struct btf_node *node; struct btf_node *node;
struct rb_node **p; struct rb_node **p;
bool ret = true;
down_write(&env->bpf_progs.lock); down_write(&env->bpf_progs.lock);
p = &env->bpf_progs.btfs.rb_node; p = &env->bpf_progs.btfs.rb_node;
...@@ -94,6 +95,7 @@ void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) ...@@ -94,6 +95,7 @@ void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
p = &(*p)->rb_right; p = &(*p)->rb_right;
} else { } else {
pr_debug("duplicated btf %u\n", btf_id); pr_debug("duplicated btf %u\n", btf_id);
ret = false;
goto out; goto out;
} }
} }
...@@ -103,6 +105,7 @@ void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) ...@@ -103,6 +105,7 @@ void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
env->bpf_progs.btfs_cnt++; env->bpf_progs.btfs_cnt++;
out: out:
up_write(&env->bpf_progs.lock); up_write(&env->bpf_progs.lock);
return ret;
} }
struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id) struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id)
......
...@@ -167,7 +167,7 @@ void perf_env__insert_bpf_prog_info(struct perf_env *env, ...@@ -167,7 +167,7 @@ void perf_env__insert_bpf_prog_info(struct perf_env *env,
struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *info_node);
struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
__u32 prog_id); __u32 prog_id);
void perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node); bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id); struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id);
int perf_env__numa_node(struct perf_env *env, int cpu); int perf_env__numa_node(struct perf_env *env, int cpu);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment