Message ID | 20220615151721.404596-3-jakub@cloudflare.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | BPF |
Headers | show |
Series | Fix tail call counting with bpf2bpf | expand |
On 6/15/22 5:17 PM, Jakub Sitnicki wrote: > Cover the case when tail call count needs to be passed from BPF function to > BPF function, and the caller has data on stack. Specifically when the size > of data allocated on BPF stack is not a multiple on 8. > > Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> > --- > .../selftests/bpf/prog_tests/tailcalls.c | 55 +++++++++++++++++++ > .../selftests/bpf/progs/tailcall_bpf2bpf6.c | 42 ++++++++++++++ > 2 files changed, 97 insertions(+) > create mode 100644 tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c > index c4da87ec3ba4..19c70880cfb3 100644 > --- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c > +++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c > @@ -831,6 +831,59 @@ static void test_tailcall_bpf2bpf_4(bool noise) > bpf_object__close(obj); > } > > +#include "tailcall_bpf2bpf6.skel.h" > + > +/* Tail call counting works even when there is data on stack which is > + * not aligned to 8 bytes. > + */ > +static void test_tailcall_bpf2bpf_6(void) > +{ > + struct tailcall_bpf2bpf6 *obj; > + int err, map_fd, prog_fd, main_fd, data_fd, i, val; > + LIBBPF_OPTS(bpf_test_run_opts, topts, > + .data_in = &pkt_v4, > + .data_size_in = sizeof(pkt_v4), > + .repeat = 1, > + ); > + > + obj = tailcall_bpf2bpf6__open_and_load(); > + if (!ASSERT_OK_PTR(obj, "open and load")) > + return; > + > + main_fd = bpf_program__fd(obj->progs.entry); > + if (!ASSERT_GE(main_fd, 0, "entry prog fd")) > + goto out; > + > + map_fd = bpf_map__fd(obj->maps.jmp_table); > + if (!ASSERT_GE(map_fd, 0, "jmp_table map fd")) > + goto out; > + > + prog_fd = bpf_program__fd(obj->progs.classifier_0); > + if (!ASSERT_GE(prog_fd, 0, "classifier_0 prog fd")) > + goto out; > + > + i = 0; > + err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); > + if (!ASSERT_OK(err, "jmp_table map update")) > + goto out; > + > + err = bpf_prog_test_run_opts(main_fd, &topts); > + ASSERT_OK(err, "entry prog test run"); > + ASSERT_EQ(topts.retval, 0, "tailcall retval"); > + > + data_fd = bpf_map__fd(obj->maps.bss); > + if (!ASSERT_GE(map_fd, 0, "bss map fd")) > + goto out; > + > + i = 0; > + err = bpf_map_lookup_elem(data_fd, &i, &val); > + ASSERT_OK(err, "bss map lookup"); > + ASSERT_EQ(val, 1, "done flag is set"); > + > +out: > + tailcall_bpf2bpf6__destroy(obj); > +} > + > void test_tailcalls(void) > { > if (test__start_subtest("tailcall_1")) > @@ -855,4 +908,6 @@ void test_tailcalls(void) > test_tailcall_bpf2bpf_4(false); > if (test__start_subtest("tailcall_bpf2bpf_5")) > test_tailcall_bpf2bpf_4(true); > + if (test__start_subtest("tailcall_bpf2bpf_6")) > + test_tailcall_bpf2bpf_6(); > } > diff --git a/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c > new file mode 100644 > index 000000000000..256de9bcc621 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c > @@ -0,0 +1,42 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include <linux/bpf.h> > +#include <bpf/bpf_helpers.h> > + > +#define __unused __attribute__((always_unused)) > + > +struct { > + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); > + __uint(max_entries, 1); > + __uint(key_size, sizeof(__u32)); > + __uint(value_size, sizeof(__u32)); > +} jmp_table SEC(".maps"); > + > +int done = 0; > + > +SEC("tc") > +int classifier_0(struct __sk_buff *skb __unused) > +{ > + done = 1; > + return 0; > +} Looks like this fails CI with: progs/tailcall_bpf2bpf6.c:17:40: error: unknown attribute 'always_unused' ignored [-Werror,-Wunknown-attributes] int classifier_0(struct __sk_buff *skb __unused) ^~~~~~~~ progs/tailcall_bpf2bpf6.c:5:33: note: expanded from macro '__unused' #define __unused __attribute__((always_unused)) ^~~~~~~~~~~~~ 1 error generated. make: *** [Makefile:509: /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tailcall_bpf2bpf6.o] Error 1 make: *** Waiting for unfinished jobs.... Error: Process completed with exit code 2.
On Thu, Jun 16, 2022 at 04:41 PM +02, Daniel Borkmann wrote: [...] > Looks like this fails CI with: > > progs/tailcall_bpf2bpf6.c:17:40: error: unknown attribute 'always_unused' ignored [-Werror,-Wunknown-attributes] > int classifier_0(struct __sk_buff *skb __unused) > ^~~~~~~~ > progs/tailcall_bpf2bpf6.c:5:33: note: expanded from macro '__unused' > #define __unused __attribute__((always_unused)) > ^~~~~~~~~~~~~ > 1 error generated. > make: *** [Makefile:509: /tmp/runner/work/bpf/bpf/tools/testing/selftests/bpf/tailcall_bpf2bpf6.o] Error 1 > make: *** Waiting for unfinished jobs.... > Error: Process completed with exit code 2. I will switch to __attribute__((unused)) and ignore what checkpatch says. Will respin. Thanks!
diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c index c4da87ec3ba4..19c70880cfb3 100644 --- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c +++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c @@ -831,6 +831,59 @@ static void test_tailcall_bpf2bpf_4(bool noise) bpf_object__close(obj); } +#include "tailcall_bpf2bpf6.skel.h" + +/* Tail call counting works even when there is data on stack which is + * not aligned to 8 bytes. + */ +static void test_tailcall_bpf2bpf_6(void) +{ + struct tailcall_bpf2bpf6 *obj; + int err, map_fd, prog_fd, main_fd, data_fd, i, val; + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 1, + ); + + obj = tailcall_bpf2bpf6__open_and_load(); + if (!ASSERT_OK_PTR(obj, "open and load")) + return; + + main_fd = bpf_program__fd(obj->progs.entry); + if (!ASSERT_GE(main_fd, 0, "entry prog fd")) + goto out; + + map_fd = bpf_map__fd(obj->maps.jmp_table); + if (!ASSERT_GE(map_fd, 0, "jmp_table map fd")) + goto out; + + prog_fd = bpf_program__fd(obj->progs.classifier_0); + if (!ASSERT_GE(prog_fd, 0, "classifier_0 prog fd")) + goto out; + + i = 0; + err = bpf_map_update_elem(map_fd, &i, &prog_fd, BPF_ANY); + if (!ASSERT_OK(err, "jmp_table map update")) + goto out; + + err = bpf_prog_test_run_opts(main_fd, &topts); + ASSERT_OK(err, "entry prog test run"); + ASSERT_EQ(topts.retval, 0, "tailcall retval"); + + data_fd = bpf_map__fd(obj->maps.bss); + if (!ASSERT_GE(map_fd, 0, "bss map fd")) + goto out; + + i = 0; + err = bpf_map_lookup_elem(data_fd, &i, &val); + ASSERT_OK(err, "bss map lookup"); + ASSERT_EQ(val, 1, "done flag is set"); + +out: + tailcall_bpf2bpf6__destroy(obj); +} + void test_tailcalls(void) { if (test__start_subtest("tailcall_1")) @@ -855,4 +908,6 @@ void test_tailcalls(void) test_tailcall_bpf2bpf_4(false); if (test__start_subtest("tailcall_bpf2bpf_5")) test_tailcall_bpf2bpf_4(true); + if (test__start_subtest("tailcall_bpf2bpf_6")) + test_tailcall_bpf2bpf_6(); } diff --git a/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c new file mode 100644 index 000000000000..256de9bcc621 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +#define __unused __attribute__((always_unused)) + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} jmp_table SEC(".maps"); + +int done = 0; + +SEC("tc") +int classifier_0(struct __sk_buff *skb __unused) +{ + done = 1; + return 0; +} + +static __noinline +int subprog_tail(struct __sk_buff *skb) +{ + /* Don't propagate the constant to the caller */ + volatile int ret = 1; + + bpf_tail_call_static(skb, &jmp_table, 0); + return ret; +} + +SEC("tc") +int entry(struct __sk_buff *skb) +{ + /* Have data on stack which size is not a multiple of 8 */ + volatile char arr[1] = {}; + + return subprog_tail(skb); +} + +char __license[] SEC("license") = "GPL";
Cover the case when tail call count needs to be passed from BPF function to BPF function, and the caller has data on stack. Specifically when the size of data allocated on BPF stack is not a multiple on 8. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> --- .../selftests/bpf/prog_tests/tailcalls.c | 55 +++++++++++++++++++ .../selftests/bpf/progs/tailcall_bpf2bpf6.c | 42 ++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/tailcall_bpf2bpf6.c