Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add test to access integer type of variable array

Add prog test for accessing integer type of variable array in tracing
program.
In addition, hook load_balance function to access sd->span[0], only
to confirm whether the load is successful. Because there is no direct
way to trigger load_balance call.

Co-developed-by: Chengming Zhou <zhouchengming@bytedance.com>
Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
Link: https://lore.kernel.org/r/20230420032735.27760-3-zhoufeng.zf@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Feng Zhou and committed by
Alexei Starovoitov
5ff54ded 2569c7b8

+70
+20
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
··· 28 28 long b; 29 29 }; 30 30 31 + struct bpf_testmod_struct_arg_3 { 32 + int a; 33 + int b[]; 34 + }; 35 + 31 36 __diag_push(); 32 37 __diag_ignore_all("-Wmissing-prototypes", 33 38 "Global functions as their definitions will be in bpf_testmod.ko BTF"); ··· 65 60 noinline int 66 61 bpf_testmod_test_struct_arg_5(void) { 67 62 bpf_testmod_test_struct_arg_result = 1; 63 + return bpf_testmod_test_struct_arg_result; 64 + } 65 + 66 + noinline int 67 + bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) { 68 + bpf_testmod_test_struct_arg_result = a->b[0]; 68 69 return bpf_testmod_test_struct_arg_result; 69 70 } 70 71 ··· 206 195 }; 207 196 struct bpf_testmod_struct_arg_1 struct_arg1 = {10}; 208 197 struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3}; 198 + struct bpf_testmod_struct_arg_3 *struct_arg3; 209 199 int i = 1; 210 200 211 201 while (bpf_testmod_return_ptr(i)) ··· 217 205 (void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2); 218 206 (void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2); 219 207 (void)bpf_testmod_test_struct_arg_5(); 208 + 209 + struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) + 210 + sizeof(int)), GFP_KERNEL); 211 + if (struct_arg3 != NULL) { 212 + struct_arg3->b[0] = 1; 213 + (void)bpf_testmod_test_struct_arg_6(struct_arg3); 214 + kfree(struct_arg3); 215 + } 220 216 221 217 /* This is always true. Use the check to make sure the compiler 222 218 * doesn't remove bpf_testmod_loop_test.
+16
tools/testing/selftests/bpf/prog_tests/access_variable_array.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2022 Bytedance */ 3 + 4 + #include <test_progs.h> 5 + #include "test_access_variable_array.skel.h" 6 + 7 + void test_access_variable_array(void) 8 + { 9 + struct test_access_variable_array *skel; 10 + 11 + skel = test_access_variable_array__open_and_load(); 12 + if (!ASSERT_OK_PTR(skel, "test_access_variable_array__open_and_load")) 13 + return; 14 + 15 + test_access_variable_array__destroy(skel); 16 + }
+2
tools/testing/selftests/bpf/prog_tests/tracing_struct.c
··· 53 53 54 54 ASSERT_EQ(skel->bss->t5_ret, 1, "t5 ret"); 55 55 56 + ASSERT_EQ(skel->bss->t6, 1, "t6 ret"); 57 + 56 58 tracing_struct__detach(skel); 57 59 destroy_skel: 58 60 tracing_struct__destroy(skel);
+19
tools/testing/selftests/bpf/progs/test_access_variable_array.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2023 Bytedance */ 3 + 4 + #include "vmlinux.h" 5 + #include <bpf/bpf_helpers.h> 6 + #include <bpf/bpf_tracing.h> 7 + 8 + unsigned long span = 0; 9 + 10 + SEC("fentry/load_balance") 11 + int BPF_PROG(fentry_fentry, int this_cpu, struct rq *this_rq, 12 + struct sched_domain *sd) 13 + { 14 + span = sd->span[0]; 15 + 16 + return 0; 17 + } 18 + 19 + char _license[] SEC("license") = "GPL";
+13
tools/testing/selftests/bpf/progs/tracing_struct.c
··· 13 13 long b; 14 14 }; 15 15 16 + struct bpf_testmod_struct_arg_3 { 17 + int a; 18 + int b[]; 19 + }; 20 + 16 21 long t1_a_a, t1_a_b, t1_b, t1_c, t1_ret, t1_nregs; 17 22 __u64 t1_reg0, t1_reg1, t1_reg2, t1_reg3; 18 23 long t2_a, t2_b_a, t2_b_b, t2_c, t2_ret; 19 24 long t3_a, t3_b, t3_c_a, t3_c_b, t3_ret; 20 25 long t4_a_a, t4_b, t4_c, t4_d, t4_e_a, t4_e_b, t4_ret; 21 26 long t5_ret; 27 + int t6; 22 28 23 29 SEC("fentry/bpf_testmod_test_struct_arg_1") 24 30 int BPF_PROG2(test_struct_arg_1, struct bpf_testmod_struct_arg_2, a, int, b, int, c) ··· 120 114 int BPF_PROG2(test_struct_arg_10, int, ret) 121 115 { 122 116 t5_ret = ret; 117 + return 0; 118 + } 119 + 120 + SEC("fentry/bpf_testmod_test_struct_arg_6") 121 + int BPF_PROG2(test_struct_arg_11, struct bpf_testmod_struct_arg_3 *, a) 122 + { 123 + t6 = a->b[0]; 123 124 return 0; 124 125 } 125 126