@@ -1084,6 +1084,26 @@ static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
.arg1_type = ARG_PTR_TO_CTX,
};
+BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
+{
+ const struct bpf_prog *prog;
+ int off = get_trampo_var_off(ctx, BPF_TRAMP_F_PROG_ID);
+
+ if (off < 0)
+ return 0;
+
+ prog = (const struct bpf_prog *)((u64 *)ctx)[-off];
+
+ return prog->aux->cookie;
+}
+
+static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
+ .func = bpf_get_attach_cookie_tracing,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_CTX,
+};
+
BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
{
#ifndef CONFIG_X86
@@ -1706,6 +1726,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
case BPF_FUNC_get_func_arg_cnt:
return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
+ case BPF_FUNC_get_attach_cookie:
+ return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
default:
fn = raw_tp_prog_func_proto(func_id, prog);
if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
@@ -231,6 +231,61 @@ static void pe_subtest(struct test_bpf_cookie *skel)
bpf_link__destroy(link);
}
+static void tracing_subtest(struct test_bpf_cookie *skel)
+{
+ __u64 cookie;
+ __u32 duration = 0, retval;
+ int prog_fd;
+ int fentry_fd = -1, fexit_fd = -1, fmod_ret_fd = -1;
+
+ skel->bss->fentry_res = 0;
+ skel->bss->fexit_res = 0;
+
+ cookie = 0x100000;
+ prog_fd = bpf_program__fd(skel->progs.fentry_test1);
+ if (!ASSERT_GE(prog_fd, 0, "fentry.prog_fd"))
+ return;
+ fentry_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie);
+ if (!ASSERT_GE(fentry_fd, 0, "fentry.open"))
+ return;
+
+ cookie = 0x200000;
+ prog_fd = bpf_program__fd(skel->progs.fexit_test1);
+ if (!ASSERT_GE(prog_fd, 0, "fexit.prog_fd"))
+ goto cleanup;
+ fexit_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie);
+ if (!ASSERT_GE(fexit_fd, 0, "fexit.open"))
+ goto cleanup;
+
+ cookie = 0x300000;
+ prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
+ if (!ASSERT_GE(prog_fd, 0, "fmod_ret.prog_fd"))
+ goto cleanup;
+ fmod_ret_fd = bpf_raw_tracepoint_cookie_open(NULL, prog_fd, cookie);
+ if (!ASSERT_GE(fmod_ret_fd, 0, "fmod_ret.opoen"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.fentry_test1);
+ bpf_prog_test_run(prog_fd, 1, NULL, 0,
+ NULL, NULL, &retval, &duration);
+
+ prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
+ bpf_prog_test_run(prog_fd, 1, NULL, 0,
+ NULL, NULL, &retval, &duration);
+
+ ASSERT_EQ(skel->bss->fentry_res, 0x100000, "fentry_res");
+ ASSERT_EQ(skel->bss->fexit_res, 0x200000, "fexit_res");
+ ASSERT_EQ(skel->bss->fmod_ret_res, 0x300000, "fmod_ret_res");
+
+cleanup:
+ if (fentry_fd >= 0)
+ close(fentry_fd);
+ if (fexit_fd >= 0)
+ close(fexit_fd);
+ if (fmod_ret_fd >= 0)
+ close(fmod_ret_fd);
+}
+
void test_bpf_cookie(void)
{
struct test_bpf_cookie *skel;
@@ -249,6 +304,8 @@ void test_bpf_cookie(void)
tp_subtest(skel);
if (test__start_subtest("perf_event"))
pe_subtest(skel);
+ if (test__start_subtest("tracing"))
+ tracing_subtest(skel);
test_bpf_cookie__destroy(skel);
}
@@ -14,6 +14,9 @@ int uprobe_res;
int uretprobe_res;
int tp_res;
int pe_res;
+int fentry_res;
+int fexit_res;
+int fmod_ret_res;
static void update(void *ctx, int *res)
{
@@ -82,4 +85,25 @@ int handle_pe(struct pt_regs *ctx)
return 0;
}
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(fentry_test1, int a)
+{
+ update(ctx, &fentry_res);
+ return 0;
+}
+
+SEC("fexit/bpf_fentry_test1")
+int BPF_PROG(fexit_test1, int a, int ret)
+{
+ update(ctx, &fexit_res);
+ return 0;
+}
+
+SEC("fmod_ret/bpf_modify_return_test")
+int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret)
+{
+ update(ctx, &fmod_ret_res);
+ return 1234;
+}
+
char _license[] SEC("license") = "GPL";
Implement bpf_get_attach_cookie() for fentry, fexit, and fmod_ret tracing programs. The cookie is from the bpf_prog from the program ID of the current BPF program. Signed-off-by: Kui-Feng Lee <kuifeng@fb.com> --- kernel/trace/bpf_trace.c | 22 +++++++ .../selftests/bpf/prog_tests/bpf_cookie.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/test_bpf_cookie.c | 24 ++++++++ 3 files changed, 103 insertions(+)