bpf: add support for BTF_KIND_DECL_TAG typedef
Subsystems (4)
cover bpf: add support for BTF_KIND_DECL_TAG typedef
Latest upstream llvm-project added support for btf_decl_tag attributes for typedef declarations ([1], [2]). Similar to other btf_decl_tag cases, func/func_param/global_var/struct/union/field, btf_decl_tag with typedef declaration can carry information from kernel source to clang compiler and then to dwarf/BTF, for bpf verification or other use cases.
This patch set added kernel support for BTF_KIND_DECL_TAG to typedef declaration (Patch 1). Additional selftests are added to cover unit testing, dedup, or bpf program usage of btf_decl_tag with typedef. (Patches 2, 3 and 4). The btf documentation is updated to include BTF_KIND_DECL_TAG typedef (Patch 5).
[1] https://reviews.llvm.org/D110127
[2] https://reviews.llvm.org/D112259
Yonghong Song (5):
bpf: add BTF_KIND_DECL_TAG typedef support
selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support
Documentation/bpf/btf.rst | 6 +- kernel/bpf/btf.c | 4 +- tools/testing/selftests/bpf/prog_tests/btf.c | 83 ++++++++++++++++++-- tools/testing/selftests/bpf/progs/tag.c | 9 ++- 4 files changed, 89 insertions(+), 13 deletions(-)
Signature · 1 line
2.30.2
PATCH 1/5 bpf: add BTF_KIND_DECL_TAG typedef support
The llvm patches ([1], [2]) added support to attach btf_decl_tag attributes to typedef declarations. This patch added support in kernel.
[1] https://reviews.llvm.org/D110127
[2] https://reviews.llvm.org/D112259
--- kernel/bpf/btf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/btf.c +2 -2
@@ -468,7 +468,7 @@ static bool btf_type_is_decl_tag(const struct btf_type *t)static bool btf_type_is_decl_tag_target (const struct btf_type *t) {return btf_type_is_func (t) ||btf_type_is_struct (t) || -btf_type_is_var (t); +btf_type_is_var (t) ||btf_type_is_typedef (t); }u32 btf_nr_types (const struct btf *btf)
@@ -3885,7 +3885,7 @@ static int btf_decl_tag_resolve(struct btf_verifier_env *env, component_idx =btf_type_decl_tag (t)->component_idx;if (component_idx !=-1 ) { -if (btf_type_is_var (next_type)) { +if (btf_type_is_var (next_type) ||btf_type_is_typedef (next_type)) {btf_verifier_log_type (env, v->t,"Invalid component_idx" );return -EINVAL ; }
Signature · 1 line
2.30.2
PATCH 2/5 selftests/bpf: add BTF_KIND_DECL_TAG typedef unit tests
In reply to Yonghong SongTest good and bad variants of typedef BTF_KIND_DECL_TAG encoding.
--- tools/testing/selftests/bpf/prog_tests/btf.c | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+)
tools/testing/selftests/bpf/prog_tests/btf.c +36 -0
@@ -3903,6 +3903,42 @@ static struct btf_raw_test raw_tests[] = { .btf_load_err = true, .err_str = "Invalid component_idx", }, +{ + .descr = "decl_tag test #13, typedef, well-formed", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [2] */ + BTF_DECL_TAG_ENC(NAME_TBD, 2, -1), + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\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, +}, +{ + .descr = "decl_tag test #14, typedef, invalid component_idx", + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [2] */ + BTF_DECL_TAG_ENC(NAME_TBD, 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", +}, }; /* struct btf_raw_test raw_tests[] */
Signature · 1 line
2.30.2
PATCH 3/5 selftests/bpf: test deduplication for BTF_KIND_DECL_TAG typedef
In reply to Yonghong SongAdd unit tests for deduplication of BTF_KIND_DECL_TAG to typedef types. Also changed a few comments from "tag" to "decl_tag" to match BTF_KIND_DECL_TAG enum value name.
--- tools/testing/selftests/bpf/prog_tests/btf.c | 47 +++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf.c +41 -6
@@ -6877,11 +6877,12 @@ 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_DECL_TAG_ENC (NAME_TBD ,13 ,-1 ),/* [15] tag */ -BTF_DECL_TAG_ENC (NAME_TBD ,13 ,1 ),/* [16] tag */ +BTF_DECL_TAG_ENC (NAME_TBD ,13 ,-1 ),/* [15] decl_tag */ +BTF_DECL_TAG_ENC (NAME_TBD ,13 ,1 ),/* [16] decl_tag */ +BTF_DECL_TAG_ENC (NAME_TBD ,7 ,-1 ),/* [17] decl_tag */ BTF_END_RAW , }, - BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"), + BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"), }, .expect = { .raw_types = {
@@ -6905,11 +6906,12 @@ 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_DECL_TAG_ENC (NAME_TBD ,13 ,-1 ),/* [15] tag */ -BTF_DECL_TAG_ENC (NAME_TBD ,13 ,1 ),/* [16] tag */ +BTF_DECL_TAG_ENC (NAME_TBD ,13 ,-1 ),/* [15] decl_tag */ +BTF_DECL_TAG_ENC (NAME_TBD ,13 ,1 ),/* [16] decl_tag */ +BTF_DECL_TAG_ENC (NAME_TBD ,7 ,-1 ),/* [17] decl_tag */ BTF_END_RAW , }, - BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P"), + BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q"), }, .opts = { .dont_resolve_fwds = false,
@@ -7204,6 +7206,39 @@ const struct btf_dedup_test dedup_tests[] = { .dont_resolve_fwds = false, }, }, +{ + .descr = "dedup: typedef tags", + .input = { + .raw_types = { + /* int */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_TYPEDEF_ENC(NAME_NTH(1), 1), /* [2] */ + BTF_TYPEDEF_ENC(NAME_NTH(1), 1), /* [3] */ + /* tag -> t: tag1, tag2 */ + BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1), /* [4] */ + BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1), /* [5] */ + /* tag -> t: tag1, tag3 */ + BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1), /* [6] */ + BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1), /* [7] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"), + }, + .expect = { + .raw_types = { + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */ + BTF_TYPEDEF_ENC(NAME_NTH(1), 1), /* [2] */ + BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1), /* [3] */ + BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1), /* [4] */ + BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1), /* [5] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"), + }, + .opts = { + .dont_resolve_fwds = false, + }, +}, };
Signature · 1 line
2.30.2
PATCH 4/5 selftests/bpf: add BTF_KIND_DECL_TAG typedef example in tag.c
In reply to Yonghong SongChange value type in progs/tag.c to a typedef with a btf_decl_tag. With bpftool btf dump file tag.o, we have
...
[14] TYPEDEF 'value_t' type_id=17
[15] DECL_TAG 'tag1' type_id=14 component_idx=-1
[16] DECL_TAG 'tag2' type_id=14 component_idx=-1
[17] STRUCT '(anon)' size=8 vlen=2
'a' type_id=2 bits_offset=0
'b' type_id=2 bits_offset=32...
The btf_tag selftest also succeeded:
$ ./test_progs -t tag
#21 btf_tag:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED--- tools/testing/selftests/bpf/progs/tag.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/tag.c +7 -2
@@ -24,18 +24,23 @@ struct key_t {int c; }__tag1 __tag2; +typedef struct { +int a; +int b; +} value_t __tag1 __tag2; +struct { __uint(type, BPF_MAP_TYPE_HASH); __uint(max_entries, 3); __type(key, struct key_t); - __type(value, __u64); + __type(value, value_t); } hashmap1 SEC(".maps"); static __noinline int foo(int x __tag1 __tag2) __tag1 __tag2 {struct key_t key; -__u64 val =1 ; +value_t val = {}; key.a = key.b = key.c = x;bpf_map_update_elem (&hashmap1, &key, &val,0 );
Signature · 1 line
2.30.2
PATCH 5/5 docs/bpf: update documentation for BTF_KIND_DECL_TAG typedef support
In reply to Yonghong SongAdd BTF_KIND_DECL_TAG typedef support in btf.rst.
--- Documentation/bpf/btf.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/bpf/btf.rst +3 -3
@@ -474,7 +474,7 @@ No additional type data follow ``btf_type``. * ``info.kind_flag``: 0 * ``info.kind``: BTF_KIND_DECL_TAG * ``info.vlen``: 0 - * ``type``: ``struct``, ``union``, ``func`` or ``var`` + * ``type``: ``struct``, ``union``, ``func``, ``var`` or ``typedef`` ``btf_type`` is followed by ``struct btf_decl_tag``.::
@@ -483,8 +483,8 @@ No additional type data follow ``btf_type``. }; The ``name_off`` encodes btf_decl_tag attribute string. -The ``type`` should be ``struct``, ``union``, ``func`` or ``var``. -For ``var`` type, ``btf_decl_tag.component_idx`` must be ``-1``. +The ``type`` should be ``struct``, ``union``, ``func``, ``var`` or ``typedef``. +For ``var`` or ``typedef`` type, ``btf_decl_tag.component_idx`` must be ``-1``. For the other three types, if the btf_decl_tag attribute is applied to the ``struct``, ``union`` or ``func`` itself, ``btf_decl_tag.component_idx`` must be ``-1``. Otherwise,
Signature · 1 line
2.30.2
Alexei Starovoitov
AppliedQuoted 7 lines · Yonghong Song
Latest upstream llvm-project added support for
btf_decl_tagattributes for typedef declarations ([1], [2]). Similar to otherbtf_decl_tagcases, func/func_param/global_var/struct/union/field,btf_decl_tagwith typedef declaration can carry information from kernel source to clang compiler and then to dwarf/BTF, for bpf verification or other use cases.
Quoted 8 lines
This patch set added kernel support for
BTF_KIND_DECL_TAGto typedef declaration (Patch 1). Additional selftests are added to cover unit testing, dedup, or bpf program usage ofbtf_decl_tagwith typedef. (Patches 2, 3 and 4). The btf documentation is updated to includeBTF_KIND_DECL_TAGtypedef (Patch 5).[1] https://reviews.llvm.org/D110127
[2] https://reviews.llvm.org/D112259
Applied. Thanks