Skip to content

Commit 23457b3

Browse files
kknjhanakryiko
authored andcommitted
selftests: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE
The ARRAY_SIZE macro is more compact and more formal in linux source. Signed-off-by: Feng Yang <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6fee7a7 commit 23457b3

File tree

10 files changed

+20
-12
lines changed

10 files changed

+20
-12
lines changed

tools/testing/selftests/bpf/prog_tests/fexit_stress.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2019 Facebook */
33
#include <test_progs.h>
4+
#include "bpf_util.h"
45

56
void serial_test_fexit_stress(void)
67
{
@@ -36,7 +37,7 @@ void serial_test_fexit_stress(void)
3637
for (i = 0; i < bpf_max_tramp_links; i++) {
3738
fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
3839
trace_program,
39-
sizeof(trace_program) / sizeof(struct bpf_insn),
40+
ARRAY_SIZE(trace_program),
4041
&trace_opts);
4142
if (!ASSERT_GE(fexit_fd[i], 0, "fexit load"))
4243
goto out;

tools/testing/selftests/bpf/prog_tests/log_buf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <bpf/btf.h>
66

77
#include "test_log_buf.skel.h"
8+
#include "bpf_util.h"
89

910
static size_t libbpf_log_pos;
1011
static char libbpf_log_buf[1024 * 1024];
@@ -143,11 +144,11 @@ static void bpf_prog_load_log_buf(void)
143144
BPF_MOV64_IMM(BPF_REG_0, 0),
144145
BPF_EXIT_INSN(),
145146
};
146-
const size_t good_prog_insn_cnt = sizeof(good_prog_insns) / sizeof(struct bpf_insn);
147+
const size_t good_prog_insn_cnt = ARRAY_SIZE(good_prog_insns);
147148
const struct bpf_insn bad_prog_insns[] = {
148149
BPF_EXIT_INSN(),
149150
};
150-
size_t bad_prog_insn_cnt = sizeof(bad_prog_insns) / sizeof(struct bpf_insn);
151+
size_t bad_prog_insn_cnt = ARRAY_SIZE(bad_prog_insns);
151152
LIBBPF_OPTS(bpf_prog_load_opts, opts);
152153
const size_t log_buf_sz = 1024 * 1024;
153154
char *log_buf;

tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <bpf/btf.h>
55
#include "bpf/libbpf_internal.h"
66
#include "cgroup_helpers.h"
7+
#include "bpf_util.h"
78

89
static const char *module_name = "bpf_testmod";
910
static const char *symbol_name = "bpf_fentry_shadow_test";
@@ -100,7 +101,7 @@ void test_module_fentry_shadow(void)
100101
load_opts.attach_btf_obj_fd = btf_fd[i];
101102
prog_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
102103
trace_program,
103-
sizeof(trace_program) / sizeof(struct bpf_insn),
104+
ARRAY_SIZE(trace_program),
104105
&load_opts);
105106
if (!ASSERT_GE(prog_fd[i], 0, "bpf_prog_load"))
106107
goto out;

tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_nbd_invalid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <test_progs.h>
44
#include <linux/nbd.h>
5+
#include "bpf_util.h"
56

67
void test_raw_tp_writable_reject_nbd_invalid(void)
78
{
@@ -25,7 +26,7 @@ void test_raw_tp_writable_reject_nbd_invalid(void)
2526
);
2627

2728
bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
28-
program, sizeof(program) / sizeof(struct bpf_insn),
29+
program, ARRAY_SIZE(program),
2930
&opts);
3031
if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable load",
3132
"failed: %d errno %d\n", bpf_fd, errno))

tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_run.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <test_progs.h>
44
#include <linux/nbd.h>
5+
#include "bpf_util.h"
56

67
/* NOTE: conflict with other tests. */
78
void serial_test_raw_tp_writable_test_run(void)
@@ -24,7 +25,7 @@ void serial_test_raw_tp_writable_test_run(void)
2425
);
2526

