Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Provide barrier() and barrier_var() in bpf_helpers.h

Add barrier() and barrier_var() macros into bpf_helpers.h to be used by
end users. While a bit advanced and specialized instruments, they are
sometimes indispensable. Instead of requiring each user to figure out
exact asm volatile incantations for themselves, provide them from
bpf_helpers.h.

Also remove conflicting definitions from selftests. Some tests rely on
barrier_var() definition being nothing, those will still work as libbpf
does the #ifndef/#endif guarding for barrier() and barrier_var(),
allowing users to redefine them, if necessary.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220509004148.1801791-8-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
f760d053 785c3342

+24 -8
+24
tools/lib/bpf/bpf_helpers.h
··· 76 76 #endif 77 77 78 78 /* 79 + * Compiler (optimization) barrier. 80 + */ 81 + #ifndef barrier 82 + #define barrier() asm volatile("" ::: "memory") 83 + #endif 84 + 85 + /* Variable-specific compiler (optimization) barrier. It's a no-op which makes 86 + * compiler believe that there is some black box modification of a given 87 + * variable and thus prevents compiler from making extra assumption about its 88 + * value and potential simplifications and optimizations on this variable. 89 + * 90 + * E.g., compiler might often delay or even omit 32-bit to 64-bit casting of 91 + * a variable, making some code patterns unverifiable. Putting barrier_var() 92 + * in place will ensure that cast is performed before the barrier_var() 93 + * invocation, because compiler has to pessimistically assume that embedded 94 + * asm section might perform some extra operations on that variable. 95 + * 96 + * This is a variable-specific variant of more global barrier(). 97 + */ 98 + #ifndef barrier_var 99 + #define barrier_var(var) asm volatile("" : "=r"(var) : "0"(var)) 100 + #endif 101 + 102 + /* 79 103 * Helper macro to throw a compilation error if __bpf_unreachable() gets 80 104 * built into the resulting code. This works given BPF back end does not 81 105 * implement __builtin_trap(). This is useful to assert that certain paths
-2
tools/testing/selftests/bpf/progs/exhandler_kern.c
··· 7 7 #include <bpf/bpf_tracing.h> 8 8 #include <bpf/bpf_core_read.h> 9 9 10 - #define barrier_var(var) asm volatile("" : "=r"(var) : "0"(var)) 11 - 12 10 char _license[] SEC("license") = "GPL"; 13 11 14 12 unsigned int exception_triggered;
-1
tools/testing/selftests/bpf/progs/loop5.c
··· 2 2 // Copyright (c) 2019 Facebook 3 3 #include <linux/bpf.h> 4 4 #include <bpf/bpf_helpers.h> 5 - #define barrier() __asm__ __volatile__("": : :"memory") 6 5 7 6 char _license[] SEC("license") = "GPL"; 8 7
-1
tools/testing/selftests/bpf/progs/profiler1.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 - #define barrier_var(var) asm volatile("" : "=r"(var) : "0"(var)) 4 3 #define UNROLL 5 4 #define INLINE __always_inline 6 5 #include "profiler.inc.h"
-2
tools/testing/selftests/bpf/progs/pyperf.h
··· 171 171 bool done; 172 172 }; 173 173 174 - #define barrier_var(var) asm volatile("" : "=r"(var) : "0"(var)) 175 - 176 174 static int process_frame_callback(__u32 i, struct process_frame_ctx *ctx) 177 175 { 178 176 int zero = 0;
-2
tools/testing/selftests/bpf/progs/test_pkt_access.c
··· 14 14 #include <bpf/bpf_helpers.h> 15 15 #include <bpf/bpf_endian.h> 16 16 17 - #define barrier() __asm__ __volatile__("": : :"memory") 18 - 19 17 /* llvm will optimize both subprograms into exactly the same BPF assembly 20 18 * 21 19 * Disassembly of section .text: