Skip to contentSkip to conversation

add distilled base BTF support to pahole

PATCH Applied Alan Maguire 2 participants 5 messages Jul 29, 2024 – Aug 26, 2024

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

cover add distilled base BTF support to pahole

Patch 1 updates the libbpf commit to use latest libbpf with btf__distill_base() support; patch 2 adds support for the 'distilled_base' BTF feature, used to support resilient split BTF for kernel modules.

Alan Maguire (2):
pahole: Sync with libbpf-1.5
btf_encoder: add "distilled_base" BTF feature to split BTF generation

 btf_encoder.c      | 50 ++++++++++++++++++++++++++++++++++------------
 dwarves.h          |  1 +
 lib/bpf            |  2 +-
 man-pages/pahole.1 |  4 ++++
 pahole.c           |  1 +
 5 files changed, 44 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.5
#1 lore raw

PATCH 1/2 pahole: Sync with libbpf-1.5

This will pull in BTF support for distilled base BTF.

---
 lib/bpf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bpf +1 -1
index 6597330..686f600 160000
@@ -1 +1 @@
-Subproject commit 6597330c45d185381900037f0130712cd326ae59
+Subproject commit 686f600bca59e107af4040d0838ca2b02c14ff50
Signature · 1 line
2.43.5
#2 lore raw

PATCH 2/2 btf_encoder: add "distilled_base" BTF feature to split BTF generation

In reply to Alan Maguire

Adding "distilled_base" to --btf_features when generating split BTF will create split and .BTF.base BTF - the latter allows us to map references from split BTF to base BTF, even if that base BTF has changed. It does this by providing just enough information about the base types in the .BTF.base section. See [1] for more.

One note - with non-embedded libbpf, we need to guard against versions of libbpf (<1.5) which do not have btf__distill_base(); in such a case, silently skip distillation in line with other unrecognized BTF features.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 btf_encoder.c      | 50 ++++++++++++++++++++++++++++++++++------------
 dwarves.h          |  1 +
 man-pages/pahole.1 |  4 ++++
 pahole.c           |  1 +
 4 files changed, 43 insertions(+), 13 deletions(-)
btf_encoder.c +37 -13
index c2df2bc..b8b1adb 100644
@@ -93,7 +93,8 @@ struct btf_encoder {
 			  gen_floats,
 			  skip_encoding_decl_tag,
 			  tag_kfuncs,
-			  is_rel;
+			  is_rel,
+			  gen_distilled_base;
 	uint32_t	  array_index_id;
 	struct {
 		struct var_info *vars;
@@ -1284,9 +1285,9 @@ static int btf_encoder__write_raw_file(struct btf_encoder *encoder)
 	return err;
 }
 
-static int btf_encoder__write_elf(struct btf_encoder *encoder)
+static int btf_encoder__write_elf(struct btf_encoder *encoder, const struct btf *btf,
+				  const char *btf_secname)
 {
-	struct btf *btf = encoder->btf;
 	const char *filename = encoder->filename;
 	GElf_Shdr shdr_mem, *shdr;
 	Elf_Data *btf_data = NULL;
@@ -1326,7 +1327,7 @@ static int btf_encoder__write_elf(struct btf_encoder *encoder)
 		if (shdr == NULL)
 			continue;
 		char *secname = elf_strptr(elf, strndx, shdr->sh_name);
-		if (strcmp(secname, ".BTF") == 0) {
+		if (strcmp(secname, btf_secname) == 0) {
 			btf_data = elf_getdata(scn, btf_data);
 			break;
 		}
@@ -1370,11 +1371,11 @@ static int btf_encoder__write_elf(struct btf_encoder *encoder)
 			goto unlink;
 		}
 
-		snprintf(cmd, sizeof(cmd), "%s --add-section .BTF=%s %s",
-			 llvm_objcopy, tmp_fn, filename);
+		snprintf(cmd, sizeof(cmd), "%s --add-section %s=%s %s",
+			 llvm_objcopy, btf_secname, tmp_fn, filename);
 		if (system(cmd)) {
-			fprintf(stderr, "%s: failed to add .BTF section to '%s': %d!\n",
-				__func__, filename, errno);
+			fprintf(stderr, "%s: failed to add %s section to '%s': %d!\n",
+				__func__, btf_secname, filename, errno);
 			goto unlink;
 		}
 
@@ -1755,12 +1756,34 @@ int btf_encoder__encode(struct btf_encoder *encoder)
 		fprintf(stderr, "%s: btf__dedup failed!\n", __func__);
 		return -1;
 	}
-
-	if (encoder->raw_output)
+	if (encoder->raw_output) {
 		err = btf_encoder__write_raw_file(encoder);
-	else
-		err = btf_encoder__write_elf(encoder);
-
+	} else {
+		/* non-embedded libbpf may not have btf__distill_base() or a
+		 * definition of BTF_BASE_ELF_SEC, so conditionally compile
+		 * distillation code.  Like other --btf_features, it will
+		 * silently ignore the feature request if libbpf does not
+		 * support it.
+		 */
+#if LIBBPF_MAJOR_VERSION >= 1 && LIBBPF_MINOR_VERSION >= 5
+		if (encoder->gen_distilled_base) {
+			struct btf *btf = NULL, *distilled_base = NULL;
+
+			if (btf__distill_base(encoder->btf, &distilled_base, &btf) < 0) {
+				fprintf(stderr, "could not generate distilled base BTF: %s\n",
+					strerror(errno));
+				return -1;
+			}
+			err = btf_encoder__write_elf(encoder, btf, BTF_ELF_SEC);
+			if (!err)
+				err = btf_encoder__write_elf(encoder, distilled_base, BTF_BASE_ELF_SEC);
+			btf__free(btf);
+			btf__free(distilled_base);
+			return err;
+		}
+#endif
+		err = btf_encoder__write_elf(encoder, encoder->btf, BTF_ELF_SEC);
+	}
 	return err;
 }
 
