libbpf: Add likely/unlikely macros and use them in selftests
authorAnton Protopopov <a.s.protopopov@gmail.com>
Mon, 31 Mar 2025 20:36:18 +0000 (20:36 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 4 Apr 2025 15:53:24 +0000 (08:53 -0700)
A few selftests and, more importantly, consequent changes to the
bpf_helpers.h file, use likely/unlikely macros, so define them here
and remove duplicate definitions from existing selftests.

Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250331203618.1973691-3-a.s.protopopov@gmail.com
tools/lib/bpf/bpf_helpers.h
tools/testing/selftests/bpf/bpf_arena_spin_lock.h
tools/testing/selftests/bpf/progs/iters.c

index 686824b8b413bf7e5819b5e7647f9ac9c29c4ca2..a50773d4616e6ce7d10e4468b042407d84cf9137 100644 (file)
 #define __array(name, val) typeof(val) *name[]
 #define __ulong(name, val) enum { ___bpf_concat(__unique_value, __COUNTER__) = val } name
 
+#ifndef likely
+#define likely(x)      (__builtin_expect(!!(x), 1))
+#endif
+
+#ifndef unlikely
+#define unlikely(x)    (__builtin_expect(!!(x), 0))
+#endif
+
 /*
  * Helper macro to place programs, maps, license in
  * different sections in elf_bpf file. Section names
index fb8dc07689998fab2e18814d38afd63ecc88a4db..4e29c31c4ef80bf2920bb7f9b3426a092279cb82 100644 (file)
@@ -95,9 +95,6 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL          (1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL         (1U << _Q_PENDING_OFFSET)
 
-#define likely(x) __builtin_expect(!!(x), 1)
-#define unlikely(x) __builtin_expect(!!(x), 0)
-
 struct arena_qnode __arena qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
index 427b72954b87e0ffe0c9aa4508008163c7643e0c..76adf4a8f2dad75e8900defc204e68b1e316a009 100644 (file)
@@ -7,8 +7,6 @@
 #include "bpf_misc.h"
 #include "bpf_compiler.h"
 
-#define unlikely(x)    __builtin_expect(!!(x), 0)
-
 static volatile int zero = 0;
 
 int my_pid;