diff mbox series

[RFC,bpf-next,3/5] libbpf: use BTF-encoded kind information to help parse unrecognized kinds

Message ID 1669225312-28949-4-git-send-email-alan.maguire@oracle.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series bpf: making BTF self-describing | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR pending PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 85 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 92 exceeds 80 columns WARNING: line length of 94 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 pending Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 pending Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 pending Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 pending Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 pending Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 pending Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 pending Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 pending Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 pending Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 pending Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 pending Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 pending Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 pending Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-22 pending Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 pending Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 pending Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 pending Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 pending Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-27 pending Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 pending Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 pending Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 pending Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 pending Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-32 pending Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 pending Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 pending Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 pending Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-36 pending Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 pending Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 pending Logs for test_verifier on x86_64 with llvm-16

Commit Message

Alan Maguire Nov. 23, 2022, 5:41 p.m. UTC
btf__add_kinds() adds typedef/struct representations of the kinds supported
at BTF encoding time.  When decoding/parsing, we can then use information
about unrecognized kinds to skip over them.  This will be useful if the
BTF encoder encoded info using a new kind, but the parser doesn't support
it yet.

Note that only size determinations of unrecognized kinds are supported;
lookup, dedup and other features are not supported; the aim here is to
not break BTF parsing if newer kinds are encountered.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/lib/bpf/btf.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index e3cea44..da719de 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -28,6 +28,8 @@ 
 
 static struct btf_type btf_void;
 
+#define NR_BTF_KINDS_POSSIBLE	0x20
+
 /* info used to encode/decode an unrecognized kind */
 struct btf_kind_desc {
 	int kind;
@@ -131,6 +133,9 @@  struct btf {
 
 	/* Pointer size (in bytes) for a target architecture of this BTF */
 	int ptr_sz;
+
+	/* representations of unrecognized kinds are stored here */
+	struct btf_kind_desc unrecognized_kinds[NR_BTF_KINDS_POSSIBLE - NR_BTF_KINDS];
 };
 
 static inline __u64 ptr_to_u64(const void *ptr)
@@ -420,6 +425,75 @@  static int btf_bswap_type_rest(struct btf_type *t)
 	}
 }
 
+static int btf_unrecognized_kind_type_size(struct btf *btf, const struct btf_type *t)
+{
+	struct btf_kind_desc *unrec_kind = NULL;
+	__u16 kind = btf_kind(t);
+	int size = 0;
+
+	if (kind >= NR_BTF_KINDS && kind < NR_BTF_KINDS_POSSIBLE)
+		unrec_kind = &btf->unrecognized_kinds[kind - NR_BTF_KINDS];
+	if (!unrec_kind) {
+		pr_debug("No information about unrecognized kind:%u\n", kind);
+		return -EINVAL;
+	}
+
+	if (unrec_kind->kind != kind) {
+		const char *kind_struct_name;
+		const struct btf_type *kt;
+		const struct btf_member *m;
+		const struct btf_array *a;
+		char typedef_name[64];
+		__s32 id;
+
+		/* we need to fill in information on this kind; it will be cached in struct btf
+		 * for subsequent references.
+		 */
+		snprintf(typedef_name, sizeof(typedef_name), BTF_KIND_PFX "%u", kind);
+		id = btf__find_by_name_kind(btf, typedef_name, BTF_KIND_TYPEDEF);
+		if (id < 0)
+			return id;
+		kt = btf__type_by_id(btf, id);
+		kt = btf__type_by_id(btf, kt->type);
+		kind_struct_name = btf__str_by_offset(btf, kt->name_off);
+		/* struct should contain type + optional meta fields; otherwise unsupported */
+		switch (btf_vlen(kt)) {
+		case 1:
+			unrec_kind->nr_meta = 0;
+			unrec_kind->meta_size = 0;
+			break;
+		case 2:
+			m = btf_members(kt);
+			kt = btf__type_by_id(btf, (++m)->type);
+			if (btf_kind(kt) != BTF_KIND_ARRAY) {
+				pr_debug("Unexpected kind %u for member in '%s'\n",
+					 btf_kind(kt), kind_struct_name);
+				return -EINVAL;
+			}
+			a = btf_array(kt);
+			unrec_kind->nr_meta = a->nelems;
+			kt = btf__type_by_id(btf, a->type);
+			unrec_kind->meta_size = kt->size;
+			unrec_kind->meta_name = btf__str_by_offset(btf, kt->name_off);
+			break;
+		default:
+			pr_debug("unexpected nr of fields for '%s'(%u)\n", kind_struct_name,
+				 kind);
+			return -EINVAL;
+		}
+		unrec_kind->kind = kind;
+		unrec_kind->struct_name = kind_struct_name;
+	}
+	size = sizeof(struct btf_type);
+	if (unrec_kind->meta_size > 0) {
+		if (unrec_kind->nr_meta == 0)
+			size += btf_vlen(t) * unrec_kind->meta_size;
+		else
+			size += unrec_kind->nr_meta * unrec_kind->meta_size;
+	}
+	return size;
+}
+
 static int btf_parse_type_sec(struct btf *btf)
 {
 	struct btf_header *hdr = btf->hdr;
@@ -433,6 +507,8 @@  static int btf_parse_type_sec(struct btf *btf)
 
 		type_size = btf_type_size(next_type);
 		if (type_size < 0)
+			type_size = btf_unrecognized_kind_type_size(btf, next_type);
+		if (type_size < 0)
 			return type_size;
 		if (next_type + type_size > end_type) {
 			pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types);