]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Add more test case for field flattening
authorHou Tao <houtao1@huawei.com>
Tue, 8 Oct 2024 07:11:14 +0000 (15:11 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 9 Oct 2024 23:32:47 +0000 (16:32 -0700)
Add three success test cases to test the flattening of array of nested
struct. For these three tests, the number of special fields in map is
BTF_FIELDS_MAX, but the array is defined in structs with different
nested level.

Add one failure test case for the flattening as well. In the test case,
the number of special fields in map is BTF_FIELDS_MAX + 1. It will make
btf_parse_fields() in map_create() return -E2BIG, the creation of map
will succeed, but the load of program will fail because the btf_record
is invalid for the map.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20241008071114.3718177-3-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/cpumask.c
tools/testing/selftests/bpf/progs/cpumask_common.h
tools/testing/selftests/bpf/progs/cpumask_failure.c
tools/testing/selftests/bpf/progs/cpumask_success.c

index 2570bd4b0cb228d3cdb9aae05a22cb4cb5534758..e58a04654238c9b4d1d366f9f0537675910cc153 100644 (file)
@@ -23,6 +23,7 @@ static const char * const cpumask_success_testcases[] = {
        "test_global_mask_array_l2_rcu",
        "test_global_mask_nested_rcu",
        "test_global_mask_nested_deep_rcu",
+       "test_global_mask_nested_deep_array_rcu",
        "test_cpumask_weight",
 };
 
index b979e91f55f07906736f12746b9c1e58d84b29c6..4ece7873ba609d3e86e7aeb2f6337ab561e59f68 100644 (file)
@@ -7,6 +7,11 @@
 #include "errno.h"
 #include <stdbool.h>
 
+/* Should use BTF_FIELDS_MAX, but it is not always available in vmlinux.h,
+ * so use the hard-coded number as a workaround.
+ */
+#define CPUMASK_KPTR_FIELDS_MAX 11
+
 int err;
 
 #define private(name) SEC(".bss." #name) __attribute__((aligned(8)))
index a988d2823b5285a3c6b40b636135b74529ce013f..b40b52548ffb0ef6a6391e78cb20feff23bc70d3 100644 (file)
 
 char _license[] SEC("license") = "GPL";
 
+struct kptr_nested_array_2 {
+       struct bpf_cpumask __kptr * mask;
+};
+
+struct kptr_nested_array_1 {
+       /* Make btf_parse_fields() in map_create() return -E2BIG */
+       struct kptr_nested_array_2 d_2[CPUMASK_KPTR_FIELDS_MAX + 1];
+};
+
+struct kptr_nested_array {
+       struct kptr_nested_array_1 d_1;
+};
+
+private(MASK_NESTED) static struct kptr_nested_array global_mask_nested_arr;
+
 /* Prototype for all of the program trace events below:
  *
  * TRACE_EVENT(task_newtask,
@@ -187,3 +202,23 @@ int BPF_PROG(test_global_mask_rcu_no_null_check, struct task_struct *task, u64 c
 
        return 0;
 }
+
+SEC("tp_btf/task_newtask")
+__failure __msg("has no valid kptr")
+int BPF_PROG(test_invalid_nested_array, struct task_struct *task, u64 clone_flags)
+{
+       struct bpf_cpumask *local, *prev;
+
+       local = create_cpumask();
+       if (!local)
+               return 0;
+
+       prev = bpf_kptr_xchg(&global_mask_nested_arr.d_1.d_2[CPUMASK_KPTR_FIELDS_MAX].mask, local);
+       if (prev) {
+               bpf_cpumask_release(prev);
+               err = 3;
+               return 0;
+       }
+
+       return 0;
+}
index fd8106831c32c356a5a494b09f374a55221e1133..80ee469b0b6028634308f05df383471bc6bcec70 100644 (file)
@@ -31,11 +31,59 @@ struct kptr_nested_deep {
        struct kptr_nested_pair ptr_pairs[3];
 };
 
+struct kptr_nested_deep_array_1_2 {
+       int dummy;
+       struct bpf_cpumask __kptr * mask[CPUMASK_KPTR_FIELDS_MAX];
+};
+
+struct kptr_nested_deep_array_1_1 {
+       int dummy;
+       struct kptr_nested_deep_array_1_2 d_2;
+};
+
+struct kptr_nested_deep_array_1 {
+       long dummy;
+       struct kptr_nested_deep_array_1_1 d_1;
+};
+
+struct kptr_nested_deep_array_2_2 {
+       long dummy[2];
+       struct bpf_cpumask __kptr * mask;
+};
+
+struct kptr_nested_deep_array_2_1 {
+       int dummy;
+       struct kptr_nested_deep_array_2_2 d_2[CPUMASK_KPTR_FIELDS_MAX];
+};
+
+struct kptr_nested_deep_array_2 {
+       long dummy;
+       struct kptr_nested_deep_array_2_1 d_1;
+};
+
+struct kptr_nested_deep_array_3_2 {
+       long dummy[2];
+       struct bpf_cpumask __kptr * mask;
+};
+
+struct kptr_nested_deep_array_3_1 {
+       int dummy;
+       struct kptr_nested_deep_array_3_2 d_2;
+};
+
+struct kptr_nested_deep_array_3 {
+       long dummy;
+       struct kptr_nested_deep_array_3_1 d_1[CPUMASK_KPTR_FIELDS_MAX];
+};
+
 private(MASK) static struct bpf_cpumask __kptr * global_mask_array[2];
 private(MASK) static struct bpf_cpumask __kptr * global_mask_array_l2[2][1];
 private(MASK) static struct bpf_cpumask __kptr * global_mask_array_one[1];
 private(MASK) static struct kptr_nested global_mask_nested[2];
 private(MASK_DEEP) static struct kptr_nested_deep global_mask_nested_deep;
+private(MASK_1) static struct kptr_nested_deep_array_1 global_mask_nested_deep_array_1;
+private(MASK_2) static struct kptr_nested_deep_array_2 global_mask_nested_deep_array_2;
+private(MASK_3) static struct kptr_nested_deep_array_3 global_mask_nested_deep_array_3;
 
 static bool is_test_task(void)
 {
@@ -543,12 +591,21 @@ static int _global_mask_array_rcu(struct bpf_cpumask **mask0,
                goto err_exit;
        }
 
-       /* [<mask 0>, NULL] */
-       if (!*mask0 || *mask1) {
+       /* [<mask 0>, *] */
+       if (!*mask0) {
                err = 2;
                goto err_exit;
        }
 
+       if (!mask1)
+               goto err_exit;
+
+       /* [*, NULL] */
+       if (*mask1) {
+               err = 3;
+               goto err_exit;
+       }
+
        local = create_cpumask();
        if (!local) {
                err = 9;
@@ -631,6 +688,23 @@ int BPF_PROG(test_global_mask_nested_deep_rcu, struct task_struct *task, u64 clo
        return 0;
 }
 
+SEC("tp_btf/task_newtask")
+int BPF_PROG(test_global_mask_nested_deep_array_rcu, struct task_struct *task, u64 clone_flags)
+{
+       int i;
+
+       for (i = 0; i < CPUMASK_KPTR_FIELDS_MAX; i++)
+               _global_mask_array_rcu(&global_mask_nested_deep_array_1.d_1.d_2.mask[i], NULL);
+
+       for (i = 0; i < CPUMASK_KPTR_FIELDS_MAX; i++)
+               _global_mask_array_rcu(&global_mask_nested_deep_array_2.d_1.d_2[i].mask, NULL);
+
+       for (i = 0; i < CPUMASK_KPTR_FIELDS_MAX; i++)
+               _global_mask_array_rcu(&global_mask_nested_deep_array_3.d_1[i].d_2.mask, NULL);
+
+       return 0;
+}
+
 SEC("tp_btf/task_newtask")
 int BPF_PROG(test_cpumask_weight, struct task_struct *task, u64 clone_flags)
 {