diff mbox series

[v6,bpf-next,7/9] selftest/bpf: Add BTF_KIND_FLOAT tests

Message ID 20210224234535.106970-8-iii@linux.ibm.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Add BTF_KIND_FLOAT support | 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 8 maintainers not CCed: sdf@google.com netdev@vger.kernel.org shuah@kernel.org kpsingh@kernel.org songliubraving@fb.com linux-kselftest@vger.kernel.org kafai@fb.com komachi.yoshiki@gmail.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 warning WARNING: quoted string split across lines
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

Ilya Leoshkevich Feb. 24, 2021, 11:45 p.m. UTC
Test the good variants as well as the potential malformed ones.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/btf_helpers.c    |   4 +
 tools/testing/selftests/bpf/prog_tests/btf.c | 131 +++++++++++++++++++
 tools/testing/selftests/bpf/test_btf.h       |   3 +
 3 files changed, 138 insertions(+)

Comments

Ilya Leoshkevich Feb. 24, 2021, 11:51 p.m. UTC | #1
On Thu, 2021-02-25 at 00:45 +0100, Ilya Leoshkevich wrote:
> Test the good variants as well as the potential malformed ones.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> Acked-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/testing/selftests/bpf/btf_helpers.c    |   4 +
>  tools/testing/selftests/bpf/prog_tests/btf.c | 131 +++++++++++++++++++
>  tools/testing/selftests/bpf/test_btf.h       |   3 +
>  3 files changed, 138 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/btf_helpers.c
> b/tools/testing/selftests/bpf/btf_helpers.c
> index 48f90490f922..b692e6ead9b5 100644
> --- a/tools/testing/selftests/bpf/btf_helpers.c
> +++ b/tools/testing/selftests/bpf/btf_helpers.c
> @@ -23,6 +23,7 @@ static const char * const btf_kind_str_mapping[] = {
>         [BTF_KIND_FUNC_PROTO]   = "FUNC_PROTO",
>         [BTF_KIND_VAR]          = "VAR",
>         [BTF_KIND_DATASEC]      = "DATASEC",
> +       [BTF_KIND_FLOAT]        = "FLOAT",
>  };
>  
>  static const char *btf_kind_str(__u16 kind)
> @@ -173,6 +174,9 @@ int fprintf_btf_type_raw(FILE *out, const struct
> btf *btf, __u32 id)
>                 }
>                 break;
>         }
> +       case BTF_KIND_FLOAT:
> +               fprintf(out, " size=%u", t->size);
> +               break;
>         default:
>                 break;
>         }
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c
> b/tools/testing/selftests/bpf/prog_tests/btf.c
> index c29406736138..11d98d3cf949 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
> @@ -3531,6 +3531,136 @@ static struct btf_raw_test raw_tests[] = {
>         .max_entries = 1,
>  },
>  
> +{
> +       .descr = "float test #1, well-formed",
> +       .raw_types = {
> +               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
> +                                                               /* [1]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),                /* [2]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),                /* [3]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 8),                /* [4]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 12),               /* [5]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 16),               /* [6]
> */
> +               BTF_STRUCT_ENC(NAME_TBD, 5, 48),                /* [7]
> */
> +               BTF_MEMBER_ENC(NAME_TBD, 2, 0),
> +               BTF_MEMBER_ENC(NAME_TBD, 3, 32),
> +               BTF_MEMBER_ENC(NAME_TBD, 4, 64),
> +               BTF_MEMBER_ENC(NAME_TBD, 5, 128),
> +               BTF_MEMBER_ENC(NAME_TBD, 6, 256),
> +               BTF_END_RAW,
> +       },
> +       BTF_STR_SEC("\0int\0_Float16\0float\0double\0_Float80\0long_dou
> ble"
> +                   "\0floats\0a\0b\0c\0d\0e"),
> +       .map_type = BPF_MAP_TYPE_ARRAY,
> +       .map_name = "float_type_check_btf",
> +       .key_size = sizeof(int),
> +       .value_size = 48,
> +       .key_type_id = 1,
> +       .value_type_id = 7,
> +       .max_entries = 1,
> +},
> +{
> +       .descr = "float test #2, invalid vlen",
> +       .raw_types = {
> +               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
> +                                                               /* [1]
> */
> +               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 0,
> 1), 4),
> +                                                               /* [2]
> */
> +               BTF_END_RAW,
> +       },
> +       BTF_STR_SEC("\0int\0float"),
> +       .map_type = BPF_MAP_TYPE_ARRAY,
> +       .map_name = "float_type_check_btf",
> +       .key_size = sizeof(int),
> +       .value_size = 4,
> +       .key_type_id = 1,
> +       .value_type_id = 2,
> +       .max_entries = 1,
> +       .btf_load_err = true,
> +       .err_str = "vlen != 0",
> +},
> +{
> +       .descr = "float test #3, invalid kind_flag",
> +       .raw_types = {
> +               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
> +                                                               /* [1]
> */
> +               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 1,
> 0), 4),
> +                                                               /* [2]
> */
> +               BTF_END_RAW,
> +       },
> +       BTF_STR_SEC("\0int\0float"),
> +       .map_type = BPF_MAP_TYPE_ARRAY,
> +       .map_name = "float_type_check_btf",
> +       .key_size = sizeof(int),
> +       .value_size = 4,
> +       .key_type_id = 1,
> +       .value_type_id = 2,
> +       .max_entries = 1,
> +       .btf_load_err = true,
> +       .err_str = "Invalid btf_info kind_flag",
> +},
> +{
> +       .descr = "float test #4, member does not fit",
> +       .raw_types = {
> +               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
> +                                                               /* [1]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),                /* [2]
> */
> +               BTF_STRUCT_ENC(NAME_TBD, 1, 2),                 /* [3]
> */
> +               BTF_MEMBER_ENC(NAME_TBD, 2, 0),
> +               BTF_END_RAW,
> +       },
> +       BTF_STR_SEC("\0int\0float\0floats\0x"),
> +       .map_type = BPF_MAP_TYPE_ARRAY,
> +       .map_name = "float_type_check_btf",
> +       .key_size = sizeof(int),
> +       .value_size = 4,
> +       .key_type_id = 1,
> +       .value_type_id = 3,
> +       .max_entries = 1,
> +       .btf_load_err = true,
> +       .err_str = "Member exceeds struct_size",
> +},
> +{
> +       .descr = "float test #5, member is not properly aligned",
> +       .raw_types = {
> +               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
> +                                                               /* [1]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),                /* [2]
> */
> +               BTF_STRUCT_ENC(NAME_TBD, 1, 8),                 /* [3]
> */
> +               BTF_MEMBER_ENC(NAME_TBD, 2, 8),
> +               BTF_END_RAW,
> +       },
> +       BTF_STR_SEC("\0int\0float\0floats\0x"),
> +       .map_type = BPF_MAP_TYPE_ARRAY,
> +       .map_name = "float_type_check_btf",
> +       .key_size = sizeof(int),
> +       .value_size = 4,
> +       .key_type_id = 1,
> +       .value_type_id = 3,
> +       .max_entries = 1,
> +       .btf_load_err = true,
> +       .err_str = "Member is not properly aligned",
> +},
> +{
> +       .descr = "float test #6, invalid size",
> +       .raw_types = {
> +               BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
> +                                                               /* [1]
> */
> +               BTF_TYPE_FLOAT_ENC(NAME_TBD, 6),                /* [2]
> */
> +               BTF_END_RAW,
> +       },
> +       BTF_STR_SEC("\0int\0float"),
> +       .map_type = BPF_MAP_TYPE_ARRAY,
> +       .map_name = "float_type_check_btf",
> +       .key_size = sizeof(int),
> +       .value_size = 6,
> +       .key_type_id = 1,
> +       .value_type_id = 2,
> +       .max_entries = 1,
> +       .btf_load_err = true,
> +       .err_str = "Invalid type_size",
> +},
> +
>  }; /* struct btf_raw_test raw_tests[] */
>  
>  static const char *get_next_str(const char *start, const char *end)
> @@ -6630,6 +6760,7 @@ static int btf_type_size(const struct btf_type
> *t)
>         case BTF_KIND_PTR:
>         case BTF_KIND_TYPEDEF:
>         case BTF_KIND_FUNC:
> +       case BTF_KIND_FLOAT:
>                 return base_size;
>         case BTF_KIND_INT:
>                 return base_size + sizeof(__u32);
> diff --git a/tools/testing/selftests/bpf/test_btf.h
> b/tools/testing/selftests/bpf/test_btf.h
> index 2023725f1962..e2394eea4b7f 100644
> --- a/tools/testing/selftests/bpf/test_btf.h
> +++ b/tools/testing/selftests/bpf/test_btf.h
> @@ -66,4 +66,7 @@
>  #define BTF_FUNC_ENC(name, func_proto) \
>         BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0),
> func_proto)
>  
> +#define BTF_TYPE_FLOAT_ENC(name, sz) \
> +       BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
> +
>  #endif /* _TEST_BTF_H */

