Skip to contentSkip to conversation

bpf: Add d_path helper - preparation changes

PATCH Superseded Jiri Olsa 3 participants 12 messages Jul 10, 2020 – Jul 11, 2020

Subsystems (12)

v6 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

v6 cover bpf: Add d_path helper - preparation changes

hi,
this patchset does preparation work for adding d_path helper, which still needs more work, but the initial set of patches is ready and useful to have.

This patchset adds:
- support to generate BTF ID lists that are resolved during kernel linking and usable within kernel code with following macros:

BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)

and access it in kernel code via:
extern u32 bpf_skb_output_btf_ids[];

- resolve_btfids tool that scans elf object for .BTF_ids section and resolves its symbols with BTF ID values
- resolving of bpf_ctx_convert struct and several other objects with BTF_ID_LIST

v6 changes:
- added acks
- added general make rule to resolve_btfids Build [Andrii]
- renamed .BTF.ids to .BTF_ids [Andrii]
- added --no-fail option to resolve_btfids [Andrii]
- changed resolve_btfids test to work over BTF from object file, so we don't depend on vmlinux BTF [Andrii]
- fixed few typos [Andrii]
- fixed the out of tree build [Andrii]

Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/d_path

thanks,
jirka

---
Jiri Olsa (9):
bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object
bpf: Compile resolve_btfids tool at kernel compilation start
bpf: Add BTF_ID_LIST/BTF_ID/BTF_ID_UNUSED macros
bpf: Resolve BTF IDs in vmlinux image
bpf: Remove btf_id helpers resolving
bpf: Use BTF_ID to resolve bpf_ctx_convert struct
bpf: Add info about .BTF_ids section to btf.rst
tools headers: Adopt verbatim copy of btf_ids.h from kernel sources selftests/bpf: Add test for resolve_btfids

 Documentation/bpf/btf.rst                               |  36 +++++
 Makefile                                                |  25 +++-
 include/asm-generic/vmlinux.lds.h                       |   4 +
 include/linux/btf_ids.h                                 |  87 ++++++++++++
 kernel/bpf/btf.c                                        | 103 ++------------
 kernel/trace/bpf_trace.c                                |   9 +-
 net/core/filter.c                                       |   9 +-
 scripts/link-vmlinux.sh                                 |   6 +
 tools/Makefile                                          |   3 +
 tools/bpf/Makefile                                      |   9 +-
 tools/bpf/resolve_btfids/Build                          |  10 ++
 tools/bpf/resolve_btfids/Makefile                       |  77 +++++++++++
 tools/bpf/resolve_btfids/main.c                         | 721 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/include/linux/btf_ids.h                           |  87 ++++++++++++
 tools/include/linux/compiler.h                          |   4 +
 tools/testing/selftests/bpf/Makefile                    |  14 +-
 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c | 107 ++++++++++++++
 tools/testing/selftests/bpf/progs/btf_data.c            |  26 ++++
 18 files changed, 1234 insertions(+), 103 deletions(-)
 create mode 100644 include/linux/btf_ids.h
 create mode 100644 tools/bpf/resolve_btfids/Build
 create mode 100644 tools/bpf/resolve_btfids/Makefile
 create mode 100644 tools/bpf/resolve_btfids/main.c
 create mode 100644 tools/include/linux/btf_ids.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf_data.c
author reviewer
#1 lore raw

PATCH v6 1/9 bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object

The resolve_btfids tool scans elf object for .BTF_ids section and resolves its symbols with BTF ID values.

It will be used to during linking time to resolve arrays of BTF ID values used in verifier, so these IDs do not need to be resolved in runtime.

The expected layout of .BTF_ids section is described in main.c header. Related kernel changes are coming in following changes.

Build issue reported by 0-DAY CI Kernel Test Service.

---
 tools/bpf/resolve_btfids/Build    |  10 +
 tools/bpf/resolve_btfids/Makefile |  77 ++++
 tools/bpf/resolve_btfids/main.c   | 721 ++++++++++++++++++++++++++++++
 3 files changed, 808 insertions(+)
 create mode 100644 tools/bpf/resolve_btfids/Build
 create mode 100644 tools/bpf/resolve_btfids/Makefile
 create mode 100644 tools/bpf/resolve_btfids/main.c
tools/bpf/resolve_btfids/Build new file +10 -0
new file mode 100644
index 000000000000..ae82da03f9bf
@@ -0,0 +1,10 @@
+resolve_btfids-y += main.o
+resolve_btfids-y += rbtree.o
+resolve_btfids-y += zalloc.o
+resolve_btfids-y += string.o
+resolve_btfids-y += ctype.o
+resolve_btfids-y += str_error_r.o
+
+$(OUTPUT)%.o: ../../lib/%.c FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,cc_o_c)
tools/bpf/resolve_btfids/Makefile new file +77 -0

77 lines not on this page · raw message

tools/bpf/resolve_btfids/main.c new file +721 -0

721 lines not on this page · raw message

Signature · 1 line
2.25.4
author reviewer
#2 lore raw

PATCH v6 2/9 bpf: Compile resolve_btfids tool at kernel compilation start

In reply to Jiri Olsa

The resolve_btfids tool will be used during the vmlinux linking, so it's necessary it's ready for it.

---
 Makefile           | 22 ++++++++++++++++++----
 tools/Makefile     |  3 +++
 tools/bpf/Makefile |  9 ++++++++-
 3 files changed, 29 insertions(+), 5 deletions(-)
Makefile +18 -4
index ac2c61c37a73..017e775b3288 100644
@@ -1053,9 +1053,10 @@ export mod_sign_cmd
 
 HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf)
 
+has_libelf = $(call try-run,\
+               echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0)
+
 ifdef CONFIG_STACK_VALIDATION
-  has_libelf := $(call try-run,\
-		echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0)
   ifeq ($(has_libelf),1)
     objtool_target := tools/objtool FORCE
   else
@@ -1064,6 +1065,14 @@ ifdef CONFIG_STACK_VALIDATION
   endif
 endif
 
+ifdef CONFIG_DEBUG_INFO_BTF
+  ifeq ($(has_libelf),1)
+    resolve_btfids_target := tools/bpf/resolve_btfids FORCE
+  else
+    ERROR_RESOLVE_BTFIDS := 1
+  endif
+endif
+
 PHONY += prepare0
 
 export MODORDER := $(extmod-prefix)modules.order
@@ -1175,7 +1184,7 @@ prepare0: archprepare
 	$(Q)$(MAKE) $(build)=.
 
 # All the preparing..
-prepare: prepare0 prepare-objtool
+prepare: prepare0 prepare-objtool prepare-resolve_btfids
 
 # Support for using generic headers in asm-generic
 asm-generic := -f $(srctree)/scripts/Makefile.asm-generic obj
@@ -1188,7 +1197,7 @@ uapi-asm-generic:
 	$(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/uapi/asm \
 	generic=include/uapi/asm-generic
 
-PHONY += prepare-objtool
+PHONY += prepare-objtool prepare-resolve_btfids
 prepare-objtool: $(objtool_target)
 ifeq ($(SKIP_STACK_VALIDATION),1)
 ifdef CONFIG_UNWINDER_ORC
@@ -1199,6 +1208,11 @@ else
 endif
 endif
 
+prepare-resolve_btfids: $(resolve_btfids_target)
+ifeq ($(ERROR_RESOLVE_BTFIDS),1)
+	@echo "error: Cannot resolve BTF IDs for CONFIG_DEBUG_INFO_BTF, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2
+	@false
+endif
 # Generate some files
 # ---------------------------------------------------------------------------
 
tools/Makefile +3 -0
index bd778812e915..85af6ebbce91 100644
@@ -67,6 +67,9 @@ cpupower: FORCE
 cgroup firewire hv guest bootconfig spi usb virtio vm bpf iio gpio objtool leds wmi pci firmware debugging: FORCE
 	$(call descend,$@)
 
+bpf/%: FORCE
+	$(call descend,$@)
+
 liblockdep: FORCE
 	$(call descend,lib/lockdep)
 
tools/bpf/Makefile +8 -1
index 6df1850f8353..74bc9a105225 100644
@@ -123,5 +123,12 @@ runqslower_install:
 runqslower_clean:
 	$(call descend,runqslower,clean)
 
+resolve_btfids:
+	$(call descend,resolve_btfids)
+
+resolve_btfids_clean:
+	$(call descend,resolve_btfids,clean)
+
 .PHONY: all install clean bpftool bpftool_install bpftool_clean \
-	runqslower runqslower_install runqslower_clean
+	runqslower runqslower_install runqslower_clean \
+	resolve_btfids resolve_btfids_clean
Signature · 1 line
2.25.4
author reviewer
#3 lore raw

PATCH v6 3/9 bpf: Add BTF_ID_LIST/BTF_ID/BTF_ID_UNUSED macros

In reply to Jiri Olsa

Adding support to generate .BTF_ids section that will hold BTF ID lists for verifier.

Adding macros that will help to define lists of BTF ID values placed in .BTF_ids section. They are initially filled with zeros (during compilation) and resolved later during the linking phase by resolve_btfids tool.

Following defines list of one BTF ID value:

BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)

It also defines following variable to access the list:

  extern u32 bpf_skb_output_btf_ids[];

The BTF_ID_UNUSED macro defines 4 zero bytes. It's used when we want to define 'unused' entry in BTF_ID_LIST, like:

BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)
BTF_ID_UNUSED
BTF_ID(struct, task_struct)

---
 include/asm-generic/vmlinux.lds.h |  4 ++
 include/linux/btf_ids.h           | 87 +++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 include/linux/btf_ids.h
include/asm-generic/vmlinux.lds.h +4 -0
index db600ef218d7..0be2ee265931 100644
@@ -641,6 +641,10 @@
 		__start_BTF = .;					\
 		*(.BTF)							\
 		__stop_BTF = .;						\
+	}								\
+	. = ALIGN(4);							\
+	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
+		*(.BTF_ids)						\
 	}
 #else
 #define BTF
include/linux/btf_ids.h new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.25.4
author reviewer
#4 lore raw

PATCH v6 4/9 bpf: Resolve BTF IDs in vmlinux image

In reply to Jiri Olsa

Using BTF_ID_LIST macro to define lists for several helpers using BTF arguments.

And running resolve_btfids on vmlinux elf object during linking, so the .BTF_ids section gets the IDs resolved.

---
 Makefile                 | 3 ++-
 kernel/trace/bpf_trace.c | 9 +++++++--
 net/core/filter.c        | 9 +++++++--
 scripts/link-vmlinux.sh  | 6 ++++++
 4 files changed, 22 insertions(+), 5 deletions(-)
Makefile +2 -1
index 017e775b3288..462f1a75fcb3 100644
@@ -448,6 +448,7 @@ OBJSIZE		= $(CROSS_COMPILE)size
 STRIP		= $(CROSS_COMPILE)strip
 endif
 PAHOLE		= pahole
+RESOLVE_BTFIDS	= $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
 LEX		= flex
 YACC		= bison
 AWK		= awk
@@ -510,7 +511,7 @@ GCC_PLUGINS_CFLAGS :=
 CLANG_FLAGS :=
 
 export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC
-export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE LEX YACC AWK INSTALLKERNEL
+export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
 export PERL PYTHON PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
 export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ
 export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
kernel/trace/bpf_trace.c +7 -2
index e0b7775039ab..e178e8e32b33 100644
@@ -13,6 +13,7 @@
 #include <linux/kprobes.h>
 #include <linux/syscalls.h>
 #include <linux/error-injection.h>
+#include <linux/btf_ids.h>
 
 #include <asm/tlb.h>
 
@@ -710,7 +711,9 @@ BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
 	return err;
 }
 
-static int bpf_seq_printf_btf_ids[5];
+BTF_ID_LIST(bpf_seq_printf_btf_ids)
+BTF_ID(struct, seq_file)
+
 static const struct bpf_func_proto bpf_seq_printf_proto = {
 	.func		= bpf_seq_printf,
 	.gpl_only	= true,
@@ -728,7 +731,9 @@ BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
 	return seq_write(m, data, len) ? -EOVERFLOW : 0;
 }
 
