diff mbox series

[bpf-next,v2,08/11] selftests/bpf: add BTF_KIND_TAG unit tests

Message ID 20210913155206.3728212-1-yhs@fb.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: add support for new btf kind BTF_KIND_TAG | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 10 maintainers not CCed: kpsingh@kernel.org john.fastabend@gmail.com iii@linux.ibm.com linux-kselftest@vger.kernel.org shuah@kernel.org yangjunlin@yulong.com quentin@isovalent.com songliubraving@fb.com netdev@vger.kernel.org kafai@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch fail ERROR: Macros with complex values should be enclosed in parentheses WARNING: line length of 81 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
bpf/vmtest-bpf-next success VM_Test
bpf/vmtest-bpf-next-PR success PR summary

Commit Message

Yonghong Song Sept. 13, 2021, 3:52 p.m. UTC
Test good and bad variants of BTF_KIND_TAG encoding.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |   3 +
 2 files changed, 226 insertions(+)

Comments

Andrii Nakryiko Sept. 14, 2021, 5:31 a.m. UTC | #1
On Mon, Sep 13, 2021 at 8:52 AM Yonghong Song <yhs@fb.com> wrote:
>
> Test good and bad variants of BTF_KIND_TAG encoding.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++
>  tools/testing/selftests/bpf/test_btf.h       |   3 +
>  2 files changed, 226 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
> index ad39f4d588d0..21b122f72a55 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
> @@ -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] */

this is not a union. Other tests open-code union, but it's probably a
good idea to add BTF_UNION_ENC

> +               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 #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.

> +               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),

nit: could have used BTF_TAG_ENC?

> +               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",
> +},
> +

please also add invalid negative component_idx test (e.g., -2)

>  }; /* 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;
> diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
> index e2394eea4b7f..0619e06d745e 100644
> --- a/tools/testing/selftests/bpf/test_btf.h
> +++ b/tools/testing/selftests/bpf/test_btf.h
> @@ -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
>
Yonghong Song Sept. 14, 2021, 5 p.m. UTC | #2
On 9/13/21 10:31 PM, Andrii Nakryiko wrote:
> On Mon, Sep 13, 2021 at 8:52 AM Yonghong Song <yhs@fb.com> wrote:
>>
>> Test good and bad variants of BTF_KIND_TAG encoding.
>>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++
>>   tools/testing/selftests/bpf/test_btf.h       |   3 +
>>   2 files changed, 226 insertions(+)
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
>> index ad39f4d588d0..21b122f72a55 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
>> @@ -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] */
> 
> 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.

> 
>> +               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 #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.

Ack.

> 
>> +               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),
> 
> nit: could have used BTF_TAG_ENC?

Ack.

> 
>> +               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",
>> +},
>> +
> 
> please also add invalid negative component_idx test (e.g., -2)

Ack.

> 
>>   }; /* 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;
>> diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
>> index e2394eea4b7f..0619e06d745e 100644
>> --- a/tools/testing/selftests/bpf/test_btf.h
>> +++ b/tools/testing/selftests/bpf/test_btf.h
>> @@ -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
>>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index ad39f4d588d0..21b122f72a55 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -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;
diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
index e2394eea4b7f..0619e06d745e 100644
--- a/tools/testing/selftests/bpf/test_btf.h
+++ b/tools/testing/selftests/bpf/test_btf.h
@@ -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 */