Sorry, I forgot to add:

Acked-by: Andrii Nakryiko <andrii@kernel.org>

to the commit description here [1].

[1]
https://lore.kernel.org/bpf/CAEf4BzZXvokRMWqDrOg2JWFPr+caNjG=yeCM-W_9cDhkVraRPg@mail.gmail.com/
John Fastabend Feb. 26, 2021, 2:01 a.m. UTC | #2
Ilya Leoshkevich wrote:
> Test the good variants as well as the potential malformed ones.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> Acked-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/testing/selftests/bpf/btf_helpers.c    |   4 +
>  tools/testing/selftests/bpf/prog_tests/btf.c | 131 +++++++++++++++++++
>  tools/testing/selftests/bpf/test_btf.h       |   3 +
>  3 files changed, 138 insertions(+)
> 

Should we also add a test in reloc_size.c to verify bpf_core_field_size()
is working correctly with floats. Seems like a useful test to me.

diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_size.c b/tools/testing/selftests/bpf/progs/test_core_reloc_size.c
index d7fb6cfc7891..c75d135009ab 100644
--- a/tools/testing/selftests/bpf/progs/test_core_reloc_size.c
+++ b/tools/testing/selftests/bpf/progs/test_core_reloc_size.c
@@ -30,6 +30,7 @@ struct core_reloc_size {
        int arr_field[4];
        void *ptr_field;
        enum { VALUE = 123 } enum_field;
+       float floating;
 };
 
 SEC("raw_tracepoint/sys_enter")
