Commit f98d6dd1 authored by Guo Zhengkui's avatar Guo Zhengkui Committed by Daniel Borkmann

selftests/bpf: Clean up array_size.cocci warnings

Clean up the array_size.cocci warnings under tools/testing/selftests/bpf/:

Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.

tools/testing/selftests/bpf/test_cgroup_storage.c uses ARRAY_SIZE() defined
in tools/include/linux/kernel.h (sys/sysinfo.h -> linux/kernel.h), while
others use ARRAY_SIZE() in bpf_util.h.
Signed-off-by: default avatarGuo Zhengkui <guozhengkui@vivo.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220315130143.2403-1-guozhengkui@vivo.com
parent 8fa42d78
...@@ -14,7 +14,7 @@ static int prog_load(void) ...@@ -14,7 +14,7 @@ static int prog_load(void)
BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */ BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(prog);
return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB, return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
prog, insns_cnt, "GPL", 0, prog, insns_cnt, "GPL", 0,
......
...@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val) ...@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(prog);
int ret; int ret;
ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB, ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
......
...@@ -16,7 +16,7 @@ static int prog_load(int verdict) ...@@ -16,7 +16,7 @@ static int prog_load(int verdict)
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(prog);
return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB, return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
prog, insns_cnt, "GPL", 0, prog, insns_cnt, "GPL", 0,
......
...@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration) ...@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
{ "relocate .rodata reference", 10, ~0 }, { "relocate .rodata reference", 10, ~0 },
}; };
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {
err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num); err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
CHECK(err || num != tests[i].num, tests[i].name, CHECK(err || num != tests[i].num, tests[i].name,
"err %d result %llx expected %llx\n", "err %d result %llx expected %llx\n",
...@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration) ...@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
{ "relocate .bss reference", 4, "\0\0hello" }, { "relocate .bss reference", 4, "\0\0hello" },
}; };
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {
err = bpf_map_lookup_elem(map_fd, &tests[i].key, str); err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
CHECK(err || memcmp(str, tests[i].str, sizeof(str)), CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
tests[i].name, "err %d result \'%s\' expected \'%s\'\n", tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
...@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration) ...@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
{ "relocate .data reference", 3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } }, { "relocate .data reference", 3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
}; };
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {
err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val); err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)), CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n", tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
......
...@@ -20,7 +20,7 @@ void test_obj_name(void) ...@@ -20,7 +20,7 @@ void test_obj_name(void)
__u32 duration = 0; __u32 duration = 0;
int i; int i;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {
size_t name_len = strlen(tests[i].name) + 1; size_t name_len = strlen(tests[i].name) + 1;
union bpf_attr attr; union bpf_attr attr;
size_t ncopy; size_t ncopy;
......
...@@ -36,7 +36,7 @@ int main(int argc, char **argv) ...@@ -36,7 +36,7 @@ int main(int argc, char **argv)
BPF_MOV64_REG(BPF_REG_0, BPF_REG_1), BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(prog);
int error = EXIT_FAILURE; int error = EXIT_FAILURE;
int map_fd, percpu_map_fd, prog_fd, cgroup_fd; int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
struct bpf_cgroup_storage_key key; struct bpf_cgroup_storage_key key;
......
...@@ -878,11 +878,11 @@ int main(int argc, char **argv) ...@@ -878,11 +878,11 @@ int main(int argc, char **argv)
assert(nr_cpus != -1); assert(nr_cpus != -1);
printf("nr_cpus:%d\n\n", nr_cpus); printf("nr_cpus:%d\n\n", nr_cpus);
for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) { for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ? unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
PERCPU_FREE_TARGET : LOCAL_FREE_TARGET; PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) { for (t = 0; t < ARRAY_SIZE(map_types); t++) {
test_lru_sanity0(map_types[t], map_flags[f]); test_lru_sanity0(map_types[t], map_flags[f]);
test_lru_sanity1(map_types[t], map_flags[f], tgt_free); test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
test_lru_sanity2(map_types[t], map_flags[f], tgt_free); test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
......
...@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test, ...@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
BPF_MOV64_IMM(BPF_REG_0, rc), BPF_MOV64_IMM(BPF_REG_0, rc),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); return load_insns(test, insns, ARRAY_SIZE(insns));
} }
static int sendmsg_allow_prog_load(const struct sock_addr_test *test) static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
...@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test) ...@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); return load_insns(test, insns, ARRAY_SIZE(insns));
} }
static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test) static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
...@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test, ...@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn)); return load_insns(test, insns, ARRAY_SIZE(insns));
} }
static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test) static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)
......
...@@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file) ...@@ -1786,7 +1786,7 @@ static int populate_progs(char *bpf_file)
i++; i++;
} }
for (i = 0; i < sizeof(map_fd)/sizeof(int); i++) { for (i = 0; i < ARRAY_SIZE(map_fd); i++) {
maps[i] = bpf_object__find_map_by_name(obj, map_names[i]); maps[i] = bpf_object__find_map_by_name(obj, map_names[i]);
map_fd[i] = bpf_map__fd(maps[i]); map_fd[i] = bpf_map__fd(maps[i]);
if (map_fd[i] < 0) { if (map_fd[i] < 0) {
...@@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt) ...@@ -1867,7 +1867,7 @@ static int __test_selftests(int cg_fd, struct sockmap_options *opt)
} }
/* Tests basic commands and APIs */ /* Tests basic commands and APIs */
for (i = 0; i < sizeof(test)/sizeof(struct _test); i++) { for (i = 0; i < ARRAY_SIZE(test); i++) {
struct _test t = test[i]; struct _test t = test[i];
if (check_whitelist(&t, opt) != 0) if (check_whitelist(&t, opt) != 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment