diff mbox series

[RFCv2,bpf-next,10/18] btf: Add helper for kernel modules to lookup full BTF ID

Message ID 166256555117.1434226.17491394872817800182.stgit@firesoul (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series XDP-hints: XDP gaining access to HW offload hints via BTF | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 pending Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
netdev/tree_selection success Clearly marked for bpf-next, async
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 fail Series longer than 15 patches (and no cover letter)
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1361 this patch: 1361
netdev/cc_maintainers warning 13 maintainers not CCed: andrii@kernel.org john.fastabend@gmail.com jolsa@kernel.org song@kernel.org yhs@fb.com daniel@iogearbox.net martin.lau@linux.dev haoluo@google.com kuba@kernel.org kpsingh@kernel.org hawk@kernel.org davem@davemloft.net sdf@google.com
netdev/build_clang success Errors and warnings before: 159 this patch: 159
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: 1353 this patch: 1353
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 36 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jesper Dangaard Brouer Sept. 7, 2022, 3:45 p.m. UTC
NIC driver modules need to store the full BTF ID as the last member in
metadata area usually written as xdp_hints_common->btf_full_id.
This full BTF ID is a 64-bit value, encoding the modules own BTF object
ID as the high 32-bit and specific struct BTF type ID as lower 32-bit.

Drivers should invoke this once at init time and cache this BTF ID for
runtime usage.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
---
 include/linux/btf.h |    1 +
 kernel/bpf/btf.c    |   23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)
diff mbox series

Patch

diff --git a/include/linux/btf.h b/include/linux/btf.h
index a66266c00c04..b8f7c92b6767 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -150,6 +150,7 @@  bool btf_is_module(const struct btf *btf);
 struct module *btf_try_get_module(const struct btf *btf);
 struct btf *btf_get_module_btf(const struct module *module);
 void btf_put_module_btf(struct btf *btf);
+u64 btf_get_module_btf_full_id(struct btf *btf, const char *name);
 u32 btf_nr_types(const struct btf *btf);
 bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
 			   const struct btf_member *m,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 1e95391e0ca1..10a859943a49 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7092,6 +7092,29 @@  struct btf *btf_get_module_btf(const struct module *module)
 }
 EXPORT_SYMBOL_GPL(btf_get_module_btf);
 
+u64 btf_get_module_btf_full_id(struct btf *btf, const char *name)
+{
+	s32 type_id;
+	u64 obj_id;
+
+	if (IS_ERR_OR_NULL(btf))
+		return 0;
+
+	obj_id  = btf_obj_id(btf);
+	type_id = btf_find_by_name_kind(btf, name, BTF_KIND_STRUCT);
+	if (type_id < 0) {
+		pr_warn("Module %s(ID:%d): BTF cannot find struct %s",
+			btf->name, (u32)obj_id, name);
+		return 0;
+	}
+
+	pr_info("Module %s(ID:%d): BTF type id %d for struct %s",
+		btf->name, (u32)obj_id, type_id, name);
+
+	return type_id | (obj_id << 32);
+}
+EXPORT_SYMBOL_GPL(btf_get_module_btf_full_id);
+
 BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags)
 {
 	struct btf *btf = NULL;