Commit 1fb5ba29 authored by John Fastabend's avatar John Fastabend Committed by Daniel Borkmann

bpf: Selftest to verify mixing bpf2bpf calls and tailcalls with insn patch

This adds some extra noise to the tailcall_bpf2bpf4 tests that will cause
verify to patch insns. This then moves around subprog start/end insn
index and poke descriptor insn index to ensure that verify and JIT will
continue to track these correctly.

If done correctly verifier should pass this program same as before and
JIT should emit tail call logic.
Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210707223848.14580-3-john.fastabend@gmail.com
parent f263a814
...@@ -715,6 +715,8 @@ static void test_tailcall_bpf2bpf_3(void) ...@@ -715,6 +715,8 @@ static void test_tailcall_bpf2bpf_3(void)
bpf_object__close(obj); bpf_object__close(obj);
} }
#include "tailcall_bpf2bpf4.skel.h"
/* test_tailcall_bpf2bpf_4 checks that tailcall counter is correctly preserved /* test_tailcall_bpf2bpf_4 checks that tailcall counter is correctly preserved
* across tailcalls combined with bpf2bpf calls. for making sure that tailcall * across tailcalls combined with bpf2bpf calls. for making sure that tailcall
* counter behaves correctly, bpf program will go through following flow: * counter behaves correctly, bpf program will go through following flow:
...@@ -727,10 +729,15 @@ static void test_tailcall_bpf2bpf_3(void) ...@@ -727,10 +729,15 @@ static void test_tailcall_bpf2bpf_3(void)
* the loop begins. At the end of the test make sure that the global counter is * the loop begins. At the end of the test make sure that the global counter is
* equal to 31, because tailcall counter includes the first two tailcalls * equal to 31, because tailcall counter includes the first two tailcalls
* whereas global counter is incremented only on loop presented on flow above. * whereas global counter is incremented only on loop presented on flow above.
*
* The noise parameter is used to insert bpf_map_update calls into the logic
* to force verifier to patch instructions. This allows us to ensure jump
* logic remains correct with instruction movement.
*/ */
static void test_tailcall_bpf2bpf_4(void) static void test_tailcall_bpf2bpf_4(bool noise)
{ {
int err, map_fd, prog_fd, main_fd, data_fd, i, val; int err, map_fd, prog_fd, main_fd, data_fd, i;
struct tailcall_bpf2bpf4__bss val;
struct bpf_map *prog_array, *data_map; struct bpf_map *prog_array, *data_map;
struct bpf_program *prog; struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
...@@ -774,11 +781,6 @@ static void test_tailcall_bpf2bpf_4(void) ...@@ -774,11 +781,6 @@ static void test_tailcall_bpf2bpf_4(void)
goto out; goto out;
} }
err = bpf_prog_test_run(main_fd, 1, &pkt_v4, sizeof(pkt_v4), 0,
&duration, &retval, NULL);
CHECK(err || retval != sizeof(pkt_v4) * 3, "tailcall", "err %d errno %d retval %d\n",
err, errno, retval);
data_map = bpf_object__find_map_by_name(obj, "tailcall.bss"); data_map = bpf_object__find_map_by_name(obj, "tailcall.bss");
if (CHECK_FAIL(!data_map || !bpf_map__is_internal(data_map))) if (CHECK_FAIL(!data_map || !bpf_map__is_internal(data_map)))
return; return;
...@@ -787,10 +789,22 @@ static void test_tailcall_bpf2bpf_4(void) ...@@ -787,10 +789,22 @@ static void test_tailcall_bpf2bpf_4(void)
if (CHECK_FAIL(map_fd < 0)) if (CHECK_FAIL(map_fd < 0))
return; return;
i = 0;
val.noise = noise;
val.count = 0;
err = bpf_map_update_elem(data_fd, &i, &val, BPF_ANY);
if (CHECK_FAIL(err))
goto out;
err = bpf_prog_test_run(main_fd, 1, &pkt_v4, sizeof(pkt_v4), 0,
&duration, &retval, NULL);
CHECK(err || retval != sizeof(pkt_v4) * 3, "tailcall", "err %d errno %d retval %d\n",
err, errno, retval);
i = 0; i = 0;
err = bpf_map_lookup_elem(data_fd, &i, &val); err = bpf_map_lookup_elem(data_fd, &i, &val);
CHECK(err || val != 31, "tailcall count", "err %d errno %d count %d\n", CHECK(err || val.count != 31, "tailcall count", "err %d errno %d count %d\n",
err, errno, val); err, errno, val.count);
out: out:
bpf_object__close(obj); bpf_object__close(obj);
...@@ -815,5 +829,7 @@ void test_tailcalls(void) ...@@ -815,5 +829,7 @@ void test_tailcalls(void)
if (test__start_subtest("tailcall_bpf2bpf_3")) if (test__start_subtest("tailcall_bpf2bpf_3"))
test_tailcall_bpf2bpf_3(); test_tailcall_bpf2bpf_3();
if (test__start_subtest("tailcall_bpf2bpf_4")) if (test__start_subtest("tailcall_bpf2bpf_4"))
test_tailcall_bpf2bpf_4(); test_tailcall_bpf2bpf_4(false);
if (test__start_subtest("tailcall_bpf2bpf_5"))
test_tailcall_bpf2bpf_4(true);
} }
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
#include <linux/bpf.h> #include <linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u32));
} nop_table SEC(".maps");
struct { struct {
__uint(type, BPF_MAP_TYPE_PROG_ARRAY); __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
__uint(max_entries, 3); __uint(max_entries, 3);
...@@ -10,10 +17,21 @@ struct { ...@@ -10,10 +17,21 @@ struct {
} jmp_table SEC(".maps"); } jmp_table SEC(".maps");
int count = 0; int count = 0;
int noise = 0;
__always_inline int subprog_noise(void)
{
__u32 key = 0;
bpf_map_lookup_elem(&nop_table, &key);
return 0;
}
__noinline __noinline
int subprog_tail_2(struct __sk_buff *skb) int subprog_tail_2(struct __sk_buff *skb)
{ {
if (noise)
subprog_noise();
bpf_tail_call_static(skb, &jmp_table, 2); bpf_tail_call_static(skb, &jmp_table, 2);
return skb->len * 3; return skb->len * 3;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment