bpf: add btf func info support
Subsystems (6)
v3 cover bpf: add btf func info support
The BTF support was added to kernel by Commit 69b693f0aefa ("bpf: btf: Introduce BPF Type Format (BTF)"), which introduced .BTF section into ELF file and is primarily
used for map pretty print.
pahole is used to convert dwarf to BTF for ELF files.
This patch added func info support to the kernel so we can get better ksym's for bpf function calls. Basically, pairs of bpf function calls and their corresponding types are passed to the kernel. Extracting function names from the types, the kernel is able to construct a ksym for each function call with embedded function name.
This patch set added support of FUNC and FUNC_PROTO types in the kernel. LLVM patch https://reviews.llvm.org/D53261
can generate func info, encoded in .BTF.ext ELF section. The following is an example to show FUNC and FUNC_PROTO difference, compiled with the above LLVM patch with Debug mode.
-bash-4.2$ cat test.c
int foo(int (*bar)(int)) { return bar(5); }
-bash-4.2$ clang -target bpf -g -O2 -mllvm -debug-only=btf -c test.c Type Table:
[1] FUNC name_off=1 info=0x0c000001 size/type=2 param_type=3
[2] INT name_off=11 info=0x01000000 size/type=4
desc=0x01000020
[3] PTR name_off=0 info=0x02000000 size/type=4
[4] FUNC_PROTO name_off=0 info=0x0d000001 size/type=2 param_type=2
String Table:
0 :
1 : foo
5 : .text
11 : int
15 : test.c
22 : int foo(int (*bar)(int)) { return bar(5); }
FuncInfo Table:
sec_name_off=5
insn_offset=<Omitted> type_id=1
...
In the above, type and string tables are in .BTF section and the func info in .BTF.ext. The "<Omitted>" is the insn offset which is not available during the dump time but resolved during later compilation process.
Following the format specification at Patch #9 and examine the raw data in .BTF.ext section, we have
FuncInfo Table:
sec_name_off=5
insn_offset=0 type_id=1
The (insn_offset, type_id) can be passed to the kernel
so the kernel can find the func name and use it in the ksym.
Below is a demonstration from Patch #13.
$ bpftool prog dump jited id 1
int _dummy_tracepoint(struct dummy_tracepoint_args * ):
bpf_prog_b07ccb89267cf242__dummy_tracepoint:
0: push %rbp
1: mov %rsp,%rbp
......
3c: add $0x28,%rbp
40: leaveq
41: retq int test_long_fname_1(struct dummy_tracepoint_args * ):
bpf_prog_2dcecc18072623fc_test_long_fname_1:
0: push %rbp
1: mov %rsp,%rbp
......
3a: add $0x28,%rbp
3e: leaveq
3f: retq int test_long_fname_2(struct dummy_tracepoint_args * ):
bpf_prog_89d64e4abf0f0126_test_long_fname_2:
0: push %rbp
1: mov %rsp,%rbp
......
80: add $0x28,%rbp
84: leaveq
85: retqFor the patchset,
Patch #1 refactors the code to break up btf_type_is_void(). Patch #2 introduces new BTF types BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO. Patch #3 syncs btf.h header to tools directory. Patch #4 adds btf func/func_proto self tests in test_btf. Patch #5 adds kernel interface to load func_info to kernel
and pass func_info to userspace.
Patch #6 syncs bpf.h header to tools directory.
Patch #7 adds news btf/func_info related fields in libbpf
program load function.
Patch #8 extends selftest test_btf to test load/retrieve func_type info. Patch #9 adds .BTF.ext func info support.
Patch #10 changes Makefile to avoid using pahole if llvm is capable of
generating BTF sections.
Patch #11 refactors to have btf_get_from_id() in libbpf for reuse. Patch #12 enhance test_btf file testing to test func info. Patch #13 adds bpftool support for func signature dump.
Changelogs:
v2 -> v3:
. Removed kernel btf extern functions btf_type_id_func()
and btf_get_name_by_id(). Instead, exposing existing functions btf_type_by_id() and btf_name_by_offset().
. Added comments about ELF section .BTF.ext layout.
. Better codes in btftool as suggested by Edward Cree.
v1 -> v2:
. Added missing sign-off.
. Limited the func_name/struct_member_name length for validity test. . Removed/changed several verifier messages.
. Modified several commit messages to remove line_off reference.
Yonghong Song (13):
bpf: btf: Break up btf_type_is_void()
bpf: btf: Add BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO tools/bpf: sync kernel btf.h header
tools/bpf: add btf func/func_proto unit tests in selftest test_btf
bpf: get better bpf_prog ksyms based on btf func type_id tools/bpf: sync kernel uapi bpf.h header to tools directory tools/bpf: add new fields for program load in lib/bpf tools/bpf: extends test_btf to test load/retrieve func_type info tools/bpf: add support to read .BTF.ext sections tools/bpf: do not use pahole if clang/llvm can generate BTF sections tools/bpf: refactor to implement btf_get_from_id() in lib/bpf tools/bpf: enhance test_btf file testing to test func info tools/bpf: bpftool: add support for jited func types
include/linux/bpf.h | 2 + include/linux/bpf_verifier.h | 1 + include/linux/btf.h | 2 + include/uapi/linux/bpf.h | 11 + include/uapi/linux/btf.h | 9 +- kernel/bpf/btf.c | 313 ++++++++-- kernel/bpf/core.c | 13 + kernel/bpf/syscall.c | 86 ++- kernel/bpf/verifier.c | 46 ++ samples/bpf/Makefile | 8 + tools/bpf/bpftool/btf_dumper.c | 90 +++ tools/bpf/bpftool/main.h | 2 + tools/bpf/bpftool/map.c | 68 +-- tools/bpf/bpftool/prog.c | 54 ++ tools/include/uapi/linux/bpf.h | 11 + tools/include/uapi/linux/btf.h | 9 +- tools/lib/bpf/bpf.c | 3 + tools/lib/bpf/bpf.h | 3 + tools/lib/bpf/btf.c | 305 ++++++++++ tools/lib/bpf/btf.h | 50 ++ tools/lib/bpf/libbpf.c | 53 +- tools/testing/selftests/bpf/Makefile | 8 + tools/testing/selftests/bpf/test_btf.c | 581 ++++++++++++++++++- tools/testing/selftests/bpf/test_btf_haskv.c | 16 +- tools/testing/selftests/bpf/test_btf_nokv.c | 16 +- 25 files changed, 1621 insertions(+), 139 deletions(-)
Signature · 1 line
2.17.1
PATCH v3 4/13 tools/bpf: add btf func/func_proto unit tests in selftest test_btf
Add several BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO
unit tests in bpf selftest test_btf.
--- tools/lib/bpf/btf.c | 4 + tools/testing/selftests/bpf/test_btf.c | 216 +++++++++++++++++++++++++ 2 files changed, 220 insertions(+)
tools/lib/bpf/btf.c +4 -0
@@ -165,6 +165,10 @@ static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)case BTF_KIND_ENUM : next_type += vlen *sizeof (struct btf_enum );break ; +case BTF_KIND_FUNC : +case BTF_KIND_FUNC_PROTO : + next_type += vlen *sizeof (int ); +break ;case BTF_KIND_TYPEDEF :case BTF_KIND_PTR :case BTF_KIND_FWD :
tools/testing/selftests/bpf/test_btf.c +216 -0
222 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 3/13 tools/bpf: sync kernel btf.h header
In reply to Yonghong SongThe kernel uapi btf.h is synced to the tools directory.
--- tools/include/uapi/linux/btf.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
tools/include/uapi/linux/btf.h +6 -3
@@ -40,7 +40,8 @@ struct btf_type {/* "size" is used by INT, ENUM, STRUCT and UNION. * "size" tells the size of the type it is describing. * -* "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT. +* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, +* FUNC and FUNC_PROTO. * "type" is a type_id referring to another type. */ union {
@@ -64,8 +65,10 @@ struct btf_type {#define BTF_KIND_VOLATILE 9/* Volatile */ #define BTF_KIND_CONST 10/* Const */ #define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_MAX 11 -#define NR_BTF_KINDS 12 +#define BTF_KIND_FUNC 12/* Function */ +#define BTF_KIND_FUNC_PROTO 13/* Function Prototype */ +#define BTF_KIND_MAX 13 +#define NR_BTF_KINDS 14/* For some specific BTF_KIND, "struct btf_type" is immediately * followed by extra data.
Signature · 1 line
2.17.1
PATCH v3 2/13 bpf: btf: Add BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO
In reply to Yonghong SongThis patch adds BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support to the type section. BTF_KIND_FUNC_PROTO is used to specify the type of a function pointer. With this, BTF has a complete set of C types (except float).
BTF_KIND_FUNC is used to specify the signature of a defined subprogram. BTF_KIND_FUNC_PROTO can be referenced by another type, e.g., a pointer type, and BTF_KIND_FUNC type cannot be referenced by another type.
For both BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO types, the func return type is in t->type (where t is a "struct btf_type" object). The func args are an array of u32s immediately following object "t".
As a concrete example, for the C program below,
$ cat test.c
int foo(int (*bar)(int)) { return bar(5); }
with LLVM patch https://reviews.llvm.org/D53261
in Debug mode, we have
$ clang -target bpf -g -O2 -mllvm -debug-only=btf -c test.c
Type Table:
[1] FUNC NameOff=1 Info=0x0c000001 Size/Type=2
ParamType=3
[2] INT NameOff=11 Info=0x01000000 Size/Type=4
Desc=0x01000020
[3] PTR NameOff=0 Info=0x02000000 Size/Type=4
[4] FUNC_PROTO NameOff=0 Info=0x0d000001 Size/Type=2
ParamType=2 String Table:
0 :
1 : foo
5 : .text
11 : int
15 : test.c
22 : int foo(int (*bar)(int)) { return bar(5); }
FuncInfo Table:
SecNameOff=5
InsnOffset=<Omitted> TypeId=1
...
(Eventually we shall have bpftool to dump btf information
like the above.)
Function "foo" has a FUNC type (type_id = 1).
The parameter of "foo" has type_id 3 which is PTR->FUNC_PROTO, where FUNC_PROTO refers to function pointer "bar".
In FuncInfo Table, for section .text, the function, with to-be-determined offset (marked as <Omitted>),
has type_id=1 which refers to a FUNC type.
This way, the function signature is
available to both kernel and user space.
Here, the insn offset is not available during the dump time as relocation is resolved pretty late in the compilation process.
--- include/uapi/linux/btf.h | 9 +- kernel/bpf/btf.c | 280 ++++++++++++++++++++++++++++++++++----- 2 files changed, 253 insertions(+), 36 deletions(-)
include/uapi/linux/btf.h +6 -3
@@ -40,7 +40,8 @@ struct btf_type {/* "size" is used by INT, ENUM, STRUCT and UNION. * "size" tells the size of the type it is describing. * -* "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT. +* "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, +* FUNC and FUNC_PROTO. * "type" is a type_id referring to another type. */ union {
@@ -64,8 +65,10 @@ struct btf_type {#define BTF_KIND_VOLATILE 9/* Volatile */ #define BTF_KIND_CONST 10/* Const */ #define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_MAX 11 -#define NR_BTF_KINDS 12 +#define BTF_KIND_FUNC 12/* Function */ +#define BTF_KIND_FUNC_PROTO 13/* Function Prototype */ +#define BTF_KIND_MAX 13 +#define NR_BTF_KINDS 14/* For some specific BTF_KIND, "struct btf_type" is immediately * followed by extra data.
kernel/bpf/btf.c +247 -33
378 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 1/13 bpf: btf: Break up btf_type_is_void()
In reply to Yonghong SongThis patch breaks up btf_type_is_void() intobtf_type_is_void() and btf_type_is_fwd().
It also adds btf_type_nosize() to better describe it is testing a type has nosize info.
--- kernel/bpf/btf.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-)
kernel/bpf/btf.c +22 -15
@@ -306,15 +306,22 @@ static bool btf_type_is_modifier(const struct btf_type *t)static bool btf_type_is_void (const struct btf_type *t) { -/* void => no type and size info. -* Hence, FWD is also treated as void. -*/ -return t == &btf_void ||BTF_INFO_KIND (t->info) ==BTF_KIND_FWD ; +return t == &btf_void; +} + +static bool btf_type_is_fwd (const struct btf_type *t) +{ +return BTF_INFO_KIND (t->info) ==BTF_KIND_FWD ; +} + +static bool btf_type_nosize (const struct btf_type *t) +{ +return btf_type_is_void (t) ||btf_type_is_fwd (t); } -static bool btf_type_is_void_or_null (const struct btf_type *t) +static bool btf_type_nosize_or_null (const struct btf_type *t) { -return !t ||btf_type_is_void (t); +return !t ||btf_type_nosize (t); }/* union is only a special case of struct:
@@ -826,7 +833,7 @@ const struct btf_type *btf_type_id_size(const struct btf *btf,u32 size =0 ; size_type =btf_type_by_id (btf, size_type_id); -if (btf_type_is_void_or_null (size_type)) +if (btf_type_nosize_or_null (size_type))return NULL ;if (btf_type_has_size (size_type)) {
@@ -842,7 +849,7 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, size = btf->resolved_sizes[size_type_id]; size_type_id = btf->resolved_ids[size_type_id]; size_type =btf_type_by_id (btf, size_type_id); -if (btf_type_is_void (size_type)) +if (btf_type_nosize_or_null (size_type))return NULL ; }
@@ -1164,7 +1171,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env, }/* "typedef void new_void", "const void"...etc */ -if (btf_type_is_void (next_type)) +if (btf_type_is_void (next_type) ||btf_type_is_fwd (next_type)) goto resolved; if (!env_type_is_resolve_sink(env, next_type) &&
@@ -1178,7 +1185,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,* pretty print). */ if (!btf_type_id_size (btf, &next_type_id, &next_type_size) && - !btf_type_is_void (btf_type_id_resolve (btf, &next_type_id))) { + !btf_type_nosize (btf_type_id_resolve (btf, &next_type_id))) {btf_verifier_log_type (env, v->t,"Invalid type_id" );return -EINVAL ; }
@@ -1205,7 +1212,7 @@ static int btf_ptr_resolve(struct btf_verifier_env *env, }/* "void *" */ -if (btf_type_is_void (next_type)) +if (btf_type_is_void (next_type) ||btf_type_is_fwd (next_type)) goto resolved; if (!env_type_is_resolve_sink(env, next_type) &&
@@ -1235,7 +1242,7 @@ static int btf_ptr_resolve(struct btf_verifier_env *env, }if (!btf_type_id_size (btf, &next_type_id, &next_type_size) && - !btf_type_is_void (btf_type_id_resolve (btf, &next_type_id))) { + !btf_type_nosize (btf_type_id_resolve (btf, &next_type_id))) {btf_verifier_log_type (env, v->t,"Invalid type_id" );return -EINVAL ; }
@@ -1396,7 +1403,7 @@ static int btf_array_resolve(struct btf_verifier_env *env,/* Check array->index_type */ index_type_id = array->index_type; index_type =btf_type_by_id (btf, index_type_id); -if (btf_type_is_void_or_null (index_type)) { +if (btf_type_nosize_or_null (index_type)) {btf_verifier_log_type (env, v->t,"Invalid index" );return -EINVAL ; }
@@ -1415,7 +1422,7 @@ static int btf_array_resolve(struct btf_verifier_env *env,/* Check array->type */ elem_type_id = array->type; elem_type =btf_type_by_id (btf, elem_type_id); -if (btf_type_is_void_or_null (elem_type)) { +if (btf_type_nosize_or_null (elem_type)) {btf_verifier_log_type (env, v->t,"Invalid elem" );return -EINVAL ;
@@ -1615,7 +1622,7 @@ static int btf_struct_resolve(struct btf_verifier_env *env,const struct btf_type *member_type =btf_type_by_id (env->btf, member_type_id); -if (btf_type_is_void_or_null (member_type)) { +if (btf_type_nosize_or_null (member_type)) {btf_verifier_log_member (env, v->t, member,"Invalid member" );return -EINVAL ;
Signature · 1 line
2.17.1
Quoted 7 lines · Yonghong Song
This patch added func info support to the kernel so we can get better ksym's for bpf function calls. Basically, pairs of bpf function calls and their corresponding types are passed to the kernel. Extracting function names from the types, the kernel is able to construct a ksym for each function call with embedded function name.
...
Quoted 11 lines
Below is a demonstration from Patch #13.
$ bpftool prog dump jited id 1 int _dummy_tracepoint(struct dummy_tracepoint_args * ): bpf_prog_b07ccb89267cf242__dummy_tracepoint: 0: push %rbp 1: mov %rsp,%rbp ...... 3c: add $0x28,%rbp 40: leaveq 41: retq
Quoted 4 lines
int
test_long_fname_1(struct dummy_tracepoint_args * ):
bpf_prog_2dcecc18072623fc_test_long_fname_1:0: push %rbp 1: mov %rsp,%rbp
Considering we only had 16-byte names for main prog and no names at all for subprogs, this is huge improvement in BPF introspection.
Cannot wait for the followup patches with line info support.
For the set: