mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-05 10:04:12 +08:00
3652c9a1b1
Yaniv reported a compilation error after pulling latest libbpf:
[...]
../libbpf/src/root/usr/include/bpf/bpf_helpers.h:99:10: error:
unknown register name 'r0' in asm
: "r0", "r1", "r2", "r3", "r4", "r5");
[...]
The issue got triggered given Yaniv was compiling tracing programs with native
target (e.g. x86) instead of BPF target, hence no BTF generated vmlinux.h nor
CO-RE used, and later llc with -march=bpf was invoked to compile from LLVM IR
to BPF object file. Given that clang was expecting x86 inline asm and not BPF
one the error complained that these regs don't exist on the former.
Guard bpf_tail_call_static() with defined(__bpf__) where BPF inline asm is valid
to use. BPF tracing programs on more modern kernels use BPF target anyway and
thus the bpf_tail_call_static() function will be available for them. BPF inline
asm is supported since clang 7 (clang <= 6 otherwise throws same above error),
and __bpf_unreachable() since clang 8, therefore include the latter condition
in order to prevent compilation errors for older clang versions. Given even an
old Ubuntu 18.04 LTS has official LLVM packages all the way up to llvm-10, I did
not bother to special case the __bpf_unreachable() inside bpf_tail_call_static()
further.
Also, undo the sockex3_kern's use of bpf_tail_call_static() sample given they
still have the old hacky way to even compile networking progs with native instead
of BPF target so bpf_tail_call_static() won't be defined there anymore.
Fixes: 0e9f6841f6
("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs")
Reported-by: Yaniv Agman <yanivagman@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Tested-by: Yaniv Agman <yanivagman@gmail.com>
Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com
Link: https://lore.kernel.org/bpf/20201021203257.26223-1-daniel@iogearbox.net
132 lines
3.7 KiB
C
132 lines
3.7 KiB
C
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
|
#ifndef __BPF_HELPERS__
|
|
#define __BPF_HELPERS__
|
|
|
|
/*
|
|
* Note that bpf programs need to include either
|
|
* vmlinux.h (auto-generated from BTF) or linux/types.h
|
|
* in advance since bpf_helper_defs.h uses such types
|
|
* as __u64.
|
|
*/
|
|
#include "bpf_helper_defs.h"
|
|
|
|
#define __uint(name, val) int (*name)[val]
|
|
#define __type(name, val) typeof(val) *name
|
|
#define __array(name, val) typeof(val) *name[]
|
|
|
|
/* Helper macro to print out debug messages */
|
|
#define bpf_printk(fmt, ...) \
|
|
({ \
|
|
char ____fmt[] = fmt; \
|
|
bpf_trace_printk(____fmt, sizeof(____fmt), \
|
|
##__VA_ARGS__); \
|
|
})
|
|
|
|
/*
|
|
* Helper macro to place programs, maps, license in
|
|
* different sections in elf_bpf file. Section names
|
|
* are interpreted by elf_bpf loader
|
|
*/
|
|
#define SEC(NAME) __attribute__((section(NAME), used))
|
|
|
|
#ifndef __always_inline
|
|
#define __always_inline __attribute__((always_inline))
|
|
#endif
|
|
#ifndef __noinline
|
|
#define __noinline __attribute__((noinline))
|
|
#endif
|
|
#ifndef __weak
|
|
#define __weak __attribute__((weak))
|
|
#endif
|
|
|
|
/*
|
|
* Helper macro to manipulate data structures
|
|
*/
|
|
#ifndef offsetof
|
|
#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER)
|
|
#endif
|
|
#ifndef container_of
|
|
#define container_of(ptr, type, member) \
|
|
({ \
|
|
void *__mptr = (void *)(ptr); \
|
|
((type *)(__mptr - offsetof(type, member))); \
|
|
})
|
|
#endif
|
|
|
|
/*
|
|
* Helper macro to throw a compilation error if __bpf_unreachable() gets
|
|
* built into the resulting code. This works given BPF back end does not
|
|
* implement __builtin_trap(). This is useful to assert that certain paths
|
|
* of the program code are never used and hence eliminated by the compiler.
|
|
*
|
|
* For example, consider a switch statement that covers known cases used by
|
|
* the program. __bpf_unreachable() can then reside in the default case. If
|
|
* the program gets extended such that a case is not covered in the switch
|
|
* statement, then it will throw a build error due to the default case not
|
|
* being compiled out.
|
|
*/
|
|
#ifndef __bpf_unreachable
|
|
# define __bpf_unreachable() __builtin_trap()
|
|
#endif
|
|
|
|
/*
|
|
* Helper function to perform a tail call with a constant/immediate map slot.
|
|
*/
|
|
#if __clang_major__ >= 8 && defined(__bpf__)
|
|
static __always_inline void
|
|
bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
|
|
{
|
|
if (!__builtin_constant_p(slot))
|
|
__bpf_unreachable();
|
|
|
|
/*
|
|
* Provide a hard guarantee that LLVM won't optimize setting r2 (map
|
|
* pointer) and r3 (constant map index) from _different paths_ ending
|
|
* up at the _same_ call insn as otherwise we won't be able to use the
|
|
* jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
|
|
* given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
|
|
* tracking for prog array pokes") for details on verifier tracking.
|
|
*
|
|
* Note on clobber list: we need to stay in-line with BPF calling
|
|
* convention, so even if we don't end up using r0, r4, r5, we need
|
|
* to mark them as clobber so that LLVM doesn't end up using them
|
|
* before / after the call.
|
|
*/
|
|
asm volatile("r1 = %[ctx]\n\t"
|
|
"r2 = %[map]\n\t"
|
|
"r3 = %[slot]\n\t"
|
|
"call 12"
|
|
:: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
|
|
: "r0", "r1", "r2", "r3", "r4", "r5");
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* Helper structure used by eBPF C program
|
|
* to describe BPF map attributes to libbpf loader
|
|
*/
|
|
struct bpf_map_def {
|
|
unsigned int type;
|
|
unsigned int key_size;
|
|
unsigned int value_size;
|
|
unsigned int max_entries;
|
|
unsigned int map_flags;
|
|
};
|
|
|
|
enum libbpf_pin_type {
|
|
LIBBPF_PIN_NONE,
|
|
/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
|
|
LIBBPF_PIN_BY_NAME,
|
|
};
|
|
|
|
enum libbpf_tristate {
|
|
TRI_NO = 0,
|
|
TRI_YES = 1,
|
|
TRI_MODULE = 2,
|
|
};
|
|
|
|
#define __kconfig __attribute__((section(".kconfig")))
|
|
#define __ksym __attribute__((section(".ksyms")))
|
|
|
|
#endif
|