bpf: add support for new btf kind BTF_KIND_TAG
Subsystems (6)
v2 cover bpf: add support for new btf kind BTF_KIND_TAG
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.
This new attribute can be used to add attributes to kernel codes, e.g., pre- or post- conditions, allow/deny info, or any other info in which only the kernel is interested. Such attributes will be processed by clang frontend and emitted to dwarf, converting to BTF by pahole. Ultimiately the verifier can use these information for verification purpose.
The new attribute can also be used for bpf programs, e.g., tagging with __user attributes for function parameters, specifying global function preconditions, etc. Such information may help verifier to detect user program bugs.
After this series, pahole dwarf->btf converter will be enhanced to support new llvm tag for btf_tag attribute. With pahole support, we will then try to add a few real use case, e.g., __user/__rcu tagging, allow/deny list, some kernel function precondition, etc, in the kernel.
In the rest of the series, Patches 1-2 had kernel support. Patches 3-4 added
libbpf support. Patch 5 added bpftool support. Patches 6-10 added various selftests. Patch 11 added documentation for the new kind.
[1] https://reviews.llvm.org/D106614
[2] https://reviews.llvm.org/D106621
[3] https://reviews.llvm.org/D106622
[4] https://reviews.llvm.org/D109560
Changelog:
v1 -> v2:
- BTF ELF format changed in llvm ([4] above), so cross-board change to use the new format.
- Clarified in commit message that BTF_KIND_TAG is not emitted by bpftool btf dump format c.
- Fix various comments from Andrii.
Yonghong Song (11):
btf: change BTF_KIND_* macros to enums
bpf: support for new btf kind BTF_KIND_TAG
libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag
libbpf: add support for BTF_KIND_TAG
bpftool: add support for BTF_KIND_TAG
selftests/bpf: test libbpf API function btf__add_tag() selftests/bpf: change NAME_NTH/IS_NAME_NTH for BTF_KIND_TAG format selftests/bpf: add BTF_KIND_TAG unit tests
selftests/bpf: test BTF_KIND_TAG for deduplication selftests/bpf: add a test with a bpf program with btf_tag attributes docs/bpf: add documentation for BTF_KIND_TAG
Documentation/bpf/btf.rst | 27 +- include/uapi/linux/btf.h | 52 +-- kernel/bpf/btf.c | 120 +++++++ tools/bpf/bpftool/btf.c | 12 + tools/include/uapi/linux/btf.h | 52 +-- tools/lib/bpf/btf.c | 85 ++++- tools/lib/bpf/btf.h | 15 + tools/lib/bpf/btf_dump.c | 3 + tools/lib/bpf/libbpf.c | 31 +- tools/lib/bpf/libbpf.map | 5 + tools/lib/bpf/libbpf_internal.h | 2 + tools/testing/selftests/bpf/btf_helpers.c | 7 +- tools/testing/selftests/bpf/prog_tests/btf.c | 318 ++++++++++++++++-- .../selftests/bpf/prog_tests/btf_tag.c | 14 + .../selftests/bpf/prog_tests/btf_write.c | 21 ++ tools/testing/selftests/bpf/progs/tag.c | 39 +++ tools/testing/selftests/bpf/test_btf.h | 3 + 17 files changed, 736 insertions(+), 70 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_tag.c create mode 100644 tools/testing/selftests/bpf/progs/tag.c
Signature · 1 line
2.30.2
PATCH v2 1/11 btf: change BTF_KIND_* macros to enums
Change BTF_KIND_* macros to enums so they are encoded in dwarf and appear in vmlinux.h. This will make it easier for bpf programs to use these constants without macro definitions.
--- include/uapi/linux/btf.h | 36 ++++++++++++++++++---------------- tools/include/uapi/linux/btf.h | 36 ++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 34 deletions(-)
include/uapi/linux/btf.h +19 -17
@@ -56,23 +56,25 @@ struct btf_type {#define BTF_INFO_VLEN (info) ((info) & 0xffff)#define BTF_INFO_KFLAG (info) ((info) >> 31) -#define BTF_KIND_UNKN 0/* Unknown */ -#define BTF_KIND_INT 1/* Integer */ -#define BTF_KIND_PTR 2/* Pointer */ -#define BTF_KIND_ARRAY 3/* Array */ -#define BTF_KIND_STRUCT 4/* Struct */ -#define BTF_KIND_UNION 5/* Union */ -#define BTF_KIND_ENUM 6/* Enumeration */ -#define BTF_KIND_FWD 7/* Forward */ -#define BTF_KIND_TYPEDEF 8/* Typedef */ -#define BTF_KIND_VOLATILE 9/* Volatile */ -#define BTF_KIND_CONST 10/* Const */ -#define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_FUNC 12/* Function */ -#define BTF_KIND_FUNC_PROTO 13/* Function Proto */ -#define BTF_KIND_VAR 14/* Variable */ -#define BTF_KIND_DATASEC 15/* Section */ -#define BTF_KIND_FLOAT 16/* Floating point */ +enum { +BTF_KIND_UNKN =0 ,/* Unknown */ +BTF_KIND_INT ,/* Integer */ +BTF_KIND_PTR ,/* Pointer */ +BTF_KIND_ARRAY ,/* Array */ +BTF_KIND_STRUCT ,/* Struct */ +BTF_KIND_UNION ,/* Union */ +BTF_KIND_ENUM ,/* Enumeration */ +BTF_KIND_FWD ,/* Forward */ +BTF_KIND_TYPEDEF ,/* Typedef */ +BTF_KIND_VOLATILE ,/* Volatile */ +BTF_KIND_CONST ,/* Const */ +BTF_KIND_RESTRICT ,/* Restrict */ +BTF_KIND_FUNC ,/* Function */ +BTF_KIND_FUNC_PROTO ,/* Function Proto */ +BTF_KIND_VAR ,/* Variable */ +BTF_KIND_DATASEC ,/* Section */ +BTF_KIND_FLOAT ,/* Floating point */ +};#define BTF_KIND_MAX BTF_KIND_FLOAT#define NR_BTF_KINDS (BTF_KIND_MAX + 1)
tools/include/uapi/linux/btf.h +19 -17
@@ -56,23 +56,25 @@ struct btf_type {#define BTF_INFO_VLEN (info) ((info) & 0xffff)#define BTF_INFO_KFLAG (info) ((info) >> 31) -#define BTF_KIND_UNKN 0/* Unknown */ -#define BTF_KIND_INT 1/* Integer */ -#define BTF_KIND_PTR 2/* Pointer */ -#define BTF_KIND_ARRAY 3/* Array */ -#define BTF_KIND_STRUCT 4/* Struct */ -#define BTF_KIND_UNION 5/* Union */ -#define BTF_KIND_ENUM 6/* Enumeration */ -#define BTF_KIND_FWD 7/* Forward */ -#define BTF_KIND_TYPEDEF 8/* Typedef */ -#define BTF_KIND_VOLATILE 9/* Volatile */ -#define BTF_KIND_CONST 10/* Const */ -#define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_FUNC 12/* Function */ -#define BTF_KIND_FUNC_PROTO 13/* Function Proto */ -#define BTF_KIND_VAR 14/* Variable */ -#define BTF_KIND_DATASEC 15/* Section */ -#define BTF_KIND_FLOAT 16/* Floating point */ +enum { +BTF_KIND_UNKN =0 ,/* Unknown */ +BTF_KIND_INT ,/* Integer */ +BTF_KIND_PTR ,/* Pointer */ +BTF_KIND_ARRAY ,/* Array */ +BTF_KIND_STRUCT ,/* Struct */ +BTF_KIND_UNION ,/* Union */ +BTF_KIND_ENUM ,/* Enumeration */ +BTF_KIND_FWD ,/* Forward */ +BTF_KIND_TYPEDEF ,/* Typedef */ +BTF_KIND_VOLATILE ,/* Volatile */ +BTF_KIND_CONST ,/* Const */ +BTF_KIND_RESTRICT ,/* Restrict */ +BTF_KIND_FUNC ,/* Function */ +BTF_KIND_FUNC_PROTO ,/* Function Proto */ +BTF_KIND_VAR ,/* Variable */ +BTF_KIND_DATASEC ,/* Section */ +BTF_KIND_FLOAT ,/* Floating point */ +};#define BTF_KIND_MAX BTF_KIND_FLOAT#define NR_BTF_KINDS (BTF_KIND_MAX + 1)
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 01/11] btf: change BTF_KIND_* macros to enums
Quoted 44 lines · Yonghong Song
Change BTF_KIND_* macros to enums so they are encoded in dwarf and appear in
vmlinux.h. This will make it easier for bpf programs to use these constants without macro definitions.Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/btf.h | 36 ++++++++++++++++++---------------- tools/include/uapi/linux/btf.h | 36 ++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 34 deletions(-)include/uapi/linux/btf.h +8
-17@@ -56,23 +56,25 @@ struct btf_type {#define BTF_INFO_VLEN (info) ((info) & 0xffff)#define BTF_INFO_KFLAG (info) ((info) >> 31) -#define BTF_KIND_UNKN 0/* Unknown */ -#define BTF_KIND_INT 1/* Integer */ -#define BTF_KIND_PTR 2/* Pointer */ -#define BTF_KIND_ARRAY 3/* Array */ -#define BTF_KIND_STRUCT 4/* Struct */ -#define BTF_KIND_UNION 5/* Union */ -#define BTF_KIND_ENUM 6/* Enumeration */ -#define BTF_KIND_FWD 7/* Forward */ -#define BTF_KIND_TYPEDEF 8/* Typedef */ -#define BTF_KIND_VOLATILE 9/* Volatile */ -#define BTF_KIND_CONST 10/* Const */ -#define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_FUNC 12/* Function */ -#define BTF_KIND_FUNC_PROTO 13/* Function Proto */ -#define BTF_KIND_VAR 14/* Variable */ -#define BTF_KIND_DATASEC 15/* Section */ -#define BTF_KIND_FLOAT 16/* Floating point */ +enum { +BTF_KIND_UNKN =0 ,/* Unknown */ +BTF_KIND_INT ,/* Integer */ +BTF_KIND_PTR ,/* Pointer */ +BTF_KIND_ARRAY ,/* Array */ +BTF_KIND_STRUCT ,/* Struct */ +BTF_KIND_UNION ,/* Union */ +BTF_KIND_ENUM ,/* Enumeration */
include/uapi/linux/btf.h struct btf_type { · line 76
Quoted 10 lines
include/uapi/linux/btf.h +10
-0+BTF_KIND_FWD ,/* Forward */ +BTF_KIND_TYPEDEF ,/* Typedef */ +BTF_KIND_VOLATILE ,/* Volatile */ +BTF_KIND_CONST ,/* Const */ +BTF_KIND_RESTRICT ,/* Restrict */ +BTF_KIND_FUNC ,/* Function */ +BTF_KIND_FUNC_PROTO ,/* Function Proto */ +BTF_KIND_VAR ,/* Variable */ +BTF_KIND_DATASEC ,/* Section */ + BTF_KIND_FLOAT, /* Floating point */
Can you please leave explicit integer values specified? It's extremely helpful and much easier in practice, compared to having to count the number of rows from BTF_KIND_UNKN. Had to do it multiple times with other BPF constants and was happy I didn't have to do that for BTF_KIND enums.
include/uapi/linux/btf.h struct btf_type { · line 79
Quoted 3 lines
include/uapi/linux/btf.h +1
-0+};#define BTF_KIND_MAX BTF_KIND_FLOAT#define NR_BTF_KINDS (BTF_KIND_MAX + 1)
these two can be just an enum values as well, and actually will be "auto-updated", if done this way (I think, haven't really tested)
BTF_KIND_FLOAT = 16,NR_BTF_KINDS,BTF_KIND_MAX = NR_BTF_KINDS - 1,
... won't it?
[...]
Re: [PATCH bpf-next v2 01/11] btf: change BTF_KIND_* macros to enums
Quoted 53 lines · Andrii Nakryiko
Quoted 52 lines · Yonghong Song
Change BTF_KIND_* macros to enums so they are encoded in dwarf and appear in
vmlinux.h. This will make it easier for bpf programs to use these constants without macro definitions.Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/btf.h | 36 ++++++++++++++++++---------------- tools/include/uapi/linux/btf.h | 36 ++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 34 deletions(-)include/uapi/linux/btf.h +16
-17@@ -56,23 +56,25 @@ struct btf_type {#define BTF_INFO_VLEN (info) ((info) & 0xffff)#define BTF_INFO_KFLAG (info) ((info) >> 31) -#define BTF_KIND_UNKN 0/* Unknown */ -#define BTF_KIND_INT 1/* Integer */ -#define BTF_KIND_PTR 2/* Pointer */ -#define BTF_KIND_ARRAY 3/* Array */ -#define BTF_KIND_STRUCT 4/* Struct */ -#define BTF_KIND_UNION 5/* Union */ -#define BTF_KIND_ENUM 6/* Enumeration */ -#define BTF_KIND_FWD 7/* Forward */ -#define BTF_KIND_TYPEDEF 8/* Typedef */ -#define BTF_KIND_VOLATILE 9/* Volatile */ -#define BTF_KIND_CONST 10/* Const */ -#define BTF_KIND_RESTRICT 11/* Restrict */ -#define BTF_KIND_FUNC 12/* Function */ -#define BTF_KIND_FUNC_PROTO 13/* Function Proto */ -#define BTF_KIND_VAR 14/* Variable */ -#define BTF_KIND_DATASEC 15/* Section */ -#define BTF_KIND_FLOAT 16/* Floating point */ +enum { +BTF_KIND_UNKN =0 ,/* Unknown */ +BTF_KIND_INT ,/* Integer */ +BTF_KIND_PTR ,/* Pointer */ +BTF_KIND_ARRAY ,/* Array */ +BTF_KIND_STRUCT ,/* Struct */ +BTF_KIND_UNION ,/* Union */ +BTF_KIND_ENUM ,/* Enumeration */ +BTF_KIND_FWD ,/* Forward */ +BTF_KIND_TYPEDEF ,/* Typedef */ +BTF_KIND_VOLATILE ,/* Volatile */ +BTF_KIND_CONST ,/* Const */ +BTF_KIND_RESTRICT ,/* Restrict */ +BTF_KIND_FUNC ,/* Function */ +BTF_KIND_FUNC_PROTO ,/* Function Proto */ +BTF_KIND_VAR ,/* Variable */
Quoted 8 lines
include/uapi/linux/btf.h struct btf_type { · line 76
Quoted 2 lines
include/uapi/linux/btf.h +2
-0+BTF_KIND_DATASEC ,/* Section */ + BTF_KIND_FLOAT, /* Floating point */Can you please leave explicit integer values specified? It's extremely helpful and much easier in practice, compared to having to count the number of rows from
BTF_KIND_UNKN. Had to do it multiple times with other BPF constants and was happy I didn't have to do that forBTF_KINDenums.
Sure. Will do.
Quoted 11 lines
include/uapi/linux/btf.h struct btf_type { · line 79
Quoted 4 lines
include/uapi/linux/btf.h +1
-0+};#define BTF_KIND_MAX BTF_KIND_FLOAT#define NR_BTF_KINDS (BTF_KIND_MAX + 1)these two can be just an enum values as well, and actually will be "auto-updated", if done this way (I think, haven't really tested)
BTF_KIND_FLOAT= 16,NR_BTF_KINDS,BTF_KIND_MAX=NR_BTF_KINDS- 1,
This should work. Previously, I tried to give a name to the enum. If that is case, e.g., put NR_BTF_KINDS and BTF_KIND_MAX inside the enum will not be a good idea since then you have some case not a real type or you have duplicated case number (e.g, BTF_KIND_FLOAT 16 and default (which covers BTF_KIND_MAX) also contains 16) and compiler may warn.
But eventually I did not put an enum name here since a common name like btf_kind may have been used by application.
So will make the change as we don't name the enum.
Quoted 5 lines
... won't it?
[...]
PATCH v2 2/11 bpf: support for new btf kind BTF_KIND_TAG
In reply to Yonghong SongLLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.
For linux kernel, the btf_tag can be applied in various places to specify user pointer, function pre- or post- condition, function allow/deny in certain context, etc. Such information will be encoded in vmlinux BTF and can be used by verifier.
The btf_tag can also be applied to bpf programs to help global verifiable functions, e.g., specifying preconditions, etc.
This patch added basic parsing and checking support in kernel for new BTF_KIND_TAG kind.
[1] https://reviews.llvm.org/D106614
[2] https://reviews.llvm.org/D106621
[3] https://reviews.llvm.org/D106622
[4] https://reviews.llvm.org/D109560
--- include/uapi/linux/btf.h | 16 ++++- kernel/bpf/btf.c | 120 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/btf.h | 16 ++++- 3 files changed, 148 insertions(+), 4 deletions(-)
include/uapi/linux/btf.h +14 -2
@@ -43,7 +43,7 @@ struct btf_type {* "size" tells the size of the type it is describing. * * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, -* FUNC, FUNC_PROTO and VAR. +* FUNC, FUNC_PROTO, VAR and TAG. * "type" is a type_id referring to another type. */ union {
@@ -74,8 +74,9 @@ enum {BTF_KIND_VAR ,/* Variable */ BTF_KIND_DATASEC ,/* Section */ BTF_KIND_FLOAT ,/* Floating point */ + BTF_KIND_TAG, /* Tag */ }; -#define BTF_KIND_MAX BTF_KIND_FLOAT +#define BTF_KIND_MAX BTF_KIND_TAG#define NR_BTF_KINDS (BTF_KIND_MAX + 1)/* For some specific BTF_KIND, "struct btf_type" is immediately
@@ -172,4 +173,15 @@ struct btf_var_secinfo { __u32 size; }; +/* BTF_KIND_TAG is followed by a single "struct btf_tag" to describe +* additional information related to the tag applied location. +* If component_idx == -1, the tag is applied to a struct, union, +* variable or function. Otherwise, it is applied to a struct/union +* member or a func argument, and component_idx indicates which member +* or argument (0 ... vlen-1). +*/ +struct btf_tag { +__s32 component_idx; +}; +#endif /* _UAPI__LINUX_BTF_H__ */
kernel/bpf/btf.c +120 -0
168 lines not on this page · raw message
tools/include/uapi/linux/btf.h +14 -2
@@ -43,7 +43,7 @@ struct btf_type {* "size" tells the size of the type it is describing. * * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, -* FUNC, FUNC_PROTO and VAR. +* FUNC, FUNC_PROTO, VAR and TAG. * "type" is a type_id referring to another type. */ union {
@@ -74,8 +74,9 @@ enum {BTF_KIND_VAR ,/* Variable */ BTF_KIND_DATASEC ,/* Section */ BTF_KIND_FLOAT ,/* Floating point */ + BTF_KIND_TAG, /* Tag */ }; -#define BTF_KIND_MAX BTF_KIND_FLOAT +#define BTF_KIND_MAX BTF_KIND_TAG#define NR_BTF_KINDS (BTF_KIND_MAX + 1)/* For some specific BTF_KIND, "struct btf_type" is immediately
@@ -172,4 +173,15 @@ struct btf_var_secinfo { __u32 size; }; +/* BTF_KIND_TAG is followed by a single "struct btf_tag" to describe +* additional information related to the tag applied location. +* If component_idx == -1, the tag is applied to a struct, union, +* variable or function. Otherwise, it is applied to a struct/union +* member or a func argument, and component_idx indicates which member +* or argument (0 ... vlen-1). +*/ +struct btf_tag { +__s32 component_idx; +}; +#endif /* _UAPI__LINUX_BTF_H__ */
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 02/11] bpf: support for new btf kind BTF_KIND_TAG
Quoted 38 lines · Yonghong Song
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.For linux kernel, the
btf_tagcan be applied in various places to specify user pointer, function pre- or post- condition, function allow/deny in certain context, etc. Such information will be encoded in vmlinux BTF and can be used by verifier.The
btf_tagcan also be applied to bpf programs to help global verifiable functions, e.g., specifying preconditions, etc.This patch added basic parsing and checking support in kernel for new
BTF_KIND_TAGkind.[1] https://reviews.llvm.org/D106614
[2] https://reviews.llvm.org/D106621
[3] https://reviews.llvm.org/D106622
[4] https://reviews.llvm.org/D109560Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/btf.h | 16 ++++- kernel/bpf/btf.c | 120 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/btf.h | 16 ++++- 3 files changed, 148 insertions(+), 4 deletions(-)
[...]
Quoted 22 lines
kernel/bpf/btf.c +21
-0+static s32 btf_tag_check_meta (struct btf_verifier_env *env, +const struct btf_type *t, +u32 meta_left) +{ +const struct btf_tag *tag; +u32 meta_needed =sizeof (*tag); +const char *value; + +if (meta_left < meta_needed) { +btf_verifier_log_basic (env, t, +"meta_left:%u meta_needed:%u" , + meta_left, meta_needed); +return -EINVAL ; + } + + value =btf_name_by_offset (env->btf, t->name_off); +if (!value || !value[0 ]) { +btf_verifier_log_type (env, t,"Invalid value" ); +return -EINVAL ; + } +
kernel/bpf/btf.c static const struct btf_kind_operations float_ops = { · line 3853
Quoted 10 lines
kernel/bpf/btf.c +10
-0+if (btf_type_vlen (t)) { +btf_verifier_log_type (env, t,"vlen != 0" ); +return -EINVAL ; + } + +if (btf_type_kflag (t)) { +btf_verifier_log_type (env, t,"Invalid btf_info kind_flag" ); +return -EINVAL ; + } +
probably need to enforce that component_idx is >= -1? -2 is not a valid supported value right now.
Quoted 17 lines
kernel/bpf/btf.c +17
-0+btf_verifier_log_type (env, t,NULL ); + +return meta_needed; +} + +static int btf_tag_resolve (struct btf_verifier_env *env, +const struct resolve_vertex *v) +{ +const struct btf_type *next_type; +const struct btf_type *t = v->t; +u32 next_type_id = t->type; +struct btf *btf = env->btf; +s32 component_idx; +u32 vlen; + + next_type =btf_type_by_id (btf, next_type_id); +if (!next_type || !btf_type_is_tag_target (next_type)) {
kernel/bpf/btf.c static const struct btf_kind_operations float_ops = { · line 3880
Quoted 10 lines
kernel/bpf/btf.c +10
-0+btf_verifier_log_type (env, v->t,"Invalid type_id" ); +return -EINVAL ; + } + +if (!env_type_is_resolve_sink (env, next_type) && + !env_type_is_resolved (env, next_type_id)) +return env_stack_push (env, next_type, next_type_id); + + component_idx =btf_type_tag (t)->component_idx; +if (component_idx !=-1 ) {
so here, if it's -2, that should be an error, but currently will be ignored, right?
Quoted 1 line
+ if (
btf_type_is_var(next_type)||component_idx< 0) {
if is_var(next_type) then component_idx should only be -1, nothing else. Or am I missing some convention?
Quoted 22 lines
kernel/bpf/btf.c +22
-0+btf_verifier_log_type (env, v->t,"Invalid component_idx" ); +return -EINVAL ; + } + +if (btf_type_is_struct (next_type)) { + vlen =btf_type_vlen (next_type); + }else { + next_type =btf_type_by_id (btf, next_type->type); + vlen =btf_type_vlen (next_type); + } + +if ((u32 )component_idx >= vlen) { +btf_verifier_log_type (env, v->t,"Invalid component_idx" ); +return -EINVAL ; + } + } + +env_stack_pop_resolved (env, next_type_id,0 ); + +return 0 ; +} +
[...]
Re: [PATCH bpf-next v2 02/11] bpf: support for new btf kind BTF_KIND_TAG
Quoted 69 lines · Andrii Nakryiko
Quoted 38 lines · Yonghong Song
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.For linux kernel, the
btf_tagcan be applied in various places to specify user pointer, function pre- or post- condition, function allow/deny in certain context, etc. Such information will be encoded in vmlinux BTF and can be used by verifier.The
btf_tagcan also be applied to bpf programs to help global verifiable functions, e.g., specifying preconditions, etc.This patch added basic parsing and checking support in kernel for new
BTF_KIND_TAGkind.[1] https://reviews.llvm.org/D106614
[2] https://reviews.llvm.org/D106621
[3] https://reviews.llvm.org/D106622
[4] https://reviews.llvm.org/D109560Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/btf.h | 16 ++++- kernel/bpf/btf.c | 120 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/btf.h | 16 ++++- 3 files changed, 148 insertions(+), 4 deletions(-)[...]
Quoted 27 lines
kernel/bpf/btf.c +26
-0+static s32 btf_tag_check_meta (struct btf_verifier_env *env, +const struct btf_type *t, +u32 meta_left) +{ +const struct btf_tag *tag; +u32 meta_needed =sizeof (*tag); +const char *value; + +if (meta_left < meta_needed) { +btf_verifier_log_basic (env, t, +"meta_left:%u meta_needed:%u" , + meta_left, meta_needed); +return -EINVAL ; + } + + value =btf_name_by_offset (env->btf, t->name_off); +if (!value || !value[0 ]) { +btf_verifier_log_type (env, t,"Invalid value" ); +return -EINVAL ; + } + +if (btf_type_vlen (t)) { +btf_verifier_log_type (env, t,"vlen != 0" ); +return -EINVAL ; + } +
Quoted 8 lines
kernel/bpf/btf.c static const struct btf_kind_operations float_ops = { · line 3853
Quoted 5 lines
kernel/bpf/btf.c +5
-0+if (btf_type_kflag (t)) { +btf_verifier_log_type (env, t,"Invalid btf_info kind_flag" ); +return -EINVAL ; + } +probably need to enforce that
component_idxis >= -1? -2 is not a valid supported value right now.
I tested below. But I can test here for kernel practice, testing error case earlier.
Quoted 23 lines
Quoted 23 lines
kernel/bpf/btf.c +22
-0+btf_verifier_log_type (env, t,NULL ); + +return meta_needed; +} + +static int btf_tag_resolve (struct btf_verifier_env *env, +const struct resolve_vertex *v) +{ +const struct btf_type *next_type; +const struct btf_type *t = v->t; +u32 next_type_id = t->type; +struct btf *btf = env->btf; +s32 component_idx; +u32 vlen; + + next_type =btf_type_by_id (btf, next_type_id); +if (!next_type || !btf_type_is_tag_target (next_type)) { +btf_verifier_log_type (env, v->t,"Invalid type_id" ); +return -EINVAL ; + } + + if (!env_type_is_resolve_sink(env, next_type) &&
Quoted 8 lines
kernel/bpf/btf.c static const struct btf_kind_operations float_ops = { · line 3880
Quoted 5 lines
kernel/bpf/btf.c +5
-0+ !env_type_is_resolved(env, next_type_id)) +return env_stack_push (env, next_type, next_type_id); + + component_idx =btf_type_tag (t)->component_idx; +if (component_idx !=-1 ) {so here, if it's -2, that should be an error, but currently will be ignored, right?
It is not. See below. At this point, component_idx could be -2 or 0 or 1 ...
Quoted 5 lines
Quoted 2 lines
+ if (
btf_type_is_var(next_type)||component_idx< 0) {if
is_var(next_type)thencomponent_idxshould only be -1, nothing else. Or am I missing some convention?
So if it is a variable, the error will return.
If it is not a variable and component_idx < 0 (-2 in this case), return error. So we do test -2 here.
I will restructure the code to test < -1 earlier, so we won't have confusion here.
Quoted 26 lines
Quoted 23 lines
kernel/bpf/btf.c +22
-0+btf_verifier_log_type (env, v->t,"Invalid component_idx" ); +return -EINVAL ; + } + +if (btf_type_is_struct (next_type)) { + vlen =btf_type_vlen (next_type); + }else { + next_type =btf_type_by_id (btf, next_type->type); + vlen =btf_type_vlen (next_type); + } + +if ((u32 )component_idx >= vlen) { +btf_verifier_log_type (env, v->t,"Invalid component_idx" ); +return -EINVAL ; + } + } + +env_stack_pop_resolved (env, next_type_id,0 ); + +return 0 ; +} +[...]
Re: [PATCH bpf-next v2 02/11] bpf: support for new btf kind BTF_KIND_TAG
Quoted 125 lines · Yonghong Song
Quoted 69 lines · Andrii Nakryiko
Quoted 38 lines · Yonghong Song
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.For linux kernel, the
btf_tagcan be applied in various places to specify user pointer, function pre- or post- condition, function allow/deny in certain context, etc. Such information will be encoded in vmlinux BTF and can be used by verifier.The
btf_tagcan also be applied to bpf programs to help global verifiable functions, e.g., specifying preconditions, etc.This patch added basic parsing and checking support in kernel for new
BTF_KIND_TAGkind.[1] https://reviews.llvm.org/D106614
[2] https://reviews.llvm.org/D106621
[3] https://reviews.llvm.org/D106622
[4] https://reviews.llvm.org/D109560Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/btf.h | 16 ++++- kernel/bpf/btf.c | 120 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/btf.h | 16 ++++- 3 files changed, 148 insertions(+), 4 deletions(-)[...]
Quoted 27 lines
kernel/bpf/btf.c +26
-0+static s32 btf_tag_check_meta (struct btf_verifier_env *env, +const struct btf_type *t, +u32 meta_left) +{ +const struct btf_tag *tag; +u32 meta_needed =sizeof (*tag); +const char *value; + +if (meta_left < meta_needed) { +btf_verifier_log_basic (env, t, +"meta_left:%u meta_needed:%u" , + meta_left, meta_needed); +return -EINVAL ; + } + + value =btf_name_by_offset (env->btf, t->name_off); +if (!value || !value[0 ]) { +btf_verifier_log_type (env, t,"Invalid value" ); +return -EINVAL ; + } + +if (btf_type_vlen (t)) { +btf_verifier_log_type (env, t,"vlen != 0" ); +return -EINVAL ; + } +Quoted 8 lines
kernel/bpf/btf.c static const struct btf_kind_operations float_ops = { · line 3853
Quoted 5 lines
kernel/bpf/btf.c +5
-0+if (btf_type_kflag (t)) { +btf_verifier_log_type (env, t,"Invalid btf_info kind_flag" ); +return -EINVAL ; + } +probably need to enforce that
component_idxis >= -1? -2 is not a valid supported value right now.I tested below. But I can test here for kernel practice, testing error case earlier.
Quoted 23 lines
Quoted 23 lines
kernel/bpf/btf.c +22
-0+btf_verifier_log_type (env, t,NULL ); + +return meta_needed; +} + +static int btf_tag_resolve (struct btf_verifier_env *env, +const struct resolve_vertex *v) +{ +const struct btf_type *next_type; +const struct btf_type *t = v->t; +u32 next_type_id = t->type; +struct btf *btf = env->btf; +s32 component_idx; +u32 vlen; + + next_type =btf_type_by_id (btf, next_type_id); +if (!next_type || !btf_type_is_tag_target (next_type)) { +btf_verifier_log_type (env, v->t,"Invalid type_id" ); +return -EINVAL ; + } + + if (!env_type_is_resolve_sink(env, next_type) &&Quoted 8 lines
kernel/bpf/btf.c static const struct btf_kind_operations float_ops = { · line 3880
Quoted 5 lines
kernel/bpf/btf.c +5
-0+ !env_type_is_resolved(env, next_type_id)) +return env_stack_push (env, next_type, next_type_id); + + component_idx =btf_type_tag (t)->component_idx; +if (component_idx !=-1 ) {so here, if it's -2, that should be an error, but currently will be ignored, right?
It is not. See below. At this point,
component_idxcould be -2 or 0 or 1 ...Quoted 5 lines
Quoted 2 lines
+ if (
btf_type_is_var(next_type)||component_idx< 0) {if
is_var(next_type)thencomponent_idxshould only be -1, nothing else. Or am I missing some convention?
Quoted 7 lines
So if it is a variable, the error will return.
If it is not a variable and
component_idx< 0 (-2 in this case), return error. So we do test -2 here.I will restructure the code to test < -1 earlier, so we won't have confusion here.
Oh, I've read this a few times and every single time I read it as (btf_type_is_var() && component_idx < 0). It makes sense now, but it is a bit convoluted to follow the checks. Thanks for improving!
Quoted 27 lines
Quoted 27 lines
Quoted 24 lines
kernel/bpf/btf.c +22
-0+btf_verifier_log_type (env, v->t,"Invalid component_idx" ); +return -EINVAL ; + } + +if (btf_type_is_struct (next_type)) { + vlen =btf_type_vlen (next_type); + }else { + next_type =btf_type_by_id (btf, next_type->type); + vlen =btf_type_vlen (next_type); + } + +if ((u32 )component_idx >= vlen) { +btf_verifier_log_type (env, v->t,"Invalid component_idx" ); +return -EINVAL ; + } + } + +env_stack_pop_resolved (env, next_type_id,0 ); + +return 0 ; +} +[...]
PATCH v2 3/11 libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag
In reply to Yonghong SongThis patch renames functions btf_{hash,equal}_int() to btf_{hash,equal}_int_tag() so they can be reused for BTF_KIND_TAG support. There is no functionality change for this patch.
--- tools/lib/bpf/btf.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/lib/bpf/btf.c +8 -8
@@ -3256,8 +3256,8 @@ static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2) t1->size == t2->size; } -/* Calculate type signature hash of INT. */ -static long btf_hash_int (struct btf_type *t) +/* Calculate type signature hash of INT or TAG. */ +static long btf_hash_int_tag (struct btf_type *t) {__u32 info = *(__u32 *)(t +1 );long h;
@@ -3267,8 +3267,8 @@ static long btf_hash_int(struct btf_type *t)return h; } -/* Check structural equality of two INTs. */ -static bool btf_equal_int (struct btf_type *t1,struct btf_type *t2) +/* Check structural equality of two INTs or TAGs. */ +static bool btf_equal_int_tag (struct btf_type *t1,struct btf_type *t2) {__u32 info1, info2;
@@ -3535,7 +3535,7 @@ static int btf_dedup_prep(struct btf_dedup *d) h =btf_hash_common (t);break ;case BTF_KIND_INT : - h =btf_hash_int (t); + h =btf_hash_int_tag (t);break ;case BTF_KIND_ENUM : h =btf_hash_enum (t);
@@ -3593,11 +3593,11 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)return 0 ;case BTF_KIND_INT : - h =btf_hash_int (t); + h =btf_hash_int_tag (t);for_each_dedup_cand (d, hash_entry, h) { cand_id = (__u32 )(long )hash_entry->value; cand =btf_type_by_id (d->btf, cand_id); -if (btf_equal_int (t, cand)) { +if (btf_equal_int_tag (t, cand)) { new_id = cand_id;break ; }
@@ -3881,7 +3881,7 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,switch (cand_kind) {case BTF_KIND_INT : -return btf_equal_int (cand_type, canon_type); +return btf_equal_int_tag (cand_type, canon_type);case BTF_KIND_ENUM :if (d->opts.dont_resolve_fwds)
Signature · 1 line
2.30.2
PATCH v2 4/11 libbpf: add support for BTF_KIND_TAG
In reply to Yonghong SongAdd BTF_KIND_TAG support for parsing and dedup.
Also added sanitization for BTF_KIND_TAG. If BTF_KIND_TAG is not supported in the kernel, sanitize it to INTs.
--- tools/lib/bpf/btf.c | 69 +++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 15 +++++++ tools/lib/bpf/btf_dump.c | 3 ++ tools/lib/bpf/libbpf.c | 31 +++++++++++++-- tools/lib/bpf/libbpf.map | 5 +++ tools/lib/bpf/libbpf_internal.h | 2 + 6 files changed, 122 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +69 -0
@@ -304,6 +304,8 @@ static int btf_type_size(const struct btf_type *t)return base_size +sizeof (struct btf_var );case BTF_KIND_DATASEC :return base_size + vlen *sizeof (struct btf_var_secinfo ); +case BTF_KIND_TAG : +return base_size +sizeof (struct btf_tag );default :pr_debug ("Unsupported BTF_KIND:%u\n" ,btf_kind (t));return -EINVAL ;
@@ -376,6 +378,9 @@ static int btf_bswap_type_rest(struct btf_type *t) v->size =bswap_32 (v->size); }return 0 ; +case BTF_KIND_TAG : +btf_tag (t)->component_idx =bswap_32 (btf_tag (t)->component_idx); +return 0 ;default :pr_debug ("Unsupported BTF_KIND:%u\n" ,btf_kind (t));return -EINVAL ;
@@ -586,6 +591,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)case BTF_KIND_CONST :case BTF_KIND_RESTRICT :case BTF_KIND_VAR : +case BTF_KIND_TAG : type_id = t->type;break ;case BTF_KIND_ARRAY :
@@ -2440,6 +2446,49 @@ int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __return 0 ; } +/* +* Append new BTF_KIND_TAG type with: +* - *value* - non-empty/non-NULL string; +* - *ref_type_id* - referenced type ID, it might not exist yet; +* - *component_idx* - -1 for tagging reference type, otherwise struct/union +* member or function argument index; +* Returns: +* - >0, type ID of newly added BTF type; +* - <0, on error. +*/ +int btf__add_tag (struct btf *btf,const char *value,int ref_type_id, +int component_idx) +{ +bool for_ref_type = false; +struct btf_type *t; +int sz, value_off; + +if (!value || !value[0 ] || component_idx <-1 ) +return libbpf_err (-EINVAL ); + +if (validate_type_id (ref_type_id)) +return libbpf_err (-EINVAL ); + +if (btf_ensure_modifiable (btf)) +return libbpf_err (-ENOMEM ); + + sz =sizeof (struct btf_type ) +sizeof (struct btf_tag ); + t =btf_add_type_mem (btf, sz); +if (!t) +return libbpf_err (-ENOMEM ); + + value_off =btf__add_str (btf, value); +if (value_off <0 ) +return value_off; + + t->name_off = value_off; + t->info =btf_type_info (BTF_KIND_TAG ,0 , for_ref_type); + t->type = ref_type_id; + ((struct btf_tag *)(t +1 ))->component_idx = component_idx; + +return btf_commit_type (btf, sz); +} +struct btf_ext_sec_setup_param {__u32 off;__u32 len;
@@ -3535,6 +3584,7 @@ static int btf_dedup_prep(struct btf_dedup *d) h =btf_hash_common (t);break ;case BTF_KIND_INT : +case BTF_KIND_TAG : h =btf_hash_int_tag (t);break ;case BTF_KIND_ENUM :
@@ -3590,6 +3640,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)case BTF_KIND_FUNC_PROTO :case BTF_KIND_VAR :case BTF_KIND_DATASEC : +case BTF_KIND_TAG :return 0 ;case BTF_KIND_INT :
@@ -4210,6 +4261,23 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id) }break ; +case BTF_KIND_TAG : + ref_type_id =btf_dedup_ref_type (d, t->type); +if (ref_type_id <0 ) +return ref_type_id; + t->type = ref_type_id; + + h =btf_hash_int_tag (t); +for_each_dedup_cand (d, hash_entry, h) { + cand_id = (__u32 )(long )hash_entry->value; + cand =btf_type_by_id (d->btf, cand_id); +if (btf_equal_int_tag (t, cand)) { + new_id = cand_id; +break ; + } + } +break ; +case BTF_KIND_ARRAY : {struct btf_array *info =btf_array (t);
@@ -4482,6 +4550,7 @@ int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctcase BTF_KIND_TYPEDEF :case BTF_KIND_FUNC :case BTF_KIND_VAR : +case BTF_KIND_TAG :return visit (&t->type, ctx);case BTF_KIND_ARRAY : {
tools/lib/bpf/btf.h +15 -0
@@ -143,6 +143,10 @@ LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,__u32 offset,__u32 byte_sz); +/* tag construction API */ +LIBBPF_API int btf__add_tag(struct btf *btf, const char *value, int ref_type_id, +int component_idx); +struct btf_dedup_opts {unsigned int dedup_table_size;bool dont_resolve_fwds;
@@ -330,6 +334,11 @@ static inline bool btf_is_float(const struct btf_type *t)return btf_kind (t) ==BTF_KIND_FLOAT ; } +static inline bool btf_is_tag (const struct btf_type *t) +{ +return btf_kind (t) ==BTF_KIND_TAG ; +} +static inline __u8 btf_int_encoding (const struct btf_type *t) {return BTF_INT_ENCODING (*(__u32 *)(t +1 ));
@@ -398,6 +407,12 @@ btf_var_secinfos(const struct btf_type *t)return (struct btf_var_secinfo *)(t +1 ); } +struct btf_tag ; +static inline struct btf_tag *btf_tag (const struct btf_type *t) +{ +return (struct btf_tag *)(t +1 ); +} + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/btf_dump.c +3 -0
@@ -316,6 +316,7 @@ static int btf_dump_mark_referenced(struct btf_dump *d)case BTF_KIND_TYPEDEF :case BTF_KIND_FUNC :case BTF_KIND_VAR : +case BTF_KIND_TAG : d->type_states[t->type].referenced =1 ;break ;
@@ -583,6 +584,7 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)case BTF_KIND_FUNC :case BTF_KIND_VAR :case BTF_KIND_DATASEC : +case BTF_KIND_TAG : d->type_states[id].order_state =ORDERED ;return 0 ;
@@ -2215,6 +2217,7 @@ static int btf_dump_dump_type_data(struct btf_dump *d,case BTF_KIND_FWD :case BTF_KIND_FUNC :case BTF_KIND_FUNC_PROTO : +case BTF_KIND_TAG : err =btf_dump_unsupported_data (d, t, id);break ;case BTF_KIND_INT :
tools/lib/bpf/libbpf.c +28 -3
@@ -195,6 +195,8 @@ enum kern_feature_id {FEAT_BTF_FLOAT ,/* BPF perf link support */ FEAT_PERF_LINK , +/* BTF_KIND_ATTR support */ +FEAT_BTF_TAG , __FEAT_CNT, };
@@ -1987,6 +1989,7 @@ static const char *__btf_kind_str(__u16 kind)case BTF_KIND_VAR :return "var" ;case BTF_KIND_DATASEC :return "datasec" ;case BTF_KIND_FLOAT :return "float" ; +case BTF_KIND_TAG :return "tag" ;default :return "unknown" ; } }
@@ -2486,8 +2489,9 @@ static bool btf_needs_sanitization(struct bpf_object *obj)bool has_datasec =kernel_supports (obj,FEAT_BTF_DATASEC );bool has_float =kernel_supports (obj,FEAT_BTF_FLOAT );bool has_func =kernel_supports (obj,FEAT_BTF_FUNC ); +bool has_tag =kernel_supports (obj,FEAT_BTF_TAG ); -return !has_func || !has_datasec || !has_func_global || !has_float; +return !has_func || !has_datasec || !has_func_global || !has_float || !has_tag; }static void bpf_object__sanitize_btf (struct bpf_object *obj,struct btf *btf)
@@ -2496,14 +2500,15 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)bool has_datasec =kernel_supports (obj,FEAT_BTF_DATASEC );bool has_float =kernel_supports (obj,FEAT_BTF_FLOAT );bool has_func =kernel_supports (obj,FEAT_BTF_FUNC ); +bool has_tag =kernel_supports (obj,FEAT_BTF_TAG );struct btf_type *t;int i, j, vlen;for (i =1 ; i <=btf__get_nr_types (btf); i++) { t = (struct btf_type *)btf__type_by_id (btf, i); -if (!has_datasec &&btf_is_var (t)) { -/* replace VAR with INT */ +if ((!has_datasec &&btf_is_var (t)) || (!has_tag &&btf_is_tag (t))) { +/* replace VAR/TAG with INT */ t->info =BTF_INFO_ENC (BTF_KIND_INT ,0 ,0 );/* * using size = 1 is the safest choice, 4 will be too
@@ -4213,6 +4218,23 @@ static int probe_kern_btf_float(void) strs, sizeof(strs))); } +static int probe_kern_btf_tag (void ) +{ +static const char strs[] ="\0tag" ; +__u32 types[] = { +/* int */ +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +/* VAR x */ /* [2] */ +BTF_TYPE_ENC (1 ,BTF_INFO_ENC (BTF_KIND_VAR ,0 ,0 ),1 ), +BTF_VAR_STATIC , +/* attr */ +BTF_TYPE_TAG_ENC (1 ,2 ,-1 ), + }; + +return probe_fd (libbpf__load_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs))); +} +static int probe_kern_array_mmap (void ) {struct bpf_create_map_attr attr = {
@@ -4429,6 +4451,9 @@ static struct kern_feature_desc { [FEAT_PERF_LINK ] = {"BPF perf link support" , probe_perf_link, }, + [FEAT_BTF_TAG ] = { +"BTF_KIND_TAG support" , probe_kern_btf_tag, + }, };static bool kernel_supports (const struct bpf_object *obj,enum kern_feature_id feat_id)
tools/lib/bpf/libbpf.map +5 -0
@@ -386,3 +386,8 @@ LIBBPF_0.5.0 { btf_dump__dump_type_data; libbpf_set_strict_mode; } LIBBPF_0.4.0; + +LIBBPF_0.6.0 { + global: + btf__add_tag; +} LIBBPF_0.5.0;
tools/lib/bpf/libbpf_internal.h +2 -0
@@ -69,6 +69,8 @@#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size)#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_TYPE_TAG_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx)#ifndef likely#define likely (x) __builtin_expect(!!(x), 1)
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 04/11] libbpf: add support for BTF_KIND_TAG
Quoted 6 lines · Yonghong Song
Add
BTF_KIND_TAGsupport for parsing and dedup.
Also added sanitization forBTF_KIND_TAG. IfBTF_KIND_TAGis not supported in the kernel, sanitize it to INTs.Signed-off-by Yonghong Song <[email protected]>
Quoted 9 lines
--- tools/lib/bpf/btf.c | 69 +++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 15 +++++++ tools/lib/bpf/btf_dump.c | 3 ++ tools/lib/bpf/libbpf.c | 31 +++++++++++++-- tools/lib/bpf/libbpf.map | 5 +++ tools/lib/bpf/libbpf_internal.h | 2 + 6 files changed, 122 insertions(+), 3 deletions(-)
Just a few small nits.
[...]
Quoted 8 lines
tools/lib/bpf/btf.c +4
-0@@ -2440,6 +2446,49 @@ int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __return 0 ; } +/* +* Append new BTF_KIND_TAG type with: +* - *value* - non-empty/non-NULL string; +* - *ref_type_id* - referenced type ID, it might not exist yet;
tools/lib/bpf/btf.c int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __ · line 2462
Quoted 10 lines
tools/lib/bpf/btf.c +10
-0+* - *component_idx* - -1 for tagging reference type, otherwise struct/union +* member or function argument index; +* Returns: +* - >0, type ID of newly added BTF type; +* - <0, on error. +*/ +int btf__add_tag (struct btf *btf,const char *value,int ref_type_id, +int component_idx) +{ +bool for_ref_type = false;
leftovers from the previous revision? let's just use 0 for kflag argument below
Quoted 15 lines
tools/lib/bpf/btf.c +15
-0+struct btf_type *t; +int sz, value_off; + +if (!value || !value[0 ] || component_idx <-1 ) +return libbpf_err (-EINVAL ); + +if (validate_type_id (ref_type_id)) +return libbpf_err (-EINVAL ); + +if (btf_ensure_modifiable (btf)) +return libbpf_err (-ENOMEM ); + + sz =sizeof (struct btf_type ) +sizeof (struct btf_tag ); + t =btf_add_type_mem (btf, sz); +if (!t)
tools/lib/bpf/btf.c int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __ · line 2487
Quoted 10 lines
tools/lib/bpf/btf.c +10
-0+return libbpf_err (-ENOMEM ); + + value_off =btf__add_str (btf, value); +if (value_off <0 ) +return value_off; + + t->name_off = value_off; + t->info =btf_type_info (BTF_KIND_TAG ,0 , for_ref_type); + t->type = ref_type_id; + ((struct btf_tag *)(t +1 ))->component_idx = component_idx;
nit: btf_tag(t)->component_idx
Quoted 7 lines
tools/lib/bpf/btf.c +4
-0+ +return btf_commit_type (btf, sz); +} +struct btf_ext_sec_setup_param {__u32 off;__u32 len;
[...]
tools/lib/bpf/libbpf.c enum kern_feature_id { · line 198
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-0@@ -195,6 +195,8 @@ enum kern_feature_id {FEAT_BTF_FLOAT ,/* BPF perf link support */ FEAT_PERF_LINK, +/* BTF_KIND_ATTR support */
s/BTF_KIND_ATTR/BTF_KIND_TAG/
Quoted 4 lines
tools/lib/bpf/libbpf.c +1
-0+FEAT_BTF_TAG , __FEAT_CNT, };
[...]
Quoted 6 lines
static bool
kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)tools/lib/bpf/libbpf.map +0
-0
tools/lib/bpf/libbpf.map LIBBPF_0.5.0 { · line 393
Quoted 8 lines
tools/lib/bpf/libbpf.map +5
-0@@ -386,3 +386,8 @@ LIBBPF_0.5.0 { btf_dump__dump_type_data; libbpf_set_strict_mode; } LIBBPF_0.4.0; + +LIBBPF_0.6.0 { + global: + btf__add_tag; +} LIBBPF_0.5.0;
you'll need a rebase for this due to my patch for libbpf_version.h, sorry
Quoted 16 lines
tools/lib/bpf/libbpf_internal.h +2
-1@@ -69,6 +69,8 @@#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size)#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_TYPE_TAG_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx)#ifndef likely#define likely (x) __builtin_expect(!!(x), 1) --2.30.2
Re: [PATCH bpf-next v2 04/11] libbpf: add support for BTF_KIND_TAG
Quoted 17 lines · Andrii Nakryiko
Quoted 6 lines · Yonghong Song
Add
BTF_KIND_TAGsupport for parsing and dedup.
Also added sanitization forBTF_KIND_TAG. IfBTF_KIND_TAGis not supported in the kernel, sanitize it to INTs.Signed-off-by Yonghong Song <[email protected]>Quoted 9 lines
--- tools/lib/bpf/btf.c | 69 +++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 15 +++++++ tools/lib/bpf/btf_dump.c | 3 ++ tools/lib/bpf/libbpf.c | 31 +++++++++++++-- tools/lib/bpf/libbpf.map | 5 +++ tools/lib/bpf/libbpf_internal.h | 2 + 6 files changed, 122 insertions(+), 3 deletions(-)
Quoted 1 line
Just a few small nits.
Ack for all nits. All these are indeed missed leftovers from previous patchset/changes.
Quoted 114 lines
[...]
Quoted 8 lines
tools/lib/bpf/btf.c +4
-0@@ -2440,6 +2446,49 @@ int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __return 0 ; } +/* +* Append new BTF_KIND_TAG type with: +* - *value* - non-empty/non-NULL string; +* - *ref_type_id* - referenced type ID, it might not exist yet; tools/lib/bpf/btf.c int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __ · line 2462
Quoted 10 lines
tools/lib/bpf/btf.c +10
-0+* - *component_idx* - -1 for tagging reference type, otherwise struct/union +* member or function argument index; +* Returns: +* - >0, type ID of newly added BTF type; +* - <0, on error. +*/ +int btf__add_tag (struct btf *btf,const char *value,int ref_type_id, +int component_idx) +{ +bool for_ref_type = false;leftovers from the previous revision? let's just use 0 for kflag argument below
Quoted 15 lines
tools/lib/bpf/btf.c +15
-0+struct btf_type *t; +int sz, value_off; + +if (!value || !value[0 ] || component_idx <-1 ) +return libbpf_err (-EINVAL ); + +if (validate_type_id (ref_type_id)) +return libbpf_err (-EINVAL ); + +if (btf_ensure_modifiable (btf)) +return libbpf_err (-ENOMEM ); + + sz =sizeof (struct btf_type ) +sizeof (struct btf_tag ); + t =btf_add_type_mem (btf, sz); +if (!t)tools/lib/bpf/btf.c int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __ · line 2487
Quoted 10 lines
tools/lib/bpf/btf.c +10
-0+return libbpf_err (-ENOMEM ); + + value_off =btf__add_str (btf, value); +if (value_off <0 ) +return value_off; + + t->name_off = value_off; + t->info =btf_type_info (BTF_KIND_TAG ,0 , for_ref_type); + t->type = ref_type_id; + ((struct btf_tag *)(t +1 ))->component_idx = component_idx;nit: btf_tag(t)->component_idx
Quoted 7 lines
tools/lib/bpf/btf.c +4
-0+ +return btf_commit_type (btf, sz); +} +struct btf_ext_sec_setup_param {__u32 off;__u32 len;[...]
tools/lib/bpf/libbpf.c enum kern_feature_id { · line 198
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-0@@ -195,6 +195,8 @@ enum kern_feature_id {FEAT_BTF_FLOAT ,/* BPF perf link support */ FEAT_PERF_LINK, +/* BTF_KIND_ATTR support */ s/BTF_KIND_ATTR/BTF_KIND_TAG/
Quoted 4 lines
tools/lib/bpf/libbpf.c +1
-0+FEAT_BTF_TAG , __FEAT_CNT, };[...]
Quoted 6 lines
static bool
kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)tools/lib/bpf/libbpf.map +0
-0tools/lib/bpf/libbpf.map LIBBPF_0.5.0 { · line 393
Quoted 8 lines
tools/lib/bpf/libbpf.map +5
-0@@ -386,3 +386,8 @@ LIBBPF_0.5.0 { btf_dump__dump_type_data; libbpf_set_strict_mode; } LIBBPF_0.4.0; + +LIBBPF_0.6.0 { + global: + btf__add_tag; +} LIBBPF_0.5.0;you'll need a rebase for this due to my patch for
libbpf_version.h, sorryQuoted 16 lines
tools/lib/bpf/libbpf_internal.h +2
-1@@ -69,6 +69,8 @@#define BTF_VAR_SECINFO_ENC (type, offset, size) (type), (offset), (size)#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_TYPE_TAG_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx)#ifndef likely#define likely (x) __builtin_expect(!!(x), 1) --2.30.2
PATCH v2 5/11 bpftool: add support for BTF_KIND_TAG
In reply to Yonghong SongAdded bpftool support to dump BTF_KIND_TAG information. The new bpftool will be used in later patches to dump btf in the test bpf program object file.
Currently, the tags are not emitted with
bpftool btf dump file <path> format c
and they are silently ignored. The tag information is mostly used in the kernel for verification purpose and the kernel uses its own btf to check. With adding these tags
to vmlinux.h, tags will be encoded in program's btf but they will not be used by the kernel, at least for now. So let us delay adding these tags to format C header files until there is a real need.
--- tools/bpf/bpftool/btf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/bpf/bpftool/btf.c +12 -0
@@ -37,6 +37,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = { [BTF_KIND_VAR] = "VAR", [BTF_KIND_DATASEC] = "DATASEC", [BTF_KIND_FLOAT] = "FLOAT", + [BTF_KIND_TAG] = "TAG", };struct btf_attach_table {
@@ -347,6 +348,17 @@ static int dump_btf_type(const struct btf *btf, __u32 id,printf (" size=%u" , t->size);break ; } +case BTF_KIND_TAG : { +const struct btf_tag *tag = (const void *)(t +1 ); + +if (json_output) { +jsonw_uint_field (w,"type_id" , t->type); +jsonw_int_field (w,"component_idx" , tag->component_idx); + }else { +printf (" type_id=%u component_idx=%d" , t->type, tag->component_idx); + } +break ; + }default :break ; }
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 05/11] bpftool: add support for BTF_KIND_TAG
Quoted 15 lines · Yonghong Song
Added bpftool support to dump
BTF_KIND_TAGinformation. The new bpftool will be used in later patches to dump btf in the test bpf program object file.Currently, the tags are not emitted with
bpftool btf dump file <path> format c
and they are silently ignored. The tag information is mostly used in the kernel for verification purpose and the kernel uses its own btf to check. With adding these tags
tovmlinux.h, tags will be encoded in program's btf but they will not be used by the kernel, at least for now. So let us delay adding these tags to format C header files until there is a real need.
Quoted 2 lines
Signed-off-by Yonghong Song <[email protected]>---
Quoted 36 lines
tools/bpf/bpftool/btf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)tools/bpf/bpftool/btf.c +12
-1@@ -37,6 +37,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = { [BTF_KIND_VAR] = "VAR", [BTF_KIND_DATASEC] = "DATASEC", [BTF_KIND_FLOAT] = "FLOAT", + [BTF_KIND_TAG] = "TAG", };struct btf_attach_table {@@ -347,6 +348,17 @@ static int dump_btf_type(const struct btf *btf, __u32 id,printf (" size=%u" , t->size);break ; } +case BTF_KIND_TAG : { +const struct btf_tag *tag = (const void *)(t +1 ); + +if (json_output) { +jsonw_uint_field (w,"type_id" , t->type); +jsonw_int_field (w,"component_idx" , tag->component_idx); + }else { +printf (" type_id=%u component_idx=%d" , t->type, tag->component_idx); + } +break ; + }default :break ; } --2.30.2
PATCH v2 6/11 selftests/bpf: test libbpf API function btf__add_tag()
In reply to Yonghong SongAdd btf_write tests with btf__add_tag() function.
--- tools/testing/selftests/bpf/btf_helpers.c | 7 ++++++- .../selftests/bpf/prog_tests/btf_write.c | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/btf_helpers.c +6 -1
@@ -24,11 +24,12 @@ static const char * const btf_kind_str_mapping[] = { [BTF_KIND_VAR] = "VAR", [BTF_KIND_DATASEC] = "DATASEC", [BTF_KIND_FLOAT] = "FLOAT", + [BTF_KIND_TAG] = "TAG", };static const char *btf_kind_str (__u16 kind) { -if (kind >BTF_KIND_DATASEC ) +if (kind >BTF_KIND_TAG )return "UNKNOWN" ;return btf_kind_str_mapping[kind]; }
@@ -177,6 +178,10 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)case BTF_KIND_FLOAT :fprintf (out," size=%u" , t->size);break ; +case BTF_KIND_TAG : +fprintf (out," type_id=%u component_idx=%d" , + t->type,btf_tag (t)->component_idx); +break ;default :break ; }
tools/testing/selftests/bpf/prog_tests/btf_write.c +21 -0
@@ -281,5 +281,26 @@ void test_btf_write() {"[17] DATASEC 'datasec1' size=12 vlen=1\n" "\ttype_id=1 offset=4 size=8", "raw_dump"); +/* TAG */ + id =btf__add_tag (btf,"tag1" ,16 ,-1 ); +ASSERT_EQ (id,18 ,"tag_id" ); + t =btf__type_by_id (btf,18 ); +ASSERT_STREQ (btf__str_by_offset (btf, t->name_off),"tag1" ,"tag_value" ); +ASSERT_EQ (btf_kind (t),BTF_KIND_TAG ,"tag_kind" ); +ASSERT_EQ (t->type,16 ,"tag_type" ); +ASSERT_EQ (btf_tag (t)->component_idx,-1 ,"tag_component_idx" ); +ASSERT_STREQ (btf_type_raw_dump (btf,18 ), +"[18] TAG 'tag1' type_id=16 component_idx=-1" ,"raw_dump" ); + + id =btf__add_tag (btf,"tag2" ,14 ,1 ); +ASSERT_EQ (id,19 ,"tag_id" ); + t =btf__type_by_id (btf,19 ); +ASSERT_STREQ (btf__str_by_offset (btf, t->name_off),"tag2" ,"tag_value" ); +ASSERT_EQ (btf_kind (t),BTF_KIND_TAG ,"tag_kind" ); +ASSERT_EQ (t->type,14 ,"tag_type" ); +ASSERT_EQ (btf_tag (t)->component_idx,1 ,"tag_component_idx" ); +ASSERT_STREQ (btf_type_raw_dump (btf,19 ), +"[19] TAG 'tag2' type_id=14 component_idx=1" ,"raw_dump" ); +btf__free (btf); }
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 06/11] selftests/bpf: test libbpf API function btf__add_tag()
Quoted 5 lines · Yonghong Song
Add
btf_writetests withbtf__add_tag()function.Signed-off-by Yonghong Song <[email protected]>---
LGTM.
Quoted 4 lines
tools/testing/selftests/bpf/btf_helpers.c | 7 ++++++- .../selftests/bpf/prog_tests/btf_write.c | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
[...]
PATCH v2 7/11 selftests/bpf: change NAME_NTH/IS_NAME_NTH for BTF_KIND_TAG format
In reply to Yonghong SongBTF_KIND_TAG ELF format has a component_idx which might have value -1. test_btf may confuse it with common_type.name as NAME_NTH checkes high 16bit to be 0xffff. Change NAME_NTH high 16bit check to be 0xfffe so it won't confuse with component_idx.
--- tools/testing/selftests/bpf/prog_tests/btf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +2 -2
@@ -39,8 +39,8 @@ static bool always_log;#define BTF_END_RAW 0xdeadbeef#define NAME_TBD 0xdeadb33f -#define NAME_NTH (N ) (0xffff0000 | N) -#define IS_NAME_NTH (X ) ((X & 0xffff0000) == 0xffff0000) +#define NAME_NTH (N ) (0xfffe0000 | N) +#define IS_NAME_NTH (X ) ((X & 0xffff0000) == 0xfffe0000)#define GET_NAME_NTH_IDX (X ) (X & 0x0000ffff)#define MAX_NR_RAW_U32 1024
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 07/11] selftests/bpf: change NAME_NTH/IS_NAME_NTH for BTF_KIND_TAG format
Quoted 8 lines · Yonghong Song
BTF_KIND_TAGELF format has acomponent_idxwhich might have value -1.test_btfmay confuse it with common_type.name asNAME_NTHcheckes high 16bit to be0xffff. ChangeNAME_NTHhigh 16bit check to be0xfffeso it won't confuse withcomponent_idx.Signed-off-by Yonghong Song <[email protected]>---
Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/btf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)tools/testing/selftests/bpf/prog_tests/btf.c +2
-3@@ -39,8 +39,8 @@ static bool always_log;#define BTF_END_RAW 0xdeadbeef#define NAME_TBD 0xdeadb33f -#define NAME_NTH (N ) (0xffff0000 | N) -#define IS_NAME_NTH (X ) ((X & 0xffff0000) == 0xffff0000) +#define NAME_NTH (N ) (0xfffe0000 | N) +#define IS_NAME_NTH (X ) ((X & 0xffff0000) == 0xfffe0000)#define GET_NAME_NTH_IDX (X ) (X & 0x0000ffff)#define MAX_NR_RAW_U32 1024 --2.30.2
PATCH v2 8/11 selftests/bpf: add BTF_KIND_TAG unit tests
In reply to Yonghong SongTest good and bad variants of BTF_KIND_TAG encoding.
--- tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++ tools/testing/selftests/bpf/test_btf.h | 3 + 2 files changed, 226 insertions(+)
tools/testing/selftests/bpf/prog_tests/btf.c +223 -0
@@ -3661,6 +3661,227 @@ static struct btf_raw_test raw_tests[] = { .err_str = "Invalid type_size", }, +{ + .descr = "tag test #1, struct/member, well-formed", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_STRUCT_ENC(0, 2, 8), /* [2] */ + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_MEMBER_ENC(NAME_TBD, 1, 32), + BTF_TAG_ENC(NAME_TBD, 2, -1), + BTF_TAG_ENC(NAME_TBD, 2, 0), + BTF_TAG_ENC(NAME_TBD, 2, 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 8, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, +}, +{ + .descr = "tag test #2, union/member, well-formed", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_STRUCT_ENC(NAME_TBD, 2, 4), /* [2] */ + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_TAG_ENC(NAME_TBD, 2, -1), + BTF_TAG_ENC(NAME_TBD, 2, 0), + BTF_TAG_ENC(NAME_TBD, 2, 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, +}, +{ + .descr = "tag test #3, variable, well-formed", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */ + BTF_VAR_ENC(NAME_TBD, 1, 1), /* [3] */ + BTF_TAG_ENC(NAME_TBD, 2, -1), + BTF_TAG_ENC(NAME_TBD, 3, -1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0global\0tag1\0tag2"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, +}, +{ + .descr = "tag test #4, func/parameter, well-formed", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_FUNC_PROTO_ENC(0, 2), /* [2] */ + BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1), + BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1), + BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */ + BTF_TAG_ENC(NAME_TBD, 3, -1), + BTF_TAG_ENC(NAME_TBD, 3, 0), + BTF_TAG_ENC(NAME_TBD, 3, 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0arg1\0arg2\0f\0tag1\0tag2\0tag3"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, +}, +{ + .descr = "tag test #5, invalid value", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */ + BTF_TAG_ENC(0, 2, -1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0tag"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid value", +}, +{ + .descr = "tag test #6, invalid target type", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_TAG_ENC(NAME_TBD, 1, -1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0tag1"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid type", +}, +{ + .descr = "tag test #7, invalid vlen", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 1), 2), (0), + BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0tag1"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "vlen != 0", +}, +{ + .descr = "tag test #8, invalid kflag", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 0), 2), (-1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0tag1"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid btf_info kind_flag", +}, +{ + .descr = "tag test #9, var, invalid component_idx", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), 2), (0), + BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0tag"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +{ + .descr = "tag test #10, struct member, invalid component_idx", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_STRUCT_ENC(0, 2, 8), /* [2] */ + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_MEMBER_ENC(NAME_TBD, 1, 32), + BTF_TAG_ENC(NAME_TBD, 2, 2), + BTF_END_RAW, + }, + BTF_STR_SEC("\0m1\0m2\0tag"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 8, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +{ + .descr = "tag test #11, func parameter, invalid component_idx", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_FUNC_PROTO_ENC(0, 2), /* [2] */ + BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1), + BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1), + BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */ + BTF_TAG_ENC(NAME_TBD, 3, 2), + BTF_END_RAW, + }, + BTF_STR_SEC("\0arg1\0arg2\0f\0tag"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, + }; /* struct btf_raw_test raw_tests[] */ static const char *get_next_str(const char *start, const char *end)
@@ -6801,6 +7022,8 @@ static int btf_type_size(const struct btf_type *t)return base_size +sizeof (struct btf_var );case BTF_KIND_DATASEC :return base_size + vlen *sizeof (struct btf_var_secinfo ); +case BTF_KIND_TAG : +return base_size +sizeof (struct btf_tag );default :fprintf (stderr,"Unsupported BTF_KIND:%u\n" , kind);return -EINVAL ;
tools/testing/selftests/bpf/test_btf.h +3 -0
@@ -69,4 +69,7 @@#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_TAG_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx) +#endif /* _TEST_BTF_H */
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 08/11] selftests/bpf: add BTF_KIND_TAG unit tests
Quoted 33 lines · Yonghong Song
Test good and bad variants of
BTF_KIND_TAGencoding.Signed-off-by Yonghong Song <[email protected]>--- tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++ tools/testing/selftests/bpf/test_btf.h | 3 + 2 files changed, 226 insertions(+)tools/testing/selftests/bpf/prog_tests/btf.c +16
-0@@ -3661,6 +3661,227 @@ static struct btf_raw_test raw_tests[] = { .err_str = "Invalid type_size", }, +{ + .descr = "tag test #1, struct/member, well-formed", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_STRUCT_ENC (0 ,2 ,8 ),/* [2] */ +BTF_MEMBER_ENC (NAME_TBD ,1 ,0 ), +BTF_MEMBER_ENC (NAME_TBD ,1 ,32 ), +BTF_TAG_ENC (NAME_TBD ,2 ,-1 ), +BTF_TAG_ENC (NAME_TBD ,2 ,0 ), +BTF_TAG_ENC (NAME_TBD ,2 ,1 ), +BTF_END_RAW , + }, +BTF_STR_SEC ("\0m1\0m2\0tag1\0tag2\0tag3" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int),
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3689
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf.c +10
-0+ .value_size = 8, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, +}, +{ + .descr = "tag test #2, union/member, well-formed", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ + BTF_STRUCT_ENC(NAME_TBD, 2, 4), /* [2] */
this is not a union. Other tests open-code union, but it's probably a good idea to add BTF_UNION_ENC
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/btf.c +16
-0+ BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_TAG_ENC(NAME_TBD, 2, -1), + BTF_TAG_ENC(NAME_TBD, 2, 0), + BTF_TAG_ENC(NAME_TBD, 2, 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, +},
[...]
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3789
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/btf.c +6
-0+{ + .descr = "tag test #7, invalid vlen", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_VAR_ENC (NAME_TBD ,1 ,0 ),/* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 1), 2), (0),
here you have both kflag and vlen specified, so it's not clear which one is rejected. Please keep kflag at 0 for this one.
Quoted 28 lines
tools/testing/selftests/bpf/prog_tests/btf.c +28
-0+BTF_END_RAW , + }, +BTF_STR_SEC ("\0local\0tag1" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "vlen != 0", +}, +{ + .descr ="tag test #8, invalid kflag" , + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_VAR_ENC (NAME_TBD ,1 ,0 ),/* [2] */ +BTF_TYPE_ENC (NAME_TBD ,BTF_INFO_ENC (BTF_KIND_TAG ,1 ,0 ),2 ), (-1 ), +BTF_END_RAW , + }, +BTF_STR_SEC ("\0local\0tag1" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1,
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3827
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf.c +10
-0+ .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid btf_info kind_flag", +}, +{ + .descr = "tag test #9, var, invalid component_idx", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_VAR_ENC (NAME_TBD ,1 ,0 ),/* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), 2), (0),
nit: could have used BTF_TAG_ENC?
Quoted 47 lines
tools/testing/selftests/bpf/prog_tests/btf.c +47
-0+BTF_END_RAW , + }, +BTF_STR_SEC ("\0local\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +{ + .descr ="tag test #10, struct member, invalid component_idx" , + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_STRUCT_ENC (0 ,2 ,8 ),/* [2] */ +BTF_MEMBER_ENC (NAME_TBD ,1 ,0 ), +BTF_MEMBER_ENC (NAME_TBD ,1 ,32 ), +BTF_TAG_ENC (NAME_TBD ,2 ,2 ), +BTF_END_RAW , + }, +BTF_STR_SEC ("\0m1\0m2\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 8, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +{ + .descr ="tag test #11, func parameter, invalid component_idx" , + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_FUNC_PROTO_ENC (0 ,2 ),/* [2] */ +BTF_FUNC_PROTO_ARG_ENC (NAME_TBD ,1 ), +BTF_FUNC_PROTO_ARG_ENC (NAME_TBD ,1 ), +BTF_FUNC_ENC (NAME_TBD ,2 ),/* [3] */ +BTF_TAG_ENC (NAME_TBD ,3 ,2 ), +BTF_END_RAW , + }, +BTF_STR_SEC ("\0arg1\0arg2\0f\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY,
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3884
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf.c +10
-0+ .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +
please also add invalid negative component_idx test (e.g., -2)
Quoted 27 lines
}; /* struct
btf_raw_testraw_tests[] */static const char *get_next_str(const char *start, const char *end)
tools/testing/selftests/bpf/prog_tests/btf.c +2
-0@@ -6801,6 +7022,8 @@ static int btf_type_size(const struct btf_type *t)return base_size +sizeof (struct btf_var );case BTF_KIND_DATASEC :return base_size + vlen *sizeof (struct btf_var_secinfo ); +case BTF_KIND_TAG : +return base_size +sizeof (struct btf_tag );default :fprintf (stderr,"Unsupported BTF_KIND:%u\n" , kind);return -EINVAL ;tools/testing/selftests/bpf/test_btf.h +3
-1@@ -69,4 +69,7 @@#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_TAG_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx) +#endif /* _TEST_BTF_H */ --2.30.2
Re: [PATCH bpf-next v2 08/11] selftests/bpf: add BTF_KIND_TAG unit tests
Quoted 39 lines · Andrii Nakryiko
Quoted 38 lines · Yonghong Song
Test good and bad variants of
BTF_KIND_TAGencoding.Signed-off-by Yonghong Song <[email protected]>--- tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++ tools/testing/selftests/bpf/test_btf.h | 3 + 2 files changed, 226 insertions(+)tools/testing/selftests/bpf/prog_tests/btf.c +21
-0@@ -3661,6 +3661,227 @@ static struct btf_raw_test raw_tests[] = { .err_str = "Invalid type_size", }, +{ + .descr = "tag test #1, struct/member, well-formed", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_STRUCT_ENC(0, 2, 8), /* [2] */ + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_MEMBER_ENC(NAME_TBD, 1, 32), + BTF_TAG_ENC(NAME_TBD, 2, -1), + BTF_TAG_ENC(NAME_TBD, 2, 0), + BTF_TAG_ENC(NAME_TBD, 2, 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 8, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, +},
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3689
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/btf.c +5
-0+{ + .descr = "tag test #2, union/member, well-formed", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ + BTF_STRUCT_ENC(NAME_TBD, 2, 4), /* [2] */this is not a union. Other tests open-code union, but it's probably a good idea to add
BTF_UNION_ENC
This intended to be a union. So will fix.
Quoted 27 lines
Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/btf.c +16
-0+ BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_MEMBER_ENC(NAME_TBD, 1, 0), + BTF_TAG_ENC(NAME_TBD, 2, -1), + BTF_TAG_ENC(NAME_TBD, 2, 0), + BTF_TAG_ENC(NAME_TBD, 2, 1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, +},[...]
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3789
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/btf.c +6
-0+{ + .descr = "tag test #7, invalid vlen", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_VAR_ENC (NAME_TBD ,1 ,0 ),/* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 1), 2), (0),
Quoted 2 lines
here you have both kflag and vlen specified, so it's not clear which one is rejected. Please keep kflag at 0 for this one.
Ack.
Quoted 33 lines
Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/btf.c +32
-0+ BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0tag1"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "vlen != 0", +}, +{ + .descr = "tag test #8, invalid kflag", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 0), 2), (-1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0local\0tag1"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid btf_info kind_flag", +},
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3827
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/btf.c +6
-0+{ + .descr = "tag test #9, var, invalid component_idx", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_VAR_ENC (NAME_TBD ,1 ,0 ),/* [2] */ + BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), 2), (0),nit: could have used
BTF_TAG_ENC?
Ack.
Quoted 52 lines
Quoted 52 lines
tools/testing/selftests/bpf/prog_tests/btf.c +51
-0+BTF_END_RAW , + }, +BTF_STR_SEC ("\0local\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +{ + .descr ="tag test #10, struct member, invalid component_idx" , + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_STRUCT_ENC (0 ,2 ,8 ),/* [2] */ +BTF_MEMBER_ENC (NAME_TBD ,1 ,0 ), +BTF_MEMBER_ENC (NAME_TBD ,1 ,32 ), +BTF_TAG_ENC (NAME_TBD ,2 ,2 ), +BTF_END_RAW , + }, +BTF_STR_SEC ("\0m1\0m2\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 8, + .key_type_id = 1, + .value_type_id = 2, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +{ + .descr ="tag test #11, func parameter, invalid component_idx" , + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_FUNC_PROTO_ENC (0 ,2 ),/* [2] */ +BTF_FUNC_PROTO_ARG_ENC (NAME_TBD ,1 ), +BTF_FUNC_PROTO_ARG_ENC (NAME_TBD ,1 ), +BTF_FUNC_ENC (NAME_TBD ,2 ),/* [3] */ +BTF_TAG_ENC (NAME_TBD ,3 ,2 ), +BTF_END_RAW , + }, +BTF_STR_SEC ("\0arg1\0arg2\0f\0tag" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1,
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/btf.c static struct btf_raw_test raw_tests[] = { · line 3884
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/btf.c +6
-0+ .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid component_idx", +}, +please also add invalid negative
component_idxtest (e.g., -2)
Ack.
Quoted 28 lines
Quoted 28 lines
}; /* struct btf_raw_test raw_tests[] */static const char *get_next_str(const char *start, const char *end)
tools/testing/selftests/bpf/prog_tests/btf.c +2
-0@@ -6801,6 +7022,8 @@ static int btf_type_size(const struct btf_type *t)return base_size +sizeof (struct btf_var );case BTF_KIND_DATASEC :return base_size + vlen *sizeof (struct btf_var_secinfo ); +case BTF_KIND_TAG : +return base_size +sizeof (struct btf_tag );default :fprintf (stderr,"Unsupported BTF_KIND:%u\n" , kind);return -EINVAL ;tools/testing/selftests/bpf/test_btf.h +3
-1@@ -69,4 +69,7 @@#define BTF_TYPE_FLOAT_ENC (name, sz) \ BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz) +#define BTF_TAG_ENC (value, type, component_idx) \ + BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx) +#endif /* _TEST_BTF_H */ --2.30.2
PATCH v2 9/11 selftests/bpf: test BTF_KIND_TAG for deduplication
In reply to Yonghong SongAdd unit tests for BTF_KIND_TAG deduplication for
- struct and struct member
- variable
- func and func argument
--- tools/testing/selftests/bpf/prog_tests/btf.c | 91 ++++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +74 -17
@@ -6642,27 +6642,33 @@ const struct btf_dedup_test dedup_tests[] = {BTF_MEMBER_ENC (NAME_NTH (4 ),5 ,64 ),/* const int *a; */ BTF_MEMBER_ENC (NAME_NTH (5 ),2 ,128 ),/* int b[16]; */ BTF_MEMBER_ENC (NAME_NTH (6 ),1 ,640 ),/* int c; */ -BTF_MEMBER_ENC (NAME_NTH (8 ),13 ,672 ),/* float d; */ +BTF_MEMBER_ENC (NAME_NTH (8 ),15 ,672 ),/* float d; */ /* ptr -> [3] struct s */ BTF_PTR_ENC (3 ),/* [4] */ /* ptr -> [6] const int */ BTF_PTR_ENC (6 ),/* [5] */ /* const -> [1] int */ BTF_CONST_ENC (1 ),/* [6] */ +/* tag -> [3] struct s */ +BTF_TAG_ENC (NAME_NTH (2 ),3 ,-1 ),/* [7] */ +/* tag -> [3] struct s, member 1 */ +BTF_TAG_ENC (NAME_NTH (2 ),3 ,1 ),/* [8] */ /* full copy of the above */ -BTF_TYPE_INT_ENC (NAME_NTH (1 ),BTF_INT_SIGNED ,0 ,32 ,4 ),/* [7] */ -BTF_TYPE_ARRAY_ENC (7 ,7 ,16 ),/* [8] */ -BTF_STRUCT_ENC (NAME_NTH (2 ),5 ,88 ),/* [9] */ -BTF_MEMBER_ENC (NAME_NTH (3 ),10 ,0 ), -BTF_MEMBER_ENC (NAME_NTH (4 ),11 ,64 ), -BTF_MEMBER_ENC (NAME_NTH (5 ),8 ,128 ), -BTF_MEMBER_ENC (NAME_NTH (6 ),7 ,640 ), -BTF_MEMBER_ENC (NAME_NTH (8 ),13 ,672 ), -BTF_PTR_ENC (9 ),/* [10] */ -BTF_PTR_ENC (12 ),/* [11] */ -BTF_CONST_ENC (7 ),/* [12] */ -BTF_TYPE_FLOAT_ENC (NAME_NTH (7 ),4 ),/* [13] */ +BTF_TYPE_INT_ENC (NAME_NTH (1 ),BTF_INT_SIGNED ,0 ,32 ,4 ),/* [9] */ +BTF_TYPE_ARRAY_ENC (9 ,9 ,16 ),/* [10] */ +BTF_STRUCT_ENC (NAME_NTH (2 ),5 ,88 ),/* [11] */ +BTF_MEMBER_ENC (NAME_NTH (3 ),12 ,0 ), +BTF_MEMBER_ENC (NAME_NTH (4 ),13 ,64 ), +BTF_MEMBER_ENC (NAME_NTH (5 ),10 ,128 ), +BTF_MEMBER_ENC (NAME_NTH (6 ),9 ,640 ), +BTF_MEMBER_ENC (NAME_NTH (8 ),15 ,672 ), +BTF_PTR_ENC (11 ),/* [12] */ +BTF_PTR_ENC (14 ),/* [13] */ +BTF_CONST_ENC (9 ),/* [14] */ +BTF_TYPE_FLOAT_ENC (NAME_NTH (7 ),4 ),/* [15] */ +BTF_TAG_ENC (NAME_NTH (2 ),11 ,-1 ),/* [16] */ +BTF_TAG_ENC (NAME_NTH (2 ),11 ,1 ),/* [17] */ BTF_END_RAW , }, BTF_STR_SEC("\0int\0s\0next\0a\0b\0c\0float\0d"),
@@ -6679,14 +6685,16 @@ const struct btf_dedup_test dedup_tests[] = {BTF_MEMBER_ENC (NAME_NTH (1 ),5 ,64 ),/* const int *a; */ BTF_MEMBER_ENC (NAME_NTH (2 ),2 ,128 ),/* int b[16]; */ BTF_MEMBER_ENC (NAME_NTH (3 ),1 ,640 ),/* int c; */ -BTF_MEMBER_ENC (NAME_NTH (4 ),7 ,672 ),/* float d; */ +BTF_MEMBER_ENC (NAME_NTH (4 ),9 ,672 ),/* float d; */ /* ptr -> [3] struct s */ BTF_PTR_ENC (3 ),/* [4] */ /* ptr -> [6] const int */ BTF_PTR_ENC (6 ),/* [5] */ /* const -> [1] int */ BTF_CONST_ENC (1 ),/* [6] */ -BTF_TYPE_FLOAT_ENC (NAME_NTH (7 ),4 ),/* [7] */ +BTF_TAG_ENC (NAME_NTH (2 ),3 ,-1 ),/* [7] */ +BTF_TAG_ENC (NAME_NTH (2 ),3 ,1 ),/* [8] */ +BTF_TYPE_FLOAT_ENC (NAME_NTH (7 ),4 ),/* [9] */ BTF_END_RAW , }, BTF_STR_SEC("\0a\0b\0c\0d\0int\0float\0next\0s"),
@@ -6811,9 +6819,11 @@ const struct btf_dedup_test dedup_tests[] = {BTF_FUNC_PROTO_ARG_ENC (NAME_TBD ,8 ),BTF_FUNC_ENC (NAME_TBD ,12 ),/* [13] func */ BTF_TYPE_FLOAT_ENC (NAME_TBD ,2 ),/* [14] float */ +BTF_TAG_ENC (NAME_TBD ,13 ,-1 ),/* [15] tag */ +BTF_TAG_ENC (NAME_TBD ,13 ,1 ),/* [16] tag */ BTF_END_RAW , }, - BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N"), + BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"), }, .expect = { .raw_types = {
@@ -6837,9 +6847,11 @@ const struct btf_dedup_test dedup_tests[] = {BTF_FUNC_PROTO_ARG_ENC (NAME_TBD ,8 ),BTF_FUNC_ENC (NAME_TBD ,12 ),/* [13] func */ BTF_TYPE_FLOAT_ENC (NAME_TBD ,2 ),/* [14] float */ +BTF_TAG_ENC (NAME_TBD ,13 ,-1 ),/* [15] tag */ +BTF_TAG_ENC (NAME_TBD ,13 ,1 ),/* [16] tag */ BTF_END_RAW , }, - BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N"), + BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"), }, .opts = { .dont_resolve_fwds = false,
@@ -6988,6 +7000,51 @@ const struct btf_dedup_test dedup_tests[] = { .dedup_table_size = 1 }, }, +{ + .descr = "dedup: func/func_arg/var tags", + .input = { + .raw_types = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + /* static int t */ + BTF_VAR_ENC(NAME_NTH(1), 1, 0), /* [2] */ + /* void f(int a1, int a2) */ + BTF_FUNC_PROTO_ENC(0, 2), /* [3] */ + BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1), + BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1), + BTF_FUNC_ENC(NAME_NTH(4), 2), /* [4] */ + /* tag -> t */ + BTF_TAG_ENC(NAME_NTH(5), 2, -1), /* [5] */ + BTF_TAG_ENC(NAME_NTH(5), 2, -1), /* [6] */ + /* tag -> func */ + BTF_TAG_ENC(NAME_NTH(5), 4, -1), /* [7] */ + BTF_TAG_ENC(NAME_NTH(5), 4, -1), /* [8] */ + /* tag -> func arg a1 */ + BTF_TAG_ENC(NAME_NTH(5), 4, 1), /* [9] */ + BTF_TAG_ENC(NAME_NTH(5), 4, 1), /* [10] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"), + }, + .expect = { + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_VAR_ENC(NAME_NTH(1), 1, 0), /* [2] */ + BTF_FUNC_PROTO_ENC(0, 2), /* [3] */ + BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1), + BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1), + BTF_FUNC_ENC(NAME_NTH(4), 2), /* [4] */ + BTF_TAG_ENC(NAME_NTH(5), 2, -1), /* [5] */ + BTF_TAG_ENC(NAME_NTH(5), 4, -1), /* [6] */ + BTF_TAG_ENC(NAME_NTH(5), 4, 1), /* [7] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"), + }, + .opts = { + .dont_resolve_fwds = false, + }, +}, };
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 09/11] selftests/bpf: test BTF_KIND_TAG for deduplication
Quoted 6 lines · Yonghong Song
Add unit tests for
BTF_KIND_TAGdeduplication for
- struct and struct member
- variable
- func and func argument
Can you please also add tests where you have duplicated struct, variable, and func (three different tests), and each copy has two
tags: one with common value (e.g., common_val) and one with unique value (uniq_val1 and uniq_val2, one for each copy of a struct/var/func). End result should be a single struct/var/func with three different tags pointing to it (e.g., common_val, uniq_val1, uniq_val2). I.e., those tags are "inherited" by the deduplicated entity and only a unique set of them is left.
Quoted 5 lines
Signed-off-by Yonghong Song <[email protected]>--- tools/testing/selftests/bpf/prog_tests/btf.c | 91 ++++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-)
[...]
Re: [PATCH bpf-next v2 09/11] selftests/bpf: test BTF_KIND_TAG for deduplication
Quoted 8 lines · Andrii Nakryiko
Quoted 6 lines · Yonghong Song
Add unit tests for
BTF_KIND_TAGdeduplication for
- struct and struct member
- variable
- func and func argument
Quoted 8 lines
Can you please also add tests where you have duplicated struct, variable, and func (three different tests), and each copy has two
tags: one with common value (e.g.,common_val) and one with unique value (uniq_val1anduniq_val2, one for each copy of a struct/var/func). End result should be a single struct/var/func with three different tags pointing to it (e.g.,common_val,uniq_val1,uniq_val2). I.e., those tags are "inherited" by the deduplicated entity and only a unique set of them is left.
Sure. Will do.
Quoted 9 lines
Quoted 6 lines
Signed-off-by Yonghong Song <[email protected]>--- tools/testing/selftests/bpf/prog_tests/btf.c | 91 ++++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-)[...]
Re: [PATCH bpf-next v2 09/11] selftests/bpf: test BTF_KIND_TAG for deduplication
In reply to Andrii NakryikoQuoted 10 lines · Andrii Nakryiko
Quoted 6 lines · Yonghong Song
Add unit tests for
BTF_KIND_TAGdeduplication for
- struct and struct member
- variable
- func and func argumentCan you please also add tests where you have duplicated struct, variable, and func (three different tests), and each copy has two
currently, variable won't be deduplicated so I will skip variable and add tests for func/argument and struct/member.
Quoted 15 lines
tags: one with common value (e.g.,
common_val) and one with unique value (uniq_val1anduniq_val2, one for each copy of a struct/var/func). End result should be a single struct/var/func with three different tags pointing to it (e.g.,common_val,uniq_val1,uniq_val2). I.e., those tags are "inherited" by the deduplicated entity and only a unique set of them is left.Quoted 5 lines
Signed-off-by Yonghong Song <[email protected]>--- tools/testing/selftests/bpf/prog_tests/btf.c | 91 ++++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-)[...]
Re: [PATCH bpf-next v2 09/11] selftests/bpf: test BTF_KIND_TAG for deduplication
Quoted 12 lines · Yonghong Song
Quoted 8 lines · Andrii Nakryiko
Quoted 6 lines · Yonghong Song
Add unit tests for
BTF_KIND_TAGdeduplication for
- struct and struct member
- variable
- func and func argument
Quoted 5 lines
Quoted 2 lines
Can you please also add tests where you have duplicated struct, variable, and func (three different tests), and each copy has two
currently, variable won't be deduplicated so I will skip variable and add tests for func/argument and struct/member.
oh, right, yeap, it makes sense only for struct/union and func
Quoted 16 lines
Quoted 16 lines
tags: one with common value (e.g.,
common_val) and one with unique value (uniq_val1anduniq_val2, one for each copy of a struct/var/func). End result should be a single struct/var/func with three different tags pointing to it (e.g.,common_val,uniq_val1,uniq_val2). I.e., those tags are "inherited" by the deduplicated entity and only a unique set of them is left.Quoted 5 lines
Signed-off-by Yonghong Song <[email protected]>--- tools/testing/selftests/bpf/prog_tests/btf.c | 91 ++++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-)[...]
PATCH v2 10/11 selftests/bpf: add a test with a bpf program with btf_tag attributes
In reply to Yonghong SongAdd a bpf program with btf_tag attributes. The program is loaded successfully with the kernel. With the command
bpftool btf dump file ./tag.o
the following dump shows that tags are properly encoded:
[8] STRUCT 'key_t' size=12 vlen=3
'a' type_id=2 bits_offset=0
'b' type_id=2 bits_offset=32
'c' type_id=2 bits_offset=64
[9] TAG 'tag1' type_id=8 component_id=-1
[10] TAG 'tag2' type_id=8 component_id=-1
[11] TAG 'tag1' type_id=8 component_id=1
[12] TAG 'tag2' type_id=8 component_id=1
...
[21] FUNC_PROTO '(anon)' ret_type_id=2 vlen=1
'x' type_id=2 [22] FUNC 'foo' type_id=21 linkage=static
[23] TAG 'tag1' type_id=22 component_id=0
[24] TAG 'tag2' type_id=22 component_id=0
[25] TAG 'tag1' type_id=22 component_id=-1
[26] TAG 'tag2' type_id=22 component_id=-1
...
[29] VAR 'total' type_id=27, linkage=global
[30] TAG 'tag1' type_id=29 component_id=-1
[31] TAG 'tag2' type_id=29 component_id=-1
If an old clang compiler, which does not support btf_tag attribute, is used, these btf_tag attributes will be silently ignored.
--- .../selftests/bpf/prog_tests/btf_tag.c | 14 +++++++ tools/testing/selftests/bpf/progs/tag.c | 39 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_tag.c create mode 100644 tools/testing/selftests/bpf/progs/tag.c
tools/testing/selftests/bpf/prog_tests/btf_tag.c new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ +#include <test_progs.h> +#include "tag.skel.h" + +void test_btf_tag (void ) +{ +struct tag *skel; + + skel =tag__open_and_load (); +if (!ASSERT_OK_PTR (skel,"btf_tag" )) +return ; +tag__destroy (skel); +}
tools/testing/selftests/bpf/progs/tag.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +#define __tag1 __attribute__((btf_tag("tag1"))) +#define __tag2 __attribute__((btf_tag("tag2"))) + +struct key_t { +int a; + int b __tag1 __tag2; +int c; +} __tag1 __tag2; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 3); + __type(key, struct key_t); + __type(value, __u64); +} hashmap1 SEC(".maps"); + +__u32 total __tag1 __tag2 = 0; + +static __noinline int foo(int x __tag1 __tag2) __tag1 __tag2 +{ +struct key_t key; +__u64 val =1 ; + + key.a = key.b = key.c = x; +bpf_map_update_elem (&hashmap1, &key, &val,0 ); +return 0 ; +} + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG (sub ,int x) +{ +return foo (x); +}
Signature · 1 line
2.30.2
PATCH v2 11/11 docs/bpf: add documentation for BTF_KIND_TAG
In reply to Yonghong SongAdd BTF_KIND_TAG documentation in btf.rst.
--- Documentation/bpf/btf.rst | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
Documentation/bpf/btf.rst +26 -1
@@ -85,6 +85,7 @@ sequentially and type id is assigned to each recognized type starting from id #define BTF_KIND_VAR 14 /* Variable */ #define BTF_KIND_DATASEC 15 /* Section */ #define BTF_KIND_FLOAT 16 /* Floating point */ + #define BTF_KIND_TAG 17 /* Tag */ Note that the type section encodes debug info, not just pure types. ``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.
@@ -106,7 +107,7 @@ Each type contains the following common data:: * "size" tells the size of the type it is describing. * * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, - * FUNC and FUNC_PROTO. + * FUNC, FUNC_PROTO and TAG. * "type" is a type_id referring to another type. */ union {
@@ -465,6 +466,30 @@ map definition. No additional type data follow ``btf_type``. +2.2.17 BTF_KIND_TAG +~~~~~~~~~~~~~~~~~~~ + +``struct btf_type`` encoding requirement: + * ``name_off``: offset to a non-empty string + * ``info.kind_flag``: 0 + * ``info.kind``: BTF_KIND_TAG + * ``info.vlen``: 0 + * ``type``: ``struct``, ``union``, ``func`` or ``var`` + +``btf_type`` is followed by ``struct btf_tag``.:: + + struct btf_tag { + __u32 component_idx; + }; + +The ``name_off`` encodes btf_tag attribute string. +The ``type`` should be ``struct``, ``union``, ``func`` or ``var``. +If ``btf_tag.component_idx = -1``, the btf_tag attribute is +applied to a valid ``type``. Otherwise, the btf_tag attribute is +applied to a ``struct``/``union`` member or a ``func`` argument, +and ``btf_tag.component_idx`` should be a valid index (starting from 0) +pointing to a member or an argument. + 3. BTF Kernel API *****************
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v2 11/11] docs/bpf: add documentation for BTF_KIND_TAG
Quoted 5 lines · Yonghong Song
Add
BTF_KIND_TAGdocumentation inbtf.rst.Signed-off-by Yonghong Song <[email protected]>---
Probably worth it to mention that component_idx for tag for VAR has to be -1.
Quoted 58 lines
Documentation/bpf/btf.rst | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)Documentation/bpf/btf.rst +26
-2@@ -85,6 +85,7 @@ sequentially and type id is assigned to each recognized type starting from id #define BTF_KIND_VAR 14 /* Variable */ #define BTF_KIND_DATASEC 15 /* Section */ #define BTF_KIND_FLOAT 16 /* Floating point */ + #define BTF_KIND_TAG 17 /* Tag */ Note that the type section encodes debug info, not just pure types. ``BTF_KIND_FUNC`` is not a type, and it represents a defined subprogram.@@ -106,7 +107,7 @@ Each type contains the following common data:: * "size" tells the size of the type it is describing. * * "type" is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT, - * FUNC and FUNC_PROTO. + * FUNC, FUNC_PROTO and TAG. * "type" is a type_id referring to another type. */ union {@@ -465,6 +466,30 @@ map definition. No additional type data follow ``btf_type``. +2.2.17 BTF_KIND_TAG +~~~~~~~~~~~~~~~~~~~ + +``struct btf_type`` encoding requirement: + * ``name_off``: offset to a non-empty string + * ``info.kind_flag``: 0 + * ``info.kind``: BTF_KIND_TAG + * ``info.vlen``: 0 + * ``type``: ``struct``, ``union``, ``func`` or ``var`` + +``btf_type`` is followed by ``struct btf_tag``.:: + + struct btf_tag { + __u32 component_idx; + }; + +The ``name_off`` encodes btf_tag attribute string. +The ``type`` should be ``struct``, ``union``, ``func`` or ``var``. +If ``btf_tag.component_idx = -1``, the btf_tag attribute is +applied to a valid ``type``. Otherwise, the btf_tag attribute is +applied to a ``struct``/``union`` member or a ``func`` argument, +and ``btf_tag.component_idx`` should be a valid index (starting from 0) +pointing to a member or an argument. + 3. BTF Kernel API ***************** --2.30.2
cc Jose E. Marchesi
Hi, Jose, just let you know that the BTF format for BTF_KIND_TAG is changed since v1 as the new format can simplify kernel/libbpf implementation. Thanks!
Quoted 88 lines · Yonghong Song
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.This new attribute can be used to add attributes to kernel codes, e.g., pre- or post- conditions, allow/deny info, or any other info in which only the kernel is interested. Such attributes will be processed by clang frontend and emitted to dwarf, converting to BTF by pahole. Ultimiately the verifier can use these information for verification purpose.
The new attribute can also be used for bpf programs, e.g., tagging with
__userattributes for function parameters, specifying global function preconditions, etc. Such information may help verifier to detect user program bugs.After this series, pahole
dwarf->btfconverter will be enhanced to support new llvm tag forbtf_tagattribute. With pahole support, we will then try to add a few real use case, e.g., __user/__rcu tagging, allow/deny list, some kernel function precondition, etc, in the kernel.In the rest of the series, Patches 1-2 had kernel support. Patches 3-4 added
libbpf support. Patch 5 added bpftool support. Patches 6-10 added various selftests. Patch 11 added documentation for the new kind.[1] https://reviews.llvm.org/D106614 [2] https://reviews.llvm.org/D106621 [3] https://reviews.llvm.org/D106622 [4] https://reviews.llvm.org/D109560Changelog:
v1 -> v2:
- BTF ELF format changed in llvm ([4] above), so cross-board change to use the new format.
- Clarified in commit message thatBTF_KIND_TAGis not emitted by bpftool btf dump format c.
- Fix various comments from Andrii.Yonghong Song (11):
btf: change BTF_KIND_* macros to enums
bpf: support for new btf kindBTF_KIND_TAG
libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag
libbpf: add support forBTF_KIND_TAG
bpftool: add support forBTF_KIND_TAG
selftests/bpf: test libbpf API functionbtf__add_tag()selftests/bpf: change NAME_NTH/IS_NAME_NTH forBTF_KIND_TAGformat selftests/bpf: addBTF_KIND_TAGunit tests
selftests/bpf: testBTF_KIND_TAGfor deduplication selftests/bpf: add a test with a bpf program withbtf_tagattributes docs/bpf: add documentation forBTF_KIND_TAGDocumentation/bpf/btf.rst | 27 +- include/uapi/linux/btf.h | 52 +-- kernel/bpf/btf.c | 120 +++++++ tools/bpf/bpftool/btf.c | 12 + tools/include/uapi/linux/btf.h | 52 +-- tools/lib/bpf/btf.c | 85 ++++- tools/lib/bpf/btf.h | 15 + tools/lib/bpf/btf_dump.c | 3 + tools/lib/bpf/libbpf.c | 31 +- tools/lib/bpf/libbpf.map | 5 + tools/lib/bpf/libbpf_internal.h | 2 + tools/testing/selftests/bpf/btf_helpers.c | 7 +- tools/testing/selftests/bpf/prog_tests/btf.c | 318 ++++++++++++++++-- .../selftests/bpf/prog_tests/btf_tag.c | 14 + .../selftests/bpf/prog_tests/btf_write.c | 21 ++ tools/testing/selftests/bpf/progs/tag.c | 39 +++ tools/testing/selftests/bpf/test_btf.h | 3 + 17 files changed, 736 insertions(+), 70 deletions(-)create mode 100644
tools/testing/selftests/bpf/prog_tests/btf_tag.ccreate mode 100644tools/testing/selftests/bpf/progs/tag.c
Quoted 6 lines
cc Jose E. Marchesi
Hi, Jose, just let you know that the BTF format for
BTF_KIND_TAGis changed since v1 as the new format can simplify kernel/libbpf implementation. Thanks!
Noted. Thanks for the update.
Quoted 82 lines · Yonghong Song
Quoted 80 lines
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))
This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this
attribute will be emitted to BTF directly ([3], [4]).
The attribute is intended to provide additional
information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.
This new attribute can be used to add attributes
to kernel codes, e.g., pre- or post- conditions,
allow/deny info, or any other info in which only
the kernel is interested. Such attributes will
be processed by clang frontend and emitted to
dwarf, converting to BTF by pahole. Ultimiately
the verifier can use these information for
verification purpose.
The new attribute can also be used for bpf
programs, e.g., tagging with__userattributes
for function parameters, specifying global
function preconditions, etc. Such information
may help verifier to detect user program
bugs.
After this series, paholedwarf->btfconverter
will be enhanced to support new llvm tag
forbtf_tagattribute. With pahole support,
we will then try to add a few real use case,
e.g., __user/__rcu tagging, allow/deny list,
some kernel function precondition, etc,
in the kernel.
In the rest of the series, Patches 1-2 had
kernel support. Patches 3-4 added
libbpf support. Patch 5 added bpftool
support. Patches 6-10 added various selftests.
Patch 11 added documentation for the new kind.
[1] https://reviews.llvm.org/D106614
[2] https://reviews.llvm.org/D106621
[3] https://reviews.llvm.org/D106622
[4] https://reviews.llvm.org/D109560
Changelog:
v1 -> v2:
- BTF ELF format changed in llvm ([4] above),
so cross-board change to use the new format.
- Clarified in commit message thatBTF_KIND_TAG
is not emitted by bpftool btf dump format c.
- Fix various comments from Andrii.
Yonghong Song (11):
btf: change BTF_KIND_* macros to enums
bpf: support for new btf kindBTF_KIND_TAG
libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag
libbpf: add support forBTF_KIND_TAG
bpftool: add support forBTF_KIND_TAG
selftests/bpf: test libbpf API functionbtf__add_tag()selftests/bpf: change NAME_NTH/IS_NAME_NTH forBTF_KIND_TAGformat selftests/bpf: addBTF_KIND_TAGunit tests
selftests/bpf: testBTF_KIND_TAGfor deduplication selftests/bpf: add a test with a bpf program withbtf_tagattributes docs/bpf: add documentation forBTF_KIND_TAGDocumentation/bpf/btf.rst | 27 +- include/uapi/linux/btf.h | 52 +-- kernel/bpf/btf.c | 120 +++++++ tools/bpf/bpftool/btf.c | 12 + tools/include/uapi/linux/btf.h | 52 +-- tools/lib/bpf/btf.c | 85 ++++- tools/lib/bpf/btf.h | 15 + tools/lib/bpf/btf_dump.c | 3 + tools/lib/bpf/libbpf.c | 31 +- tools/lib/bpf/libbpf.map | 5 + tools/lib/bpf/libbpf_internal.h | 2 + tools/testing/selftests/bpf/btf_helpers.c | 7 +- tools/testing/selftests/bpf/prog_tests/btf.c | 318 ++++++++++++++++-- .../selftests/bpf/prog_tests/btf_tag.c | 14 + .../selftests/bpf/prog_tests/btf_write.c | 21 ++ tools/testing/selftests/bpf/progs/tag.c | 39 +++ tools/testing/selftests/bpf/test_btf.h | 3 + 17 files changed, 736 insertions(+), 70 deletions(-)create mode 100644
tools/testing/selftests/bpf/prog_tests/btf_tag.ccreate mode 100644tools/testing/selftests/bpf/progs/tag.c
Quoted 8 lines · Jose E. Marchesi
Quoted 6 lines
cc Jose E. Marchesi
Hi, Jose, just let you know that the BTF format for
BTF_KIND_TAGis changed since v1 as the new format can simplify kernel/libbpf implementation. Thanks!Noted. Thanks for the update.
Hi, Jose,
This is just another update on btf_tag development. Now, btf_tag is divided into btf_decl_tag and btf_type_tag for tagging declarations and types as clang compiler prefers not to mix them with each other. All compiler works in llvm has done and you can check upstream llvm-project "main" branch for implementation.
The patch set below (under review)
https://lore.kernel.org/bpf/[email protected]/
actually tried to use btf_type_tag for linux kernel __user annotation so bpf verifier can use it.
Another question from Omar (open source drgn maintainer)
https://developers.facebook.com/blog/post/2021/12/09/drgn-how-linux-kernel-team-meta-debugs-kernel-scale/
mentioned that btf_tag information will also help drgn since it can then especially distinguish between __percpu pointer from other pointers. Currently drgn is using dwarf, clang compiled kernel puts btf_tag information in dwarf. Based on our earlier discussion, gcc intends to generate btf tags for BTF only. Maybe we could discuss to also generate for dwarf? Do we need a flag?
Please let me know if you have any questions.
Happy to help in whatever way to get gcc also implementing btf tag support.
Thanks!
Yonghong
Quoted 83 lines
Quoted 83 lines · Yonghong Song
Quoted 80 lines
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.
This new attribute can be used to add attributes to kernel codes, e.g., pre- or post- conditions, allow/deny info, or any other info in which only the kernel is interested. Such attributes will be processed by clang frontend and emitted to dwarf, converting to BTF by pahole. Ultimiately the verifier can use these information for verification purpose.
The new attribute can also be used for bpf programs, e.g., tagging with__userattributes for function parameters, specifying global function preconditions, etc. Such information may help verifier to detect user program
bugs.
After this series, paholedwarf->btfconverter will be enhanced to support new llvm tag
forbtf_tagattribute. With pahole support,
we will then try to add a few real use case, e.g., __user/__rcu tagging, allow/deny list, some kernel function precondition, etc,
in the kernel.
In the rest of the series, Patches 1-2 had kernel support. Patches 3-4 added
libbpf support. Patch 5 added bpftool
support. Patches 6-10 added various selftests. Patch 11 added documentation for the new kind.[1] https://reviews.llvm.org/D106614 [2] https://reviews.llvm.org/D106621 [3] https://reviews.llvm.org/D106622 [4] https://reviews.llvm.org/D109560Changelog:
v1 -> v2:
- BTF ELF format changed in llvm ([4] above),
so cross-board change to use the new format.
- Clarified in commit message thatBTF_KIND_TAG
is not emitted by bpftool btf dump format c.
- Fix various comments from Andrii.
Yonghong Song (11):
btf: change BTF_KIND_* macros to enums
bpf: support for new btf kindBTF_KIND_TAG
libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag
libbpf: add support forBTF_KIND_TAG
bpftool: add support forBTF_KIND_TAG
selftests/bpf: test libbpf API functionbtf__add_tag()selftests/bpf: change NAME_NTH/IS_NAME_NTH forBTF_KIND_TAGformat selftests/bpf: addBTF_KIND_TAGunit tests selftests/bpf: testBTF_KIND_TAGfor deduplication selftests/bpf: add a test with a bpf program withbtf_tagattributes docs/bpf: add documentation forBTF_KIND_TAGDocumentation/bpf/btf.rst | 27 +- include/uapi/linux/btf.h | 52 +-- kernel/bpf/btf.c | 120 +++++++ tools/bpf/bpftool/btf.c | 12 + tools/include/uapi/linux/btf.h | 52 +-- tools/lib/bpf/btf.c | 85 ++++- tools/lib/bpf/btf.h | 15 + tools/lib/bpf/btf_dump.c | 3 + tools/lib/bpf/libbpf.c | 31 +- tools/lib/bpf/libbpf.map | 5 + tools/lib/bpf/libbpf_internal.h | 2 + tools/testing/selftests/bpf/btf_helpers.c | 7 +- tools/testing/selftests/bpf/prog_tests/btf.c | 318 ++++++++++++++++-- .../selftests/bpf/prog_tests/btf_tag.c | 14 + .../selftests/bpf/prog_tests/btf_write.c | 21 ++ tools/testing/selftests/bpf/progs/tag.c | 39 +++ tools/testing/selftests/bpf/test_btf.h | 3 + 17 files changed, 736 insertions(+), 70 deletions(-)create mode 100644
tools/testing/selftests/bpf/prog_tests/btf_tag.ccreate mode 100644tools/testing/selftests/bpf/progs/tag.c
Hi Yonghong.
Quoted 18 lines · Jose E. Marchesi
Quoted 7 lines
Quoted 6 lines
cc Jose E. Marchesi
Hi, Jose, just let you know that the BTF format for
BTF_KIND_TAGis changed since v1 as the new format can simplify kernel/libbpf implementation. Thanks!Noted. Thanks for the update.
Hi, Jose,
This is just another update on
btf_tagdevelopment. Now,btf_tagis divided intobtf_decl_tagandbtf_type_tagfor tagging declarations and types as clang compiler prefers not to mix them with each other. All compiler works in llvm has done and you can check upstream llvm-project "main" branch for implementation.
Quoted 4 lines
The patch set below (under review)
https://lore.kernel.org/bpf/[email protected]/
actually tried to usebtf_type_tagfor linux kernel__userannotation so bpf verifier can use it.
Noted, thanks for the heads up :)
We have not yet started to implement this.
Quoted 9 lines
Another question from Omar (open source drgn maintainer)
https://developers.facebook.com/blog/post/2021/12/09/drgn-how-linux-kernel-team-meta-debugs-kernel-scale/
mentioned thatbtf_taginformation will also help drgn since it can then especially distinguish between__percpupointer from other pointers. Currently drgn is using dwarf, clang compiled kernel putsbtf_taginformation in dwarf. Based on our earlier discussion, gcc intends to generate btf tags for BTF only. Maybe we could discuss to also generate for dwarf? Do we need a flag?
It seems to me that there are three different orthogonal issues/topics here, even if somehow related. Each would require a separated discussion, probably on different contexts:
[Please let me know if I am wrong on any detail in the summary below.
In part I am writing it down as a recap to help myself :)]
1) The need for BTF to convey free-text tags on certain elements, such as members of struct types.
IMO there is not much to discuss about this one. The specification is straightforward as is the implementation. We will be adding it to GCC soon.
Note that:
- This is obviously BTF specific.
- This is not strictly BPF specific, as GCC can generate BTF for any supported target and not just BPF. I think you have patches for LLVM to the same effect.
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
Therefore I would avoid any reference to BTF in the proposal to keep it general, like in the name of DIE types and so on. This would
Whom to involve to discuss this? Well, I would say that at a minimum we need to involve GCC people, LLVM people and DWARF people, and the consensum of all the three parties. Once agreed, we can all implement the same DIEs counting on the next version of the DWARF spec will catch with them. I would really avoid rushed solutions based on compiler-specific extensions.
Where to discuss this? I don't know. In some DWARF forum? Or cross-posting gcc-patches and whatever LLVM list uses for development? I am CCing Mark Wielaard who is a DWARF wizard... any suggestion?
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Whom to involve to discuss this? Definitely, the front-end chaps of both GCC and LLVM will have something to say about this, particularly the ones in charge of the C-like language front-ends like C and C++. A consensum among them would be ideal and would avoid compiler-specific hacks.
Where to discuss this? Again, it seems that we need some neutral ground to discuss inter-operability issues between the different free software compilers...
In any case we are more than willing to help and participate in the discussions :)
Quoted 90 lines
Please let me know if you have any questions. Happy to help in whatever way to get gcc also implementing btf tag support.
Thanks!
Yonghong
Quoted 83 lines
Quoted 83 lines · Yonghong Song
Quoted 80 lines
LLVM14 added support for a new C attribute ([1])
__attribute__((btf_tag("arbitrary_str")))This attribute will be emitted to dwarf ([2]) and pahole will convert it to BTF. Or for bpf target, this attribute will be emitted to BTF directly ([3], [4]). The attribute is intended to provide additional information for
- struct/union type or struct/union member
- static/global variables
- static/global function or function parameter.
This new attribute can be used to add attributes to kernel codes, e.g., pre- or post- conditions, allow/deny info, or any other info in which only the kernel is interested. Such attributes will be processed by clang frontend and emitted to dwarf, converting to BTF by pahole. Ultimiately the verifier can use these information for verification purpose.
The new attribute can also be used for bpf programs, e.g., tagging with__userattributes for function parameters, specifying global function preconditions, etc. Such information may help verifier to detect user program
bugs.
After this series, paholedwarf->btfconverter will be enhanced to support new llvm tag
forbtf_tagattribute. With pahole support,
we will then try to add a few real use case, e.g., __user/__rcu tagging, allow/deny list, some kernel function precondition, etc,
in the kernel.
In the rest of the series, Patches 1-2 had kernel support. Patches 3-4 added
libbpf support. Patch 5 added bpftool
support. Patches 6-10 added various selftests. Patch 11 added documentation for the new kind.[1] https://reviews.llvm.org/D106614 [2] https://reviews.llvm.org/D106621 [3] https://reviews.llvm.org/D106622 [4] https://reviews.llvm.org/D109560Changelog:
v1 -> v2:
- BTF ELF format changed in llvm ([4] above),
so cross-board change to use the new format.
- Clarified in commit message thatBTF_KIND_TAG
is not emitted by bpftool btf dump format c.
- Fix various comments from Andrii.
Yonghong Song (11):
btf: change BTF_KIND_* macros to enums
bpf: support for new btf kindBTF_KIND_TAG
libbpf: rename btf_{hash,equal}_int to btf_{hash,equal}_int_tag
libbpf: add support forBTF_KIND_TAG
bpftool: add support forBTF_KIND_TAG
selftests/bpf: test libbpf API functionbtf__add_tag()selftests/bpf: change NAME_NTH/IS_NAME_NTH forBTF_KIND_TAGformat selftests/bpf: addBTF_KIND_TAGunit tests selftests/bpf: testBTF_KIND_TAGfor deduplication selftests/bpf: add a test with a bpf program withbtf_tagattributes docs/bpf: add documentation forBTF_KIND_TAGDocumentation/bpf/btf.rst | 27 +- include/uapi/linux/btf.h | 52 +-- kernel/bpf/btf.c | 120 +++++++ tools/bpf/bpftool/btf.c | 12 + tools/include/uapi/linux/btf.h | 52 +-- tools/lib/bpf/btf.c | 85 ++++- tools/lib/bpf/btf.h | 15 + tools/lib/bpf/btf_dump.c | 3 + tools/lib/bpf/libbpf.c | 31 +- tools/lib/bpf/libbpf.map | 5 + tools/lib/bpf/libbpf_internal.h | 2 + tools/testing/selftests/bpf/btf_helpers.c | 7 +- tools/testing/selftests/bpf/prog_tests/btf.c | 318 ++++++++++++++++-- .../selftests/bpf/prog_tests/btf_tag.c | 14 + .../selftests/bpf/prog_tests/btf_write.c | 21 ++ tools/testing/selftests/bpf/progs/tag.c | 39 +++ tools/testing/selftests/bpf/test_btf.h | 3 + 17 files changed, 736 insertions(+), 70 deletions(-)create mode 100644
tools/testing/selftests/bpf/prog_tests/btf_tag.ccreate mode 100644tools/testing/selftests/bpf/progs/tag.c
Quoted 13 lines · Jose E. Marchesi
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpu annotation is kernel specific. __user and __rcu are kernel specific too. Only BPF and BTF can meaningfully consume all three. drgn can consume __percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.
Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.
Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing with btf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. The include/linux/compiler.h can accommodate it.
Quoted 60 lines · Alexei Starovoitov
Quoted 13 lines · Jose E. Marchesi
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too. Only BPF and BTF can meaningfully consume all three. drgn can consume__percpu.In that sense if GCC follows LLVM and emits compiler specific DWARF tag pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.
Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.
Just want to add a little bit context for this. In the beginning when we
proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested 'btf_tag' since the use case we proposed is for bpf. At that point, we don't know
drgn use cases yet. Even with that, the use cases are still just for
linux kernel.
At that time, some *similar* use cases did came up, e.g., for
swift<->C++ conversion encoding ("tag name", "attribute info") for
attributes in the source code, will help a lot. But they will use a different "tag name" than btf_tag to differentiate.
Quoted 77 lines · Alexei Starovoitov
Quoted 56 lines · Jose E. Marchesi
Quoted 13 lines
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too.
Only BPF and BTF can meaningfully consume all three.
drgn can consume__percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag
pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.Just want to add a little bit context for this. In the beginning when we proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested '
btf_tag' since the use case we proposed is for bpf. At that point, we don't know drgn use cases yet. Even with that, the use cases are still just for linux kernel.
Quoted 4 lines
At that time, some *similar* use cases did came up, e.g., for swift<->C++ conversion encoding ("tag name", "attribute info") for attributes in the source code, will help a lot. But they will use a different "tag name" than
btf_tagto differentiate.
Thanks for the info.
I find it very interesting that the LLVM people prefers to have several "use case specific" tag names instead of something more generic, which is the exact opposite of what I would have done :) They may have appealing reasons for doing so. Do you have a pointer to the dicussion you had upstream at hand?
Anyway, I will taste the waters with the other GCC hackers about both DIEs and attribute and see what we can come out with. Thanks again for reaching out Yonghong.
Quoted 82 lines · Jose E. Marchesi
Quoted 77 lines · Alexei Starovoitov
Quoted 56 lines · Jose E. Marchesi
Quoted 13 lines
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too.
Only BPF and BTF can meaningfully consume all three.
drgn can consume__percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag
pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.Just want to add a little bit context for this. In the beginning when we proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested '
btf_tag' since the use case we proposed is for bpf. At that point, we don't know drgn use cases yet. Even with that, the use cases are still just for linux kernel.Quoted 4 lines
At that time, some *similar* use cases did came up, e.g., for swift<->C++ conversion encoding ("tag name", "attribute info") for attributes in the source code, will help a lot. But they will use a different "tag name" than
btf_tagto differentiate.
Quoted 7 lines
Thanks for the info.
I find it very interesting that the LLVM people prefers to have several "use case specific" tag names instead of something more generic, which is the exact opposite of what I would have done :) They may have appealing reasons for doing so. Do you have a pointer to the dicussion you had upstream at hand?
Jose, the llvm-dev discussion link is below:
https://lists.llvm.org/pipermail/llvm-dev/2021-June/151009.html
Quoted 4 lines
Anyway, I will taste the waters with the other GCC hackers about both DIEs and attribute and see what we can come out with. Thanks again for reaching out Yonghong.
Thanks!
Quoted 90 lines · Jose E. Marchesi
Quoted 77 lines · Alexei Starovoitov
Quoted 56 lines · Jose E. Marchesi
Quoted 13 lines
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too.
Only BPF and BTF can meaningfully consume all three.
drgn can consume__percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag
pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.Just want to add a little bit context for this. In the beginning when we proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested '
btf_tag' since the use case we proposed is for bpf. At that point, we don't know drgn use cases yet. Even with that, the use cases are still just for linux kernel.Quoted 4 lines
At that time, some *similar* use cases did came up, e.g., for swift<->C++ conversion encoding ("tag name", "attribute info") for attributes in the source code, will help a lot. But they will use a different "tag name" than
btf_tagto differentiate.Thanks for the info.
I find it very interesting that the LLVM people prefers to have several "use case specific" tag names instead of something more generic, which is the exact opposite of what I would have done :) They may have appealing reasons for doing so. Do you have a pointer to the dicussion you had upstream at hand?
Quoted 3 lines
Anyway, I will taste the waters with the other GCC hackers about both DIEs and attribute and see what we can come out with. Thanks again for reaching out Yonghong.
Hi, Jose,
Any progress on gcc btf_tag support discussion? If possible, could you add me to the discussion mailing list so I may help to move the project forward? Thanks a lot!
Yonghong
Quoted 95 lines · Jose E. Marchesi
Quoted 81 lines
Quoted 77 lines · Alexei Starovoitov
Quoted 56 lines · Jose E. Marchesi
Quoted 13 lines
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too.
Only BPF and BTF can meaningfully consume all three.
drgn can consume__percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag
pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.Just want to add a little bit context for this. In the beginning when we proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested '
btf_tag' since the use case we proposed is for bpf. At that point, we don't know drgn use cases yet. Even with that, the use cases are still just for linux kernel.Quoted 4 lines
At that time, some *similar* use cases did came up, e.g., for swift<->C++ conversion encoding ("tag name", "attribute info") for attributes in the source code, will help a lot. But they will use a different "tag name" than
btf_tagto differentiate.Quoted 11 lines
Thanks for the info.
I find it very interesting that the LLVM people prefers to have several
"use case specific" tag names instead of something more generic, which is the exact opposite of what I would have done :) They may have appealing reasons for doing so. Do you have a pointer to the dicussion you had upstream at hand?
Anyway, I will taste the waters with the other GCC hackers about both
DIEs and attribute and see what we can come out with. Thanks again for reaching out Yonghong.
Quoted 5 lines
Hi, Jose,
Any progress on gcc
btf_tagsupport discussion? If possible, could you add me to the discussion mailing list so I may help to move the project forward? Thanks a lot!
We are in the process of implementing the support of the BTF extensions (which is done) and the C language attributes (which is WIP.)
I haven't started the discussion about DWARF yet. Will do shortly. You will be in CC :)
Quoted 95 lines · Jose E. Marchesi
Quoted 95 lines · Jose E. Marchesi
Quoted 92 lines
Quoted 77 lines · Alexei Starovoitov
Quoted 56 lines · Jose E. Marchesi
Quoted 13 lines
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too.
Only BPF and BTF can meaningfully consume all three.
drgn can consume__percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag
pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.Just want to add a little bit context for this. In the beginning when we proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested '
btf_tag' since the use case we proposed is for bpf. At that point, we don't know drgn use cases yet. Even with that, the use cases are still just for linux kernel.Quoted 4 lines
At that time, some *similar* use cases did came up, e.g., for swift<->C++ conversion encoding ("tag name", "attribute info") for attributes in the source code, will help a lot. But they will use a different "tag name" than
btf_tagto differentiate.Thanks for the info.
I find it very interesting that the LLVM people prefers to have several
"use case specific" tag names instead of something more generic, which is the exact opposite of what I would have done :) They may have appealing reasons for doing so. Do you have a pointer to the dicussion you had upstream at hand?
Anyway, I will taste the waters with the other GCC hackers about both
DIEs and attribute and see what we can come out with. Thanks again for reaching out Yonghong.
Quoted 8 lines
Quoted 5 lines
Hi, Jose,
Any progress on gcc
btf_tagsupport discussion? If possible, could you add me to the discussion mailing list so I may help to move the project forward? Thanks a lot!We are in the process of implementing the support of the BTF extensions (which is done) and the C language attributes (which is WIP.)
Sounds good. I am happy to answer questions if you have any.
Quoted 3 lines
I haven't started the discussion about DWARF yet. Will do shortly. You will be in CC :)
Thanks a lot, Jose! I am looking forward to the discussion.
Quoted 106 lines · Jose E. Marchesi
Quoted 95 lines
Quoted 95 lines · Jose E. Marchesi
Quoted 92 lines
Quoted 77 lines · Alexei Starovoitov
Quoted 56 lines · Jose E. Marchesi
Quoted 13 lines
2) The need for DWARF to convey free-text tags on certain elements, such as members of struct types.
The motivation for this was originally the way the Linux kernel generates its BTF information, using pahole, using DWARF as a source. As we discussed in our last exchange on this topic, this is accidental, i.e. if the kernel switched to generate BTF directly from the compiler and the linker could merge/deduplicate BTF, there would be no need for using DWARF to act as the "unwilling conveyer" of this information. There are additional benefits of this second approach. Thats why we didn't plan to add these extended DWARF DIEs to GCC.
Quoted 3 lines
However, it now seems that a DWARF consumer, the drgn project, would also benefit from having such a support in DWARF to distinguish between different kind of pointers.
drgn can use .percpu section in vmlinux for global percpu vars. For pointers the annotation is indeed necessary.
Quoted 6 lines
So it seems to me that now we have two use-cases for adding support for these free-text tags to DWARF, as a proper extension to the format, strictly unrelated to BTF, BPF or even the kernel, since:
- This is not kernel specific.
- This is not directly related to BTF.
- This is not directly related to BPF.
__percpuannotation is kernel specific.__userand__rcuare kernel specific too.
Only BPF and BTF can meaningfully consume all three.
drgn can consume__percpu.
In that sense if GCC follows LLVM and emits compiler specific DWARF tag
pahole can convert it to the same BTF regardless whether kernel was compiled with clang or gcc.
drgn can consume dwarf generated by clang or gcc as well even when BTF is not there. That is the fastest way forward.
In that sense it would be nice to have common DWARF tag for pointer annotations, but it's not mandatory. The time is the most valuable asset. Implementing GCC specific DWARF tag doesn't require committee voting and the mailing list bikeshedding.Quoted 11 lines
3) Addition of C-family language-level constructions to specify free-text tags on certain language elements, such as struct fields.
These are the attributes, or built-ins or whatever syntax.
Note that, strictly speaking:
- This is orthogonal to both DWARF and BTF, and any other supported debugging format, which may or may not be expressive enough to convey the free-form text tag.
- This is not specific to BPF.Quoted 4 lines
Therefore I would avoid any reference to BTF or BPF in the attribute names. Something like `__attribute__((btf_tag("arbitrary_str")))' makes very little sense to me; the attribute name ought to be more generic.
Quoted 11 lines
Let's agree to disagree.
When BPF ISA was designed we didn't go to Intel, Arm, Mips, etc in order to come up with the best ISA that would JIT to those architectures the best possible way. Same thing withbtf_tag. Today it is specific to BTF and BPF only. Hence it's called this way. Whenever actual users will appear that need free-text tags on a struct field then and only then will be the time to discuss generic tag name. Just because "free-text tag on a struct field" sounds generic it doesn't mean that it has any use case beyond what we're using it for in BPF land. It goes back to the point of coding now instead of talking about coding. If gcc wants to call it __attribute__((my_precious_gcc_tag("arbitrary_str"))) go ahead and code it this way. Theinclude/linux/compiler.hcan accommodate it.Just want to add a little bit context for this. In the beginning when we proposed to add the attribute, we named as a generic name like 'tag' (or something like that). But eventually upstream suggested '
btf_tag' since the use case we proposed is for bpf. At that point, we don't know drgn use cases yet. Even with that, the use cases are still just for linux kernel.Quoted 4 lines
At that time, some *similar* use cases did came up, e.g., for swift<->C++ conversion encoding ("tag name", "attribute info") for attributes in the source code, will help a lot. But they will use a different "tag name" than
btf_tagto differentiate.Thanks for the info.
I find it very interesting that the LLVM people prefers to have several
"use case specific" tag names instead of something more generic, which is the exact opposite of what I would have done :) They may have appealing reasons for doing so. Do you have a pointer to the dicussion you had upstream at hand?
Anyway, I will taste the waters with the other GCC hackers about both
DIEs and attribute and see what we can come out with. Thanks again for reaching out Yonghong.Quoted 8 lines
Quoted 5 lines
Hi, Jose,
Any progress on gcc
btf_tagsupport discussion? If possible, could you add me to the discussion mailing list so I may help to move the project forward? Thanks a lot!We are in the process of implementing the support of the BTF extensions
(which is done) and the C language attributes (which is WIP.)
Quoted 7 lines
Sounds good. I am happy to answer questions if you have any.
Quoted 3 lines
I haven't started the discussion about DWARF yet. Will do shortly. You
will be in CC :)Thanks a lot, Jose! I am looking forward to the discussion.
Just a heads-up.
We are still working on the GCC implementation of the tags. Having some difficulties with the ordering of the C type attributes.
Regarding the DWARF part, GCC uses DWARF as the internal "canonical" debug info, and the BTF is generated from it. This means we had to add a DWARF DIE for the pointer tag qualifier anyway in order to convey the info to BTF. So now it is just a matter of emitting it along with the rest of the DWARF.
Quoted 11 lines · Jose E. Marchesi
Just a heads-up.
We are still working on the GCC implementation of the tags. Having some difficulties with the ordering of the C type attributes.
Regarding the DWARF part, GCC uses DWARF as the internal "canonical" debug info, and the BTF is generated from it. This means we had to add a DWARF DIE for the pointer tag qualifier anyway in order to convey the info to BTF. So now it is just a matter of emitting it along with the rest of the DWARF.
Thanks for the update!
Do you have an early git branch we can use to test building the kernel with it?
Or is it not at this level yet?
Quoted 15 lines
Quoted 11 lines · Jose E. Marchesi
Just a heads-up.
We are still working on the GCC implementation of the tags. Having some difficulties with the ordering of the C type attributes.
Regarding the DWARF part, GCC uses DWARF as the internal "canonical" debug info, and the BTF is generated from it. This means we had to add a DWARF DIE for the pointer tag qualifier anyway in order to convey the info to BTF. So now it is just a matter of emitting it along with the rest of the DWARF.
Quoted 4 lines
Thanks for the update!
Do you have an early git branch we can use to test building the kernel with it?
Or is it not at this level yet?
Not yet.
Once we have something working internally we will submit the patches to gcc-patches for discussion. At that point we can put them in a branch for early testing.