Skip to contentSkip to conversation

libbpf: handle BTF parsing and loading properly

PATCH Applied Andrii Nakryiko 4 participants 4 messages Mar 8, 2019 – Mar 11, 2019

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

PATCH 1/1 libbpf: handle BTF parsing and loading properly

This patch splits and cleans up error handling logic for loading BTF data. Previously, if BTF data was parsed successfully, but failed to load into kernel, we'd report nonsensical error code, instead of error returned from btf__load(). Now btf__new() and btf__load() are handled separately with proper cleanup and warning reporting.

Fixes d29d87f7e612 ("btf: separate btf creation and loading")
---
 tools/lib/bpf/libbpf.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +10 -3
index d5b830d60601..5e977d2688da 100644
@@ -835,12 +835,19 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
 			obj->efile.maps_shndx = idx;
 		else if (strcmp(name, BTF_ELF_SEC) == 0) {
 			obj->btf = btf__new(data->d_buf, data->d_size);
-			if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
+			if (IS_ERR(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;
+				continue;
+			}
+			err = btf__load(obj->btf);
+			if (err) {
+				pr_warning("Error loading %s into kernel: %d. Ignored and continue.\n",
+					   BTF_ELF_SEC, err);
+				btf__free(obj->btf);
+				obj->btf = NULL;
+				err = 0;
 			}
 		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
 			btf_ext_data = data;
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Andrii Nakryiko

This patch splits and cleans up error handling logic for loading BTF data. Previously, if BTF data was parsed successfully, but failed to load into kernel, we'd report nonsensical error code, instead of error returned from btf__load(). Now btf__new() and btf__load() are handled separately with proper cleanup and warning reporting.

Acked-by on v1 1/1
#2 lore raw
In reply to Andrii Nakryiko
Quoted 9 lines · Andrii Nakryiko

This patch splits and cleans up error handling logic for loading BTF data. Previously, if BTF data was parsed successfully, but failed to load into kernel, we'd report nonsensical error code, instead of error returned from btf__load(). Now btf__new() and btf__load() are handled separately with proper cleanup and warning reporting.

Fixes d29d87f7e612 ("btf: separate btf creation and loading")
Quoted 32 lines
---
  tools/lib/bpf/libbpf.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +10 -3
index d5b830d60601..5e977d2688da 100644
@@ -835,12 +835,19 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
  			obj->efile.maps_shndx = idx;
  		else if (strcmp(name, BTF_ELF_SEC) == 0) {
  			obj->btf = btf__new(data->d_buf, data->d_size);
-			if (IS_ERR(obj->btf) || btf__load(obj->btf)) {
+			if (IS_ERR(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;
+				continue;
+			}
+			err = btf__load(obj->btf);
+			if (err) {
+				pr_warning("Error loading %s into kernel: %d. Ignored and continue.\n",
+					   BTF_ELF_SEC, err);
+				btf__free(obj->btf);
+				obj->btf = NULL;
+				err = 0;
  			}
  		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
  			btf_ext_data = data;
maintainer Applied
#3 lore raw
In reply to Andrii Nakryiko
Quoted 9 lines · Andrii Nakryiko

This patch splits and cleans up error handling logic for loading BTF data. Previously, if BTF data was parsed successfully, but failed to load into kernel, we'd report nonsensical error code, instead of error returned from btf__load(). Now btf__new() and btf__load() are handled separately with proper cleanup and warning reporting.

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

Applied, thanks!