-static int bpf_seq_write_btf_ids[5];
+BTF_ID_LIST(bpf_seq_write_btf_ids)
+BTF_ID(struct, seq_file)
+
 static const struct bpf_func_proto bpf_seq_write_proto = {
 	.func		= bpf_seq_write,
 	.gpl_only	= true,
net/core/filter.c +7 -2
index ddcc0d6209e1..4e572441e64a 100644
@@ -75,6 +75,7 @@
 #include <net/ipv6_stubs.h>
 #include <net/bpf_sk_storage.h>
 #include <net/transp_v6.h>
+#include <linux/btf_ids.h>
 
 /**
  *	sk_filter_trim_cap - run a packet through a socket filter
@@ -3779,7 +3780,9 @@ static const struct bpf_func_proto bpf_skb_event_output_proto = {
 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
 };
 
-static int bpf_skb_output_btf_ids[5];
+BTF_ID_LIST(bpf_skb_output_btf_ids)
+BTF_ID(struct, sk_buff)
+
 const struct bpf_func_proto bpf_skb_output_proto = {
 	.func		= bpf_skb_event_output,
 	.gpl_only	= true,
@@ -4173,7 +4176,9 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = {
 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
 };
 
-static int bpf_xdp_output_btf_ids[5];
+BTF_ID_LIST(bpf_xdp_output_btf_ids)
+BTF_ID(struct, xdp_buff)
+
 const struct bpf_func_proto bpf_xdp_output_proto = {
 	.func		= bpf_xdp_event_output,
 	.gpl_only	= true,
scripts/link-vmlinux.sh +6 -0
index 92dd745906f4..e26f02dbedee 100755
@@ -336,6 +336,12 @@ fi
 
 vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
 
+# fill in BTF IDs
+if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
+info BTFIDS vmlinux
+${RESOLVE_BTFIDS} vmlinux
+fi
+
 if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then
 	info SORTTAB vmlinux
 	if ! sorttable vmlinux; then
Signature · 1 line
2.25.4
author reviewer
#5 lore raw

PATCH v6 5/9 bpf: Remove btf_id helpers resolving

In reply to Jiri Olsa

Now when we moved the helpers btf_id arrays into .BTF_ids section, we can remove the code that resolve those IDs in runtime.

---
 kernel/bpf/btf.c | 89 +++---------------------------------------------
 1 file changed, 5 insertions(+), 84 deletions(-)
kernel/bpf/btf.c +5 -84
index 4c3007f428b1..71140b73ae3c 100644
@@ -4079,96 +4079,17 @@ int btf_struct_access(struct bpf_verifier_log *log,
 	return -EINVAL;
 }
 
-static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
-				   int arg)
-{
-	char fnname[KSYM_SYMBOL_LEN + 4] = "btf_";
-	const struct btf_param *args;
-	const struct btf_type *t;
-	const char *tname, *sym;
-	u32 btf_id, i;
-
-	if (IS_ERR(btf_vmlinux)) {
-		bpf_log(log, "btf_vmlinux is malformed\n");
-		return -EINVAL;
-	}
-
-	sym = kallsyms_lookup((long)fn, NULL, NULL, NULL, fnname + 4);
-	if (!sym) {
-		bpf_log(log, "kernel doesn't have kallsyms\n");
-		return -EFAULT;
-	}
-
-	for (i = 1; i <= btf_vmlinux->nr_types; i++) {
-		t = btf_type_by_id(btf_vmlinux, i);
-		if (BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF)
-			continue;
-		tname = __btf_name_by_offset(btf_vmlinux, t->name_off);
-		if (!strcmp(tname, fnname))
-			break;
-	}
-	if (i > btf_vmlinux->nr_types) {
-		bpf_log(log, "helper %s type is not found\n", fnname);
-		return -ENOENT;
-	}
-
-	t = btf_type_by_id(btf_vmlinux, t->type);
-	if (!btf_type_is_ptr(t))
-		return -EFAULT;
-	t = btf_type_by_id(btf_vmlinux, t->type);
-	if (!btf_type_is_func_proto(t))
-		return -EFAULT;
-
-	args = (const struct btf_param *)(t + 1);
-	if (arg >= btf_type_vlen(t)) {
-		bpf_log(log, "bpf helper %s doesn't have %d-th argument\n",
-			fnname, arg);
-		return -EINVAL;
-	}
-
-	t = btf_type_by_id(btf_vmlinux, args[arg].type);
-	if (!btf_type_is_ptr(t) || !t->type) {
-		/* anything but the pointer to struct is a helper config bug */
-		bpf_log(log, "ARG_PTR_TO_BTF is misconfigured\n");
-		return -EFAULT;
-	}
-	btf_id = t->type;
-	t = btf_type_by_id(btf_vmlinux, t->type);
-	/* skip modifiers */
-	while (btf_type_is_modifier(t)) {
-		btf_id = t->type;
-		t = btf_type_by_id(btf_vmlinux, t->type);
-	}
-	if (!btf_type_is_struct(t)) {
-		bpf_log(log, "ARG_PTR_TO_BTF is not a struct\n");
-		return -EFAULT;
-	}
-	bpf_log(log, "helper %s arg%d has btf_id %d struct %s\n", fnname + 4,
-		arg, btf_id, __btf_name_by_offset(btf_vmlinux, t->name_off));
-	return btf_id;
-}
-
 int btf_resolve_helper_id(struct bpf_verifier_log *log,
 			  const struct bpf_func_proto *fn, int arg)
 {
-	int *btf_id = &fn->btf_id[arg];
-	int ret;
+	int id;
 
 	if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID)
 		return -EINVAL;
