Skip to content

Commit

Permalink
selftests/bpf: Move ARRAY_SIZE to bpf_misc.h
Browse files Browse the repository at this point in the history
ARRAY_SIZE is used on multiple places, move its definition in
bpf_misc.h header.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Link: https://lore.kernel.org/bpf/20240626134719.3893748-1-jolsa@kernel.org
  • Loading branch information
Jiri Olsa authored and Andrii Nakryiko committed Jun 27, 2024
1 parent 0f31c2c commit a129787
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 40 deletions.
4 changes: 4 additions & 0 deletions tools/testing/selftests/bpf/progs/bpf_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@
/* make it look to compiler like value is read and written */
#define __sink(expr) asm volatile("" : "+g"(expr))

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

#endif
2 changes: 0 additions & 2 deletions tools/testing/selftests/bpf/progs/iters.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "bpf_misc.h"
#include "bpf_compiler.h"

#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))

static volatile int zero = 0;

int my_pid;
Expand Down
3 changes: 1 addition & 2 deletions tools/testing/selftests/bpf/progs/kprobe_multi_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <bpf/bpf_tracing.h>
#include <stdbool.h>
#include "bpf_kfuncs.h"

#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
#include "bpf_misc.h"

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

Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_experimental.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
#endif
#include "bpf_misc.h"

#include "linked_list.h"

Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/netif_receive_skb.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h"

#include <errno.h>

Expand All @@ -23,10 +24,6 @@ bool skip = false;
#define BADPTR 0
#endif

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/profiler.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "err.h"
#include "bpf_experimental.h"
#include "bpf_compiler.h"
#include "bpf_misc.h"

#ifndef NULL
#define NULL 0
Expand Down Expand Up @@ -133,10 +134,6 @@ struct {
__uint(max_entries, 16);
} disallowed_exec_inodes SEC(".maps");

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof(arr[0]))
#endif

static INLINE bool IS_ERR(const void* ptr)
{
return IS_ERR_VALUE((unsigned long)ptr);
Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/setget_sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
#include "bpf_misc.h"

extern unsigned long CONFIG_HZ __kconfig;

Expand Down
4 changes: 0 additions & 4 deletions tools/testing/selftests/bpf/progs/test_bpf_ma.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include "bpf_experimental.h"
#include "bpf_misc.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

struct generic_map_value {
void *data;
};
Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/test_sysctl_loop1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
#include <bpf/bpf_helpers.h>

#include "bpf_compiler.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
#include "bpf_misc.h"

/* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */
#define TCP_MEM_LOOPS 28 /* because 30 doesn't fit into 512 bytes of stack */
Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/test_sysctl_loop2.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
#include <bpf/bpf_helpers.h>

#include "bpf_compiler.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
#include "bpf_misc.h"

/* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */
#define TCP_MEM_LOOPS 20 /* because 30 doesn't fit into 512 bytes of stack */
Expand Down
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/test_sysctl_prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
#include <bpf/bpf_helpers.h>

#include "bpf_compiler.h"
#include "bpf_misc.h"

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

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

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

const char tcp_mem_name[] = "net/ipv4/tcp_mem";
static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "bpf_kfuncs.h"
#include "test_siphash.h"
#include "test_tcp_custom_syncookie.h"
#include "bpf_misc.h"

#define MAX_PACKET_OFF 0xffff

Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#define __packed __attribute__((__packed__))
#define __force

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

#define swap(a, b) \
do { \
typeof(a) __tmp = (a); \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "bpf_misc.h"
#include <../../../tools/include/linux/filter.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

int vals[] SEC(".data.vals") = {1, 2, 3, 4};

__naked __noinline __used
Expand Down

0 comments on commit a129787

Please sign in to comment.