Skip to contentSkip to conversation

: libbpf: fixing leak when kernel does not support btf

PATCH Applied Nikita V. Shirokov 4 participants 5 messages Mar 8, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 : libbpf: fixing leak when kernel does not support btf

we could end up in situation when we have object file w/ all btf info but kernel does not support btf yet. in this situation currently libbpf just set obj->btf to NULL w/o freeing it first. this patch if fixing it by making sure to run btf__free first

v2->v3:
- adding "Fixes" tag

v1->v2:
- adding netdev to cc

Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")
---
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index f5eb60379c8d..d5b830d60601 100644
@@ -838,6 +838,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
 			if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
 				pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
 					   BTF_ELF_SEC, PTR_ERR(obj->btf));
+				if (!IS_ERR(obj->btf))
+					btf__free(obj->btf);
 				obj->btf = NULL;
 			}
 		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
Signature · 1 line
2.17.1
Acked-by on v3 1/1
#1 lore raw
Quoted 5 lines · Nikita V. Shirokov

we could end up in situation when we have object file w/ all btf info but kernel does not support btf yet. in this situation currently libbpf just set obj->btf to NULL w/o freeing it first. this patch if fixing it by making sure to run btf__free first

Quoted 8 lines

v2->v3:
- adding "Fixes" tag

v1->v2:
- adding netdev to cc

Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")
Quoted 18 lines
---
  tools/lib/bpf/libbpf.c | 2 ++
  1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index f5eb60379c8d..d5b830d60601 100644
@@ -838,6 +838,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
  			if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
  				pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
  					   BTF_ELF_SEC, PTR_ERR(obj->btf));
+				if (!IS_ERR(obj->btf))
+					btf__free(obj->btf);
  				obj->btf = NULL;
  			}
  		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
maintainer Applied
#2 lore raw
In reply to Nikita V. Shirokov
Quoted 5 lines · Nikita V. Shirokov

we could end up in situation when we have object file w/ all btf info but kernel does not support btf yet. in this situation currently libbpf just set obj->btf to NULL w/o freeing it first. this patch if fixing it by making sure to run btf__free first

Quoted 8 lines

v2->v3:
- adding "Fixes" tag

v1->v2:
- adding netdev to cc

Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")

Applied, thanks!

#3 lore raw
In reply to Nikita V. Shirokov
Quoted 5 lines · Nikita V. Shirokov

we could end up in situation when we have object file w/ all btf info but kernel does not support btf yet. in this situation currently libbpf just set obj->btf to NULL w/o freeing it first. this patch if fixing it by making sure to run btf__free first

Quoted 7 lines

v2->v3:
- adding "Fixes" tag

v1->v2:
- adding netdev to cc

Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")

I think the Fixes tag should belong to a later patch, just in case there will be an issue when pulling into stable:

Fixes d29d87f7e612 ("btf: separate btf creation and loading")

tools/lib/bpf/libbpf.c static int bpf_object__elf_collect(struct bpf_object *obj, int flags) · line 840

Quoted 13 lines
---
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index f5eb60379c8d..d5b830d60601 100644
@@ -838,6 +838,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
 			if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
 				pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
 					   BTF_ELF_SEC, PTR_ERR(obj->btf));

Printing this PTR_ERR(obj->btf) seems not very correct also if btf__load() was the one failing.

Quoted 8 lines
tools/lib/bpf/libbpf.c +2 -0
+				if (!IS_ERR(obj->btf))
+					btf__free(obj->btf);
 				obj->btf = NULL;
 			}
 		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
Signature · 1 line
2.17.1
maintainer
#4 lore raw
Quoted 9 lines · Martin Lau
Quoted 8 lines · Nikita V. Shirokov

we could end up in situation when we have object file w/ all btf info but kernel does not support btf yet. in this situation currently libbpf just set obj->btf to NULL w/o freeing it first. this patch if fixing it by making sure to run btf__free first

v2->v3:
- adding "Fixes" tag

Quoted 8 lines
Quoted 4 lines

v1->v2:
- adding netdev to cc

Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")

I think the Fixes tag should belong to a later patch, just in case there will be an issue when pulling into stable:

Fixes d29d87f7e612 ("btf: separate btf creation and loading")

Agree, I just fixed up the tags, thanks!

Quoted 5 lines
Quoted 5 lines
---
 tools/lib/bpf/libbpf.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 10 lines

tools/lib/bpf/libbpf.c static int bpf_object__elf_collect(struct bpf_object *obj, int flags) · line 840

Quoted 8 lines
tools/lib/bpf/libbpf.c +0 -0
index f5eb60379c8d..d5b830d60601 100644
@@ -838,6 +838,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
 			if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
 				pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
 					   BTF_ELF_SEC, PTR_ERR(obj->btf));

Printing this PTR_ERR(obj->btf) seems not very correct also if btf__load() was the one failing.

Makes sense as well. Given this is logically independent from addressing this leak, please send a follow-up fix for the pr_warning().

Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.c +2 -0
+				if (!IS_ERR(obj->btf))
+					btf__free(obj->btf);
 				obj->btf = NULL;
 			}
 		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
Signature · 1 line
2.17.1