@@ -2037,6 +2060,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
 		encoder->skip_encoding_vars = conf_load->skip_encoding_btf_vars;
 		encoder->skip_encoding_decl_tag	 = conf_load->skip_encoding_btf_decl_tag;
 		encoder->tag_kfuncs	 = conf_load->btf_decl_tag_kfuncs;
+		encoder->gen_distilled_base = conf_load->btf_gen_distilled_base;
 		encoder->verbose	 = verbose;
 		encoder->has_index_type  = false;
 		encoder->need_index_type = false;
dwarves.h +1 -0
index f5ae79f..ad26951 100644
@@ -95,6 +95,7 @@ struct conf_load {
 	bool			btf_encode_force;
 	bool			reproducible_build;
 	bool			btf_decl_tag_kfuncs;
+	bool			btf_gen_distilled_base;
 	uint8_t			hashtable_bits;
 	uint8_t			max_hashtable_bits;
 	uint16_t		kabi_prefix_len;
man-pages/pahole.1 +4 -0
index f060593..2f623b4 100644
@@ -317,6 +317,10 @@ Supported non-standard features (not enabled for 'default')
 	                   without this parallel BTF encoding can result in
 	                   inconsistent BTF ids.
 	decl_tag_kfuncs    Inject a BTF_KIND_DECL_TAG for each discovered kfunc.
+	distilled_base     For split BTF, generate a distilled version of
+	                   the associated base BTF to support later relocation
+	                   of split BTF with a possibly changed base, storing
+	                   it in a .BTF.base ELF section.
 .fi
 
 So for example, specifying \-\-btf_encode=var,enum64 will result in a BTF encoding that (as well as encoding basic BTF information) will contain variables and enum64 values.
pahole.c +1 -0
index 954498d..fdb65d4 100644
@@ -1291,6 +1291,7 @@ struct btf_feature {
 	BTF_DEFAULT_FEATURE(consistent_func, skip_encoding_btf_inconsistent_proto, false),
 	BTF_DEFAULT_FEATURE(decl_tag_kfuncs, btf_decl_tag_kfuncs, false),
 	BTF_NON_DEFAULT_FEATURE(reproducible_build, reproducible_build, false),
+	BTF_NON_DEFAULT_FEATURE(distilled_base, btf_gen_distilled_base, false),
 };
 
 #define BTF_MAX_FEATURE_STR	1024
Signature · 1 line
2.43.5
#3 lore raw
In reply to Alan Maguire
Quoted 4 lines · Alan Maguire

Patch 1 updates the libbpf commit to use latest libbpf with btf__distill_base() support; patch 2 adds support for the 'distilled_base' BTF feature, used to support resilient split BTF for kernel modules.

Thanks, applied to perf-tools-next,

- Arnaldo

Quoted 13 lines

Alan Maguire (2):
pahole: Sync with libbpf-1.5
btf_encoder: add "distilled_base" BTF feature to split BTF generation

 btf_encoder.c      | 50 ++++++++++++++++++++++++++++++++++------------
 dwarves.h          |  1 +
 lib/bpf            |  2 +-
 man-pages/pahole.1 |  4 ++++
 pahole.c           |  1 +
 5 files changed, 44 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.5
#4 lore raw
Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Alan Maguire

Patch 1 updates the libbpf commit to use latest libbpf with btf__distill_base() support; patch 2 adds support for the 'distilled_base' BTF feature, used to support resilient split BTF for kernel modules.

Thanks, applied to perf-tools-next,

To the next branch, canned reply, sorry :-)

- Arnaldo

Quoted 15 lines

- Arnaldo

Quoted 13 lines

Alan Maguire (2):
pahole: Sync with libbpf-1.5
btf_encoder: add "distilled_base" BTF feature to split BTF generation

 btf_encoder.c      | 50 ++++++++++++++++++++++++++++++++++------------
 dwarves.h          |  1 +
 lib/bpf            |  2 +-
 man-pages/pahole.1 |  4 ++++
 pahole.c           |  1 +
 5 files changed, 44 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.5