Skip to contentSkip to conversation

likely/unlikely for bpf_helpers

PATCH Superseded Anton Protopopov 3 participants 7 messages Mar 31, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover likely/unlikely for bpf_helpers

Andrii suggested to send this piece with small fixes separately from the insn set rfc.

The first patch fixes a comment in <linux/bpf.h>, and the latter three patches add likely/unlikely macros to <bph/bpf_helpers.h>. The reason there are three patches and not one is to separate libbpf changes such that userspace libbpf can be updated more easily, and the order is such that each commit can be built.

Anton Protopopov (4):
bpf: fix a comment describing bpf_attr selftests/bpf: add guard macros around likely/unlikely
libbpf: add likely/unlikely macros selftests/bpf: remove likely/unlikely definitions

 include/uapi/linux/bpf.h                          | 2 +-
 tools/include/uapi/linux/bpf.h                    | 2 +-
 tools/lib/bpf/bpf_helpers.h                       | 8 ++++++++
 tools/testing/selftests/bpf/bpf_arena_spin_lock.h | 3 ---
 tools/testing/selftests/bpf/progs/iters.c         | 2 --
 5 files changed, 10 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 bpf: fix a comment describing bpf_attr

The map_fd field of the bpf_attr union is used in the BPF_MAP_FREEZE syscall. Explicitly mention this in the comments.

---
 include/uapi/linux/bpf.h       | 2 +-
 tools/include/uapi/linux/bpf.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +1 -1
index 661de2444965..1388db053d9e 100644
@@ -1506,7 +1506,7 @@ union bpf_attr {
 		__s32	map_token_fd;
 	};
 
