Skip to contentSkip to conversation

likely/unlikely for bpf_helpers and a small comment fix

PATCH Applied Anton Protopopov 2 participants 4 messages Mar 31, 2025 – Apr 4, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover likely/unlikely for bpf_helpers and a small comment fix

These two commits fix a comment describing bpf_attr in <linux/bpf.h> and add likely/unlikely macros to <bph/bpf_helpers.h> to be consumed by selftests and, later, by the static_branch_likely/unlikely macros.

v1 -> v2:
* squash libbpf and selftests fixes into one patch (Andrii)

Anton Protopopov (2):
bpf: fix a comment describing bpf_attr
libbpf: add likely/unlikely macros and use them in selftests

 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 v2 1/2 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 v2 2/2 libbpf: add likely/unlikely macros and use them in selftests

In reply to Anton Protopopov

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.

---
 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 --
 3 files changed, 8 insertions(+), 5 deletions(-)
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
tools/testing/selftests/bpf/bpf_arena_spin_lock.h +0 -3
index fb8dc0768999..4e29c31c4ef8 100644
@@ -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)
tools/testing/selftests/bpf/progs/iters.c +0 -2
index 427b72954b87..76adf4a8f2da 100644
@@ -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;
Signature · 1 line
2.34.1
#3 lore raw
In reply to Anton Protopopov
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Mon, 31 Mar 2025 20:36:16 +0000 you wrote:

These two commits fix a comment describing bpf_attr in <linux/bpf.h> and add likely/unlikely macros to <bph/bpf_helpers.h> to be consumed by selftests and, later, by the static_branch_likely/unlikely macros.

v1 -> v2:
* squash libbpf and selftests fixes into one patch (Andrii)

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf: fix a comment describing bpf_attr

    https://git.kernel.org/bpf/bpf-next/c/62aa5790cec8
  - [bpf-next,2/2] libbpf: add likely/unlikely macros and use them in selftests
    https://git.kernel.org/bpf/bpf-next/c/dafae1ae2ad3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html