@@ -45,6 +46,7 @@ int test_core_size(void *ctx)
        out->arr_elem_sz = bpf_core_field_size(in->arr_field[0]);
        out->ptr_sz = bpf_core_field_size(in->ptr_field);
        out->enum_sz = bpf_core_field_size(in->enum_field);
+       out->floating_sz = bpf_core_field_size(in->floating);
 
        return 0;
 }
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/btf_helpers.c b/tools/testing/selftests/bpf/btf_helpers.c
index 48f90490f922..b692e6ead9b5 100644
--- a/tools/testing/selftests/bpf/btf_helpers.c
+++ b/tools/testing/selftests/bpf/btf_helpers.c
@@ -23,6 +23,7 @@  static const char * const btf_kind_str_mapping[] = {
 	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
 	[BTF_KIND_VAR]		= "VAR",
 	[BTF_KIND_DATASEC]	= "DATASEC",
+	[BTF_KIND_FLOAT]	= "FLOAT",
 };
 
 static const char *btf_kind_str(__u16 kind)
@@ -173,6 +174,9 @@  int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
 		}
 		break;
 	}
+	case BTF_KIND_FLOAT:
+		fprintf(out, " size=%u", t->size);
+		break;
 	default:
 		break;
 	}
diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index c29406736138..11d98d3cf949 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -3531,6 +3531,136 @@  static struct btf_raw_test raw_tests[] = {
 	.max_entries = 1,
 },
 
+{
+	.descr = "float test #1, well-formed",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+								/* [1] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),		/* [2] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [3] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 8),		/* [4] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 12),		/* [5] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 16),		/* [6] */
+		BTF_STRUCT_ENC(NAME_TBD, 5, 48),		/* [7] */
+		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
+		BTF_MEMBER_ENC(NAME_TBD, 3, 32),
+		BTF_MEMBER_ENC(NAME_TBD, 4, 64),
+		BTF_MEMBER_ENC(NAME_TBD, 5, 128),
+		BTF_MEMBER_ENC(NAME_TBD, 6, 256),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0int\0_Float16\0float\0double\0_Float80\0long_double"
+		    "\0floats\0a\0b\0c\0d\0e"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 48,
+	.key_type_id = 1,
+	.value_type_id = 7,
+	.max_entries = 1,
+},
+{
+	.descr = "float test #2, invalid vlen",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+								/* [1] */
+		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 1), 4),
+								/* [2] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0int\0float"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 2,
+	.max_entries = 1,
+	.btf_load_err = true,
+	.err_str = "vlen != 0",
+},
+{
+	.descr = "float test #3, invalid kind_flag",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+								/* [1] */
+		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 1, 0), 4),
+								/* [2] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0int\0float"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 2,
+	.max_entries = 1,
+	.btf_load_err = true,
+	.err_str = "Invalid btf_info kind_flag",
+},
+{
+	.descr = "float test #4, member does not fit",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+								/* [1] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
+		BTF_STRUCT_ENC(NAME_TBD, 1, 2),			/* [3] */
+		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0int\0float\0floats\0x"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 3,
+	.max_entries = 1,
+	.btf_load_err = true,
+	.err_str = "Member exceeds struct_size",
+},
+{
+	.descr = "float test #5, member is not properly aligned",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+								/* [1] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
+		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] */
+		BTF_MEMBER_ENC(NAME_TBD, 2, 8),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0int\0float\0floats\0x"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 4,
+	.key_type_id = 1,
+	.value_type_id = 3,
+	.max_entries = 1,
+	.btf_load_err = true,
+	.err_str = "Member is not properly aligned",
+},
+{
+	.descr = "float test #6, invalid size",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
+								/* [1] */
+		BTF_TYPE_FLOAT_ENC(NAME_TBD, 6),		/* [2] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0int\0float"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 6,
+	.key_type_id = 1,
+	.value_type_id = 2,
+	.max_entries = 1,
+	.btf_load_err = true,
+	.err_str = "Invalid type_size",
+},
+
 }; /* struct btf_raw_test raw_tests[] */
 
 static const char *get_next_str(const char *start, const char *end)
@@ -6630,6 +6760,7 @@  static int btf_type_size(const struct btf_type *t)
 	case BTF_KIND_PTR:
 	case BTF_KIND_TYPEDEF:
 	case BTF_KIND_FUNC:
+	case BTF_KIND_FLOAT:
 		return base_size;
 	case BTF_KIND_INT:
 		return base_size + sizeof(__u32);
diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
index 2023725f1962..e2394eea4b7f 100644
--- a/tools/testing/selftests/bpf/test_btf.h
+++ b/tools/testing/selftests/bpf/test_btf.h
@@ -66,4 +66,7 @@ 
 #define BTF_FUNC_ENC(name, func_proto) \
 	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), func_proto)
 
+#define BTF_TYPE_FLOAT_ENC(name, sz) \
+	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
+
 #endif /* _TEST_BTF_H */