diff mbox series

[v2,bpf-next,5/6] selftest/bpf: Add BTF_KIND_FLOAT tests

Message ID 20210219022543.20893-6-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 9 maintainers not CCed: linux-kselftest@vger.kernel.org netdev@vger.kernel.org songliubraving@fb.com shuah@kernel.org sdf@google.com kpsingh@kernel.org yangjunlin@yulong.com komachi.yoshiki@gmail.com 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 fail Errors and warnings before: 2 this patch: 2
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 157 lines checked
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. 19, 2021, 2:25 a.m. UTC
Test the good variants as well as the potential malformed ones.

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

Comments

Yonghong Song Feb. 19, 2021, 5:38 a.m. UTC | #1
On 2/18/21 6:25 PM, Ilya Leoshkevich wrote:
> Test the good variants as well as the potential malformed ones.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>   tools/testing/selftests/bpf/btf_helpers.c    |   4 +
>   tools/testing/selftests/bpf/prog_tests/btf.c | 122 +++++++++++++++++++
>   tools/testing/selftests/bpf/test_btf.h       |   3 +
>   3 files changed, 129 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 6a7ee7420701..4be14d853cc3 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
> @@ -3531,6 +3531,127 @@ static struct btf_raw_test raw_tests[] = {
>   	.max_entries = 1,
>   },
>   
> +{
> +	.descr = "float test #1, well-formed",
> +	.raw_types = {
> +		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_FLOAT_ENC(1, 2),			/* [2] */
> +		BTF_TYPE_FLOAT_ENC(10, 4),			/* [3] */
> +		BTF_TYPE_FLOAT_ENC(16, 8),			/* [4] */
> +		BTF_TYPE_FLOAT_ENC(23, 16),			/* [5] */
> +		BTF_STRUCT_ENC(35, 4, 32),			/* [6] */
> +		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_END_RAW,
> +	},
> +	BTF_STR_SEC("\0_Float16\0float\0double\0long_double\0floats"),
> +	.map_type = BPF_MAP_TYPE_ARRAY,
> +	.map_name = "float_type_check_btf",
> +	.key_size = sizeof(int),
> +	.value_size = 32,
> +	.key_type_id = 1,
> +	.value_type_id = 6,
> +	.max_entries = 1,
> +},
> +{
> +	.descr = "float test #2, invalid vlen",
> +	.raw_types = {
> +		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 1), 4),
> +								/* [2] */
> +		BTF_END_RAW,
> +	},
> +	BTF_STR_SEC("\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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FLOAT, 1, 0), 4),
> +								/* [2] */
> +		BTF_END_RAW,
> +	},
> +	BTF_STR_SEC("\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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_FLOAT_ENC(1, 4),			/* [2] */
> +		BTF_STRUCT_ENC(7, 1, 2),			/* [3] */
> +		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
> +		BTF_END_RAW,
> +	},
> +	BTF_STR_SEC("\0float\0floats"),
> +	.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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_FLOAT_ENC(1, 4),			/* [2] */
> +		BTF_STRUCT_ENC(7, 1, 8),			/* [3] */
> +		BTF_MEMBER_ENC(NAME_TBD, 2, 8),
> +		BTF_END_RAW,
> +	},
> +	BTF_STR_SEC("\0float\0floats"),
> +	.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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_FLOAT_ENC(1, 6),			/* [2] */
> +		BTF_END_RAW,
> +	},
> +	BTF_STR_SEC("\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)
> @@ -6632,6 +6753,7 @@ static int btf_type_size(const struct btf_type *t)
>   	case BTF_KIND_FUNC:
>   		return base_size;
>   	case BTF_KIND_INT:
> +	case BTF_KIND_FLOAT:
>   		return base_size + sizeof(__u32);

This is not correct.

>   	case BTF_KIND_ENUM:
>   		return base_size + vlen * sizeof(struct btf_enum);
> 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 */
>
Yonghong Song Feb. 19, 2021, 5:44 a.m. UTC | #2
On 2/18/21 6:25 PM, Ilya Leoshkevich wrote:
> Test the good variants as well as the potential malformed ones.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>   tools/testing/selftests/bpf/btf_helpers.c    |   4 +
>   tools/testing/selftests/bpf/prog_tests/btf.c | 122 +++++++++++++++++++
>   tools/testing/selftests/bpf/test_btf.h       |   3 +
>   3 files changed, 129 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 6a7ee7420701..4be14d853cc3 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf.c
> @@ -3531,6 +3531,127 @@ static struct btf_raw_test raw_tests[] = {
>   	.max_entries = 1,
>   },
>   
> +{
> +	.descr = "float test #1, well-formed",
> +	.raw_types = {
> +		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
> +		BTF_TYPE_FLOAT_ENC(1, 2),			/* [2] */
> +		BTF_TYPE_FLOAT_ENC(10, 4),			/* [3] */
> +		BTF_TYPE_FLOAT_ENC(16, 8),			/* [4] */
> +		BTF_TYPE_FLOAT_ENC(23, 16),			/* [5] */
> +		BTF_STRUCT_ENC(35, 4, 32),			/* [6] */

The float and struct names can also use NAME_TBD to avoid potential
miss counting, I think. This also make it consistent with using NAME_TBD 
below.

> +		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_END_RAW,
> +	},
> +	BTF_STR_SEC("\0_Float16\0float\0double\0long_double\0floats"),
> +	.map_type = BPF_MAP_TYPE_ARRAY,
> +	.map_name = "float_type_check_btf",
> +	.key_size = sizeof(int),
> +	.value_size = 32,
> +	.key_type_id = 1,
> +	.value_type_id = 6,
> +	.max_entries = 1,
> +},
> +{
[...]
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 6a7ee7420701..4be14d853cc3 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -3531,6 +3531,127 @@  static struct btf_raw_test raw_tests[] = {
 	.max_entries = 1,
 },
 
+{
+	.descr = "float test #1, well-formed",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_FLOAT_ENC(1, 2),			/* [2] */
+		BTF_TYPE_FLOAT_ENC(10, 4),			/* [3] */
+		BTF_TYPE_FLOAT_ENC(16, 8),			/* [4] */
+		BTF_TYPE_FLOAT_ENC(23, 16),			/* [5] */
+		BTF_STRUCT_ENC(35, 4, 32),			/* [6] */
+		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_END_RAW,
+	},
+	BTF_STR_SEC("\0_Float16\0float\0double\0long_double\0floats"),
+	.map_type = BPF_MAP_TYPE_ARRAY,
+	.map_name = "float_type_check_btf",
+	.key_size = sizeof(int),
+	.value_size = 32,
+	.key_type_id = 1,
+	.value_type_id = 6,
+	.max_entries = 1,
+},
+{
+	.descr = "float test #2, invalid vlen",
+	.raw_types = {
+		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 1), 4),
+								/* [2] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FLOAT, 1, 0), 4),
+								/* [2] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_FLOAT_ENC(1, 4),			/* [2] */
+		BTF_STRUCT_ENC(7, 1, 2),			/* [3] */
+		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0float\0floats"),
+	.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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_FLOAT_ENC(1, 4),			/* [2] */
+		BTF_STRUCT_ENC(7, 1, 8),			/* [3] */
+		BTF_MEMBER_ENC(NAME_TBD, 2, 8),
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\0float\0floats"),
+	.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(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+		BTF_TYPE_FLOAT_ENC(1, 6),			/* [2] */
+		BTF_END_RAW,
+	},
+	BTF_STR_SEC("\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)
@@ -6632,6 +6753,7 @@  static int btf_type_size(const struct btf_type *t)
 	case BTF_KIND_FUNC:
 		return base_size;
 	case BTF_KIND_INT:
+	case BTF_KIND_FLOAT:
 		return base_size + sizeof(__u32);
 	case BTF_KIND_ENUM:
 		return base_size + vlen * sizeof(struct btf_enum);
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 */