Skip to content

Commit a129787

Browse files
olsajirianakryiko
authored andcommitted
selftests/bpf: Move ARRAY_SIZE to bpf_misc.h
ARRAY_SIZE is used on multiple places, move its definition in bpf_misc.h header. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Alan Maguire <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0f31c2c commit a129787

14 files changed

+13
-40
lines changed

tools/testing/selftests/bpf/progs/bpf_misc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,8 @@
140140
/* make it look to compiler like value is read and written */
141141
#define __sink(expr) asm volatile("" : "+g"(expr))
142142

143+
#ifndef ARRAY_SIZE
144+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
145+
#endif
146+
143147
#endif

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "bpf_misc.h"
88
#include "bpf_compiler.h"
99

10-
#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
11-
1210
static volatile int zero = 0;
1311

1412
int my_pid;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#include <bpf/bpf_tracing.h>
55
#include <stdbool.h>
66
#include "bpf_kfuncs.h"
7-
8-
#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
7+
#include "bpf_misc.h"
98

109
char _license[] SEC("license") = "GPL";
1110

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
#include <bpf/bpf_helpers.h>
55
#include <bpf/bpf_core_read.h>
66
#include "bpf_experimental.h"
7-
8-
#ifndef ARRAY_SIZE
9-
#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
10-
#endif
7+
#include "bpf_misc.h"
118

129
#include "linked_list.h"
1310

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <bpf/bpf_helpers.h>
66
#include <bpf/bpf_tracing.h>
77
#include <bpf/bpf_core_read.h>
8+
#include "bpf_misc.h"
89

910
#include <errno.h>
1011

@@ -23,10 +24,6 @@ bool skip = false;
2324
#define BADPTR 0
2425
#endif
2526

26-
#ifndef ARRAY_SIZE
27-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
28-
#endif
29-
3027
struct {
3128
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
3229
__uint(max_entries, 1);

tools/testing/selftests/bpf/progs/profiler.inc.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "err.h"
1010
#include "bpf_experimental.h"
1111
#include "bpf_compiler.h"
12+
#include "bpf_misc.h"
1213

1314
#ifndef NULL
1415
#define NULL 0
@@ -133,10 +134,6 @@ struct {
133134
__uint(max_entries, 16);
134135
} disallowed_exec_inodes SEC(".maps");
135136

136-
#ifndef ARRAY_SIZE
137-
#define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof(arr[0]))
138-
#endif
139-
140137
static INLINE bool IS_ERR(const void* ptr)
141138
{
142139
return IS_ERR_VALUE((unsigned long)ptr);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
#include <bpf/bpf_core_read.h>
77
#include <bpf/bpf_helpers.h>
88
#include <bpf/bpf_tracing.h>
9-
10-
#ifndef ARRAY_SIZE
11-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
12-
#endif
9+
#include "bpf_misc.h"
1310

1411
extern unsigned long CONFIG_HZ __kconfig;
1512

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "bpf_experimental.h"
88
#include "bpf_misc.h"
99

10-
#ifndef ARRAY_SIZE
11-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
12-
#endif
13-
1410
struct generic_map_value {
1511
void *data;
1612
};

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#include <bpf/bpf_helpers.h>
1111

1212
#include "bpf_compiler.h"
13-
14-
#ifndef ARRAY_SIZE
15-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
16-
#endif
13+
#include "bpf_misc.h"
1714

1815
/* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */
1916
#define TCP_MEM_LOOPS 28 /* because 30 doesn't fit into 512 bytes of stack */

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#include <bpf/bpf_helpers.h>
1111

1212
#include "bpf_compiler.h"
13-
14-
#ifndef ARRAY_SIZE
15-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
16-
#endif
13+
#include "bpf_misc.h"
1714

1815
/* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */
1916
#define TCP_MEM_LOOPS 20 /* because 30 doesn't fit into 512 bytes of stack */

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010
#include <bpf/bpf_helpers.h>
1111

1212
#include "bpf_compiler.h"
13+
#include "bpf_misc.h"
1314

1415
/* Max supported length of a string with unsigned long in base 10 (pow2 - 1). */
1516
#define MAX_ULONG_STR_LEN 0xF
1617

1718
/* Max supported length of sysctl value string (pow2). */
1819
#define MAX_VALUE_STR_LEN 0x40
1920

20-
#ifndef ARRAY_SIZE
21-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22-
#endif
23-
2421
const char tcp_mem_name[] = "net/ipv4/tcp_mem";
2522
static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx)
2623
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "bpf_kfuncs.h"
1010
#include "test_siphash.h"
1111
#include "test_tcp_custom_syncookie.h"
12+
#include "bpf_misc.h"
1213

1314
#define MAX_PACKET_OFF 0xffff
1415

tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#define __packed __attribute__((__packed__))
88
#define __force
99

10-
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
11-
1210
#define swap(a, b) \
1311
do { \
1412
typeof(a) __tmp = (a); \

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "bpf_misc.h"
99
#include <../../../tools/include/linux/filter.h>
1010

11-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
12-
1311
int vals[] SEC(".data.vals") = {1, 2, 3, 4};
1412

1513
__naked __noinline __used

0 commit comments

Comments
 (0)