Skip to content

Commit

Permalink
bpf: selftests: Test fentry tracing a struct_ops program
Browse files Browse the repository at this point in the history
This patch tests attaching an fentry prog to a struct_ops prog.

Signed-off-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Acked-by: Yonghong Song <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
iamkafai authored and Alexei Starovoitov committed Mar 31, 2022
1 parent 4a9c7bb commit 0a210af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
#include <test_progs.h>
#include "dummy_st_ops.skel.h"
#include "trace_dummy_st_ops.skel.h"

/* Need to keep consistent with definition in include/linux/bpf.h */
struct bpf_dummy_ops_state {
Expand Down Expand Up @@ -56,6 +57,7 @@ static void test_dummy_init_ptr_arg(void)
.ctx_in = args,
.ctx_size_in = sizeof(args),
);
struct trace_dummy_st_ops *trace_skel;
struct dummy_st_ops *skel;
int fd, err;

Expand All @@ -64,12 +66,33 @@ static void test_dummy_init_ptr_arg(void)
return;

fd = bpf_program__fd(skel->progs.test_1);

trace_skel = trace_dummy_st_ops__open();
if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open"))
goto done;

err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1,
fd, "test_1");
if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)"))
goto done;

err = trace_dummy_st_ops__load(trace_skel);
if (!ASSERT_OK(err, "load(trace_skel)"))
goto done;

err = trace_dummy_st_ops__attach(trace_skel);
if (!ASSERT_OK(err, "attach(trace_skel)"))
goto done;

err = bpf_prog_test_run_opts(fd, &attr);
ASSERT_OK(err, "test_run");
ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret");
ASSERT_EQ(attr.retval, exp_retval, "test_ret");
ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val");

done:
dummy_st_ops__destroy(skel);
trace_dummy_st_ops__destroy(trace_skel);
}

static void test_dummy_multiple_args(void)
Expand Down
21 changes: 21 additions & 0 deletions tools/testing/selftests/bpf/progs/trace_dummy_st_ops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

int val = 0;

SEC("fentry/test_1")
int BPF_PROG(fentry_test_1, __u64 *st_ops_ctx)
{
__u64 state;

/* Read the traced st_ops arg1 which is a pointer */
bpf_probe_read_kernel(&state, sizeof(__u64), (void *)st_ops_ctx);
/* Read state->val */
bpf_probe_read_kernel(&val, sizeof(__u32), (void *)state);

return 0;
}

char _license[] SEC("license") = "GPL";

0 comments on commit 0a210af

Please sign in to comment.