Skip to content

Commit 3652c9a

Browse files
committed
bpf, libbpf: Guard bpf inline asm from bpf_tail_call_static
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: 0e9f684 ("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs") Reported-by: Yaniv Agman <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Yonghong Song <[email protected]> Tested-by: Yaniv Agman <[email protected]> Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com Link: https://lore.kernel.org/bpf/[email protected]
1 parent adfd272 commit 3652c9a

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

samples/bpf/sockex3_kern.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ static inline void parse_eth_proto(struct __sk_buff *skb, u32 proto)
4444
switch (proto) {
4545
case ETH_P_8021Q:
4646
case ETH_P_8021AD:
47-
bpf_tail_call_static(skb, &jmp_table, PARSE_VLAN);
47+
bpf_tail_call(skb, &jmp_table, PARSE_VLAN);
4848
break;
4949
case ETH_P_MPLS_UC:
5050
case ETH_P_MPLS_MC:
51-
bpf_tail_call_static(skb, &jmp_table, PARSE_MPLS);
51+
bpf_tail_call(skb, &jmp_table, PARSE_MPLS);
5252
break;
5353
case ETH_P_IP:
54-
bpf_tail_call_static(skb, &jmp_table, PARSE_IP);
54+
bpf_tail_call(skb, &jmp_table, PARSE_IP);
5555
break;
5656
case ETH_P_IPV6:
57-
bpf_tail_call_static(skb, &jmp_table, PARSE_IPV6);
57+
bpf_tail_call(skb, &jmp_table, PARSE_IPV6);
5858
break;
5959
}
6060
}

tools/lib/bpf/bpf_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
/*
7373
* Helper function to perform a tail call with a constant/immediate map slot.
7474
*/
75+
#if __clang_major__ >= 8 && defined(__bpf__)
7576
static __always_inline void
7677
bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
7778
{
@@ -98,6 +99,7 @@ bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
9899
:: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
99100
: "r0", "r1", "r2", "r3", "r4", "r5");
100101
}
102+
#endif
101103

102104
/*
103105
* Helper structure used by eBPF C program

0 commit comments

Comments
 (0)