-
-	ret = READ_ONCE(*btf_id);
-	if (ret)
-		return ret;
-	/* ok to race the search. The result is the same */
-	ret = __btf_resolve_helper_id(log, fn->func, arg);
-	if (!ret) {
-		/* Function argument cannot be type 'void' */
-		bpf_log(log, "BTF resolution bug\n");
-		return -EFAULT;
-	}
-	WRITE_ONCE(*btf_id, ret);
-	return ret;
+	id = fn->btf_id[arg];
+	if (!id || id > btf_vmlinux->nr_types)
+		return -EINVAL;
+	return id;
 }
 
 static int __get_type_size(struct btf *btf, u32 btf_id,
Signature · 1 line
2.25.4
author reviewer
#6 lore raw

PATCH v6 6/9 bpf: Use BTF_ID to resolve bpf_ctx_convert struct

In reply to Jiri Olsa

This way the ID is resolved during compile time,
and we can remove the runtime name search.

---
 kernel/bpf/btf.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/bpf/btf.c +6 -8
index 71140b73ae3c..a710e3ee1f18 100644
@@ -18,6 +18,7 @@
 #include <linux/sort.h>
 #include <linux/bpf_verifier.h>
 #include <linux/btf.h>
+#include <linux/btf_ids.h>
 #include <linux/skmsg.h>
 #include <linux/perf_event.h>
 #include <net/sock.h>
@@ -3621,12 +3622,15 @@ static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
 	return kern_ctx_type->type;
 }
 
+BTF_ID_LIST(bpf_ctx_convert_btf_id)
+BTF_ID(struct, bpf_ctx_convert)
+
 struct btf *btf_parse_vmlinux(void)
 {
 	struct btf_verifier_env *env = NULL;
 	struct bpf_verifier_log *log;
 	struct btf *btf = NULL;
-	int err, btf_id;
+	int err;
 
 	env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
 	if (!env)
@@ -3659,14 +3663,8 @@ struct btf *btf_parse_vmlinux(void)
 	if (err)
 		goto errout;
 
-	/* find struct bpf_ctx_convert for type checking later */
-	btf_id = btf_find_by_name_kind(btf, "bpf_ctx_convert", BTF_KIND_STRUCT);
-	if (btf_id < 0) {
-		err = btf_id;
-		goto errout;
-	}
 	/* btf_parse_vmlinux() runs under bpf_verifier_lock */
-	bpf_ctx_convert.t = btf_type_by_id(btf, btf_id);
+	bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
 
 	/* find bpf map structs for map_ptr access checking */
 	err = btf_vmlinux_map_ids_init(btf, log);
Signature · 1 line
2.25.4
author reviewer
#7 lore raw

PATCH v6 7/9 bpf: Add info about .BTF_ids section to btf.rst

In reply to Jiri Olsa

Updating btf.rst doc with info about .BTF_ids section

---
 Documentation/bpf/btf.rst | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
Documentation/bpf/btf.rst +36 -0
index 4d565d202ce3..c9dec7a565e8 100644
@@ -691,6 +691,42 @@ kernel API, the ``insn_off`` is the instruction offset in the unit of ``struct
 bpf_insn``. For ELF API, the ``insn_off`` is the byte offset from the
 beginning of section (``btf_ext_info_sec->sec_name_off``).
 
+4.2 .BTF_ids section
+====================
+
+The .BTF_ids section encodes BTF ID values that are used within the kernel.
+
+This section is created during the kernel compilation with the help of
+macros defined in ``include/linux/btf_ids.h`` header file. Kernel code can
+use them to create lists and sets (sorted lists) of BTF ID values.
+
+The ``BTF_ID_LIST`` and ``BTF_ID`` macros define unsorted list of BTF ID values,
+with following syntax::
+
+  BTF_ID_LIST(list)
+  BTF_ID(type1, name1)
+  BTF_ID(type2, name2)
+
+resulting in following layout in .BTF_ids section::
+
+  __BTF_ID__type1__name1__1:
+  .zero 4
+  __BTF_ID__type2__name2__2:
+  .zero 4
+
+The ``int list[];`` variable is defined to access the list.
+
+The ``BTF_ID_UNUSED`` macro defines 4 zero bytes. It's used when we
+want to define unused entry in BTF_ID_LIST, like::
+
+      BTF_ID_LIST(bpf_skb_output_btf_ids)
+      BTF_ID(struct, sk_buff)
+      BTF_ID_UNUSED
+      BTF_ID(struct, task_struct)
+
+All the BTF ID lists and sets are compiled in the .BTF_ids section and
+resolved during the linking phase of kernel build by ``resolve_btfids`` tool.
+
 5. Using BTF
 ************
 
Signature · 1 line
2.25.4
author reviewer
#8 lore raw

PATCH v6 8/9 tools headers: Adopt verbatim copy of btf_ids.h from kernel sources

In reply to Jiri Olsa

It will be needed by bpf selftest for resolve_btfids tool.

Also adding __PASTE macro as btf_ids.h dependency, which is defined in:

include/linux/compiler_types.h

but because tools/include do not have this header, I'm putting the macro into linux/compiler.h header.

---
 tools/include/linux/btf_ids.h  | 87 ++++++++++++++++++++++++++++++++++
 tools/include/linux/compiler.h |  4 ++
 2 files changed, 91 insertions(+)
 create mode 100644 tools/include/linux/btf_ids.h
tools/include/linux/btf_ids.h new file +87 -0
new file mode 100644
index 000000000000..fe019774f8a7
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _LINUX_BTF_IDS_H
+#define _LINUX_BTF_IDS_H
+
+#include <linux/compiler.h> /* for __PASTE */
+
+/*
+ * Following macros help to define lists of BTF IDs placed
+ * in .BTF_ids section. They are initially filled with zeros
+ * (during compilation) and resolved later during the
+ * linking phase by resolve_btfids tool.
+ *
+ * Any change in list layout must be reflected in resolve_btfids
+ * tool logic.
+ */
+
+#define BTF_IDS_SECTION ".BTF_ids"
+
+#define ____BTF_ID(symbol)				\
+asm(							\
+".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
+".local " #symbol " ;                          \n"	\
+".type  " #symbol ", @object;                  \n"	\
+".size  " #symbol ", 4;                        \n"	\
+#symbol ":                                     \n"	\
+".zero 4                                       \n"	\
+".popsection;                                  \n");
+
+#define __BTF_ID(symbol) \
+	____BTF_ID(symbol)
+
+#define __ID(prefix) \
+	__PASTE(prefix, __COUNTER__)
+
+/*
+ * The BTF_ID defines unique symbol for each ID pointing
+ * to 4 zero bytes.
+ */
+#define BTF_ID(prefix, name) \
+	__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__))
+
+/*
+ * The BTF_ID_LIST macro defines pure (unsorted) list
+ * of BTF IDs, with following layout:
+ *
+ * BTF_ID_LIST(list1)
+ * BTF_ID(type1, name1)
+ * BTF_ID(type2, name2)
+ *
+ * list1:
+ * __BTF_ID__type1__name1__1:
+ * .zero 4
+ * __BTF_ID__type2__name2__2:
+ * .zero 4
+ *
+ */
+#define __BTF_ID_LIST(name)				\
+asm(							\
+".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
+".local " #name ";                             \n"	\
+#name ":;                                      \n"	\
+".popsection;                                  \n");	\
+
+#define BTF_ID_LIST(name)				\
+__BTF_ID_LIST(name)					\
+extern u32 name[];
+
+/*
+ * The BTF_ID_UNUSED macro defines 4 zero bytes.
+ * It's used when we want to define 'unused' entry
+ * in BTF_ID_LIST, like:
+ *
+ *   BTF_ID_LIST(bpf_skb_output_btf_ids)
+ *   BTF_ID(struct, sk_buff)
+ *   BTF_ID_UNUSED
+ *   BTF_ID(struct, task_struct)
+ */
+
+#define BTF_ID_UNUSED					\
+asm(							\
+".pushsection " BTF_IDS_SECTION ",\"a\";       \n"	\
+".zero 4                                       \n"	\
+".popsection;                                  \n");
+
+
+#endif
tools/include/linux/compiler.h +4 -0
index 9f9002734e19..6eac24d44e81 100644
@@ -201,4 +201,8 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
 # define __fallthrough
 #endif
 