-	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
+	struct { /* anonymous struct used by BPF_MAP_*_ELEM and BPF_MAP_FREEZE commands */
 		__u32		map_fd;
 		__aligned_u64	key;
 		union {
tools/include/uapi/linux/bpf.h +1 -1
index 661de2444965..1388db053d9e 100644
@@ -1506,7 +1506,7 @@ union bpf_attr {
 		__s32	map_token_fd;
 	};
 
-	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
+	struct { /* anonymous struct used by BPF_MAP_*_ELEM and BPF_MAP_FREEZE commands */
 		__u32		map_fd;
 		__aligned_u64	key;
 		union {
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/4 selftests/bpf: add guard macros around likely/unlikely

In reply to Anton Protopopov

Add guard macros around likely/unlikely definitions such that, if defined previously, the compilation doesn't break. (Those macros, actually, will be defined in libbpf in a consequent commit.)

---
 tools/testing/selftests/bpf/bpf_arena_spin_lock.h | 5 +++++
 tools/testing/selftests/bpf/progs/iters.c         | 2 ++
 2 files changed, 7 insertions(+)
tools/testing/selftests/bpf/bpf_arena_spin_lock.h +5 -0
index fb8dc0768999..d60d899dd9da 100644
@@ -95,8 +95,13 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL		(1U << _Q_PENDING_OFFSET)
 
+#ifndef likely
 #define likely(x) __builtin_expect(!!(x), 1)
+#endif
+
+#ifndef unlikely
 #define unlikely(x) __builtin_expect(!!(x), 0)
+#endif
 
 struct arena_qnode __arena qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
tools/testing/selftests/bpf/progs/iters.c +2 -0
index 427b72954b87..1b9a908f2607 100644
@@ -7,7 +7,9 @@
 #include "bpf_misc.h"
 #include "bpf_compiler.h"
 
+#ifndef unlikely
 #define unlikely(x)	__builtin_expect(!!(x), 0)
+#endif
 
 static volatile int zero = 0;
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/4 libbpf: add likely/unlikely macros

In reply to Anton Protopopov

A few selftests and, more importantly, a consequent changes to the bpf_helpers.h file use likely/unlikely macros. So define them here.

---
 tools/lib/bpf/bpf_helpers.h | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/lib/bpf/bpf_helpers.h +8 -0
index 686824b8b413..a50773d4616e 100644
@@ -15,6 +15,14 @@
 #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
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/4 selftests/bpf: remove likely/unlikely definitions

In reply to Anton Protopopov

Now likely/unlikely macros are defined in tools/lib/bpf/bpf_helpers.h and thus can be removed from selftests.

---
 tools/testing/selftests/bpf/bpf_arena_spin_lock.h | 8 --------
 tools/testing/selftests/bpf/progs/iters.c         | 4 ----
 2 files changed, 12 deletions(-)
tools/testing/selftests/bpf/bpf_arena_spin_lock.h +0 -8
index d60d899dd9da..4e29c31c4ef8 100644
@@ -95,14 +95,6 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL		(1U << _Q_PENDING_OFFSET)
 
-#ifndef likely
-#define likely(x) __builtin_expect(!!(x), 1)
-#endif
-
-#ifndef unlikely
-#define unlikely(x) __builtin_expect(!!(x), 0)
-#endif
-
 struct arena_qnode __arena qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
tools/testing/selftests/bpf/progs/iters.c +0 -4
index 1b9a908f2607..76adf4a8f2da 100644
@@ -7,10 +7,6 @@
 #include "bpf_misc.h"
 #include "bpf_compiler.h"
 
-#ifndef unlikely
-#define unlikely(x)	__builtin_expect(!!(x), 0)
-#endif
-
 static volatile int zero = 0;
 
 int my_pid;
Signature · 1 line
2.34.1
#5 lore raw
In reply to Anton Protopopov
Quoted 10 lines · Anton Protopopov

Andrii suggested to send this piece with small fixes separately from the insn set rfc.

The first patch fixes a comment in <linux/bpf.h>, and the latter three patches add likely/unlikely macros to <bph/bpf_helpers.h>. The reason there are three patches and not one is to separate libbpf changes such that userspace libbpf can be updated more easily, and the order is such that each commit can be built.

Quoted 5 lines

Anton Protopopov (4):
bpf: fix a comment describing bpf_attr selftests/bpf: add guard macros around likely/unlikely
libbpf: add likely/unlikely macros selftests/bpf: remove likely/unlikely definitions

let's just collapse the last 3 patches into one? libbpf sync process will be totally fine with that.

pw-bot: cr

Quoted 11 lines
 include/uapi/linux/bpf.h                          | 2 +-
 tools/include/uapi/linux/bpf.h                    | 2 +-
 tools/lib/bpf/bpf_helpers.h                       | 8 ++++++++
 tools/testing/selftests/bpf/bpf_arena_spin_lock.h | 3 ---
 tools/testing/selftests/bpf/progs/iters.c         | 2 --
 5 files changed, 10 insertions(+), 7 deletions(-)

--
2.34.1

#6 lore raw
Quoted 12 lines · Andrii Nakryiko
Quoted 10 lines · Anton Protopopov

Andrii suggested to send this piece with small fixes separately from the insn set rfc.

The first patch fixes a comment in <linux/bpf.h>, and the latter three patches add likely/unlikely macros to <bph/bpf_helpers.h>. The reason there are three patches and not one is to separate libbpf changes such that userspace libbpf can be updated more easily, and the order is such that each commit can be built.

Quoted 8 lines
Quoted 5 lines

Anton Protopopov (4):
bpf: fix a comment describing bpf_attr selftests/bpf: add guard macros around likely/unlikely
libbpf: add likely/unlikely macros selftests/bpf: remove likely/unlikely definitions

let's just collapse the last 3 patches into one? libbpf sync process will be totally fine with that.

Thanks, I've squashed them in v2.

Quoted 13 lines

pw-bot: cr

Quoted 11 lines
 include/uapi/linux/bpf.h                          | 2 +-
 tools/include/uapi/linux/bpf.h                    | 2 +-
 tools/lib/bpf/bpf_helpers.h                       | 8 ++++++++
 tools/testing/selftests/bpf/bpf_arena_spin_lock.h | 3 ---
 tools/testing/selftests/bpf/progs/iters.c         | 2 --
 5 files changed, 10 insertions(+), 7 deletions(-)

--
2.34.1