Skip to content

Commit dbd7eb1

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf, selftests: Replicate tailcall limit test for indirect call case
The tailcall_3 test program uses bpf_tail_call_static() where the JIT would patch a direct jump. Add a new tailcall_6 test program replicating exactly the same test just ensuring that bpf_tail_call() uses a map index where the verifier cannot make assumptions this time. In other words, this will now cover both on x86-64 JIT, meaning, JIT images with emit_bpf_tail_call_direct() emission as well as JIT images with emit_bpf_tail_call_indirect() emission. # echo 1 > /proc/sys/net/core/bpf_jit_enable # ./test_progs -t tailcalls #136/1 tailcalls/tailcall_1:OK #136/2 tailcalls/tailcall_2:OK #136/3 tailcalls/tailcall_3:OK #136/4 tailcalls/tailcall_4:OK #136/5 tailcalls/tailcall_5:OK #136/6 tailcalls/tailcall_6:OK #136/7 tailcalls/tailcall_bpf2bpf_1:OK #136/8 tailcalls/tailcall_bpf2bpf_2:OK #136/9 tailcalls/tailcall_bpf2bpf_3:OK #136/10 tailcalls/tailcall_bpf2bpf_4:OK #136/11 tailcalls/tailcall_bpf2bpf_5:OK #136 tailcalls:OK Summary: 1/11 PASSED, 0 SKIPPED, 0 FAILED # echo 0 > /proc/sys/net/core/bpf_jit_enable # ./test_progs -t tailcalls #136/1 tailcalls/tailcall_1:OK #136/2 tailcalls/tailcall_2:OK #136/3 tailcalls/tailcall_3:OK #136/4 tailcalls/tailcall_4:OK #136/5 tailcalls/tailcall_5:OK #136/6 tailcalls/tailcall_6:OK [...] For interpreter, the tailcall_1-6 tests are passing as well. The later tailcall_bpf2bpf_* are failing due lack of bpf2bpf + tailcall support in interpreter, so this is expected. Also, manual inspection shows that both loaded programs from tailcall_3 and tailcall_6 test case emit the expected opcodes: * tailcall_3 disasm, emit_bpf_tail_call_direct(): [...] b: push %rax c: push %rbx d: push %r13 f: mov %rdi,%rbx 12: movabs $0xffff8d3f5afb0200,%r13 1c: mov %rbx,%rdi 1f: mov %r13,%rsi 22: xor %edx,%edx _ 24: mov -0x4(%rbp),%eax | limit check 2a: cmp $0x20,%eax | 2d: ja 0x0000000000000046 | 2f: add $0x1,%eax | 32: mov %eax,-0x4(%rbp) |_ 38: nopl 0x0(%rax,%rax,1) 3d: pop %r13 3f: pop %rbx 40: pop %rax 41: jmpq 0xffffffffffffe377 [...] * tailcall_6 disasm, emit_bpf_tail_call_indirect(): [...] 47: movabs $0xffff8d3f59143a00,%rsi 51: mov %edx,%edx 53: cmp %edx,0x24(%rsi) 56: jbe 0x0000000000000093 _ 58: mov -0x4(%rbp),%eax | limit check 5e: cmp $0x20,%eax | 61: ja 0x0000000000000093 | 63: add $0x1,%eax | 66: mov %eax,-0x4(%rbp) |_ 6c: mov 0x110(%rsi,%rdx,8),%rcx 74: test %rcx,%rcx 77: je 0x0000000000000093 79: pop %rax 7a: mov 0x30(%rcx),%rcx 7e: add $0xb,%rcx 82: callq 0x000000000000008e 87: pause 89: lfence 8c: jmp 0x0000000000000087 8e: mov %rcx,(%rsp) 92: retq [...] Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Tested-by: Tiezhu Yang <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Johan Almbladh <[email protected]> Acked-by: Paul Chaignon <[email protected]> Link: https://lore.kernel.org/bpf/CAM1=_QRyRVCODcXo_Y6qOm1iT163HoiSj8U2pZ8Rj3hzMTT=HQ@mail.gmail.com Link: https://lore.kernel.org/bpf/[email protected]
1 parent 14bef1a commit dbd7eb1

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

tools/testing/selftests/bpf/prog_tests/tailcalls.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ static void test_tailcall_2(void)
219219
bpf_object__close(obj);
220220
}
221221

222-
/* test_tailcall_3 checks that the count value of the tail call limit
223-
* enforcement matches with expectations.
224-
*/
225-
static void test_tailcall_3(void)
222+
static void test_tailcall_count(const char *which)
226223
{
227224
int err, map_fd, prog_fd, main_fd, data_fd, i, val;
228225
struct bpf_map *prog_array, *data_map;
@@ -231,7 +228,7 @@ static void test_tailcall_3(void)
231228
__u32 retval, duration;
232229
char buff[128] = {};
233230

234-
err = bpf_prog_load("tailcall3.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
231+
err = bpf_prog_load(which, BPF_PROG_TYPE_SCHED_CLS, &obj,
235232
&prog_fd);
236233
if (CHECK_FAIL(err))
237234
return;
@@ -296,6 +293,22 @@ static void test_tailcall_3(void)
296293
bpf_object__close(obj);
297294
}
298295

296+
/* test_tailcall_3 checks that the count value of the tail call limit
297+
* enforcement matches with expectations. JIT uses direct jump.
298+
*/
299+
static void test_tailcall_3(void)
300+
{
301+
test_tailcall_count("tailcall3.o");
302+
}
303+
304+
/* test_tailcall_6 checks that the count value of the tail call limit
305+
* enforcement matches with expectations. JIT uses indirect jump.
306+
*/
307+
static void test_tailcall_6(void)
308+
{
309+
test_tailcall_count("tailcall6.o");
310+
}
311+
299312
/* test_tailcall_4 checks that the kernel properly selects indirect jump
300313
* for the case where the key is not known. Latter is passed via global
301314
* data to select different targets we can compare return value of.
@@ -822,6 +835,8 @@ void test_tailcalls(void)
822835
test_tailcall_4();
823836
if (test__start_subtest("tailcall_5"))
824837
test_tailcall_5();
838+
if (test__start_subtest("tailcall_6"))
839+
test_tailcall_6();
825840
if (test__start_subtest("tailcall_bpf2bpf_1"))
826841
test_tailcall_bpf2bpf_1();
827842
if (test__start_subtest("tailcall_bpf2bpf_2"))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
4+
#include <bpf/bpf_helpers.h>
5+
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
8+
__uint(max_entries, 1);
9+
__uint(key_size, sizeof(__u32));
10+
__uint(value_size, sizeof(__u32));
11+
} jmp_table SEC(".maps");
12+
13+
int count, which;
14+
15+
SEC("classifier/0")
16+
int bpf_func_0(struct __sk_buff *skb)
17+
{
18+
count++;
19+
if (__builtin_constant_p(which))
20+
__bpf_unreachable();
21+
bpf_tail_call(skb, &jmp_table, which);
22+
return 1;
23+
}
24+
25+
SEC("classifier")
26+
int entry(struct __sk_buff *skb)
27+
{
28+
if (__builtin_constant_p(which))
29+
__bpf_unreachable();
30+
bpf_tail_call(skb, &jmp_table, which);
31+
return 0;
32+
}
33+
34+
char __license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)