+/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
+#define ___PASTE(a, b) a##b
+#define __PASTE(a, b) ___PASTE(a, b)
+
 #endif /* _TOOLS_LINUX_COMPILER_H */
Signature · 1 line
2.25.4
author reviewer
#9 lore raw

PATCH v6 9/9 selftests/bpf: Add test for resolve_btfids

In reply to Jiri Olsa

Adding resolve_btfids test under test_progs suite.

It's possible to use btf_ids.h header and its logic in user space application, so we can add easy test for it.

The test defines BTF_ID_LIST and checks it gets properly resolved.

For this reason the test_progs binary (and other binaries that use TRUNNER* macros) is processed with resolve_btfids tool, which resolves BTF IDs in .BTF_ids section. The BTF data are taken from btf_data.o object rceated from progs/btf_data.c.

---
 tools/testing/selftests/bpf/Makefile          |  14 ++-
 .../selftests/bpf/prog_tests/resolve_btfids.c | 107 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/btf_data.c  |  26 +++++
 3 files changed, 146 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf_data.c
tools/testing/selftests/bpf/Makefile +13 -1
index 1f9c696b3edf..2eae482b2713 100644
@@ -190,6 +190,16 @@ else
 	cp "$(VMLINUX_H)" $@
 endif
 
+$(SCRATCH_DIR)/resolve_btfids: $(BPFOBJ)				\
+			       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
+			       $(TOOLSDIR)/lib/rbtree.c			\
+			       $(TOOLSDIR)/lib/zalloc.c			\
+			       $(TOOLSDIR)/lib/string.c			\
+			       $(TOOLSDIR)/lib/ctype.c			\
+			       $(TOOLSDIR)/lib/str_error_r.c
+	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
+		OUTPUT=$(SCRATCH_DIR)/ BPFOBJ=$(BPFOBJ)
+
 # Get Clang's default includes on this system, as opposed to those seen by
 # '-target bpf'. This fixes "missing" files on some architectures/distros,
 # such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