2627
int bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
27-
trace_program, sizeof(trace_program) / sizeof(struct bpf_insn),
28+
trace_program, ARRAY_SIZE(trace_program),
2829
&trace_opts);
2930
if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded",
3031
"failed: %d errno %d\n", bpf_fd, errno))
@@ -41,7 +42,7 @@ void serial_test_raw_tp_writable_test_run(void)
4142
);
4243

4344
int filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL v2",
44-
skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
45+
skb_program, ARRAY_SIZE(skb_program),
4546
&skb_opts);
4647
if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
4748
filter_fd, errno))

tools/testing/selftests/bpf/prog_tests/tc_opts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,7 @@ static int generate_dummy_prog(void)
23842384
BPF_MOV64_IMM(BPF_REG_0, 0),
23852385
BPF_EXIT_INSN(),
23862386
};
2387-
const size_t prog_insn_cnt = sizeof(prog_insns) / sizeof(struct bpf_insn);
2387+
const size_t prog_insn_cnt = ARRAY_SIZE(prog_insns);
23882388
LIBBPF_OPTS(bpf_prog_load_opts, opts);
23892389
const size_t log_buf_sz = 256;
23902390
char log_buf[log_buf_sz];

tools/testing/selftests/bpf/prog_tests/unpriv_bpf_disabled.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "test_unpriv_bpf_disabled.skel.h"
88

99
#include "cap_helpers.h"
10+
#include "bpf_util.h"
1011

1112
/* Using CAP_LAST_CAP is risky here, since it can get pulled in from
1213
* an old /usr/include/linux/capability.h and be < CAP_BPF; as a result
@@ -146,7 +147,7 @@ static void test_unpriv_bpf_disabled_negative(struct test_unpriv_bpf_disabled *s
146147
BPF_MOV64_IMM(BPF_REG_0, 0),
147148
BPF_EXIT_INSN(),
148149
};
149-
const size_t prog_insn_cnt = sizeof(prog_insns) / sizeof(struct bpf_insn);
150+
const size_t prog_insn_cnt = ARRAY_SIZE(prog_insns);
150151
LIBBPF_OPTS(bpf_prog_load_opts, load_opts);
151152
struct bpf_map_info map_info = {};
152153
__u32 map_info_len = sizeof(map_info);

tools/testing/selftests/bpf/progs/syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/btf.h>
99
#include <string.h>
1010
#include <errno.h>
11+
#include "bpf_misc.h"
1112

1213
char _license[] SEC("license") = "GPL";
1314

@@ -119,7 +120,7 @@ int load_prog(struct args *ctx)
119120
static __u64 value = 34;
120121
static union bpf_attr prog_load_attr = {
121122
.prog_type = BPF_PROG_TYPE_XDP,
122-
.insn_cnt = sizeof(insns) / sizeof(insns[0]),
123+
.insn_cnt = ARRAY_SIZE(insns),
123124
};
124125
int ret;
125126

tools/testing/selftests/bpf/progs/test_rdonly_maps.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/ptrace.h>
55
#include <linux/bpf.h>
66
#include <bpf/bpf_helpers.h>
7+
#include "bpf_misc.h"
78

89
const struct {
910
unsigned a[4];
@@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
6465
{
6566
/* prevent compiler to optimize everything out */
6667
unsigned * volatile p = (void *)&rdonly_values.a;
67-
int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
68+
int i = ARRAY_SIZE(rdonly_values.a);
6869
unsigned iters = 0, sum = 0;
6970

7071
/* validate verifier can allow full loop as well */

tools/testing/selftests/bpf/progs/verifier_bits_iter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ int bits_memalloc(void)
8787
int *bit;
8888

8989
__builtin_memset(&data, 0xf0, sizeof(data)); /* 4 * 16 */
90-
bpf_for_each(bits, bit, &data[0], sizeof(data) / sizeof(u64))
90+
bpf_for_each(bits, bit, &data[0], ARRAY_SIZE(data))
9191
nr++;
9292
return nr;
9393
}

0 commit comments

Comments
 (0)