Re: [PATCH 10/10] selftest/bpf: Add kprobe_multi test for bpf_cookie values

From: Andrii Nakryiko
Date: Fri Mar 04 2022 - 18:12:17 EST


On Tue, Feb 22, 2022 at 9:08 AM Jiri Olsa <jolsa@xxxxxxxxxx> wrote:
>
> Adding bpf_cookie test for programs attached by kprobe_multi links.
>
> Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
> ---
> .../selftests/bpf/prog_tests/bpf_cookie.c | 72 +++++++++++++++++++
> .../bpf/progs/kprobe_multi_bpf_cookie.c | 62 ++++++++++++++++
> 2 files changed, 134 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_bpf_cookie.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
> index cd10df6cd0fc..edfb9f8736c6 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
> @@ -7,6 +7,7 @@
> #include <unistd.h>
> #include <test_progs.h>
> #include "test_bpf_cookie.skel.h"
> +#include "kprobe_multi_bpf_cookie.skel.h"
>
> /* uprobe attach point */
> static void trigger_func(void)
> @@ -63,6 +64,75 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)
> bpf_link__destroy(retlink2);
> }
>
> +static void kprobe_multi_subtest(void)
> +{
> + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
> + int err, prog_fd, link1_fd = -1, link2_fd = -1;
> + LIBBPF_OPTS(bpf_test_run_opts, topts);

consistency ftw, LIBBPF_OPTS


> + struct kprobe_multi_bpf_cookie *skel = NULL;
> + __u64 addrs[8], cookies[8];
> +

[..]

> diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_bpf_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_bpf_cookie.c
> new file mode 100644
> index 000000000000..d6f8454ba093
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_bpf_cookie.c
> @@ -0,0 +1,62 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <linux/bpf.h>
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +extern const void bpf_fentry_test1 __ksym;
> +extern const void bpf_fentry_test2 __ksym;
> +extern const void bpf_fentry_test3 __ksym;
> +extern const void bpf_fentry_test4 __ksym;
> +extern const void bpf_fentry_test5 __ksym;
> +extern const void bpf_fentry_test6 __ksym;
> +extern const void bpf_fentry_test7 __ksym;
> +extern const void bpf_fentry_test8 __ksym;
> +
> +/* No tests, just to trigger bpf_fentry_test* through tracing test_run */
> +SEC("fentry/bpf_modify_return_test")
> +int BPF_PROG(test1)
> +{
> + return 0;
> +}
> +
> +__u64 test2_result = 0;
> +
> +SEC("kprobe.multi/bpf_fentry_tes??")
> +int test2(struct pt_regs *ctx)
> +{
> + __u64 cookie = bpf_get_attach_cookie(ctx);
> + __u64 addr = bpf_get_func_ip(ctx);
> +
> + test2_result += (const void *) addr == &bpf_fentry_test1 && cookie == 1;
> + test2_result += (const void *) addr == &bpf_fentry_test2 && cookie == 2;
> + test2_result += (const void *) addr == &bpf_fentry_test3 && cookie == 3;
> + test2_result += (const void *) addr == &bpf_fentry_test4 && cookie == 4;
> + test2_result += (const void *) addr == &bpf_fentry_test5 && cookie == 5;
> + test2_result += (const void *) addr == &bpf_fentry_test6 && cookie == 6;
> + test2_result += (const void *) addr == &bpf_fentry_test7 && cookie == 7;
> + test2_result += (const void *) addr == &bpf_fentry_test8 && cookie == 8;

this is not parallel mode friendly

let's filter by pid, but also it's best to do count locally and just
assign it (so that multiple calls of the program still produce the
same value, instead of constantly increasing global variable with each
run)


> +
> + return 0;
> +}
> +
> +__u64 test3_result = 0;
> +
> +SEC("kretprobe.multi/bpf_fentry_test*")
> +int test3(struct pt_regs *ctx)
> +{
> + __u64 cookie = bpf_get_attach_cookie(ctx);
> + __u64 addr = bpf_get_func_ip(ctx);
> +
> + test3_result += (const void *) addr == &bpf_fentry_test1 && cookie == 8;
> + test3_result += (const void *) addr == &bpf_fentry_test2 && cookie == 7;
> + test3_result += (const void *) addr == &bpf_fentry_test3 && cookie == 6;
> + test3_result += (const void *) addr == &bpf_fentry_test4 && cookie == 5;
> + test3_result += (const void *) addr == &bpf_fentry_test5 && cookie == 4;
> + test3_result += (const void *) addr == &bpf_fentry_test6 && cookie == 3;
> + test3_result += (const void *) addr == &bpf_fentry_test7 && cookie == 2;
> + test3_result += (const void *) addr == &bpf_fentry_test8 && cookie == 1;
> +
> + return 0;
> +}
> --
> 2.35.1
>