@@ -355,6 +365,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
 			     | $(TRUNNER_BINARY)-extras
 	$$(call msg,BINARY,,$$@)
 	$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
+	$(SCRATCH_DIR)/resolve_btfids --no-fail --btf btf_data.o $$@
 
 endef
 
@@ -365,7 +376,8 @@ TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
 			 network_helpers.c testing_helpers.c		\
 			 flow_dissector_load.h
 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read				\
-		       $(wildcard progs/btf_dump_test_case_*.c)
+		       $(wildcard progs/btf_dump_test_case_*.c)		\
+		       $(SCRATCH_DIR)/resolve_btfids
 TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
 TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
 TRUNNER_BPF_LDFLAGS := -mattr=+alu32
tools/testing/selftests/bpf/prog_tests/resolve_btfids.c new file +107 -0

107 lines not on this page · raw message

tools/testing/selftests/bpf/progs/btf_data.c new file +26 -0
new file mode 100644
index 000000000000..26b85f45e584
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+struct S {
+	int	a;
+	int	b;
+	int	c;
+};
+
+union U {
+	int	a;
+	int	b;
+	int	c;
+};
+
+typedef int T;
+
+struct root_struct {
+	T		m_1;
+	struct S	m_2;
+	union U		m_3;
+};
+
+int func(struct root_struct *root)
+{
+	return 0;
+}
Signature · 1 line
2.25.4
Acked-by Tested-by on the v6 series
#10 lore raw
In reply to Jiri Olsa
Quoted 36 lines · Jiri Olsa

hi,
this patchset does preparation work for adding d_path helper, which still needs more work, but the initial set of patches is ready and useful to have.

This patchset adds:
- support to generate BTF ID lists that are resolved during kernel linking and usable within kernel code with following macros:

BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)

and access it in kernel code via:
extern u32 bpf_skb_output_btf_ids[];

- resolve_btfids tool that scans elf object for .BTF_ids section and resolves its symbols with BTF ID values
- resolving of bpf_ctx_convert struct and several other objects with BTF_ID_LIST

v6 changes:
- added acks
- added general make rule to resolve_btfids Build [Andrii]
- renamed .BTF.ids to .BTF_ids [Andrii]
- added --no-fail option to resolve_btfids [Andrii]
- changed resolve_btfids test to work over BTF from object file, so we don't depend on vmlinux BTF [Andrii]
- fixed few typos [Andrii]
- fixed the out of tree build [Andrii]

Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/d_path

Quoted 5 lines

thanks,
jirka

---

You've missed fixing bpf_get_task_stack_proto with your BTF_IDS_LIST/BTF_ID macro. It's currently failing in subtests. With BTF_IDS_LIST/BTF_ID trivial fix it works again. Please fix that before this can be applied.

btf_data.c could also use some name-conflicting entries, just to make sure that kind (struct vs typedef) is taken into account. Maybe just add some dummy typedef int S; or something?

So with the above, please add for the next revision:

Quoted 38 lines

Jiri Olsa (9):
bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object
bpf: Compile resolve_btfids tool at kernel compilation start
bpf: Add BTF_ID_LIST/BTF_ID/BTF_ID_UNUSED macros
bpf: Resolve BTF IDs in vmlinux image
bpf: Remove btf_id helpers resolving
bpf: Use BTF_ID to resolve bpf_ctx_convert struct
bpf: Add info about .BTF_ids section to btf.rst
tools headers: Adopt verbatim copy of btf_ids.h from kernel sources selftests/bpf: Add test for resolve_btfids

 Documentation/bpf/btf.rst                               |  36 +++++
 Makefile                                                |  25 +++-
 include/asm-generic/vmlinux.lds.h                       |   4 +
 include/linux/btf_ids.h                                 |  87 ++++++++++++
 kernel/bpf/btf.c                                        | 103 ++------------
 kernel/trace/bpf_trace.c                                |   9 +-
 net/core/filter.c                                       |   9 +-
 scripts/link-vmlinux.sh                                 |   6 +
 tools/Makefile                                          |   3 +
 tools/bpf/Makefile                                      |   9 +-
 tools/bpf/resolve_btfids/Build                          |  10 ++
 tools/bpf/resolve_btfids/Makefile                       |  77 +++++++++++
 tools/bpf/resolve_btfids/main.c                         | 721 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/include/linux/btf_ids.h                           |  87 ++++++++++++
 tools/include/linux/compiler.h                          |   4 +
 tools/testing/selftests/bpf/Makefile                    |  14 +-
 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c | 107 ++++++++++++++
 tools/testing/selftests/bpf/progs/btf_data.c            |  26 ++++
 18 files changed, 1234 insertions(+), 103 deletions(-)
 create mode 100644 include/linux/btf_ids.h
 create mode 100644 tools/bpf/resolve_btfids/Build
 create mode 100644 tools/bpf/resolve_btfids/Makefile
 create mode 100644 tools/bpf/resolve_btfids/main.c
 create mode 100644 tools/include/linux/btf_ids.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf_data.c
#11 lore raw
Quoted 41 lines · Andrii Nakryiko
Quoted 40 lines · Jiri Olsa

hi,
this patchset does preparation work for adding d_path helper, which still needs more work, but the initial set of patches is ready and useful to have.

This patchset adds:
- support to generate BTF ID lists that are resolved during kernel linking and usable within kernel code with following macros:

BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)

and access it in kernel code via:
extern u32 bpf_skb_output_btf_ids[];

- resolve_btfids tool that scans elf object for .BTF_ids section and resolves its symbols with BTF ID values
- resolving of bpf_ctx_convert struct and several other objects with BTF_ID_LIST

v6 changes:
- added acks
- added general make rule to resolve_btfids Build [Andrii]
- renamed .BTF.ids to .BTF_ids [Andrii]
- added --no-fail option to resolve_btfids [Andrii]
- changed resolve_btfids test to work over BTF from object file, so we don't depend on vmlinux BTF [Andrii]
- fixed few typos [Andrii]
- fixed the out of tree build [Andrii]

Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/d_path

thanks,
jirka

Quoted 6 lines
Quoted 1 line

---

You've missed fixing bpf_get_task_stack_proto with your BTF_IDS_LIST/BTF_ID macro. It's currently failing in subtests. With BTF_IDS_LIST/BTF_ID trivial fix it works again. Please fix that before this can be applied.

ugh.. moving fast ;-) some of the selftests are failing for me on regular basis, I overlooked this one, sry

Quoted 4 lines

btf_data.c could also use some name-conflicting entries, just to make sure that kind (struct vs typedef) is taken into account. Maybe just add some dummy typedef int S; or something?

sure.. and I had to change resolve_btfids a bit because of that, so I'll not add your ack to first patch yet

I'll send the new version shortly

thanks,
jirka

Quoted 45 lines

So with the above, please add for the next revision:

Quoted 38 lines

Jiri Olsa (9):
bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object
bpf: Compile resolve_btfids tool at kernel compilation start
bpf: Add BTF_ID_LIST/BTF_ID/BTF_ID_UNUSED macros
bpf: Resolve BTF IDs in vmlinux image
bpf: Remove btf_id helpers resolving
bpf: Use BTF_ID to resolve bpf_ctx_convert struct
bpf: Add info about .BTF_ids section to btf.rst
tools headers: Adopt verbatim copy of btf_ids.h from kernel sources selftests/bpf: Add test for resolve_btfids

 Documentation/bpf/btf.rst                               |  36 +++++
 Makefile                                                |  25 +++-
 include/asm-generic/vmlinux.lds.h                       |   4 +
 include/linux/btf_ids.h                                 |  87 ++++++++++++
 kernel/bpf/btf.c                                        | 103 ++------------
 kernel/trace/bpf_trace.c                                |   9 +-
 net/core/filter.c                                       |   9 +-
 scripts/link-vmlinux.sh                                 |   6 +
 tools/Makefile                                          |   3 +
 tools/bpf/Makefile                                      |   9 +-
 tools/bpf/resolve_btfids/Build                          |  10 ++
 tools/bpf/resolve_btfids/Makefile                       |  77 +++++++++++
 tools/bpf/resolve_btfids/main.c                         | 721 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/include/linux/btf_ids.h                           |  87 ++++++++++++
 tools/include/linux/compiler.h                          |   4 +
 tools/testing/selftests/bpf/Makefile                    |  14 +-
 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c | 107 ++++++++++++++
 tools/testing/selftests/bpf/progs/btf_data.c            |  26 ++++
 18 files changed, 1234 insertions(+), 103 deletions(-)
 create mode 100644 include/linux/btf_ids.h
 create mode 100644 tools/bpf/resolve_btfids/Build
 create mode 100644 tools/bpf/resolve_btfids/Makefile
 create mode 100644 tools/bpf/resolve_btfids/main.c
 create mode 100644 tools/include/linux/btf_ids.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/resolve_btfids.c
 create mode 100644 tools/testing/selftests/bpf/progs/btf_data.c