Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: selftests: Test fentry tracing a struct_ops program

This patch tests attaching an fentry prog to a struct_ops prog.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220330011502.2985292-1-kafai@fb.com

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
0a210af6 4a9c7bbe

+44
+23
tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c
··· 2 2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */ 3 3 #include <test_progs.h> 4 4 #include "dummy_st_ops.skel.h" 5 + #include "trace_dummy_st_ops.skel.h" 5 6 6 7 /* Need to keep consistent with definition in include/linux/bpf.h */ 7 8 struct bpf_dummy_ops_state { ··· 57 56 .ctx_in = args, 58 57 .ctx_size_in = sizeof(args), 59 58 ); 59 + struct trace_dummy_st_ops *trace_skel; 60 60 struct dummy_st_ops *skel; 61 61 int fd, err; 62 62 ··· 66 64 return; 67 65 68 66 fd = bpf_program__fd(skel->progs.test_1); 67 + 68 + trace_skel = trace_dummy_st_ops__open(); 69 + if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open")) 70 + goto done; 71 + 72 + err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1, 73 + fd, "test_1"); 74 + if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)")) 75 + goto done; 76 + 77 + err = trace_dummy_st_ops__load(trace_skel); 78 + if (!ASSERT_OK(err, "load(trace_skel)")) 79 + goto done; 80 + 81 + err = trace_dummy_st_ops__attach(trace_skel); 82 + if (!ASSERT_OK(err, "attach(trace_skel)")) 83 + goto done; 84 + 69 85 err = bpf_prog_test_run_opts(fd, &attr); 70 86 ASSERT_OK(err, "test_run"); 71 87 ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret"); 72 88 ASSERT_EQ(attr.retval, exp_retval, "test_ret"); 89 + ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val"); 73 90 91 + done: 74 92 dummy_st_ops__destroy(skel); 93 + trace_dummy_st_ops__destroy(trace_skel); 75 94 } 76 95 77 96 static void test_dummy_multiple_args(void)
+21
tools/testing/selftests/bpf/progs/trace_dummy_st_ops.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/bpf.h> 3 + #include <bpf/bpf_helpers.h> 4 + #include <bpf/bpf_tracing.h> 5 + 6 + int val = 0; 7 + 8 + SEC("fentry/test_1") 9 + int BPF_PROG(fentry_test_1, __u64 *st_ops_ctx) 10 + { 11 + __u64 state; 12 + 13 + /* Read the traced st_ops arg1 which is a pointer */ 14 + bpf_probe_read_kernel(&state, sizeof(__u64), (void *)st_ops_ctx); 15 + /* Read state->val */ 16 + bpf_probe_read_kernel(&val, sizeof(__u32), (void *)state); 17 + 18 + return 0; 19 + } 20 + 21 + char _license[] SEC("license") = "GPL";