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

selftests/bpf: extend existing map resize tests for per-cpu use case

Add a per-cpu array resizing use case and demonstrate how
bpf_get_smp_processor_id() can be used to directly access proper data
with no extra checks.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230711232400.1658562-2-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
c21de5fc f42bcd16

+17 -5
+11 -3
tools/testing/selftests/bpf/prog_tests/global_map_resize.c
··· 22 22 struct test_global_map_resize *skel; 23 23 struct bpf_map *map; 24 24 const __u32 desired_sz = sizeof(skel->bss->sum) + sysconf(_SC_PAGE_SIZE) * 2; 25 - size_t array_len, actual_sz; 25 + size_t array_len, actual_sz, new_sz; 26 26 27 27 skel = test_global_map_resize__open(); 28 28 if (!ASSERT_OK_PTR(skel, "test_global_map_resize__open")) ··· 41 41 goto teardown; 42 42 if (!ASSERT_EQ(bpf_map__value_size(map), desired_sz, "resize")) 43 43 goto teardown; 44 + 45 + new_sz = sizeof(skel->data_percpu_arr->percpu_arr[0]) * libbpf_num_possible_cpus(); 46 + err = bpf_map__set_value_size(skel->maps.data_percpu_arr, new_sz); 47 + ASSERT_OK(err, "percpu_arr_resize"); 44 48 45 49 /* set the expected number of elements based on the resized array */ 46 50 array_len = (desired_sz - sizeof(skel->bss->sum)) / sizeof(skel->bss->array[0]); ··· 88 84 89 85 static void global_map_resize_data_subtest(void) 90 86 { 91 - int err; 92 87 struct test_global_map_resize *skel; 93 88 struct bpf_map *map; 94 89 const __u32 desired_sz = sysconf(_SC_PAGE_SIZE) * 2; 95 - size_t array_len, actual_sz; 90 + size_t array_len, actual_sz, new_sz; 91 + int err; 96 92 97 93 skel = test_global_map_resize__open(); 98 94 if (!ASSERT_OK_PTR(skel, "test_global_map_resize__open")) ··· 111 107 goto teardown; 112 108 if (!ASSERT_EQ(bpf_map__value_size(map), desired_sz, "resize")) 113 109 goto teardown; 110 + 111 + new_sz = sizeof(skel->data_percpu_arr->percpu_arr[0]) * libbpf_num_possible_cpus(); 112 + err = bpf_map__set_value_size(skel->maps.data_percpu_arr, new_sz); 113 + ASSERT_OK(err, "percpu_arr_resize"); 114 114 115 115 /* set the expected number of elements based on the resized array */ 116 116 array_len = (desired_sz - sizeof(skel->bss->sum)) / sizeof(skel->data_custom->my_array[0]);
+6 -2
tools/testing/selftests/bpf/progs/test_global_map_resize.c
··· 29 29 int my_array_first[1] SEC(".data.array_not_last"); 30 30 int my_int_last SEC(".data.array_not_last"); 31 31 32 + int percpu_arr[1] SEC(".data.percpu_arr"); 33 + 32 34 SEC("tp/syscalls/sys_enter_getpid") 33 35 int bss_array_sum(void *ctx) 34 36 { 35 37 if (pid != (bpf_get_current_pid_tgid() >> 32)) 36 38 return 0; 37 39 38 - sum = 0; 40 + /* this will be zero, we just rely on verifier not rejecting this */ 41 + sum = percpu_arr[bpf_get_smp_processor_id()]; 39 42 40 43 for (size_t i = 0; i < bss_array_len; ++i) 41 44 sum += array[i]; ··· 52 49 if (pid != (bpf_get_current_pid_tgid() >> 32)) 53 50 return 0; 54 51 55 - sum = 0; 52 + /* this will be zero, we just rely on verifier not rejecting this */ 53 + sum = percpu_arr[bpf_get_smp_processor_id()]; 56 54 57 55 for (size_t i = 0; i < data_array_len; ++i) 58 56 sum += my_array[i];